0002-usb-fotg210-Collect-pieces-of-dual-mode-controller.patch 455 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990
  1. From 30367636930864f71b2bd462adedcf8484313864 Mon Sep 17 00:00:00 2001
  2. From: Linus Walleij <[email protected]>
  3. Date: Sun, 23 Oct 2022 16:47:06 +0200
  4. Subject: [PATCH 02/29] usb: fotg210: Collect pieces of dual mode controller
  5. The Faraday FOTG210 is a dual-mode OTG USB controller that can
  6. act as host, peripheral or both. To be able to probe from one
  7. hardware description and to follow the pattern of other dual-
  8. mode controllers such as MUSB or MTU3 we need to collect the
  9. two, currently completely separate drivers in the same
  10. directory.
  11. After this, users need to select the main symbol USB_FOTG210
  12. and then each respective subdriver. We pave the road to
  13. compile both drivers into the same kernel and select the
  14. one we want to use at probe() time, and possibly add OTG
  15. support in the end.
  16. This patch doesn't do much more than create the new symbol
  17. and collect the drivers in one place. We also add a comment
  18. for the section of dual-mode controllers in the Kconfig
  19. file so people can see what these selections are about.
  20. Also add myself as maintainer as there has been little
  21. response on my patches to these drivers.
  22. Cc: Fabian Vogt <[email protected]>
  23. Cc: Yuan-Hsin Chen <[email protected]>
  24. Cc: Felipe Balbi <[email protected]>
  25. Signed-off-by: Linus Walleij <[email protected]>
  26. Link: https://lore.kernel.org/r/[email protected]
  27. Signed-off-by: Greg Kroah-Hartman <[email protected]>
  28. ---
  29. --- a/drivers/usb/Kconfig
  30. +++ b/drivers/usb/Kconfig
  31. @@ -111,8 +111,12 @@ source "drivers/usb/usbip/Kconfig"
  32. endif
  33. +comment "USB dual-mode controller drivers"
  34. +
  35. source "drivers/usb/cdns3/Kconfig"
  36. +source "drivers/usb/fotg210/Kconfig"
  37. +
  38. source "drivers/usb/mtu3/Kconfig"
  39. source "drivers/usb/musb/Kconfig"
  40. --- a/drivers/usb/Makefile
  41. +++ b/drivers/usb/Makefile
  42. @@ -17,6 +17,8 @@ obj-$(CONFIG_USB_CDNS_SUPPORT) += cdns3/
  43. obj-$(CONFIG_USB_CDNS3) += cdns3/
  44. obj-$(CONFIG_USB_CDNSP_PCI) += cdns3/
  45. +obj-$(CONFIG_USB_FOTG210) += fotg210/
  46. +
  47. obj-$(CONFIG_USB_MON) += mon/
  48. obj-$(CONFIG_USB_MTU3) += mtu3/
  49. --- /dev/null
  50. +++ b/drivers/usb/fotg210/Kconfig
  51. @@ -0,0 +1,36 @@
  52. +# SPDX-License-Identifier: GPL-2.0
  53. +
  54. +config USB_FOTG210
  55. + tristate "Faraday FOTG210 USB2 Dual Role controller"
  56. + depends on USB || USB_GADGET
  57. + depends on HAS_DMA && HAS_IOMEM
  58. + default ARCH_GEMINI
  59. + help
  60. + Faraday FOTG210 is a dual-mode USB controller that can act
  61. + in both host controller and peripheral controller mode.
  62. +
  63. +if USB_FOTG210
  64. +
  65. +config USB_FOTG210_HCD
  66. + tristate "Faraday FOTG210 USB Host Controller support"
  67. + depends on USB
  68. + help
  69. + Faraday FOTG210 is an OTG controller which can be configured as
  70. + an USB2.0 host. It is designed to meet USB2.0 EHCI specification
  71. + with minor modification.
  72. +
  73. + To compile this driver as a module, choose M here: the
  74. + module will be called fotg210-hcd.
  75. +
  76. +config USB_FOTG210_UDC
  77. + depends on USB_GADGET
  78. + tristate "Faraday FOTG210 USB Peripheral Controller support"
  79. + help
  80. + Faraday USB2.0 OTG controller which can be configured as
  81. + high speed or full speed USB device. This driver suppports
  82. + Bulk Transfer so far.
  83. +
  84. + Say "y" to link the driver statically, or "m" to build a
  85. + dynamically linked module called "fotg210-udc".
  86. +
  87. +endif
  88. --- /dev/null
  89. +++ b/drivers/usb/fotg210/Makefile
  90. @@ -0,0 +1,3 @@
  91. +# SPDX-License-Identifier: GPL-2.0
  92. +obj-$(CONFIG_USB_FOTG210_HCD) += fotg210-hcd.o
  93. +obj-$(CONFIG_USB_FOTG210_UDC) += fotg210-udc.o
  94. --- a/drivers/usb/host/fotg210-hcd.c
  95. +++ /dev/null
  96. @@ -1,5724 +0,0 @@
  97. -// SPDX-License-Identifier: GPL-2.0+
  98. -/* Faraday FOTG210 EHCI-like driver
  99. - *
  100. - * Copyright (c) 2013 Faraday Technology Corporation
  101. - *
  102. - * Author: Yuan-Hsin Chen <[email protected]>
  103. - * Feng-Hsin Chiang <[email protected]>
  104. - * Po-Yu Chuang <[email protected]>
  105. - *
  106. - * Most of code borrowed from the Linux-3.7 EHCI driver
  107. - */
  108. -#include <linux/module.h>
  109. -#include <linux/of.h>
  110. -#include <linux/device.h>
  111. -#include <linux/dmapool.h>
  112. -#include <linux/kernel.h>
  113. -#include <linux/delay.h>
  114. -#include <linux/ioport.h>
  115. -#include <linux/sched.h>
  116. -#include <linux/vmalloc.h>
  117. -#include <linux/errno.h>
  118. -#include <linux/init.h>
  119. -#include <linux/hrtimer.h>
  120. -#include <linux/list.h>
  121. -#include <linux/interrupt.h>
  122. -#include <linux/usb.h>
  123. -#include <linux/usb/hcd.h>
  124. -#include <linux/moduleparam.h>
  125. -#include <linux/dma-mapping.h>
  126. -#include <linux/debugfs.h>
  127. -#include <linux/slab.h>
  128. -#include <linux/uaccess.h>
  129. -#include <linux/platform_device.h>
  130. -#include <linux/io.h>
  131. -#include <linux/iopoll.h>
  132. -#include <linux/clk.h>
  133. -
  134. -#include <asm/byteorder.h>
  135. -#include <asm/irq.h>
  136. -#include <asm/unaligned.h>
  137. -
  138. -#define DRIVER_AUTHOR "Yuan-Hsin Chen"
  139. -#define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
  140. -static const char hcd_name[] = "fotg210_hcd";
  141. -
  142. -#undef FOTG210_URB_TRACE
  143. -#define FOTG210_STATS
  144. -
  145. -/* magic numbers that can affect system performance */
  146. -#define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  147. -#define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  148. -#define FOTG210_TUNE_RL_TT 0
  149. -#define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  150. -#define FOTG210_TUNE_MULT_TT 1
  151. -
  152. -/* Some drivers think it's safe to schedule isochronous transfers more than 256
  153. - * ms into the future (partly as a result of an old bug in the scheduling
  154. - * code). In an attempt to avoid trouble, we will use a minimum scheduling
  155. - * length of 512 frames instead of 256.
  156. - */
  157. -#define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
  158. -
  159. -/* Initial IRQ latency: faster than hw default */
  160. -static int log2_irq_thresh; /* 0 to 6 */
  161. -module_param(log2_irq_thresh, int, S_IRUGO);
  162. -MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  163. -
  164. -/* initial park setting: slower than hw default */
  165. -static unsigned park;
  166. -module_param(park, uint, S_IRUGO);
  167. -MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
  168. -
  169. -/* for link power management(LPM) feature */
  170. -static unsigned int hird;
  171. -module_param(hird, int, S_IRUGO);
  172. -MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
  173. -
  174. -#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  175. -
  176. -#include "fotg210.h"
  177. -
  178. -#define fotg210_dbg(fotg210, fmt, args...) \
  179. - dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  180. -#define fotg210_err(fotg210, fmt, args...) \
  181. - dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  182. -#define fotg210_info(fotg210, fmt, args...) \
  183. - dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  184. -#define fotg210_warn(fotg210, fmt, args...) \
  185. - dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  186. -
  187. -/* check the values in the HCSPARAMS register (host controller _Structural_
  188. - * parameters) see EHCI spec, Table 2-4 for each value
  189. - */
  190. -static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
  191. -{
  192. - u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
  193. -
  194. - fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
  195. - HCS_N_PORTS(params));
  196. -}
  197. -
  198. -/* check the values in the HCCPARAMS register (host controller _Capability_
  199. - * parameters) see EHCI Spec, Table 2-5 for each value
  200. - */
  201. -static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
  202. -{
  203. - u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  204. -
  205. - fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label,
  206. - params,
  207. - HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
  208. - HCC_CANPARK(params) ? " park" : "");
  209. -}
  210. -
  211. -static void __maybe_unused
  212. -dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
  213. -{
  214. - fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
  215. - hc32_to_cpup(fotg210, &qtd->hw_next),
  216. - hc32_to_cpup(fotg210, &qtd->hw_alt_next),
  217. - hc32_to_cpup(fotg210, &qtd->hw_token),
  218. - hc32_to_cpup(fotg210, &qtd->hw_buf[0]));
  219. - if (qtd->hw_buf[1])
  220. - fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
  221. - hc32_to_cpup(fotg210, &qtd->hw_buf[1]),
  222. - hc32_to_cpup(fotg210, &qtd->hw_buf[2]),
  223. - hc32_to_cpup(fotg210, &qtd->hw_buf[3]),
  224. - hc32_to_cpup(fotg210, &qtd->hw_buf[4]));
  225. -}
  226. -
  227. -static void __maybe_unused
  228. -dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  229. -{
  230. - struct fotg210_qh_hw *hw = qh->hw;
  231. -
  232. - fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label, qh,
  233. - hw->hw_next, hw->hw_info1, hw->hw_info2,
  234. - hw->hw_current);
  235. -
  236. - dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
  237. -}
  238. -
  239. -static void __maybe_unused
  240. -dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
  241. -{
  242. - fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n", label,
  243. - itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next),
  244. - itd->urb);
  245. -
  246. - fotg210_dbg(fotg210,
  247. - " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  248. - hc32_to_cpu(fotg210, itd->hw_transaction[0]),
  249. - hc32_to_cpu(fotg210, itd->hw_transaction[1]),
  250. - hc32_to_cpu(fotg210, itd->hw_transaction[2]),
  251. - hc32_to_cpu(fotg210, itd->hw_transaction[3]),
  252. - hc32_to_cpu(fotg210, itd->hw_transaction[4]),
  253. - hc32_to_cpu(fotg210, itd->hw_transaction[5]),
  254. - hc32_to_cpu(fotg210, itd->hw_transaction[6]),
  255. - hc32_to_cpu(fotg210, itd->hw_transaction[7]));
  256. -
  257. - fotg210_dbg(fotg210,
  258. - " buf: %08x %08x %08x %08x %08x %08x %08x\n",
  259. - hc32_to_cpu(fotg210, itd->hw_bufp[0]),
  260. - hc32_to_cpu(fotg210, itd->hw_bufp[1]),
  261. - hc32_to_cpu(fotg210, itd->hw_bufp[2]),
  262. - hc32_to_cpu(fotg210, itd->hw_bufp[3]),
  263. - hc32_to_cpu(fotg210, itd->hw_bufp[4]),
  264. - hc32_to_cpu(fotg210, itd->hw_bufp[5]),
  265. - hc32_to_cpu(fotg210, itd->hw_bufp[6]));
  266. -
  267. - fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n",
  268. - itd->index[0], itd->index[1], itd->index[2],
  269. - itd->index[3], itd->index[4], itd->index[5],
  270. - itd->index[6], itd->index[7]);
  271. -}
  272. -
  273. -static int __maybe_unused
  274. -dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
  275. -{
  276. - return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
  277. - label, label[0] ? " " : "", status,
  278. - (status & STS_ASS) ? " Async" : "",
  279. - (status & STS_PSS) ? " Periodic" : "",
  280. - (status & STS_RECL) ? " Recl" : "",
  281. - (status & STS_HALT) ? " Halt" : "",
  282. - (status & STS_IAA) ? " IAA" : "",
  283. - (status & STS_FATAL) ? " FATAL" : "",
  284. - (status & STS_FLR) ? " FLR" : "",
  285. - (status & STS_PCD) ? " PCD" : "",
  286. - (status & STS_ERR) ? " ERR" : "",
  287. - (status & STS_INT) ? " INT" : "");
  288. -}
  289. -
  290. -static int __maybe_unused
  291. -dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
  292. -{
  293. - return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
  294. - label, label[0] ? " " : "", enable,
  295. - (enable & STS_IAA) ? " IAA" : "",
  296. - (enable & STS_FATAL) ? " FATAL" : "",
  297. - (enable & STS_FLR) ? " FLR" : "",
  298. - (enable & STS_PCD) ? " PCD" : "",
  299. - (enable & STS_ERR) ? " ERR" : "",
  300. - (enable & STS_INT) ? " INT" : "");
  301. -}
  302. -
  303. -static const char *const fls_strings[] = { "1024", "512", "256", "??" };
  304. -
  305. -static int dbg_command_buf(char *buf, unsigned len, const char *label,
  306. - u32 command)
  307. -{
  308. - return scnprintf(buf, len,
  309. - "%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s",
  310. - label, label[0] ? " " : "", command,
  311. - (command & CMD_PARK) ? " park" : "(park)",
  312. - CMD_PARK_CNT(command),
  313. - (command >> 16) & 0x3f,
  314. - (command & CMD_IAAD) ? " IAAD" : "",
  315. - (command & CMD_ASE) ? " Async" : "",
  316. - (command & CMD_PSE) ? " Periodic" : "",
  317. - fls_strings[(command >> 2) & 0x3],
  318. - (command & CMD_RESET) ? " Reset" : "",
  319. - (command & CMD_RUN) ? "RUN" : "HALT");
  320. -}
  321. -
  322. -static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
  323. - u32 status)
  324. -{
  325. - char *sig;
  326. -
  327. - /* signaling state */
  328. - switch (status & (3 << 10)) {
  329. - case 0 << 10:
  330. - sig = "se0";
  331. - break;
  332. - case 1 << 10:
  333. - sig = "k";
  334. - break; /* low speed */
  335. - case 2 << 10:
  336. - sig = "j";
  337. - break;
  338. - default:
  339. - sig = "?";
  340. - break;
  341. - }
  342. -
  343. - scnprintf(buf, len, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s",
  344. - label, label[0] ? " " : "", port, status,
  345. - status >> 25, /*device address */
  346. - sig,
  347. - (status & PORT_RESET) ? " RESET" : "",
  348. - (status & PORT_SUSPEND) ? " SUSPEND" : "",
  349. - (status & PORT_RESUME) ? " RESUME" : "",
  350. - (status & PORT_PEC) ? " PEC" : "",
  351. - (status & PORT_PE) ? " PE" : "",
  352. - (status & PORT_CSC) ? " CSC" : "",
  353. - (status & PORT_CONNECT) ? " CONNECT" : "");
  354. -
  355. - return buf;
  356. -}
  357. -
  358. -/* functions have the "wrong" filename when they're output... */
  359. -#define dbg_status(fotg210, label, status) { \
  360. - char _buf[80]; \
  361. - dbg_status_buf(_buf, sizeof(_buf), label, status); \
  362. - fotg210_dbg(fotg210, "%s\n", _buf); \
  363. -}
  364. -
  365. -#define dbg_cmd(fotg210, label, command) { \
  366. - char _buf[80]; \
  367. - dbg_command_buf(_buf, sizeof(_buf), label, command); \
  368. - fotg210_dbg(fotg210, "%s\n", _buf); \
  369. -}
  370. -
  371. -#define dbg_port(fotg210, label, port, status) { \
  372. - char _buf[80]; \
  373. - fotg210_dbg(fotg210, "%s\n", \
  374. - dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
  375. -}
  376. -
  377. -/* troubleshooting help: expose state in debugfs */
  378. -static int debug_async_open(struct inode *, struct file *);
  379. -static int debug_periodic_open(struct inode *, struct file *);
  380. -static int debug_registers_open(struct inode *, struct file *);
  381. -static int debug_async_open(struct inode *, struct file *);
  382. -
  383. -static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
  384. -static int debug_close(struct inode *, struct file *);
  385. -
  386. -static const struct file_operations debug_async_fops = {
  387. - .owner = THIS_MODULE,
  388. - .open = debug_async_open,
  389. - .read = debug_output,
  390. - .release = debug_close,
  391. - .llseek = default_llseek,
  392. -};
  393. -static const struct file_operations debug_periodic_fops = {
  394. - .owner = THIS_MODULE,
  395. - .open = debug_periodic_open,
  396. - .read = debug_output,
  397. - .release = debug_close,
  398. - .llseek = default_llseek,
  399. -};
  400. -static const struct file_operations debug_registers_fops = {
  401. - .owner = THIS_MODULE,
  402. - .open = debug_registers_open,
  403. - .read = debug_output,
  404. - .release = debug_close,
  405. - .llseek = default_llseek,
  406. -};
  407. -
  408. -static struct dentry *fotg210_debug_root;
  409. -
  410. -struct debug_buffer {
  411. - ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
  412. - struct usb_bus *bus;
  413. - struct mutex mutex; /* protect filling of buffer */
  414. - size_t count; /* number of characters filled into buffer */
  415. - char *output_buf;
  416. - size_t alloc_size;
  417. -};
  418. -
  419. -static inline char speed_char(u32 scratch)
  420. -{
  421. - switch (scratch & (3 << 12)) {
  422. - case QH_FULL_SPEED:
  423. - return 'f';
  424. -
  425. - case QH_LOW_SPEED:
  426. - return 'l';
  427. -
  428. - case QH_HIGH_SPEED:
  429. - return 'h';
  430. -
  431. - default:
  432. - return '?';
  433. - }
  434. -}
  435. -
  436. -static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
  437. -{
  438. - __u32 v = hc32_to_cpu(fotg210, token);
  439. -
  440. - if (v & QTD_STS_ACTIVE)
  441. - return '*';
  442. - if (v & QTD_STS_HALT)
  443. - return '-';
  444. - if (!IS_SHORT_READ(v))
  445. - return ' ';
  446. - /* tries to advance through hw_alt_next */
  447. - return '/';
  448. -}
  449. -
  450. -static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
  451. - char **nextp, unsigned *sizep)
  452. -{
  453. - u32 scratch;
  454. - u32 hw_curr;
  455. - struct fotg210_qtd *td;
  456. - unsigned temp;
  457. - unsigned size = *sizep;
  458. - char *next = *nextp;
  459. - char mark;
  460. - __le32 list_end = FOTG210_LIST_END(fotg210);
  461. - struct fotg210_qh_hw *hw = qh->hw;
  462. -
  463. - if (hw->hw_qtd_next == list_end) /* NEC does this */
  464. - mark = '@';
  465. - else
  466. - mark = token_mark(fotg210, hw->hw_token);
  467. - if (mark == '/') { /* qh_alt_next controls qh advance? */
  468. - if ((hw->hw_alt_next & QTD_MASK(fotg210)) ==
  469. - fotg210->async->hw->hw_alt_next)
  470. - mark = '#'; /* blocked */
  471. - else if (hw->hw_alt_next == list_end)
  472. - mark = '.'; /* use hw_qtd_next */
  473. - /* else alt_next points to some other qtd */
  474. - }
  475. - scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
  476. - hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0;
  477. - temp = scnprintf(next, size,
  478. - "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
  479. - qh, scratch & 0x007f,
  480. - speed_char(scratch),
  481. - (scratch >> 8) & 0x000f,
  482. - scratch, hc32_to_cpup(fotg210, &hw->hw_info2),
  483. - hc32_to_cpup(fotg210, &hw->hw_token), mark,
  484. - (cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token)
  485. - ? "data1" : "data0",
  486. - (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
  487. - size -= temp;
  488. - next += temp;
  489. -
  490. - /* hc may be modifying the list as we read it ... */
  491. - list_for_each_entry(td, &qh->qtd_list, qtd_list) {
  492. - scratch = hc32_to_cpup(fotg210, &td->hw_token);
  493. - mark = ' ';
  494. - if (hw_curr == td->qtd_dma)
  495. - mark = '*';
  496. - else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
  497. - mark = '+';
  498. - else if (QTD_LENGTH(scratch)) {
  499. - if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
  500. - mark = '#';
  501. - else if (td->hw_alt_next != list_end)
  502. - mark = '/';
  503. - }
  504. - temp = snprintf(next, size,
  505. - "\n\t%p%c%s len=%d %08x urb %p",
  506. - td, mark, ({ char *tmp;
  507. - switch ((scratch>>8)&0x03) {
  508. - case 0:
  509. - tmp = "out";
  510. - break;
  511. - case 1:
  512. - tmp = "in";
  513. - break;
  514. - case 2:
  515. - tmp = "setup";
  516. - break;
  517. - default:
  518. - tmp = "?";
  519. - break;
  520. - } tmp; }),
  521. - (scratch >> 16) & 0x7fff,
  522. - scratch,
  523. - td->urb);
  524. - if (size < temp)
  525. - temp = size;
  526. - size -= temp;
  527. - next += temp;
  528. - }
  529. -
  530. - temp = snprintf(next, size, "\n");
  531. - if (size < temp)
  532. - temp = size;
  533. -
  534. - size -= temp;
  535. - next += temp;
  536. -
  537. - *sizep = size;
  538. - *nextp = next;
  539. -}
  540. -
  541. -static ssize_t fill_async_buffer(struct debug_buffer *buf)
  542. -{
  543. - struct usb_hcd *hcd;
  544. - struct fotg210_hcd *fotg210;
  545. - unsigned long flags;
  546. - unsigned temp, size;
  547. - char *next;
  548. - struct fotg210_qh *qh;
  549. -
  550. - hcd = bus_to_hcd(buf->bus);
  551. - fotg210 = hcd_to_fotg210(hcd);
  552. - next = buf->output_buf;
  553. - size = buf->alloc_size;
  554. -
  555. - *next = 0;
  556. -
  557. - /* dumps a snapshot of the async schedule.
  558. - * usually empty except for long-term bulk reads, or head.
  559. - * one QH per line, and TDs we know about
  560. - */
  561. - spin_lock_irqsave(&fotg210->lock, flags);
  562. - for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
  563. - qh = qh->qh_next.qh)
  564. - qh_lines(fotg210, qh, &next, &size);
  565. - if (fotg210->async_unlink && size > 0) {
  566. - temp = scnprintf(next, size, "\nunlink =\n");
  567. - size -= temp;
  568. - next += temp;
  569. -
  570. - for (qh = fotg210->async_unlink; size > 0 && qh;
  571. - qh = qh->unlink_next)
  572. - qh_lines(fotg210, qh, &next, &size);
  573. - }
  574. - spin_unlock_irqrestore(&fotg210->lock, flags);
  575. -
  576. - return strlen(buf->output_buf);
  577. -}
  578. -
  579. -/* count tds, get ep direction */
  580. -static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
  581. - struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
  582. -{
  583. - u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
  584. - struct fotg210_qtd *qtd;
  585. - char *type = "";
  586. - unsigned temp = 0;
  587. -
  588. - /* count tds, get ep direction */
  589. - list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  590. - temp++;
  591. - switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
  592. - case 0:
  593. - type = "out";
  594. - continue;
  595. - case 1:
  596. - type = "in";
  597. - continue;
  598. - }
  599. - }
  600. -
  601. - return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)",
  602. - speed_char(scratch), scratch & 0x007f,
  603. - (scratch >> 8) & 0x000f, type, qh->usecs,
  604. - qh->c_usecs, temp, (scratch >> 16) & 0x7ff);
  605. -}
  606. -
  607. -#define DBG_SCHED_LIMIT 64
  608. -static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
  609. -{
  610. - struct usb_hcd *hcd;
  611. - struct fotg210_hcd *fotg210;
  612. - unsigned long flags;
  613. - union fotg210_shadow p, *seen;
  614. - unsigned temp, size, seen_count;
  615. - char *next;
  616. - unsigned i;
  617. - __hc32 tag;
  618. -
  619. - seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
  620. - if (!seen)
  621. - return 0;
  622. -
  623. - seen_count = 0;
  624. -
  625. - hcd = bus_to_hcd(buf->bus);
  626. - fotg210 = hcd_to_fotg210(hcd);
  627. - next = buf->output_buf;
  628. - size = buf->alloc_size;
  629. -
  630. - temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
  631. - size -= temp;
  632. - next += temp;
  633. -
  634. - /* dump a snapshot of the periodic schedule.
  635. - * iso changes, interrupt usually doesn't.
  636. - */
  637. - spin_lock_irqsave(&fotg210->lock, flags);
  638. - for (i = 0; i < fotg210->periodic_size; i++) {
  639. - p = fotg210->pshadow[i];
  640. - if (likely(!p.ptr))
  641. - continue;
  642. -
  643. - tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);
  644. -
  645. - temp = scnprintf(next, size, "%4d: ", i);
  646. - size -= temp;
  647. - next += temp;
  648. -
  649. - do {
  650. - struct fotg210_qh_hw *hw;
  651. -
  652. - switch (hc32_to_cpu(fotg210, tag)) {
  653. - case Q_TYPE_QH:
  654. - hw = p.qh->hw;
  655. - temp = scnprintf(next, size, " qh%d-%04x/%p",
  656. - p.qh->period,
  657. - hc32_to_cpup(fotg210,
  658. - &hw->hw_info2)
  659. - /* uframe masks */
  660. - & (QH_CMASK | QH_SMASK),
  661. - p.qh);
  662. - size -= temp;
  663. - next += temp;
  664. - /* don't repeat what follows this qh */
  665. - for (temp = 0; temp < seen_count; temp++) {
  666. - if (seen[temp].ptr != p.ptr)
  667. - continue;
  668. - if (p.qh->qh_next.ptr) {
  669. - temp = scnprintf(next, size,
  670. - " ...");
  671. - size -= temp;
  672. - next += temp;
  673. - }
  674. - break;
  675. - }
  676. - /* show more info the first time around */
  677. - if (temp == seen_count) {
  678. - temp = output_buf_tds_dir(next,
  679. - fotg210, hw,
  680. - p.qh, size);
  681. -
  682. - if (seen_count < DBG_SCHED_LIMIT)
  683. - seen[seen_count++].qh = p.qh;
  684. - } else
  685. - temp = 0;
  686. - tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
  687. - p = p.qh->qh_next;
  688. - break;
  689. - case Q_TYPE_FSTN:
  690. - temp = scnprintf(next, size,
  691. - " fstn-%8x/%p",
  692. - p.fstn->hw_prev, p.fstn);
  693. - tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
  694. - p = p.fstn->fstn_next;
  695. - break;
  696. - case Q_TYPE_ITD:
  697. - temp = scnprintf(next, size,
  698. - " itd/%p", p.itd);
  699. - tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
  700. - p = p.itd->itd_next;
  701. - break;
  702. - }
  703. - size -= temp;
  704. - next += temp;
  705. - } while (p.ptr);
  706. -
  707. - temp = scnprintf(next, size, "\n");
  708. - size -= temp;
  709. - next += temp;
  710. - }
  711. - spin_unlock_irqrestore(&fotg210->lock, flags);
  712. - kfree(seen);
  713. -
  714. - return buf->alloc_size - size;
  715. -}
  716. -#undef DBG_SCHED_LIMIT
  717. -
  718. -static const char *rh_state_string(struct fotg210_hcd *fotg210)
  719. -{
  720. - switch (fotg210->rh_state) {
  721. - case FOTG210_RH_HALTED:
  722. - return "halted";
  723. - case FOTG210_RH_SUSPENDED:
  724. - return "suspended";
  725. - case FOTG210_RH_RUNNING:
  726. - return "running";
  727. - case FOTG210_RH_STOPPING:
  728. - return "stopping";
  729. - }
  730. - return "?";
  731. -}
  732. -
  733. -static ssize_t fill_registers_buffer(struct debug_buffer *buf)
  734. -{
  735. - struct usb_hcd *hcd;
  736. - struct fotg210_hcd *fotg210;
  737. - unsigned long flags;
  738. - unsigned temp, size, i;
  739. - char *next, scratch[80];
  740. - static const char fmt[] = "%*s\n";
  741. - static const char label[] = "";
  742. -
  743. - hcd = bus_to_hcd(buf->bus);
  744. - fotg210 = hcd_to_fotg210(hcd);
  745. - next = buf->output_buf;
  746. - size = buf->alloc_size;
  747. -
  748. - spin_lock_irqsave(&fotg210->lock, flags);
  749. -
  750. - if (!HCD_HW_ACCESSIBLE(hcd)) {
  751. - size = scnprintf(next, size,
  752. - "bus %s, device %s\n"
  753. - "%s\n"
  754. - "SUSPENDED(no register access)\n",
  755. - hcd->self.controller->bus->name,
  756. - dev_name(hcd->self.controller),
  757. - hcd->product_desc);
  758. - goto done;
  759. - }
  760. -
  761. - /* Capability Registers */
  762. - i = HC_VERSION(fotg210, fotg210_readl(fotg210,
  763. - &fotg210->caps->hc_capbase));
  764. - temp = scnprintf(next, size,
  765. - "bus %s, device %s\n"
  766. - "%s\n"
  767. - "EHCI %x.%02x, rh state %s\n",
  768. - hcd->self.controller->bus->name,
  769. - dev_name(hcd->self.controller),
  770. - hcd->product_desc,
  771. - i >> 8, i & 0x0ff, rh_state_string(fotg210));
  772. - size -= temp;
  773. - next += temp;
  774. -
  775. - /* FIXME interpret both types of params */
  776. - i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
  777. - temp = scnprintf(next, size, "structural params 0x%08x\n", i);
  778. - size -= temp;
  779. - next += temp;
  780. -
  781. - i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  782. - temp = scnprintf(next, size, "capability params 0x%08x\n", i);
  783. - size -= temp;
  784. - next += temp;
  785. -
  786. - /* Operational Registers */
  787. - temp = dbg_status_buf(scratch, sizeof(scratch), label,
  788. - fotg210_readl(fotg210, &fotg210->regs->status));
  789. - temp = scnprintf(next, size, fmt, temp, scratch);
  790. - size -= temp;
  791. - next += temp;
  792. -
  793. - temp = dbg_command_buf(scratch, sizeof(scratch), label,
  794. - fotg210_readl(fotg210, &fotg210->regs->command));
  795. - temp = scnprintf(next, size, fmt, temp, scratch);
  796. - size -= temp;
  797. - next += temp;
  798. -
  799. - temp = dbg_intr_buf(scratch, sizeof(scratch), label,
  800. - fotg210_readl(fotg210, &fotg210->regs->intr_enable));
  801. - temp = scnprintf(next, size, fmt, temp, scratch);
  802. - size -= temp;
  803. - next += temp;
  804. -
  805. - temp = scnprintf(next, size, "uframe %04x\n",
  806. - fotg210_read_frame_index(fotg210));
  807. - size -= temp;
  808. - next += temp;
  809. -
  810. - if (fotg210->async_unlink) {
  811. - temp = scnprintf(next, size, "async unlink qh %p\n",
  812. - fotg210->async_unlink);
  813. - size -= temp;
  814. - next += temp;
  815. - }
  816. -
  817. -#ifdef FOTG210_STATS
  818. - temp = scnprintf(next, size,
  819. - "irq normal %ld err %ld iaa %ld(lost %ld)\n",
  820. - fotg210->stats.normal, fotg210->stats.error,
  821. - fotg210->stats.iaa, fotg210->stats.lost_iaa);
  822. - size -= temp;
  823. - next += temp;
  824. -
  825. - temp = scnprintf(next, size, "complete %ld unlink %ld\n",
  826. - fotg210->stats.complete, fotg210->stats.unlink);
  827. - size -= temp;
  828. - next += temp;
  829. -#endif
  830. -
  831. -done:
  832. - spin_unlock_irqrestore(&fotg210->lock, flags);
  833. -
  834. - return buf->alloc_size - size;
  835. -}
  836. -
  837. -static struct debug_buffer
  838. -*alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
  839. -{
  840. - struct debug_buffer *buf;
  841. -
  842. - buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
  843. -
  844. - if (buf) {
  845. - buf->bus = bus;
  846. - buf->fill_func = fill_func;
  847. - mutex_init(&buf->mutex);
  848. - buf->alloc_size = PAGE_SIZE;
  849. - }
  850. -
  851. - return buf;
  852. -}
  853. -
  854. -static int fill_buffer(struct debug_buffer *buf)
  855. -{
  856. - int ret = 0;
  857. -
  858. - if (!buf->output_buf)
  859. - buf->output_buf = vmalloc(buf->alloc_size);
  860. -
  861. - if (!buf->output_buf) {
  862. - ret = -ENOMEM;
  863. - goto out;
  864. - }
  865. -
  866. - ret = buf->fill_func(buf);
  867. -
  868. - if (ret >= 0) {
  869. - buf->count = ret;
  870. - ret = 0;
  871. - }
  872. -
  873. -out:
  874. - return ret;
  875. -}
  876. -
  877. -static ssize_t debug_output(struct file *file, char __user *user_buf,
  878. - size_t len, loff_t *offset)
  879. -{
  880. - struct debug_buffer *buf = file->private_data;
  881. - int ret = 0;
  882. -
  883. - mutex_lock(&buf->mutex);
  884. - if (buf->count == 0) {
  885. - ret = fill_buffer(buf);
  886. - if (ret != 0) {
  887. - mutex_unlock(&buf->mutex);
  888. - goto out;
  889. - }
  890. - }
  891. - mutex_unlock(&buf->mutex);
  892. -
  893. - ret = simple_read_from_buffer(user_buf, len, offset,
  894. - buf->output_buf, buf->count);
  895. -
  896. -out:
  897. - return ret;
  898. -
  899. -}
  900. -
  901. -static int debug_close(struct inode *inode, struct file *file)
  902. -{
  903. - struct debug_buffer *buf = file->private_data;
  904. -
  905. - if (buf) {
  906. - vfree(buf->output_buf);
  907. - kfree(buf);
  908. - }
  909. -
  910. - return 0;
  911. -}
  912. -static int debug_async_open(struct inode *inode, struct file *file)
  913. -{
  914. - file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
  915. -
  916. - return file->private_data ? 0 : -ENOMEM;
  917. -}
  918. -
  919. -static int debug_periodic_open(struct inode *inode, struct file *file)
  920. -{
  921. - struct debug_buffer *buf;
  922. -
  923. - buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
  924. - if (!buf)
  925. - return -ENOMEM;
  926. -
  927. - buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
  928. - file->private_data = buf;
  929. - return 0;
  930. -}
  931. -
  932. -static int debug_registers_open(struct inode *inode, struct file *file)
  933. -{
  934. - file->private_data = alloc_buffer(inode->i_private,
  935. - fill_registers_buffer);
  936. -
  937. - return file->private_data ? 0 : -ENOMEM;
  938. -}
  939. -
  940. -static inline void create_debug_files(struct fotg210_hcd *fotg210)
  941. -{
  942. - struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
  943. - struct dentry *root;
  944. -
  945. - root = debugfs_create_dir(bus->bus_name, fotg210_debug_root);
  946. -
  947. - debugfs_create_file("async", S_IRUGO, root, bus, &debug_async_fops);
  948. - debugfs_create_file("periodic", S_IRUGO, root, bus,
  949. - &debug_periodic_fops);
  950. - debugfs_create_file("registers", S_IRUGO, root, bus,
  951. - &debug_registers_fops);
  952. -}
  953. -
  954. -static inline void remove_debug_files(struct fotg210_hcd *fotg210)
  955. -{
  956. - struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
  957. -
  958. - debugfs_lookup_and_remove(bus->bus_name, fotg210_debug_root);
  959. -}
  960. -
  961. -/* handshake - spin reading hc until handshake completes or fails
  962. - * @ptr: address of hc register to be read
  963. - * @mask: bits to look at in result of read
  964. - * @done: value of those bits when handshake succeeds
  965. - * @usec: timeout in microseconds
  966. - *
  967. - * Returns negative errno, or zero on success
  968. - *
  969. - * Success happens when the "mask" bits have the specified value (hardware
  970. - * handshake done). There are two failure modes: "usec" have passed (major
  971. - * hardware flakeout), or the register reads as all-ones (hardware removed).
  972. - *
  973. - * That last failure should_only happen in cases like physical cardbus eject
  974. - * before driver shutdown. But it also seems to be caused by bugs in cardbus
  975. - * bridge shutdown: shutting down the bridge before the devices using it.
  976. - */
  977. -static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
  978. - u32 mask, u32 done, int usec)
  979. -{
  980. - u32 result;
  981. - int ret;
  982. -
  983. - ret = readl_poll_timeout_atomic(ptr, result,
  984. - ((result & mask) == done ||
  985. - result == U32_MAX), 1, usec);
  986. - if (result == U32_MAX) /* card removed */
  987. - return -ENODEV;
  988. -
  989. - return ret;
  990. -}
  991. -
  992. -/* Force HC to halt state from unknown (EHCI spec section 2.3).
  993. - * Must be called with interrupts enabled and the lock not held.
  994. - */
  995. -static int fotg210_halt(struct fotg210_hcd *fotg210)
  996. -{
  997. - u32 temp;
  998. -
  999. - spin_lock_irq(&fotg210->lock);
  1000. -
  1001. - /* disable any irqs left enabled by previous code */
  1002. - fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  1003. -
  1004. - /*
  1005. - * This routine gets called during probe before fotg210->command
  1006. - * has been initialized, so we can't rely on its value.
  1007. - */
  1008. - fotg210->command &= ~CMD_RUN;
  1009. - temp = fotg210_readl(fotg210, &fotg210->regs->command);
  1010. - temp &= ~(CMD_RUN | CMD_IAAD);
  1011. - fotg210_writel(fotg210, temp, &fotg210->regs->command);
  1012. -
  1013. - spin_unlock_irq(&fotg210->lock);
  1014. - synchronize_irq(fotg210_to_hcd(fotg210)->irq);
  1015. -
  1016. - return handshake(fotg210, &fotg210->regs->status,
  1017. - STS_HALT, STS_HALT, 16 * 125);
  1018. -}
  1019. -
  1020. -/* Reset a non-running (STS_HALT == 1) controller.
  1021. - * Must be called with interrupts enabled and the lock not held.
  1022. - */
  1023. -static int fotg210_reset(struct fotg210_hcd *fotg210)
  1024. -{
  1025. - int retval;
  1026. - u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
  1027. -
  1028. - /* If the EHCI debug controller is active, special care must be
  1029. - * taken before and after a host controller reset
  1030. - */
  1031. - if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
  1032. - fotg210->debug = NULL;
  1033. -
  1034. - command |= CMD_RESET;
  1035. - dbg_cmd(fotg210, "reset", command);
  1036. - fotg210_writel(fotg210, command, &fotg210->regs->command);
  1037. - fotg210->rh_state = FOTG210_RH_HALTED;
  1038. - fotg210->next_statechange = jiffies;
  1039. - retval = handshake(fotg210, &fotg210->regs->command,
  1040. - CMD_RESET, 0, 250 * 1000);
  1041. -
  1042. - if (retval)
  1043. - return retval;
  1044. -
  1045. - if (fotg210->debug)
  1046. - dbgp_external_startup(fotg210_to_hcd(fotg210));
  1047. -
  1048. - fotg210->port_c_suspend = fotg210->suspended_ports =
  1049. - fotg210->resuming_ports = 0;
  1050. - return retval;
  1051. -}
  1052. -
  1053. -/* Idle the controller (turn off the schedules).
  1054. - * Must be called with interrupts enabled and the lock not held.
  1055. - */
  1056. -static void fotg210_quiesce(struct fotg210_hcd *fotg210)
  1057. -{
  1058. - u32 temp;
  1059. -
  1060. - if (fotg210->rh_state != FOTG210_RH_RUNNING)
  1061. - return;
  1062. -
  1063. - /* wait for any schedule enables/disables to take effect */
  1064. - temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
  1065. - handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
  1066. - 16 * 125);
  1067. -
  1068. - /* then disable anything that's still active */
  1069. - spin_lock_irq(&fotg210->lock);
  1070. - fotg210->command &= ~(CMD_ASE | CMD_PSE);
  1071. - fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  1072. - spin_unlock_irq(&fotg210->lock);
  1073. -
  1074. - /* hardware can take 16 microframes to turn off ... */
  1075. - handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
  1076. - 16 * 125);
  1077. -}
  1078. -
  1079. -static void end_unlink_async(struct fotg210_hcd *fotg210);
  1080. -static void unlink_empty_async(struct fotg210_hcd *fotg210);
  1081. -static void fotg210_work(struct fotg210_hcd *fotg210);
  1082. -static void start_unlink_intr(struct fotg210_hcd *fotg210,
  1083. - struct fotg210_qh *qh);
  1084. -static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  1085. -
  1086. -/* Set a bit in the USBCMD register */
  1087. -static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
  1088. -{
  1089. - fotg210->command |= bit;
  1090. - fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  1091. -
  1092. - /* unblock posted write */
  1093. - fotg210_readl(fotg210, &fotg210->regs->command);
  1094. -}
  1095. -
  1096. -/* Clear a bit in the USBCMD register */
  1097. -static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
  1098. -{
  1099. - fotg210->command &= ~bit;
  1100. - fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  1101. -
  1102. - /* unblock posted write */
  1103. - fotg210_readl(fotg210, &fotg210->regs->command);
  1104. -}
  1105. -
  1106. -/* EHCI timer support... Now using hrtimers.
  1107. - *
  1108. - * Lots of different events are triggered from fotg210->hrtimer. Whenever
  1109. - * the timer routine runs, it checks each possible event; events that are
  1110. - * currently enabled and whose expiration time has passed get handled.
  1111. - * The set of enabled events is stored as a collection of bitflags in
  1112. - * fotg210->enabled_hrtimer_events, and they are numbered in order of
  1113. - * increasing delay values (ranging between 1 ms and 100 ms).
  1114. - *
  1115. - * Rather than implementing a sorted list or tree of all pending events,
  1116. - * we keep track only of the lowest-numbered pending event, in
  1117. - * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
  1118. - * expiration time is set to the timeout value for this event.
  1119. - *
  1120. - * As a result, events might not get handled right away; the actual delay
  1121. - * could be anywhere up to twice the requested delay. This doesn't
  1122. - * matter, because none of the events are especially time-critical. The
  1123. - * ones that matter most all have a delay of 1 ms, so they will be
  1124. - * handled after 2 ms at most, which is okay. In addition to this, we
  1125. - * allow for an expiration range of 1 ms.
  1126. - */
  1127. -
  1128. -/* Delay lengths for the hrtimer event types.
  1129. - * Keep this list sorted by delay length, in the same order as
  1130. - * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
  1131. - */
  1132. -static unsigned event_delays_ns[] = {
  1133. - 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */
  1134. - 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */
  1135. - 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */
  1136. - 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */
  1137. - 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */
  1138. - 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
  1139. - 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */
  1140. - 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
  1141. - 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */
  1142. - 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */
  1143. -};
  1144. -
  1145. -/* Enable a pending hrtimer event */
  1146. -static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
  1147. - bool resched)
  1148. -{
  1149. - ktime_t *timeout = &fotg210->hr_timeouts[event];
  1150. -
  1151. - if (resched)
  1152. - *timeout = ktime_add(ktime_get(), event_delays_ns[event]);
  1153. - fotg210->enabled_hrtimer_events |= (1 << event);
  1154. -
  1155. - /* Track only the lowest-numbered pending event */
  1156. - if (event < fotg210->next_hrtimer_event) {
  1157. - fotg210->next_hrtimer_event = event;
  1158. - hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
  1159. - NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  1160. - }
  1161. -}
  1162. -
  1163. -
  1164. -/* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  1165. -static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
  1166. -{
  1167. - unsigned actual, want;
  1168. -
  1169. - /* Don't enable anything if the controller isn't running (e.g., died) */
  1170. - if (fotg210->rh_state != FOTG210_RH_RUNNING)
  1171. - return;
  1172. -
  1173. - want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
  1174. - actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;
  1175. -
  1176. - if (want != actual) {
  1177. -
  1178. - /* Poll again later, but give up after about 20 ms */
  1179. - if (fotg210->ASS_poll_count++ < 20) {
  1180. - fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
  1181. - true);
  1182. - return;
  1183. - }
  1184. - fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
  1185. - want, actual);
  1186. - }
  1187. - fotg210->ASS_poll_count = 0;
  1188. -
  1189. - /* The status is up-to-date; restart or stop the schedule as needed */
  1190. - if (want == 0) { /* Stopped */
  1191. - if (fotg210->async_count > 0)
  1192. - fotg210_set_command_bit(fotg210, CMD_ASE);
  1193. -
  1194. - } else { /* Running */
  1195. - if (fotg210->async_count == 0) {
  1196. -
  1197. - /* Turn off the schedule after a while */
  1198. - fotg210_enable_event(fotg210,
  1199. - FOTG210_HRTIMER_DISABLE_ASYNC,
  1200. - true);
  1201. - }
  1202. - }
  1203. -}
  1204. -
  1205. -/* Turn off the async schedule after a brief delay */
  1206. -static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
  1207. -{
  1208. - fotg210_clear_command_bit(fotg210, CMD_ASE);
  1209. -}
  1210. -
  1211. -
  1212. -/* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  1213. -static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
  1214. -{
  1215. - unsigned actual, want;
  1216. -
  1217. - /* Don't do anything if the controller isn't running (e.g., died) */
  1218. - if (fotg210->rh_state != FOTG210_RH_RUNNING)
  1219. - return;
  1220. -
  1221. - want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
  1222. - actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;
  1223. -
  1224. - if (want != actual) {
  1225. -
  1226. - /* Poll again later, but give up after about 20 ms */
  1227. - if (fotg210->PSS_poll_count++ < 20) {
  1228. - fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
  1229. - true);
  1230. - return;
  1231. - }
  1232. - fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
  1233. - want, actual);
  1234. - }
  1235. - fotg210->PSS_poll_count = 0;
  1236. -
  1237. - /* The status is up-to-date; restart or stop the schedule as needed */
  1238. - if (want == 0) { /* Stopped */
  1239. - if (fotg210->periodic_count > 0)
  1240. - fotg210_set_command_bit(fotg210, CMD_PSE);
  1241. -
  1242. - } else { /* Running */
  1243. - if (fotg210->periodic_count == 0) {
  1244. -
  1245. - /* Turn off the schedule after a while */
  1246. - fotg210_enable_event(fotg210,
  1247. - FOTG210_HRTIMER_DISABLE_PERIODIC,
  1248. - true);
  1249. - }
  1250. - }
  1251. -}
  1252. -
  1253. -/* Turn off the periodic schedule after a brief delay */
  1254. -static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
  1255. -{
  1256. - fotg210_clear_command_bit(fotg210, CMD_PSE);
  1257. -}
  1258. -
  1259. -
  1260. -/* Poll the STS_HALT status bit; see when a dead controller stops */
  1261. -static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
  1262. -{
  1263. - if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
  1264. -
  1265. - /* Give up after a few milliseconds */
  1266. - if (fotg210->died_poll_count++ < 5) {
  1267. - /* Try again later */
  1268. - fotg210_enable_event(fotg210,
  1269. - FOTG210_HRTIMER_POLL_DEAD, true);
  1270. - return;
  1271. - }
  1272. - fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
  1273. - }
  1274. -
  1275. - /* Clean up the mess */
  1276. - fotg210->rh_state = FOTG210_RH_HALTED;
  1277. - fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  1278. - fotg210_work(fotg210);
  1279. - end_unlink_async(fotg210);
  1280. -
  1281. - /* Not in process context, so don't try to reset the controller */
  1282. -}
  1283. -
  1284. -
  1285. -/* Handle unlinked interrupt QHs once they are gone from the hardware */
  1286. -static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
  1287. -{
  1288. - bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
  1289. -
  1290. - /*
  1291. - * Process all the QHs on the intr_unlink list that were added
  1292. - * before the current unlink cycle began. The list is in
  1293. - * temporal order, so stop when we reach the first entry in the
  1294. - * current cycle. But if the root hub isn't running then
  1295. - * process all the QHs on the list.
  1296. - */
  1297. - fotg210->intr_unlinking = true;
  1298. - while (fotg210->intr_unlink) {
  1299. - struct fotg210_qh *qh = fotg210->intr_unlink;
  1300. -
  1301. - if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
  1302. - break;
  1303. - fotg210->intr_unlink = qh->unlink_next;
  1304. - qh->unlink_next = NULL;
  1305. - end_unlink_intr(fotg210, qh);
  1306. - }
  1307. -
  1308. - /* Handle remaining entries later */
  1309. - if (fotg210->intr_unlink) {
  1310. - fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
  1311. - true);
  1312. - ++fotg210->intr_unlink_cycle;
  1313. - }
  1314. - fotg210->intr_unlinking = false;
  1315. -}
  1316. -
  1317. -
  1318. -/* Start another free-iTDs/siTDs cycle */
  1319. -static void start_free_itds(struct fotg210_hcd *fotg210)
  1320. -{
  1321. - if (!(fotg210->enabled_hrtimer_events &
  1322. - BIT(FOTG210_HRTIMER_FREE_ITDS))) {
  1323. - fotg210->last_itd_to_free = list_entry(
  1324. - fotg210->cached_itd_list.prev,
  1325. - struct fotg210_itd, itd_list);
  1326. - fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
  1327. - }
  1328. -}
  1329. -
  1330. -/* Wait for controller to stop using old iTDs and siTDs */
  1331. -static void end_free_itds(struct fotg210_hcd *fotg210)
  1332. -{
  1333. - struct fotg210_itd *itd, *n;
  1334. -
  1335. - if (fotg210->rh_state < FOTG210_RH_RUNNING)
  1336. - fotg210->last_itd_to_free = NULL;
  1337. -
  1338. - list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
  1339. - list_del(&itd->itd_list);
  1340. - dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
  1341. - if (itd == fotg210->last_itd_to_free)
  1342. - break;
  1343. - }
  1344. -
  1345. - if (!list_empty(&fotg210->cached_itd_list))
  1346. - start_free_itds(fotg210);
  1347. -}
  1348. -
  1349. -
  1350. -/* Handle lost (or very late) IAA interrupts */
  1351. -static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
  1352. -{
  1353. - if (fotg210->rh_state != FOTG210_RH_RUNNING)
  1354. - return;
  1355. -
  1356. - /*
  1357. - * Lost IAA irqs wedge things badly; seen first with a vt8235.
  1358. - * So we need this watchdog, but must protect it against both
  1359. - * (a) SMP races against real IAA firing and retriggering, and
  1360. - * (b) clean HC shutdown, when IAA watchdog was pending.
  1361. - */
  1362. - if (fotg210->async_iaa) {
  1363. - u32 cmd, status;
  1364. -
  1365. - /* If we get here, IAA is *REALLY* late. It's barely
  1366. - * conceivable that the system is so busy that CMD_IAAD
  1367. - * is still legitimately set, so let's be sure it's
  1368. - * clear before we read STS_IAA. (The HC should clear
  1369. - * CMD_IAAD when it sets STS_IAA.)
  1370. - */
  1371. - cmd = fotg210_readl(fotg210, &fotg210->regs->command);
  1372. -
  1373. - /*
  1374. - * If IAA is set here it either legitimately triggered
  1375. - * after the watchdog timer expired (_way_ late, so we'll
  1376. - * still count it as lost) ... or a silicon erratum:
  1377. - * - VIA seems to set IAA without triggering the IRQ;
  1378. - * - IAAD potentially cleared without setting IAA.
  1379. - */
  1380. - status = fotg210_readl(fotg210, &fotg210->regs->status);
  1381. - if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
  1382. - INCR(fotg210->stats.lost_iaa);
  1383. - fotg210_writel(fotg210, STS_IAA,
  1384. - &fotg210->regs->status);
  1385. - }
  1386. -
  1387. - fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
  1388. - status, cmd);
  1389. - end_unlink_async(fotg210);
  1390. - }
  1391. -}
  1392. -
  1393. -
  1394. -/* Enable the I/O watchdog, if appropriate */
  1395. -static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
  1396. -{
  1397. - /* Not needed if the controller isn't running or it's already enabled */
  1398. - if (fotg210->rh_state != FOTG210_RH_RUNNING ||
  1399. - (fotg210->enabled_hrtimer_events &
  1400. - BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
  1401. - return;
  1402. -
  1403. - /*
  1404. - * Isochronous transfers always need the watchdog.
  1405. - * For other sorts we use it only if the flag is set.
  1406. - */
  1407. - if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog &&
  1408. - fotg210->async_count + fotg210->intr_count > 0))
  1409. - fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG,
  1410. - true);
  1411. -}
  1412. -
  1413. -
  1414. -/* Handler functions for the hrtimer event types.
  1415. - * Keep this array in the same order as the event types indexed by
  1416. - * enum fotg210_hrtimer_event in fotg210.h.
  1417. - */
  1418. -static void (*event_handlers[])(struct fotg210_hcd *) = {
  1419. - fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */
  1420. - fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */
  1421. - fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */
  1422. - fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */
  1423. - end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */
  1424. - unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
  1425. - fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */
  1426. - fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
  1427. - fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */
  1428. - fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */
  1429. -};
  1430. -
  1431. -static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
  1432. -{
  1433. - struct fotg210_hcd *fotg210 =
  1434. - container_of(t, struct fotg210_hcd, hrtimer);
  1435. - ktime_t now;
  1436. - unsigned long events;
  1437. - unsigned long flags;
  1438. - unsigned e;
  1439. -
  1440. - spin_lock_irqsave(&fotg210->lock, flags);
  1441. -
  1442. - events = fotg210->enabled_hrtimer_events;
  1443. - fotg210->enabled_hrtimer_events = 0;
  1444. - fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
  1445. -
  1446. - /*
  1447. - * Check each pending event. If its time has expired, handle
  1448. - * the event; otherwise re-enable it.
  1449. - */
  1450. - now = ktime_get();
  1451. - for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) {
  1452. - if (ktime_compare(now, fotg210->hr_timeouts[e]) >= 0)
  1453. - event_handlers[e](fotg210);
  1454. - else
  1455. - fotg210_enable_event(fotg210, e, false);
  1456. - }
  1457. -
  1458. - spin_unlock_irqrestore(&fotg210->lock, flags);
  1459. - return HRTIMER_NORESTART;
  1460. -}
  1461. -
  1462. -#define fotg210_bus_suspend NULL
  1463. -#define fotg210_bus_resume NULL
  1464. -
  1465. -static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
  1466. - u32 __iomem *status_reg, int port_status)
  1467. -{
  1468. - if (!(port_status & PORT_CONNECT))
  1469. - return port_status;
  1470. -
  1471. - /* if reset finished and it's still not enabled -- handoff */
  1472. - if (!(port_status & PORT_PE))
  1473. - /* with integrated TT, there's nobody to hand it to! */
  1474. - fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
  1475. - index + 1);
  1476. - else
  1477. - fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
  1478. - index + 1);
  1479. -
  1480. - return port_status;
  1481. -}
  1482. -
  1483. -
  1484. -/* build "status change" packet (one or two bytes) from HC registers */
  1485. -
  1486. -static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
  1487. -{
  1488. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  1489. - u32 temp, status;
  1490. - u32 mask;
  1491. - int retval = 1;
  1492. - unsigned long flags;
  1493. -
  1494. - /* init status to no-changes */
  1495. - buf[0] = 0;
  1496. -
  1497. - /* Inform the core about resumes-in-progress by returning
  1498. - * a non-zero value even if there are no status changes.
  1499. - */
  1500. - status = fotg210->resuming_ports;
  1501. -
  1502. - mask = PORT_CSC | PORT_PEC;
  1503. - /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
  1504. -
  1505. - /* no hub change reports (bit 0) for now (power, ...) */
  1506. -
  1507. - /* port N changes (bit N)? */
  1508. - spin_lock_irqsave(&fotg210->lock, flags);
  1509. -
  1510. - temp = fotg210_readl(fotg210, &fotg210->regs->port_status);
  1511. -
  1512. - /*
  1513. - * Return status information even for ports with OWNER set.
  1514. - * Otherwise hub_wq wouldn't see the disconnect event when a
  1515. - * high-speed device is switched over to the companion
  1516. - * controller by the user.
  1517. - */
  1518. -
  1519. - if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend) ||
  1520. - (fotg210->reset_done[0] &&
  1521. - time_after_eq(jiffies, fotg210->reset_done[0]))) {
  1522. - buf[0] |= 1 << 1;
  1523. - status = STS_PCD;
  1524. - }
  1525. - /* FIXME autosuspend idle root hubs */
  1526. - spin_unlock_irqrestore(&fotg210->lock, flags);
  1527. - return status ? retval : 0;
  1528. -}
  1529. -
  1530. -static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210,
  1531. - struct usb_hub_descriptor *desc)
  1532. -{
  1533. - int ports = HCS_N_PORTS(fotg210->hcs_params);
  1534. - u16 temp;
  1535. -
  1536. - desc->bDescriptorType = USB_DT_HUB;
  1537. - desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */
  1538. - desc->bHubContrCurrent = 0;
  1539. -
  1540. - desc->bNbrPorts = ports;
  1541. - temp = 1 + (ports / 8);
  1542. - desc->bDescLength = 7 + 2 * temp;
  1543. -
  1544. - /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  1545. - memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  1546. - memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  1547. -
  1548. - temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
  1549. - temp |= HUB_CHAR_NO_LPSM; /* no power switching */
  1550. - desc->wHubCharacteristics = cpu_to_le16(temp);
  1551. -}
  1552. -
  1553. -static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1554. - u16 wIndex, char *buf, u16 wLength)
  1555. -{
  1556. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  1557. - int ports = HCS_N_PORTS(fotg210->hcs_params);
  1558. - u32 __iomem *status_reg = &fotg210->regs->port_status;
  1559. - u32 temp, temp1, status;
  1560. - unsigned long flags;
  1561. - int retval = 0;
  1562. - unsigned selector;
  1563. -
  1564. - /*
  1565. - * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  1566. - * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  1567. - * (track current state ourselves) ... blink for diagnostics,
  1568. - * power, "this is the one", etc. EHCI spec supports this.
  1569. - */
  1570. -
  1571. - spin_lock_irqsave(&fotg210->lock, flags);
  1572. - switch (typeReq) {
  1573. - case ClearHubFeature:
  1574. - switch (wValue) {
  1575. - case C_HUB_LOCAL_POWER:
  1576. - case C_HUB_OVER_CURRENT:
  1577. - /* no hub-wide feature/status flags */
  1578. - break;
  1579. - default:
  1580. - goto error;
  1581. - }
  1582. - break;
  1583. - case ClearPortFeature:
  1584. - if (!wIndex || wIndex > ports)
  1585. - goto error;
  1586. - wIndex--;
  1587. - temp = fotg210_readl(fotg210, status_reg);
  1588. - temp &= ~PORT_RWC_BITS;
  1589. -
  1590. - /*
  1591. - * Even if OWNER is set, so the port is owned by the
  1592. - * companion controller, hub_wq needs to be able to clear
  1593. - * the port-change status bits (especially
  1594. - * USB_PORT_STAT_C_CONNECTION).
  1595. - */
  1596. -
  1597. - switch (wValue) {
  1598. - case USB_PORT_FEAT_ENABLE:
  1599. - fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
  1600. - break;
  1601. - case USB_PORT_FEAT_C_ENABLE:
  1602. - fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
  1603. - break;
  1604. - case USB_PORT_FEAT_SUSPEND:
  1605. - if (temp & PORT_RESET)
  1606. - goto error;
  1607. - if (!(temp & PORT_SUSPEND))
  1608. - break;
  1609. - if ((temp & PORT_PE) == 0)
  1610. - goto error;
  1611. -
  1612. - /* resume signaling for 20 msec */
  1613. - fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
  1614. - fotg210->reset_done[wIndex] = jiffies
  1615. - + msecs_to_jiffies(USB_RESUME_TIMEOUT);
  1616. - break;
  1617. - case USB_PORT_FEAT_C_SUSPEND:
  1618. - clear_bit(wIndex, &fotg210->port_c_suspend);
  1619. - break;
  1620. - case USB_PORT_FEAT_C_CONNECTION:
  1621. - fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
  1622. - break;
  1623. - case USB_PORT_FEAT_C_OVER_CURRENT:
  1624. - fotg210_writel(fotg210, temp | OTGISR_OVC,
  1625. - &fotg210->regs->otgisr);
  1626. - break;
  1627. - case USB_PORT_FEAT_C_RESET:
  1628. - /* GetPortStatus clears reset */
  1629. - break;
  1630. - default:
  1631. - goto error;
  1632. - }
  1633. - fotg210_readl(fotg210, &fotg210->regs->command);
  1634. - break;
  1635. - case GetHubDescriptor:
  1636. - fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
  1637. - buf);
  1638. - break;
  1639. - case GetHubStatus:
  1640. - /* no hub-wide feature/status flags */
  1641. - memset(buf, 0, 4);
  1642. - /*cpu_to_le32s ((u32 *) buf); */
  1643. - break;
  1644. - case GetPortStatus:
  1645. - if (!wIndex || wIndex > ports)
  1646. - goto error;
  1647. - wIndex--;
  1648. - status = 0;
  1649. - temp = fotg210_readl(fotg210, status_reg);
  1650. -
  1651. - /* wPortChange bits */
  1652. - if (temp & PORT_CSC)
  1653. - status |= USB_PORT_STAT_C_CONNECTION << 16;
  1654. - if (temp & PORT_PEC)
  1655. - status |= USB_PORT_STAT_C_ENABLE << 16;
  1656. -
  1657. - temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
  1658. - if (temp1 & OTGISR_OVC)
  1659. - status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  1660. -
  1661. - /* whoever resumes must GetPortStatus to complete it!! */
  1662. - if (temp & PORT_RESUME) {
  1663. -
  1664. - /* Remote Wakeup received? */
  1665. - if (!fotg210->reset_done[wIndex]) {
  1666. - /* resume signaling for 20 msec */
  1667. - fotg210->reset_done[wIndex] = jiffies
  1668. - + msecs_to_jiffies(20);
  1669. - /* check the port again */
  1670. - mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
  1671. - fotg210->reset_done[wIndex]);
  1672. - }
  1673. -
  1674. - /* resume completed? */
  1675. - else if (time_after_eq(jiffies,
  1676. - fotg210->reset_done[wIndex])) {
  1677. - clear_bit(wIndex, &fotg210->suspended_ports);
  1678. - set_bit(wIndex, &fotg210->port_c_suspend);
  1679. - fotg210->reset_done[wIndex] = 0;
  1680. -
  1681. - /* stop resume signaling */
  1682. - temp = fotg210_readl(fotg210, status_reg);
  1683. - fotg210_writel(fotg210, temp &
  1684. - ~(PORT_RWC_BITS | PORT_RESUME),
  1685. - status_reg);
  1686. - clear_bit(wIndex, &fotg210->resuming_ports);
  1687. - retval = handshake(fotg210, status_reg,
  1688. - PORT_RESUME, 0, 2000);/* 2ms */
  1689. - if (retval != 0) {
  1690. - fotg210_err(fotg210,
  1691. - "port %d resume error %d\n",
  1692. - wIndex + 1, retval);
  1693. - goto error;
  1694. - }
  1695. - temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  1696. - }
  1697. - }
  1698. -
  1699. - /* whoever resets must GetPortStatus to complete it!! */
  1700. - if ((temp & PORT_RESET) && time_after_eq(jiffies,
  1701. - fotg210->reset_done[wIndex])) {
  1702. - status |= USB_PORT_STAT_C_RESET << 16;
  1703. - fotg210->reset_done[wIndex] = 0;
  1704. - clear_bit(wIndex, &fotg210->resuming_ports);
  1705. -
  1706. - /* force reset to complete */
  1707. - fotg210_writel(fotg210,
  1708. - temp & ~(PORT_RWC_BITS | PORT_RESET),
  1709. - status_reg);
  1710. - /* REVISIT: some hardware needs 550+ usec to clear
  1711. - * this bit; seems too long to spin routinely...
  1712. - */
  1713. - retval = handshake(fotg210, status_reg,
  1714. - PORT_RESET, 0, 1000);
  1715. - if (retval != 0) {
  1716. - fotg210_err(fotg210, "port %d reset error %d\n",
  1717. - wIndex + 1, retval);
  1718. - goto error;
  1719. - }
  1720. -
  1721. - /* see what we found out */
  1722. - temp = check_reset_complete(fotg210, wIndex, status_reg,
  1723. - fotg210_readl(fotg210, status_reg));
  1724. -
  1725. - /* restart schedule */
  1726. - fotg210->command |= CMD_RUN;
  1727. - fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  1728. - }
  1729. -
  1730. - if (!(temp & (PORT_RESUME|PORT_RESET))) {
  1731. - fotg210->reset_done[wIndex] = 0;
  1732. - clear_bit(wIndex, &fotg210->resuming_ports);
  1733. - }
  1734. -
  1735. - /* transfer dedicated ports to the companion hc */
  1736. - if ((temp & PORT_CONNECT) &&
  1737. - test_bit(wIndex, &fotg210->companion_ports)) {
  1738. - temp &= ~PORT_RWC_BITS;
  1739. - fotg210_writel(fotg210, temp, status_reg);
  1740. - fotg210_dbg(fotg210, "port %d --> companion\n",
  1741. - wIndex + 1);
  1742. - temp = fotg210_readl(fotg210, status_reg);
  1743. - }
  1744. -
  1745. - /*
  1746. - * Even if OWNER is set, there's no harm letting hub_wq
  1747. - * see the wPortStatus values (they should all be 0 except
  1748. - * for PORT_POWER anyway).
  1749. - */
  1750. -
  1751. - if (temp & PORT_CONNECT) {
  1752. - status |= USB_PORT_STAT_CONNECTION;
  1753. - status |= fotg210_port_speed(fotg210, temp);
  1754. - }
  1755. - if (temp & PORT_PE)
  1756. - status |= USB_PORT_STAT_ENABLE;
  1757. -
  1758. - /* maybe the port was unsuspended without our knowledge */
  1759. - if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  1760. - status |= USB_PORT_STAT_SUSPEND;
  1761. - } else if (test_bit(wIndex, &fotg210->suspended_ports)) {
  1762. - clear_bit(wIndex, &fotg210->suspended_ports);
  1763. - clear_bit(wIndex, &fotg210->resuming_ports);
  1764. - fotg210->reset_done[wIndex] = 0;
  1765. - if (temp & PORT_PE)
  1766. - set_bit(wIndex, &fotg210->port_c_suspend);
  1767. - }
  1768. -
  1769. - temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
  1770. - if (temp1 & OTGISR_OVC)
  1771. - status |= USB_PORT_STAT_OVERCURRENT;
  1772. - if (temp & PORT_RESET)
  1773. - status |= USB_PORT_STAT_RESET;
  1774. - if (test_bit(wIndex, &fotg210->port_c_suspend))
  1775. - status |= USB_PORT_STAT_C_SUSPEND << 16;
  1776. -
  1777. - if (status & ~0xffff) /* only if wPortChange is interesting */
  1778. - dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
  1779. - put_unaligned_le32(status, buf);
  1780. - break;
  1781. - case SetHubFeature:
  1782. - switch (wValue) {
  1783. - case C_HUB_LOCAL_POWER:
  1784. - case C_HUB_OVER_CURRENT:
  1785. - /* no hub-wide feature/status flags */
  1786. - break;
  1787. - default:
  1788. - goto error;
  1789. - }
  1790. - break;
  1791. - case SetPortFeature:
  1792. - selector = wIndex >> 8;
  1793. - wIndex &= 0xff;
  1794. -
  1795. - if (!wIndex || wIndex > ports)
  1796. - goto error;
  1797. - wIndex--;
  1798. - temp = fotg210_readl(fotg210, status_reg);
  1799. - temp &= ~PORT_RWC_BITS;
  1800. - switch (wValue) {
  1801. - case USB_PORT_FEAT_SUSPEND:
  1802. - if ((temp & PORT_PE) == 0
  1803. - || (temp & PORT_RESET) != 0)
  1804. - goto error;
  1805. -
  1806. - /* After above check the port must be connected.
  1807. - * Set appropriate bit thus could put phy into low power
  1808. - * mode if we have hostpc feature
  1809. - */
  1810. - fotg210_writel(fotg210, temp | PORT_SUSPEND,
  1811. - status_reg);
  1812. - set_bit(wIndex, &fotg210->suspended_ports);
  1813. - break;
  1814. - case USB_PORT_FEAT_RESET:
  1815. - if (temp & PORT_RESUME)
  1816. - goto error;
  1817. - /* line status bits may report this as low speed,
  1818. - * which can be fine if this root hub has a
  1819. - * transaction translator built in.
  1820. - */
  1821. - fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
  1822. - temp |= PORT_RESET;
  1823. - temp &= ~PORT_PE;
  1824. -
  1825. - /*
  1826. - * caller must wait, then call GetPortStatus
  1827. - * usb 2.0 spec says 50 ms resets on root
  1828. - */
  1829. - fotg210->reset_done[wIndex] = jiffies
  1830. - + msecs_to_jiffies(50);
  1831. - fotg210_writel(fotg210, temp, status_reg);
  1832. - break;
  1833. -
  1834. - /* For downstream facing ports (these): one hub port is put
  1835. - * into test mode according to USB2 11.24.2.13, then the hub
  1836. - * must be reset (which for root hub now means rmmod+modprobe,
  1837. - * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  1838. - * about the EHCI-specific stuff.
  1839. - */
  1840. - case USB_PORT_FEAT_TEST:
  1841. - if (!selector || selector > 5)
  1842. - goto error;
  1843. - spin_unlock_irqrestore(&fotg210->lock, flags);
  1844. - fotg210_quiesce(fotg210);
  1845. - spin_lock_irqsave(&fotg210->lock, flags);
  1846. -
  1847. - /* Put all enabled ports into suspend */
  1848. - temp = fotg210_readl(fotg210, status_reg) &
  1849. - ~PORT_RWC_BITS;
  1850. - if (temp & PORT_PE)
  1851. - fotg210_writel(fotg210, temp | PORT_SUSPEND,
  1852. - status_reg);
  1853. -
  1854. - spin_unlock_irqrestore(&fotg210->lock, flags);
  1855. - fotg210_halt(fotg210);
  1856. - spin_lock_irqsave(&fotg210->lock, flags);
  1857. -
  1858. - temp = fotg210_readl(fotg210, status_reg);
  1859. - temp |= selector << 16;
  1860. - fotg210_writel(fotg210, temp, status_reg);
  1861. - break;
  1862. -
  1863. - default:
  1864. - goto error;
  1865. - }
  1866. - fotg210_readl(fotg210, &fotg210->regs->command);
  1867. - break;
  1868. -
  1869. - default:
  1870. -error:
  1871. - /* "stall" on error */
  1872. - retval = -EPIPE;
  1873. - }
  1874. - spin_unlock_irqrestore(&fotg210->lock, flags);
  1875. - return retval;
  1876. -}
  1877. -
  1878. -static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
  1879. - int portnum)
  1880. -{
  1881. - return;
  1882. -}
  1883. -
  1884. -static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
  1885. - int portnum)
  1886. -{
  1887. - return 0;
  1888. -}
  1889. -
  1890. -/* There's basically three types of memory:
  1891. - * - data used only by the HCD ... kmalloc is fine
  1892. - * - async and periodic schedules, shared by HC and HCD ... these
  1893. - * need to use dma_pool or dma_alloc_coherent
  1894. - * - driver buffers, read/written by HC ... single shot DMA mapped
  1895. - *
  1896. - * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
  1897. - * No memory seen by this driver is pageable.
  1898. - */
  1899. -
  1900. -/* Allocate the key transfer structures from the previously allocated pool */
  1901. -static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
  1902. - struct fotg210_qtd *qtd, dma_addr_t dma)
  1903. -{
  1904. - memset(qtd, 0, sizeof(*qtd));
  1905. - qtd->qtd_dma = dma;
  1906. - qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
  1907. - qtd->hw_next = FOTG210_LIST_END(fotg210);
  1908. - qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
  1909. - INIT_LIST_HEAD(&qtd->qtd_list);
  1910. -}
  1911. -
  1912. -static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
  1913. - gfp_t flags)
  1914. -{
  1915. - struct fotg210_qtd *qtd;
  1916. - dma_addr_t dma;
  1917. -
  1918. - qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
  1919. - if (qtd != NULL)
  1920. - fotg210_qtd_init(fotg210, qtd, dma);
  1921. -
  1922. - return qtd;
  1923. -}
  1924. -
  1925. -static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
  1926. - struct fotg210_qtd *qtd)
  1927. -{
  1928. - dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
  1929. -}
  1930. -
  1931. -
  1932. -static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  1933. -{
  1934. - /* clean qtds first, and know this is not linked */
  1935. - if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
  1936. - fotg210_dbg(fotg210, "unused qh not empty!\n");
  1937. - BUG();
  1938. - }
  1939. - if (qh->dummy)
  1940. - fotg210_qtd_free(fotg210, qh->dummy);
  1941. - dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
  1942. - kfree(qh);
  1943. -}
  1944. -
  1945. -static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
  1946. - gfp_t flags)
  1947. -{
  1948. - struct fotg210_qh *qh;
  1949. - dma_addr_t dma;
  1950. -
  1951. - qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
  1952. - if (!qh)
  1953. - goto done;
  1954. - qh->hw = (struct fotg210_qh_hw *)
  1955. - dma_pool_zalloc(fotg210->qh_pool, flags, &dma);
  1956. - if (!qh->hw)
  1957. - goto fail;
  1958. - qh->qh_dma = dma;
  1959. - INIT_LIST_HEAD(&qh->qtd_list);
  1960. -
  1961. - /* dummy td enables safe urb queuing */
  1962. - qh->dummy = fotg210_qtd_alloc(fotg210, flags);
  1963. - if (qh->dummy == NULL) {
  1964. - fotg210_dbg(fotg210, "no dummy td\n");
  1965. - goto fail1;
  1966. - }
  1967. -done:
  1968. - return qh;
  1969. -fail1:
  1970. - dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
  1971. -fail:
  1972. - kfree(qh);
  1973. - return NULL;
  1974. -}
  1975. -
  1976. -/* The queue heads and transfer descriptors are managed from pools tied
  1977. - * to each of the "per device" structures.
  1978. - * This is the initialisation and cleanup code.
  1979. - */
  1980. -
  1981. -static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
  1982. -{
  1983. - if (fotg210->async)
  1984. - qh_destroy(fotg210, fotg210->async);
  1985. - fotg210->async = NULL;
  1986. -
  1987. - if (fotg210->dummy)
  1988. - qh_destroy(fotg210, fotg210->dummy);
  1989. - fotg210->dummy = NULL;
  1990. -
  1991. - /* DMA consistent memory and pools */
  1992. - dma_pool_destroy(fotg210->qtd_pool);
  1993. - fotg210->qtd_pool = NULL;
  1994. -
  1995. - dma_pool_destroy(fotg210->qh_pool);
  1996. - fotg210->qh_pool = NULL;
  1997. -
  1998. - dma_pool_destroy(fotg210->itd_pool);
  1999. - fotg210->itd_pool = NULL;
  2000. -
  2001. - if (fotg210->periodic)
  2002. - dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller,
  2003. - fotg210->periodic_size * sizeof(u32),
  2004. - fotg210->periodic, fotg210->periodic_dma);
  2005. - fotg210->periodic = NULL;
  2006. -
  2007. - /* shadow periodic table */
  2008. - kfree(fotg210->pshadow);
  2009. - fotg210->pshadow = NULL;
  2010. -}
  2011. -
  2012. -/* remember to add cleanup code (above) if you add anything here */
  2013. -static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
  2014. -{
  2015. - int i;
  2016. -
  2017. - /* QTDs for control/bulk/intr transfers */
  2018. - fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
  2019. - fotg210_to_hcd(fotg210)->self.controller,
  2020. - sizeof(struct fotg210_qtd),
  2021. - 32 /* byte alignment (for hw parts) */,
  2022. - 4096 /* can't cross 4K */);
  2023. - if (!fotg210->qtd_pool)
  2024. - goto fail;
  2025. -
  2026. - /* QHs for control/bulk/intr transfers */
  2027. - fotg210->qh_pool = dma_pool_create("fotg210_qh",
  2028. - fotg210_to_hcd(fotg210)->self.controller,
  2029. - sizeof(struct fotg210_qh_hw),
  2030. - 32 /* byte alignment (for hw parts) */,
  2031. - 4096 /* can't cross 4K */);
  2032. - if (!fotg210->qh_pool)
  2033. - goto fail;
  2034. -
  2035. - fotg210->async = fotg210_qh_alloc(fotg210, flags);
  2036. - if (!fotg210->async)
  2037. - goto fail;
  2038. -
  2039. - /* ITD for high speed ISO transfers */
  2040. - fotg210->itd_pool = dma_pool_create("fotg210_itd",
  2041. - fotg210_to_hcd(fotg210)->self.controller,
  2042. - sizeof(struct fotg210_itd),
  2043. - 64 /* byte alignment (for hw parts) */,
  2044. - 4096 /* can't cross 4K */);
  2045. - if (!fotg210->itd_pool)
  2046. - goto fail;
  2047. -
  2048. - /* Hardware periodic table */
  2049. - fotg210->periodic =
  2050. - dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller,
  2051. - fotg210->periodic_size * sizeof(__le32),
  2052. - &fotg210->periodic_dma, 0);
  2053. - if (fotg210->periodic == NULL)
  2054. - goto fail;
  2055. -
  2056. - for (i = 0; i < fotg210->periodic_size; i++)
  2057. - fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
  2058. -
  2059. - /* software shadow of hardware table */
  2060. - fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
  2061. - flags);
  2062. - if (fotg210->pshadow != NULL)
  2063. - return 0;
  2064. -
  2065. -fail:
  2066. - fotg210_dbg(fotg210, "couldn't init memory\n");
  2067. - fotg210_mem_cleanup(fotg210);
  2068. - return -ENOMEM;
  2069. -}
  2070. -/* EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
  2071. - *
  2072. - * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
  2073. - * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
  2074. - * buffers needed for the larger number). We use one QH per endpoint, queue
  2075. - * multiple urbs (all three types) per endpoint. URBs may need several qtds.
  2076. - *
  2077. - * ISO traffic uses "ISO TD" (itd) records, and (along with
  2078. - * interrupts) needs careful scheduling. Performance improvements can be
  2079. - * an ongoing challenge. That's in "ehci-sched.c".
  2080. - *
  2081. - * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
  2082. - * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
  2083. - * (b) special fields in qh entries or (c) split iso entries. TTs will
  2084. - * buffer low/full speed data so the host collects it at high speed.
  2085. - */
  2086. -
  2087. -/* fill a qtd, returning how much of the buffer we were able to queue up */
  2088. -static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd,
  2089. - dma_addr_t buf, size_t len, int token, int maxpacket)
  2090. -{
  2091. - int i, count;
  2092. - u64 addr = buf;
  2093. -
  2094. - /* one buffer entry per 4K ... first might be short or unaligned */
  2095. - qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr);
  2096. - qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32));
  2097. - count = 0x1000 - (buf & 0x0fff); /* rest of that page */
  2098. - if (likely(len < count)) /* ... iff needed */
  2099. - count = len;
  2100. - else {
  2101. - buf += 0x1000;
  2102. - buf &= ~0x0fff;
  2103. -
  2104. - /* per-qtd limit: from 16K to 20K (best alignment) */
  2105. - for (i = 1; count < len && i < 5; i++) {
  2106. - addr = buf;
  2107. - qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
  2108. - qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
  2109. - (u32)(addr >> 32));
  2110. - buf += 0x1000;
  2111. - if ((count + 0x1000) < len)
  2112. - count += 0x1000;
  2113. - else
  2114. - count = len;
  2115. - }
  2116. -
  2117. - /* short packets may only terminate transfers */
  2118. - if (count != len)
  2119. - count -= (count % maxpacket);
  2120. - }
  2121. - qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
  2122. - qtd->length = count;
  2123. -
  2124. - return count;
  2125. -}
  2126. -
  2127. -static inline void qh_update(struct fotg210_hcd *fotg210,
  2128. - struct fotg210_qh *qh, struct fotg210_qtd *qtd)
  2129. -{
  2130. - struct fotg210_qh_hw *hw = qh->hw;
  2131. -
  2132. - /* writes to an active overlay are unsafe */
  2133. - BUG_ON(qh->qh_state != QH_STATE_IDLE);
  2134. -
  2135. - hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2136. - hw->hw_alt_next = FOTG210_LIST_END(fotg210);
  2137. -
  2138. - /* Except for control endpoints, we make hardware maintain data
  2139. - * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
  2140. - * and set the pseudo-toggle in udev. Only usb_clear_halt() will
  2141. - * ever clear it.
  2142. - */
  2143. - if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
  2144. - unsigned is_out, epnum;
  2145. -
  2146. - is_out = qh->is_out;
  2147. - epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f;
  2148. - if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
  2149. - hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE);
  2150. - usb_settoggle(qh->dev, epnum, is_out, 1);
  2151. - }
  2152. - }
  2153. -
  2154. - hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
  2155. -}
  2156. -
  2157. -/* if it weren't for a common silicon quirk (writing the dummy into the qh
  2158. - * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
  2159. - * recovery (including urb dequeue) would need software changes to a QH...
  2160. - */
  2161. -static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  2162. -{
  2163. - struct fotg210_qtd *qtd;
  2164. -
  2165. - if (list_empty(&qh->qtd_list))
  2166. - qtd = qh->dummy;
  2167. - else {
  2168. - qtd = list_entry(qh->qtd_list.next,
  2169. - struct fotg210_qtd, qtd_list);
  2170. - /*
  2171. - * first qtd may already be partially processed.
  2172. - * If we come here during unlink, the QH overlay region
  2173. - * might have reference to the just unlinked qtd. The
  2174. - * qtd is updated in qh_completions(). Update the QH
  2175. - * overlay here.
  2176. - */
  2177. - if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
  2178. - qh->hw->hw_qtd_next = qtd->hw_next;
  2179. - qtd = NULL;
  2180. - }
  2181. - }
  2182. -
  2183. - if (qtd)
  2184. - qh_update(fotg210, qh, qtd);
  2185. -}
  2186. -
  2187. -static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  2188. -
  2189. -static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
  2190. - struct usb_host_endpoint *ep)
  2191. -{
  2192. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  2193. - struct fotg210_qh *qh = ep->hcpriv;
  2194. - unsigned long flags;
  2195. -
  2196. - spin_lock_irqsave(&fotg210->lock, flags);
  2197. - qh->clearing_tt = 0;
  2198. - if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
  2199. - && fotg210->rh_state == FOTG210_RH_RUNNING)
  2200. - qh_link_async(fotg210, qh);
  2201. - spin_unlock_irqrestore(&fotg210->lock, flags);
  2202. -}
  2203. -
  2204. -static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
  2205. - struct fotg210_qh *qh, struct urb *urb, u32 token)
  2206. -{
  2207. -
  2208. - /* If an async split transaction gets an error or is unlinked,
  2209. - * the TT buffer may be left in an indeterminate state. We
  2210. - * have to clear the TT buffer.
  2211. - *
  2212. - * Note: this routine is never called for Isochronous transfers.
  2213. - */
  2214. - if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
  2215. - struct usb_device *tt = urb->dev->tt->hub;
  2216. -
  2217. - dev_dbg(&tt->dev,
  2218. - "clear tt buffer port %d, a%d ep%d t%08x\n",
  2219. - urb->dev->ttport, urb->dev->devnum,
  2220. - usb_pipeendpoint(urb->pipe), token);
  2221. -
  2222. - if (urb->dev->tt->hub !=
  2223. - fotg210_to_hcd(fotg210)->self.root_hub) {
  2224. - if (usb_hub_clear_tt_buffer(urb) == 0)
  2225. - qh->clearing_tt = 1;
  2226. - }
  2227. - }
  2228. -}
  2229. -
  2230. -static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb,
  2231. - size_t length, u32 token)
  2232. -{
  2233. - int status = -EINPROGRESS;
  2234. -
  2235. - /* count IN/OUT bytes, not SETUP (even short packets) */
  2236. - if (likely(QTD_PID(token) != 2))
  2237. - urb->actual_length += length - QTD_LENGTH(token);
  2238. -
  2239. - /* don't modify error codes */
  2240. - if (unlikely(urb->unlinked))
  2241. - return status;
  2242. -
  2243. - /* force cleanup after short read; not always an error */
  2244. - if (unlikely(IS_SHORT_READ(token)))
  2245. - status = -EREMOTEIO;
  2246. -
  2247. - /* serious "can't proceed" faults reported by the hardware */
  2248. - if (token & QTD_STS_HALT) {
  2249. - if (token & QTD_STS_BABBLE) {
  2250. - /* FIXME "must" disable babbling device's port too */
  2251. - status = -EOVERFLOW;
  2252. - /* CERR nonzero + halt --> stall */
  2253. - } else if (QTD_CERR(token)) {
  2254. - status = -EPIPE;
  2255. -
  2256. - /* In theory, more than one of the following bits can be set
  2257. - * since they are sticky and the transaction is retried.
  2258. - * Which to test first is rather arbitrary.
  2259. - */
  2260. - } else if (token & QTD_STS_MMF) {
  2261. - /* fs/ls interrupt xfer missed the complete-split */
  2262. - status = -EPROTO;
  2263. - } else if (token & QTD_STS_DBE) {
  2264. - status = (QTD_PID(token) == 1) /* IN ? */
  2265. - ? -ENOSR /* hc couldn't read data */
  2266. - : -ECOMM; /* hc couldn't write data */
  2267. - } else if (token & QTD_STS_XACT) {
  2268. - /* timeout, bad CRC, wrong PID, etc */
  2269. - fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
  2270. - urb->dev->devpath,
  2271. - usb_pipeendpoint(urb->pipe),
  2272. - usb_pipein(urb->pipe) ? "in" : "out");
  2273. - status = -EPROTO;
  2274. - } else { /* unknown */
  2275. - status = -EPROTO;
  2276. - }
  2277. -
  2278. - fotg210_dbg(fotg210,
  2279. - "dev%d ep%d%s qtd token %08x --> status %d\n",
  2280. - usb_pipedevice(urb->pipe),
  2281. - usb_pipeendpoint(urb->pipe),
  2282. - usb_pipein(urb->pipe) ? "in" : "out",
  2283. - token, status);
  2284. - }
  2285. -
  2286. - return status;
  2287. -}
  2288. -
  2289. -static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb,
  2290. - int status)
  2291. -__releases(fotg210->lock)
  2292. -__acquires(fotg210->lock)
  2293. -{
  2294. - if (likely(urb->hcpriv != NULL)) {
  2295. - struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
  2296. -
  2297. - /* S-mask in a QH means it's an interrupt urb */
  2298. - if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
  2299. -
  2300. - /* ... update hc-wide periodic stats (for usbfs) */
  2301. - fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
  2302. - }
  2303. - }
  2304. -
  2305. - if (unlikely(urb->unlinked)) {
  2306. - INCR(fotg210->stats.unlink);
  2307. - } else {
  2308. - /* report non-error and short read status as zero */
  2309. - if (status == -EINPROGRESS || status == -EREMOTEIO)
  2310. - status = 0;
  2311. - INCR(fotg210->stats.complete);
  2312. - }
  2313. -
  2314. -#ifdef FOTG210_URB_TRACE
  2315. - fotg210_dbg(fotg210,
  2316. - "%s %s urb %p ep%d%s status %d len %d/%d\n",
  2317. - __func__, urb->dev->devpath, urb,
  2318. - usb_pipeendpoint(urb->pipe),
  2319. - usb_pipein(urb->pipe) ? "in" : "out",
  2320. - status,
  2321. - urb->actual_length, urb->transfer_buffer_length);
  2322. -#endif
  2323. -
  2324. - /* complete() can reenter this HCD */
  2325. - usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  2326. - spin_unlock(&fotg210->lock);
  2327. - usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status);
  2328. - spin_lock(&fotg210->lock);
  2329. -}
  2330. -
  2331. -static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  2332. -
  2333. -/* Process and free completed qtds for a qh, returning URBs to drivers.
  2334. - * Chases up to qh->hw_current. Returns number of completions called,
  2335. - * indicating how much "real" work we did.
  2336. - */
  2337. -static unsigned qh_completions(struct fotg210_hcd *fotg210,
  2338. - struct fotg210_qh *qh)
  2339. -{
  2340. - struct fotg210_qtd *last, *end = qh->dummy;
  2341. - struct fotg210_qtd *qtd, *tmp;
  2342. - int last_status;
  2343. - int stopped;
  2344. - unsigned count = 0;
  2345. - u8 state;
  2346. - struct fotg210_qh_hw *hw = qh->hw;
  2347. -
  2348. - if (unlikely(list_empty(&qh->qtd_list)))
  2349. - return count;
  2350. -
  2351. - /* completions (or tasks on other cpus) must never clobber HALT
  2352. - * till we've gone through and cleaned everything up, even when
  2353. - * they add urbs to this qh's queue or mark them for unlinking.
  2354. - *
  2355. - * NOTE: unlinking expects to be done in queue order.
  2356. - *
  2357. - * It's a bug for qh->qh_state to be anything other than
  2358. - * QH_STATE_IDLE, unless our caller is scan_async() or
  2359. - * scan_intr().
  2360. - */
  2361. - state = qh->qh_state;
  2362. - qh->qh_state = QH_STATE_COMPLETING;
  2363. - stopped = (state == QH_STATE_IDLE);
  2364. -
  2365. -rescan:
  2366. - last = NULL;
  2367. - last_status = -EINPROGRESS;
  2368. - qh->needs_rescan = 0;
  2369. -
  2370. - /* remove de-activated QTDs from front of queue.
  2371. - * after faults (including short reads), cleanup this urb
  2372. - * then let the queue advance.
  2373. - * if queue is stopped, handles unlinks.
  2374. - */
  2375. - list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
  2376. - struct urb *urb;
  2377. - u32 token = 0;
  2378. -
  2379. - urb = qtd->urb;
  2380. -
  2381. - /* clean up any state from previous QTD ...*/
  2382. - if (last) {
  2383. - if (likely(last->urb != urb)) {
  2384. - fotg210_urb_done(fotg210, last->urb,
  2385. - last_status);
  2386. - count++;
  2387. - last_status = -EINPROGRESS;
  2388. - }
  2389. - fotg210_qtd_free(fotg210, last);
  2390. - last = NULL;
  2391. - }
  2392. -
  2393. - /* ignore urbs submitted during completions we reported */
  2394. - if (qtd == end)
  2395. - break;
  2396. -
  2397. - /* hardware copies qtd out of qh overlay */
  2398. - rmb();
  2399. - token = hc32_to_cpu(fotg210, qtd->hw_token);
  2400. -
  2401. - /* always clean up qtds the hc de-activated */
  2402. -retry_xacterr:
  2403. - if ((token & QTD_STS_ACTIVE) == 0) {
  2404. -
  2405. - /* Report Data Buffer Error: non-fatal but useful */
  2406. - if (token & QTD_STS_DBE)
  2407. - fotg210_dbg(fotg210,
  2408. - "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  2409. - urb, usb_endpoint_num(&urb->ep->desc),
  2410. - usb_endpoint_dir_in(&urb->ep->desc)
  2411. - ? "in" : "out",
  2412. - urb->transfer_buffer_length, qtd, qh);
  2413. -
  2414. - /* on STALL, error, and short reads this urb must
  2415. - * complete and all its qtds must be recycled.
  2416. - */
  2417. - if ((token & QTD_STS_HALT) != 0) {
  2418. -
  2419. - /* retry transaction errors until we
  2420. - * reach the software xacterr limit
  2421. - */
  2422. - if ((token & QTD_STS_XACT) &&
  2423. - QTD_CERR(token) == 0 &&
  2424. - ++qh->xacterrs < QH_XACTERR_MAX &&
  2425. - !urb->unlinked) {
  2426. - fotg210_dbg(fotg210,
  2427. - "detected XactErr len %zu/%zu retry %d\n",
  2428. - qtd->length - QTD_LENGTH(token),
  2429. - qtd->length,
  2430. - qh->xacterrs);
  2431. -
  2432. - /* reset the token in the qtd and the
  2433. - * qh overlay (which still contains
  2434. - * the qtd) so that we pick up from
  2435. - * where we left off
  2436. - */
  2437. - token &= ~QTD_STS_HALT;
  2438. - token |= QTD_STS_ACTIVE |
  2439. - (FOTG210_TUNE_CERR << 10);
  2440. - qtd->hw_token = cpu_to_hc32(fotg210,
  2441. - token);
  2442. - wmb();
  2443. - hw->hw_token = cpu_to_hc32(fotg210,
  2444. - token);
  2445. - goto retry_xacterr;
  2446. - }
  2447. - stopped = 1;
  2448. -
  2449. - /* magic dummy for some short reads; qh won't advance.
  2450. - * that silicon quirk can kick in with this dummy too.
  2451. - *
  2452. - * other short reads won't stop the queue, including
  2453. - * control transfers (status stage handles that) or
  2454. - * most other single-qtd reads ... the queue stops if
  2455. - * URB_SHORT_NOT_OK was set so the driver submitting
  2456. - * the urbs could clean it up.
  2457. - */
  2458. - } else if (IS_SHORT_READ(token) &&
  2459. - !(qtd->hw_alt_next &
  2460. - FOTG210_LIST_END(fotg210))) {
  2461. - stopped = 1;
  2462. - }
  2463. -
  2464. - /* stop scanning when we reach qtds the hc is using */
  2465. - } else if (likely(!stopped
  2466. - && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
  2467. - break;
  2468. -
  2469. - /* scan the whole queue for unlinks whenever it stops */
  2470. - } else {
  2471. - stopped = 1;
  2472. -
  2473. - /* cancel everything if we halt, suspend, etc */
  2474. - if (fotg210->rh_state < FOTG210_RH_RUNNING)
  2475. - last_status = -ESHUTDOWN;
  2476. -
  2477. - /* this qtd is active; skip it unless a previous qtd
  2478. - * for its urb faulted, or its urb was canceled.
  2479. - */
  2480. - else if (last_status == -EINPROGRESS && !urb->unlinked)
  2481. - continue;
  2482. -
  2483. - /* qh unlinked; token in overlay may be most current */
  2484. - if (state == QH_STATE_IDLE &&
  2485. - cpu_to_hc32(fotg210, qtd->qtd_dma)
  2486. - == hw->hw_current) {
  2487. - token = hc32_to_cpu(fotg210, hw->hw_token);
  2488. -
  2489. - /* An unlink may leave an incomplete
  2490. - * async transaction in the TT buffer.
  2491. - * We have to clear it.
  2492. - */
  2493. - fotg210_clear_tt_buffer(fotg210, qh, urb,
  2494. - token);
  2495. - }
  2496. - }
  2497. -
  2498. - /* unless we already know the urb's status, collect qtd status
  2499. - * and update count of bytes transferred. in common short read
  2500. - * cases with only one data qtd (including control transfers),
  2501. - * queue processing won't halt. but with two or more qtds (for
  2502. - * example, with a 32 KB transfer), when the first qtd gets a
  2503. - * short read the second must be removed by hand.
  2504. - */
  2505. - if (last_status == -EINPROGRESS) {
  2506. - last_status = qtd_copy_status(fotg210, urb,
  2507. - qtd->length, token);
  2508. - if (last_status == -EREMOTEIO &&
  2509. - (qtd->hw_alt_next &
  2510. - FOTG210_LIST_END(fotg210)))
  2511. - last_status = -EINPROGRESS;
  2512. -
  2513. - /* As part of low/full-speed endpoint-halt processing
  2514. - * we must clear the TT buffer (11.17.5).
  2515. - */
  2516. - if (unlikely(last_status != -EINPROGRESS &&
  2517. - last_status != -EREMOTEIO)) {
  2518. - /* The TT's in some hubs malfunction when they
  2519. - * receive this request following a STALL (they
  2520. - * stop sending isochronous packets). Since a
  2521. - * STALL can't leave the TT buffer in a busy
  2522. - * state (if you believe Figures 11-48 - 11-51
  2523. - * in the USB 2.0 spec), we won't clear the TT
  2524. - * buffer in this case. Strictly speaking this
  2525. - * is a violation of the spec.
  2526. - */
  2527. - if (last_status != -EPIPE)
  2528. - fotg210_clear_tt_buffer(fotg210, qh,
  2529. - urb, token);
  2530. - }
  2531. - }
  2532. -
  2533. - /* if we're removing something not at the queue head,
  2534. - * patch the hardware queue pointer.
  2535. - */
  2536. - if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
  2537. - last = list_entry(qtd->qtd_list.prev,
  2538. - struct fotg210_qtd, qtd_list);
  2539. - last->hw_next = qtd->hw_next;
  2540. - }
  2541. -
  2542. - /* remove qtd; it's recycled after possible urb completion */
  2543. - list_del(&qtd->qtd_list);
  2544. - last = qtd;
  2545. -
  2546. - /* reinit the xacterr counter for the next qtd */
  2547. - qh->xacterrs = 0;
  2548. - }
  2549. -
  2550. - /* last urb's completion might still need calling */
  2551. - if (likely(last != NULL)) {
  2552. - fotg210_urb_done(fotg210, last->urb, last_status);
  2553. - count++;
  2554. - fotg210_qtd_free(fotg210, last);
  2555. - }
  2556. -
  2557. - /* Do we need to rescan for URBs dequeued during a giveback? */
  2558. - if (unlikely(qh->needs_rescan)) {
  2559. - /* If the QH is already unlinked, do the rescan now. */
  2560. - if (state == QH_STATE_IDLE)
  2561. - goto rescan;
  2562. -
  2563. - /* Otherwise we have to wait until the QH is fully unlinked.
  2564. - * Our caller will start an unlink if qh->needs_rescan is
  2565. - * set. But if an unlink has already started, nothing needs
  2566. - * to be done.
  2567. - */
  2568. - if (state != QH_STATE_LINKED)
  2569. - qh->needs_rescan = 0;
  2570. - }
  2571. -
  2572. - /* restore original state; caller must unlink or relink */
  2573. - qh->qh_state = state;
  2574. -
  2575. - /* be sure the hardware's done with the qh before refreshing
  2576. - * it after fault cleanup, or recovering from silicon wrongly
  2577. - * overlaying the dummy qtd (which reduces DMA chatter).
  2578. - */
  2579. - if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
  2580. - switch (state) {
  2581. - case QH_STATE_IDLE:
  2582. - qh_refresh(fotg210, qh);
  2583. - break;
  2584. - case QH_STATE_LINKED:
  2585. - /* We won't refresh a QH that's linked (after the HC
  2586. - * stopped the queue). That avoids a race:
  2587. - * - HC reads first part of QH;
  2588. - * - CPU updates that first part and the token;
  2589. - * - HC reads rest of that QH, including token
  2590. - * Result: HC gets an inconsistent image, and then
  2591. - * DMAs to/from the wrong memory (corrupting it).
  2592. - *
  2593. - * That should be rare for interrupt transfers,
  2594. - * except maybe high bandwidth ...
  2595. - */
  2596. -
  2597. - /* Tell the caller to start an unlink */
  2598. - qh->needs_rescan = 1;
  2599. - break;
  2600. - /* otherwise, unlink already started */
  2601. - }
  2602. - }
  2603. -
  2604. - return count;
  2605. -}
  2606. -
  2607. -/* reverse of qh_urb_transaction: free a list of TDs.
  2608. - * used for cleanup after errors, before HC sees an URB's TDs.
  2609. - */
  2610. -static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb,
  2611. - struct list_head *head)
  2612. -{
  2613. - struct fotg210_qtd *qtd, *temp;
  2614. -
  2615. - list_for_each_entry_safe(qtd, temp, head, qtd_list) {
  2616. - list_del(&qtd->qtd_list);
  2617. - fotg210_qtd_free(fotg210, qtd);
  2618. - }
  2619. -}
  2620. -
  2621. -/* create a list of filled qtds for this URB; won't link into qh.
  2622. - */
  2623. -static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210,
  2624. - struct urb *urb, struct list_head *head, gfp_t flags)
  2625. -{
  2626. - struct fotg210_qtd *qtd, *qtd_prev;
  2627. - dma_addr_t buf;
  2628. - int len, this_sg_len, maxpacket;
  2629. - int is_input;
  2630. - u32 token;
  2631. - int i;
  2632. - struct scatterlist *sg;
  2633. -
  2634. - /*
  2635. - * URBs map to sequences of QTDs: one logical transaction
  2636. - */
  2637. - qtd = fotg210_qtd_alloc(fotg210, flags);
  2638. - if (unlikely(!qtd))
  2639. - return NULL;
  2640. - list_add_tail(&qtd->qtd_list, head);
  2641. - qtd->urb = urb;
  2642. -
  2643. - token = QTD_STS_ACTIVE;
  2644. - token |= (FOTG210_TUNE_CERR << 10);
  2645. - /* for split transactions, SplitXState initialized to zero */
  2646. -
  2647. - len = urb->transfer_buffer_length;
  2648. - is_input = usb_pipein(urb->pipe);
  2649. - if (usb_pipecontrol(urb->pipe)) {
  2650. - /* SETUP pid */
  2651. - qtd_fill(fotg210, qtd, urb->setup_dma,
  2652. - sizeof(struct usb_ctrlrequest),
  2653. - token | (2 /* "setup" */ << 8), 8);
  2654. -
  2655. - /* ... and always at least one more pid */
  2656. - token ^= QTD_TOGGLE;
  2657. - qtd_prev = qtd;
  2658. - qtd = fotg210_qtd_alloc(fotg210, flags);
  2659. - if (unlikely(!qtd))
  2660. - goto cleanup;
  2661. - qtd->urb = urb;
  2662. - qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2663. - list_add_tail(&qtd->qtd_list, head);
  2664. -
  2665. - /* for zero length DATA stages, STATUS is always IN */
  2666. - if (len == 0)
  2667. - token |= (1 /* "in" */ << 8);
  2668. - }
  2669. -
  2670. - /*
  2671. - * data transfer stage: buffer setup
  2672. - */
  2673. - i = urb->num_mapped_sgs;
  2674. - if (len > 0 && i > 0) {
  2675. - sg = urb->sg;
  2676. - buf = sg_dma_address(sg);
  2677. -
  2678. - /* urb->transfer_buffer_length may be smaller than the
  2679. - * size of the scatterlist (or vice versa)
  2680. - */
  2681. - this_sg_len = min_t(int, sg_dma_len(sg), len);
  2682. - } else {
  2683. - sg = NULL;
  2684. - buf = urb->transfer_dma;
  2685. - this_sg_len = len;
  2686. - }
  2687. -
  2688. - if (is_input)
  2689. - token |= (1 /* "in" */ << 8);
  2690. - /* else it's already initted to "out" pid (0 << 8) */
  2691. -
  2692. - maxpacket = usb_maxpacket(urb->dev, urb->pipe);
  2693. -
  2694. - /*
  2695. - * buffer gets wrapped in one or more qtds;
  2696. - * last one may be "short" (including zero len)
  2697. - * and may serve as a control status ack
  2698. - */
  2699. - for (;;) {
  2700. - int this_qtd_len;
  2701. -
  2702. - this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
  2703. - maxpacket);
  2704. - this_sg_len -= this_qtd_len;
  2705. - len -= this_qtd_len;
  2706. - buf += this_qtd_len;
  2707. -
  2708. - /*
  2709. - * short reads advance to a "magic" dummy instead of the next
  2710. - * qtd ... that forces the queue to stop, for manual cleanup.
  2711. - * (this will usually be overridden later.)
  2712. - */
  2713. - if (is_input)
  2714. - qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
  2715. -
  2716. - /* qh makes control packets use qtd toggle; maybe switch it */
  2717. - if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
  2718. - token ^= QTD_TOGGLE;
  2719. -
  2720. - if (likely(this_sg_len <= 0)) {
  2721. - if (--i <= 0 || len <= 0)
  2722. - break;
  2723. - sg = sg_next(sg);
  2724. - buf = sg_dma_address(sg);
  2725. - this_sg_len = min_t(int, sg_dma_len(sg), len);
  2726. - }
  2727. -
  2728. - qtd_prev = qtd;
  2729. - qtd = fotg210_qtd_alloc(fotg210, flags);
  2730. - if (unlikely(!qtd))
  2731. - goto cleanup;
  2732. - qtd->urb = urb;
  2733. - qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2734. - list_add_tail(&qtd->qtd_list, head);
  2735. - }
  2736. -
  2737. - /*
  2738. - * unless the caller requires manual cleanup after short reads,
  2739. - * have the alt_next mechanism keep the queue running after the
  2740. - * last data qtd (the only one, for control and most other cases).
  2741. - */
  2742. - if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 ||
  2743. - usb_pipecontrol(urb->pipe)))
  2744. - qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
  2745. -
  2746. - /*
  2747. - * control requests may need a terminating data "status" ack;
  2748. - * other OUT ones may need a terminating short packet
  2749. - * (zero length).
  2750. - */
  2751. - if (likely(urb->transfer_buffer_length != 0)) {
  2752. - int one_more = 0;
  2753. -
  2754. - if (usb_pipecontrol(urb->pipe)) {
  2755. - one_more = 1;
  2756. - token ^= 0x0100; /* "in" <--> "out" */
  2757. - token |= QTD_TOGGLE; /* force DATA1 */
  2758. - } else if (usb_pipeout(urb->pipe)
  2759. - && (urb->transfer_flags & URB_ZERO_PACKET)
  2760. - && !(urb->transfer_buffer_length % maxpacket)) {
  2761. - one_more = 1;
  2762. - }
  2763. - if (one_more) {
  2764. - qtd_prev = qtd;
  2765. - qtd = fotg210_qtd_alloc(fotg210, flags);
  2766. - if (unlikely(!qtd))
  2767. - goto cleanup;
  2768. - qtd->urb = urb;
  2769. - qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  2770. - list_add_tail(&qtd->qtd_list, head);
  2771. -
  2772. - /* never any data in such packets */
  2773. - qtd_fill(fotg210, qtd, 0, 0, token, 0);
  2774. - }
  2775. - }
  2776. -
  2777. - /* by default, enable interrupt on urb completion */
  2778. - if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
  2779. - qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
  2780. - return head;
  2781. -
  2782. -cleanup:
  2783. - qtd_list_free(fotg210, urb, head);
  2784. - return NULL;
  2785. -}
  2786. -
  2787. -/* Would be best to create all qh's from config descriptors,
  2788. - * when each interface/altsetting is established. Unlink
  2789. - * any previous qh and cancel its urbs first; endpoints are
  2790. - * implicitly reset then (data toggle too).
  2791. - * That'd mean updating how usbcore talks to HCDs. (2.7?)
  2792. - */
  2793. -
  2794. -
  2795. -/* Each QH holds a qtd list; a QH is used for everything except iso.
  2796. - *
  2797. - * For interrupt urbs, the scheduler must set the microframe scheduling
  2798. - * mask(s) each time the QH gets scheduled. For highspeed, that's
  2799. - * just one microframe in the s-mask. For split interrupt transactions
  2800. - * there are additional complications: c-mask, maybe FSTNs.
  2801. - */
  2802. -static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb,
  2803. - gfp_t flags)
  2804. -{
  2805. - struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags);
  2806. - struct usb_host_endpoint *ep;
  2807. - u32 info1 = 0, info2 = 0;
  2808. - int is_input, type;
  2809. - int maxp = 0;
  2810. - int mult;
  2811. - struct usb_tt *tt = urb->dev->tt;
  2812. - struct fotg210_qh_hw *hw;
  2813. -
  2814. - if (!qh)
  2815. - return qh;
  2816. -
  2817. - /*
  2818. - * init endpoint/device data for this QH
  2819. - */
  2820. - info1 |= usb_pipeendpoint(urb->pipe) << 8;
  2821. - info1 |= usb_pipedevice(urb->pipe) << 0;
  2822. -
  2823. - is_input = usb_pipein(urb->pipe);
  2824. - type = usb_pipetype(urb->pipe);
  2825. - ep = usb_pipe_endpoint(urb->dev, urb->pipe);
  2826. - maxp = usb_endpoint_maxp(&ep->desc);
  2827. - mult = usb_endpoint_maxp_mult(&ep->desc);
  2828. -
  2829. - /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
  2830. - * acts like up to 3KB, but is built from smaller packets.
  2831. - */
  2832. - if (maxp > 1024) {
  2833. - fotg210_dbg(fotg210, "bogus qh maxpacket %d\n", maxp);
  2834. - goto done;
  2835. - }
  2836. -
  2837. - /* Compute interrupt scheduling parameters just once, and save.
  2838. - * - allowing for high bandwidth, how many nsec/uframe are used?
  2839. - * - split transactions need a second CSPLIT uframe; same question
  2840. - * - splits also need a schedule gap (for full/low speed I/O)
  2841. - * - qh has a polling interval
  2842. - *
  2843. - * For control/bulk requests, the HC or TT handles these.
  2844. - */
  2845. - if (type == PIPE_INTERRUPT) {
  2846. - qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
  2847. - is_input, 0, mult * maxp));
  2848. - qh->start = NO_FRAME;
  2849. -
  2850. - if (urb->dev->speed == USB_SPEED_HIGH) {
  2851. - qh->c_usecs = 0;
  2852. - qh->gap_uf = 0;
  2853. -
  2854. - qh->period = urb->interval >> 3;
  2855. - if (qh->period == 0 && urb->interval != 1) {
  2856. - /* NOTE interval 2 or 4 uframes could work.
  2857. - * But interval 1 scheduling is simpler, and
  2858. - * includes high bandwidth.
  2859. - */
  2860. - urb->interval = 1;
  2861. - } else if (qh->period > fotg210->periodic_size) {
  2862. - qh->period = fotg210->periodic_size;
  2863. - urb->interval = qh->period << 3;
  2864. - }
  2865. - } else {
  2866. - int think_time;
  2867. -
  2868. - /* gap is f(FS/LS transfer times) */
  2869. - qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
  2870. - is_input, 0, maxp) / (125 * 1000);
  2871. -
  2872. - /* FIXME this just approximates SPLIT/CSPLIT times */
  2873. - if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
  2874. - qh->c_usecs = qh->usecs + HS_USECS(0);
  2875. - qh->usecs = HS_USECS(1);
  2876. - } else { /* SPLIT+DATA, gap, CSPLIT */
  2877. - qh->usecs += HS_USECS(1);
  2878. - qh->c_usecs = HS_USECS(0);
  2879. - }
  2880. -
  2881. - think_time = tt ? tt->think_time : 0;
  2882. - qh->tt_usecs = NS_TO_US(think_time +
  2883. - usb_calc_bus_time(urb->dev->speed,
  2884. - is_input, 0, maxp));
  2885. - qh->period = urb->interval;
  2886. - if (qh->period > fotg210->periodic_size) {
  2887. - qh->period = fotg210->periodic_size;
  2888. - urb->interval = qh->period;
  2889. - }
  2890. - }
  2891. - }
  2892. -
  2893. - /* support for tt scheduling, and access to toggles */
  2894. - qh->dev = urb->dev;
  2895. -
  2896. - /* using TT? */
  2897. - switch (urb->dev->speed) {
  2898. - case USB_SPEED_LOW:
  2899. - info1 |= QH_LOW_SPEED;
  2900. - fallthrough;
  2901. -
  2902. - case USB_SPEED_FULL:
  2903. - /* EPS 0 means "full" */
  2904. - if (type != PIPE_INTERRUPT)
  2905. - info1 |= (FOTG210_TUNE_RL_TT << 28);
  2906. - if (type == PIPE_CONTROL) {
  2907. - info1 |= QH_CONTROL_EP; /* for TT */
  2908. - info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  2909. - }
  2910. - info1 |= maxp << 16;
  2911. -
  2912. - info2 |= (FOTG210_TUNE_MULT_TT << 30);
  2913. -
  2914. - /* Some Freescale processors have an erratum in which the
  2915. - * port number in the queue head was 0..N-1 instead of 1..N.
  2916. - */
  2917. - if (fotg210_has_fsl_portno_bug(fotg210))
  2918. - info2 |= (urb->dev->ttport-1) << 23;
  2919. - else
  2920. - info2 |= urb->dev->ttport << 23;
  2921. -
  2922. - /* set the address of the TT; for TDI's integrated
  2923. - * root hub tt, leave it zeroed.
  2924. - */
  2925. - if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
  2926. - info2 |= tt->hub->devnum << 16;
  2927. -
  2928. - /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
  2929. -
  2930. - break;
  2931. -
  2932. - case USB_SPEED_HIGH: /* no TT involved */
  2933. - info1 |= QH_HIGH_SPEED;
  2934. - if (type == PIPE_CONTROL) {
  2935. - info1 |= (FOTG210_TUNE_RL_HS << 28);
  2936. - info1 |= 64 << 16; /* usb2 fixed maxpacket */
  2937. - info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  2938. - info2 |= (FOTG210_TUNE_MULT_HS << 30);
  2939. - } else if (type == PIPE_BULK) {
  2940. - info1 |= (FOTG210_TUNE_RL_HS << 28);
  2941. - /* The USB spec says that high speed bulk endpoints
  2942. - * always use 512 byte maxpacket. But some device
  2943. - * vendors decided to ignore that, and MSFT is happy
  2944. - * to help them do so. So now people expect to use
  2945. - * such nonconformant devices with Linux too; sigh.
  2946. - */
  2947. - info1 |= maxp << 16;
  2948. - info2 |= (FOTG210_TUNE_MULT_HS << 30);
  2949. - } else { /* PIPE_INTERRUPT */
  2950. - info1 |= maxp << 16;
  2951. - info2 |= mult << 30;
  2952. - }
  2953. - break;
  2954. - default:
  2955. - fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
  2956. - urb->dev->speed);
  2957. -done:
  2958. - qh_destroy(fotg210, qh);
  2959. - return NULL;
  2960. - }
  2961. -
  2962. - /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
  2963. -
  2964. - /* init as live, toggle clear, advance to dummy */
  2965. - qh->qh_state = QH_STATE_IDLE;
  2966. - hw = qh->hw;
  2967. - hw->hw_info1 = cpu_to_hc32(fotg210, info1);
  2968. - hw->hw_info2 = cpu_to_hc32(fotg210, info2);
  2969. - qh->is_out = !is_input;
  2970. - usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
  2971. - qh_refresh(fotg210, qh);
  2972. - return qh;
  2973. -}
  2974. -
  2975. -static void enable_async(struct fotg210_hcd *fotg210)
  2976. -{
  2977. - if (fotg210->async_count++)
  2978. - return;
  2979. -
  2980. - /* Stop waiting to turn off the async schedule */
  2981. - fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
  2982. -
  2983. - /* Don't start the schedule until ASS is 0 */
  2984. - fotg210_poll_ASS(fotg210);
  2985. - turn_on_io_watchdog(fotg210);
  2986. -}
  2987. -
  2988. -static void disable_async(struct fotg210_hcd *fotg210)
  2989. -{
  2990. - if (--fotg210->async_count)
  2991. - return;
  2992. -
  2993. - /* The async schedule and async_unlink list are supposed to be empty */
  2994. - WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
  2995. -
  2996. - /* Don't turn off the schedule until ASS is 1 */
  2997. - fotg210_poll_ASS(fotg210);
  2998. -}
  2999. -
  3000. -/* move qh (and its qtds) onto async queue; maybe enable queue. */
  3001. -
  3002. -static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3003. -{
  3004. - __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
  3005. - struct fotg210_qh *head;
  3006. -
  3007. - /* Don't link a QH if there's a Clear-TT-Buffer pending */
  3008. - if (unlikely(qh->clearing_tt))
  3009. - return;
  3010. -
  3011. - WARN_ON(qh->qh_state != QH_STATE_IDLE);
  3012. -
  3013. - /* clear halt and/or toggle; and maybe recover from silicon quirk */
  3014. - qh_refresh(fotg210, qh);
  3015. -
  3016. - /* splice right after start */
  3017. - head = fotg210->async;
  3018. - qh->qh_next = head->qh_next;
  3019. - qh->hw->hw_next = head->hw->hw_next;
  3020. - wmb();
  3021. -
  3022. - head->qh_next.qh = qh;
  3023. - head->hw->hw_next = dma;
  3024. -
  3025. - qh->xacterrs = 0;
  3026. - qh->qh_state = QH_STATE_LINKED;
  3027. - /* qtd completions reported later by interrupt */
  3028. -
  3029. - enable_async(fotg210);
  3030. -}
  3031. -
  3032. -/* For control/bulk/interrupt, return QH with these TDs appended.
  3033. - * Allocates and initializes the QH if necessary.
  3034. - * Returns null if it can't allocate a QH it needs to.
  3035. - * If the QH has TDs (urbs) already, that's great.
  3036. - */
  3037. -static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210,
  3038. - struct urb *urb, struct list_head *qtd_list,
  3039. - int epnum, void **ptr)
  3040. -{
  3041. - struct fotg210_qh *qh = NULL;
  3042. - __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
  3043. -
  3044. - qh = (struct fotg210_qh *) *ptr;
  3045. - if (unlikely(qh == NULL)) {
  3046. - /* can't sleep here, we have fotg210->lock... */
  3047. - qh = qh_make(fotg210, urb, GFP_ATOMIC);
  3048. - *ptr = qh;
  3049. - }
  3050. - if (likely(qh != NULL)) {
  3051. - struct fotg210_qtd *qtd;
  3052. -
  3053. - if (unlikely(list_empty(qtd_list)))
  3054. - qtd = NULL;
  3055. - else
  3056. - qtd = list_entry(qtd_list->next, struct fotg210_qtd,
  3057. - qtd_list);
  3058. -
  3059. - /* control qh may need patching ... */
  3060. - if (unlikely(epnum == 0)) {
  3061. - /* usb_reset_device() briefly reverts to address 0 */
  3062. - if (usb_pipedevice(urb->pipe) == 0)
  3063. - qh->hw->hw_info1 &= ~qh_addr_mask;
  3064. - }
  3065. -
  3066. - /* just one way to queue requests: swap with the dummy qtd.
  3067. - * only hc or qh_refresh() ever modify the overlay.
  3068. - */
  3069. - if (likely(qtd != NULL)) {
  3070. - struct fotg210_qtd *dummy;
  3071. - dma_addr_t dma;
  3072. - __hc32 token;
  3073. -
  3074. - /* to avoid racing the HC, use the dummy td instead of
  3075. - * the first td of our list (becomes new dummy). both
  3076. - * tds stay deactivated until we're done, when the
  3077. - * HC is allowed to fetch the old dummy (4.10.2).
  3078. - */
  3079. - token = qtd->hw_token;
  3080. - qtd->hw_token = HALT_BIT(fotg210);
  3081. -
  3082. - dummy = qh->dummy;
  3083. -
  3084. - dma = dummy->qtd_dma;
  3085. - *dummy = *qtd;
  3086. - dummy->qtd_dma = dma;
  3087. -
  3088. - list_del(&qtd->qtd_list);
  3089. - list_add(&dummy->qtd_list, qtd_list);
  3090. - list_splice_tail(qtd_list, &qh->qtd_list);
  3091. -
  3092. - fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
  3093. - qh->dummy = qtd;
  3094. -
  3095. - /* hc must see the new dummy at list end */
  3096. - dma = qtd->qtd_dma;
  3097. - qtd = list_entry(qh->qtd_list.prev,
  3098. - struct fotg210_qtd, qtd_list);
  3099. - qtd->hw_next = QTD_NEXT(fotg210, dma);
  3100. -
  3101. - /* let the hc process these next qtds */
  3102. - wmb();
  3103. - dummy->hw_token = token;
  3104. -
  3105. - urb->hcpriv = qh;
  3106. - }
  3107. - }
  3108. - return qh;
  3109. -}
  3110. -
  3111. -static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb,
  3112. - struct list_head *qtd_list, gfp_t mem_flags)
  3113. -{
  3114. - int epnum;
  3115. - unsigned long flags;
  3116. - struct fotg210_qh *qh = NULL;
  3117. - int rc;
  3118. -
  3119. - epnum = urb->ep->desc.bEndpointAddress;
  3120. -
  3121. -#ifdef FOTG210_URB_TRACE
  3122. - {
  3123. - struct fotg210_qtd *qtd;
  3124. -
  3125. - qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list);
  3126. - fotg210_dbg(fotg210,
  3127. - "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  3128. - __func__, urb->dev->devpath, urb,
  3129. - epnum & 0x0f, (epnum & USB_DIR_IN)
  3130. - ? "in" : "out",
  3131. - urb->transfer_buffer_length,
  3132. - qtd, urb->ep->hcpriv);
  3133. - }
  3134. -#endif
  3135. -
  3136. - spin_lock_irqsave(&fotg210->lock, flags);
  3137. - if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  3138. - rc = -ESHUTDOWN;
  3139. - goto done;
  3140. - }
  3141. - rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  3142. - if (unlikely(rc))
  3143. - goto done;
  3144. -
  3145. - qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
  3146. - if (unlikely(qh == NULL)) {
  3147. - usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  3148. - rc = -ENOMEM;
  3149. - goto done;
  3150. - }
  3151. -
  3152. - /* Control/bulk operations through TTs don't need scheduling,
  3153. - * the HC and TT handle it when the TT has a buffer ready.
  3154. - */
  3155. - if (likely(qh->qh_state == QH_STATE_IDLE))
  3156. - qh_link_async(fotg210, qh);
  3157. -done:
  3158. - spin_unlock_irqrestore(&fotg210->lock, flags);
  3159. - if (unlikely(qh == NULL))
  3160. - qtd_list_free(fotg210, urb, qtd_list);
  3161. - return rc;
  3162. -}
  3163. -
  3164. -static void single_unlink_async(struct fotg210_hcd *fotg210,
  3165. - struct fotg210_qh *qh)
  3166. -{
  3167. - struct fotg210_qh *prev;
  3168. -
  3169. - /* Add to the end of the list of QHs waiting for the next IAAD */
  3170. - qh->qh_state = QH_STATE_UNLINK;
  3171. - if (fotg210->async_unlink)
  3172. - fotg210->async_unlink_last->unlink_next = qh;
  3173. - else
  3174. - fotg210->async_unlink = qh;
  3175. - fotg210->async_unlink_last = qh;
  3176. -
  3177. - /* Unlink it from the schedule */
  3178. - prev = fotg210->async;
  3179. - while (prev->qh_next.qh != qh)
  3180. - prev = prev->qh_next.qh;
  3181. -
  3182. - prev->hw->hw_next = qh->hw->hw_next;
  3183. - prev->qh_next = qh->qh_next;
  3184. - if (fotg210->qh_scan_next == qh)
  3185. - fotg210->qh_scan_next = qh->qh_next.qh;
  3186. -}
  3187. -
  3188. -static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
  3189. -{
  3190. - /*
  3191. - * Do nothing if an IAA cycle is already running or
  3192. - * if one will be started shortly.
  3193. - */
  3194. - if (fotg210->async_iaa || fotg210->async_unlinking)
  3195. - return;
  3196. -
  3197. - /* Do all the waiting QHs at once */
  3198. - fotg210->async_iaa = fotg210->async_unlink;
  3199. - fotg210->async_unlink = NULL;
  3200. -
  3201. - /* If the controller isn't running, we don't have to wait for it */
  3202. - if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
  3203. - if (!nested) /* Avoid recursion */
  3204. - end_unlink_async(fotg210);
  3205. -
  3206. - /* Otherwise start a new IAA cycle */
  3207. - } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
  3208. - /* Make sure the unlinks are all visible to the hardware */
  3209. - wmb();
  3210. -
  3211. - fotg210_writel(fotg210, fotg210->command | CMD_IAAD,
  3212. - &fotg210->regs->command);
  3213. - fotg210_readl(fotg210, &fotg210->regs->command);
  3214. - fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG,
  3215. - true);
  3216. - }
  3217. -}
  3218. -
  3219. -/* the async qh for the qtds being unlinked are now gone from the HC */
  3220. -
  3221. -static void end_unlink_async(struct fotg210_hcd *fotg210)
  3222. -{
  3223. - struct fotg210_qh *qh;
  3224. -
  3225. - /* Process the idle QHs */
  3226. -restart:
  3227. - fotg210->async_unlinking = true;
  3228. - while (fotg210->async_iaa) {
  3229. - qh = fotg210->async_iaa;
  3230. - fotg210->async_iaa = qh->unlink_next;
  3231. - qh->unlink_next = NULL;
  3232. -
  3233. - qh->qh_state = QH_STATE_IDLE;
  3234. - qh->qh_next.qh = NULL;
  3235. -
  3236. - qh_completions(fotg210, qh);
  3237. - if (!list_empty(&qh->qtd_list) &&
  3238. - fotg210->rh_state == FOTG210_RH_RUNNING)
  3239. - qh_link_async(fotg210, qh);
  3240. - disable_async(fotg210);
  3241. - }
  3242. - fotg210->async_unlinking = false;
  3243. -
  3244. - /* Start a new IAA cycle if any QHs are waiting for it */
  3245. - if (fotg210->async_unlink) {
  3246. - start_iaa_cycle(fotg210, true);
  3247. - if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
  3248. - goto restart;
  3249. - }
  3250. -}
  3251. -
  3252. -static void unlink_empty_async(struct fotg210_hcd *fotg210)
  3253. -{
  3254. - struct fotg210_qh *qh, *next;
  3255. - bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
  3256. - bool check_unlinks_later = false;
  3257. -
  3258. - /* Unlink all the async QHs that have been empty for a timer cycle */
  3259. - next = fotg210->async->qh_next.qh;
  3260. - while (next) {
  3261. - qh = next;
  3262. - next = qh->qh_next.qh;
  3263. -
  3264. - if (list_empty(&qh->qtd_list) &&
  3265. - qh->qh_state == QH_STATE_LINKED) {
  3266. - if (!stopped && qh->unlink_cycle ==
  3267. - fotg210->async_unlink_cycle)
  3268. - check_unlinks_later = true;
  3269. - else
  3270. - single_unlink_async(fotg210, qh);
  3271. - }
  3272. - }
  3273. -
  3274. - /* Start a new IAA cycle if any QHs are waiting for it */
  3275. - if (fotg210->async_unlink)
  3276. - start_iaa_cycle(fotg210, false);
  3277. -
  3278. - /* QHs that haven't been empty for long enough will be handled later */
  3279. - if (check_unlinks_later) {
  3280. - fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
  3281. - true);
  3282. - ++fotg210->async_unlink_cycle;
  3283. - }
  3284. -}
  3285. -
  3286. -/* makes sure the async qh will become idle */
  3287. -/* caller must own fotg210->lock */
  3288. -
  3289. -static void start_unlink_async(struct fotg210_hcd *fotg210,
  3290. - struct fotg210_qh *qh)
  3291. -{
  3292. - /*
  3293. - * If the QH isn't linked then there's nothing we can do
  3294. - * unless we were called during a giveback, in which case
  3295. - * qh_completions() has to deal with it.
  3296. - */
  3297. - if (qh->qh_state != QH_STATE_LINKED) {
  3298. - if (qh->qh_state == QH_STATE_COMPLETING)
  3299. - qh->needs_rescan = 1;
  3300. - return;
  3301. - }
  3302. -
  3303. - single_unlink_async(fotg210, qh);
  3304. - start_iaa_cycle(fotg210, false);
  3305. -}
  3306. -
  3307. -static void scan_async(struct fotg210_hcd *fotg210)
  3308. -{
  3309. - struct fotg210_qh *qh;
  3310. - bool check_unlinks_later = false;
  3311. -
  3312. - fotg210->qh_scan_next = fotg210->async->qh_next.qh;
  3313. - while (fotg210->qh_scan_next) {
  3314. - qh = fotg210->qh_scan_next;
  3315. - fotg210->qh_scan_next = qh->qh_next.qh;
  3316. -rescan:
  3317. - /* clean any finished work for this qh */
  3318. - if (!list_empty(&qh->qtd_list)) {
  3319. - int temp;
  3320. -
  3321. - /*
  3322. - * Unlinks could happen here; completion reporting
  3323. - * drops the lock. That's why fotg210->qh_scan_next
  3324. - * always holds the next qh to scan; if the next qh
  3325. - * gets unlinked then fotg210->qh_scan_next is adjusted
  3326. - * in single_unlink_async().
  3327. - */
  3328. - temp = qh_completions(fotg210, qh);
  3329. - if (qh->needs_rescan) {
  3330. - start_unlink_async(fotg210, qh);
  3331. - } else if (list_empty(&qh->qtd_list)
  3332. - && qh->qh_state == QH_STATE_LINKED) {
  3333. - qh->unlink_cycle = fotg210->async_unlink_cycle;
  3334. - check_unlinks_later = true;
  3335. - } else if (temp != 0)
  3336. - goto rescan;
  3337. - }
  3338. - }
  3339. -
  3340. - /*
  3341. - * Unlink empty entries, reducing DMA usage as well
  3342. - * as HCD schedule-scanning costs. Delay for any qh
  3343. - * we just scanned, there's a not-unusual case that it
  3344. - * doesn't stay idle for long.
  3345. - */
  3346. - if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING &&
  3347. - !(fotg210->enabled_hrtimer_events &
  3348. - BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) {
  3349. - fotg210_enable_event(fotg210,
  3350. - FOTG210_HRTIMER_ASYNC_UNLINKS, true);
  3351. - ++fotg210->async_unlink_cycle;
  3352. - }
  3353. -}
  3354. -/* EHCI scheduled transaction support: interrupt, iso, split iso
  3355. - * These are called "periodic" transactions in the EHCI spec.
  3356. - *
  3357. - * Note that for interrupt transfers, the QH/QTD manipulation is shared
  3358. - * with the "asynchronous" transaction support (control/bulk transfers).
  3359. - * The only real difference is in how interrupt transfers are scheduled.
  3360. - *
  3361. - * For ISO, we make an "iso_stream" head to serve the same role as a QH.
  3362. - * It keeps track of every ITD (or SITD) that's linked, and holds enough
  3363. - * pre-calculated schedule data to make appending to the queue be quick.
  3364. - */
  3365. -static int fotg210_get_frame(struct usb_hcd *hcd);
  3366. -
  3367. -/* periodic_next_shadow - return "next" pointer on shadow list
  3368. - * @periodic: host pointer to qh/itd
  3369. - * @tag: hardware tag for type of this record
  3370. - */
  3371. -static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210,
  3372. - union fotg210_shadow *periodic, __hc32 tag)
  3373. -{
  3374. - switch (hc32_to_cpu(fotg210, tag)) {
  3375. - case Q_TYPE_QH:
  3376. - return &periodic->qh->qh_next;
  3377. - case Q_TYPE_FSTN:
  3378. - return &periodic->fstn->fstn_next;
  3379. - default:
  3380. - return &periodic->itd->itd_next;
  3381. - }
  3382. -}
  3383. -
  3384. -static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210,
  3385. - union fotg210_shadow *periodic, __hc32 tag)
  3386. -{
  3387. - switch (hc32_to_cpu(fotg210, tag)) {
  3388. - /* our fotg210_shadow.qh is actually software part */
  3389. - case Q_TYPE_QH:
  3390. - return &periodic->qh->hw->hw_next;
  3391. - /* others are hw parts */
  3392. - default:
  3393. - return periodic->hw_next;
  3394. - }
  3395. -}
  3396. -
  3397. -/* caller must hold fotg210->lock */
  3398. -static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
  3399. - void *ptr)
  3400. -{
  3401. - union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
  3402. - __hc32 *hw_p = &fotg210->periodic[frame];
  3403. - union fotg210_shadow here = *prev_p;
  3404. -
  3405. - /* find predecessor of "ptr"; hw and shadow lists are in sync */
  3406. - while (here.ptr && here.ptr != ptr) {
  3407. - prev_p = periodic_next_shadow(fotg210, prev_p,
  3408. - Q_NEXT_TYPE(fotg210, *hw_p));
  3409. - hw_p = shadow_next_periodic(fotg210, &here,
  3410. - Q_NEXT_TYPE(fotg210, *hw_p));
  3411. - here = *prev_p;
  3412. - }
  3413. - /* an interrupt entry (at list end) could have been shared */
  3414. - if (!here.ptr)
  3415. - return;
  3416. -
  3417. - /* update shadow and hardware lists ... the old "next" pointers
  3418. - * from ptr may still be in use, the caller updates them.
  3419. - */
  3420. - *prev_p = *periodic_next_shadow(fotg210, &here,
  3421. - Q_NEXT_TYPE(fotg210, *hw_p));
  3422. -
  3423. - *hw_p = *shadow_next_periodic(fotg210, &here,
  3424. - Q_NEXT_TYPE(fotg210, *hw_p));
  3425. -}
  3426. -
  3427. -/* how many of the uframe's 125 usecs are allocated? */
  3428. -static unsigned short periodic_usecs(struct fotg210_hcd *fotg210,
  3429. - unsigned frame, unsigned uframe)
  3430. -{
  3431. - __hc32 *hw_p = &fotg210->periodic[frame];
  3432. - union fotg210_shadow *q = &fotg210->pshadow[frame];
  3433. - unsigned usecs = 0;
  3434. - struct fotg210_qh_hw *hw;
  3435. -
  3436. - while (q->ptr) {
  3437. - switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
  3438. - case Q_TYPE_QH:
  3439. - hw = q->qh->hw;
  3440. - /* is it in the S-mask? */
  3441. - if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
  3442. - usecs += q->qh->usecs;
  3443. - /* ... or C-mask? */
  3444. - if (hw->hw_info2 & cpu_to_hc32(fotg210,
  3445. - 1 << (8 + uframe)))
  3446. - usecs += q->qh->c_usecs;
  3447. - hw_p = &hw->hw_next;
  3448. - q = &q->qh->qh_next;
  3449. - break;
  3450. - /* case Q_TYPE_FSTN: */
  3451. - default:
  3452. - /* for "save place" FSTNs, count the relevant INTR
  3453. - * bandwidth from the previous frame
  3454. - */
  3455. - if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
  3456. - fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
  3457. -
  3458. - hw_p = &q->fstn->hw_next;
  3459. - q = &q->fstn->fstn_next;
  3460. - break;
  3461. - case Q_TYPE_ITD:
  3462. - if (q->itd->hw_transaction[uframe])
  3463. - usecs += q->itd->stream->usecs;
  3464. - hw_p = &q->itd->hw_next;
  3465. - q = &q->itd->itd_next;
  3466. - break;
  3467. - }
  3468. - }
  3469. - if (usecs > fotg210->uframe_periodic_max)
  3470. - fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
  3471. - frame * 8 + uframe, usecs);
  3472. - return usecs;
  3473. -}
  3474. -
  3475. -static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
  3476. -{
  3477. - if (!dev1->tt || !dev2->tt)
  3478. - return 0;
  3479. - if (dev1->tt != dev2->tt)
  3480. - return 0;
  3481. - if (dev1->tt->multi)
  3482. - return dev1->ttport == dev2->ttport;
  3483. - else
  3484. - return 1;
  3485. -}
  3486. -
  3487. -/* return true iff the device's transaction translator is available
  3488. - * for a periodic transfer starting at the specified frame, using
  3489. - * all the uframes in the mask.
  3490. - */
  3491. -static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period,
  3492. - struct usb_device *dev, unsigned frame, u32 uf_mask)
  3493. -{
  3494. - if (period == 0) /* error */
  3495. - return 0;
  3496. -
  3497. - /* note bandwidth wastage: split never follows csplit
  3498. - * (different dev or endpoint) until the next uframe.
  3499. - * calling convention doesn't make that distinction.
  3500. - */
  3501. - for (; frame < fotg210->periodic_size; frame += period) {
  3502. - union fotg210_shadow here;
  3503. - __hc32 type;
  3504. - struct fotg210_qh_hw *hw;
  3505. -
  3506. - here = fotg210->pshadow[frame];
  3507. - type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
  3508. - while (here.ptr) {
  3509. - switch (hc32_to_cpu(fotg210, type)) {
  3510. - case Q_TYPE_ITD:
  3511. - type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
  3512. - here = here.itd->itd_next;
  3513. - continue;
  3514. - case Q_TYPE_QH:
  3515. - hw = here.qh->hw;
  3516. - if (same_tt(dev, here.qh->dev)) {
  3517. - u32 mask;
  3518. -
  3519. - mask = hc32_to_cpu(fotg210,
  3520. - hw->hw_info2);
  3521. - /* "knows" no gap is needed */
  3522. - mask |= mask >> 8;
  3523. - if (mask & uf_mask)
  3524. - break;
  3525. - }
  3526. - type = Q_NEXT_TYPE(fotg210, hw->hw_next);
  3527. - here = here.qh->qh_next;
  3528. - continue;
  3529. - /* case Q_TYPE_FSTN: */
  3530. - default:
  3531. - fotg210_dbg(fotg210,
  3532. - "periodic frame %d bogus type %d\n",
  3533. - frame, type);
  3534. - }
  3535. -
  3536. - /* collision or error */
  3537. - return 0;
  3538. - }
  3539. - }
  3540. -
  3541. - /* no collision */
  3542. - return 1;
  3543. -}
  3544. -
  3545. -static void enable_periodic(struct fotg210_hcd *fotg210)
  3546. -{
  3547. - if (fotg210->periodic_count++)
  3548. - return;
  3549. -
  3550. - /* Stop waiting to turn off the periodic schedule */
  3551. - fotg210->enabled_hrtimer_events &=
  3552. - ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
  3553. -
  3554. - /* Don't start the schedule until PSS is 0 */
  3555. - fotg210_poll_PSS(fotg210);
  3556. - turn_on_io_watchdog(fotg210);
  3557. -}
  3558. -
  3559. -static void disable_periodic(struct fotg210_hcd *fotg210)
  3560. -{
  3561. - if (--fotg210->periodic_count)
  3562. - return;
  3563. -
  3564. - /* Don't turn off the schedule until PSS is 1 */
  3565. - fotg210_poll_PSS(fotg210);
  3566. -}
  3567. -
  3568. -/* periodic schedule slots have iso tds (normal or split) first, then a
  3569. - * sparse tree for active interrupt transfers.
  3570. - *
  3571. - * this just links in a qh; caller guarantees uframe masks are set right.
  3572. - * no FSTN support (yet; fotg210 0.96+)
  3573. - */
  3574. -static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3575. -{
  3576. - unsigned i;
  3577. - unsigned period = qh->period;
  3578. -
  3579. - dev_dbg(&qh->dev->dev,
  3580. - "link qh%d-%04x/%p start %d [%d/%d us]\n", period,
  3581. - hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
  3582. - (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
  3583. - qh->c_usecs);
  3584. -
  3585. - /* high bandwidth, or otherwise every microframe */
  3586. - if (period == 0)
  3587. - period = 1;
  3588. -
  3589. - for (i = qh->start; i < fotg210->periodic_size; i += period) {
  3590. - union fotg210_shadow *prev = &fotg210->pshadow[i];
  3591. - __hc32 *hw_p = &fotg210->periodic[i];
  3592. - union fotg210_shadow here = *prev;
  3593. - __hc32 type = 0;
  3594. -
  3595. - /* skip the iso nodes at list head */
  3596. - while (here.ptr) {
  3597. - type = Q_NEXT_TYPE(fotg210, *hw_p);
  3598. - if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
  3599. - break;
  3600. - prev = periodic_next_shadow(fotg210, prev, type);
  3601. - hw_p = shadow_next_periodic(fotg210, &here, type);
  3602. - here = *prev;
  3603. - }
  3604. -
  3605. - /* sorting each branch by period (slow-->fast)
  3606. - * enables sharing interior tree nodes
  3607. - */
  3608. - while (here.ptr && qh != here.qh) {
  3609. - if (qh->period > here.qh->period)
  3610. - break;
  3611. - prev = &here.qh->qh_next;
  3612. - hw_p = &here.qh->hw->hw_next;
  3613. - here = *prev;
  3614. - }
  3615. - /* link in this qh, unless some earlier pass did that */
  3616. - if (qh != here.qh) {
  3617. - qh->qh_next = here;
  3618. - if (here.qh)
  3619. - qh->hw->hw_next = *hw_p;
  3620. - wmb();
  3621. - prev->qh = qh;
  3622. - *hw_p = QH_NEXT(fotg210, qh->qh_dma);
  3623. - }
  3624. - }
  3625. - qh->qh_state = QH_STATE_LINKED;
  3626. - qh->xacterrs = 0;
  3627. -
  3628. - /* update per-qh bandwidth for usbfs */
  3629. - fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
  3630. - ? ((qh->usecs + qh->c_usecs) / qh->period)
  3631. - : (qh->usecs * 8);
  3632. -
  3633. - list_add(&qh->intr_node, &fotg210->intr_qh_list);
  3634. -
  3635. - /* maybe enable periodic schedule processing */
  3636. - ++fotg210->intr_count;
  3637. - enable_periodic(fotg210);
  3638. -}
  3639. -
  3640. -static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
  3641. - struct fotg210_qh *qh)
  3642. -{
  3643. - unsigned i;
  3644. - unsigned period;
  3645. -
  3646. - /*
  3647. - * If qh is for a low/full-speed device, simply unlinking it
  3648. - * could interfere with an ongoing split transaction. To unlink
  3649. - * it safely would require setting the QH_INACTIVATE bit and
  3650. - * waiting at least one frame, as described in EHCI 4.12.2.5.
  3651. - *
  3652. - * We won't bother with any of this. Instead, we assume that the
  3653. - * only reason for unlinking an interrupt QH while the current URB
  3654. - * is still active is to dequeue all the URBs (flush the whole
  3655. - * endpoint queue).
  3656. - *
  3657. - * If rebalancing the periodic schedule is ever implemented, this
  3658. - * approach will no longer be valid.
  3659. - */
  3660. -
  3661. - /* high bandwidth, or otherwise part of every microframe */
  3662. - period = qh->period;
  3663. - if (!period)
  3664. - period = 1;
  3665. -
  3666. - for (i = qh->start; i < fotg210->periodic_size; i += period)
  3667. - periodic_unlink(fotg210, i, qh);
  3668. -
  3669. - /* update per-qh bandwidth for usbfs */
  3670. - fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
  3671. - ? ((qh->usecs + qh->c_usecs) / qh->period)
  3672. - : (qh->usecs * 8);
  3673. -
  3674. - dev_dbg(&qh->dev->dev,
  3675. - "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
  3676. - qh->period, hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
  3677. - (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
  3678. - qh->c_usecs);
  3679. -
  3680. - /* qh->qh_next still "live" to HC */
  3681. - qh->qh_state = QH_STATE_UNLINK;
  3682. - qh->qh_next.ptr = NULL;
  3683. -
  3684. - if (fotg210->qh_scan_next == qh)
  3685. - fotg210->qh_scan_next = list_entry(qh->intr_node.next,
  3686. - struct fotg210_qh, intr_node);
  3687. - list_del(&qh->intr_node);
  3688. -}
  3689. -
  3690. -static void start_unlink_intr(struct fotg210_hcd *fotg210,
  3691. - struct fotg210_qh *qh)
  3692. -{
  3693. - /* If the QH isn't linked then there's nothing we can do
  3694. - * unless we were called during a giveback, in which case
  3695. - * qh_completions() has to deal with it.
  3696. - */
  3697. - if (qh->qh_state != QH_STATE_LINKED) {
  3698. - if (qh->qh_state == QH_STATE_COMPLETING)
  3699. - qh->needs_rescan = 1;
  3700. - return;
  3701. - }
  3702. -
  3703. - qh_unlink_periodic(fotg210, qh);
  3704. -
  3705. - /* Make sure the unlinks are visible before starting the timer */
  3706. - wmb();
  3707. -
  3708. - /*
  3709. - * The EHCI spec doesn't say how long it takes the controller to
  3710. - * stop accessing an unlinked interrupt QH. The timer delay is
  3711. - * 9 uframes; presumably that will be long enough.
  3712. - */
  3713. - qh->unlink_cycle = fotg210->intr_unlink_cycle;
  3714. -
  3715. - /* New entries go at the end of the intr_unlink list */
  3716. - if (fotg210->intr_unlink)
  3717. - fotg210->intr_unlink_last->unlink_next = qh;
  3718. - else
  3719. - fotg210->intr_unlink = qh;
  3720. - fotg210->intr_unlink_last = qh;
  3721. -
  3722. - if (fotg210->intr_unlinking)
  3723. - ; /* Avoid recursive calls */
  3724. - else if (fotg210->rh_state < FOTG210_RH_RUNNING)
  3725. - fotg210_handle_intr_unlinks(fotg210);
  3726. - else if (fotg210->intr_unlink == qh) {
  3727. - fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
  3728. - true);
  3729. - ++fotg210->intr_unlink_cycle;
  3730. - }
  3731. -}
  3732. -
  3733. -static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3734. -{
  3735. - struct fotg210_qh_hw *hw = qh->hw;
  3736. - int rc;
  3737. -
  3738. - qh->qh_state = QH_STATE_IDLE;
  3739. - hw->hw_next = FOTG210_LIST_END(fotg210);
  3740. -
  3741. - qh_completions(fotg210, qh);
  3742. -
  3743. - /* reschedule QH iff another request is queued */
  3744. - if (!list_empty(&qh->qtd_list) &&
  3745. - fotg210->rh_state == FOTG210_RH_RUNNING) {
  3746. - rc = qh_schedule(fotg210, qh);
  3747. -
  3748. - /* An error here likely indicates handshake failure
  3749. - * or no space left in the schedule. Neither fault
  3750. - * should happen often ...
  3751. - *
  3752. - * FIXME kill the now-dysfunctional queued urbs
  3753. - */
  3754. - if (rc != 0)
  3755. - fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
  3756. - qh, rc);
  3757. - }
  3758. -
  3759. - /* maybe turn off periodic schedule */
  3760. - --fotg210->intr_count;
  3761. - disable_periodic(fotg210);
  3762. -}
  3763. -
  3764. -static int check_period(struct fotg210_hcd *fotg210, unsigned frame,
  3765. - unsigned uframe, unsigned period, unsigned usecs)
  3766. -{
  3767. - int claimed;
  3768. -
  3769. - /* complete split running into next frame?
  3770. - * given FSTN support, we could sometimes check...
  3771. - */
  3772. - if (uframe >= 8)
  3773. - return 0;
  3774. -
  3775. - /* convert "usecs we need" to "max already claimed" */
  3776. - usecs = fotg210->uframe_periodic_max - usecs;
  3777. -
  3778. - /* we "know" 2 and 4 uframe intervals were rejected; so
  3779. - * for period 0, check _every_ microframe in the schedule.
  3780. - */
  3781. - if (unlikely(period == 0)) {
  3782. - do {
  3783. - for (uframe = 0; uframe < 7; uframe++) {
  3784. - claimed = periodic_usecs(fotg210, frame,
  3785. - uframe);
  3786. - if (claimed > usecs)
  3787. - return 0;
  3788. - }
  3789. - } while ((frame += 1) < fotg210->periodic_size);
  3790. -
  3791. - /* just check the specified uframe, at that period */
  3792. - } else {
  3793. - do {
  3794. - claimed = periodic_usecs(fotg210, frame, uframe);
  3795. - if (claimed > usecs)
  3796. - return 0;
  3797. - } while ((frame += period) < fotg210->periodic_size);
  3798. - }
  3799. -
  3800. - /* success! */
  3801. - return 1;
  3802. -}
  3803. -
  3804. -static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame,
  3805. - unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp)
  3806. -{
  3807. - int retval = -ENOSPC;
  3808. - u8 mask = 0;
  3809. -
  3810. - if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
  3811. - goto done;
  3812. -
  3813. - if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
  3814. - goto done;
  3815. - if (!qh->c_usecs) {
  3816. - retval = 0;
  3817. - *c_maskp = 0;
  3818. - goto done;
  3819. - }
  3820. -
  3821. - /* Make sure this tt's buffer is also available for CSPLITs.
  3822. - * We pessimize a bit; probably the typical full speed case
  3823. - * doesn't need the second CSPLIT.
  3824. - *
  3825. - * NOTE: both SPLIT and CSPLIT could be checked in just
  3826. - * one smart pass...
  3827. - */
  3828. - mask = 0x03 << (uframe + qh->gap_uf);
  3829. - *c_maskp = cpu_to_hc32(fotg210, mask << 8);
  3830. -
  3831. - mask |= 1 << uframe;
  3832. - if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) {
  3833. - if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1,
  3834. - qh->period, qh->c_usecs))
  3835. - goto done;
  3836. - if (!check_period(fotg210, frame, uframe + qh->gap_uf,
  3837. - qh->period, qh->c_usecs))
  3838. - goto done;
  3839. - retval = 0;
  3840. - }
  3841. -done:
  3842. - return retval;
  3843. -}
  3844. -
  3845. -/* "first fit" scheduling policy used the first time through,
  3846. - * or when the previous schedule slot can't be re-used.
  3847. - */
  3848. -static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  3849. -{
  3850. - int status;
  3851. - unsigned uframe;
  3852. - __hc32 c_mask;
  3853. - unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
  3854. - struct fotg210_qh_hw *hw = qh->hw;
  3855. -
  3856. - qh_refresh(fotg210, qh);
  3857. - hw->hw_next = FOTG210_LIST_END(fotg210);
  3858. - frame = qh->start;
  3859. -
  3860. - /* reuse the previous schedule slots, if we can */
  3861. - if (frame < qh->period) {
  3862. - uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK);
  3863. - status = check_intr_schedule(fotg210, frame, --uframe,
  3864. - qh, &c_mask);
  3865. - } else {
  3866. - uframe = 0;
  3867. - c_mask = 0;
  3868. - status = -ENOSPC;
  3869. - }
  3870. -
  3871. - /* else scan the schedule to find a group of slots such that all
  3872. - * uframes have enough periodic bandwidth available.
  3873. - */
  3874. - if (status) {
  3875. - /* "normal" case, uframing flexible except with splits */
  3876. - if (qh->period) {
  3877. - int i;
  3878. -
  3879. - for (i = qh->period; status && i > 0; --i) {
  3880. - frame = ++fotg210->random_frame % qh->period;
  3881. - for (uframe = 0; uframe < 8; uframe++) {
  3882. - status = check_intr_schedule(fotg210,
  3883. - frame, uframe, qh,
  3884. - &c_mask);
  3885. - if (status == 0)
  3886. - break;
  3887. - }
  3888. - }
  3889. -
  3890. - /* qh->period == 0 means every uframe */
  3891. - } else {
  3892. - frame = 0;
  3893. - status = check_intr_schedule(fotg210, 0, 0, qh,
  3894. - &c_mask);
  3895. - }
  3896. - if (status)
  3897. - goto done;
  3898. - qh->start = frame;
  3899. -
  3900. - /* reset S-frame and (maybe) C-frame masks */
  3901. - hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK));
  3902. - hw->hw_info2 |= qh->period
  3903. - ? cpu_to_hc32(fotg210, 1 << uframe)
  3904. - : cpu_to_hc32(fotg210, QH_SMASK);
  3905. - hw->hw_info2 |= c_mask;
  3906. - } else
  3907. - fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
  3908. -
  3909. - /* stuff into the periodic schedule */
  3910. - qh_link_periodic(fotg210, qh);
  3911. -done:
  3912. - return status;
  3913. -}
  3914. -
  3915. -static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb,
  3916. - struct list_head *qtd_list, gfp_t mem_flags)
  3917. -{
  3918. - unsigned epnum;
  3919. - unsigned long flags;
  3920. - struct fotg210_qh *qh;
  3921. - int status;
  3922. - struct list_head empty;
  3923. -
  3924. - /* get endpoint and transfer/schedule data */
  3925. - epnum = urb->ep->desc.bEndpointAddress;
  3926. -
  3927. - spin_lock_irqsave(&fotg210->lock, flags);
  3928. -
  3929. - if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  3930. - status = -ESHUTDOWN;
  3931. - goto done_not_linked;
  3932. - }
  3933. - status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  3934. - if (unlikely(status))
  3935. - goto done_not_linked;
  3936. -
  3937. - /* get qh and force any scheduling errors */
  3938. - INIT_LIST_HEAD(&empty);
  3939. - qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
  3940. - if (qh == NULL) {
  3941. - status = -ENOMEM;
  3942. - goto done;
  3943. - }
  3944. - if (qh->qh_state == QH_STATE_IDLE) {
  3945. - status = qh_schedule(fotg210, qh);
  3946. - if (status)
  3947. - goto done;
  3948. - }
  3949. -
  3950. - /* then queue the urb's tds to the qh */
  3951. - qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
  3952. - BUG_ON(qh == NULL);
  3953. -
  3954. - /* ... update usbfs periodic stats */
  3955. - fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
  3956. -
  3957. -done:
  3958. - if (unlikely(status))
  3959. - usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  3960. -done_not_linked:
  3961. - spin_unlock_irqrestore(&fotg210->lock, flags);
  3962. - if (status)
  3963. - qtd_list_free(fotg210, urb, qtd_list);
  3964. -
  3965. - return status;
  3966. -}
  3967. -
  3968. -static void scan_intr(struct fotg210_hcd *fotg210)
  3969. -{
  3970. - struct fotg210_qh *qh;
  3971. -
  3972. - list_for_each_entry_safe(qh, fotg210->qh_scan_next,
  3973. - &fotg210->intr_qh_list, intr_node) {
  3974. -rescan:
  3975. - /* clean any finished work for this qh */
  3976. - if (!list_empty(&qh->qtd_list)) {
  3977. - int temp;
  3978. -
  3979. - /*
  3980. - * Unlinks could happen here; completion reporting
  3981. - * drops the lock. That's why fotg210->qh_scan_next
  3982. - * always holds the next qh to scan; if the next qh
  3983. - * gets unlinked then fotg210->qh_scan_next is adjusted
  3984. - * in qh_unlink_periodic().
  3985. - */
  3986. - temp = qh_completions(fotg210, qh);
  3987. - if (unlikely(qh->needs_rescan ||
  3988. - (list_empty(&qh->qtd_list) &&
  3989. - qh->qh_state == QH_STATE_LINKED)))
  3990. - start_unlink_intr(fotg210, qh);
  3991. - else if (temp != 0)
  3992. - goto rescan;
  3993. - }
  3994. - }
  3995. -}
  3996. -
  3997. -/* fotg210_iso_stream ops work with both ITD and SITD */
  3998. -
  3999. -static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags)
  4000. -{
  4001. - struct fotg210_iso_stream *stream;
  4002. -
  4003. - stream = kzalloc(sizeof(*stream), mem_flags);
  4004. - if (likely(stream != NULL)) {
  4005. - INIT_LIST_HEAD(&stream->td_list);
  4006. - INIT_LIST_HEAD(&stream->free_list);
  4007. - stream->next_uframe = -1;
  4008. - }
  4009. - return stream;
  4010. -}
  4011. -
  4012. -static void iso_stream_init(struct fotg210_hcd *fotg210,
  4013. - struct fotg210_iso_stream *stream, struct usb_device *dev,
  4014. - int pipe, unsigned interval)
  4015. -{
  4016. - u32 buf1;
  4017. - unsigned epnum, maxp;
  4018. - int is_input;
  4019. - long bandwidth;
  4020. - unsigned multi;
  4021. - struct usb_host_endpoint *ep;
  4022. -
  4023. - /*
  4024. - * this might be a "high bandwidth" highspeed endpoint,
  4025. - * as encoded in the ep descriptor's wMaxPacket field
  4026. - */
  4027. - epnum = usb_pipeendpoint(pipe);
  4028. - is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
  4029. - ep = usb_pipe_endpoint(dev, pipe);
  4030. - maxp = usb_endpoint_maxp(&ep->desc);
  4031. - if (is_input)
  4032. - buf1 = (1 << 11);
  4033. - else
  4034. - buf1 = 0;
  4035. -
  4036. - multi = usb_endpoint_maxp_mult(&ep->desc);
  4037. - buf1 |= maxp;
  4038. - maxp *= multi;
  4039. -
  4040. - stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum);
  4041. - stream->buf1 = cpu_to_hc32(fotg210, buf1);
  4042. - stream->buf2 = cpu_to_hc32(fotg210, multi);
  4043. -
  4044. - /* usbfs wants to report the average usecs per frame tied up
  4045. - * when transfers on this endpoint are scheduled ...
  4046. - */
  4047. - if (dev->speed == USB_SPEED_FULL) {
  4048. - interval <<= 3;
  4049. - stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
  4050. - is_input, 1, maxp));
  4051. - stream->usecs /= 8;
  4052. - } else {
  4053. - stream->highspeed = 1;
  4054. - stream->usecs = HS_USECS_ISO(maxp);
  4055. - }
  4056. - bandwidth = stream->usecs * 8;
  4057. - bandwidth /= interval;
  4058. -
  4059. - stream->bandwidth = bandwidth;
  4060. - stream->udev = dev;
  4061. - stream->bEndpointAddress = is_input | epnum;
  4062. - stream->interval = interval;
  4063. - stream->maxp = maxp;
  4064. -}
  4065. -
  4066. -static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210,
  4067. - struct urb *urb)
  4068. -{
  4069. - unsigned epnum;
  4070. - struct fotg210_iso_stream *stream;
  4071. - struct usb_host_endpoint *ep;
  4072. - unsigned long flags;
  4073. -
  4074. - epnum = usb_pipeendpoint(urb->pipe);
  4075. - if (usb_pipein(urb->pipe))
  4076. - ep = urb->dev->ep_in[epnum];
  4077. - else
  4078. - ep = urb->dev->ep_out[epnum];
  4079. -
  4080. - spin_lock_irqsave(&fotg210->lock, flags);
  4081. - stream = ep->hcpriv;
  4082. -
  4083. - if (unlikely(stream == NULL)) {
  4084. - stream = iso_stream_alloc(GFP_ATOMIC);
  4085. - if (likely(stream != NULL)) {
  4086. - ep->hcpriv = stream;
  4087. - stream->ep = ep;
  4088. - iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
  4089. - urb->interval);
  4090. - }
  4091. -
  4092. - /* if dev->ep[epnum] is a QH, hw is set */
  4093. - } else if (unlikely(stream->hw != NULL)) {
  4094. - fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n",
  4095. - urb->dev->devpath, epnum,
  4096. - usb_pipein(urb->pipe) ? "in" : "out");
  4097. - stream = NULL;
  4098. - }
  4099. -
  4100. - spin_unlock_irqrestore(&fotg210->lock, flags);
  4101. - return stream;
  4102. -}
  4103. -
  4104. -/* fotg210_iso_sched ops can be ITD-only or SITD-only */
  4105. -
  4106. -static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
  4107. - gfp_t mem_flags)
  4108. -{
  4109. - struct fotg210_iso_sched *iso_sched;
  4110. -
  4111. - iso_sched = kzalloc(struct_size(iso_sched, packet, packets), mem_flags);
  4112. - if (likely(iso_sched != NULL))
  4113. - INIT_LIST_HEAD(&iso_sched->td_list);
  4114. -
  4115. - return iso_sched;
  4116. -}
  4117. -
  4118. -static inline void itd_sched_init(struct fotg210_hcd *fotg210,
  4119. - struct fotg210_iso_sched *iso_sched,
  4120. - struct fotg210_iso_stream *stream, struct urb *urb)
  4121. -{
  4122. - unsigned i;
  4123. - dma_addr_t dma = urb->transfer_dma;
  4124. -
  4125. - /* how many uframes are needed for these transfers */
  4126. - iso_sched->span = urb->number_of_packets * stream->interval;
  4127. -
  4128. - /* figure out per-uframe itd fields that we'll need later
  4129. - * when we fit new itds into the schedule.
  4130. - */
  4131. - for (i = 0; i < urb->number_of_packets; i++) {
  4132. - struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
  4133. - unsigned length;
  4134. - dma_addr_t buf;
  4135. - u32 trans;
  4136. -
  4137. - length = urb->iso_frame_desc[i].length;
  4138. - buf = dma + urb->iso_frame_desc[i].offset;
  4139. -
  4140. - trans = FOTG210_ISOC_ACTIVE;
  4141. - trans |= buf & 0x0fff;
  4142. - if (unlikely(((i + 1) == urb->number_of_packets))
  4143. - && !(urb->transfer_flags & URB_NO_INTERRUPT))
  4144. - trans |= FOTG210_ITD_IOC;
  4145. - trans |= length << 16;
  4146. - uframe->transaction = cpu_to_hc32(fotg210, trans);
  4147. -
  4148. - /* might need to cross a buffer page within a uframe */
  4149. - uframe->bufp = (buf & ~(u64)0x0fff);
  4150. - buf += length;
  4151. - if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
  4152. - uframe->cross = 1;
  4153. - }
  4154. -}
  4155. -
  4156. -static void iso_sched_free(struct fotg210_iso_stream *stream,
  4157. - struct fotg210_iso_sched *iso_sched)
  4158. -{
  4159. - if (!iso_sched)
  4160. - return;
  4161. - /* caller must hold fotg210->lock!*/
  4162. - list_splice(&iso_sched->td_list, &stream->free_list);
  4163. - kfree(iso_sched);
  4164. -}
  4165. -
  4166. -static int itd_urb_transaction(struct fotg210_iso_stream *stream,
  4167. - struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags)
  4168. -{
  4169. - struct fotg210_itd *itd;
  4170. - dma_addr_t itd_dma;
  4171. - int i;
  4172. - unsigned num_itds;
  4173. - struct fotg210_iso_sched *sched;
  4174. - unsigned long flags;
  4175. -
  4176. - sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
  4177. - if (unlikely(sched == NULL))
  4178. - return -ENOMEM;
  4179. -
  4180. - itd_sched_init(fotg210, sched, stream, urb);
  4181. -
  4182. - if (urb->interval < 8)
  4183. - num_itds = 1 + (sched->span + 7) / 8;
  4184. - else
  4185. - num_itds = urb->number_of_packets;
  4186. -
  4187. - /* allocate/init ITDs */
  4188. - spin_lock_irqsave(&fotg210->lock, flags);
  4189. - for (i = 0; i < num_itds; i++) {
  4190. -
  4191. - /*
  4192. - * Use iTDs from the free list, but not iTDs that may
  4193. - * still be in use by the hardware.
  4194. - */
  4195. - if (likely(!list_empty(&stream->free_list))) {
  4196. - itd = list_first_entry(&stream->free_list,
  4197. - struct fotg210_itd, itd_list);
  4198. - if (itd->frame == fotg210->now_frame)
  4199. - goto alloc_itd;
  4200. - list_del(&itd->itd_list);
  4201. - itd_dma = itd->itd_dma;
  4202. - } else {
  4203. -alloc_itd:
  4204. - spin_unlock_irqrestore(&fotg210->lock, flags);
  4205. - itd = dma_pool_alloc(fotg210->itd_pool, mem_flags,
  4206. - &itd_dma);
  4207. - spin_lock_irqsave(&fotg210->lock, flags);
  4208. - if (!itd) {
  4209. - iso_sched_free(stream, sched);
  4210. - spin_unlock_irqrestore(&fotg210->lock, flags);
  4211. - return -ENOMEM;
  4212. - }
  4213. - }
  4214. -
  4215. - memset(itd, 0, sizeof(*itd));
  4216. - itd->itd_dma = itd_dma;
  4217. - list_add(&itd->itd_list, &sched->td_list);
  4218. - }
  4219. - spin_unlock_irqrestore(&fotg210->lock, flags);
  4220. -
  4221. - /* temporarily store schedule info in hcpriv */
  4222. - urb->hcpriv = sched;
  4223. - urb->error_count = 0;
  4224. - return 0;
  4225. -}
  4226. -
  4227. -static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe,
  4228. - u8 usecs, u32 period)
  4229. -{
  4230. - uframe %= period;
  4231. - do {
  4232. - /* can't commit more than uframe_periodic_max usec */
  4233. - if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
  4234. - > (fotg210->uframe_periodic_max - usecs))
  4235. - return 0;
  4236. -
  4237. - /* we know urb->interval is 2^N uframes */
  4238. - uframe += period;
  4239. - } while (uframe < mod);
  4240. - return 1;
  4241. -}
  4242. -
  4243. -/* This scheduler plans almost as far into the future as it has actual
  4244. - * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
  4245. - * "as small as possible" to be cache-friendlier.) That limits the size
  4246. - * transfers you can stream reliably; avoid more than 64 msec per urb.
  4247. - * Also avoid queue depths of less than fotg210's worst irq latency (affected
  4248. - * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
  4249. - * and other factors); or more than about 230 msec total (for portability,
  4250. - * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
  4251. - */
  4252. -
  4253. -#define SCHEDULE_SLOP 80 /* microframes */
  4254. -
  4255. -static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb,
  4256. - struct fotg210_iso_stream *stream)
  4257. -{
  4258. - u32 now, next, start, period, span;
  4259. - int status;
  4260. - unsigned mod = fotg210->periodic_size << 3;
  4261. - struct fotg210_iso_sched *sched = urb->hcpriv;
  4262. -
  4263. - period = urb->interval;
  4264. - span = sched->span;
  4265. -
  4266. - if (span > mod - SCHEDULE_SLOP) {
  4267. - fotg210_dbg(fotg210, "iso request %p too long\n", urb);
  4268. - status = -EFBIG;
  4269. - goto fail;
  4270. - }
  4271. -
  4272. - now = fotg210_read_frame_index(fotg210) & (mod - 1);
  4273. -
  4274. - /* Typical case: reuse current schedule, stream is still active.
  4275. - * Hopefully there are no gaps from the host falling behind
  4276. - * (irq delays etc), but if there are we'll take the next
  4277. - * slot in the schedule, implicitly assuming URB_ISO_ASAP.
  4278. - */
  4279. - if (likely(!list_empty(&stream->td_list))) {
  4280. - u32 excess;
  4281. -
  4282. - /* For high speed devices, allow scheduling within the
  4283. - * isochronous scheduling threshold. For full speed devices
  4284. - * and Intel PCI-based controllers, don't (work around for
  4285. - * Intel ICH9 bug).
  4286. - */
  4287. - if (!stream->highspeed && fotg210->fs_i_thresh)
  4288. - next = now + fotg210->i_thresh;
  4289. - else
  4290. - next = now;
  4291. -
  4292. - /* Fell behind (by up to twice the slop amount)?
  4293. - * We decide based on the time of the last currently-scheduled
  4294. - * slot, not the time of the next available slot.
  4295. - */
  4296. - excess = (stream->next_uframe - period - next) & (mod - 1);
  4297. - if (excess >= mod - 2 * SCHEDULE_SLOP)
  4298. - start = next + excess - mod + period *
  4299. - DIV_ROUND_UP(mod - excess, period);
  4300. - else
  4301. - start = next + excess + period;
  4302. - if (start - now >= mod) {
  4303. - fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
  4304. - urb, start - now - period, period,
  4305. - mod);
  4306. - status = -EFBIG;
  4307. - goto fail;
  4308. - }
  4309. - }
  4310. -
  4311. - /* need to schedule; when's the next (u)frame we could start?
  4312. - * this is bigger than fotg210->i_thresh allows; scheduling itself
  4313. - * isn't free, the slop should handle reasonably slow cpus. it
  4314. - * can also help high bandwidth if the dma and irq loads don't
  4315. - * jump until after the queue is primed.
  4316. - */
  4317. - else {
  4318. - int done = 0;
  4319. -
  4320. - start = SCHEDULE_SLOP + (now & ~0x07);
  4321. -
  4322. - /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
  4323. -
  4324. - /* find a uframe slot with enough bandwidth.
  4325. - * Early uframes are more precious because full-speed
  4326. - * iso IN transfers can't use late uframes,
  4327. - * and therefore they should be allocated last.
  4328. - */
  4329. - next = start;
  4330. - start += period;
  4331. - do {
  4332. - start--;
  4333. - /* check schedule: enough space? */
  4334. - if (itd_slot_ok(fotg210, mod, start,
  4335. - stream->usecs, period))
  4336. - done = 1;
  4337. - } while (start > next && !done);
  4338. -
  4339. - /* no room in the schedule */
  4340. - if (!done) {
  4341. - fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
  4342. - urb, now, now + mod);
  4343. - status = -ENOSPC;
  4344. - goto fail;
  4345. - }
  4346. - }
  4347. -
  4348. - /* Tried to schedule too far into the future? */
  4349. - if (unlikely(start - now + span - period >=
  4350. - mod - 2 * SCHEDULE_SLOP)) {
  4351. - fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
  4352. - urb, start - now, span - period,
  4353. - mod - 2 * SCHEDULE_SLOP);
  4354. - status = -EFBIG;
  4355. - goto fail;
  4356. - }
  4357. -
  4358. - stream->next_uframe = start & (mod - 1);
  4359. -
  4360. - /* report high speed start in uframes; full speed, in frames */
  4361. - urb->start_frame = stream->next_uframe;
  4362. - if (!stream->highspeed)
  4363. - urb->start_frame >>= 3;
  4364. -
  4365. - /* Make sure scan_isoc() sees these */
  4366. - if (fotg210->isoc_count == 0)
  4367. - fotg210->next_frame = now >> 3;
  4368. - return 0;
  4369. -
  4370. -fail:
  4371. - iso_sched_free(stream, sched);
  4372. - urb->hcpriv = NULL;
  4373. - return status;
  4374. -}
  4375. -
  4376. -static inline void itd_init(struct fotg210_hcd *fotg210,
  4377. - struct fotg210_iso_stream *stream, struct fotg210_itd *itd)
  4378. -{
  4379. - int i;
  4380. -
  4381. - /* it's been recently zeroed */
  4382. - itd->hw_next = FOTG210_LIST_END(fotg210);
  4383. - itd->hw_bufp[0] = stream->buf0;
  4384. - itd->hw_bufp[1] = stream->buf1;
  4385. - itd->hw_bufp[2] = stream->buf2;
  4386. -
  4387. - for (i = 0; i < 8; i++)
  4388. - itd->index[i] = -1;
  4389. -
  4390. - /* All other fields are filled when scheduling */
  4391. -}
  4392. -
  4393. -static inline void itd_patch(struct fotg210_hcd *fotg210,
  4394. - struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched,
  4395. - unsigned index, u16 uframe)
  4396. -{
  4397. - struct fotg210_iso_packet *uf = &iso_sched->packet[index];
  4398. - unsigned pg = itd->pg;
  4399. -
  4400. - uframe &= 0x07;
  4401. - itd->index[uframe] = index;
  4402. -
  4403. - itd->hw_transaction[uframe] = uf->transaction;
  4404. - itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12);
  4405. - itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0);
  4406. - itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32));
  4407. -
  4408. - /* iso_frame_desc[].offset must be strictly increasing */
  4409. - if (unlikely(uf->cross)) {
  4410. - u64 bufp = uf->bufp + 4096;
  4411. -
  4412. - itd->pg = ++pg;
  4413. - itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0);
  4414. - itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32));
  4415. - }
  4416. -}
  4417. -
  4418. -static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame,
  4419. - struct fotg210_itd *itd)
  4420. -{
  4421. - union fotg210_shadow *prev = &fotg210->pshadow[frame];
  4422. - __hc32 *hw_p = &fotg210->periodic[frame];
  4423. - union fotg210_shadow here = *prev;
  4424. - __hc32 type = 0;
  4425. -
  4426. - /* skip any iso nodes which might belong to previous microframes */
  4427. - while (here.ptr) {
  4428. - type = Q_NEXT_TYPE(fotg210, *hw_p);
  4429. - if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
  4430. - break;
  4431. - prev = periodic_next_shadow(fotg210, prev, type);
  4432. - hw_p = shadow_next_periodic(fotg210, &here, type);
  4433. - here = *prev;
  4434. - }
  4435. -
  4436. - itd->itd_next = here;
  4437. - itd->hw_next = *hw_p;
  4438. - prev->itd = itd;
  4439. - itd->frame = frame;
  4440. - wmb();
  4441. - *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
  4442. -}
  4443. -
  4444. -/* fit urb's itds into the selected schedule slot; activate as needed */
  4445. -static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb,
  4446. - unsigned mod, struct fotg210_iso_stream *stream)
  4447. -{
  4448. - int packet;
  4449. - unsigned next_uframe, uframe, frame;
  4450. - struct fotg210_iso_sched *iso_sched = urb->hcpriv;
  4451. - struct fotg210_itd *itd;
  4452. -
  4453. - next_uframe = stream->next_uframe & (mod - 1);
  4454. -
  4455. - if (unlikely(list_empty(&stream->td_list))) {
  4456. - fotg210_to_hcd(fotg210)->self.bandwidth_allocated
  4457. - += stream->bandwidth;
  4458. - fotg210_dbg(fotg210,
  4459. - "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
  4460. - urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  4461. - (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  4462. - urb->interval,
  4463. - next_uframe >> 3, next_uframe & 0x7);
  4464. - }
  4465. -
  4466. - /* fill iTDs uframe by uframe */
  4467. - for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
  4468. - if (itd == NULL) {
  4469. - /* ASSERT: we have all necessary itds */
  4470. -
  4471. - /* ASSERT: no itds for this endpoint in this uframe */
  4472. -
  4473. - itd = list_entry(iso_sched->td_list.next,
  4474. - struct fotg210_itd, itd_list);
  4475. - list_move_tail(&itd->itd_list, &stream->td_list);
  4476. - itd->stream = stream;
  4477. - itd->urb = urb;
  4478. - itd_init(fotg210, stream, itd);
  4479. - }
  4480. -
  4481. - uframe = next_uframe & 0x07;
  4482. - frame = next_uframe >> 3;
  4483. -
  4484. - itd_patch(fotg210, itd, iso_sched, packet, uframe);
  4485. -
  4486. - next_uframe += stream->interval;
  4487. - next_uframe &= mod - 1;
  4488. - packet++;
  4489. -
  4490. - /* link completed itds into the schedule */
  4491. - if (((next_uframe >> 3) != frame)
  4492. - || packet == urb->number_of_packets) {
  4493. - itd_link(fotg210, frame & (fotg210->periodic_size - 1),
  4494. - itd);
  4495. - itd = NULL;
  4496. - }
  4497. - }
  4498. - stream->next_uframe = next_uframe;
  4499. -
  4500. - /* don't need that schedule data any more */
  4501. - iso_sched_free(stream, iso_sched);
  4502. - urb->hcpriv = NULL;
  4503. -
  4504. - ++fotg210->isoc_count;
  4505. - enable_periodic(fotg210);
  4506. -}
  4507. -
  4508. -#define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
  4509. - FOTG210_ISOC_XACTERR)
  4510. -
  4511. -/* Process and recycle a completed ITD. Return true iff its urb completed,
  4512. - * and hence its completion callback probably added things to the hardware
  4513. - * schedule.
  4514. - *
  4515. - * Note that we carefully avoid recycling this descriptor until after any
  4516. - * completion callback runs, so that it won't be reused quickly. That is,
  4517. - * assuming (a) no more than two urbs per frame on this endpoint, and also
  4518. - * (b) only this endpoint's completions submit URBs. It seems some silicon
  4519. - * corrupts things if you reuse completed descriptors very quickly...
  4520. - */
  4521. -static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
  4522. -{
  4523. - struct urb *urb = itd->urb;
  4524. - struct usb_iso_packet_descriptor *desc;
  4525. - u32 t;
  4526. - unsigned uframe;
  4527. - int urb_index = -1;
  4528. - struct fotg210_iso_stream *stream = itd->stream;
  4529. - struct usb_device *dev;
  4530. - bool retval = false;
  4531. -
  4532. - /* for each uframe with a packet */
  4533. - for (uframe = 0; uframe < 8; uframe++) {
  4534. - if (likely(itd->index[uframe] == -1))
  4535. - continue;
  4536. - urb_index = itd->index[uframe];
  4537. - desc = &urb->iso_frame_desc[urb_index];
  4538. -
  4539. - t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
  4540. - itd->hw_transaction[uframe] = 0;
  4541. -
  4542. - /* report transfer status */
  4543. - if (unlikely(t & ISO_ERRS)) {
  4544. - urb->error_count++;
  4545. - if (t & FOTG210_ISOC_BUF_ERR)
  4546. - desc->status = usb_pipein(urb->pipe)
  4547. - ? -ENOSR /* hc couldn't read */
  4548. - : -ECOMM; /* hc couldn't write */
  4549. - else if (t & FOTG210_ISOC_BABBLE)
  4550. - desc->status = -EOVERFLOW;
  4551. - else /* (t & FOTG210_ISOC_XACTERR) */
  4552. - desc->status = -EPROTO;
  4553. -
  4554. - /* HC need not update length with this error */
  4555. - if (!(t & FOTG210_ISOC_BABBLE)) {
  4556. - desc->actual_length = FOTG210_ITD_LENGTH(t);
  4557. - urb->actual_length += desc->actual_length;
  4558. - }
  4559. - } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
  4560. - desc->status = 0;
  4561. - desc->actual_length = FOTG210_ITD_LENGTH(t);
  4562. - urb->actual_length += desc->actual_length;
  4563. - } else {
  4564. - /* URB was too late */
  4565. - desc->status = -EXDEV;
  4566. - }
  4567. - }
  4568. -
  4569. - /* handle completion now? */
  4570. - if (likely((urb_index + 1) != urb->number_of_packets))
  4571. - goto done;
  4572. -
  4573. - /* ASSERT: it's really the last itd for this urb
  4574. - * list_for_each_entry (itd, &stream->td_list, itd_list)
  4575. - * BUG_ON (itd->urb == urb);
  4576. - */
  4577. -
  4578. - /* give urb back to the driver; completion often (re)submits */
  4579. - dev = urb->dev;
  4580. - fotg210_urb_done(fotg210, urb, 0);
  4581. - retval = true;
  4582. - urb = NULL;
  4583. -
  4584. - --fotg210->isoc_count;
  4585. - disable_periodic(fotg210);
  4586. -
  4587. - if (unlikely(list_is_singular(&stream->td_list))) {
  4588. - fotg210_to_hcd(fotg210)->self.bandwidth_allocated
  4589. - -= stream->bandwidth;
  4590. - fotg210_dbg(fotg210,
  4591. - "deschedule devp %s ep%d%s-iso\n",
  4592. - dev->devpath, stream->bEndpointAddress & 0x0f,
  4593. - (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  4594. - }
  4595. -
  4596. -done:
  4597. - itd->urb = NULL;
  4598. -
  4599. - /* Add to the end of the free list for later reuse */
  4600. - list_move_tail(&itd->itd_list, &stream->free_list);
  4601. -
  4602. - /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
  4603. - if (list_empty(&stream->td_list)) {
  4604. - list_splice_tail_init(&stream->free_list,
  4605. - &fotg210->cached_itd_list);
  4606. - start_free_itds(fotg210);
  4607. - }
  4608. -
  4609. - return retval;
  4610. -}
  4611. -
  4612. -static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
  4613. - gfp_t mem_flags)
  4614. -{
  4615. - int status = -EINVAL;
  4616. - unsigned long flags;
  4617. - struct fotg210_iso_stream *stream;
  4618. -
  4619. - /* Get iso_stream head */
  4620. - stream = iso_stream_find(fotg210, urb);
  4621. - if (unlikely(stream == NULL)) {
  4622. - fotg210_dbg(fotg210, "can't get iso stream\n");
  4623. - return -ENOMEM;
  4624. - }
  4625. - if (unlikely(urb->interval != stream->interval &&
  4626. - fotg210_port_speed(fotg210, 0) ==
  4627. - USB_PORT_STAT_HIGH_SPEED)) {
  4628. - fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n",
  4629. - stream->interval, urb->interval);
  4630. - goto done;
  4631. - }
  4632. -
  4633. -#ifdef FOTG210_URB_TRACE
  4634. - fotg210_dbg(fotg210,
  4635. - "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
  4636. - __func__, urb->dev->devpath, urb,
  4637. - usb_pipeendpoint(urb->pipe),
  4638. - usb_pipein(urb->pipe) ? "in" : "out",
  4639. - urb->transfer_buffer_length,
  4640. - urb->number_of_packets, urb->interval,
  4641. - stream);
  4642. -#endif
  4643. -
  4644. - /* allocate ITDs w/o locking anything */
  4645. - status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
  4646. - if (unlikely(status < 0)) {
  4647. - fotg210_dbg(fotg210, "can't init itds\n");
  4648. - goto done;
  4649. - }
  4650. -
  4651. - /* schedule ... need to lock */
  4652. - spin_lock_irqsave(&fotg210->lock, flags);
  4653. - if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  4654. - status = -ESHUTDOWN;
  4655. - goto done_not_linked;
  4656. - }
  4657. - status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  4658. - if (unlikely(status))
  4659. - goto done_not_linked;
  4660. - status = iso_stream_schedule(fotg210, urb, stream);
  4661. - if (likely(status == 0))
  4662. - itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream);
  4663. - else
  4664. - usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  4665. -done_not_linked:
  4666. - spin_unlock_irqrestore(&fotg210->lock, flags);
  4667. -done:
  4668. - return status;
  4669. -}
  4670. -
  4671. -static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame,
  4672. - unsigned now_frame, bool live)
  4673. -{
  4674. - unsigned uf;
  4675. - bool modified;
  4676. - union fotg210_shadow q, *q_p;
  4677. - __hc32 type, *hw_p;
  4678. -
  4679. - /* scan each element in frame's queue for completions */
  4680. - q_p = &fotg210->pshadow[frame];
  4681. - hw_p = &fotg210->periodic[frame];
  4682. - q.ptr = q_p->ptr;
  4683. - type = Q_NEXT_TYPE(fotg210, *hw_p);
  4684. - modified = false;
  4685. -
  4686. - while (q.ptr) {
  4687. - switch (hc32_to_cpu(fotg210, type)) {
  4688. - case Q_TYPE_ITD:
  4689. - /* If this ITD is still active, leave it for
  4690. - * later processing ... check the next entry.
  4691. - * No need to check for activity unless the
  4692. - * frame is current.
  4693. - */
  4694. - if (frame == now_frame && live) {
  4695. - rmb();
  4696. - for (uf = 0; uf < 8; uf++) {
  4697. - if (q.itd->hw_transaction[uf] &
  4698. - ITD_ACTIVE(fotg210))
  4699. - break;
  4700. - }
  4701. - if (uf < 8) {
  4702. - q_p = &q.itd->itd_next;
  4703. - hw_p = &q.itd->hw_next;
  4704. - type = Q_NEXT_TYPE(fotg210,
  4705. - q.itd->hw_next);
  4706. - q = *q_p;
  4707. - break;
  4708. - }
  4709. - }
  4710. -
  4711. - /* Take finished ITDs out of the schedule
  4712. - * and process them: recycle, maybe report
  4713. - * URB completion. HC won't cache the
  4714. - * pointer for much longer, if at all.
  4715. - */
  4716. - *q_p = q.itd->itd_next;
  4717. - *hw_p = q.itd->hw_next;
  4718. - type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
  4719. - wmb();
  4720. - modified = itd_complete(fotg210, q.itd);
  4721. - q = *q_p;
  4722. - break;
  4723. - default:
  4724. - fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
  4725. - type, frame, q.ptr);
  4726. - fallthrough;
  4727. - case Q_TYPE_QH:
  4728. - case Q_TYPE_FSTN:
  4729. - /* End of the iTDs and siTDs */
  4730. - q.ptr = NULL;
  4731. - break;
  4732. - }
  4733. -
  4734. - /* assume completion callbacks modify the queue */
  4735. - if (unlikely(modified && fotg210->isoc_count > 0))
  4736. - return -EINVAL;
  4737. - }
  4738. - return 0;
  4739. -}
  4740. -
  4741. -static void scan_isoc(struct fotg210_hcd *fotg210)
  4742. -{
  4743. - unsigned uf, now_frame, frame, ret;
  4744. - unsigned fmask = fotg210->periodic_size - 1;
  4745. - bool live;
  4746. -
  4747. - /*
  4748. - * When running, scan from last scan point up to "now"
  4749. - * else clean up by scanning everything that's left.
  4750. - * Touches as few pages as possible: cache-friendly.
  4751. - */
  4752. - if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
  4753. - uf = fotg210_read_frame_index(fotg210);
  4754. - now_frame = (uf >> 3) & fmask;
  4755. - live = true;
  4756. - } else {
  4757. - now_frame = (fotg210->next_frame - 1) & fmask;
  4758. - live = false;
  4759. - }
  4760. - fotg210->now_frame = now_frame;
  4761. -
  4762. - frame = fotg210->next_frame;
  4763. - for (;;) {
  4764. - ret = 1;
  4765. - while (ret != 0)
  4766. - ret = scan_frame_queue(fotg210, frame,
  4767. - now_frame, live);
  4768. -
  4769. - /* Stop when we have reached the current frame */
  4770. - if (frame == now_frame)
  4771. - break;
  4772. - frame = (frame + 1) & fmask;
  4773. - }
  4774. - fotg210->next_frame = now_frame;
  4775. -}
  4776. -
  4777. -/* Display / Set uframe_periodic_max
  4778. - */
  4779. -static ssize_t uframe_periodic_max_show(struct device *dev,
  4780. - struct device_attribute *attr, char *buf)
  4781. -{
  4782. - struct fotg210_hcd *fotg210;
  4783. - int n;
  4784. -
  4785. - fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
  4786. - n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
  4787. - return n;
  4788. -}
  4789. -
  4790. -
  4791. -static ssize_t uframe_periodic_max_store(struct device *dev,
  4792. - struct device_attribute *attr, const char *buf, size_t count)
  4793. -{
  4794. - struct fotg210_hcd *fotg210;
  4795. - unsigned uframe_periodic_max;
  4796. - unsigned frame, uframe;
  4797. - unsigned short allocated_max;
  4798. - unsigned long flags;
  4799. - ssize_t ret;
  4800. -
  4801. - fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
  4802. - if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
  4803. - return -EINVAL;
  4804. -
  4805. - if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
  4806. - fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n",
  4807. - uframe_periodic_max);
  4808. - return -EINVAL;
  4809. - }
  4810. -
  4811. - ret = -EINVAL;
  4812. -
  4813. - /*
  4814. - * lock, so that our checking does not race with possible periodic
  4815. - * bandwidth allocation through submitting new urbs.
  4816. - */
  4817. - spin_lock_irqsave(&fotg210->lock, flags);
  4818. -
  4819. - /*
  4820. - * for request to decrease max periodic bandwidth, we have to check
  4821. - * every microframe in the schedule to see whether the decrease is
  4822. - * possible.
  4823. - */
  4824. - if (uframe_periodic_max < fotg210->uframe_periodic_max) {
  4825. - allocated_max = 0;
  4826. -
  4827. - for (frame = 0; frame < fotg210->periodic_size; ++frame)
  4828. - for (uframe = 0; uframe < 7; ++uframe)
  4829. - allocated_max = max(allocated_max,
  4830. - periodic_usecs(fotg210, frame,
  4831. - uframe));
  4832. -
  4833. - if (allocated_max > uframe_periodic_max) {
  4834. - fotg210_info(fotg210,
  4835. - "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n",
  4836. - allocated_max, uframe_periodic_max);
  4837. - goto out_unlock;
  4838. - }
  4839. - }
  4840. -
  4841. - /* increasing is always ok */
  4842. -
  4843. - fotg210_info(fotg210,
  4844. - "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
  4845. - 100 * uframe_periodic_max/125, uframe_periodic_max);
  4846. -
  4847. - if (uframe_periodic_max != 100)
  4848. - fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
  4849. -
  4850. - fotg210->uframe_periodic_max = uframe_periodic_max;
  4851. - ret = count;
  4852. -
  4853. -out_unlock:
  4854. - spin_unlock_irqrestore(&fotg210->lock, flags);
  4855. - return ret;
  4856. -}
  4857. -
  4858. -static DEVICE_ATTR_RW(uframe_periodic_max);
  4859. -
  4860. -static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
  4861. -{
  4862. - struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
  4863. -
  4864. - return device_create_file(controller, &dev_attr_uframe_periodic_max);
  4865. -}
  4866. -
  4867. -static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
  4868. -{
  4869. - struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
  4870. -
  4871. - device_remove_file(controller, &dev_attr_uframe_periodic_max);
  4872. -}
  4873. -/* On some systems, leaving remote wakeup enabled prevents system shutdown.
  4874. - * The firmware seems to think that powering off is a wakeup event!
  4875. - * This routine turns off remote wakeup and everything else, on all ports.
  4876. - */
  4877. -static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
  4878. -{
  4879. - u32 __iomem *status_reg = &fotg210->regs->port_status;
  4880. -
  4881. - fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
  4882. -}
  4883. -
  4884. -/* Halt HC, turn off all ports, and let the BIOS use the companion controllers.
  4885. - * Must be called with interrupts enabled and the lock not held.
  4886. - */
  4887. -static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
  4888. -{
  4889. - fotg210_halt(fotg210);
  4890. -
  4891. - spin_lock_irq(&fotg210->lock);
  4892. - fotg210->rh_state = FOTG210_RH_HALTED;
  4893. - fotg210_turn_off_all_ports(fotg210);
  4894. - spin_unlock_irq(&fotg210->lock);
  4895. -}
  4896. -
  4897. -/* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
  4898. - * This forcibly disables dma and IRQs, helping kexec and other cases
  4899. - * where the next system software may expect clean state.
  4900. - */
  4901. -static void fotg210_shutdown(struct usb_hcd *hcd)
  4902. -{
  4903. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4904. -
  4905. - spin_lock_irq(&fotg210->lock);
  4906. - fotg210->shutdown = true;
  4907. - fotg210->rh_state = FOTG210_RH_STOPPING;
  4908. - fotg210->enabled_hrtimer_events = 0;
  4909. - spin_unlock_irq(&fotg210->lock);
  4910. -
  4911. - fotg210_silence_controller(fotg210);
  4912. -
  4913. - hrtimer_cancel(&fotg210->hrtimer);
  4914. -}
  4915. -
  4916. -/* fotg210_work is called from some interrupts, timers, and so on.
  4917. - * it calls driver completion functions, after dropping fotg210->lock.
  4918. - */
  4919. -static void fotg210_work(struct fotg210_hcd *fotg210)
  4920. -{
  4921. - /* another CPU may drop fotg210->lock during a schedule scan while
  4922. - * it reports urb completions. this flag guards against bogus
  4923. - * attempts at re-entrant schedule scanning.
  4924. - */
  4925. - if (fotg210->scanning) {
  4926. - fotg210->need_rescan = true;
  4927. - return;
  4928. - }
  4929. - fotg210->scanning = true;
  4930. -
  4931. -rescan:
  4932. - fotg210->need_rescan = false;
  4933. - if (fotg210->async_count)
  4934. - scan_async(fotg210);
  4935. - if (fotg210->intr_count > 0)
  4936. - scan_intr(fotg210);
  4937. - if (fotg210->isoc_count > 0)
  4938. - scan_isoc(fotg210);
  4939. - if (fotg210->need_rescan)
  4940. - goto rescan;
  4941. - fotg210->scanning = false;
  4942. -
  4943. - /* the IO watchdog guards against hardware or driver bugs that
  4944. - * misplace IRQs, and should let us run completely without IRQs.
  4945. - * such lossage has been observed on both VT6202 and VT8235.
  4946. - */
  4947. - turn_on_io_watchdog(fotg210);
  4948. -}
  4949. -
  4950. -/* Called when the fotg210_hcd module is removed.
  4951. - */
  4952. -static void fotg210_stop(struct usb_hcd *hcd)
  4953. -{
  4954. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4955. -
  4956. - fotg210_dbg(fotg210, "stop\n");
  4957. -
  4958. - /* no more interrupts ... */
  4959. -
  4960. - spin_lock_irq(&fotg210->lock);
  4961. - fotg210->enabled_hrtimer_events = 0;
  4962. - spin_unlock_irq(&fotg210->lock);
  4963. -
  4964. - fotg210_quiesce(fotg210);
  4965. - fotg210_silence_controller(fotg210);
  4966. - fotg210_reset(fotg210);
  4967. -
  4968. - hrtimer_cancel(&fotg210->hrtimer);
  4969. - remove_sysfs_files(fotg210);
  4970. - remove_debug_files(fotg210);
  4971. -
  4972. - /* root hub is shut down separately (first, when possible) */
  4973. - spin_lock_irq(&fotg210->lock);
  4974. - end_free_itds(fotg210);
  4975. - spin_unlock_irq(&fotg210->lock);
  4976. - fotg210_mem_cleanup(fotg210);
  4977. -
  4978. -#ifdef FOTG210_STATS
  4979. - fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
  4980. - fotg210->stats.normal, fotg210->stats.error,
  4981. - fotg210->stats.iaa, fotg210->stats.lost_iaa);
  4982. - fotg210_dbg(fotg210, "complete %ld unlink %ld\n",
  4983. - fotg210->stats.complete, fotg210->stats.unlink);
  4984. -#endif
  4985. -
  4986. - dbg_status(fotg210, "fotg210_stop completed",
  4987. - fotg210_readl(fotg210, &fotg210->regs->status));
  4988. -}
  4989. -
  4990. -/* one-time init, only for memory state */
  4991. -static int hcd_fotg210_init(struct usb_hcd *hcd)
  4992. -{
  4993. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  4994. - u32 temp;
  4995. - int retval;
  4996. - u32 hcc_params;
  4997. - struct fotg210_qh_hw *hw;
  4998. -
  4999. - spin_lock_init(&fotg210->lock);
  5000. -
  5001. - /*
  5002. - * keep io watchdog by default, those good HCDs could turn off it later
  5003. - */
  5004. - fotg210->need_io_watchdog = 1;
  5005. -
  5006. - hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  5007. - fotg210->hrtimer.function = fotg210_hrtimer_func;
  5008. - fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
  5009. -
  5010. - hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  5011. -
  5012. - /*
  5013. - * by default set standard 80% (== 100 usec/uframe) max periodic
  5014. - * bandwidth as required by USB 2.0
  5015. - */
  5016. - fotg210->uframe_periodic_max = 100;
  5017. -
  5018. - /*
  5019. - * hw default: 1K periodic list heads, one per frame.
  5020. - * periodic_size can shrink by USBCMD update if hcc_params allows.
  5021. - */
  5022. - fotg210->periodic_size = DEFAULT_I_TDPS;
  5023. - INIT_LIST_HEAD(&fotg210->intr_qh_list);
  5024. - INIT_LIST_HEAD(&fotg210->cached_itd_list);
  5025. -
  5026. - if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  5027. - /* periodic schedule size can be smaller than default */
  5028. - switch (FOTG210_TUNE_FLS) {
  5029. - case 0:
  5030. - fotg210->periodic_size = 1024;
  5031. - break;
  5032. - case 1:
  5033. - fotg210->periodic_size = 512;
  5034. - break;
  5035. - case 2:
  5036. - fotg210->periodic_size = 256;
  5037. - break;
  5038. - default:
  5039. - BUG();
  5040. - }
  5041. - }
  5042. - retval = fotg210_mem_init(fotg210, GFP_KERNEL);
  5043. - if (retval < 0)
  5044. - return retval;
  5045. -
  5046. - /* controllers may cache some of the periodic schedule ... */
  5047. - fotg210->i_thresh = 2;
  5048. -
  5049. - /*
  5050. - * dedicate a qh for the async ring head, since we couldn't unlink
  5051. - * a 'real' qh without stopping the async schedule [4.8]. use it
  5052. - * as the 'reclamation list head' too.
  5053. - * its dummy is used in hw_alt_next of many tds, to prevent the qh
  5054. - * from automatically advancing to the next td after short reads.
  5055. - */
  5056. - fotg210->async->qh_next.qh = NULL;
  5057. - hw = fotg210->async->hw;
  5058. - hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma);
  5059. - hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD);
  5060. - hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
  5061. - hw->hw_qtd_next = FOTG210_LIST_END(fotg210);
  5062. - fotg210->async->qh_state = QH_STATE_LINKED;
  5063. - hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma);
  5064. -
  5065. - /* clear interrupt enables, set irq latency */
  5066. - if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  5067. - log2_irq_thresh = 0;
  5068. - temp = 1 << (16 + log2_irq_thresh);
  5069. - if (HCC_CANPARK(hcc_params)) {
  5070. - /* HW default park == 3, on hardware that supports it (like
  5071. - * NVidia and ALI silicon), maximizes throughput on the async
  5072. - * schedule by avoiding QH fetches between transfers.
  5073. - *
  5074. - * With fast usb storage devices and NForce2, "park" seems to
  5075. - * make problems: throughput reduction (!), data errors...
  5076. - */
  5077. - if (park) {
  5078. - park = min_t(unsigned, park, 3);
  5079. - temp |= CMD_PARK;
  5080. - temp |= park << 8;
  5081. - }
  5082. - fotg210_dbg(fotg210, "park %d\n", park);
  5083. - }
  5084. - if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  5085. - /* periodic schedule size can be smaller than default */
  5086. - temp &= ~(3 << 2);
  5087. - temp |= (FOTG210_TUNE_FLS << 2);
  5088. - }
  5089. - fotg210->command = temp;
  5090. -
  5091. - /* Accept arbitrarily long scatter-gather lists */
  5092. - if (!hcd->localmem_pool)
  5093. - hcd->self.sg_tablesize = ~0;
  5094. - return 0;
  5095. -}
  5096. -
  5097. -/* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
  5098. -static int fotg210_run(struct usb_hcd *hcd)
  5099. -{
  5100. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5101. - u32 temp;
  5102. -
  5103. - hcd->uses_new_polling = 1;
  5104. -
  5105. - /* EHCI spec section 4.1 */
  5106. -
  5107. - fotg210_writel(fotg210, fotg210->periodic_dma,
  5108. - &fotg210->regs->frame_list);
  5109. - fotg210_writel(fotg210, (u32)fotg210->async->qh_dma,
  5110. - &fotg210->regs->async_next);
  5111. -
  5112. - /*
  5113. - * hcc_params controls whether fotg210->regs->segment must (!!!)
  5114. - * be used; it constrains QH/ITD/SITD and QTD locations.
  5115. - * dma_pool consistent memory always uses segment zero.
  5116. - * streaming mappings for I/O buffers, like dma_map_single(),
  5117. - * can return segments above 4GB, if the device allows.
  5118. - *
  5119. - * NOTE: the dma mask is visible through dev->dma_mask, so
  5120. - * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  5121. - * Scsi_Host.highmem_io, and so forth. It's readonly to all
  5122. - * host side drivers though.
  5123. - */
  5124. - fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  5125. -
  5126. - /*
  5127. - * Philips, Intel, and maybe others need CMD_RUN before the
  5128. - * root hub will detect new devices (why?); NEC doesn't
  5129. - */
  5130. - fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  5131. - fotg210->command |= CMD_RUN;
  5132. - fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  5133. - dbg_cmd(fotg210, "init", fotg210->command);
  5134. -
  5135. - /*
  5136. - * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  5137. - * are explicitly handed to companion controller(s), so no TT is
  5138. - * involved with the root hub. (Except where one is integrated,
  5139. - * and there's no companion controller unless maybe for USB OTG.)
  5140. - *
  5141. - * Turning on the CF flag will transfer ownership of all ports
  5142. - * from the companions to the EHCI controller. If any of the
  5143. - * companions are in the middle of a port reset at the time, it
  5144. - * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  5145. - * guarantees that no resets are in progress. After we set CF,
  5146. - * a short delay lets the hardware catch up; new resets shouldn't
  5147. - * be started before the port switching actions could complete.
  5148. - */
  5149. - down_write(&ehci_cf_port_reset_rwsem);
  5150. - fotg210->rh_state = FOTG210_RH_RUNNING;
  5151. - /* unblock posted writes */
  5152. - fotg210_readl(fotg210, &fotg210->regs->command);
  5153. - usleep_range(5000, 10000);
  5154. - up_write(&ehci_cf_port_reset_rwsem);
  5155. - fotg210->last_periodic_enable = ktime_get_real();
  5156. -
  5157. - temp = HC_VERSION(fotg210,
  5158. - fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
  5159. - fotg210_info(fotg210,
  5160. - "USB %x.%x started, EHCI %x.%02x\n",
  5161. - ((fotg210->sbrn & 0xf0) >> 4), (fotg210->sbrn & 0x0f),
  5162. - temp >> 8, temp & 0xff);
  5163. -
  5164. - fotg210_writel(fotg210, INTR_MASK,
  5165. - &fotg210->regs->intr_enable); /* Turn On Interrupts */
  5166. -
  5167. - /* GRR this is run-once init(), being done every time the HC starts.
  5168. - * So long as they're part of class devices, we can't do it init()
  5169. - * since the class device isn't created that early.
  5170. - */
  5171. - create_debug_files(fotg210);
  5172. - create_sysfs_files(fotg210);
  5173. -
  5174. - return 0;
  5175. -}
  5176. -
  5177. -static int fotg210_setup(struct usb_hcd *hcd)
  5178. -{
  5179. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5180. - int retval;
  5181. -
  5182. - fotg210->regs = (void __iomem *)fotg210->caps +
  5183. - HC_LENGTH(fotg210,
  5184. - fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
  5185. - dbg_hcs_params(fotg210, "reset");
  5186. - dbg_hcc_params(fotg210, "reset");
  5187. -
  5188. - /* cache this readonly data; minimize chip reads */
  5189. - fotg210->hcs_params = fotg210_readl(fotg210,
  5190. - &fotg210->caps->hcs_params);
  5191. -
  5192. - fotg210->sbrn = HCD_USB2;
  5193. -
  5194. - /* data structure init */
  5195. - retval = hcd_fotg210_init(hcd);
  5196. - if (retval)
  5197. - return retval;
  5198. -
  5199. - retval = fotg210_halt(fotg210);
  5200. - if (retval)
  5201. - return retval;
  5202. -
  5203. - fotg210_reset(fotg210);
  5204. -
  5205. - return 0;
  5206. -}
  5207. -
  5208. -static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
  5209. -{
  5210. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5211. - u32 status, masked_status, pcd_status = 0, cmd;
  5212. - int bh;
  5213. -
  5214. - spin_lock(&fotg210->lock);
  5215. -
  5216. - status = fotg210_readl(fotg210, &fotg210->regs->status);
  5217. -
  5218. - /* e.g. cardbus physical eject */
  5219. - if (status == ~(u32) 0) {
  5220. - fotg210_dbg(fotg210, "device removed\n");
  5221. - goto dead;
  5222. - }
  5223. -
  5224. - /*
  5225. - * We don't use STS_FLR, but some controllers don't like it to
  5226. - * remain on, so mask it out along with the other status bits.
  5227. - */
  5228. - masked_status = status & (INTR_MASK | STS_FLR);
  5229. -
  5230. - /* Shared IRQ? */
  5231. - if (!masked_status ||
  5232. - unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
  5233. - spin_unlock(&fotg210->lock);
  5234. - return IRQ_NONE;
  5235. - }
  5236. -
  5237. - /* clear (just) interrupts */
  5238. - fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
  5239. - cmd = fotg210_readl(fotg210, &fotg210->regs->command);
  5240. - bh = 0;
  5241. -
  5242. - /* unrequested/ignored: Frame List Rollover */
  5243. - dbg_status(fotg210, "irq", status);
  5244. -
  5245. - /* INT, ERR, and IAA interrupt rates can be throttled */
  5246. -
  5247. - /* normal [4.15.1.2] or error [4.15.1.1] completion */
  5248. - if (likely((status & (STS_INT|STS_ERR)) != 0)) {
  5249. - if (likely((status & STS_ERR) == 0))
  5250. - INCR(fotg210->stats.normal);
  5251. - else
  5252. - INCR(fotg210->stats.error);
  5253. - bh = 1;
  5254. - }
  5255. -
  5256. - /* complete the unlinking of some qh [4.15.2.3] */
  5257. - if (status & STS_IAA) {
  5258. -
  5259. - /* Turn off the IAA watchdog */
  5260. - fotg210->enabled_hrtimer_events &=
  5261. - ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
  5262. -
  5263. - /*
  5264. - * Mild optimization: Allow another IAAD to reset the
  5265. - * hrtimer, if one occurs before the next expiration.
  5266. - * In theory we could always cancel the hrtimer, but
  5267. - * tests show that about half the time it will be reset
  5268. - * for some other event anyway.
  5269. - */
  5270. - if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
  5271. - ++fotg210->next_hrtimer_event;
  5272. -
  5273. - /* guard against (alleged) silicon errata */
  5274. - if (cmd & CMD_IAAD)
  5275. - fotg210_dbg(fotg210, "IAA with IAAD still set?\n");
  5276. - if (fotg210->async_iaa) {
  5277. - INCR(fotg210->stats.iaa);
  5278. - end_unlink_async(fotg210);
  5279. - } else
  5280. - fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
  5281. - }
  5282. -
  5283. - /* remote wakeup [4.3.1] */
  5284. - if (status & STS_PCD) {
  5285. - int pstatus;
  5286. - u32 __iomem *status_reg = &fotg210->regs->port_status;
  5287. -
  5288. - /* kick root hub later */
  5289. - pcd_status = status;
  5290. -
  5291. - /* resume root hub? */
  5292. - if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
  5293. - usb_hcd_resume_root_hub(hcd);
  5294. -
  5295. - pstatus = fotg210_readl(fotg210, status_reg);
  5296. -
  5297. - if (test_bit(0, &fotg210->suspended_ports) &&
  5298. - ((pstatus & PORT_RESUME) ||
  5299. - !(pstatus & PORT_SUSPEND)) &&
  5300. - (pstatus & PORT_PE) &&
  5301. - fotg210->reset_done[0] == 0) {
  5302. -
  5303. - /* start 20 msec resume signaling from this port,
  5304. - * and make hub_wq collect PORT_STAT_C_SUSPEND to
  5305. - * stop that signaling. Use 5 ms extra for safety,
  5306. - * like usb_port_resume() does.
  5307. - */
  5308. - fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25);
  5309. - set_bit(0, &fotg210->resuming_ports);
  5310. - fotg210_dbg(fotg210, "port 1 remote wakeup\n");
  5311. - mod_timer(&hcd->rh_timer, fotg210->reset_done[0]);
  5312. - }
  5313. - }
  5314. -
  5315. - /* PCI errors [4.15.2.4] */
  5316. - if (unlikely((status & STS_FATAL) != 0)) {
  5317. - fotg210_err(fotg210, "fatal error\n");
  5318. - dbg_cmd(fotg210, "fatal", cmd);
  5319. - dbg_status(fotg210, "fatal", status);
  5320. -dead:
  5321. - usb_hc_died(hcd);
  5322. -
  5323. - /* Don't let the controller do anything more */
  5324. - fotg210->shutdown = true;
  5325. - fotg210->rh_state = FOTG210_RH_STOPPING;
  5326. - fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
  5327. - fotg210_writel(fotg210, fotg210->command,
  5328. - &fotg210->regs->command);
  5329. - fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  5330. - fotg210_handle_controller_death(fotg210);
  5331. -
  5332. - /* Handle completions when the controller stops */
  5333. - bh = 0;
  5334. - }
  5335. -
  5336. - if (bh)
  5337. - fotg210_work(fotg210);
  5338. - spin_unlock(&fotg210->lock);
  5339. - if (pcd_status)
  5340. - usb_hcd_poll_rh_status(hcd);
  5341. - return IRQ_HANDLED;
  5342. -}
  5343. -
  5344. -/* non-error returns are a promise to giveback() the urb later
  5345. - * we drop ownership so next owner (or urb unlink) can get it
  5346. - *
  5347. - * urb + dev is in hcd.self.controller.urb_list
  5348. - * we're queueing TDs onto software and hardware lists
  5349. - *
  5350. - * hcd-specific init for hcpriv hasn't been done yet
  5351. - *
  5352. - * NOTE: control, bulk, and interrupt share the same code to append TDs
  5353. - * to a (possibly active) QH, and the same QH scanning code.
  5354. - */
  5355. -static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  5356. - gfp_t mem_flags)
  5357. -{
  5358. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5359. - struct list_head qtd_list;
  5360. -
  5361. - INIT_LIST_HEAD(&qtd_list);
  5362. -
  5363. - switch (usb_pipetype(urb->pipe)) {
  5364. - case PIPE_CONTROL:
  5365. - /* qh_completions() code doesn't handle all the fault cases
  5366. - * in multi-TD control transfers. Even 1KB is rare anyway.
  5367. - */
  5368. - if (urb->transfer_buffer_length > (16 * 1024))
  5369. - return -EMSGSIZE;
  5370. - fallthrough;
  5371. - /* case PIPE_BULK: */
  5372. - default:
  5373. - if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
  5374. - return -ENOMEM;
  5375. - return submit_async(fotg210, urb, &qtd_list, mem_flags);
  5376. -
  5377. - case PIPE_INTERRUPT:
  5378. - if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
  5379. - return -ENOMEM;
  5380. - return intr_submit(fotg210, urb, &qtd_list, mem_flags);
  5381. -
  5382. - case PIPE_ISOCHRONOUS:
  5383. - return itd_submit(fotg210, urb, mem_flags);
  5384. - }
  5385. -}
  5386. -
  5387. -/* remove from hardware lists
  5388. - * completions normally happen asynchronously
  5389. - */
  5390. -
  5391. -static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  5392. -{
  5393. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5394. - struct fotg210_qh *qh;
  5395. - unsigned long flags;
  5396. - int rc;
  5397. -
  5398. - spin_lock_irqsave(&fotg210->lock, flags);
  5399. - rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  5400. - if (rc)
  5401. - goto done;
  5402. -
  5403. - switch (usb_pipetype(urb->pipe)) {
  5404. - /* case PIPE_CONTROL: */
  5405. - /* case PIPE_BULK:*/
  5406. - default:
  5407. - qh = (struct fotg210_qh *) urb->hcpriv;
  5408. - if (!qh)
  5409. - break;
  5410. - switch (qh->qh_state) {
  5411. - case QH_STATE_LINKED:
  5412. - case QH_STATE_COMPLETING:
  5413. - start_unlink_async(fotg210, qh);
  5414. - break;
  5415. - case QH_STATE_UNLINK:
  5416. - case QH_STATE_UNLINK_WAIT:
  5417. - /* already started */
  5418. - break;
  5419. - case QH_STATE_IDLE:
  5420. - /* QH might be waiting for a Clear-TT-Buffer */
  5421. - qh_completions(fotg210, qh);
  5422. - break;
  5423. - }
  5424. - break;
  5425. -
  5426. - case PIPE_INTERRUPT:
  5427. - qh = (struct fotg210_qh *) urb->hcpriv;
  5428. - if (!qh)
  5429. - break;
  5430. - switch (qh->qh_state) {
  5431. - case QH_STATE_LINKED:
  5432. - case QH_STATE_COMPLETING:
  5433. - start_unlink_intr(fotg210, qh);
  5434. - break;
  5435. - case QH_STATE_IDLE:
  5436. - qh_completions(fotg210, qh);
  5437. - break;
  5438. - default:
  5439. - fotg210_dbg(fotg210, "bogus qh %p state %d\n",
  5440. - qh, qh->qh_state);
  5441. - goto done;
  5442. - }
  5443. - break;
  5444. -
  5445. - case PIPE_ISOCHRONOUS:
  5446. - /* itd... */
  5447. -
  5448. - /* wait till next completion, do it then. */
  5449. - /* completion irqs can wait up to 1024 msec, */
  5450. - break;
  5451. - }
  5452. -done:
  5453. - spin_unlock_irqrestore(&fotg210->lock, flags);
  5454. - return rc;
  5455. -}
  5456. -
  5457. -/* bulk qh holds the data toggle */
  5458. -
  5459. -static void fotg210_endpoint_disable(struct usb_hcd *hcd,
  5460. - struct usb_host_endpoint *ep)
  5461. -{
  5462. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5463. - unsigned long flags;
  5464. - struct fotg210_qh *qh, *tmp;
  5465. -
  5466. - /* ASSERT: any requests/urbs are being unlinked */
  5467. - /* ASSERT: nobody can be submitting urbs for this any more */
  5468. -
  5469. -rescan:
  5470. - spin_lock_irqsave(&fotg210->lock, flags);
  5471. - qh = ep->hcpriv;
  5472. - if (!qh)
  5473. - goto done;
  5474. -
  5475. - /* endpoints can be iso streams. for now, we don't
  5476. - * accelerate iso completions ... so spin a while.
  5477. - */
  5478. - if (qh->hw == NULL) {
  5479. - struct fotg210_iso_stream *stream = ep->hcpriv;
  5480. -
  5481. - if (!list_empty(&stream->td_list))
  5482. - goto idle_timeout;
  5483. -
  5484. - /* BUG_ON(!list_empty(&stream->free_list)); */
  5485. - kfree(stream);
  5486. - goto done;
  5487. - }
  5488. -
  5489. - if (fotg210->rh_state < FOTG210_RH_RUNNING)
  5490. - qh->qh_state = QH_STATE_IDLE;
  5491. - switch (qh->qh_state) {
  5492. - case QH_STATE_LINKED:
  5493. - case QH_STATE_COMPLETING:
  5494. - for (tmp = fotg210->async->qh_next.qh;
  5495. - tmp && tmp != qh;
  5496. - tmp = tmp->qh_next.qh)
  5497. - continue;
  5498. - /* periodic qh self-unlinks on empty, and a COMPLETING qh
  5499. - * may already be unlinked.
  5500. - */
  5501. - if (tmp)
  5502. - start_unlink_async(fotg210, qh);
  5503. - fallthrough;
  5504. - case QH_STATE_UNLINK: /* wait for hw to finish? */
  5505. - case QH_STATE_UNLINK_WAIT:
  5506. -idle_timeout:
  5507. - spin_unlock_irqrestore(&fotg210->lock, flags);
  5508. - schedule_timeout_uninterruptible(1);
  5509. - goto rescan;
  5510. - case QH_STATE_IDLE: /* fully unlinked */
  5511. - if (qh->clearing_tt)
  5512. - goto idle_timeout;
  5513. - if (list_empty(&qh->qtd_list)) {
  5514. - qh_destroy(fotg210, qh);
  5515. - break;
  5516. - }
  5517. - fallthrough;
  5518. - default:
  5519. - /* caller was supposed to have unlinked any requests;
  5520. - * that's not our job. just leak this memory.
  5521. - */
  5522. - fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n",
  5523. - qh, ep->desc.bEndpointAddress, qh->qh_state,
  5524. - list_empty(&qh->qtd_list) ? "" : "(has tds)");
  5525. - break;
  5526. - }
  5527. -done:
  5528. - ep->hcpriv = NULL;
  5529. - spin_unlock_irqrestore(&fotg210->lock, flags);
  5530. -}
  5531. -
  5532. -static void fotg210_endpoint_reset(struct usb_hcd *hcd,
  5533. - struct usb_host_endpoint *ep)
  5534. -{
  5535. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5536. - struct fotg210_qh *qh;
  5537. - int eptype = usb_endpoint_type(&ep->desc);
  5538. - int epnum = usb_endpoint_num(&ep->desc);
  5539. - int is_out = usb_endpoint_dir_out(&ep->desc);
  5540. - unsigned long flags;
  5541. -
  5542. - if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
  5543. - return;
  5544. -
  5545. - spin_lock_irqsave(&fotg210->lock, flags);
  5546. - qh = ep->hcpriv;
  5547. -
  5548. - /* For Bulk and Interrupt endpoints we maintain the toggle state
  5549. - * in the hardware; the toggle bits in udev aren't used at all.
  5550. - * When an endpoint is reset by usb_clear_halt() we must reset
  5551. - * the toggle bit in the QH.
  5552. - */
  5553. - if (qh) {
  5554. - usb_settoggle(qh->dev, epnum, is_out, 0);
  5555. - if (!list_empty(&qh->qtd_list)) {
  5556. - WARN_ONCE(1, "clear_halt for a busy endpoint\n");
  5557. - } else if (qh->qh_state == QH_STATE_LINKED ||
  5558. - qh->qh_state == QH_STATE_COMPLETING) {
  5559. -
  5560. - /* The toggle value in the QH can't be updated
  5561. - * while the QH is active. Unlink it now;
  5562. - * re-linking will call qh_refresh().
  5563. - */
  5564. - if (eptype == USB_ENDPOINT_XFER_BULK)
  5565. - start_unlink_async(fotg210, qh);
  5566. - else
  5567. - start_unlink_intr(fotg210, qh);
  5568. - }
  5569. - }
  5570. - spin_unlock_irqrestore(&fotg210->lock, flags);
  5571. -}
  5572. -
  5573. -static int fotg210_get_frame(struct usb_hcd *hcd)
  5574. -{
  5575. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5576. -
  5577. - return (fotg210_read_frame_index(fotg210) >> 3) %
  5578. - fotg210->periodic_size;
  5579. -}
  5580. -
  5581. -/* The EHCI in ChipIdea HDRC cannot be a separate module or device,
  5582. - * because its registers (and irq) are shared between host/gadget/otg
  5583. - * functions and in order to facilitate role switching we cannot
  5584. - * give the fotg210 driver exclusive access to those.
  5585. - */
  5586. -MODULE_DESCRIPTION(DRIVER_DESC);
  5587. -MODULE_AUTHOR(DRIVER_AUTHOR);
  5588. -MODULE_LICENSE("GPL");
  5589. -
  5590. -static const struct hc_driver fotg210_fotg210_hc_driver = {
  5591. - .description = hcd_name,
  5592. - .product_desc = "Faraday USB2.0 Host Controller",
  5593. - .hcd_priv_size = sizeof(struct fotg210_hcd),
  5594. -
  5595. - /*
  5596. - * generic hardware linkage
  5597. - */
  5598. - .irq = fotg210_irq,
  5599. - .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
  5600. -
  5601. - /*
  5602. - * basic lifecycle operations
  5603. - */
  5604. - .reset = hcd_fotg210_init,
  5605. - .start = fotg210_run,
  5606. - .stop = fotg210_stop,
  5607. - .shutdown = fotg210_shutdown,
  5608. -
  5609. - /*
  5610. - * managing i/o requests and associated device resources
  5611. - */
  5612. - .urb_enqueue = fotg210_urb_enqueue,
  5613. - .urb_dequeue = fotg210_urb_dequeue,
  5614. - .endpoint_disable = fotg210_endpoint_disable,
  5615. - .endpoint_reset = fotg210_endpoint_reset,
  5616. -
  5617. - /*
  5618. - * scheduling support
  5619. - */
  5620. - .get_frame_number = fotg210_get_frame,
  5621. -
  5622. - /*
  5623. - * root hub support
  5624. - */
  5625. - .hub_status_data = fotg210_hub_status_data,
  5626. - .hub_control = fotg210_hub_control,
  5627. - .bus_suspend = fotg210_bus_suspend,
  5628. - .bus_resume = fotg210_bus_resume,
  5629. -
  5630. - .relinquish_port = fotg210_relinquish_port,
  5631. - .port_handed_over = fotg210_port_handed_over,
  5632. -
  5633. - .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
  5634. -};
  5635. -
  5636. -static void fotg210_init(struct fotg210_hcd *fotg210)
  5637. -{
  5638. - u32 value;
  5639. -
  5640. - iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
  5641. - &fotg210->regs->gmir);
  5642. -
  5643. - value = ioread32(&fotg210->regs->otgcsr);
  5644. - value &= ~OTGCSR_A_BUS_DROP;
  5645. - value |= OTGCSR_A_BUS_REQ;
  5646. - iowrite32(value, &fotg210->regs->otgcsr);
  5647. -}
  5648. -
  5649. -/*
  5650. - * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
  5651. - *
  5652. - * Allocates basic resources for this USB host controller, and
  5653. - * then invokes the start() method for the HCD associated with it
  5654. - * through the hotplug entry's driver_data.
  5655. - */
  5656. -static int fotg210_hcd_probe(struct platform_device *pdev)
  5657. -{
  5658. - struct device *dev = &pdev->dev;
  5659. - struct usb_hcd *hcd;
  5660. - struct resource *res;
  5661. - int irq;
  5662. - int retval;
  5663. - struct fotg210_hcd *fotg210;
  5664. -
  5665. - if (usb_disabled())
  5666. - return -ENODEV;
  5667. -
  5668. - pdev->dev.power.power_state = PMSG_ON;
  5669. -
  5670. - irq = platform_get_irq(pdev, 0);
  5671. - if (irq < 0)
  5672. - return irq;
  5673. -
  5674. - hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
  5675. - dev_name(dev));
  5676. - if (!hcd) {
  5677. - dev_err(dev, "failed to create hcd\n");
  5678. - retval = -ENOMEM;
  5679. - goto fail_create_hcd;
  5680. - }
  5681. -
  5682. - hcd->has_tt = 1;
  5683. -
  5684. - res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  5685. - hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  5686. - if (IS_ERR(hcd->regs)) {
  5687. - retval = PTR_ERR(hcd->regs);
  5688. - goto failed_put_hcd;
  5689. - }
  5690. -
  5691. - hcd->rsrc_start = res->start;
  5692. - hcd->rsrc_len = resource_size(res);
  5693. -
  5694. - fotg210 = hcd_to_fotg210(hcd);
  5695. -
  5696. - fotg210->caps = hcd->regs;
  5697. -
  5698. - /* It's OK not to supply this clock */
  5699. - fotg210->pclk = clk_get(dev, "PCLK");
  5700. - if (!IS_ERR(fotg210->pclk)) {
  5701. - retval = clk_prepare_enable(fotg210->pclk);
  5702. - if (retval) {
  5703. - dev_err(dev, "failed to enable PCLK\n");
  5704. - goto failed_put_hcd;
  5705. - }
  5706. - } else if (PTR_ERR(fotg210->pclk) == -EPROBE_DEFER) {
  5707. - /*
  5708. - * Percolate deferrals, for anything else,
  5709. - * just live without the clocking.
  5710. - */
  5711. - retval = PTR_ERR(fotg210->pclk);
  5712. - goto failed_dis_clk;
  5713. - }
  5714. -
  5715. - retval = fotg210_setup(hcd);
  5716. - if (retval)
  5717. - goto failed_dis_clk;
  5718. -
  5719. - fotg210_init(fotg210);
  5720. -
  5721. - retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  5722. - if (retval) {
  5723. - dev_err(dev, "failed to add hcd with err %d\n", retval);
  5724. - goto failed_dis_clk;
  5725. - }
  5726. - device_wakeup_enable(hcd->self.controller);
  5727. - platform_set_drvdata(pdev, hcd);
  5728. -
  5729. - return retval;
  5730. -
  5731. -failed_dis_clk:
  5732. - if (!IS_ERR(fotg210->pclk)) {
  5733. - clk_disable_unprepare(fotg210->pclk);
  5734. - clk_put(fotg210->pclk);
  5735. - }
  5736. -failed_put_hcd:
  5737. - usb_put_hcd(hcd);
  5738. -fail_create_hcd:
  5739. - dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
  5740. - return retval;
  5741. -}
  5742. -
  5743. -/*
  5744. - * fotg210_hcd_remove - shutdown processing for EHCI HCDs
  5745. - * @dev: USB Host Controller being removed
  5746. - *
  5747. - */
  5748. -static int fotg210_hcd_remove(struct platform_device *pdev)
  5749. -{
  5750. - struct usb_hcd *hcd = platform_get_drvdata(pdev);
  5751. - struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  5752. -
  5753. - if (!IS_ERR(fotg210->pclk)) {
  5754. - clk_disable_unprepare(fotg210->pclk);
  5755. - clk_put(fotg210->pclk);
  5756. - }
  5757. -
  5758. - usb_remove_hcd(hcd);
  5759. - usb_put_hcd(hcd);
  5760. -
  5761. - return 0;
  5762. -}
  5763. -
  5764. -#ifdef CONFIG_OF
  5765. -static const struct of_device_id fotg210_of_match[] = {
  5766. - { .compatible = "faraday,fotg210" },
  5767. - {},
  5768. -};
  5769. -MODULE_DEVICE_TABLE(of, fotg210_of_match);
  5770. -#endif
  5771. -
  5772. -static struct platform_driver fotg210_hcd_driver = {
  5773. - .driver = {
  5774. - .name = "fotg210-hcd",
  5775. - .of_match_table = of_match_ptr(fotg210_of_match),
  5776. - },
  5777. - .probe = fotg210_hcd_probe,
  5778. - .remove = fotg210_hcd_remove,
  5779. -};
  5780. -
  5781. -static int __init fotg210_hcd_init(void)
  5782. -{
  5783. - int retval = 0;
  5784. -
  5785. - if (usb_disabled())
  5786. - return -ENODEV;
  5787. -
  5788. - set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  5789. - if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
  5790. - test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
  5791. - pr_warn("Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
  5792. -
  5793. - pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd\n",
  5794. - hcd_name, sizeof(struct fotg210_qh),
  5795. - sizeof(struct fotg210_qtd),
  5796. - sizeof(struct fotg210_itd));
  5797. -
  5798. - fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
  5799. -
  5800. - retval = platform_driver_register(&fotg210_hcd_driver);
  5801. - if (retval < 0)
  5802. - goto clean;
  5803. - return retval;
  5804. -
  5805. -clean:
  5806. - debugfs_remove(fotg210_debug_root);
  5807. - fotg210_debug_root = NULL;
  5808. -
  5809. - clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  5810. - return retval;
  5811. -}
  5812. -module_init(fotg210_hcd_init);
  5813. -
  5814. -static void __exit fotg210_hcd_cleanup(void)
  5815. -{
  5816. - platform_driver_unregister(&fotg210_hcd_driver);
  5817. - debugfs_remove(fotg210_debug_root);
  5818. - clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  5819. -}
  5820. -module_exit(fotg210_hcd_cleanup);
  5821. --- /dev/null
  5822. +++ b/drivers/usb/fotg210/fotg210-hcd.c
  5823. @@ -0,0 +1,5727 @@
  5824. +// SPDX-License-Identifier: GPL-2.0+
  5825. +/* Faraday FOTG210 EHCI-like driver
  5826. + *
  5827. + * Copyright (c) 2013 Faraday Technology Corporation
  5828. + *
  5829. + * Author: Yuan-Hsin Chen <[email protected]>
  5830. + * Feng-Hsin Chiang <[email protected]>
  5831. + * Po-Yu Chuang <[email protected]>
  5832. + *
  5833. + * Most of code borrowed from the Linux-3.7 EHCI driver
  5834. + */
  5835. +#include <linux/module.h>
  5836. +#include <linux/of.h>
  5837. +#include <linux/device.h>
  5838. +#include <linux/dmapool.h>
  5839. +#include <linux/kernel.h>
  5840. +#include <linux/delay.h>
  5841. +#include <linux/ioport.h>
  5842. +#include <linux/sched.h>
  5843. +#include <linux/vmalloc.h>
  5844. +#include <linux/errno.h>
  5845. +#include <linux/init.h>
  5846. +#include <linux/hrtimer.h>
  5847. +#include <linux/list.h>
  5848. +#include <linux/interrupt.h>
  5849. +#include <linux/usb.h>
  5850. +#include <linux/usb/hcd.h>
  5851. +#include <linux/moduleparam.h>
  5852. +#include <linux/dma-mapping.h>
  5853. +#include <linux/debugfs.h>
  5854. +#include <linux/slab.h>
  5855. +#include <linux/uaccess.h>
  5856. +#include <linux/platform_device.h>
  5857. +#include <linux/io.h>
  5858. +#include <linux/iopoll.h>
  5859. +#include <linux/clk.h>
  5860. +
  5861. +#include <asm/byteorder.h>
  5862. +#include <asm/irq.h>
  5863. +#include <asm/unaligned.h>
  5864. +
  5865. +#define DRIVER_AUTHOR "Yuan-Hsin Chen"
  5866. +#define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
  5867. +static const char hcd_name[] = "fotg210_hcd";
  5868. +
  5869. +#undef FOTG210_URB_TRACE
  5870. +#define FOTG210_STATS
  5871. +
  5872. +/* magic numbers that can affect system performance */
  5873. +#define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  5874. +#define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  5875. +#define FOTG210_TUNE_RL_TT 0
  5876. +#define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  5877. +#define FOTG210_TUNE_MULT_TT 1
  5878. +
  5879. +/* Some drivers think it's safe to schedule isochronous transfers more than 256
  5880. + * ms into the future (partly as a result of an old bug in the scheduling
  5881. + * code). In an attempt to avoid trouble, we will use a minimum scheduling
  5882. + * length of 512 frames instead of 256.
  5883. + */
  5884. +#define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
  5885. +
  5886. +/* Initial IRQ latency: faster than hw default */
  5887. +static int log2_irq_thresh; /* 0 to 6 */
  5888. +module_param(log2_irq_thresh, int, S_IRUGO);
  5889. +MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  5890. +
  5891. +/* initial park setting: slower than hw default */
  5892. +static unsigned park;
  5893. +module_param(park, uint, S_IRUGO);
  5894. +MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
  5895. +
  5896. +/* for link power management(LPM) feature */
  5897. +static unsigned int hird;
  5898. +module_param(hird, int, S_IRUGO);
  5899. +MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
  5900. +
  5901. +#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  5902. +
  5903. +#include "fotg210-hcd.h"
  5904. +
  5905. +#define fotg210_dbg(fotg210, fmt, args...) \
  5906. + dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  5907. +#define fotg210_err(fotg210, fmt, args...) \
  5908. + dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  5909. +#define fotg210_info(fotg210, fmt, args...) \
  5910. + dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  5911. +#define fotg210_warn(fotg210, fmt, args...) \
  5912. + dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
  5913. +
  5914. +/* check the values in the HCSPARAMS register (host controller _Structural_
  5915. + * parameters) see EHCI spec, Table 2-4 for each value
  5916. + */
  5917. +static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
  5918. +{
  5919. + u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
  5920. +
  5921. + fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
  5922. + HCS_N_PORTS(params));
  5923. +}
  5924. +
  5925. +/* check the values in the HCCPARAMS register (host controller _Capability_
  5926. + * parameters) see EHCI Spec, Table 2-5 for each value
  5927. + */
  5928. +static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
  5929. +{
  5930. + u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  5931. +
  5932. + fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label,
  5933. + params,
  5934. + HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
  5935. + HCC_CANPARK(params) ? " park" : "");
  5936. +}
  5937. +
  5938. +static void __maybe_unused
  5939. +dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
  5940. +{
  5941. + fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
  5942. + hc32_to_cpup(fotg210, &qtd->hw_next),
  5943. + hc32_to_cpup(fotg210, &qtd->hw_alt_next),
  5944. + hc32_to_cpup(fotg210, &qtd->hw_token),
  5945. + hc32_to_cpup(fotg210, &qtd->hw_buf[0]));
  5946. + if (qtd->hw_buf[1])
  5947. + fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
  5948. + hc32_to_cpup(fotg210, &qtd->hw_buf[1]),
  5949. + hc32_to_cpup(fotg210, &qtd->hw_buf[2]),
  5950. + hc32_to_cpup(fotg210, &qtd->hw_buf[3]),
  5951. + hc32_to_cpup(fotg210, &qtd->hw_buf[4]));
  5952. +}
  5953. +
  5954. +static void __maybe_unused
  5955. +dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  5956. +{
  5957. + struct fotg210_qh_hw *hw = qh->hw;
  5958. +
  5959. + fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label, qh,
  5960. + hw->hw_next, hw->hw_info1, hw->hw_info2,
  5961. + hw->hw_current);
  5962. +
  5963. + dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
  5964. +}
  5965. +
  5966. +static void __maybe_unused
  5967. +dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
  5968. +{
  5969. + fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n", label,
  5970. + itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next),
  5971. + itd->urb);
  5972. +
  5973. + fotg210_dbg(fotg210,
  5974. + " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  5975. + hc32_to_cpu(fotg210, itd->hw_transaction[0]),
  5976. + hc32_to_cpu(fotg210, itd->hw_transaction[1]),
  5977. + hc32_to_cpu(fotg210, itd->hw_transaction[2]),
  5978. + hc32_to_cpu(fotg210, itd->hw_transaction[3]),
  5979. + hc32_to_cpu(fotg210, itd->hw_transaction[4]),
  5980. + hc32_to_cpu(fotg210, itd->hw_transaction[5]),
  5981. + hc32_to_cpu(fotg210, itd->hw_transaction[6]),
  5982. + hc32_to_cpu(fotg210, itd->hw_transaction[7]));
  5983. +
  5984. + fotg210_dbg(fotg210,
  5985. + " buf: %08x %08x %08x %08x %08x %08x %08x\n",
  5986. + hc32_to_cpu(fotg210, itd->hw_bufp[0]),
  5987. + hc32_to_cpu(fotg210, itd->hw_bufp[1]),
  5988. + hc32_to_cpu(fotg210, itd->hw_bufp[2]),
  5989. + hc32_to_cpu(fotg210, itd->hw_bufp[3]),
  5990. + hc32_to_cpu(fotg210, itd->hw_bufp[4]),
  5991. + hc32_to_cpu(fotg210, itd->hw_bufp[5]),
  5992. + hc32_to_cpu(fotg210, itd->hw_bufp[6]));
  5993. +
  5994. + fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n",
  5995. + itd->index[0], itd->index[1], itd->index[2],
  5996. + itd->index[3], itd->index[4], itd->index[5],
  5997. + itd->index[6], itd->index[7]);
  5998. +}
  5999. +
  6000. +static int __maybe_unused
  6001. +dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
  6002. +{
  6003. + return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
  6004. + label, label[0] ? " " : "", status,
  6005. + (status & STS_ASS) ? " Async" : "",
  6006. + (status & STS_PSS) ? " Periodic" : "",
  6007. + (status & STS_RECL) ? " Recl" : "",
  6008. + (status & STS_HALT) ? " Halt" : "",
  6009. + (status & STS_IAA) ? " IAA" : "",
  6010. + (status & STS_FATAL) ? " FATAL" : "",
  6011. + (status & STS_FLR) ? " FLR" : "",
  6012. + (status & STS_PCD) ? " PCD" : "",
  6013. + (status & STS_ERR) ? " ERR" : "",
  6014. + (status & STS_INT) ? " INT" : "");
  6015. +}
  6016. +
  6017. +static int __maybe_unused
  6018. +dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
  6019. +{
  6020. + return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
  6021. + label, label[0] ? " " : "", enable,
  6022. + (enable & STS_IAA) ? " IAA" : "",
  6023. + (enable & STS_FATAL) ? " FATAL" : "",
  6024. + (enable & STS_FLR) ? " FLR" : "",
  6025. + (enable & STS_PCD) ? " PCD" : "",
  6026. + (enable & STS_ERR) ? " ERR" : "",
  6027. + (enable & STS_INT) ? " INT" : "");
  6028. +}
  6029. +
  6030. +static const char *const fls_strings[] = { "1024", "512", "256", "??" };
  6031. +
  6032. +static int dbg_command_buf(char *buf, unsigned len, const char *label,
  6033. + u32 command)
  6034. +{
  6035. + return scnprintf(buf, len,
  6036. + "%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s",
  6037. + label, label[0] ? " " : "", command,
  6038. + (command & CMD_PARK) ? " park" : "(park)",
  6039. + CMD_PARK_CNT(command),
  6040. + (command >> 16) & 0x3f,
  6041. + (command & CMD_IAAD) ? " IAAD" : "",
  6042. + (command & CMD_ASE) ? " Async" : "",
  6043. + (command & CMD_PSE) ? " Periodic" : "",
  6044. + fls_strings[(command >> 2) & 0x3],
  6045. + (command & CMD_RESET) ? " Reset" : "",
  6046. + (command & CMD_RUN) ? "RUN" : "HALT");
  6047. +}
  6048. +
  6049. +static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
  6050. + u32 status)
  6051. +{
  6052. + char *sig;
  6053. +
  6054. + /* signaling state */
  6055. + switch (status & (3 << 10)) {
  6056. + case 0 << 10:
  6057. + sig = "se0";
  6058. + break;
  6059. + case 1 << 10:
  6060. + sig = "k";
  6061. + break; /* low speed */
  6062. + case 2 << 10:
  6063. + sig = "j";
  6064. + break;
  6065. + default:
  6066. + sig = "?";
  6067. + break;
  6068. + }
  6069. +
  6070. + scnprintf(buf, len, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s",
  6071. + label, label[0] ? " " : "", port, status,
  6072. + status >> 25, /*device address */
  6073. + sig,
  6074. + (status & PORT_RESET) ? " RESET" : "",
  6075. + (status & PORT_SUSPEND) ? " SUSPEND" : "",
  6076. + (status & PORT_RESUME) ? " RESUME" : "",
  6077. + (status & PORT_PEC) ? " PEC" : "",
  6078. + (status & PORT_PE) ? " PE" : "",
  6079. + (status & PORT_CSC) ? " CSC" : "",
  6080. + (status & PORT_CONNECT) ? " CONNECT" : "");
  6081. +
  6082. + return buf;
  6083. +}
  6084. +
  6085. +/* functions have the "wrong" filename when they're output... */
  6086. +#define dbg_status(fotg210, label, status) { \
  6087. + char _buf[80]; \
  6088. + dbg_status_buf(_buf, sizeof(_buf), label, status); \
  6089. + fotg210_dbg(fotg210, "%s\n", _buf); \
  6090. +}
  6091. +
  6092. +#define dbg_cmd(fotg210, label, command) { \
  6093. + char _buf[80]; \
  6094. + dbg_command_buf(_buf, sizeof(_buf), label, command); \
  6095. + fotg210_dbg(fotg210, "%s\n", _buf); \
  6096. +}
  6097. +
  6098. +#define dbg_port(fotg210, label, port, status) { \
  6099. + char _buf[80]; \
  6100. + fotg210_dbg(fotg210, "%s\n", \
  6101. + dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
  6102. +}
  6103. +
  6104. +/* troubleshooting help: expose state in debugfs */
  6105. +static int debug_async_open(struct inode *, struct file *);
  6106. +static int debug_periodic_open(struct inode *, struct file *);
  6107. +static int debug_registers_open(struct inode *, struct file *);
  6108. +static int debug_async_open(struct inode *, struct file *);
  6109. +
  6110. +static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
  6111. +static int debug_close(struct inode *, struct file *);
  6112. +
  6113. +static const struct file_operations debug_async_fops = {
  6114. + .owner = THIS_MODULE,
  6115. + .open = debug_async_open,
  6116. + .read = debug_output,
  6117. + .release = debug_close,
  6118. + .llseek = default_llseek,
  6119. +};
  6120. +static const struct file_operations debug_periodic_fops = {
  6121. + .owner = THIS_MODULE,
  6122. + .open = debug_periodic_open,
  6123. + .read = debug_output,
  6124. + .release = debug_close,
  6125. + .llseek = default_llseek,
  6126. +};
  6127. +static const struct file_operations debug_registers_fops = {
  6128. + .owner = THIS_MODULE,
  6129. + .open = debug_registers_open,
  6130. + .read = debug_output,
  6131. + .release = debug_close,
  6132. + .llseek = default_llseek,
  6133. +};
  6134. +
  6135. +static struct dentry *fotg210_debug_root;
  6136. +
  6137. +struct debug_buffer {
  6138. + ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
  6139. + struct usb_bus *bus;
  6140. + struct mutex mutex; /* protect filling of buffer */
  6141. + size_t count; /* number of characters filled into buffer */
  6142. + char *output_buf;
  6143. + size_t alloc_size;
  6144. +};
  6145. +
  6146. +static inline char speed_char(u32 scratch)
  6147. +{
  6148. + switch (scratch & (3 << 12)) {
  6149. + case QH_FULL_SPEED:
  6150. + return 'f';
  6151. +
  6152. + case QH_LOW_SPEED:
  6153. + return 'l';
  6154. +
  6155. + case QH_HIGH_SPEED:
  6156. + return 'h';
  6157. +
  6158. + default:
  6159. + return '?';
  6160. + }
  6161. +}
  6162. +
  6163. +static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
  6164. +{
  6165. + __u32 v = hc32_to_cpu(fotg210, token);
  6166. +
  6167. + if (v & QTD_STS_ACTIVE)
  6168. + return '*';
  6169. + if (v & QTD_STS_HALT)
  6170. + return '-';
  6171. + if (!IS_SHORT_READ(v))
  6172. + return ' ';
  6173. + /* tries to advance through hw_alt_next */
  6174. + return '/';
  6175. +}
  6176. +
  6177. +static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
  6178. + char **nextp, unsigned *sizep)
  6179. +{
  6180. + u32 scratch;
  6181. + u32 hw_curr;
  6182. + struct fotg210_qtd *td;
  6183. + unsigned temp;
  6184. + unsigned size = *sizep;
  6185. + char *next = *nextp;
  6186. + char mark;
  6187. + __le32 list_end = FOTG210_LIST_END(fotg210);
  6188. + struct fotg210_qh_hw *hw = qh->hw;
  6189. +
  6190. + if (hw->hw_qtd_next == list_end) /* NEC does this */
  6191. + mark = '@';
  6192. + else
  6193. + mark = token_mark(fotg210, hw->hw_token);
  6194. + if (mark == '/') { /* qh_alt_next controls qh advance? */
  6195. + if ((hw->hw_alt_next & QTD_MASK(fotg210)) ==
  6196. + fotg210->async->hw->hw_alt_next)
  6197. + mark = '#'; /* blocked */
  6198. + else if (hw->hw_alt_next == list_end)
  6199. + mark = '.'; /* use hw_qtd_next */
  6200. + /* else alt_next points to some other qtd */
  6201. + }
  6202. + scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
  6203. + hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0;
  6204. + temp = scnprintf(next, size,
  6205. + "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
  6206. + qh, scratch & 0x007f,
  6207. + speed_char(scratch),
  6208. + (scratch >> 8) & 0x000f,
  6209. + scratch, hc32_to_cpup(fotg210, &hw->hw_info2),
  6210. + hc32_to_cpup(fotg210, &hw->hw_token), mark,
  6211. + (cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token)
  6212. + ? "data1" : "data0",
  6213. + (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
  6214. + size -= temp;
  6215. + next += temp;
  6216. +
  6217. + /* hc may be modifying the list as we read it ... */
  6218. + list_for_each_entry(td, &qh->qtd_list, qtd_list) {
  6219. + scratch = hc32_to_cpup(fotg210, &td->hw_token);
  6220. + mark = ' ';
  6221. + if (hw_curr == td->qtd_dma)
  6222. + mark = '*';
  6223. + else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
  6224. + mark = '+';
  6225. + else if (QTD_LENGTH(scratch)) {
  6226. + if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
  6227. + mark = '#';
  6228. + else if (td->hw_alt_next != list_end)
  6229. + mark = '/';
  6230. + }
  6231. + temp = snprintf(next, size,
  6232. + "\n\t%p%c%s len=%d %08x urb %p",
  6233. + td, mark, ({ char *tmp;
  6234. + switch ((scratch>>8)&0x03) {
  6235. + case 0:
  6236. + tmp = "out";
  6237. + break;
  6238. + case 1:
  6239. + tmp = "in";
  6240. + break;
  6241. + case 2:
  6242. + tmp = "setup";
  6243. + break;
  6244. + default:
  6245. + tmp = "?";
  6246. + break;
  6247. + } tmp; }),
  6248. + (scratch >> 16) & 0x7fff,
  6249. + scratch,
  6250. + td->urb);
  6251. + if (size < temp)
  6252. + temp = size;
  6253. + size -= temp;
  6254. + next += temp;
  6255. + if (temp == size)
  6256. + goto done;
  6257. + }
  6258. +
  6259. + temp = snprintf(next, size, "\n");
  6260. + if (size < temp)
  6261. + temp = size;
  6262. +
  6263. + size -= temp;
  6264. + next += temp;
  6265. +
  6266. +done:
  6267. + *sizep = size;
  6268. + *nextp = next;
  6269. +}
  6270. +
  6271. +static ssize_t fill_async_buffer(struct debug_buffer *buf)
  6272. +{
  6273. + struct usb_hcd *hcd;
  6274. + struct fotg210_hcd *fotg210;
  6275. + unsigned long flags;
  6276. + unsigned temp, size;
  6277. + char *next;
  6278. + struct fotg210_qh *qh;
  6279. +
  6280. + hcd = bus_to_hcd(buf->bus);
  6281. + fotg210 = hcd_to_fotg210(hcd);
  6282. + next = buf->output_buf;
  6283. + size = buf->alloc_size;
  6284. +
  6285. + *next = 0;
  6286. +
  6287. + /* dumps a snapshot of the async schedule.
  6288. + * usually empty except for long-term bulk reads, or head.
  6289. + * one QH per line, and TDs we know about
  6290. + */
  6291. + spin_lock_irqsave(&fotg210->lock, flags);
  6292. + for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
  6293. + qh = qh->qh_next.qh)
  6294. + qh_lines(fotg210, qh, &next, &size);
  6295. + if (fotg210->async_unlink && size > 0) {
  6296. + temp = scnprintf(next, size, "\nunlink =\n");
  6297. + size -= temp;
  6298. + next += temp;
  6299. +
  6300. + for (qh = fotg210->async_unlink; size > 0 && qh;
  6301. + qh = qh->unlink_next)
  6302. + qh_lines(fotg210, qh, &next, &size);
  6303. + }
  6304. + spin_unlock_irqrestore(&fotg210->lock, flags);
  6305. +
  6306. + return strlen(buf->output_buf);
  6307. +}
  6308. +
  6309. +/* count tds, get ep direction */
  6310. +static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
  6311. + struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
  6312. +{
  6313. + u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
  6314. + struct fotg210_qtd *qtd;
  6315. + char *type = "";
  6316. + unsigned temp = 0;
  6317. +
  6318. + /* count tds, get ep direction */
  6319. + list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  6320. + temp++;
  6321. + switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
  6322. + case 0:
  6323. + type = "out";
  6324. + continue;
  6325. + case 1:
  6326. + type = "in";
  6327. + continue;
  6328. + }
  6329. + }
  6330. +
  6331. + return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)",
  6332. + speed_char(scratch), scratch & 0x007f,
  6333. + (scratch >> 8) & 0x000f, type, qh->usecs,
  6334. + qh->c_usecs, temp, (scratch >> 16) & 0x7ff);
  6335. +}
  6336. +
  6337. +#define DBG_SCHED_LIMIT 64
  6338. +static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
  6339. +{
  6340. + struct usb_hcd *hcd;
  6341. + struct fotg210_hcd *fotg210;
  6342. + unsigned long flags;
  6343. + union fotg210_shadow p, *seen;
  6344. + unsigned temp, size, seen_count;
  6345. + char *next;
  6346. + unsigned i;
  6347. + __hc32 tag;
  6348. +
  6349. + seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
  6350. + if (!seen)
  6351. + return 0;
  6352. +
  6353. + seen_count = 0;
  6354. +
  6355. + hcd = bus_to_hcd(buf->bus);
  6356. + fotg210 = hcd_to_fotg210(hcd);
  6357. + next = buf->output_buf;
  6358. + size = buf->alloc_size;
  6359. +
  6360. + temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
  6361. + size -= temp;
  6362. + next += temp;
  6363. +
  6364. + /* dump a snapshot of the periodic schedule.
  6365. + * iso changes, interrupt usually doesn't.
  6366. + */
  6367. + spin_lock_irqsave(&fotg210->lock, flags);
  6368. + for (i = 0; i < fotg210->periodic_size; i++) {
  6369. + p = fotg210->pshadow[i];
  6370. + if (likely(!p.ptr))
  6371. + continue;
  6372. +
  6373. + tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);
  6374. +
  6375. + temp = scnprintf(next, size, "%4d: ", i);
  6376. + size -= temp;
  6377. + next += temp;
  6378. +
  6379. + do {
  6380. + struct fotg210_qh_hw *hw;
  6381. +
  6382. + switch (hc32_to_cpu(fotg210, tag)) {
  6383. + case Q_TYPE_QH:
  6384. + hw = p.qh->hw;
  6385. + temp = scnprintf(next, size, " qh%d-%04x/%p",
  6386. + p.qh->period,
  6387. + hc32_to_cpup(fotg210,
  6388. + &hw->hw_info2)
  6389. + /* uframe masks */
  6390. + & (QH_CMASK | QH_SMASK),
  6391. + p.qh);
  6392. + size -= temp;
  6393. + next += temp;
  6394. + /* don't repeat what follows this qh */
  6395. + for (temp = 0; temp < seen_count; temp++) {
  6396. + if (seen[temp].ptr != p.ptr)
  6397. + continue;
  6398. + if (p.qh->qh_next.ptr) {
  6399. + temp = scnprintf(next, size,
  6400. + " ...");
  6401. + size -= temp;
  6402. + next += temp;
  6403. + }
  6404. + break;
  6405. + }
  6406. + /* show more info the first time around */
  6407. + if (temp == seen_count) {
  6408. + temp = output_buf_tds_dir(next,
  6409. + fotg210, hw,
  6410. + p.qh, size);
  6411. +
  6412. + if (seen_count < DBG_SCHED_LIMIT)
  6413. + seen[seen_count++].qh = p.qh;
  6414. + } else
  6415. + temp = 0;
  6416. + tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
  6417. + p = p.qh->qh_next;
  6418. + break;
  6419. + case Q_TYPE_FSTN:
  6420. + temp = scnprintf(next, size,
  6421. + " fstn-%8x/%p",
  6422. + p.fstn->hw_prev, p.fstn);
  6423. + tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
  6424. + p = p.fstn->fstn_next;
  6425. + break;
  6426. + case Q_TYPE_ITD:
  6427. + temp = scnprintf(next, size,
  6428. + " itd/%p", p.itd);
  6429. + tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
  6430. + p = p.itd->itd_next;
  6431. + break;
  6432. + }
  6433. + size -= temp;
  6434. + next += temp;
  6435. + } while (p.ptr);
  6436. +
  6437. + temp = scnprintf(next, size, "\n");
  6438. + size -= temp;
  6439. + next += temp;
  6440. + }
  6441. + spin_unlock_irqrestore(&fotg210->lock, flags);
  6442. + kfree(seen);
  6443. +
  6444. + return buf->alloc_size - size;
  6445. +}
  6446. +#undef DBG_SCHED_LIMIT
  6447. +
  6448. +static const char *rh_state_string(struct fotg210_hcd *fotg210)
  6449. +{
  6450. + switch (fotg210->rh_state) {
  6451. + case FOTG210_RH_HALTED:
  6452. + return "halted";
  6453. + case FOTG210_RH_SUSPENDED:
  6454. + return "suspended";
  6455. + case FOTG210_RH_RUNNING:
  6456. + return "running";
  6457. + case FOTG210_RH_STOPPING:
  6458. + return "stopping";
  6459. + }
  6460. + return "?";
  6461. +}
  6462. +
  6463. +static ssize_t fill_registers_buffer(struct debug_buffer *buf)
  6464. +{
  6465. + struct usb_hcd *hcd;
  6466. + struct fotg210_hcd *fotg210;
  6467. + unsigned long flags;
  6468. + unsigned temp, size, i;
  6469. + char *next, scratch[80];
  6470. + static const char fmt[] = "%*s\n";
  6471. + static const char label[] = "";
  6472. +
  6473. + hcd = bus_to_hcd(buf->bus);
  6474. + fotg210 = hcd_to_fotg210(hcd);
  6475. + next = buf->output_buf;
  6476. + size = buf->alloc_size;
  6477. +
  6478. + spin_lock_irqsave(&fotg210->lock, flags);
  6479. +
  6480. + if (!HCD_HW_ACCESSIBLE(hcd)) {
  6481. + size = scnprintf(next, size,
  6482. + "bus %s, device %s\n"
  6483. + "%s\n"
  6484. + "SUSPENDED(no register access)\n",
  6485. + hcd->self.controller->bus->name,
  6486. + dev_name(hcd->self.controller),
  6487. + hcd->product_desc);
  6488. + goto done;
  6489. + }
  6490. +
  6491. + /* Capability Registers */
  6492. + i = HC_VERSION(fotg210, fotg210_readl(fotg210,
  6493. + &fotg210->caps->hc_capbase));
  6494. + temp = scnprintf(next, size,
  6495. + "bus %s, device %s\n"
  6496. + "%s\n"
  6497. + "EHCI %x.%02x, rh state %s\n",
  6498. + hcd->self.controller->bus->name,
  6499. + dev_name(hcd->self.controller),
  6500. + hcd->product_desc,
  6501. + i >> 8, i & 0x0ff, rh_state_string(fotg210));
  6502. + size -= temp;
  6503. + next += temp;
  6504. +
  6505. + /* FIXME interpret both types of params */
  6506. + i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
  6507. + temp = scnprintf(next, size, "structural params 0x%08x\n", i);
  6508. + size -= temp;
  6509. + next += temp;
  6510. +
  6511. + i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  6512. + temp = scnprintf(next, size, "capability params 0x%08x\n", i);
  6513. + size -= temp;
  6514. + next += temp;
  6515. +
  6516. + /* Operational Registers */
  6517. + temp = dbg_status_buf(scratch, sizeof(scratch), label,
  6518. + fotg210_readl(fotg210, &fotg210->regs->status));
  6519. + temp = scnprintf(next, size, fmt, temp, scratch);
  6520. + size -= temp;
  6521. + next += temp;
  6522. +
  6523. + temp = dbg_command_buf(scratch, sizeof(scratch), label,
  6524. + fotg210_readl(fotg210, &fotg210->regs->command));
  6525. + temp = scnprintf(next, size, fmt, temp, scratch);
  6526. + size -= temp;
  6527. + next += temp;
  6528. +
  6529. + temp = dbg_intr_buf(scratch, sizeof(scratch), label,
  6530. + fotg210_readl(fotg210, &fotg210->regs->intr_enable));
  6531. + temp = scnprintf(next, size, fmt, temp, scratch);
  6532. + size -= temp;
  6533. + next += temp;
  6534. +
  6535. + temp = scnprintf(next, size, "uframe %04x\n",
  6536. + fotg210_read_frame_index(fotg210));
  6537. + size -= temp;
  6538. + next += temp;
  6539. +
  6540. + if (fotg210->async_unlink) {
  6541. + temp = scnprintf(next, size, "async unlink qh %p\n",
  6542. + fotg210->async_unlink);
  6543. + size -= temp;
  6544. + next += temp;
  6545. + }
  6546. +
  6547. +#ifdef FOTG210_STATS
  6548. + temp = scnprintf(next, size,
  6549. + "irq normal %ld err %ld iaa %ld(lost %ld)\n",
  6550. + fotg210->stats.normal, fotg210->stats.error,
  6551. + fotg210->stats.iaa, fotg210->stats.lost_iaa);
  6552. + size -= temp;
  6553. + next += temp;
  6554. +
  6555. + temp = scnprintf(next, size, "complete %ld unlink %ld\n",
  6556. + fotg210->stats.complete, fotg210->stats.unlink);
  6557. + size -= temp;
  6558. + next += temp;
  6559. +#endif
  6560. +
  6561. +done:
  6562. + spin_unlock_irqrestore(&fotg210->lock, flags);
  6563. +
  6564. + return buf->alloc_size - size;
  6565. +}
  6566. +
  6567. +static struct debug_buffer
  6568. +*alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
  6569. +{
  6570. + struct debug_buffer *buf;
  6571. +
  6572. + buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
  6573. +
  6574. + if (buf) {
  6575. + buf->bus = bus;
  6576. + buf->fill_func = fill_func;
  6577. + mutex_init(&buf->mutex);
  6578. + buf->alloc_size = PAGE_SIZE;
  6579. + }
  6580. +
  6581. + return buf;
  6582. +}
  6583. +
  6584. +static int fill_buffer(struct debug_buffer *buf)
  6585. +{
  6586. + int ret = 0;
  6587. +
  6588. + if (!buf->output_buf)
  6589. + buf->output_buf = vmalloc(buf->alloc_size);
  6590. +
  6591. + if (!buf->output_buf) {
  6592. + ret = -ENOMEM;
  6593. + goto out;
  6594. + }
  6595. +
  6596. + ret = buf->fill_func(buf);
  6597. +
  6598. + if (ret >= 0) {
  6599. + buf->count = ret;
  6600. + ret = 0;
  6601. + }
  6602. +
  6603. +out:
  6604. + return ret;
  6605. +}
  6606. +
  6607. +static ssize_t debug_output(struct file *file, char __user *user_buf,
  6608. + size_t len, loff_t *offset)
  6609. +{
  6610. + struct debug_buffer *buf = file->private_data;
  6611. + int ret = 0;
  6612. +
  6613. + mutex_lock(&buf->mutex);
  6614. + if (buf->count == 0) {
  6615. + ret = fill_buffer(buf);
  6616. + if (ret != 0) {
  6617. + mutex_unlock(&buf->mutex);
  6618. + goto out;
  6619. + }
  6620. + }
  6621. + mutex_unlock(&buf->mutex);
  6622. +
  6623. + ret = simple_read_from_buffer(user_buf, len, offset,
  6624. + buf->output_buf, buf->count);
  6625. +
  6626. +out:
  6627. + return ret;
  6628. +
  6629. +}
  6630. +
  6631. +static int debug_close(struct inode *inode, struct file *file)
  6632. +{
  6633. + struct debug_buffer *buf = file->private_data;
  6634. +
  6635. + if (buf) {
  6636. + vfree(buf->output_buf);
  6637. + kfree(buf);
  6638. + }
  6639. +
  6640. + return 0;
  6641. +}
  6642. +static int debug_async_open(struct inode *inode, struct file *file)
  6643. +{
  6644. + file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
  6645. +
  6646. + return file->private_data ? 0 : -ENOMEM;
  6647. +}
  6648. +
  6649. +static int debug_periodic_open(struct inode *inode, struct file *file)
  6650. +{
  6651. + struct debug_buffer *buf;
  6652. +
  6653. + buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
  6654. + if (!buf)
  6655. + return -ENOMEM;
  6656. +
  6657. + buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
  6658. + file->private_data = buf;
  6659. + return 0;
  6660. +}
  6661. +
  6662. +static int debug_registers_open(struct inode *inode, struct file *file)
  6663. +{
  6664. + file->private_data = alloc_buffer(inode->i_private,
  6665. + fill_registers_buffer);
  6666. +
  6667. + return file->private_data ? 0 : -ENOMEM;
  6668. +}
  6669. +
  6670. +static inline void create_debug_files(struct fotg210_hcd *fotg210)
  6671. +{
  6672. + struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
  6673. + struct dentry *root;
  6674. +
  6675. + root = debugfs_create_dir(bus->bus_name, fotg210_debug_root);
  6676. +
  6677. + debugfs_create_file("async", S_IRUGO, root, bus, &debug_async_fops);
  6678. + debugfs_create_file("periodic", S_IRUGO, root, bus,
  6679. + &debug_periodic_fops);
  6680. + debugfs_create_file("registers", S_IRUGO, root, bus,
  6681. + &debug_registers_fops);
  6682. +}
  6683. +
  6684. +static inline void remove_debug_files(struct fotg210_hcd *fotg210)
  6685. +{
  6686. + struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
  6687. +
  6688. + debugfs_lookup_and_remove(bus->bus_name, fotg210_debug_root);
  6689. +}
  6690. +
  6691. +/* handshake - spin reading hc until handshake completes or fails
  6692. + * @ptr: address of hc register to be read
  6693. + * @mask: bits to look at in result of read
  6694. + * @done: value of those bits when handshake succeeds
  6695. + * @usec: timeout in microseconds
  6696. + *
  6697. + * Returns negative errno, or zero on success
  6698. + *
  6699. + * Success happens when the "mask" bits have the specified value (hardware
  6700. + * handshake done). There are two failure modes: "usec" have passed (major
  6701. + * hardware flakeout), or the register reads as all-ones (hardware removed).
  6702. + *
  6703. + * That last failure should_only happen in cases like physical cardbus eject
  6704. + * before driver shutdown. But it also seems to be caused by bugs in cardbus
  6705. + * bridge shutdown: shutting down the bridge before the devices using it.
  6706. + */
  6707. +static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
  6708. + u32 mask, u32 done, int usec)
  6709. +{
  6710. + u32 result;
  6711. + int ret;
  6712. +
  6713. + ret = readl_poll_timeout_atomic(ptr, result,
  6714. + ((result & mask) == done ||
  6715. + result == U32_MAX), 1, usec);
  6716. + if (result == U32_MAX) /* card removed */
  6717. + return -ENODEV;
  6718. +
  6719. + return ret;
  6720. +}
  6721. +
  6722. +/* Force HC to halt state from unknown (EHCI spec section 2.3).
  6723. + * Must be called with interrupts enabled and the lock not held.
  6724. + */
  6725. +static int fotg210_halt(struct fotg210_hcd *fotg210)
  6726. +{
  6727. + u32 temp;
  6728. +
  6729. + spin_lock_irq(&fotg210->lock);
  6730. +
  6731. + /* disable any irqs left enabled by previous code */
  6732. + fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  6733. +
  6734. + /*
  6735. + * This routine gets called during probe before fotg210->command
  6736. + * has been initialized, so we can't rely on its value.
  6737. + */
  6738. + fotg210->command &= ~CMD_RUN;
  6739. + temp = fotg210_readl(fotg210, &fotg210->regs->command);
  6740. + temp &= ~(CMD_RUN | CMD_IAAD);
  6741. + fotg210_writel(fotg210, temp, &fotg210->regs->command);
  6742. +
  6743. + spin_unlock_irq(&fotg210->lock);
  6744. + synchronize_irq(fotg210_to_hcd(fotg210)->irq);
  6745. +
  6746. + return handshake(fotg210, &fotg210->regs->status,
  6747. + STS_HALT, STS_HALT, 16 * 125);
  6748. +}
  6749. +
  6750. +/* Reset a non-running (STS_HALT == 1) controller.
  6751. + * Must be called with interrupts enabled and the lock not held.
  6752. + */
  6753. +static int fotg210_reset(struct fotg210_hcd *fotg210)
  6754. +{
  6755. + int retval;
  6756. + u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
  6757. +
  6758. + /* If the EHCI debug controller is active, special care must be
  6759. + * taken before and after a host controller reset
  6760. + */
  6761. + if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
  6762. + fotg210->debug = NULL;
  6763. +
  6764. + command |= CMD_RESET;
  6765. + dbg_cmd(fotg210, "reset", command);
  6766. + fotg210_writel(fotg210, command, &fotg210->regs->command);
  6767. + fotg210->rh_state = FOTG210_RH_HALTED;
  6768. + fotg210->next_statechange = jiffies;
  6769. + retval = handshake(fotg210, &fotg210->regs->command,
  6770. + CMD_RESET, 0, 250 * 1000);
  6771. +
  6772. + if (retval)
  6773. + return retval;
  6774. +
  6775. + if (fotg210->debug)
  6776. + dbgp_external_startup(fotg210_to_hcd(fotg210));
  6777. +
  6778. + fotg210->port_c_suspend = fotg210->suspended_ports =
  6779. + fotg210->resuming_ports = 0;
  6780. + return retval;
  6781. +}
  6782. +
  6783. +/* Idle the controller (turn off the schedules).
  6784. + * Must be called with interrupts enabled and the lock not held.
  6785. + */
  6786. +static void fotg210_quiesce(struct fotg210_hcd *fotg210)
  6787. +{
  6788. + u32 temp;
  6789. +
  6790. + if (fotg210->rh_state != FOTG210_RH_RUNNING)
  6791. + return;
  6792. +
  6793. + /* wait for any schedule enables/disables to take effect */
  6794. + temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
  6795. + handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
  6796. + 16 * 125);
  6797. +
  6798. + /* then disable anything that's still active */
  6799. + spin_lock_irq(&fotg210->lock);
  6800. + fotg210->command &= ~(CMD_ASE | CMD_PSE);
  6801. + fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  6802. + spin_unlock_irq(&fotg210->lock);
  6803. +
  6804. + /* hardware can take 16 microframes to turn off ... */
  6805. + handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
  6806. + 16 * 125);
  6807. +}
  6808. +
  6809. +static void end_unlink_async(struct fotg210_hcd *fotg210);
  6810. +static void unlink_empty_async(struct fotg210_hcd *fotg210);
  6811. +static void fotg210_work(struct fotg210_hcd *fotg210);
  6812. +static void start_unlink_intr(struct fotg210_hcd *fotg210,
  6813. + struct fotg210_qh *qh);
  6814. +static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  6815. +
  6816. +/* Set a bit in the USBCMD register */
  6817. +static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
  6818. +{
  6819. + fotg210->command |= bit;
  6820. + fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  6821. +
  6822. + /* unblock posted write */
  6823. + fotg210_readl(fotg210, &fotg210->regs->command);
  6824. +}
  6825. +
  6826. +/* Clear a bit in the USBCMD register */
  6827. +static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
  6828. +{
  6829. + fotg210->command &= ~bit;
  6830. + fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  6831. +
  6832. + /* unblock posted write */
  6833. + fotg210_readl(fotg210, &fotg210->regs->command);
  6834. +}
  6835. +
  6836. +/* EHCI timer support... Now using hrtimers.
  6837. + *
  6838. + * Lots of different events are triggered from fotg210->hrtimer. Whenever
  6839. + * the timer routine runs, it checks each possible event; events that are
  6840. + * currently enabled and whose expiration time has passed get handled.
  6841. + * The set of enabled events is stored as a collection of bitflags in
  6842. + * fotg210->enabled_hrtimer_events, and they are numbered in order of
  6843. + * increasing delay values (ranging between 1 ms and 100 ms).
  6844. + *
  6845. + * Rather than implementing a sorted list or tree of all pending events,
  6846. + * we keep track only of the lowest-numbered pending event, in
  6847. + * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
  6848. + * expiration time is set to the timeout value for this event.
  6849. + *
  6850. + * As a result, events might not get handled right away; the actual delay
  6851. + * could be anywhere up to twice the requested delay. This doesn't
  6852. + * matter, because none of the events are especially time-critical. The
  6853. + * ones that matter most all have a delay of 1 ms, so they will be
  6854. + * handled after 2 ms at most, which is okay. In addition to this, we
  6855. + * allow for an expiration range of 1 ms.
  6856. + */
  6857. +
  6858. +/* Delay lengths for the hrtimer event types.
  6859. + * Keep this list sorted by delay length, in the same order as
  6860. + * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
  6861. + */
  6862. +static unsigned event_delays_ns[] = {
  6863. + 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */
  6864. + 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */
  6865. + 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */
  6866. + 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */
  6867. + 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */
  6868. + 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
  6869. + 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */
  6870. + 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
  6871. + 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */
  6872. + 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */
  6873. +};
  6874. +
  6875. +/* Enable a pending hrtimer event */
  6876. +static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
  6877. + bool resched)
  6878. +{
  6879. + ktime_t *timeout = &fotg210->hr_timeouts[event];
  6880. +
  6881. + if (resched)
  6882. + *timeout = ktime_add(ktime_get(), event_delays_ns[event]);
  6883. + fotg210->enabled_hrtimer_events |= (1 << event);
  6884. +
  6885. + /* Track only the lowest-numbered pending event */
  6886. + if (event < fotg210->next_hrtimer_event) {
  6887. + fotg210->next_hrtimer_event = event;
  6888. + hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
  6889. + NSEC_PER_MSEC, HRTIMER_MODE_ABS);
  6890. + }
  6891. +}
  6892. +
  6893. +
  6894. +/* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
  6895. +static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
  6896. +{
  6897. + unsigned actual, want;
  6898. +
  6899. + /* Don't enable anything if the controller isn't running (e.g., died) */
  6900. + if (fotg210->rh_state != FOTG210_RH_RUNNING)
  6901. + return;
  6902. +
  6903. + want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
  6904. + actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;
  6905. +
  6906. + if (want != actual) {
  6907. +
  6908. + /* Poll again later, but give up after about 20 ms */
  6909. + if (fotg210->ASS_poll_count++ < 20) {
  6910. + fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
  6911. + true);
  6912. + return;
  6913. + }
  6914. + fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
  6915. + want, actual);
  6916. + }
  6917. + fotg210->ASS_poll_count = 0;
  6918. +
  6919. + /* The status is up-to-date; restart or stop the schedule as needed */
  6920. + if (want == 0) { /* Stopped */
  6921. + if (fotg210->async_count > 0)
  6922. + fotg210_set_command_bit(fotg210, CMD_ASE);
  6923. +
  6924. + } else { /* Running */
  6925. + if (fotg210->async_count == 0) {
  6926. +
  6927. + /* Turn off the schedule after a while */
  6928. + fotg210_enable_event(fotg210,
  6929. + FOTG210_HRTIMER_DISABLE_ASYNC,
  6930. + true);
  6931. + }
  6932. + }
  6933. +}
  6934. +
  6935. +/* Turn off the async schedule after a brief delay */
  6936. +static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
  6937. +{
  6938. + fotg210_clear_command_bit(fotg210, CMD_ASE);
  6939. +}
  6940. +
  6941. +
  6942. +/* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
  6943. +static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
  6944. +{
  6945. + unsigned actual, want;
  6946. +
  6947. + /* Don't do anything if the controller isn't running (e.g., died) */
  6948. + if (fotg210->rh_state != FOTG210_RH_RUNNING)
  6949. + return;
  6950. +
  6951. + want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
  6952. + actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;
  6953. +
  6954. + if (want != actual) {
  6955. +
  6956. + /* Poll again later, but give up after about 20 ms */
  6957. + if (fotg210->PSS_poll_count++ < 20) {
  6958. + fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
  6959. + true);
  6960. + return;
  6961. + }
  6962. + fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
  6963. + want, actual);
  6964. + }
  6965. + fotg210->PSS_poll_count = 0;
  6966. +
  6967. + /* The status is up-to-date; restart or stop the schedule as needed */
  6968. + if (want == 0) { /* Stopped */
  6969. + if (fotg210->periodic_count > 0)
  6970. + fotg210_set_command_bit(fotg210, CMD_PSE);
  6971. +
  6972. + } else { /* Running */
  6973. + if (fotg210->periodic_count == 0) {
  6974. +
  6975. + /* Turn off the schedule after a while */
  6976. + fotg210_enable_event(fotg210,
  6977. + FOTG210_HRTIMER_DISABLE_PERIODIC,
  6978. + true);
  6979. + }
  6980. + }
  6981. +}
  6982. +
  6983. +/* Turn off the periodic schedule after a brief delay */
  6984. +static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
  6985. +{
  6986. + fotg210_clear_command_bit(fotg210, CMD_PSE);
  6987. +}
  6988. +
  6989. +
  6990. +/* Poll the STS_HALT status bit; see when a dead controller stops */
  6991. +static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
  6992. +{
  6993. + if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
  6994. +
  6995. + /* Give up after a few milliseconds */
  6996. + if (fotg210->died_poll_count++ < 5) {
  6997. + /* Try again later */
  6998. + fotg210_enable_event(fotg210,
  6999. + FOTG210_HRTIMER_POLL_DEAD, true);
  7000. + return;
  7001. + }
  7002. + fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
  7003. + }
  7004. +
  7005. + /* Clean up the mess */
  7006. + fotg210->rh_state = FOTG210_RH_HALTED;
  7007. + fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  7008. + fotg210_work(fotg210);
  7009. + end_unlink_async(fotg210);
  7010. +
  7011. + /* Not in process context, so don't try to reset the controller */
  7012. +}
  7013. +
  7014. +
  7015. +/* Handle unlinked interrupt QHs once they are gone from the hardware */
  7016. +static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
  7017. +{
  7018. + bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
  7019. +
  7020. + /*
  7021. + * Process all the QHs on the intr_unlink list that were added
  7022. + * before the current unlink cycle began. The list is in
  7023. + * temporal order, so stop when we reach the first entry in the
  7024. + * current cycle. But if the root hub isn't running then
  7025. + * process all the QHs on the list.
  7026. + */
  7027. + fotg210->intr_unlinking = true;
  7028. + while (fotg210->intr_unlink) {
  7029. + struct fotg210_qh *qh = fotg210->intr_unlink;
  7030. +
  7031. + if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
  7032. + break;
  7033. + fotg210->intr_unlink = qh->unlink_next;
  7034. + qh->unlink_next = NULL;
  7035. + end_unlink_intr(fotg210, qh);
  7036. + }
  7037. +
  7038. + /* Handle remaining entries later */
  7039. + if (fotg210->intr_unlink) {
  7040. + fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
  7041. + true);
  7042. + ++fotg210->intr_unlink_cycle;
  7043. + }
  7044. + fotg210->intr_unlinking = false;
  7045. +}
  7046. +
  7047. +
  7048. +/* Start another free-iTDs/siTDs cycle */
  7049. +static void start_free_itds(struct fotg210_hcd *fotg210)
  7050. +{
  7051. + if (!(fotg210->enabled_hrtimer_events &
  7052. + BIT(FOTG210_HRTIMER_FREE_ITDS))) {
  7053. + fotg210->last_itd_to_free = list_entry(
  7054. + fotg210->cached_itd_list.prev,
  7055. + struct fotg210_itd, itd_list);
  7056. + fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
  7057. + }
  7058. +}
  7059. +
  7060. +/* Wait for controller to stop using old iTDs and siTDs */
  7061. +static void end_free_itds(struct fotg210_hcd *fotg210)
  7062. +{
  7063. + struct fotg210_itd *itd, *n;
  7064. +
  7065. + if (fotg210->rh_state < FOTG210_RH_RUNNING)
  7066. + fotg210->last_itd_to_free = NULL;
  7067. +
  7068. + list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
  7069. + list_del(&itd->itd_list);
  7070. + dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
  7071. + if (itd == fotg210->last_itd_to_free)
  7072. + break;
  7073. + }
  7074. +
  7075. + if (!list_empty(&fotg210->cached_itd_list))
  7076. + start_free_itds(fotg210);
  7077. +}
  7078. +
  7079. +
  7080. +/* Handle lost (or very late) IAA interrupts */
  7081. +static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
  7082. +{
  7083. + if (fotg210->rh_state != FOTG210_RH_RUNNING)
  7084. + return;
  7085. +
  7086. + /*
  7087. + * Lost IAA irqs wedge things badly; seen first with a vt8235.
  7088. + * So we need this watchdog, but must protect it against both
  7089. + * (a) SMP races against real IAA firing and retriggering, and
  7090. + * (b) clean HC shutdown, when IAA watchdog was pending.
  7091. + */
  7092. + if (fotg210->async_iaa) {
  7093. + u32 cmd, status;
  7094. +
  7095. + /* If we get here, IAA is *REALLY* late. It's barely
  7096. + * conceivable that the system is so busy that CMD_IAAD
  7097. + * is still legitimately set, so let's be sure it's
  7098. + * clear before we read STS_IAA. (The HC should clear
  7099. + * CMD_IAAD when it sets STS_IAA.)
  7100. + */
  7101. + cmd = fotg210_readl(fotg210, &fotg210->regs->command);
  7102. +
  7103. + /*
  7104. + * If IAA is set here it either legitimately triggered
  7105. + * after the watchdog timer expired (_way_ late, so we'll
  7106. + * still count it as lost) ... or a silicon erratum:
  7107. + * - VIA seems to set IAA without triggering the IRQ;
  7108. + * - IAAD potentially cleared without setting IAA.
  7109. + */
  7110. + status = fotg210_readl(fotg210, &fotg210->regs->status);
  7111. + if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
  7112. + INCR(fotg210->stats.lost_iaa);
  7113. + fotg210_writel(fotg210, STS_IAA,
  7114. + &fotg210->regs->status);
  7115. + }
  7116. +
  7117. + fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
  7118. + status, cmd);
  7119. + end_unlink_async(fotg210);
  7120. + }
  7121. +}
  7122. +
  7123. +
  7124. +/* Enable the I/O watchdog, if appropriate */
  7125. +static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
  7126. +{
  7127. + /* Not needed if the controller isn't running or it's already enabled */
  7128. + if (fotg210->rh_state != FOTG210_RH_RUNNING ||
  7129. + (fotg210->enabled_hrtimer_events &
  7130. + BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
  7131. + return;
  7132. +
  7133. + /*
  7134. + * Isochronous transfers always need the watchdog.
  7135. + * For other sorts we use it only if the flag is set.
  7136. + */
  7137. + if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog &&
  7138. + fotg210->async_count + fotg210->intr_count > 0))
  7139. + fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG,
  7140. + true);
  7141. +}
  7142. +
  7143. +
  7144. +/* Handler functions for the hrtimer event types.
  7145. + * Keep this array in the same order as the event types indexed by
  7146. + * enum fotg210_hrtimer_event in fotg210.h.
  7147. + */
  7148. +static void (*event_handlers[])(struct fotg210_hcd *) = {
  7149. + fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */
  7150. + fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */
  7151. + fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */
  7152. + fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */
  7153. + end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */
  7154. + unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
  7155. + fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */
  7156. + fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
  7157. + fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */
  7158. + fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */
  7159. +};
  7160. +
  7161. +static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
  7162. +{
  7163. + struct fotg210_hcd *fotg210 =
  7164. + container_of(t, struct fotg210_hcd, hrtimer);
  7165. + ktime_t now;
  7166. + unsigned long events;
  7167. + unsigned long flags;
  7168. + unsigned e;
  7169. +
  7170. + spin_lock_irqsave(&fotg210->lock, flags);
  7171. +
  7172. + events = fotg210->enabled_hrtimer_events;
  7173. + fotg210->enabled_hrtimer_events = 0;
  7174. + fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
  7175. +
  7176. + /*
  7177. + * Check each pending event. If its time has expired, handle
  7178. + * the event; otherwise re-enable it.
  7179. + */
  7180. + now = ktime_get();
  7181. + for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) {
  7182. + if (ktime_compare(now, fotg210->hr_timeouts[e]) >= 0)
  7183. + event_handlers[e](fotg210);
  7184. + else
  7185. + fotg210_enable_event(fotg210, e, false);
  7186. + }
  7187. +
  7188. + spin_unlock_irqrestore(&fotg210->lock, flags);
  7189. + return HRTIMER_NORESTART;
  7190. +}
  7191. +
  7192. +#define fotg210_bus_suspend NULL
  7193. +#define fotg210_bus_resume NULL
  7194. +
  7195. +static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
  7196. + u32 __iomem *status_reg, int port_status)
  7197. +{
  7198. + if (!(port_status & PORT_CONNECT))
  7199. + return port_status;
  7200. +
  7201. + /* if reset finished and it's still not enabled -- handoff */
  7202. + if (!(port_status & PORT_PE))
  7203. + /* with integrated TT, there's nobody to hand it to! */
  7204. + fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
  7205. + index + 1);
  7206. + else
  7207. + fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
  7208. + index + 1);
  7209. +
  7210. + return port_status;
  7211. +}
  7212. +
  7213. +
  7214. +/* build "status change" packet (one or two bytes) from HC registers */
  7215. +
  7216. +static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
  7217. +{
  7218. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  7219. + u32 temp, status;
  7220. + u32 mask;
  7221. + int retval = 1;
  7222. + unsigned long flags;
  7223. +
  7224. + /* init status to no-changes */
  7225. + buf[0] = 0;
  7226. +
  7227. + /* Inform the core about resumes-in-progress by returning
  7228. + * a non-zero value even if there are no status changes.
  7229. + */
  7230. + status = fotg210->resuming_ports;
  7231. +
  7232. + mask = PORT_CSC | PORT_PEC;
  7233. + /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
  7234. +
  7235. + /* no hub change reports (bit 0) for now (power, ...) */
  7236. +
  7237. + /* port N changes (bit N)? */
  7238. + spin_lock_irqsave(&fotg210->lock, flags);
  7239. +
  7240. + temp = fotg210_readl(fotg210, &fotg210->regs->port_status);
  7241. +
  7242. + /*
  7243. + * Return status information even for ports with OWNER set.
  7244. + * Otherwise hub_wq wouldn't see the disconnect event when a
  7245. + * high-speed device is switched over to the companion
  7246. + * controller by the user.
  7247. + */
  7248. +
  7249. + if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend) ||
  7250. + (fotg210->reset_done[0] &&
  7251. + time_after_eq(jiffies, fotg210->reset_done[0]))) {
  7252. + buf[0] |= 1 << 1;
  7253. + status = STS_PCD;
  7254. + }
  7255. + /* FIXME autosuspend idle root hubs */
  7256. + spin_unlock_irqrestore(&fotg210->lock, flags);
  7257. + return status ? retval : 0;
  7258. +}
  7259. +
  7260. +static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210,
  7261. + struct usb_hub_descriptor *desc)
  7262. +{
  7263. + int ports = HCS_N_PORTS(fotg210->hcs_params);
  7264. + u16 temp;
  7265. +
  7266. + desc->bDescriptorType = USB_DT_HUB;
  7267. + desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */
  7268. + desc->bHubContrCurrent = 0;
  7269. +
  7270. + desc->bNbrPorts = ports;
  7271. + temp = 1 + (ports / 8);
  7272. + desc->bDescLength = 7 + 2 * temp;
  7273. +
  7274. + /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  7275. + memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  7276. + memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  7277. +
  7278. + temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
  7279. + temp |= HUB_CHAR_NO_LPSM; /* no power switching */
  7280. + desc->wHubCharacteristics = cpu_to_le16(temp);
  7281. +}
  7282. +
  7283. +static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  7284. + u16 wIndex, char *buf, u16 wLength)
  7285. +{
  7286. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  7287. + int ports = HCS_N_PORTS(fotg210->hcs_params);
  7288. + u32 __iomem *status_reg = &fotg210->regs->port_status;
  7289. + u32 temp, temp1, status;
  7290. + unsigned long flags;
  7291. + int retval = 0;
  7292. + unsigned selector;
  7293. +
  7294. + /*
  7295. + * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  7296. + * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  7297. + * (track current state ourselves) ... blink for diagnostics,
  7298. + * power, "this is the one", etc. EHCI spec supports this.
  7299. + */
  7300. +
  7301. + spin_lock_irqsave(&fotg210->lock, flags);
  7302. + switch (typeReq) {
  7303. + case ClearHubFeature:
  7304. + switch (wValue) {
  7305. + case C_HUB_LOCAL_POWER:
  7306. + case C_HUB_OVER_CURRENT:
  7307. + /* no hub-wide feature/status flags */
  7308. + break;
  7309. + default:
  7310. + goto error;
  7311. + }
  7312. + break;
  7313. + case ClearPortFeature:
  7314. + if (!wIndex || wIndex > ports)
  7315. + goto error;
  7316. + wIndex--;
  7317. + temp = fotg210_readl(fotg210, status_reg);
  7318. + temp &= ~PORT_RWC_BITS;
  7319. +
  7320. + /*
  7321. + * Even if OWNER is set, so the port is owned by the
  7322. + * companion controller, hub_wq needs to be able to clear
  7323. + * the port-change status bits (especially
  7324. + * USB_PORT_STAT_C_CONNECTION).
  7325. + */
  7326. +
  7327. + switch (wValue) {
  7328. + case USB_PORT_FEAT_ENABLE:
  7329. + fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
  7330. + break;
  7331. + case USB_PORT_FEAT_C_ENABLE:
  7332. + fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
  7333. + break;
  7334. + case USB_PORT_FEAT_SUSPEND:
  7335. + if (temp & PORT_RESET)
  7336. + goto error;
  7337. + if (!(temp & PORT_SUSPEND))
  7338. + break;
  7339. + if ((temp & PORT_PE) == 0)
  7340. + goto error;
  7341. +
  7342. + /* resume signaling for 20 msec */
  7343. + fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
  7344. + fotg210->reset_done[wIndex] = jiffies
  7345. + + msecs_to_jiffies(USB_RESUME_TIMEOUT);
  7346. + break;
  7347. + case USB_PORT_FEAT_C_SUSPEND:
  7348. + clear_bit(wIndex, &fotg210->port_c_suspend);
  7349. + break;
  7350. + case USB_PORT_FEAT_C_CONNECTION:
  7351. + fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
  7352. + break;
  7353. + case USB_PORT_FEAT_C_OVER_CURRENT:
  7354. + fotg210_writel(fotg210, temp | OTGISR_OVC,
  7355. + &fotg210->regs->otgisr);
  7356. + break;
  7357. + case USB_PORT_FEAT_C_RESET:
  7358. + /* GetPortStatus clears reset */
  7359. + break;
  7360. + default:
  7361. + goto error;
  7362. + }
  7363. + fotg210_readl(fotg210, &fotg210->regs->command);
  7364. + break;
  7365. + case GetHubDescriptor:
  7366. + fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
  7367. + buf);
  7368. + break;
  7369. + case GetHubStatus:
  7370. + /* no hub-wide feature/status flags */
  7371. + memset(buf, 0, 4);
  7372. + /*cpu_to_le32s ((u32 *) buf); */
  7373. + break;
  7374. + case GetPortStatus:
  7375. + if (!wIndex || wIndex > ports)
  7376. + goto error;
  7377. + wIndex--;
  7378. + status = 0;
  7379. + temp = fotg210_readl(fotg210, status_reg);
  7380. +
  7381. + /* wPortChange bits */
  7382. + if (temp & PORT_CSC)
  7383. + status |= USB_PORT_STAT_C_CONNECTION << 16;
  7384. + if (temp & PORT_PEC)
  7385. + status |= USB_PORT_STAT_C_ENABLE << 16;
  7386. +
  7387. + temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
  7388. + if (temp1 & OTGISR_OVC)
  7389. + status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  7390. +
  7391. + /* whoever resumes must GetPortStatus to complete it!! */
  7392. + if (temp & PORT_RESUME) {
  7393. +
  7394. + /* Remote Wakeup received? */
  7395. + if (!fotg210->reset_done[wIndex]) {
  7396. + /* resume signaling for 20 msec */
  7397. + fotg210->reset_done[wIndex] = jiffies
  7398. + + msecs_to_jiffies(20);
  7399. + /* check the port again */
  7400. + mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
  7401. + fotg210->reset_done[wIndex]);
  7402. + }
  7403. +
  7404. + /* resume completed? */
  7405. + else if (time_after_eq(jiffies,
  7406. + fotg210->reset_done[wIndex])) {
  7407. + clear_bit(wIndex, &fotg210->suspended_ports);
  7408. + set_bit(wIndex, &fotg210->port_c_suspend);
  7409. + fotg210->reset_done[wIndex] = 0;
  7410. +
  7411. + /* stop resume signaling */
  7412. + temp = fotg210_readl(fotg210, status_reg);
  7413. + fotg210_writel(fotg210, temp &
  7414. + ~(PORT_RWC_BITS | PORT_RESUME),
  7415. + status_reg);
  7416. + clear_bit(wIndex, &fotg210->resuming_ports);
  7417. + retval = handshake(fotg210, status_reg,
  7418. + PORT_RESUME, 0, 2000);/* 2ms */
  7419. + if (retval != 0) {
  7420. + fotg210_err(fotg210,
  7421. + "port %d resume error %d\n",
  7422. + wIndex + 1, retval);
  7423. + goto error;
  7424. + }
  7425. + temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  7426. + }
  7427. + }
  7428. +
  7429. + /* whoever resets must GetPortStatus to complete it!! */
  7430. + if ((temp & PORT_RESET) && time_after_eq(jiffies,
  7431. + fotg210->reset_done[wIndex])) {
  7432. + status |= USB_PORT_STAT_C_RESET << 16;
  7433. + fotg210->reset_done[wIndex] = 0;
  7434. + clear_bit(wIndex, &fotg210->resuming_ports);
  7435. +
  7436. + /* force reset to complete */
  7437. + fotg210_writel(fotg210,
  7438. + temp & ~(PORT_RWC_BITS | PORT_RESET),
  7439. + status_reg);
  7440. + /* REVISIT: some hardware needs 550+ usec to clear
  7441. + * this bit; seems too long to spin routinely...
  7442. + */
  7443. + retval = handshake(fotg210, status_reg,
  7444. + PORT_RESET, 0, 1000);
  7445. + if (retval != 0) {
  7446. + fotg210_err(fotg210, "port %d reset error %d\n",
  7447. + wIndex + 1, retval);
  7448. + goto error;
  7449. + }
  7450. +
  7451. + /* see what we found out */
  7452. + temp = check_reset_complete(fotg210, wIndex, status_reg,
  7453. + fotg210_readl(fotg210, status_reg));
  7454. +
  7455. + /* restart schedule */
  7456. + fotg210->command |= CMD_RUN;
  7457. + fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  7458. + }
  7459. +
  7460. + if (!(temp & (PORT_RESUME|PORT_RESET))) {
  7461. + fotg210->reset_done[wIndex] = 0;
  7462. + clear_bit(wIndex, &fotg210->resuming_ports);
  7463. + }
  7464. +
  7465. + /* transfer dedicated ports to the companion hc */
  7466. + if ((temp & PORT_CONNECT) &&
  7467. + test_bit(wIndex, &fotg210->companion_ports)) {
  7468. + temp &= ~PORT_RWC_BITS;
  7469. + fotg210_writel(fotg210, temp, status_reg);
  7470. + fotg210_dbg(fotg210, "port %d --> companion\n",
  7471. + wIndex + 1);
  7472. + temp = fotg210_readl(fotg210, status_reg);
  7473. + }
  7474. +
  7475. + /*
  7476. + * Even if OWNER is set, there's no harm letting hub_wq
  7477. + * see the wPortStatus values (they should all be 0 except
  7478. + * for PORT_POWER anyway).
  7479. + */
  7480. +
  7481. + if (temp & PORT_CONNECT) {
  7482. + status |= USB_PORT_STAT_CONNECTION;
  7483. + status |= fotg210_port_speed(fotg210, temp);
  7484. + }
  7485. + if (temp & PORT_PE)
  7486. + status |= USB_PORT_STAT_ENABLE;
  7487. +
  7488. + /* maybe the port was unsuspended without our knowledge */
  7489. + if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  7490. + status |= USB_PORT_STAT_SUSPEND;
  7491. + } else if (test_bit(wIndex, &fotg210->suspended_ports)) {
  7492. + clear_bit(wIndex, &fotg210->suspended_ports);
  7493. + clear_bit(wIndex, &fotg210->resuming_ports);
  7494. + fotg210->reset_done[wIndex] = 0;
  7495. + if (temp & PORT_PE)
  7496. + set_bit(wIndex, &fotg210->port_c_suspend);
  7497. + }
  7498. +
  7499. + temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
  7500. + if (temp1 & OTGISR_OVC)
  7501. + status |= USB_PORT_STAT_OVERCURRENT;
  7502. + if (temp & PORT_RESET)
  7503. + status |= USB_PORT_STAT_RESET;
  7504. + if (test_bit(wIndex, &fotg210->port_c_suspend))
  7505. + status |= USB_PORT_STAT_C_SUSPEND << 16;
  7506. +
  7507. + if (status & ~0xffff) /* only if wPortChange is interesting */
  7508. + dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
  7509. + put_unaligned_le32(status, buf);
  7510. + break;
  7511. + case SetHubFeature:
  7512. + switch (wValue) {
  7513. + case C_HUB_LOCAL_POWER:
  7514. + case C_HUB_OVER_CURRENT:
  7515. + /* no hub-wide feature/status flags */
  7516. + break;
  7517. + default:
  7518. + goto error;
  7519. + }
  7520. + break;
  7521. + case SetPortFeature:
  7522. + selector = wIndex >> 8;
  7523. + wIndex &= 0xff;
  7524. +
  7525. + if (!wIndex || wIndex > ports)
  7526. + goto error;
  7527. + wIndex--;
  7528. + temp = fotg210_readl(fotg210, status_reg);
  7529. + temp &= ~PORT_RWC_BITS;
  7530. + switch (wValue) {
  7531. + case USB_PORT_FEAT_SUSPEND:
  7532. + if ((temp & PORT_PE) == 0
  7533. + || (temp & PORT_RESET) != 0)
  7534. + goto error;
  7535. +
  7536. + /* After above check the port must be connected.
  7537. + * Set appropriate bit thus could put phy into low power
  7538. + * mode if we have hostpc feature
  7539. + */
  7540. + fotg210_writel(fotg210, temp | PORT_SUSPEND,
  7541. + status_reg);
  7542. + set_bit(wIndex, &fotg210->suspended_ports);
  7543. + break;
  7544. + case USB_PORT_FEAT_RESET:
  7545. + if (temp & PORT_RESUME)
  7546. + goto error;
  7547. + /* line status bits may report this as low speed,
  7548. + * which can be fine if this root hub has a
  7549. + * transaction translator built in.
  7550. + */
  7551. + fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
  7552. + temp |= PORT_RESET;
  7553. + temp &= ~PORT_PE;
  7554. +
  7555. + /*
  7556. + * caller must wait, then call GetPortStatus
  7557. + * usb 2.0 spec says 50 ms resets on root
  7558. + */
  7559. + fotg210->reset_done[wIndex] = jiffies
  7560. + + msecs_to_jiffies(50);
  7561. + fotg210_writel(fotg210, temp, status_reg);
  7562. + break;
  7563. +
  7564. + /* For downstream facing ports (these): one hub port is put
  7565. + * into test mode according to USB2 11.24.2.13, then the hub
  7566. + * must be reset (which for root hub now means rmmod+modprobe,
  7567. + * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  7568. + * about the EHCI-specific stuff.
  7569. + */
  7570. + case USB_PORT_FEAT_TEST:
  7571. + if (!selector || selector > 5)
  7572. + goto error;
  7573. + spin_unlock_irqrestore(&fotg210->lock, flags);
  7574. + fotg210_quiesce(fotg210);
  7575. + spin_lock_irqsave(&fotg210->lock, flags);
  7576. +
  7577. + /* Put all enabled ports into suspend */
  7578. + temp = fotg210_readl(fotg210, status_reg) &
  7579. + ~PORT_RWC_BITS;
  7580. + if (temp & PORT_PE)
  7581. + fotg210_writel(fotg210, temp | PORT_SUSPEND,
  7582. + status_reg);
  7583. +
  7584. + spin_unlock_irqrestore(&fotg210->lock, flags);
  7585. + fotg210_halt(fotg210);
  7586. + spin_lock_irqsave(&fotg210->lock, flags);
  7587. +
  7588. + temp = fotg210_readl(fotg210, status_reg);
  7589. + temp |= selector << 16;
  7590. + fotg210_writel(fotg210, temp, status_reg);
  7591. + break;
  7592. +
  7593. + default:
  7594. + goto error;
  7595. + }
  7596. + fotg210_readl(fotg210, &fotg210->regs->command);
  7597. + break;
  7598. +
  7599. + default:
  7600. +error:
  7601. + /* "stall" on error */
  7602. + retval = -EPIPE;
  7603. + }
  7604. + spin_unlock_irqrestore(&fotg210->lock, flags);
  7605. + return retval;
  7606. +}
  7607. +
  7608. +static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
  7609. + int portnum)
  7610. +{
  7611. + return;
  7612. +}
  7613. +
  7614. +static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
  7615. + int portnum)
  7616. +{
  7617. + return 0;
  7618. +}
  7619. +
  7620. +/* There's basically three types of memory:
  7621. + * - data used only by the HCD ... kmalloc is fine
  7622. + * - async and periodic schedules, shared by HC and HCD ... these
  7623. + * need to use dma_pool or dma_alloc_coherent
  7624. + * - driver buffers, read/written by HC ... single shot DMA mapped
  7625. + *
  7626. + * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
  7627. + * No memory seen by this driver is pageable.
  7628. + */
  7629. +
  7630. +/* Allocate the key transfer structures from the previously allocated pool */
  7631. +static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
  7632. + struct fotg210_qtd *qtd, dma_addr_t dma)
  7633. +{
  7634. + memset(qtd, 0, sizeof(*qtd));
  7635. + qtd->qtd_dma = dma;
  7636. + qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
  7637. + qtd->hw_next = FOTG210_LIST_END(fotg210);
  7638. + qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
  7639. + INIT_LIST_HEAD(&qtd->qtd_list);
  7640. +}
  7641. +
  7642. +static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
  7643. + gfp_t flags)
  7644. +{
  7645. + struct fotg210_qtd *qtd;
  7646. + dma_addr_t dma;
  7647. +
  7648. + qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
  7649. + if (qtd != NULL)
  7650. + fotg210_qtd_init(fotg210, qtd, dma);
  7651. +
  7652. + return qtd;
  7653. +}
  7654. +
  7655. +static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
  7656. + struct fotg210_qtd *qtd)
  7657. +{
  7658. + dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
  7659. +}
  7660. +
  7661. +
  7662. +static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  7663. +{
  7664. + /* clean qtds first, and know this is not linked */
  7665. + if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
  7666. + fotg210_dbg(fotg210, "unused qh not empty!\n");
  7667. + BUG();
  7668. + }
  7669. + if (qh->dummy)
  7670. + fotg210_qtd_free(fotg210, qh->dummy);
  7671. + dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
  7672. + kfree(qh);
  7673. +}
  7674. +
  7675. +static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
  7676. + gfp_t flags)
  7677. +{
  7678. + struct fotg210_qh *qh;
  7679. + dma_addr_t dma;
  7680. +
  7681. + qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
  7682. + if (!qh)
  7683. + goto done;
  7684. + qh->hw = (struct fotg210_qh_hw *)
  7685. + dma_pool_zalloc(fotg210->qh_pool, flags, &dma);
  7686. + if (!qh->hw)
  7687. + goto fail;
  7688. + qh->qh_dma = dma;
  7689. + INIT_LIST_HEAD(&qh->qtd_list);
  7690. +
  7691. + /* dummy td enables safe urb queuing */
  7692. + qh->dummy = fotg210_qtd_alloc(fotg210, flags);
  7693. + if (qh->dummy == NULL) {
  7694. + fotg210_dbg(fotg210, "no dummy td\n");
  7695. + goto fail1;
  7696. + }
  7697. +done:
  7698. + return qh;
  7699. +fail1:
  7700. + dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
  7701. +fail:
  7702. + kfree(qh);
  7703. + return NULL;
  7704. +}
  7705. +
  7706. +/* The queue heads and transfer descriptors are managed from pools tied
  7707. + * to each of the "per device" structures.
  7708. + * This is the initialisation and cleanup code.
  7709. + */
  7710. +
  7711. +static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
  7712. +{
  7713. + if (fotg210->async)
  7714. + qh_destroy(fotg210, fotg210->async);
  7715. + fotg210->async = NULL;
  7716. +
  7717. + if (fotg210->dummy)
  7718. + qh_destroy(fotg210, fotg210->dummy);
  7719. + fotg210->dummy = NULL;
  7720. +
  7721. + /* DMA consistent memory and pools */
  7722. + dma_pool_destroy(fotg210->qtd_pool);
  7723. + fotg210->qtd_pool = NULL;
  7724. +
  7725. + dma_pool_destroy(fotg210->qh_pool);
  7726. + fotg210->qh_pool = NULL;
  7727. +
  7728. + dma_pool_destroy(fotg210->itd_pool);
  7729. + fotg210->itd_pool = NULL;
  7730. +
  7731. + if (fotg210->periodic)
  7732. + dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller,
  7733. + fotg210->periodic_size * sizeof(u32),
  7734. + fotg210->periodic, fotg210->periodic_dma);
  7735. + fotg210->periodic = NULL;
  7736. +
  7737. + /* shadow periodic table */
  7738. + kfree(fotg210->pshadow);
  7739. + fotg210->pshadow = NULL;
  7740. +}
  7741. +
  7742. +/* remember to add cleanup code (above) if you add anything here */
  7743. +static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
  7744. +{
  7745. + int i;
  7746. +
  7747. + /* QTDs for control/bulk/intr transfers */
  7748. + fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
  7749. + fotg210_to_hcd(fotg210)->self.controller,
  7750. + sizeof(struct fotg210_qtd),
  7751. + 32 /* byte alignment (for hw parts) */,
  7752. + 4096 /* can't cross 4K */);
  7753. + if (!fotg210->qtd_pool)
  7754. + goto fail;
  7755. +
  7756. + /* QHs for control/bulk/intr transfers */
  7757. + fotg210->qh_pool = dma_pool_create("fotg210_qh",
  7758. + fotg210_to_hcd(fotg210)->self.controller,
  7759. + sizeof(struct fotg210_qh_hw),
  7760. + 32 /* byte alignment (for hw parts) */,
  7761. + 4096 /* can't cross 4K */);
  7762. + if (!fotg210->qh_pool)
  7763. + goto fail;
  7764. +
  7765. + fotg210->async = fotg210_qh_alloc(fotg210, flags);
  7766. + if (!fotg210->async)
  7767. + goto fail;
  7768. +
  7769. + /* ITD for high speed ISO transfers */
  7770. + fotg210->itd_pool = dma_pool_create("fotg210_itd",
  7771. + fotg210_to_hcd(fotg210)->self.controller,
  7772. + sizeof(struct fotg210_itd),
  7773. + 64 /* byte alignment (for hw parts) */,
  7774. + 4096 /* can't cross 4K */);
  7775. + if (!fotg210->itd_pool)
  7776. + goto fail;
  7777. +
  7778. + /* Hardware periodic table */
  7779. + fotg210->periodic =
  7780. + dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller,
  7781. + fotg210->periodic_size * sizeof(__le32),
  7782. + &fotg210->periodic_dma, 0);
  7783. + if (fotg210->periodic == NULL)
  7784. + goto fail;
  7785. +
  7786. + for (i = 0; i < fotg210->periodic_size; i++)
  7787. + fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
  7788. +
  7789. + /* software shadow of hardware table */
  7790. + fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
  7791. + flags);
  7792. + if (fotg210->pshadow != NULL)
  7793. + return 0;
  7794. +
  7795. +fail:
  7796. + fotg210_dbg(fotg210, "couldn't init memory\n");
  7797. + fotg210_mem_cleanup(fotg210);
  7798. + return -ENOMEM;
  7799. +}
  7800. +/* EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
  7801. + *
  7802. + * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
  7803. + * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
  7804. + * buffers needed for the larger number). We use one QH per endpoint, queue
  7805. + * multiple urbs (all three types) per endpoint. URBs may need several qtds.
  7806. + *
  7807. + * ISO traffic uses "ISO TD" (itd) records, and (along with
  7808. + * interrupts) needs careful scheduling. Performance improvements can be
  7809. + * an ongoing challenge. That's in "ehci-sched.c".
  7810. + *
  7811. + * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
  7812. + * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
  7813. + * (b) special fields in qh entries or (c) split iso entries. TTs will
  7814. + * buffer low/full speed data so the host collects it at high speed.
  7815. + */
  7816. +
  7817. +/* fill a qtd, returning how much of the buffer we were able to queue up */
  7818. +static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd,
  7819. + dma_addr_t buf, size_t len, int token, int maxpacket)
  7820. +{
  7821. + int i, count;
  7822. + u64 addr = buf;
  7823. +
  7824. + /* one buffer entry per 4K ... first might be short or unaligned */
  7825. + qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr);
  7826. + qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32));
  7827. + count = 0x1000 - (buf & 0x0fff); /* rest of that page */
  7828. + if (likely(len < count)) /* ... iff needed */
  7829. + count = len;
  7830. + else {
  7831. + buf += 0x1000;
  7832. + buf &= ~0x0fff;
  7833. +
  7834. + /* per-qtd limit: from 16K to 20K (best alignment) */
  7835. + for (i = 1; count < len && i < 5; i++) {
  7836. + addr = buf;
  7837. + qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
  7838. + qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
  7839. + (u32)(addr >> 32));
  7840. + buf += 0x1000;
  7841. + if ((count + 0x1000) < len)
  7842. + count += 0x1000;
  7843. + else
  7844. + count = len;
  7845. + }
  7846. +
  7847. + /* short packets may only terminate transfers */
  7848. + if (count != len)
  7849. + count -= (count % maxpacket);
  7850. + }
  7851. + qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
  7852. + qtd->length = count;
  7853. +
  7854. + return count;
  7855. +}
  7856. +
  7857. +static inline void qh_update(struct fotg210_hcd *fotg210,
  7858. + struct fotg210_qh *qh, struct fotg210_qtd *qtd)
  7859. +{
  7860. + struct fotg210_qh_hw *hw = qh->hw;
  7861. +
  7862. + /* writes to an active overlay are unsafe */
  7863. + BUG_ON(qh->qh_state != QH_STATE_IDLE);
  7864. +
  7865. + hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  7866. + hw->hw_alt_next = FOTG210_LIST_END(fotg210);
  7867. +
  7868. + /* Except for control endpoints, we make hardware maintain data
  7869. + * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
  7870. + * and set the pseudo-toggle in udev. Only usb_clear_halt() will
  7871. + * ever clear it.
  7872. + */
  7873. + if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
  7874. + unsigned is_out, epnum;
  7875. +
  7876. + is_out = qh->is_out;
  7877. + epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f;
  7878. + if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
  7879. + hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE);
  7880. + usb_settoggle(qh->dev, epnum, is_out, 1);
  7881. + }
  7882. + }
  7883. +
  7884. + hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
  7885. +}
  7886. +
  7887. +/* if it weren't for a common silicon quirk (writing the dummy into the qh
  7888. + * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
  7889. + * recovery (including urb dequeue) would need software changes to a QH...
  7890. + */
  7891. +static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  7892. +{
  7893. + struct fotg210_qtd *qtd;
  7894. +
  7895. + if (list_empty(&qh->qtd_list))
  7896. + qtd = qh->dummy;
  7897. + else {
  7898. + qtd = list_entry(qh->qtd_list.next,
  7899. + struct fotg210_qtd, qtd_list);
  7900. + /*
  7901. + * first qtd may already be partially processed.
  7902. + * If we come here during unlink, the QH overlay region
  7903. + * might have reference to the just unlinked qtd. The
  7904. + * qtd is updated in qh_completions(). Update the QH
  7905. + * overlay here.
  7906. + */
  7907. + if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
  7908. + qh->hw->hw_qtd_next = qtd->hw_next;
  7909. + qtd = NULL;
  7910. + }
  7911. + }
  7912. +
  7913. + if (qtd)
  7914. + qh_update(fotg210, qh, qtd);
  7915. +}
  7916. +
  7917. +static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  7918. +
  7919. +static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
  7920. + struct usb_host_endpoint *ep)
  7921. +{
  7922. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  7923. + struct fotg210_qh *qh = ep->hcpriv;
  7924. + unsigned long flags;
  7925. +
  7926. + spin_lock_irqsave(&fotg210->lock, flags);
  7927. + qh->clearing_tt = 0;
  7928. + if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
  7929. + && fotg210->rh_state == FOTG210_RH_RUNNING)
  7930. + qh_link_async(fotg210, qh);
  7931. + spin_unlock_irqrestore(&fotg210->lock, flags);
  7932. +}
  7933. +
  7934. +static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
  7935. + struct fotg210_qh *qh, struct urb *urb, u32 token)
  7936. +{
  7937. +
  7938. + /* If an async split transaction gets an error or is unlinked,
  7939. + * the TT buffer may be left in an indeterminate state. We
  7940. + * have to clear the TT buffer.
  7941. + *
  7942. + * Note: this routine is never called for Isochronous transfers.
  7943. + */
  7944. + if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
  7945. + struct usb_device *tt = urb->dev->tt->hub;
  7946. +
  7947. + dev_dbg(&tt->dev,
  7948. + "clear tt buffer port %d, a%d ep%d t%08x\n",
  7949. + urb->dev->ttport, urb->dev->devnum,
  7950. + usb_pipeendpoint(urb->pipe), token);
  7951. +
  7952. + if (urb->dev->tt->hub !=
  7953. + fotg210_to_hcd(fotg210)->self.root_hub) {
  7954. + if (usb_hub_clear_tt_buffer(urb) == 0)
  7955. + qh->clearing_tt = 1;
  7956. + }
  7957. + }
  7958. +}
  7959. +
  7960. +static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb,
  7961. + size_t length, u32 token)
  7962. +{
  7963. + int status = -EINPROGRESS;
  7964. +
  7965. + /* count IN/OUT bytes, not SETUP (even short packets) */
  7966. + if (likely(QTD_PID(token) != 2))
  7967. + urb->actual_length += length - QTD_LENGTH(token);
  7968. +
  7969. + /* don't modify error codes */
  7970. + if (unlikely(urb->unlinked))
  7971. + return status;
  7972. +
  7973. + /* force cleanup after short read; not always an error */
  7974. + if (unlikely(IS_SHORT_READ(token)))
  7975. + status = -EREMOTEIO;
  7976. +
  7977. + /* serious "can't proceed" faults reported by the hardware */
  7978. + if (token & QTD_STS_HALT) {
  7979. + if (token & QTD_STS_BABBLE) {
  7980. + /* FIXME "must" disable babbling device's port too */
  7981. + status = -EOVERFLOW;
  7982. + /* CERR nonzero + halt --> stall */
  7983. + } else if (QTD_CERR(token)) {
  7984. + status = -EPIPE;
  7985. +
  7986. + /* In theory, more than one of the following bits can be set
  7987. + * since they are sticky and the transaction is retried.
  7988. + * Which to test first is rather arbitrary.
  7989. + */
  7990. + } else if (token & QTD_STS_MMF) {
  7991. + /* fs/ls interrupt xfer missed the complete-split */
  7992. + status = -EPROTO;
  7993. + } else if (token & QTD_STS_DBE) {
  7994. + status = (QTD_PID(token) == 1) /* IN ? */
  7995. + ? -ENOSR /* hc couldn't read data */
  7996. + : -ECOMM; /* hc couldn't write data */
  7997. + } else if (token & QTD_STS_XACT) {
  7998. + /* timeout, bad CRC, wrong PID, etc */
  7999. + fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
  8000. + urb->dev->devpath,
  8001. + usb_pipeendpoint(urb->pipe),
  8002. + usb_pipein(urb->pipe) ? "in" : "out");
  8003. + status = -EPROTO;
  8004. + } else { /* unknown */
  8005. + status = -EPROTO;
  8006. + }
  8007. +
  8008. + fotg210_dbg(fotg210,
  8009. + "dev%d ep%d%s qtd token %08x --> status %d\n",
  8010. + usb_pipedevice(urb->pipe),
  8011. + usb_pipeendpoint(urb->pipe),
  8012. + usb_pipein(urb->pipe) ? "in" : "out",
  8013. + token, status);
  8014. + }
  8015. +
  8016. + return status;
  8017. +}
  8018. +
  8019. +static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb,
  8020. + int status)
  8021. +__releases(fotg210->lock)
  8022. +__acquires(fotg210->lock)
  8023. +{
  8024. + if (likely(urb->hcpriv != NULL)) {
  8025. + struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
  8026. +
  8027. + /* S-mask in a QH means it's an interrupt urb */
  8028. + if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
  8029. +
  8030. + /* ... update hc-wide periodic stats (for usbfs) */
  8031. + fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
  8032. + }
  8033. + }
  8034. +
  8035. + if (unlikely(urb->unlinked)) {
  8036. + INCR(fotg210->stats.unlink);
  8037. + } else {
  8038. + /* report non-error and short read status as zero */
  8039. + if (status == -EINPROGRESS || status == -EREMOTEIO)
  8040. + status = 0;
  8041. + INCR(fotg210->stats.complete);
  8042. + }
  8043. +
  8044. +#ifdef FOTG210_URB_TRACE
  8045. + fotg210_dbg(fotg210,
  8046. + "%s %s urb %p ep%d%s status %d len %d/%d\n",
  8047. + __func__, urb->dev->devpath, urb,
  8048. + usb_pipeendpoint(urb->pipe),
  8049. + usb_pipein(urb->pipe) ? "in" : "out",
  8050. + status,
  8051. + urb->actual_length, urb->transfer_buffer_length);
  8052. +#endif
  8053. +
  8054. + /* complete() can reenter this HCD */
  8055. + usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  8056. + spin_unlock(&fotg210->lock);
  8057. + usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status);
  8058. + spin_lock(&fotg210->lock);
  8059. +}
  8060. +
  8061. +static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
  8062. +
  8063. +/* Process and free completed qtds for a qh, returning URBs to drivers.
  8064. + * Chases up to qh->hw_current. Returns number of completions called,
  8065. + * indicating how much "real" work we did.
  8066. + */
  8067. +static unsigned qh_completions(struct fotg210_hcd *fotg210,
  8068. + struct fotg210_qh *qh)
  8069. +{
  8070. + struct fotg210_qtd *last, *end = qh->dummy;
  8071. + struct fotg210_qtd *qtd, *tmp;
  8072. + int last_status;
  8073. + int stopped;
  8074. + unsigned count = 0;
  8075. + u8 state;
  8076. + struct fotg210_qh_hw *hw = qh->hw;
  8077. +
  8078. + if (unlikely(list_empty(&qh->qtd_list)))
  8079. + return count;
  8080. +
  8081. + /* completions (or tasks on other cpus) must never clobber HALT
  8082. + * till we've gone through and cleaned everything up, even when
  8083. + * they add urbs to this qh's queue or mark them for unlinking.
  8084. + *
  8085. + * NOTE: unlinking expects to be done in queue order.
  8086. + *
  8087. + * It's a bug for qh->qh_state to be anything other than
  8088. + * QH_STATE_IDLE, unless our caller is scan_async() or
  8089. + * scan_intr().
  8090. + */
  8091. + state = qh->qh_state;
  8092. + qh->qh_state = QH_STATE_COMPLETING;
  8093. + stopped = (state == QH_STATE_IDLE);
  8094. +
  8095. +rescan:
  8096. + last = NULL;
  8097. + last_status = -EINPROGRESS;
  8098. + qh->needs_rescan = 0;
  8099. +
  8100. + /* remove de-activated QTDs from front of queue.
  8101. + * after faults (including short reads), cleanup this urb
  8102. + * then let the queue advance.
  8103. + * if queue is stopped, handles unlinks.
  8104. + */
  8105. + list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
  8106. + struct urb *urb;
  8107. + u32 token = 0;
  8108. +
  8109. + urb = qtd->urb;
  8110. +
  8111. + /* clean up any state from previous QTD ...*/
  8112. + if (last) {
  8113. + if (likely(last->urb != urb)) {
  8114. + fotg210_urb_done(fotg210, last->urb,
  8115. + last_status);
  8116. + count++;
  8117. + last_status = -EINPROGRESS;
  8118. + }
  8119. + fotg210_qtd_free(fotg210, last);
  8120. + last = NULL;
  8121. + }
  8122. +
  8123. + /* ignore urbs submitted during completions we reported */
  8124. + if (qtd == end)
  8125. + break;
  8126. +
  8127. + /* hardware copies qtd out of qh overlay */
  8128. + rmb();
  8129. + token = hc32_to_cpu(fotg210, qtd->hw_token);
  8130. +
  8131. + /* always clean up qtds the hc de-activated */
  8132. +retry_xacterr:
  8133. + if ((token & QTD_STS_ACTIVE) == 0) {
  8134. +
  8135. + /* Report Data Buffer Error: non-fatal but useful */
  8136. + if (token & QTD_STS_DBE)
  8137. + fotg210_dbg(fotg210,
  8138. + "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  8139. + urb, usb_endpoint_num(&urb->ep->desc),
  8140. + usb_endpoint_dir_in(&urb->ep->desc)
  8141. + ? "in" : "out",
  8142. + urb->transfer_buffer_length, qtd, qh);
  8143. +
  8144. + /* on STALL, error, and short reads this urb must
  8145. + * complete and all its qtds must be recycled.
  8146. + */
  8147. + if ((token & QTD_STS_HALT) != 0) {
  8148. +
  8149. + /* retry transaction errors until we
  8150. + * reach the software xacterr limit
  8151. + */
  8152. + if ((token & QTD_STS_XACT) &&
  8153. + QTD_CERR(token) == 0 &&
  8154. + ++qh->xacterrs < QH_XACTERR_MAX &&
  8155. + !urb->unlinked) {
  8156. + fotg210_dbg(fotg210,
  8157. + "detected XactErr len %zu/%zu retry %d\n",
  8158. + qtd->length - QTD_LENGTH(token),
  8159. + qtd->length,
  8160. + qh->xacterrs);
  8161. +
  8162. + /* reset the token in the qtd and the
  8163. + * qh overlay (which still contains
  8164. + * the qtd) so that we pick up from
  8165. + * where we left off
  8166. + */
  8167. + token &= ~QTD_STS_HALT;
  8168. + token |= QTD_STS_ACTIVE |
  8169. + (FOTG210_TUNE_CERR << 10);
  8170. + qtd->hw_token = cpu_to_hc32(fotg210,
  8171. + token);
  8172. + wmb();
  8173. + hw->hw_token = cpu_to_hc32(fotg210,
  8174. + token);
  8175. + goto retry_xacterr;
  8176. + }
  8177. + stopped = 1;
  8178. +
  8179. + /* magic dummy for some short reads; qh won't advance.
  8180. + * that silicon quirk can kick in with this dummy too.
  8181. + *
  8182. + * other short reads won't stop the queue, including
  8183. + * control transfers (status stage handles that) or
  8184. + * most other single-qtd reads ... the queue stops if
  8185. + * URB_SHORT_NOT_OK was set so the driver submitting
  8186. + * the urbs could clean it up.
  8187. + */
  8188. + } else if (IS_SHORT_READ(token) &&
  8189. + !(qtd->hw_alt_next &
  8190. + FOTG210_LIST_END(fotg210))) {
  8191. + stopped = 1;
  8192. + }
  8193. +
  8194. + /* stop scanning when we reach qtds the hc is using */
  8195. + } else if (likely(!stopped
  8196. + && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
  8197. + break;
  8198. +
  8199. + /* scan the whole queue for unlinks whenever it stops */
  8200. + } else {
  8201. + stopped = 1;
  8202. +
  8203. + /* cancel everything if we halt, suspend, etc */
  8204. + if (fotg210->rh_state < FOTG210_RH_RUNNING)
  8205. + last_status = -ESHUTDOWN;
  8206. +
  8207. + /* this qtd is active; skip it unless a previous qtd
  8208. + * for its urb faulted, or its urb was canceled.
  8209. + */
  8210. + else if (last_status == -EINPROGRESS && !urb->unlinked)
  8211. + continue;
  8212. +
  8213. + /* qh unlinked; token in overlay may be most current */
  8214. + if (state == QH_STATE_IDLE &&
  8215. + cpu_to_hc32(fotg210, qtd->qtd_dma)
  8216. + == hw->hw_current) {
  8217. + token = hc32_to_cpu(fotg210, hw->hw_token);
  8218. +
  8219. + /* An unlink may leave an incomplete
  8220. + * async transaction in the TT buffer.
  8221. + * We have to clear it.
  8222. + */
  8223. + fotg210_clear_tt_buffer(fotg210, qh, urb,
  8224. + token);
  8225. + }
  8226. + }
  8227. +
  8228. + /* unless we already know the urb's status, collect qtd status
  8229. + * and update count of bytes transferred. in common short read
  8230. + * cases with only one data qtd (including control transfers),
  8231. + * queue processing won't halt. but with two or more qtds (for
  8232. + * example, with a 32 KB transfer), when the first qtd gets a
  8233. + * short read the second must be removed by hand.
  8234. + */
  8235. + if (last_status == -EINPROGRESS) {
  8236. + last_status = qtd_copy_status(fotg210, urb,
  8237. + qtd->length, token);
  8238. + if (last_status == -EREMOTEIO &&
  8239. + (qtd->hw_alt_next &
  8240. + FOTG210_LIST_END(fotg210)))
  8241. + last_status = -EINPROGRESS;
  8242. +
  8243. + /* As part of low/full-speed endpoint-halt processing
  8244. + * we must clear the TT buffer (11.17.5).
  8245. + */
  8246. + if (unlikely(last_status != -EINPROGRESS &&
  8247. + last_status != -EREMOTEIO)) {
  8248. + /* The TT's in some hubs malfunction when they
  8249. + * receive this request following a STALL (they
  8250. + * stop sending isochronous packets). Since a
  8251. + * STALL can't leave the TT buffer in a busy
  8252. + * state (if you believe Figures 11-48 - 11-51
  8253. + * in the USB 2.0 spec), we won't clear the TT
  8254. + * buffer in this case. Strictly speaking this
  8255. + * is a violation of the spec.
  8256. + */
  8257. + if (last_status != -EPIPE)
  8258. + fotg210_clear_tt_buffer(fotg210, qh,
  8259. + urb, token);
  8260. + }
  8261. + }
  8262. +
  8263. + /* if we're removing something not at the queue head,
  8264. + * patch the hardware queue pointer.
  8265. + */
  8266. + if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
  8267. + last = list_entry(qtd->qtd_list.prev,
  8268. + struct fotg210_qtd, qtd_list);
  8269. + last->hw_next = qtd->hw_next;
  8270. + }
  8271. +
  8272. + /* remove qtd; it's recycled after possible urb completion */
  8273. + list_del(&qtd->qtd_list);
  8274. + last = qtd;
  8275. +
  8276. + /* reinit the xacterr counter for the next qtd */
  8277. + qh->xacterrs = 0;
  8278. + }
  8279. +
  8280. + /* last urb's completion might still need calling */
  8281. + if (likely(last != NULL)) {
  8282. + fotg210_urb_done(fotg210, last->urb, last_status);
  8283. + count++;
  8284. + fotg210_qtd_free(fotg210, last);
  8285. + }
  8286. +
  8287. + /* Do we need to rescan for URBs dequeued during a giveback? */
  8288. + if (unlikely(qh->needs_rescan)) {
  8289. + /* If the QH is already unlinked, do the rescan now. */
  8290. + if (state == QH_STATE_IDLE)
  8291. + goto rescan;
  8292. +
  8293. + /* Otherwise we have to wait until the QH is fully unlinked.
  8294. + * Our caller will start an unlink if qh->needs_rescan is
  8295. + * set. But if an unlink has already started, nothing needs
  8296. + * to be done.
  8297. + */
  8298. + if (state != QH_STATE_LINKED)
  8299. + qh->needs_rescan = 0;
  8300. + }
  8301. +
  8302. + /* restore original state; caller must unlink or relink */
  8303. + qh->qh_state = state;
  8304. +
  8305. + /* be sure the hardware's done with the qh before refreshing
  8306. + * it after fault cleanup, or recovering from silicon wrongly
  8307. + * overlaying the dummy qtd (which reduces DMA chatter).
  8308. + */
  8309. + if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
  8310. + switch (state) {
  8311. + case QH_STATE_IDLE:
  8312. + qh_refresh(fotg210, qh);
  8313. + break;
  8314. + case QH_STATE_LINKED:
  8315. + /* We won't refresh a QH that's linked (after the HC
  8316. + * stopped the queue). That avoids a race:
  8317. + * - HC reads first part of QH;
  8318. + * - CPU updates that first part and the token;
  8319. + * - HC reads rest of that QH, including token
  8320. + * Result: HC gets an inconsistent image, and then
  8321. + * DMAs to/from the wrong memory (corrupting it).
  8322. + *
  8323. + * That should be rare for interrupt transfers,
  8324. + * except maybe high bandwidth ...
  8325. + */
  8326. +
  8327. + /* Tell the caller to start an unlink */
  8328. + qh->needs_rescan = 1;
  8329. + break;
  8330. + /* otherwise, unlink already started */
  8331. + }
  8332. + }
  8333. +
  8334. + return count;
  8335. +}
  8336. +
  8337. +/* reverse of qh_urb_transaction: free a list of TDs.
  8338. + * used for cleanup after errors, before HC sees an URB's TDs.
  8339. + */
  8340. +static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb,
  8341. + struct list_head *head)
  8342. +{
  8343. + struct fotg210_qtd *qtd, *temp;
  8344. +
  8345. + list_for_each_entry_safe(qtd, temp, head, qtd_list) {
  8346. + list_del(&qtd->qtd_list);
  8347. + fotg210_qtd_free(fotg210, qtd);
  8348. + }
  8349. +}
  8350. +
  8351. +/* create a list of filled qtds for this URB; won't link into qh.
  8352. + */
  8353. +static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210,
  8354. + struct urb *urb, struct list_head *head, gfp_t flags)
  8355. +{
  8356. + struct fotg210_qtd *qtd, *qtd_prev;
  8357. + dma_addr_t buf;
  8358. + int len, this_sg_len, maxpacket;
  8359. + int is_input;
  8360. + u32 token;
  8361. + int i;
  8362. + struct scatterlist *sg;
  8363. +
  8364. + /*
  8365. + * URBs map to sequences of QTDs: one logical transaction
  8366. + */
  8367. + qtd = fotg210_qtd_alloc(fotg210, flags);
  8368. + if (unlikely(!qtd))
  8369. + return NULL;
  8370. + list_add_tail(&qtd->qtd_list, head);
  8371. + qtd->urb = urb;
  8372. +
  8373. + token = QTD_STS_ACTIVE;
  8374. + token |= (FOTG210_TUNE_CERR << 10);
  8375. + /* for split transactions, SplitXState initialized to zero */
  8376. +
  8377. + len = urb->transfer_buffer_length;
  8378. + is_input = usb_pipein(urb->pipe);
  8379. + if (usb_pipecontrol(urb->pipe)) {
  8380. + /* SETUP pid */
  8381. + qtd_fill(fotg210, qtd, urb->setup_dma,
  8382. + sizeof(struct usb_ctrlrequest),
  8383. + token | (2 /* "setup" */ << 8), 8);
  8384. +
  8385. + /* ... and always at least one more pid */
  8386. + token ^= QTD_TOGGLE;
  8387. + qtd_prev = qtd;
  8388. + qtd = fotg210_qtd_alloc(fotg210, flags);
  8389. + if (unlikely(!qtd))
  8390. + goto cleanup;
  8391. + qtd->urb = urb;
  8392. + qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  8393. + list_add_tail(&qtd->qtd_list, head);
  8394. +
  8395. + /* for zero length DATA stages, STATUS is always IN */
  8396. + if (len == 0)
  8397. + token |= (1 /* "in" */ << 8);
  8398. + }
  8399. +
  8400. + /*
  8401. + * data transfer stage: buffer setup
  8402. + */
  8403. + i = urb->num_mapped_sgs;
  8404. + if (len > 0 && i > 0) {
  8405. + sg = urb->sg;
  8406. + buf = sg_dma_address(sg);
  8407. +
  8408. + /* urb->transfer_buffer_length may be smaller than the
  8409. + * size of the scatterlist (or vice versa)
  8410. + */
  8411. + this_sg_len = min_t(int, sg_dma_len(sg), len);
  8412. + } else {
  8413. + sg = NULL;
  8414. + buf = urb->transfer_dma;
  8415. + this_sg_len = len;
  8416. + }
  8417. +
  8418. + if (is_input)
  8419. + token |= (1 /* "in" */ << 8);
  8420. + /* else it's already initted to "out" pid (0 << 8) */
  8421. +
  8422. + maxpacket = usb_maxpacket(urb->dev, urb->pipe);
  8423. +
  8424. + /*
  8425. + * buffer gets wrapped in one or more qtds;
  8426. + * last one may be "short" (including zero len)
  8427. + * and may serve as a control status ack
  8428. + */
  8429. + for (;;) {
  8430. + int this_qtd_len;
  8431. +
  8432. + this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
  8433. + maxpacket);
  8434. + this_sg_len -= this_qtd_len;
  8435. + len -= this_qtd_len;
  8436. + buf += this_qtd_len;
  8437. +
  8438. + /*
  8439. + * short reads advance to a "magic" dummy instead of the next
  8440. + * qtd ... that forces the queue to stop, for manual cleanup.
  8441. + * (this will usually be overridden later.)
  8442. + */
  8443. + if (is_input)
  8444. + qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
  8445. +
  8446. + /* qh makes control packets use qtd toggle; maybe switch it */
  8447. + if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
  8448. + token ^= QTD_TOGGLE;
  8449. +
  8450. + if (likely(this_sg_len <= 0)) {
  8451. + if (--i <= 0 || len <= 0)
  8452. + break;
  8453. + sg = sg_next(sg);
  8454. + buf = sg_dma_address(sg);
  8455. + this_sg_len = min_t(int, sg_dma_len(sg), len);
  8456. + }
  8457. +
  8458. + qtd_prev = qtd;
  8459. + qtd = fotg210_qtd_alloc(fotg210, flags);
  8460. + if (unlikely(!qtd))
  8461. + goto cleanup;
  8462. + qtd->urb = urb;
  8463. + qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  8464. + list_add_tail(&qtd->qtd_list, head);
  8465. + }
  8466. +
  8467. + /*
  8468. + * unless the caller requires manual cleanup after short reads,
  8469. + * have the alt_next mechanism keep the queue running after the
  8470. + * last data qtd (the only one, for control and most other cases).
  8471. + */
  8472. + if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 ||
  8473. + usb_pipecontrol(urb->pipe)))
  8474. + qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
  8475. +
  8476. + /*
  8477. + * control requests may need a terminating data "status" ack;
  8478. + * other OUT ones may need a terminating short packet
  8479. + * (zero length).
  8480. + */
  8481. + if (likely(urb->transfer_buffer_length != 0)) {
  8482. + int one_more = 0;
  8483. +
  8484. + if (usb_pipecontrol(urb->pipe)) {
  8485. + one_more = 1;
  8486. + token ^= 0x0100; /* "in" <--> "out" */
  8487. + token |= QTD_TOGGLE; /* force DATA1 */
  8488. + } else if (usb_pipeout(urb->pipe)
  8489. + && (urb->transfer_flags & URB_ZERO_PACKET)
  8490. + && !(urb->transfer_buffer_length % maxpacket)) {
  8491. + one_more = 1;
  8492. + }
  8493. + if (one_more) {
  8494. + qtd_prev = qtd;
  8495. + qtd = fotg210_qtd_alloc(fotg210, flags);
  8496. + if (unlikely(!qtd))
  8497. + goto cleanup;
  8498. + qtd->urb = urb;
  8499. + qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
  8500. + list_add_tail(&qtd->qtd_list, head);
  8501. +
  8502. + /* never any data in such packets */
  8503. + qtd_fill(fotg210, qtd, 0, 0, token, 0);
  8504. + }
  8505. + }
  8506. +
  8507. + /* by default, enable interrupt on urb completion */
  8508. + if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
  8509. + qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
  8510. + return head;
  8511. +
  8512. +cleanup:
  8513. + qtd_list_free(fotg210, urb, head);
  8514. + return NULL;
  8515. +}
  8516. +
  8517. +/* Would be best to create all qh's from config descriptors,
  8518. + * when each interface/altsetting is established. Unlink
  8519. + * any previous qh and cancel its urbs first; endpoints are
  8520. + * implicitly reset then (data toggle too).
  8521. + * That'd mean updating how usbcore talks to HCDs. (2.7?)
  8522. + */
  8523. +
  8524. +
  8525. +/* Each QH holds a qtd list; a QH is used for everything except iso.
  8526. + *
  8527. + * For interrupt urbs, the scheduler must set the microframe scheduling
  8528. + * mask(s) each time the QH gets scheduled. For highspeed, that's
  8529. + * just one microframe in the s-mask. For split interrupt transactions
  8530. + * there are additional complications: c-mask, maybe FSTNs.
  8531. + */
  8532. +static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb,
  8533. + gfp_t flags)
  8534. +{
  8535. + struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags);
  8536. + struct usb_host_endpoint *ep;
  8537. + u32 info1 = 0, info2 = 0;
  8538. + int is_input, type;
  8539. + int maxp = 0;
  8540. + int mult;
  8541. + struct usb_tt *tt = urb->dev->tt;
  8542. + struct fotg210_qh_hw *hw;
  8543. +
  8544. + if (!qh)
  8545. + return qh;
  8546. +
  8547. + /*
  8548. + * init endpoint/device data for this QH
  8549. + */
  8550. + info1 |= usb_pipeendpoint(urb->pipe) << 8;
  8551. + info1 |= usb_pipedevice(urb->pipe) << 0;
  8552. +
  8553. + is_input = usb_pipein(urb->pipe);
  8554. + type = usb_pipetype(urb->pipe);
  8555. + ep = usb_pipe_endpoint(urb->dev, urb->pipe);
  8556. + maxp = usb_endpoint_maxp(&ep->desc);
  8557. + mult = usb_endpoint_maxp_mult(&ep->desc);
  8558. +
  8559. + /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
  8560. + * acts like up to 3KB, but is built from smaller packets.
  8561. + */
  8562. + if (maxp > 1024) {
  8563. + fotg210_dbg(fotg210, "bogus qh maxpacket %d\n", maxp);
  8564. + goto done;
  8565. + }
  8566. +
  8567. + /* Compute interrupt scheduling parameters just once, and save.
  8568. + * - allowing for high bandwidth, how many nsec/uframe are used?
  8569. + * - split transactions need a second CSPLIT uframe; same question
  8570. + * - splits also need a schedule gap (for full/low speed I/O)
  8571. + * - qh has a polling interval
  8572. + *
  8573. + * For control/bulk requests, the HC or TT handles these.
  8574. + */
  8575. + if (type == PIPE_INTERRUPT) {
  8576. + qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
  8577. + is_input, 0, mult * maxp));
  8578. + qh->start = NO_FRAME;
  8579. +
  8580. + if (urb->dev->speed == USB_SPEED_HIGH) {
  8581. + qh->c_usecs = 0;
  8582. + qh->gap_uf = 0;
  8583. +
  8584. + qh->period = urb->interval >> 3;
  8585. + if (qh->period == 0 && urb->interval != 1) {
  8586. + /* NOTE interval 2 or 4 uframes could work.
  8587. + * But interval 1 scheduling is simpler, and
  8588. + * includes high bandwidth.
  8589. + */
  8590. + urb->interval = 1;
  8591. + } else if (qh->period > fotg210->periodic_size) {
  8592. + qh->period = fotg210->periodic_size;
  8593. + urb->interval = qh->period << 3;
  8594. + }
  8595. + } else {
  8596. + int think_time;
  8597. +
  8598. + /* gap is f(FS/LS transfer times) */
  8599. + qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
  8600. + is_input, 0, maxp) / (125 * 1000);
  8601. +
  8602. + /* FIXME this just approximates SPLIT/CSPLIT times */
  8603. + if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
  8604. + qh->c_usecs = qh->usecs + HS_USECS(0);
  8605. + qh->usecs = HS_USECS(1);
  8606. + } else { /* SPLIT+DATA, gap, CSPLIT */
  8607. + qh->usecs += HS_USECS(1);
  8608. + qh->c_usecs = HS_USECS(0);
  8609. + }
  8610. +
  8611. + think_time = tt ? tt->think_time : 0;
  8612. + qh->tt_usecs = NS_TO_US(think_time +
  8613. + usb_calc_bus_time(urb->dev->speed,
  8614. + is_input, 0, maxp));
  8615. + qh->period = urb->interval;
  8616. + if (qh->period > fotg210->periodic_size) {
  8617. + qh->period = fotg210->periodic_size;
  8618. + urb->interval = qh->period;
  8619. + }
  8620. + }
  8621. + }
  8622. +
  8623. + /* support for tt scheduling, and access to toggles */
  8624. + qh->dev = urb->dev;
  8625. +
  8626. + /* using TT? */
  8627. + switch (urb->dev->speed) {
  8628. + case USB_SPEED_LOW:
  8629. + info1 |= QH_LOW_SPEED;
  8630. + fallthrough;
  8631. +
  8632. + case USB_SPEED_FULL:
  8633. + /* EPS 0 means "full" */
  8634. + if (type != PIPE_INTERRUPT)
  8635. + info1 |= (FOTG210_TUNE_RL_TT << 28);
  8636. + if (type == PIPE_CONTROL) {
  8637. + info1 |= QH_CONTROL_EP; /* for TT */
  8638. + info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  8639. + }
  8640. + info1 |= maxp << 16;
  8641. +
  8642. + info2 |= (FOTG210_TUNE_MULT_TT << 30);
  8643. +
  8644. + /* Some Freescale processors have an erratum in which the
  8645. + * port number in the queue head was 0..N-1 instead of 1..N.
  8646. + */
  8647. + if (fotg210_has_fsl_portno_bug(fotg210))
  8648. + info2 |= (urb->dev->ttport-1) << 23;
  8649. + else
  8650. + info2 |= urb->dev->ttport << 23;
  8651. +
  8652. + /* set the address of the TT; for TDI's integrated
  8653. + * root hub tt, leave it zeroed.
  8654. + */
  8655. + if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
  8656. + info2 |= tt->hub->devnum << 16;
  8657. +
  8658. + /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
  8659. +
  8660. + break;
  8661. +
  8662. + case USB_SPEED_HIGH: /* no TT involved */
  8663. + info1 |= QH_HIGH_SPEED;
  8664. + if (type == PIPE_CONTROL) {
  8665. + info1 |= (FOTG210_TUNE_RL_HS << 28);
  8666. + info1 |= 64 << 16; /* usb2 fixed maxpacket */
  8667. + info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  8668. + info2 |= (FOTG210_TUNE_MULT_HS << 30);
  8669. + } else if (type == PIPE_BULK) {
  8670. + info1 |= (FOTG210_TUNE_RL_HS << 28);
  8671. + /* The USB spec says that high speed bulk endpoints
  8672. + * always use 512 byte maxpacket. But some device
  8673. + * vendors decided to ignore that, and MSFT is happy
  8674. + * to help them do so. So now people expect to use
  8675. + * such nonconformant devices with Linux too; sigh.
  8676. + */
  8677. + info1 |= maxp << 16;
  8678. + info2 |= (FOTG210_TUNE_MULT_HS << 30);
  8679. + } else { /* PIPE_INTERRUPT */
  8680. + info1 |= maxp << 16;
  8681. + info2 |= mult << 30;
  8682. + }
  8683. + break;
  8684. + default:
  8685. + fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
  8686. + urb->dev->speed);
  8687. +done:
  8688. + qh_destroy(fotg210, qh);
  8689. + return NULL;
  8690. + }
  8691. +
  8692. + /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
  8693. +
  8694. + /* init as live, toggle clear, advance to dummy */
  8695. + qh->qh_state = QH_STATE_IDLE;
  8696. + hw = qh->hw;
  8697. + hw->hw_info1 = cpu_to_hc32(fotg210, info1);
  8698. + hw->hw_info2 = cpu_to_hc32(fotg210, info2);
  8699. + qh->is_out = !is_input;
  8700. + usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
  8701. + qh_refresh(fotg210, qh);
  8702. + return qh;
  8703. +}
  8704. +
  8705. +static void enable_async(struct fotg210_hcd *fotg210)
  8706. +{
  8707. + if (fotg210->async_count++)
  8708. + return;
  8709. +
  8710. + /* Stop waiting to turn off the async schedule */
  8711. + fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
  8712. +
  8713. + /* Don't start the schedule until ASS is 0 */
  8714. + fotg210_poll_ASS(fotg210);
  8715. + turn_on_io_watchdog(fotg210);
  8716. +}
  8717. +
  8718. +static void disable_async(struct fotg210_hcd *fotg210)
  8719. +{
  8720. + if (--fotg210->async_count)
  8721. + return;
  8722. +
  8723. + /* The async schedule and async_unlink list are supposed to be empty */
  8724. + WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
  8725. +
  8726. + /* Don't turn off the schedule until ASS is 1 */
  8727. + fotg210_poll_ASS(fotg210);
  8728. +}
  8729. +
  8730. +/* move qh (and its qtds) onto async queue; maybe enable queue. */
  8731. +
  8732. +static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  8733. +{
  8734. + __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
  8735. + struct fotg210_qh *head;
  8736. +
  8737. + /* Don't link a QH if there's a Clear-TT-Buffer pending */
  8738. + if (unlikely(qh->clearing_tt))
  8739. + return;
  8740. +
  8741. + WARN_ON(qh->qh_state != QH_STATE_IDLE);
  8742. +
  8743. + /* clear halt and/or toggle; and maybe recover from silicon quirk */
  8744. + qh_refresh(fotg210, qh);
  8745. +
  8746. + /* splice right after start */
  8747. + head = fotg210->async;
  8748. + qh->qh_next = head->qh_next;
  8749. + qh->hw->hw_next = head->hw->hw_next;
  8750. + wmb();
  8751. +
  8752. + head->qh_next.qh = qh;
  8753. + head->hw->hw_next = dma;
  8754. +
  8755. + qh->xacterrs = 0;
  8756. + qh->qh_state = QH_STATE_LINKED;
  8757. + /* qtd completions reported later by interrupt */
  8758. +
  8759. + enable_async(fotg210);
  8760. +}
  8761. +
  8762. +/* For control/bulk/interrupt, return QH with these TDs appended.
  8763. + * Allocates and initializes the QH if necessary.
  8764. + * Returns null if it can't allocate a QH it needs to.
  8765. + * If the QH has TDs (urbs) already, that's great.
  8766. + */
  8767. +static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210,
  8768. + struct urb *urb, struct list_head *qtd_list,
  8769. + int epnum, void **ptr)
  8770. +{
  8771. + struct fotg210_qh *qh = NULL;
  8772. + __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
  8773. +
  8774. + qh = (struct fotg210_qh *) *ptr;
  8775. + if (unlikely(qh == NULL)) {
  8776. + /* can't sleep here, we have fotg210->lock... */
  8777. + qh = qh_make(fotg210, urb, GFP_ATOMIC);
  8778. + *ptr = qh;
  8779. + }
  8780. + if (likely(qh != NULL)) {
  8781. + struct fotg210_qtd *qtd;
  8782. +
  8783. + if (unlikely(list_empty(qtd_list)))
  8784. + qtd = NULL;
  8785. + else
  8786. + qtd = list_entry(qtd_list->next, struct fotg210_qtd,
  8787. + qtd_list);
  8788. +
  8789. + /* control qh may need patching ... */
  8790. + if (unlikely(epnum == 0)) {
  8791. + /* usb_reset_device() briefly reverts to address 0 */
  8792. + if (usb_pipedevice(urb->pipe) == 0)
  8793. + qh->hw->hw_info1 &= ~qh_addr_mask;
  8794. + }
  8795. +
  8796. + /* just one way to queue requests: swap with the dummy qtd.
  8797. + * only hc or qh_refresh() ever modify the overlay.
  8798. + */
  8799. + if (likely(qtd != NULL)) {
  8800. + struct fotg210_qtd *dummy;
  8801. + dma_addr_t dma;
  8802. + __hc32 token;
  8803. +
  8804. + /* to avoid racing the HC, use the dummy td instead of
  8805. + * the first td of our list (becomes new dummy). both
  8806. + * tds stay deactivated until we're done, when the
  8807. + * HC is allowed to fetch the old dummy (4.10.2).
  8808. + */
  8809. + token = qtd->hw_token;
  8810. + qtd->hw_token = HALT_BIT(fotg210);
  8811. +
  8812. + dummy = qh->dummy;
  8813. +
  8814. + dma = dummy->qtd_dma;
  8815. + *dummy = *qtd;
  8816. + dummy->qtd_dma = dma;
  8817. +
  8818. + list_del(&qtd->qtd_list);
  8819. + list_add(&dummy->qtd_list, qtd_list);
  8820. + list_splice_tail(qtd_list, &qh->qtd_list);
  8821. +
  8822. + fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
  8823. + qh->dummy = qtd;
  8824. +
  8825. + /* hc must see the new dummy at list end */
  8826. + dma = qtd->qtd_dma;
  8827. + qtd = list_entry(qh->qtd_list.prev,
  8828. + struct fotg210_qtd, qtd_list);
  8829. + qtd->hw_next = QTD_NEXT(fotg210, dma);
  8830. +
  8831. + /* let the hc process these next qtds */
  8832. + wmb();
  8833. + dummy->hw_token = token;
  8834. +
  8835. + urb->hcpriv = qh;
  8836. + }
  8837. + }
  8838. + return qh;
  8839. +}
  8840. +
  8841. +static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb,
  8842. + struct list_head *qtd_list, gfp_t mem_flags)
  8843. +{
  8844. + int epnum;
  8845. + unsigned long flags;
  8846. + struct fotg210_qh *qh = NULL;
  8847. + int rc;
  8848. +
  8849. + epnum = urb->ep->desc.bEndpointAddress;
  8850. +
  8851. +#ifdef FOTG210_URB_TRACE
  8852. + {
  8853. + struct fotg210_qtd *qtd;
  8854. +
  8855. + qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list);
  8856. + fotg210_dbg(fotg210,
  8857. + "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  8858. + __func__, urb->dev->devpath, urb,
  8859. + epnum & 0x0f, (epnum & USB_DIR_IN)
  8860. + ? "in" : "out",
  8861. + urb->transfer_buffer_length,
  8862. + qtd, urb->ep->hcpriv);
  8863. + }
  8864. +#endif
  8865. +
  8866. + spin_lock_irqsave(&fotg210->lock, flags);
  8867. + if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  8868. + rc = -ESHUTDOWN;
  8869. + goto done;
  8870. + }
  8871. + rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  8872. + if (unlikely(rc))
  8873. + goto done;
  8874. +
  8875. + qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
  8876. + if (unlikely(qh == NULL)) {
  8877. + usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  8878. + rc = -ENOMEM;
  8879. + goto done;
  8880. + }
  8881. +
  8882. + /* Control/bulk operations through TTs don't need scheduling,
  8883. + * the HC and TT handle it when the TT has a buffer ready.
  8884. + */
  8885. + if (likely(qh->qh_state == QH_STATE_IDLE))
  8886. + qh_link_async(fotg210, qh);
  8887. +done:
  8888. + spin_unlock_irqrestore(&fotg210->lock, flags);
  8889. + if (unlikely(qh == NULL))
  8890. + qtd_list_free(fotg210, urb, qtd_list);
  8891. + return rc;
  8892. +}
  8893. +
  8894. +static void single_unlink_async(struct fotg210_hcd *fotg210,
  8895. + struct fotg210_qh *qh)
  8896. +{
  8897. + struct fotg210_qh *prev;
  8898. +
  8899. + /* Add to the end of the list of QHs waiting for the next IAAD */
  8900. + qh->qh_state = QH_STATE_UNLINK;
  8901. + if (fotg210->async_unlink)
  8902. + fotg210->async_unlink_last->unlink_next = qh;
  8903. + else
  8904. + fotg210->async_unlink = qh;
  8905. + fotg210->async_unlink_last = qh;
  8906. +
  8907. + /* Unlink it from the schedule */
  8908. + prev = fotg210->async;
  8909. + while (prev->qh_next.qh != qh)
  8910. + prev = prev->qh_next.qh;
  8911. +
  8912. + prev->hw->hw_next = qh->hw->hw_next;
  8913. + prev->qh_next = qh->qh_next;
  8914. + if (fotg210->qh_scan_next == qh)
  8915. + fotg210->qh_scan_next = qh->qh_next.qh;
  8916. +}
  8917. +
  8918. +static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
  8919. +{
  8920. + /*
  8921. + * Do nothing if an IAA cycle is already running or
  8922. + * if one will be started shortly.
  8923. + */
  8924. + if (fotg210->async_iaa || fotg210->async_unlinking)
  8925. + return;
  8926. +
  8927. + /* Do all the waiting QHs at once */
  8928. + fotg210->async_iaa = fotg210->async_unlink;
  8929. + fotg210->async_unlink = NULL;
  8930. +
  8931. + /* If the controller isn't running, we don't have to wait for it */
  8932. + if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
  8933. + if (!nested) /* Avoid recursion */
  8934. + end_unlink_async(fotg210);
  8935. +
  8936. + /* Otherwise start a new IAA cycle */
  8937. + } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
  8938. + /* Make sure the unlinks are all visible to the hardware */
  8939. + wmb();
  8940. +
  8941. + fotg210_writel(fotg210, fotg210->command | CMD_IAAD,
  8942. + &fotg210->regs->command);
  8943. + fotg210_readl(fotg210, &fotg210->regs->command);
  8944. + fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG,
  8945. + true);
  8946. + }
  8947. +}
  8948. +
  8949. +/* the async qh for the qtds being unlinked are now gone from the HC */
  8950. +
  8951. +static void end_unlink_async(struct fotg210_hcd *fotg210)
  8952. +{
  8953. + struct fotg210_qh *qh;
  8954. +
  8955. + /* Process the idle QHs */
  8956. +restart:
  8957. + fotg210->async_unlinking = true;
  8958. + while (fotg210->async_iaa) {
  8959. + qh = fotg210->async_iaa;
  8960. + fotg210->async_iaa = qh->unlink_next;
  8961. + qh->unlink_next = NULL;
  8962. +
  8963. + qh->qh_state = QH_STATE_IDLE;
  8964. + qh->qh_next.qh = NULL;
  8965. +
  8966. + qh_completions(fotg210, qh);
  8967. + if (!list_empty(&qh->qtd_list) &&
  8968. + fotg210->rh_state == FOTG210_RH_RUNNING)
  8969. + qh_link_async(fotg210, qh);
  8970. + disable_async(fotg210);
  8971. + }
  8972. + fotg210->async_unlinking = false;
  8973. +
  8974. + /* Start a new IAA cycle if any QHs are waiting for it */
  8975. + if (fotg210->async_unlink) {
  8976. + start_iaa_cycle(fotg210, true);
  8977. + if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
  8978. + goto restart;
  8979. + }
  8980. +}
  8981. +
  8982. +static void unlink_empty_async(struct fotg210_hcd *fotg210)
  8983. +{
  8984. + struct fotg210_qh *qh, *next;
  8985. + bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
  8986. + bool check_unlinks_later = false;
  8987. +
  8988. + /* Unlink all the async QHs that have been empty for a timer cycle */
  8989. + next = fotg210->async->qh_next.qh;
  8990. + while (next) {
  8991. + qh = next;
  8992. + next = qh->qh_next.qh;
  8993. +
  8994. + if (list_empty(&qh->qtd_list) &&
  8995. + qh->qh_state == QH_STATE_LINKED) {
  8996. + if (!stopped && qh->unlink_cycle ==
  8997. + fotg210->async_unlink_cycle)
  8998. + check_unlinks_later = true;
  8999. + else
  9000. + single_unlink_async(fotg210, qh);
  9001. + }
  9002. + }
  9003. +
  9004. + /* Start a new IAA cycle if any QHs are waiting for it */
  9005. + if (fotg210->async_unlink)
  9006. + start_iaa_cycle(fotg210, false);
  9007. +
  9008. + /* QHs that haven't been empty for long enough will be handled later */
  9009. + if (check_unlinks_later) {
  9010. + fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
  9011. + true);
  9012. + ++fotg210->async_unlink_cycle;
  9013. + }
  9014. +}
  9015. +
  9016. +/* makes sure the async qh will become idle */
  9017. +/* caller must own fotg210->lock */
  9018. +
  9019. +static void start_unlink_async(struct fotg210_hcd *fotg210,
  9020. + struct fotg210_qh *qh)
  9021. +{
  9022. + /*
  9023. + * If the QH isn't linked then there's nothing we can do
  9024. + * unless we were called during a giveback, in which case
  9025. + * qh_completions() has to deal with it.
  9026. + */
  9027. + if (qh->qh_state != QH_STATE_LINKED) {
  9028. + if (qh->qh_state == QH_STATE_COMPLETING)
  9029. + qh->needs_rescan = 1;
  9030. + return;
  9031. + }
  9032. +
  9033. + single_unlink_async(fotg210, qh);
  9034. + start_iaa_cycle(fotg210, false);
  9035. +}
  9036. +
  9037. +static void scan_async(struct fotg210_hcd *fotg210)
  9038. +{
  9039. + struct fotg210_qh *qh;
  9040. + bool check_unlinks_later = false;
  9041. +
  9042. + fotg210->qh_scan_next = fotg210->async->qh_next.qh;
  9043. + while (fotg210->qh_scan_next) {
  9044. + qh = fotg210->qh_scan_next;
  9045. + fotg210->qh_scan_next = qh->qh_next.qh;
  9046. +rescan:
  9047. + /* clean any finished work for this qh */
  9048. + if (!list_empty(&qh->qtd_list)) {
  9049. + int temp;
  9050. +
  9051. + /*
  9052. + * Unlinks could happen here; completion reporting
  9053. + * drops the lock. That's why fotg210->qh_scan_next
  9054. + * always holds the next qh to scan; if the next qh
  9055. + * gets unlinked then fotg210->qh_scan_next is adjusted
  9056. + * in single_unlink_async().
  9057. + */
  9058. + temp = qh_completions(fotg210, qh);
  9059. + if (qh->needs_rescan) {
  9060. + start_unlink_async(fotg210, qh);
  9061. + } else if (list_empty(&qh->qtd_list)
  9062. + && qh->qh_state == QH_STATE_LINKED) {
  9063. + qh->unlink_cycle = fotg210->async_unlink_cycle;
  9064. + check_unlinks_later = true;
  9065. + } else if (temp != 0)
  9066. + goto rescan;
  9067. + }
  9068. + }
  9069. +
  9070. + /*
  9071. + * Unlink empty entries, reducing DMA usage as well
  9072. + * as HCD schedule-scanning costs. Delay for any qh
  9073. + * we just scanned, there's a not-unusual case that it
  9074. + * doesn't stay idle for long.
  9075. + */
  9076. + if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING &&
  9077. + !(fotg210->enabled_hrtimer_events &
  9078. + BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) {
  9079. + fotg210_enable_event(fotg210,
  9080. + FOTG210_HRTIMER_ASYNC_UNLINKS, true);
  9081. + ++fotg210->async_unlink_cycle;
  9082. + }
  9083. +}
  9084. +/* EHCI scheduled transaction support: interrupt, iso, split iso
  9085. + * These are called "periodic" transactions in the EHCI spec.
  9086. + *
  9087. + * Note that for interrupt transfers, the QH/QTD manipulation is shared
  9088. + * with the "asynchronous" transaction support (control/bulk transfers).
  9089. + * The only real difference is in how interrupt transfers are scheduled.
  9090. + *
  9091. + * For ISO, we make an "iso_stream" head to serve the same role as a QH.
  9092. + * It keeps track of every ITD (or SITD) that's linked, and holds enough
  9093. + * pre-calculated schedule data to make appending to the queue be quick.
  9094. + */
  9095. +static int fotg210_get_frame(struct usb_hcd *hcd);
  9096. +
  9097. +/* periodic_next_shadow - return "next" pointer on shadow list
  9098. + * @periodic: host pointer to qh/itd
  9099. + * @tag: hardware tag for type of this record
  9100. + */
  9101. +static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210,
  9102. + union fotg210_shadow *periodic, __hc32 tag)
  9103. +{
  9104. + switch (hc32_to_cpu(fotg210, tag)) {
  9105. + case Q_TYPE_QH:
  9106. + return &periodic->qh->qh_next;
  9107. + case Q_TYPE_FSTN:
  9108. + return &periodic->fstn->fstn_next;
  9109. + default:
  9110. + return &periodic->itd->itd_next;
  9111. + }
  9112. +}
  9113. +
  9114. +static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210,
  9115. + union fotg210_shadow *periodic, __hc32 tag)
  9116. +{
  9117. + switch (hc32_to_cpu(fotg210, tag)) {
  9118. + /* our fotg210_shadow.qh is actually software part */
  9119. + case Q_TYPE_QH:
  9120. + return &periodic->qh->hw->hw_next;
  9121. + /* others are hw parts */
  9122. + default:
  9123. + return periodic->hw_next;
  9124. + }
  9125. +}
  9126. +
  9127. +/* caller must hold fotg210->lock */
  9128. +static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
  9129. + void *ptr)
  9130. +{
  9131. + union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
  9132. + __hc32 *hw_p = &fotg210->periodic[frame];
  9133. + union fotg210_shadow here = *prev_p;
  9134. +
  9135. + /* find predecessor of "ptr"; hw and shadow lists are in sync */
  9136. + while (here.ptr && here.ptr != ptr) {
  9137. + prev_p = periodic_next_shadow(fotg210, prev_p,
  9138. + Q_NEXT_TYPE(fotg210, *hw_p));
  9139. + hw_p = shadow_next_periodic(fotg210, &here,
  9140. + Q_NEXT_TYPE(fotg210, *hw_p));
  9141. + here = *prev_p;
  9142. + }
  9143. + /* an interrupt entry (at list end) could have been shared */
  9144. + if (!here.ptr)
  9145. + return;
  9146. +
  9147. + /* update shadow and hardware lists ... the old "next" pointers
  9148. + * from ptr may still be in use, the caller updates them.
  9149. + */
  9150. + *prev_p = *periodic_next_shadow(fotg210, &here,
  9151. + Q_NEXT_TYPE(fotg210, *hw_p));
  9152. +
  9153. + *hw_p = *shadow_next_periodic(fotg210, &here,
  9154. + Q_NEXT_TYPE(fotg210, *hw_p));
  9155. +}
  9156. +
  9157. +/* how many of the uframe's 125 usecs are allocated? */
  9158. +static unsigned short periodic_usecs(struct fotg210_hcd *fotg210,
  9159. + unsigned frame, unsigned uframe)
  9160. +{
  9161. + __hc32 *hw_p = &fotg210->periodic[frame];
  9162. + union fotg210_shadow *q = &fotg210->pshadow[frame];
  9163. + unsigned usecs = 0;
  9164. + struct fotg210_qh_hw *hw;
  9165. +
  9166. + while (q->ptr) {
  9167. + switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
  9168. + case Q_TYPE_QH:
  9169. + hw = q->qh->hw;
  9170. + /* is it in the S-mask? */
  9171. + if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
  9172. + usecs += q->qh->usecs;
  9173. + /* ... or C-mask? */
  9174. + if (hw->hw_info2 & cpu_to_hc32(fotg210,
  9175. + 1 << (8 + uframe)))
  9176. + usecs += q->qh->c_usecs;
  9177. + hw_p = &hw->hw_next;
  9178. + q = &q->qh->qh_next;
  9179. + break;
  9180. + /* case Q_TYPE_FSTN: */
  9181. + default:
  9182. + /* for "save place" FSTNs, count the relevant INTR
  9183. + * bandwidth from the previous frame
  9184. + */
  9185. + if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
  9186. + fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
  9187. +
  9188. + hw_p = &q->fstn->hw_next;
  9189. + q = &q->fstn->fstn_next;
  9190. + break;
  9191. + case Q_TYPE_ITD:
  9192. + if (q->itd->hw_transaction[uframe])
  9193. + usecs += q->itd->stream->usecs;
  9194. + hw_p = &q->itd->hw_next;
  9195. + q = &q->itd->itd_next;
  9196. + break;
  9197. + }
  9198. + }
  9199. + if (usecs > fotg210->uframe_periodic_max)
  9200. + fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
  9201. + frame * 8 + uframe, usecs);
  9202. + return usecs;
  9203. +}
  9204. +
  9205. +static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
  9206. +{
  9207. + if (!dev1->tt || !dev2->tt)
  9208. + return 0;
  9209. + if (dev1->tt != dev2->tt)
  9210. + return 0;
  9211. + if (dev1->tt->multi)
  9212. + return dev1->ttport == dev2->ttport;
  9213. + else
  9214. + return 1;
  9215. +}
  9216. +
  9217. +/* return true iff the device's transaction translator is available
  9218. + * for a periodic transfer starting at the specified frame, using
  9219. + * all the uframes in the mask.
  9220. + */
  9221. +static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period,
  9222. + struct usb_device *dev, unsigned frame, u32 uf_mask)
  9223. +{
  9224. + if (period == 0) /* error */
  9225. + return 0;
  9226. +
  9227. + /* note bandwidth wastage: split never follows csplit
  9228. + * (different dev or endpoint) until the next uframe.
  9229. + * calling convention doesn't make that distinction.
  9230. + */
  9231. + for (; frame < fotg210->periodic_size; frame += period) {
  9232. + union fotg210_shadow here;
  9233. + __hc32 type;
  9234. + struct fotg210_qh_hw *hw;
  9235. +
  9236. + here = fotg210->pshadow[frame];
  9237. + type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
  9238. + while (here.ptr) {
  9239. + switch (hc32_to_cpu(fotg210, type)) {
  9240. + case Q_TYPE_ITD:
  9241. + type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
  9242. + here = here.itd->itd_next;
  9243. + continue;
  9244. + case Q_TYPE_QH:
  9245. + hw = here.qh->hw;
  9246. + if (same_tt(dev, here.qh->dev)) {
  9247. + u32 mask;
  9248. +
  9249. + mask = hc32_to_cpu(fotg210,
  9250. + hw->hw_info2);
  9251. + /* "knows" no gap is needed */
  9252. + mask |= mask >> 8;
  9253. + if (mask & uf_mask)
  9254. + break;
  9255. + }
  9256. + type = Q_NEXT_TYPE(fotg210, hw->hw_next);
  9257. + here = here.qh->qh_next;
  9258. + continue;
  9259. + /* case Q_TYPE_FSTN: */
  9260. + default:
  9261. + fotg210_dbg(fotg210,
  9262. + "periodic frame %d bogus type %d\n",
  9263. + frame, type);
  9264. + }
  9265. +
  9266. + /* collision or error */
  9267. + return 0;
  9268. + }
  9269. + }
  9270. +
  9271. + /* no collision */
  9272. + return 1;
  9273. +}
  9274. +
  9275. +static void enable_periodic(struct fotg210_hcd *fotg210)
  9276. +{
  9277. + if (fotg210->periodic_count++)
  9278. + return;
  9279. +
  9280. + /* Stop waiting to turn off the periodic schedule */
  9281. + fotg210->enabled_hrtimer_events &=
  9282. + ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
  9283. +
  9284. + /* Don't start the schedule until PSS is 0 */
  9285. + fotg210_poll_PSS(fotg210);
  9286. + turn_on_io_watchdog(fotg210);
  9287. +}
  9288. +
  9289. +static void disable_periodic(struct fotg210_hcd *fotg210)
  9290. +{
  9291. + if (--fotg210->periodic_count)
  9292. + return;
  9293. +
  9294. + /* Don't turn off the schedule until PSS is 1 */
  9295. + fotg210_poll_PSS(fotg210);
  9296. +}
  9297. +
  9298. +/* periodic schedule slots have iso tds (normal or split) first, then a
  9299. + * sparse tree for active interrupt transfers.
  9300. + *
  9301. + * this just links in a qh; caller guarantees uframe masks are set right.
  9302. + * no FSTN support (yet; fotg210 0.96+)
  9303. + */
  9304. +static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  9305. +{
  9306. + unsigned i;
  9307. + unsigned period = qh->period;
  9308. +
  9309. + dev_dbg(&qh->dev->dev,
  9310. + "link qh%d-%04x/%p start %d [%d/%d us]\n", period,
  9311. + hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
  9312. + (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
  9313. + qh->c_usecs);
  9314. +
  9315. + /* high bandwidth, or otherwise every microframe */
  9316. + if (period == 0)
  9317. + period = 1;
  9318. +
  9319. + for (i = qh->start; i < fotg210->periodic_size; i += period) {
  9320. + union fotg210_shadow *prev = &fotg210->pshadow[i];
  9321. + __hc32 *hw_p = &fotg210->periodic[i];
  9322. + union fotg210_shadow here = *prev;
  9323. + __hc32 type = 0;
  9324. +
  9325. + /* skip the iso nodes at list head */
  9326. + while (here.ptr) {
  9327. + type = Q_NEXT_TYPE(fotg210, *hw_p);
  9328. + if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
  9329. + break;
  9330. + prev = periodic_next_shadow(fotg210, prev, type);
  9331. + hw_p = shadow_next_periodic(fotg210, &here, type);
  9332. + here = *prev;
  9333. + }
  9334. +
  9335. + /* sorting each branch by period (slow-->fast)
  9336. + * enables sharing interior tree nodes
  9337. + */
  9338. + while (here.ptr && qh != here.qh) {
  9339. + if (qh->period > here.qh->period)
  9340. + break;
  9341. + prev = &here.qh->qh_next;
  9342. + hw_p = &here.qh->hw->hw_next;
  9343. + here = *prev;
  9344. + }
  9345. + /* link in this qh, unless some earlier pass did that */
  9346. + if (qh != here.qh) {
  9347. + qh->qh_next = here;
  9348. + if (here.qh)
  9349. + qh->hw->hw_next = *hw_p;
  9350. + wmb();
  9351. + prev->qh = qh;
  9352. + *hw_p = QH_NEXT(fotg210, qh->qh_dma);
  9353. + }
  9354. + }
  9355. + qh->qh_state = QH_STATE_LINKED;
  9356. + qh->xacterrs = 0;
  9357. +
  9358. + /* update per-qh bandwidth for usbfs */
  9359. + fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
  9360. + ? ((qh->usecs + qh->c_usecs) / qh->period)
  9361. + : (qh->usecs * 8);
  9362. +
  9363. + list_add(&qh->intr_node, &fotg210->intr_qh_list);
  9364. +
  9365. + /* maybe enable periodic schedule processing */
  9366. + ++fotg210->intr_count;
  9367. + enable_periodic(fotg210);
  9368. +}
  9369. +
  9370. +static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
  9371. + struct fotg210_qh *qh)
  9372. +{
  9373. + unsigned i;
  9374. + unsigned period;
  9375. +
  9376. + /*
  9377. + * If qh is for a low/full-speed device, simply unlinking it
  9378. + * could interfere with an ongoing split transaction. To unlink
  9379. + * it safely would require setting the QH_INACTIVATE bit and
  9380. + * waiting at least one frame, as described in EHCI 4.12.2.5.
  9381. + *
  9382. + * We won't bother with any of this. Instead, we assume that the
  9383. + * only reason for unlinking an interrupt QH while the current URB
  9384. + * is still active is to dequeue all the URBs (flush the whole
  9385. + * endpoint queue).
  9386. + *
  9387. + * If rebalancing the periodic schedule is ever implemented, this
  9388. + * approach will no longer be valid.
  9389. + */
  9390. +
  9391. + /* high bandwidth, or otherwise part of every microframe */
  9392. + period = qh->period;
  9393. + if (!period)
  9394. + period = 1;
  9395. +
  9396. + for (i = qh->start; i < fotg210->periodic_size; i += period)
  9397. + periodic_unlink(fotg210, i, qh);
  9398. +
  9399. + /* update per-qh bandwidth for usbfs */
  9400. + fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
  9401. + ? ((qh->usecs + qh->c_usecs) / qh->period)
  9402. + : (qh->usecs * 8);
  9403. +
  9404. + dev_dbg(&qh->dev->dev,
  9405. + "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
  9406. + qh->period, hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
  9407. + (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
  9408. + qh->c_usecs);
  9409. +
  9410. + /* qh->qh_next still "live" to HC */
  9411. + qh->qh_state = QH_STATE_UNLINK;
  9412. + qh->qh_next.ptr = NULL;
  9413. +
  9414. + if (fotg210->qh_scan_next == qh)
  9415. + fotg210->qh_scan_next = list_entry(qh->intr_node.next,
  9416. + struct fotg210_qh, intr_node);
  9417. + list_del(&qh->intr_node);
  9418. +}
  9419. +
  9420. +static void start_unlink_intr(struct fotg210_hcd *fotg210,
  9421. + struct fotg210_qh *qh)
  9422. +{
  9423. + /* If the QH isn't linked then there's nothing we can do
  9424. + * unless we were called during a giveback, in which case
  9425. + * qh_completions() has to deal with it.
  9426. + */
  9427. + if (qh->qh_state != QH_STATE_LINKED) {
  9428. + if (qh->qh_state == QH_STATE_COMPLETING)
  9429. + qh->needs_rescan = 1;
  9430. + return;
  9431. + }
  9432. +
  9433. + qh_unlink_periodic(fotg210, qh);
  9434. +
  9435. + /* Make sure the unlinks are visible before starting the timer */
  9436. + wmb();
  9437. +
  9438. + /*
  9439. + * The EHCI spec doesn't say how long it takes the controller to
  9440. + * stop accessing an unlinked interrupt QH. The timer delay is
  9441. + * 9 uframes; presumably that will be long enough.
  9442. + */
  9443. + qh->unlink_cycle = fotg210->intr_unlink_cycle;
  9444. +
  9445. + /* New entries go at the end of the intr_unlink list */
  9446. + if (fotg210->intr_unlink)
  9447. + fotg210->intr_unlink_last->unlink_next = qh;
  9448. + else
  9449. + fotg210->intr_unlink = qh;
  9450. + fotg210->intr_unlink_last = qh;
  9451. +
  9452. + if (fotg210->intr_unlinking)
  9453. + ; /* Avoid recursive calls */
  9454. + else if (fotg210->rh_state < FOTG210_RH_RUNNING)
  9455. + fotg210_handle_intr_unlinks(fotg210);
  9456. + else if (fotg210->intr_unlink == qh) {
  9457. + fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
  9458. + true);
  9459. + ++fotg210->intr_unlink_cycle;
  9460. + }
  9461. +}
  9462. +
  9463. +static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  9464. +{
  9465. + struct fotg210_qh_hw *hw = qh->hw;
  9466. + int rc;
  9467. +
  9468. + qh->qh_state = QH_STATE_IDLE;
  9469. + hw->hw_next = FOTG210_LIST_END(fotg210);
  9470. +
  9471. + qh_completions(fotg210, qh);
  9472. +
  9473. + /* reschedule QH iff another request is queued */
  9474. + if (!list_empty(&qh->qtd_list) &&
  9475. + fotg210->rh_state == FOTG210_RH_RUNNING) {
  9476. + rc = qh_schedule(fotg210, qh);
  9477. +
  9478. + /* An error here likely indicates handshake failure
  9479. + * or no space left in the schedule. Neither fault
  9480. + * should happen often ...
  9481. + *
  9482. + * FIXME kill the now-dysfunctional queued urbs
  9483. + */
  9484. + if (rc != 0)
  9485. + fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
  9486. + qh, rc);
  9487. + }
  9488. +
  9489. + /* maybe turn off periodic schedule */
  9490. + --fotg210->intr_count;
  9491. + disable_periodic(fotg210);
  9492. +}
  9493. +
  9494. +static int check_period(struct fotg210_hcd *fotg210, unsigned frame,
  9495. + unsigned uframe, unsigned period, unsigned usecs)
  9496. +{
  9497. + int claimed;
  9498. +
  9499. + /* complete split running into next frame?
  9500. + * given FSTN support, we could sometimes check...
  9501. + */
  9502. + if (uframe >= 8)
  9503. + return 0;
  9504. +
  9505. + /* convert "usecs we need" to "max already claimed" */
  9506. + usecs = fotg210->uframe_periodic_max - usecs;
  9507. +
  9508. + /* we "know" 2 and 4 uframe intervals were rejected; so
  9509. + * for period 0, check _every_ microframe in the schedule.
  9510. + */
  9511. + if (unlikely(period == 0)) {
  9512. + do {
  9513. + for (uframe = 0; uframe < 7; uframe++) {
  9514. + claimed = periodic_usecs(fotg210, frame,
  9515. + uframe);
  9516. + if (claimed > usecs)
  9517. + return 0;
  9518. + }
  9519. + } while ((frame += 1) < fotg210->periodic_size);
  9520. +
  9521. + /* just check the specified uframe, at that period */
  9522. + } else {
  9523. + do {
  9524. + claimed = periodic_usecs(fotg210, frame, uframe);
  9525. + if (claimed > usecs)
  9526. + return 0;
  9527. + } while ((frame += period) < fotg210->periodic_size);
  9528. + }
  9529. +
  9530. + /* success! */
  9531. + return 1;
  9532. +}
  9533. +
  9534. +static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame,
  9535. + unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp)
  9536. +{
  9537. + int retval = -ENOSPC;
  9538. + u8 mask = 0;
  9539. +
  9540. + if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
  9541. + goto done;
  9542. +
  9543. + if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
  9544. + goto done;
  9545. + if (!qh->c_usecs) {
  9546. + retval = 0;
  9547. + *c_maskp = 0;
  9548. + goto done;
  9549. + }
  9550. +
  9551. + /* Make sure this tt's buffer is also available for CSPLITs.
  9552. + * We pessimize a bit; probably the typical full speed case
  9553. + * doesn't need the second CSPLIT.
  9554. + *
  9555. + * NOTE: both SPLIT and CSPLIT could be checked in just
  9556. + * one smart pass...
  9557. + */
  9558. + mask = 0x03 << (uframe + qh->gap_uf);
  9559. + *c_maskp = cpu_to_hc32(fotg210, mask << 8);
  9560. +
  9561. + mask |= 1 << uframe;
  9562. + if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) {
  9563. + if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1,
  9564. + qh->period, qh->c_usecs))
  9565. + goto done;
  9566. + if (!check_period(fotg210, frame, uframe + qh->gap_uf,
  9567. + qh->period, qh->c_usecs))
  9568. + goto done;
  9569. + retval = 0;
  9570. + }
  9571. +done:
  9572. + return retval;
  9573. +}
  9574. +
  9575. +/* "first fit" scheduling policy used the first time through,
  9576. + * or when the previous schedule slot can't be re-used.
  9577. + */
  9578. +static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
  9579. +{
  9580. + int status;
  9581. + unsigned uframe;
  9582. + __hc32 c_mask;
  9583. + unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
  9584. + struct fotg210_qh_hw *hw = qh->hw;
  9585. +
  9586. + qh_refresh(fotg210, qh);
  9587. + hw->hw_next = FOTG210_LIST_END(fotg210);
  9588. + frame = qh->start;
  9589. +
  9590. + /* reuse the previous schedule slots, if we can */
  9591. + if (frame < qh->period) {
  9592. + uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK);
  9593. + status = check_intr_schedule(fotg210, frame, --uframe,
  9594. + qh, &c_mask);
  9595. + } else {
  9596. + uframe = 0;
  9597. + c_mask = 0;
  9598. + status = -ENOSPC;
  9599. + }
  9600. +
  9601. + /* else scan the schedule to find a group of slots such that all
  9602. + * uframes have enough periodic bandwidth available.
  9603. + */
  9604. + if (status) {
  9605. + /* "normal" case, uframing flexible except with splits */
  9606. + if (qh->period) {
  9607. + int i;
  9608. +
  9609. + for (i = qh->period; status && i > 0; --i) {
  9610. + frame = ++fotg210->random_frame % qh->period;
  9611. + for (uframe = 0; uframe < 8; uframe++) {
  9612. + status = check_intr_schedule(fotg210,
  9613. + frame, uframe, qh,
  9614. + &c_mask);
  9615. + if (status == 0)
  9616. + break;
  9617. + }
  9618. + }
  9619. +
  9620. + /* qh->period == 0 means every uframe */
  9621. + } else {
  9622. + frame = 0;
  9623. + status = check_intr_schedule(fotg210, 0, 0, qh,
  9624. + &c_mask);
  9625. + }
  9626. + if (status)
  9627. + goto done;
  9628. + qh->start = frame;
  9629. +
  9630. + /* reset S-frame and (maybe) C-frame masks */
  9631. + hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK));
  9632. + hw->hw_info2 |= qh->period
  9633. + ? cpu_to_hc32(fotg210, 1 << uframe)
  9634. + : cpu_to_hc32(fotg210, QH_SMASK);
  9635. + hw->hw_info2 |= c_mask;
  9636. + } else
  9637. + fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
  9638. +
  9639. + /* stuff into the periodic schedule */
  9640. + qh_link_periodic(fotg210, qh);
  9641. +done:
  9642. + return status;
  9643. +}
  9644. +
  9645. +static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb,
  9646. + struct list_head *qtd_list, gfp_t mem_flags)
  9647. +{
  9648. + unsigned epnum;
  9649. + unsigned long flags;
  9650. + struct fotg210_qh *qh;
  9651. + int status;
  9652. + struct list_head empty;
  9653. +
  9654. + /* get endpoint and transfer/schedule data */
  9655. + epnum = urb->ep->desc.bEndpointAddress;
  9656. +
  9657. + spin_lock_irqsave(&fotg210->lock, flags);
  9658. +
  9659. + if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  9660. + status = -ESHUTDOWN;
  9661. + goto done_not_linked;
  9662. + }
  9663. + status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  9664. + if (unlikely(status))
  9665. + goto done_not_linked;
  9666. +
  9667. + /* get qh and force any scheduling errors */
  9668. + INIT_LIST_HEAD(&empty);
  9669. + qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
  9670. + if (qh == NULL) {
  9671. + status = -ENOMEM;
  9672. + goto done;
  9673. + }
  9674. + if (qh->qh_state == QH_STATE_IDLE) {
  9675. + status = qh_schedule(fotg210, qh);
  9676. + if (status)
  9677. + goto done;
  9678. + }
  9679. +
  9680. + /* then queue the urb's tds to the qh */
  9681. + qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
  9682. + BUG_ON(qh == NULL);
  9683. +
  9684. + /* ... update usbfs periodic stats */
  9685. + fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
  9686. +
  9687. +done:
  9688. + if (unlikely(status))
  9689. + usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  9690. +done_not_linked:
  9691. + spin_unlock_irqrestore(&fotg210->lock, flags);
  9692. + if (status)
  9693. + qtd_list_free(fotg210, urb, qtd_list);
  9694. +
  9695. + return status;
  9696. +}
  9697. +
  9698. +static void scan_intr(struct fotg210_hcd *fotg210)
  9699. +{
  9700. + struct fotg210_qh *qh;
  9701. +
  9702. + list_for_each_entry_safe(qh, fotg210->qh_scan_next,
  9703. + &fotg210->intr_qh_list, intr_node) {
  9704. +rescan:
  9705. + /* clean any finished work for this qh */
  9706. + if (!list_empty(&qh->qtd_list)) {
  9707. + int temp;
  9708. +
  9709. + /*
  9710. + * Unlinks could happen here; completion reporting
  9711. + * drops the lock. That's why fotg210->qh_scan_next
  9712. + * always holds the next qh to scan; if the next qh
  9713. + * gets unlinked then fotg210->qh_scan_next is adjusted
  9714. + * in qh_unlink_periodic().
  9715. + */
  9716. + temp = qh_completions(fotg210, qh);
  9717. + if (unlikely(qh->needs_rescan ||
  9718. + (list_empty(&qh->qtd_list) &&
  9719. + qh->qh_state == QH_STATE_LINKED)))
  9720. + start_unlink_intr(fotg210, qh);
  9721. + else if (temp != 0)
  9722. + goto rescan;
  9723. + }
  9724. + }
  9725. +}
  9726. +
  9727. +/* fotg210_iso_stream ops work with both ITD and SITD */
  9728. +
  9729. +static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags)
  9730. +{
  9731. + struct fotg210_iso_stream *stream;
  9732. +
  9733. + stream = kzalloc(sizeof(*stream), mem_flags);
  9734. + if (likely(stream != NULL)) {
  9735. + INIT_LIST_HEAD(&stream->td_list);
  9736. + INIT_LIST_HEAD(&stream->free_list);
  9737. + stream->next_uframe = -1;
  9738. + }
  9739. + return stream;
  9740. +}
  9741. +
  9742. +static void iso_stream_init(struct fotg210_hcd *fotg210,
  9743. + struct fotg210_iso_stream *stream, struct usb_device *dev,
  9744. + int pipe, unsigned interval)
  9745. +{
  9746. + u32 buf1;
  9747. + unsigned epnum, maxp;
  9748. + int is_input;
  9749. + long bandwidth;
  9750. + unsigned multi;
  9751. + struct usb_host_endpoint *ep;
  9752. +
  9753. + /*
  9754. + * this might be a "high bandwidth" highspeed endpoint,
  9755. + * as encoded in the ep descriptor's wMaxPacket field
  9756. + */
  9757. + epnum = usb_pipeendpoint(pipe);
  9758. + is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
  9759. + ep = usb_pipe_endpoint(dev, pipe);
  9760. + maxp = usb_endpoint_maxp(&ep->desc);
  9761. + if (is_input)
  9762. + buf1 = (1 << 11);
  9763. + else
  9764. + buf1 = 0;
  9765. +
  9766. + multi = usb_endpoint_maxp_mult(&ep->desc);
  9767. + buf1 |= maxp;
  9768. + maxp *= multi;
  9769. +
  9770. + stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum);
  9771. + stream->buf1 = cpu_to_hc32(fotg210, buf1);
  9772. + stream->buf2 = cpu_to_hc32(fotg210, multi);
  9773. +
  9774. + /* usbfs wants to report the average usecs per frame tied up
  9775. + * when transfers on this endpoint are scheduled ...
  9776. + */
  9777. + if (dev->speed == USB_SPEED_FULL) {
  9778. + interval <<= 3;
  9779. + stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
  9780. + is_input, 1, maxp));
  9781. + stream->usecs /= 8;
  9782. + } else {
  9783. + stream->highspeed = 1;
  9784. + stream->usecs = HS_USECS_ISO(maxp);
  9785. + }
  9786. + bandwidth = stream->usecs * 8;
  9787. + bandwidth /= interval;
  9788. +
  9789. + stream->bandwidth = bandwidth;
  9790. + stream->udev = dev;
  9791. + stream->bEndpointAddress = is_input | epnum;
  9792. + stream->interval = interval;
  9793. + stream->maxp = maxp;
  9794. +}
  9795. +
  9796. +static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210,
  9797. + struct urb *urb)
  9798. +{
  9799. + unsigned epnum;
  9800. + struct fotg210_iso_stream *stream;
  9801. + struct usb_host_endpoint *ep;
  9802. + unsigned long flags;
  9803. +
  9804. + epnum = usb_pipeendpoint(urb->pipe);
  9805. + if (usb_pipein(urb->pipe))
  9806. + ep = urb->dev->ep_in[epnum];
  9807. + else
  9808. + ep = urb->dev->ep_out[epnum];
  9809. +
  9810. + spin_lock_irqsave(&fotg210->lock, flags);
  9811. + stream = ep->hcpriv;
  9812. +
  9813. + if (unlikely(stream == NULL)) {
  9814. + stream = iso_stream_alloc(GFP_ATOMIC);
  9815. + if (likely(stream != NULL)) {
  9816. + ep->hcpriv = stream;
  9817. + stream->ep = ep;
  9818. + iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
  9819. + urb->interval);
  9820. + }
  9821. +
  9822. + /* if dev->ep[epnum] is a QH, hw is set */
  9823. + } else if (unlikely(stream->hw != NULL)) {
  9824. + fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n",
  9825. + urb->dev->devpath, epnum,
  9826. + usb_pipein(urb->pipe) ? "in" : "out");
  9827. + stream = NULL;
  9828. + }
  9829. +
  9830. + spin_unlock_irqrestore(&fotg210->lock, flags);
  9831. + return stream;
  9832. +}
  9833. +
  9834. +/* fotg210_iso_sched ops can be ITD-only or SITD-only */
  9835. +
  9836. +static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
  9837. + gfp_t mem_flags)
  9838. +{
  9839. + struct fotg210_iso_sched *iso_sched;
  9840. +
  9841. + iso_sched = kzalloc(struct_size(iso_sched, packet, packets), mem_flags);
  9842. + if (likely(iso_sched != NULL))
  9843. + INIT_LIST_HEAD(&iso_sched->td_list);
  9844. +
  9845. + return iso_sched;
  9846. +}
  9847. +
  9848. +static inline void itd_sched_init(struct fotg210_hcd *fotg210,
  9849. + struct fotg210_iso_sched *iso_sched,
  9850. + struct fotg210_iso_stream *stream, struct urb *urb)
  9851. +{
  9852. + unsigned i;
  9853. + dma_addr_t dma = urb->transfer_dma;
  9854. +
  9855. + /* how many uframes are needed for these transfers */
  9856. + iso_sched->span = urb->number_of_packets * stream->interval;
  9857. +
  9858. + /* figure out per-uframe itd fields that we'll need later
  9859. + * when we fit new itds into the schedule.
  9860. + */
  9861. + for (i = 0; i < urb->number_of_packets; i++) {
  9862. + struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
  9863. + unsigned length;
  9864. + dma_addr_t buf;
  9865. + u32 trans;
  9866. +
  9867. + length = urb->iso_frame_desc[i].length;
  9868. + buf = dma + urb->iso_frame_desc[i].offset;
  9869. +
  9870. + trans = FOTG210_ISOC_ACTIVE;
  9871. + trans |= buf & 0x0fff;
  9872. + if (unlikely(((i + 1) == urb->number_of_packets))
  9873. + && !(urb->transfer_flags & URB_NO_INTERRUPT))
  9874. + trans |= FOTG210_ITD_IOC;
  9875. + trans |= length << 16;
  9876. + uframe->transaction = cpu_to_hc32(fotg210, trans);
  9877. +
  9878. + /* might need to cross a buffer page within a uframe */
  9879. + uframe->bufp = (buf & ~(u64)0x0fff);
  9880. + buf += length;
  9881. + if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
  9882. + uframe->cross = 1;
  9883. + }
  9884. +}
  9885. +
  9886. +static void iso_sched_free(struct fotg210_iso_stream *stream,
  9887. + struct fotg210_iso_sched *iso_sched)
  9888. +{
  9889. + if (!iso_sched)
  9890. + return;
  9891. + /* caller must hold fotg210->lock!*/
  9892. + list_splice(&iso_sched->td_list, &stream->free_list);
  9893. + kfree(iso_sched);
  9894. +}
  9895. +
  9896. +static int itd_urb_transaction(struct fotg210_iso_stream *stream,
  9897. + struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags)
  9898. +{
  9899. + struct fotg210_itd *itd;
  9900. + dma_addr_t itd_dma;
  9901. + int i;
  9902. + unsigned num_itds;
  9903. + struct fotg210_iso_sched *sched;
  9904. + unsigned long flags;
  9905. +
  9906. + sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
  9907. + if (unlikely(sched == NULL))
  9908. + return -ENOMEM;
  9909. +
  9910. + itd_sched_init(fotg210, sched, stream, urb);
  9911. +
  9912. + if (urb->interval < 8)
  9913. + num_itds = 1 + (sched->span + 7) / 8;
  9914. + else
  9915. + num_itds = urb->number_of_packets;
  9916. +
  9917. + /* allocate/init ITDs */
  9918. + spin_lock_irqsave(&fotg210->lock, flags);
  9919. + for (i = 0; i < num_itds; i++) {
  9920. +
  9921. + /*
  9922. + * Use iTDs from the free list, but not iTDs that may
  9923. + * still be in use by the hardware.
  9924. + */
  9925. + if (likely(!list_empty(&stream->free_list))) {
  9926. + itd = list_first_entry(&stream->free_list,
  9927. + struct fotg210_itd, itd_list);
  9928. + if (itd->frame == fotg210->now_frame)
  9929. + goto alloc_itd;
  9930. + list_del(&itd->itd_list);
  9931. + itd_dma = itd->itd_dma;
  9932. + } else {
  9933. +alloc_itd:
  9934. + spin_unlock_irqrestore(&fotg210->lock, flags);
  9935. + itd = dma_pool_alloc(fotg210->itd_pool, mem_flags,
  9936. + &itd_dma);
  9937. + spin_lock_irqsave(&fotg210->lock, flags);
  9938. + if (!itd) {
  9939. + iso_sched_free(stream, sched);
  9940. + spin_unlock_irqrestore(&fotg210->lock, flags);
  9941. + return -ENOMEM;
  9942. + }
  9943. + }
  9944. +
  9945. + memset(itd, 0, sizeof(*itd));
  9946. + itd->itd_dma = itd_dma;
  9947. + list_add(&itd->itd_list, &sched->td_list);
  9948. + }
  9949. + spin_unlock_irqrestore(&fotg210->lock, flags);
  9950. +
  9951. + /* temporarily store schedule info in hcpriv */
  9952. + urb->hcpriv = sched;
  9953. + urb->error_count = 0;
  9954. + return 0;
  9955. +}
  9956. +
  9957. +static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe,
  9958. + u8 usecs, u32 period)
  9959. +{
  9960. + uframe %= period;
  9961. + do {
  9962. + /* can't commit more than uframe_periodic_max usec */
  9963. + if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
  9964. + > (fotg210->uframe_periodic_max - usecs))
  9965. + return 0;
  9966. +
  9967. + /* we know urb->interval is 2^N uframes */
  9968. + uframe += period;
  9969. + } while (uframe < mod);
  9970. + return 1;
  9971. +}
  9972. +
  9973. +/* This scheduler plans almost as far into the future as it has actual
  9974. + * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
  9975. + * "as small as possible" to be cache-friendlier.) That limits the size
  9976. + * transfers you can stream reliably; avoid more than 64 msec per urb.
  9977. + * Also avoid queue depths of less than fotg210's worst irq latency (affected
  9978. + * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
  9979. + * and other factors); or more than about 230 msec total (for portability,
  9980. + * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
  9981. + */
  9982. +
  9983. +#define SCHEDULE_SLOP 80 /* microframes */
  9984. +
  9985. +static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb,
  9986. + struct fotg210_iso_stream *stream)
  9987. +{
  9988. + u32 now, next, start, period, span;
  9989. + int status;
  9990. + unsigned mod = fotg210->periodic_size << 3;
  9991. + struct fotg210_iso_sched *sched = urb->hcpriv;
  9992. +
  9993. + period = urb->interval;
  9994. + span = sched->span;
  9995. +
  9996. + if (span > mod - SCHEDULE_SLOP) {
  9997. + fotg210_dbg(fotg210, "iso request %p too long\n", urb);
  9998. + status = -EFBIG;
  9999. + goto fail;
  10000. + }
  10001. +
  10002. + now = fotg210_read_frame_index(fotg210) & (mod - 1);
  10003. +
  10004. + /* Typical case: reuse current schedule, stream is still active.
  10005. + * Hopefully there are no gaps from the host falling behind
  10006. + * (irq delays etc), but if there are we'll take the next
  10007. + * slot in the schedule, implicitly assuming URB_ISO_ASAP.
  10008. + */
  10009. + if (likely(!list_empty(&stream->td_list))) {
  10010. + u32 excess;
  10011. +
  10012. + /* For high speed devices, allow scheduling within the
  10013. + * isochronous scheduling threshold. For full speed devices
  10014. + * and Intel PCI-based controllers, don't (work around for
  10015. + * Intel ICH9 bug).
  10016. + */
  10017. + if (!stream->highspeed && fotg210->fs_i_thresh)
  10018. + next = now + fotg210->i_thresh;
  10019. + else
  10020. + next = now;
  10021. +
  10022. + /* Fell behind (by up to twice the slop amount)?
  10023. + * We decide based on the time of the last currently-scheduled
  10024. + * slot, not the time of the next available slot.
  10025. + */
  10026. + excess = (stream->next_uframe - period - next) & (mod - 1);
  10027. + if (excess >= mod - 2 * SCHEDULE_SLOP)
  10028. + start = next + excess - mod + period *
  10029. + DIV_ROUND_UP(mod - excess, period);
  10030. + else
  10031. + start = next + excess + period;
  10032. + if (start - now >= mod) {
  10033. + fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
  10034. + urb, start - now - period, period,
  10035. + mod);
  10036. + status = -EFBIG;
  10037. + goto fail;
  10038. + }
  10039. + }
  10040. +
  10041. + /* need to schedule; when's the next (u)frame we could start?
  10042. + * this is bigger than fotg210->i_thresh allows; scheduling itself
  10043. + * isn't free, the slop should handle reasonably slow cpus. it
  10044. + * can also help high bandwidth if the dma and irq loads don't
  10045. + * jump until after the queue is primed.
  10046. + */
  10047. + else {
  10048. + int done = 0;
  10049. +
  10050. + start = SCHEDULE_SLOP + (now & ~0x07);
  10051. +
  10052. + /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
  10053. +
  10054. + /* find a uframe slot with enough bandwidth.
  10055. + * Early uframes are more precious because full-speed
  10056. + * iso IN transfers can't use late uframes,
  10057. + * and therefore they should be allocated last.
  10058. + */
  10059. + next = start;
  10060. + start += period;
  10061. + do {
  10062. + start--;
  10063. + /* check schedule: enough space? */
  10064. + if (itd_slot_ok(fotg210, mod, start,
  10065. + stream->usecs, period))
  10066. + done = 1;
  10067. + } while (start > next && !done);
  10068. +
  10069. + /* no room in the schedule */
  10070. + if (!done) {
  10071. + fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
  10072. + urb, now, now + mod);
  10073. + status = -ENOSPC;
  10074. + goto fail;
  10075. + }
  10076. + }
  10077. +
  10078. + /* Tried to schedule too far into the future? */
  10079. + if (unlikely(start - now + span - period >=
  10080. + mod - 2 * SCHEDULE_SLOP)) {
  10081. + fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
  10082. + urb, start - now, span - period,
  10083. + mod - 2 * SCHEDULE_SLOP);
  10084. + status = -EFBIG;
  10085. + goto fail;
  10086. + }
  10087. +
  10088. + stream->next_uframe = start & (mod - 1);
  10089. +
  10090. + /* report high speed start in uframes; full speed, in frames */
  10091. + urb->start_frame = stream->next_uframe;
  10092. + if (!stream->highspeed)
  10093. + urb->start_frame >>= 3;
  10094. +
  10095. + /* Make sure scan_isoc() sees these */
  10096. + if (fotg210->isoc_count == 0)
  10097. + fotg210->next_frame = now >> 3;
  10098. + return 0;
  10099. +
  10100. +fail:
  10101. + iso_sched_free(stream, sched);
  10102. + urb->hcpriv = NULL;
  10103. + return status;
  10104. +}
  10105. +
  10106. +static inline void itd_init(struct fotg210_hcd *fotg210,
  10107. + struct fotg210_iso_stream *stream, struct fotg210_itd *itd)
  10108. +{
  10109. + int i;
  10110. +
  10111. + /* it's been recently zeroed */
  10112. + itd->hw_next = FOTG210_LIST_END(fotg210);
  10113. + itd->hw_bufp[0] = stream->buf0;
  10114. + itd->hw_bufp[1] = stream->buf1;
  10115. + itd->hw_bufp[2] = stream->buf2;
  10116. +
  10117. + for (i = 0; i < 8; i++)
  10118. + itd->index[i] = -1;
  10119. +
  10120. + /* All other fields are filled when scheduling */
  10121. +}
  10122. +
  10123. +static inline void itd_patch(struct fotg210_hcd *fotg210,
  10124. + struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched,
  10125. + unsigned index, u16 uframe)
  10126. +{
  10127. + struct fotg210_iso_packet *uf = &iso_sched->packet[index];
  10128. + unsigned pg = itd->pg;
  10129. +
  10130. + uframe &= 0x07;
  10131. + itd->index[uframe] = index;
  10132. +
  10133. + itd->hw_transaction[uframe] = uf->transaction;
  10134. + itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12);
  10135. + itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0);
  10136. + itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32));
  10137. +
  10138. + /* iso_frame_desc[].offset must be strictly increasing */
  10139. + if (unlikely(uf->cross)) {
  10140. + u64 bufp = uf->bufp + 4096;
  10141. +
  10142. + itd->pg = ++pg;
  10143. + itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0);
  10144. + itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32));
  10145. + }
  10146. +}
  10147. +
  10148. +static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame,
  10149. + struct fotg210_itd *itd)
  10150. +{
  10151. + union fotg210_shadow *prev = &fotg210->pshadow[frame];
  10152. + __hc32 *hw_p = &fotg210->periodic[frame];
  10153. + union fotg210_shadow here = *prev;
  10154. + __hc32 type = 0;
  10155. +
  10156. + /* skip any iso nodes which might belong to previous microframes */
  10157. + while (here.ptr) {
  10158. + type = Q_NEXT_TYPE(fotg210, *hw_p);
  10159. + if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
  10160. + break;
  10161. + prev = periodic_next_shadow(fotg210, prev, type);
  10162. + hw_p = shadow_next_periodic(fotg210, &here, type);
  10163. + here = *prev;
  10164. + }
  10165. +
  10166. + itd->itd_next = here;
  10167. + itd->hw_next = *hw_p;
  10168. + prev->itd = itd;
  10169. + itd->frame = frame;
  10170. + wmb();
  10171. + *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
  10172. +}
  10173. +
  10174. +/* fit urb's itds into the selected schedule slot; activate as needed */
  10175. +static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb,
  10176. + unsigned mod, struct fotg210_iso_stream *stream)
  10177. +{
  10178. + int packet;
  10179. + unsigned next_uframe, uframe, frame;
  10180. + struct fotg210_iso_sched *iso_sched = urb->hcpriv;
  10181. + struct fotg210_itd *itd;
  10182. +
  10183. + next_uframe = stream->next_uframe & (mod - 1);
  10184. +
  10185. + if (unlikely(list_empty(&stream->td_list))) {
  10186. + fotg210_to_hcd(fotg210)->self.bandwidth_allocated
  10187. + += stream->bandwidth;
  10188. + fotg210_dbg(fotg210,
  10189. + "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
  10190. + urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  10191. + (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  10192. + urb->interval,
  10193. + next_uframe >> 3, next_uframe & 0x7);
  10194. + }
  10195. +
  10196. + /* fill iTDs uframe by uframe */
  10197. + for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
  10198. + if (itd == NULL) {
  10199. + /* ASSERT: we have all necessary itds */
  10200. +
  10201. + /* ASSERT: no itds for this endpoint in this uframe */
  10202. +
  10203. + itd = list_entry(iso_sched->td_list.next,
  10204. + struct fotg210_itd, itd_list);
  10205. + list_move_tail(&itd->itd_list, &stream->td_list);
  10206. + itd->stream = stream;
  10207. + itd->urb = urb;
  10208. + itd_init(fotg210, stream, itd);
  10209. + }
  10210. +
  10211. + uframe = next_uframe & 0x07;
  10212. + frame = next_uframe >> 3;
  10213. +
  10214. + itd_patch(fotg210, itd, iso_sched, packet, uframe);
  10215. +
  10216. + next_uframe += stream->interval;
  10217. + next_uframe &= mod - 1;
  10218. + packet++;
  10219. +
  10220. + /* link completed itds into the schedule */
  10221. + if (((next_uframe >> 3) != frame)
  10222. + || packet == urb->number_of_packets) {
  10223. + itd_link(fotg210, frame & (fotg210->periodic_size - 1),
  10224. + itd);
  10225. + itd = NULL;
  10226. + }
  10227. + }
  10228. + stream->next_uframe = next_uframe;
  10229. +
  10230. + /* don't need that schedule data any more */
  10231. + iso_sched_free(stream, iso_sched);
  10232. + urb->hcpriv = NULL;
  10233. +
  10234. + ++fotg210->isoc_count;
  10235. + enable_periodic(fotg210);
  10236. +}
  10237. +
  10238. +#define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
  10239. + FOTG210_ISOC_XACTERR)
  10240. +
  10241. +/* Process and recycle a completed ITD. Return true iff its urb completed,
  10242. + * and hence its completion callback probably added things to the hardware
  10243. + * schedule.
  10244. + *
  10245. + * Note that we carefully avoid recycling this descriptor until after any
  10246. + * completion callback runs, so that it won't be reused quickly. That is,
  10247. + * assuming (a) no more than two urbs per frame on this endpoint, and also
  10248. + * (b) only this endpoint's completions submit URBs. It seems some silicon
  10249. + * corrupts things if you reuse completed descriptors very quickly...
  10250. + */
  10251. +static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
  10252. +{
  10253. + struct urb *urb = itd->urb;
  10254. + struct usb_iso_packet_descriptor *desc;
  10255. + u32 t;
  10256. + unsigned uframe;
  10257. + int urb_index = -1;
  10258. + struct fotg210_iso_stream *stream = itd->stream;
  10259. + struct usb_device *dev;
  10260. + bool retval = false;
  10261. +
  10262. + /* for each uframe with a packet */
  10263. + for (uframe = 0; uframe < 8; uframe++) {
  10264. + if (likely(itd->index[uframe] == -1))
  10265. + continue;
  10266. + urb_index = itd->index[uframe];
  10267. + desc = &urb->iso_frame_desc[urb_index];
  10268. +
  10269. + t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
  10270. + itd->hw_transaction[uframe] = 0;
  10271. +
  10272. + /* report transfer status */
  10273. + if (unlikely(t & ISO_ERRS)) {
  10274. + urb->error_count++;
  10275. + if (t & FOTG210_ISOC_BUF_ERR)
  10276. + desc->status = usb_pipein(urb->pipe)
  10277. + ? -ENOSR /* hc couldn't read */
  10278. + : -ECOMM; /* hc couldn't write */
  10279. + else if (t & FOTG210_ISOC_BABBLE)
  10280. + desc->status = -EOVERFLOW;
  10281. + else /* (t & FOTG210_ISOC_XACTERR) */
  10282. + desc->status = -EPROTO;
  10283. +
  10284. + /* HC need not update length with this error */
  10285. + if (!(t & FOTG210_ISOC_BABBLE)) {
  10286. + desc->actual_length = FOTG210_ITD_LENGTH(t);
  10287. + urb->actual_length += desc->actual_length;
  10288. + }
  10289. + } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
  10290. + desc->status = 0;
  10291. + desc->actual_length = FOTG210_ITD_LENGTH(t);
  10292. + urb->actual_length += desc->actual_length;
  10293. + } else {
  10294. + /* URB was too late */
  10295. + desc->status = -EXDEV;
  10296. + }
  10297. + }
  10298. +
  10299. + /* handle completion now? */
  10300. + if (likely((urb_index + 1) != urb->number_of_packets))
  10301. + goto done;
  10302. +
  10303. + /* ASSERT: it's really the last itd for this urb
  10304. + * list_for_each_entry (itd, &stream->td_list, itd_list)
  10305. + * BUG_ON (itd->urb == urb);
  10306. + */
  10307. +
  10308. + /* give urb back to the driver; completion often (re)submits */
  10309. + dev = urb->dev;
  10310. + fotg210_urb_done(fotg210, urb, 0);
  10311. + retval = true;
  10312. + urb = NULL;
  10313. +
  10314. + --fotg210->isoc_count;
  10315. + disable_periodic(fotg210);
  10316. +
  10317. + if (unlikely(list_is_singular(&stream->td_list))) {
  10318. + fotg210_to_hcd(fotg210)->self.bandwidth_allocated
  10319. + -= stream->bandwidth;
  10320. + fotg210_dbg(fotg210,
  10321. + "deschedule devp %s ep%d%s-iso\n",
  10322. + dev->devpath, stream->bEndpointAddress & 0x0f,
  10323. + (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  10324. + }
  10325. +
  10326. +done:
  10327. + itd->urb = NULL;
  10328. +
  10329. + /* Add to the end of the free list for later reuse */
  10330. + list_move_tail(&itd->itd_list, &stream->free_list);
  10331. +
  10332. + /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
  10333. + if (list_empty(&stream->td_list)) {
  10334. + list_splice_tail_init(&stream->free_list,
  10335. + &fotg210->cached_itd_list);
  10336. + start_free_itds(fotg210);
  10337. + }
  10338. +
  10339. + return retval;
  10340. +}
  10341. +
  10342. +static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
  10343. + gfp_t mem_flags)
  10344. +{
  10345. + int status = -EINVAL;
  10346. + unsigned long flags;
  10347. + struct fotg210_iso_stream *stream;
  10348. +
  10349. + /* Get iso_stream head */
  10350. + stream = iso_stream_find(fotg210, urb);
  10351. + if (unlikely(stream == NULL)) {
  10352. + fotg210_dbg(fotg210, "can't get iso stream\n");
  10353. + return -ENOMEM;
  10354. + }
  10355. + if (unlikely(urb->interval != stream->interval &&
  10356. + fotg210_port_speed(fotg210, 0) ==
  10357. + USB_PORT_STAT_HIGH_SPEED)) {
  10358. + fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n",
  10359. + stream->interval, urb->interval);
  10360. + goto done;
  10361. + }
  10362. +
  10363. +#ifdef FOTG210_URB_TRACE
  10364. + fotg210_dbg(fotg210,
  10365. + "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
  10366. + __func__, urb->dev->devpath, urb,
  10367. + usb_pipeendpoint(urb->pipe),
  10368. + usb_pipein(urb->pipe) ? "in" : "out",
  10369. + urb->transfer_buffer_length,
  10370. + urb->number_of_packets, urb->interval,
  10371. + stream);
  10372. +#endif
  10373. +
  10374. + /* allocate ITDs w/o locking anything */
  10375. + status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
  10376. + if (unlikely(status < 0)) {
  10377. + fotg210_dbg(fotg210, "can't init itds\n");
  10378. + goto done;
  10379. + }
  10380. +
  10381. + /* schedule ... need to lock */
  10382. + spin_lock_irqsave(&fotg210->lock, flags);
  10383. + if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
  10384. + status = -ESHUTDOWN;
  10385. + goto done_not_linked;
  10386. + }
  10387. + status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
  10388. + if (unlikely(status))
  10389. + goto done_not_linked;
  10390. + status = iso_stream_schedule(fotg210, urb, stream);
  10391. + if (likely(status == 0))
  10392. + itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream);
  10393. + else
  10394. + usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
  10395. +done_not_linked:
  10396. + spin_unlock_irqrestore(&fotg210->lock, flags);
  10397. +done:
  10398. + return status;
  10399. +}
  10400. +
  10401. +static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame,
  10402. + unsigned now_frame, bool live)
  10403. +{
  10404. + unsigned uf;
  10405. + bool modified;
  10406. + union fotg210_shadow q, *q_p;
  10407. + __hc32 type, *hw_p;
  10408. +
  10409. + /* scan each element in frame's queue for completions */
  10410. + q_p = &fotg210->pshadow[frame];
  10411. + hw_p = &fotg210->periodic[frame];
  10412. + q.ptr = q_p->ptr;
  10413. + type = Q_NEXT_TYPE(fotg210, *hw_p);
  10414. + modified = false;
  10415. +
  10416. + while (q.ptr) {
  10417. + switch (hc32_to_cpu(fotg210, type)) {
  10418. + case Q_TYPE_ITD:
  10419. + /* If this ITD is still active, leave it for
  10420. + * later processing ... check the next entry.
  10421. + * No need to check for activity unless the
  10422. + * frame is current.
  10423. + */
  10424. + if (frame == now_frame && live) {
  10425. + rmb();
  10426. + for (uf = 0; uf < 8; uf++) {
  10427. + if (q.itd->hw_transaction[uf] &
  10428. + ITD_ACTIVE(fotg210))
  10429. + break;
  10430. + }
  10431. + if (uf < 8) {
  10432. + q_p = &q.itd->itd_next;
  10433. + hw_p = &q.itd->hw_next;
  10434. + type = Q_NEXT_TYPE(fotg210,
  10435. + q.itd->hw_next);
  10436. + q = *q_p;
  10437. + break;
  10438. + }
  10439. + }
  10440. +
  10441. + /* Take finished ITDs out of the schedule
  10442. + * and process them: recycle, maybe report
  10443. + * URB completion. HC won't cache the
  10444. + * pointer for much longer, if at all.
  10445. + */
  10446. + *q_p = q.itd->itd_next;
  10447. + *hw_p = q.itd->hw_next;
  10448. + type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
  10449. + wmb();
  10450. + modified = itd_complete(fotg210, q.itd);
  10451. + q = *q_p;
  10452. + break;
  10453. + default:
  10454. + fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
  10455. + type, frame, q.ptr);
  10456. + fallthrough;
  10457. + case Q_TYPE_QH:
  10458. + case Q_TYPE_FSTN:
  10459. + /* End of the iTDs and siTDs */
  10460. + q.ptr = NULL;
  10461. + break;
  10462. + }
  10463. +
  10464. + /* assume completion callbacks modify the queue */
  10465. + if (unlikely(modified && fotg210->isoc_count > 0))
  10466. + return -EINVAL;
  10467. + }
  10468. + return 0;
  10469. +}
  10470. +
  10471. +static void scan_isoc(struct fotg210_hcd *fotg210)
  10472. +{
  10473. + unsigned uf, now_frame, frame, ret;
  10474. + unsigned fmask = fotg210->periodic_size - 1;
  10475. + bool live;
  10476. +
  10477. + /*
  10478. + * When running, scan from last scan point up to "now"
  10479. + * else clean up by scanning everything that's left.
  10480. + * Touches as few pages as possible: cache-friendly.
  10481. + */
  10482. + if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
  10483. + uf = fotg210_read_frame_index(fotg210);
  10484. + now_frame = (uf >> 3) & fmask;
  10485. + live = true;
  10486. + } else {
  10487. + now_frame = (fotg210->next_frame - 1) & fmask;
  10488. + live = false;
  10489. + }
  10490. + fotg210->now_frame = now_frame;
  10491. +
  10492. + frame = fotg210->next_frame;
  10493. + for (;;) {
  10494. + ret = 1;
  10495. + while (ret != 0)
  10496. + ret = scan_frame_queue(fotg210, frame,
  10497. + now_frame, live);
  10498. +
  10499. + /* Stop when we have reached the current frame */
  10500. + if (frame == now_frame)
  10501. + break;
  10502. + frame = (frame + 1) & fmask;
  10503. + }
  10504. + fotg210->next_frame = now_frame;
  10505. +}
  10506. +
  10507. +/* Display / Set uframe_periodic_max
  10508. + */
  10509. +static ssize_t uframe_periodic_max_show(struct device *dev,
  10510. + struct device_attribute *attr, char *buf)
  10511. +{
  10512. + struct fotg210_hcd *fotg210;
  10513. + int n;
  10514. +
  10515. + fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
  10516. + n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
  10517. + return n;
  10518. +}
  10519. +
  10520. +
  10521. +static ssize_t uframe_periodic_max_store(struct device *dev,
  10522. + struct device_attribute *attr, const char *buf, size_t count)
  10523. +{
  10524. + struct fotg210_hcd *fotg210;
  10525. + unsigned uframe_periodic_max;
  10526. + unsigned frame, uframe;
  10527. + unsigned short allocated_max;
  10528. + unsigned long flags;
  10529. + ssize_t ret;
  10530. +
  10531. + fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
  10532. + if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
  10533. + return -EINVAL;
  10534. +
  10535. + if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
  10536. + fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n",
  10537. + uframe_periodic_max);
  10538. + return -EINVAL;
  10539. + }
  10540. +
  10541. + ret = -EINVAL;
  10542. +
  10543. + /*
  10544. + * lock, so that our checking does not race with possible periodic
  10545. + * bandwidth allocation through submitting new urbs.
  10546. + */
  10547. + spin_lock_irqsave(&fotg210->lock, flags);
  10548. +
  10549. + /*
  10550. + * for request to decrease max periodic bandwidth, we have to check
  10551. + * every microframe in the schedule to see whether the decrease is
  10552. + * possible.
  10553. + */
  10554. + if (uframe_periodic_max < fotg210->uframe_periodic_max) {
  10555. + allocated_max = 0;
  10556. +
  10557. + for (frame = 0; frame < fotg210->periodic_size; ++frame)
  10558. + for (uframe = 0; uframe < 7; ++uframe)
  10559. + allocated_max = max(allocated_max,
  10560. + periodic_usecs(fotg210, frame,
  10561. + uframe));
  10562. +
  10563. + if (allocated_max > uframe_periodic_max) {
  10564. + fotg210_info(fotg210,
  10565. + "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n",
  10566. + allocated_max, uframe_periodic_max);
  10567. + goto out_unlock;
  10568. + }
  10569. + }
  10570. +
  10571. + /* increasing is always ok */
  10572. +
  10573. + fotg210_info(fotg210,
  10574. + "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
  10575. + 100 * uframe_periodic_max/125, uframe_periodic_max);
  10576. +
  10577. + if (uframe_periodic_max != 100)
  10578. + fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
  10579. +
  10580. + fotg210->uframe_periodic_max = uframe_periodic_max;
  10581. + ret = count;
  10582. +
  10583. +out_unlock:
  10584. + spin_unlock_irqrestore(&fotg210->lock, flags);
  10585. + return ret;
  10586. +}
  10587. +
  10588. +static DEVICE_ATTR_RW(uframe_periodic_max);
  10589. +
  10590. +static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
  10591. +{
  10592. + struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
  10593. +
  10594. + return device_create_file(controller, &dev_attr_uframe_periodic_max);
  10595. +}
  10596. +
  10597. +static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
  10598. +{
  10599. + struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
  10600. +
  10601. + device_remove_file(controller, &dev_attr_uframe_periodic_max);
  10602. +}
  10603. +/* On some systems, leaving remote wakeup enabled prevents system shutdown.
  10604. + * The firmware seems to think that powering off is a wakeup event!
  10605. + * This routine turns off remote wakeup and everything else, on all ports.
  10606. + */
  10607. +static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
  10608. +{
  10609. + u32 __iomem *status_reg = &fotg210->regs->port_status;
  10610. +
  10611. + fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
  10612. +}
  10613. +
  10614. +/* Halt HC, turn off all ports, and let the BIOS use the companion controllers.
  10615. + * Must be called with interrupts enabled and the lock not held.
  10616. + */
  10617. +static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
  10618. +{
  10619. + fotg210_halt(fotg210);
  10620. +
  10621. + spin_lock_irq(&fotg210->lock);
  10622. + fotg210->rh_state = FOTG210_RH_HALTED;
  10623. + fotg210_turn_off_all_ports(fotg210);
  10624. + spin_unlock_irq(&fotg210->lock);
  10625. +}
  10626. +
  10627. +/* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
  10628. + * This forcibly disables dma and IRQs, helping kexec and other cases
  10629. + * where the next system software may expect clean state.
  10630. + */
  10631. +static void fotg210_shutdown(struct usb_hcd *hcd)
  10632. +{
  10633. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  10634. +
  10635. + spin_lock_irq(&fotg210->lock);
  10636. + fotg210->shutdown = true;
  10637. + fotg210->rh_state = FOTG210_RH_STOPPING;
  10638. + fotg210->enabled_hrtimer_events = 0;
  10639. + spin_unlock_irq(&fotg210->lock);
  10640. +
  10641. + fotg210_silence_controller(fotg210);
  10642. +
  10643. + hrtimer_cancel(&fotg210->hrtimer);
  10644. +}
  10645. +
  10646. +/* fotg210_work is called from some interrupts, timers, and so on.
  10647. + * it calls driver completion functions, after dropping fotg210->lock.
  10648. + */
  10649. +static void fotg210_work(struct fotg210_hcd *fotg210)
  10650. +{
  10651. + /* another CPU may drop fotg210->lock during a schedule scan while
  10652. + * it reports urb completions. this flag guards against bogus
  10653. + * attempts at re-entrant schedule scanning.
  10654. + */
  10655. + if (fotg210->scanning) {
  10656. + fotg210->need_rescan = true;
  10657. + return;
  10658. + }
  10659. + fotg210->scanning = true;
  10660. +
  10661. +rescan:
  10662. + fotg210->need_rescan = false;
  10663. + if (fotg210->async_count)
  10664. + scan_async(fotg210);
  10665. + if (fotg210->intr_count > 0)
  10666. + scan_intr(fotg210);
  10667. + if (fotg210->isoc_count > 0)
  10668. + scan_isoc(fotg210);
  10669. + if (fotg210->need_rescan)
  10670. + goto rescan;
  10671. + fotg210->scanning = false;
  10672. +
  10673. + /* the IO watchdog guards against hardware or driver bugs that
  10674. + * misplace IRQs, and should let us run completely without IRQs.
  10675. + * such lossage has been observed on both VT6202 and VT8235.
  10676. + */
  10677. + turn_on_io_watchdog(fotg210);
  10678. +}
  10679. +
  10680. +/* Called when the fotg210_hcd module is removed.
  10681. + */
  10682. +static void fotg210_stop(struct usb_hcd *hcd)
  10683. +{
  10684. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  10685. +
  10686. + fotg210_dbg(fotg210, "stop\n");
  10687. +
  10688. + /* no more interrupts ... */
  10689. +
  10690. + spin_lock_irq(&fotg210->lock);
  10691. + fotg210->enabled_hrtimer_events = 0;
  10692. + spin_unlock_irq(&fotg210->lock);
  10693. +
  10694. + fotg210_quiesce(fotg210);
  10695. + fotg210_silence_controller(fotg210);
  10696. + fotg210_reset(fotg210);
  10697. +
  10698. + hrtimer_cancel(&fotg210->hrtimer);
  10699. + remove_sysfs_files(fotg210);
  10700. + remove_debug_files(fotg210);
  10701. +
  10702. + /* root hub is shut down separately (first, when possible) */
  10703. + spin_lock_irq(&fotg210->lock);
  10704. + end_free_itds(fotg210);
  10705. + spin_unlock_irq(&fotg210->lock);
  10706. + fotg210_mem_cleanup(fotg210);
  10707. +
  10708. +#ifdef FOTG210_STATS
  10709. + fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
  10710. + fotg210->stats.normal, fotg210->stats.error,
  10711. + fotg210->stats.iaa, fotg210->stats.lost_iaa);
  10712. + fotg210_dbg(fotg210, "complete %ld unlink %ld\n",
  10713. + fotg210->stats.complete, fotg210->stats.unlink);
  10714. +#endif
  10715. +
  10716. + dbg_status(fotg210, "fotg210_stop completed",
  10717. + fotg210_readl(fotg210, &fotg210->regs->status));
  10718. +}
  10719. +
  10720. +/* one-time init, only for memory state */
  10721. +static int hcd_fotg210_init(struct usb_hcd *hcd)
  10722. +{
  10723. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  10724. + u32 temp;
  10725. + int retval;
  10726. + u32 hcc_params;
  10727. + struct fotg210_qh_hw *hw;
  10728. +
  10729. + spin_lock_init(&fotg210->lock);
  10730. +
  10731. + /*
  10732. + * keep io watchdog by default, those good HCDs could turn off it later
  10733. + */
  10734. + fotg210->need_io_watchdog = 1;
  10735. +
  10736. + hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  10737. + fotg210->hrtimer.function = fotg210_hrtimer_func;
  10738. + fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
  10739. +
  10740. + hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  10741. +
  10742. + /*
  10743. + * by default set standard 80% (== 100 usec/uframe) max periodic
  10744. + * bandwidth as required by USB 2.0
  10745. + */
  10746. + fotg210->uframe_periodic_max = 100;
  10747. +
  10748. + /*
  10749. + * hw default: 1K periodic list heads, one per frame.
  10750. + * periodic_size can shrink by USBCMD update if hcc_params allows.
  10751. + */
  10752. + fotg210->periodic_size = DEFAULT_I_TDPS;
  10753. + INIT_LIST_HEAD(&fotg210->intr_qh_list);
  10754. + INIT_LIST_HEAD(&fotg210->cached_itd_list);
  10755. +
  10756. + if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  10757. + /* periodic schedule size can be smaller than default */
  10758. + switch (FOTG210_TUNE_FLS) {
  10759. + case 0:
  10760. + fotg210->periodic_size = 1024;
  10761. + break;
  10762. + case 1:
  10763. + fotg210->periodic_size = 512;
  10764. + break;
  10765. + case 2:
  10766. + fotg210->periodic_size = 256;
  10767. + break;
  10768. + default:
  10769. + BUG();
  10770. + }
  10771. + }
  10772. + retval = fotg210_mem_init(fotg210, GFP_KERNEL);
  10773. + if (retval < 0)
  10774. + return retval;
  10775. +
  10776. + /* controllers may cache some of the periodic schedule ... */
  10777. + fotg210->i_thresh = 2;
  10778. +
  10779. + /*
  10780. + * dedicate a qh for the async ring head, since we couldn't unlink
  10781. + * a 'real' qh without stopping the async schedule [4.8]. use it
  10782. + * as the 'reclamation list head' too.
  10783. + * its dummy is used in hw_alt_next of many tds, to prevent the qh
  10784. + * from automatically advancing to the next td after short reads.
  10785. + */
  10786. + fotg210->async->qh_next.qh = NULL;
  10787. + hw = fotg210->async->hw;
  10788. + hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma);
  10789. + hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD);
  10790. + hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
  10791. + hw->hw_qtd_next = FOTG210_LIST_END(fotg210);
  10792. + fotg210->async->qh_state = QH_STATE_LINKED;
  10793. + hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma);
  10794. +
  10795. + /* clear interrupt enables, set irq latency */
  10796. + if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  10797. + log2_irq_thresh = 0;
  10798. + temp = 1 << (16 + log2_irq_thresh);
  10799. + if (HCC_CANPARK(hcc_params)) {
  10800. + /* HW default park == 3, on hardware that supports it (like
  10801. + * NVidia and ALI silicon), maximizes throughput on the async
  10802. + * schedule by avoiding QH fetches between transfers.
  10803. + *
  10804. + * With fast usb storage devices and NForce2, "park" seems to
  10805. + * make problems: throughput reduction (!), data errors...
  10806. + */
  10807. + if (park) {
  10808. + park = min_t(unsigned, park, 3);
  10809. + temp |= CMD_PARK;
  10810. + temp |= park << 8;
  10811. + }
  10812. + fotg210_dbg(fotg210, "park %d\n", park);
  10813. + }
  10814. + if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  10815. + /* periodic schedule size can be smaller than default */
  10816. + temp &= ~(3 << 2);
  10817. + temp |= (FOTG210_TUNE_FLS << 2);
  10818. + }
  10819. + fotg210->command = temp;
  10820. +
  10821. + /* Accept arbitrarily long scatter-gather lists */
  10822. + if (!hcd->localmem_pool)
  10823. + hcd->self.sg_tablesize = ~0;
  10824. + return 0;
  10825. +}
  10826. +
  10827. +/* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
  10828. +static int fotg210_run(struct usb_hcd *hcd)
  10829. +{
  10830. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  10831. + u32 temp;
  10832. +
  10833. + hcd->uses_new_polling = 1;
  10834. +
  10835. + /* EHCI spec section 4.1 */
  10836. +
  10837. + fotg210_writel(fotg210, fotg210->periodic_dma,
  10838. + &fotg210->regs->frame_list);
  10839. + fotg210_writel(fotg210, (u32)fotg210->async->qh_dma,
  10840. + &fotg210->regs->async_next);
  10841. +
  10842. + /*
  10843. + * hcc_params controls whether fotg210->regs->segment must (!!!)
  10844. + * be used; it constrains QH/ITD/SITD and QTD locations.
  10845. + * dma_pool consistent memory always uses segment zero.
  10846. + * streaming mappings for I/O buffers, like dma_map_single(),
  10847. + * can return segments above 4GB, if the device allows.
  10848. + *
  10849. + * NOTE: the dma mask is visible through dev->dma_mask, so
  10850. + * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  10851. + * Scsi_Host.highmem_io, and so forth. It's readonly to all
  10852. + * host side drivers though.
  10853. + */
  10854. + fotg210_readl(fotg210, &fotg210->caps->hcc_params);
  10855. +
  10856. + /*
  10857. + * Philips, Intel, and maybe others need CMD_RUN before the
  10858. + * root hub will detect new devices (why?); NEC doesn't
  10859. + */
  10860. + fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  10861. + fotg210->command |= CMD_RUN;
  10862. + fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
  10863. + dbg_cmd(fotg210, "init", fotg210->command);
  10864. +
  10865. + /*
  10866. + * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  10867. + * are explicitly handed to companion controller(s), so no TT is
  10868. + * involved with the root hub. (Except where one is integrated,
  10869. + * and there's no companion controller unless maybe for USB OTG.)
  10870. + *
  10871. + * Turning on the CF flag will transfer ownership of all ports
  10872. + * from the companions to the EHCI controller. If any of the
  10873. + * companions are in the middle of a port reset at the time, it
  10874. + * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  10875. + * guarantees that no resets are in progress. After we set CF,
  10876. + * a short delay lets the hardware catch up; new resets shouldn't
  10877. + * be started before the port switching actions could complete.
  10878. + */
  10879. + down_write(&ehci_cf_port_reset_rwsem);
  10880. + fotg210->rh_state = FOTG210_RH_RUNNING;
  10881. + /* unblock posted writes */
  10882. + fotg210_readl(fotg210, &fotg210->regs->command);
  10883. + usleep_range(5000, 10000);
  10884. + up_write(&ehci_cf_port_reset_rwsem);
  10885. + fotg210->last_periodic_enable = ktime_get_real();
  10886. +
  10887. + temp = HC_VERSION(fotg210,
  10888. + fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
  10889. + fotg210_info(fotg210,
  10890. + "USB %x.%x started, EHCI %x.%02x\n",
  10891. + ((fotg210->sbrn & 0xf0) >> 4), (fotg210->sbrn & 0x0f),
  10892. + temp >> 8, temp & 0xff);
  10893. +
  10894. + fotg210_writel(fotg210, INTR_MASK,
  10895. + &fotg210->regs->intr_enable); /* Turn On Interrupts */
  10896. +
  10897. + /* GRR this is run-once init(), being done every time the HC starts.
  10898. + * So long as they're part of class devices, we can't do it init()
  10899. + * since the class device isn't created that early.
  10900. + */
  10901. + create_debug_files(fotg210);
  10902. + create_sysfs_files(fotg210);
  10903. +
  10904. + return 0;
  10905. +}
  10906. +
  10907. +static int fotg210_setup(struct usb_hcd *hcd)
  10908. +{
  10909. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  10910. + int retval;
  10911. +
  10912. + fotg210->regs = (void __iomem *)fotg210->caps +
  10913. + HC_LENGTH(fotg210,
  10914. + fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
  10915. + dbg_hcs_params(fotg210, "reset");
  10916. + dbg_hcc_params(fotg210, "reset");
  10917. +
  10918. + /* cache this readonly data; minimize chip reads */
  10919. + fotg210->hcs_params = fotg210_readl(fotg210,
  10920. + &fotg210->caps->hcs_params);
  10921. +
  10922. + fotg210->sbrn = HCD_USB2;
  10923. +
  10924. + /* data structure init */
  10925. + retval = hcd_fotg210_init(hcd);
  10926. + if (retval)
  10927. + return retval;
  10928. +
  10929. + retval = fotg210_halt(fotg210);
  10930. + if (retval)
  10931. + return retval;
  10932. +
  10933. + fotg210_reset(fotg210);
  10934. +
  10935. + return 0;
  10936. +}
  10937. +
  10938. +static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
  10939. +{
  10940. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  10941. + u32 status, masked_status, pcd_status = 0, cmd;
  10942. + int bh;
  10943. +
  10944. + spin_lock(&fotg210->lock);
  10945. +
  10946. + status = fotg210_readl(fotg210, &fotg210->regs->status);
  10947. +
  10948. + /* e.g. cardbus physical eject */
  10949. + if (status == ~(u32) 0) {
  10950. + fotg210_dbg(fotg210, "device removed\n");
  10951. + goto dead;
  10952. + }
  10953. +
  10954. + /*
  10955. + * We don't use STS_FLR, but some controllers don't like it to
  10956. + * remain on, so mask it out along with the other status bits.
  10957. + */
  10958. + masked_status = status & (INTR_MASK | STS_FLR);
  10959. +
  10960. + /* Shared IRQ? */
  10961. + if (!masked_status ||
  10962. + unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
  10963. + spin_unlock(&fotg210->lock);
  10964. + return IRQ_NONE;
  10965. + }
  10966. +
  10967. + /* clear (just) interrupts */
  10968. + fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
  10969. + cmd = fotg210_readl(fotg210, &fotg210->regs->command);
  10970. + bh = 0;
  10971. +
  10972. + /* unrequested/ignored: Frame List Rollover */
  10973. + dbg_status(fotg210, "irq", status);
  10974. +
  10975. + /* INT, ERR, and IAA interrupt rates can be throttled */
  10976. +
  10977. + /* normal [4.15.1.2] or error [4.15.1.1] completion */
  10978. + if (likely((status & (STS_INT|STS_ERR)) != 0)) {
  10979. + if (likely((status & STS_ERR) == 0))
  10980. + INCR(fotg210->stats.normal);
  10981. + else
  10982. + INCR(fotg210->stats.error);
  10983. + bh = 1;
  10984. + }
  10985. +
  10986. + /* complete the unlinking of some qh [4.15.2.3] */
  10987. + if (status & STS_IAA) {
  10988. +
  10989. + /* Turn off the IAA watchdog */
  10990. + fotg210->enabled_hrtimer_events &=
  10991. + ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
  10992. +
  10993. + /*
  10994. + * Mild optimization: Allow another IAAD to reset the
  10995. + * hrtimer, if one occurs before the next expiration.
  10996. + * In theory we could always cancel the hrtimer, but
  10997. + * tests show that about half the time it will be reset
  10998. + * for some other event anyway.
  10999. + */
  11000. + if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
  11001. + ++fotg210->next_hrtimer_event;
  11002. +
  11003. + /* guard against (alleged) silicon errata */
  11004. + if (cmd & CMD_IAAD)
  11005. + fotg210_dbg(fotg210, "IAA with IAAD still set?\n");
  11006. + if (fotg210->async_iaa) {
  11007. + INCR(fotg210->stats.iaa);
  11008. + end_unlink_async(fotg210);
  11009. + } else
  11010. + fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
  11011. + }
  11012. +
  11013. + /* remote wakeup [4.3.1] */
  11014. + if (status & STS_PCD) {
  11015. + int pstatus;
  11016. + u32 __iomem *status_reg = &fotg210->regs->port_status;
  11017. +
  11018. + /* kick root hub later */
  11019. + pcd_status = status;
  11020. +
  11021. + /* resume root hub? */
  11022. + if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
  11023. + usb_hcd_resume_root_hub(hcd);
  11024. +
  11025. + pstatus = fotg210_readl(fotg210, status_reg);
  11026. +
  11027. + if (test_bit(0, &fotg210->suspended_ports) &&
  11028. + ((pstatus & PORT_RESUME) ||
  11029. + !(pstatus & PORT_SUSPEND)) &&
  11030. + (pstatus & PORT_PE) &&
  11031. + fotg210->reset_done[0] == 0) {
  11032. +
  11033. + /* start 20 msec resume signaling from this port,
  11034. + * and make hub_wq collect PORT_STAT_C_SUSPEND to
  11035. + * stop that signaling. Use 5 ms extra for safety,
  11036. + * like usb_port_resume() does.
  11037. + */
  11038. + fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25);
  11039. + set_bit(0, &fotg210->resuming_ports);
  11040. + fotg210_dbg(fotg210, "port 1 remote wakeup\n");
  11041. + mod_timer(&hcd->rh_timer, fotg210->reset_done[0]);
  11042. + }
  11043. + }
  11044. +
  11045. + /* PCI errors [4.15.2.4] */
  11046. + if (unlikely((status & STS_FATAL) != 0)) {
  11047. + fotg210_err(fotg210, "fatal error\n");
  11048. + dbg_cmd(fotg210, "fatal", cmd);
  11049. + dbg_status(fotg210, "fatal", status);
  11050. +dead:
  11051. + usb_hc_died(hcd);
  11052. +
  11053. + /* Don't let the controller do anything more */
  11054. + fotg210->shutdown = true;
  11055. + fotg210->rh_state = FOTG210_RH_STOPPING;
  11056. + fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
  11057. + fotg210_writel(fotg210, fotg210->command,
  11058. + &fotg210->regs->command);
  11059. + fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
  11060. + fotg210_handle_controller_death(fotg210);
  11061. +
  11062. + /* Handle completions when the controller stops */
  11063. + bh = 0;
  11064. + }
  11065. +
  11066. + if (bh)
  11067. + fotg210_work(fotg210);
  11068. + spin_unlock(&fotg210->lock);
  11069. + if (pcd_status)
  11070. + usb_hcd_poll_rh_status(hcd);
  11071. + return IRQ_HANDLED;
  11072. +}
  11073. +
  11074. +/* non-error returns are a promise to giveback() the urb later
  11075. + * we drop ownership so next owner (or urb unlink) can get it
  11076. + *
  11077. + * urb + dev is in hcd.self.controller.urb_list
  11078. + * we're queueing TDs onto software and hardware lists
  11079. + *
  11080. + * hcd-specific init for hcpriv hasn't been done yet
  11081. + *
  11082. + * NOTE: control, bulk, and interrupt share the same code to append TDs
  11083. + * to a (possibly active) QH, and the same QH scanning code.
  11084. + */
  11085. +static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  11086. + gfp_t mem_flags)
  11087. +{
  11088. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  11089. + struct list_head qtd_list;
  11090. +
  11091. + INIT_LIST_HEAD(&qtd_list);
  11092. +
  11093. + switch (usb_pipetype(urb->pipe)) {
  11094. + case PIPE_CONTROL:
  11095. + /* qh_completions() code doesn't handle all the fault cases
  11096. + * in multi-TD control transfers. Even 1KB is rare anyway.
  11097. + */
  11098. + if (urb->transfer_buffer_length > (16 * 1024))
  11099. + return -EMSGSIZE;
  11100. + fallthrough;
  11101. + /* case PIPE_BULK: */
  11102. + default:
  11103. + if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
  11104. + return -ENOMEM;
  11105. + return submit_async(fotg210, urb, &qtd_list, mem_flags);
  11106. +
  11107. + case PIPE_INTERRUPT:
  11108. + if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
  11109. + return -ENOMEM;
  11110. + return intr_submit(fotg210, urb, &qtd_list, mem_flags);
  11111. +
  11112. + case PIPE_ISOCHRONOUS:
  11113. + return itd_submit(fotg210, urb, mem_flags);
  11114. + }
  11115. +}
  11116. +
  11117. +/* remove from hardware lists
  11118. + * completions normally happen asynchronously
  11119. + */
  11120. +
  11121. +static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  11122. +{
  11123. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  11124. + struct fotg210_qh *qh;
  11125. + unsigned long flags;
  11126. + int rc;
  11127. +
  11128. + spin_lock_irqsave(&fotg210->lock, flags);
  11129. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  11130. + if (rc)
  11131. + goto done;
  11132. +
  11133. + switch (usb_pipetype(urb->pipe)) {
  11134. + /* case PIPE_CONTROL: */
  11135. + /* case PIPE_BULK:*/
  11136. + default:
  11137. + qh = (struct fotg210_qh *) urb->hcpriv;
  11138. + if (!qh)
  11139. + break;
  11140. + switch (qh->qh_state) {
  11141. + case QH_STATE_LINKED:
  11142. + case QH_STATE_COMPLETING:
  11143. + start_unlink_async(fotg210, qh);
  11144. + break;
  11145. + case QH_STATE_UNLINK:
  11146. + case QH_STATE_UNLINK_WAIT:
  11147. + /* already started */
  11148. + break;
  11149. + case QH_STATE_IDLE:
  11150. + /* QH might be waiting for a Clear-TT-Buffer */
  11151. + qh_completions(fotg210, qh);
  11152. + break;
  11153. + }
  11154. + break;
  11155. +
  11156. + case PIPE_INTERRUPT:
  11157. + qh = (struct fotg210_qh *) urb->hcpriv;
  11158. + if (!qh)
  11159. + break;
  11160. + switch (qh->qh_state) {
  11161. + case QH_STATE_LINKED:
  11162. + case QH_STATE_COMPLETING:
  11163. + start_unlink_intr(fotg210, qh);
  11164. + break;
  11165. + case QH_STATE_IDLE:
  11166. + qh_completions(fotg210, qh);
  11167. + break;
  11168. + default:
  11169. + fotg210_dbg(fotg210, "bogus qh %p state %d\n",
  11170. + qh, qh->qh_state);
  11171. + goto done;
  11172. + }
  11173. + break;
  11174. +
  11175. + case PIPE_ISOCHRONOUS:
  11176. + /* itd... */
  11177. +
  11178. + /* wait till next completion, do it then. */
  11179. + /* completion irqs can wait up to 1024 msec, */
  11180. + break;
  11181. + }
  11182. +done:
  11183. + spin_unlock_irqrestore(&fotg210->lock, flags);
  11184. + return rc;
  11185. +}
  11186. +
  11187. +/* bulk qh holds the data toggle */
  11188. +
  11189. +static void fotg210_endpoint_disable(struct usb_hcd *hcd,
  11190. + struct usb_host_endpoint *ep)
  11191. +{
  11192. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  11193. + unsigned long flags;
  11194. + struct fotg210_qh *qh, *tmp;
  11195. +
  11196. + /* ASSERT: any requests/urbs are being unlinked */
  11197. + /* ASSERT: nobody can be submitting urbs for this any more */
  11198. +
  11199. +rescan:
  11200. + spin_lock_irqsave(&fotg210->lock, flags);
  11201. + qh = ep->hcpriv;
  11202. + if (!qh)
  11203. + goto done;
  11204. +
  11205. + /* endpoints can be iso streams. for now, we don't
  11206. + * accelerate iso completions ... so spin a while.
  11207. + */
  11208. + if (qh->hw == NULL) {
  11209. + struct fotg210_iso_stream *stream = ep->hcpriv;
  11210. +
  11211. + if (!list_empty(&stream->td_list))
  11212. + goto idle_timeout;
  11213. +
  11214. + /* BUG_ON(!list_empty(&stream->free_list)); */
  11215. + kfree(stream);
  11216. + goto done;
  11217. + }
  11218. +
  11219. + if (fotg210->rh_state < FOTG210_RH_RUNNING)
  11220. + qh->qh_state = QH_STATE_IDLE;
  11221. + switch (qh->qh_state) {
  11222. + case QH_STATE_LINKED:
  11223. + case QH_STATE_COMPLETING:
  11224. + for (tmp = fotg210->async->qh_next.qh;
  11225. + tmp && tmp != qh;
  11226. + tmp = tmp->qh_next.qh)
  11227. + continue;
  11228. + /* periodic qh self-unlinks on empty, and a COMPLETING qh
  11229. + * may already be unlinked.
  11230. + */
  11231. + if (tmp)
  11232. + start_unlink_async(fotg210, qh);
  11233. + fallthrough;
  11234. + case QH_STATE_UNLINK: /* wait for hw to finish? */
  11235. + case QH_STATE_UNLINK_WAIT:
  11236. +idle_timeout:
  11237. + spin_unlock_irqrestore(&fotg210->lock, flags);
  11238. + schedule_timeout_uninterruptible(1);
  11239. + goto rescan;
  11240. + case QH_STATE_IDLE: /* fully unlinked */
  11241. + if (qh->clearing_tt)
  11242. + goto idle_timeout;
  11243. + if (list_empty(&qh->qtd_list)) {
  11244. + qh_destroy(fotg210, qh);
  11245. + break;
  11246. + }
  11247. + fallthrough;
  11248. + default:
  11249. + /* caller was supposed to have unlinked any requests;
  11250. + * that's not our job. just leak this memory.
  11251. + */
  11252. + fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n",
  11253. + qh, ep->desc.bEndpointAddress, qh->qh_state,
  11254. + list_empty(&qh->qtd_list) ? "" : "(has tds)");
  11255. + break;
  11256. + }
  11257. +done:
  11258. + ep->hcpriv = NULL;
  11259. + spin_unlock_irqrestore(&fotg210->lock, flags);
  11260. +}
  11261. +
  11262. +static void fotg210_endpoint_reset(struct usb_hcd *hcd,
  11263. + struct usb_host_endpoint *ep)
  11264. +{
  11265. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  11266. + struct fotg210_qh *qh;
  11267. + int eptype = usb_endpoint_type(&ep->desc);
  11268. + int epnum = usb_endpoint_num(&ep->desc);
  11269. + int is_out = usb_endpoint_dir_out(&ep->desc);
  11270. + unsigned long flags;
  11271. +
  11272. + if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
  11273. + return;
  11274. +
  11275. + spin_lock_irqsave(&fotg210->lock, flags);
  11276. + qh = ep->hcpriv;
  11277. +
  11278. + /* For Bulk and Interrupt endpoints we maintain the toggle state
  11279. + * in the hardware; the toggle bits in udev aren't used at all.
  11280. + * When an endpoint is reset by usb_clear_halt() we must reset
  11281. + * the toggle bit in the QH.
  11282. + */
  11283. + if (qh) {
  11284. + usb_settoggle(qh->dev, epnum, is_out, 0);
  11285. + if (!list_empty(&qh->qtd_list)) {
  11286. + WARN_ONCE(1, "clear_halt for a busy endpoint\n");
  11287. + } else if (qh->qh_state == QH_STATE_LINKED ||
  11288. + qh->qh_state == QH_STATE_COMPLETING) {
  11289. +
  11290. + /* The toggle value in the QH can't be updated
  11291. + * while the QH is active. Unlink it now;
  11292. + * re-linking will call qh_refresh().
  11293. + */
  11294. + if (eptype == USB_ENDPOINT_XFER_BULK)
  11295. + start_unlink_async(fotg210, qh);
  11296. + else
  11297. + start_unlink_intr(fotg210, qh);
  11298. + }
  11299. + }
  11300. + spin_unlock_irqrestore(&fotg210->lock, flags);
  11301. +}
  11302. +
  11303. +static int fotg210_get_frame(struct usb_hcd *hcd)
  11304. +{
  11305. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  11306. +
  11307. + return (fotg210_read_frame_index(fotg210) >> 3) %
  11308. + fotg210->periodic_size;
  11309. +}
  11310. +
  11311. +/* The EHCI in ChipIdea HDRC cannot be a separate module or device,
  11312. + * because its registers (and irq) are shared between host/gadget/otg
  11313. + * functions and in order to facilitate role switching we cannot
  11314. + * give the fotg210 driver exclusive access to those.
  11315. + */
  11316. +MODULE_DESCRIPTION(DRIVER_DESC);
  11317. +MODULE_AUTHOR(DRIVER_AUTHOR);
  11318. +MODULE_LICENSE("GPL");
  11319. +
  11320. +static const struct hc_driver fotg210_fotg210_hc_driver = {
  11321. + .description = hcd_name,
  11322. + .product_desc = "Faraday USB2.0 Host Controller",
  11323. + .hcd_priv_size = sizeof(struct fotg210_hcd),
  11324. +
  11325. + /*
  11326. + * generic hardware linkage
  11327. + */
  11328. + .irq = fotg210_irq,
  11329. + .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
  11330. +
  11331. + /*
  11332. + * basic lifecycle operations
  11333. + */
  11334. + .reset = hcd_fotg210_init,
  11335. + .start = fotg210_run,
  11336. + .stop = fotg210_stop,
  11337. + .shutdown = fotg210_shutdown,
  11338. +
  11339. + /*
  11340. + * managing i/o requests and associated device resources
  11341. + */
  11342. + .urb_enqueue = fotg210_urb_enqueue,
  11343. + .urb_dequeue = fotg210_urb_dequeue,
  11344. + .endpoint_disable = fotg210_endpoint_disable,
  11345. + .endpoint_reset = fotg210_endpoint_reset,
  11346. +
  11347. + /*
  11348. + * scheduling support
  11349. + */
  11350. + .get_frame_number = fotg210_get_frame,
  11351. +
  11352. + /*
  11353. + * root hub support
  11354. + */
  11355. + .hub_status_data = fotg210_hub_status_data,
  11356. + .hub_control = fotg210_hub_control,
  11357. + .bus_suspend = fotg210_bus_suspend,
  11358. + .bus_resume = fotg210_bus_resume,
  11359. +
  11360. + .relinquish_port = fotg210_relinquish_port,
  11361. + .port_handed_over = fotg210_port_handed_over,
  11362. +
  11363. + .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
  11364. +};
  11365. +
  11366. +static void fotg210_init(struct fotg210_hcd *fotg210)
  11367. +{
  11368. + u32 value;
  11369. +
  11370. + iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
  11371. + &fotg210->regs->gmir);
  11372. +
  11373. + value = ioread32(&fotg210->regs->otgcsr);
  11374. + value &= ~OTGCSR_A_BUS_DROP;
  11375. + value |= OTGCSR_A_BUS_REQ;
  11376. + iowrite32(value, &fotg210->regs->otgcsr);
  11377. +}
  11378. +
  11379. +/*
  11380. + * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
  11381. + *
  11382. + * Allocates basic resources for this USB host controller, and
  11383. + * then invokes the start() method for the HCD associated with it
  11384. + * through the hotplug entry's driver_data.
  11385. + */
  11386. +static int fotg210_hcd_probe(struct platform_device *pdev)
  11387. +{
  11388. + struct device *dev = &pdev->dev;
  11389. + struct usb_hcd *hcd;
  11390. + struct resource *res;
  11391. + int irq;
  11392. + int retval;
  11393. + struct fotg210_hcd *fotg210;
  11394. +
  11395. + if (usb_disabled())
  11396. + return -ENODEV;
  11397. +
  11398. + pdev->dev.power.power_state = PMSG_ON;
  11399. +
  11400. + irq = platform_get_irq(pdev, 0);
  11401. + if (irq < 0)
  11402. + return irq;
  11403. +
  11404. + hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
  11405. + dev_name(dev));
  11406. + if (!hcd) {
  11407. + dev_err(dev, "failed to create hcd\n");
  11408. + retval = -ENOMEM;
  11409. + goto fail_create_hcd;
  11410. + }
  11411. +
  11412. + hcd->has_tt = 1;
  11413. +
  11414. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  11415. + hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  11416. + if (IS_ERR(hcd->regs)) {
  11417. + retval = PTR_ERR(hcd->regs);
  11418. + goto failed_put_hcd;
  11419. + }
  11420. +
  11421. + hcd->rsrc_start = res->start;
  11422. + hcd->rsrc_len = resource_size(res);
  11423. +
  11424. + fotg210 = hcd_to_fotg210(hcd);
  11425. +
  11426. + fotg210->caps = hcd->regs;
  11427. +
  11428. + /* It's OK not to supply this clock */
  11429. + fotg210->pclk = clk_get(dev, "PCLK");
  11430. + if (!IS_ERR(fotg210->pclk)) {
  11431. + retval = clk_prepare_enable(fotg210->pclk);
  11432. + if (retval) {
  11433. + dev_err(dev, "failed to enable PCLK\n");
  11434. + goto failed_put_hcd;
  11435. + }
  11436. + } else if (PTR_ERR(fotg210->pclk) == -EPROBE_DEFER) {
  11437. + /*
  11438. + * Percolate deferrals, for anything else,
  11439. + * just live without the clocking.
  11440. + */
  11441. + retval = PTR_ERR(fotg210->pclk);
  11442. + goto failed_dis_clk;
  11443. + }
  11444. +
  11445. + retval = fotg210_setup(hcd);
  11446. + if (retval)
  11447. + goto failed_dis_clk;
  11448. +
  11449. + fotg210_init(fotg210);
  11450. +
  11451. + retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  11452. + if (retval) {
  11453. + dev_err(dev, "failed to add hcd with err %d\n", retval);
  11454. + goto failed_dis_clk;
  11455. + }
  11456. + device_wakeup_enable(hcd->self.controller);
  11457. + platform_set_drvdata(pdev, hcd);
  11458. +
  11459. + return retval;
  11460. +
  11461. +failed_dis_clk:
  11462. + if (!IS_ERR(fotg210->pclk)) {
  11463. + clk_disable_unprepare(fotg210->pclk);
  11464. + clk_put(fotg210->pclk);
  11465. + }
  11466. +failed_put_hcd:
  11467. + usb_put_hcd(hcd);
  11468. +fail_create_hcd:
  11469. + dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
  11470. + return retval;
  11471. +}
  11472. +
  11473. +/*
  11474. + * fotg210_hcd_remove - shutdown processing for EHCI HCDs
  11475. + * @dev: USB Host Controller being removed
  11476. + *
  11477. + */
  11478. +static int fotg210_hcd_remove(struct platform_device *pdev)
  11479. +{
  11480. + struct usb_hcd *hcd = platform_get_drvdata(pdev);
  11481. + struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
  11482. +
  11483. + if (!IS_ERR(fotg210->pclk)) {
  11484. + clk_disable_unprepare(fotg210->pclk);
  11485. + clk_put(fotg210->pclk);
  11486. + }
  11487. +
  11488. + usb_remove_hcd(hcd);
  11489. + usb_put_hcd(hcd);
  11490. +
  11491. + return 0;
  11492. +}
  11493. +
  11494. +#ifdef CONFIG_OF
  11495. +static const struct of_device_id fotg210_of_match[] = {
  11496. + { .compatible = "faraday,fotg210" },
  11497. + {},
  11498. +};
  11499. +MODULE_DEVICE_TABLE(of, fotg210_of_match);
  11500. +#endif
  11501. +
  11502. +static struct platform_driver fotg210_hcd_driver = {
  11503. + .driver = {
  11504. + .name = "fotg210-hcd",
  11505. + .of_match_table = of_match_ptr(fotg210_of_match),
  11506. + },
  11507. + .probe = fotg210_hcd_probe,
  11508. + .remove = fotg210_hcd_remove,
  11509. +};
  11510. +
  11511. +static int __init fotg210_hcd_init(void)
  11512. +{
  11513. + int retval = 0;
  11514. +
  11515. + if (usb_disabled())
  11516. + return -ENODEV;
  11517. +
  11518. + set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  11519. + if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
  11520. + test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
  11521. + pr_warn("Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
  11522. +
  11523. + pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd\n",
  11524. + hcd_name, sizeof(struct fotg210_qh),
  11525. + sizeof(struct fotg210_qtd),
  11526. + sizeof(struct fotg210_itd));
  11527. +
  11528. + fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
  11529. +
  11530. + retval = platform_driver_register(&fotg210_hcd_driver);
  11531. + if (retval < 0)
  11532. + goto clean;
  11533. + return retval;
  11534. +
  11535. +clean:
  11536. + debugfs_remove(fotg210_debug_root);
  11537. + fotg210_debug_root = NULL;
  11538. +
  11539. + clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  11540. + return retval;
  11541. +}
  11542. +module_init(fotg210_hcd_init);
  11543. +
  11544. +static void __exit fotg210_hcd_cleanup(void)
  11545. +{
  11546. + platform_driver_unregister(&fotg210_hcd_driver);
  11547. + debugfs_remove(fotg210_debug_root);
  11548. + clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  11549. +}
  11550. +module_exit(fotg210_hcd_cleanup);
  11551. --- a/drivers/usb/gadget/udc/fotg210-udc.c
  11552. +++ /dev/null
  11553. @@ -1,1239 +0,0 @@
  11554. -// SPDX-License-Identifier: GPL-2.0
  11555. -/*
  11556. - * FOTG210 UDC Driver supports Bulk transfer so far
  11557. - *
  11558. - * Copyright (C) 2013 Faraday Technology Corporation
  11559. - *
  11560. - * Author : Yuan-Hsin Chen <[email protected]>
  11561. - */
  11562. -
  11563. -#include <linux/dma-mapping.h>
  11564. -#include <linux/err.h>
  11565. -#include <linux/interrupt.h>
  11566. -#include <linux/io.h>
  11567. -#include <linux/module.h>
  11568. -#include <linux/platform_device.h>
  11569. -#include <linux/usb/ch9.h>
  11570. -#include <linux/usb/gadget.h>
  11571. -
  11572. -#include "fotg210.h"
  11573. -
  11574. -#define DRIVER_DESC "FOTG210 USB Device Controller Driver"
  11575. -#define DRIVER_VERSION "30-April-2013"
  11576. -
  11577. -static const char udc_name[] = "fotg210_udc";
  11578. -static const char * const fotg210_ep_name[] = {
  11579. - "ep0", "ep1", "ep2", "ep3", "ep4"};
  11580. -
  11581. -static void fotg210_disable_fifo_int(struct fotg210_ep *ep)
  11582. -{
  11583. - u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
  11584. -
  11585. - if (ep->dir_in)
  11586. - value |= DMISGR1_MF_IN_INT(ep->epnum - 1);
  11587. - else
  11588. - value |= DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
  11589. - iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
  11590. -}
  11591. -
  11592. -static void fotg210_enable_fifo_int(struct fotg210_ep *ep)
  11593. -{
  11594. - u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
  11595. -
  11596. - if (ep->dir_in)
  11597. - value &= ~DMISGR1_MF_IN_INT(ep->epnum - 1);
  11598. - else
  11599. - value &= ~DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
  11600. - iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
  11601. -}
  11602. -
  11603. -static void fotg210_set_cxdone(struct fotg210_udc *fotg210)
  11604. -{
  11605. - u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
  11606. -
  11607. - value |= DCFESR_CX_DONE;
  11608. - iowrite32(value, fotg210->reg + FOTG210_DCFESR);
  11609. -}
  11610. -
  11611. -static void fotg210_done(struct fotg210_ep *ep, struct fotg210_request *req,
  11612. - int status)
  11613. -{
  11614. - list_del_init(&req->queue);
  11615. -
  11616. - /* don't modify queue heads during completion callback */
  11617. - if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
  11618. - req->req.status = -ESHUTDOWN;
  11619. - else
  11620. - req->req.status = status;
  11621. -
  11622. - spin_unlock(&ep->fotg210->lock);
  11623. - usb_gadget_giveback_request(&ep->ep, &req->req);
  11624. - spin_lock(&ep->fotg210->lock);
  11625. -
  11626. - if (ep->epnum) {
  11627. - if (list_empty(&ep->queue))
  11628. - fotg210_disable_fifo_int(ep);
  11629. - } else {
  11630. - fotg210_set_cxdone(ep->fotg210);
  11631. - }
  11632. -}
  11633. -
  11634. -static void fotg210_fifo_ep_mapping(struct fotg210_ep *ep, u32 epnum,
  11635. - u32 dir_in)
  11636. -{
  11637. - struct fotg210_udc *fotg210 = ep->fotg210;
  11638. - u32 val;
  11639. -
  11640. - /* Driver should map an ep to a fifo and then map the fifo
  11641. - * to the ep. What a brain-damaged design!
  11642. - */
  11643. -
  11644. - /* map a fifo to an ep */
  11645. - val = ioread32(fotg210->reg + FOTG210_EPMAP);
  11646. - val &= ~EPMAP_FIFONOMSK(epnum, dir_in);
  11647. - val |= EPMAP_FIFONO(epnum, dir_in);
  11648. - iowrite32(val, fotg210->reg + FOTG210_EPMAP);
  11649. -
  11650. - /* map the ep to the fifo */
  11651. - val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
  11652. - val &= ~FIFOMAP_EPNOMSK(epnum);
  11653. - val |= FIFOMAP_EPNO(epnum);
  11654. - iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
  11655. -
  11656. - /* enable fifo */
  11657. - val = ioread32(fotg210->reg + FOTG210_FIFOCF);
  11658. - val |= FIFOCF_FIFO_EN(epnum - 1);
  11659. - iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
  11660. -}
  11661. -
  11662. -static void fotg210_set_fifo_dir(struct fotg210_ep *ep, u32 epnum, u32 dir_in)
  11663. -{
  11664. - struct fotg210_udc *fotg210 = ep->fotg210;
  11665. - u32 val;
  11666. -
  11667. - val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
  11668. - val |= (dir_in ? FIFOMAP_DIRIN(epnum - 1) : FIFOMAP_DIROUT(epnum - 1));
  11669. - iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
  11670. -}
  11671. -
  11672. -static void fotg210_set_tfrtype(struct fotg210_ep *ep, u32 epnum, u32 type)
  11673. -{
  11674. - struct fotg210_udc *fotg210 = ep->fotg210;
  11675. - u32 val;
  11676. -
  11677. - val = ioread32(fotg210->reg + FOTG210_FIFOCF);
  11678. - val |= FIFOCF_TYPE(type, epnum - 1);
  11679. - iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
  11680. -}
  11681. -
  11682. -static void fotg210_set_mps(struct fotg210_ep *ep, u32 epnum, u32 mps,
  11683. - u32 dir_in)
  11684. -{
  11685. - struct fotg210_udc *fotg210 = ep->fotg210;
  11686. - u32 val;
  11687. - u32 offset = dir_in ? FOTG210_INEPMPSR(epnum) :
  11688. - FOTG210_OUTEPMPSR(epnum);
  11689. -
  11690. - val = ioread32(fotg210->reg + offset);
  11691. - val |= INOUTEPMPSR_MPS(mps);
  11692. - iowrite32(val, fotg210->reg + offset);
  11693. -}
  11694. -
  11695. -static int fotg210_config_ep(struct fotg210_ep *ep,
  11696. - const struct usb_endpoint_descriptor *desc)
  11697. -{
  11698. - struct fotg210_udc *fotg210 = ep->fotg210;
  11699. -
  11700. - fotg210_set_fifo_dir(ep, ep->epnum, ep->dir_in);
  11701. - fotg210_set_tfrtype(ep, ep->epnum, ep->type);
  11702. - fotg210_set_mps(ep, ep->epnum, ep->ep.maxpacket, ep->dir_in);
  11703. - fotg210_fifo_ep_mapping(ep, ep->epnum, ep->dir_in);
  11704. -
  11705. - fotg210->ep[ep->epnum] = ep;
  11706. -
  11707. - return 0;
  11708. -}
  11709. -
  11710. -static int fotg210_ep_enable(struct usb_ep *_ep,
  11711. - const struct usb_endpoint_descriptor *desc)
  11712. -{
  11713. - struct fotg210_ep *ep;
  11714. -
  11715. - ep = container_of(_ep, struct fotg210_ep, ep);
  11716. -
  11717. - ep->desc = desc;
  11718. - ep->epnum = usb_endpoint_num(desc);
  11719. - ep->type = usb_endpoint_type(desc);
  11720. - ep->dir_in = usb_endpoint_dir_in(desc);
  11721. - ep->ep.maxpacket = usb_endpoint_maxp(desc);
  11722. -
  11723. - return fotg210_config_ep(ep, desc);
  11724. -}
  11725. -
  11726. -static void fotg210_reset_tseq(struct fotg210_udc *fotg210, u8 epnum)
  11727. -{
  11728. - struct fotg210_ep *ep = fotg210->ep[epnum];
  11729. - u32 value;
  11730. - void __iomem *reg;
  11731. -
  11732. - reg = (ep->dir_in) ?
  11733. - fotg210->reg + FOTG210_INEPMPSR(epnum) :
  11734. - fotg210->reg + FOTG210_OUTEPMPSR(epnum);
  11735. -
  11736. - /* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ
  11737. - * bit. Controller wouldn't clear this bit. WTF!!!
  11738. - */
  11739. -
  11740. - value = ioread32(reg);
  11741. - value |= INOUTEPMPSR_RESET_TSEQ;
  11742. - iowrite32(value, reg);
  11743. -
  11744. - value = ioread32(reg);
  11745. - value &= ~INOUTEPMPSR_RESET_TSEQ;
  11746. - iowrite32(value, reg);
  11747. -}
  11748. -
  11749. -static int fotg210_ep_release(struct fotg210_ep *ep)
  11750. -{
  11751. - if (!ep->epnum)
  11752. - return 0;
  11753. - ep->epnum = 0;
  11754. - ep->stall = 0;
  11755. - ep->wedged = 0;
  11756. -
  11757. - fotg210_reset_tseq(ep->fotg210, ep->epnum);
  11758. -
  11759. - return 0;
  11760. -}
  11761. -
  11762. -static int fotg210_ep_disable(struct usb_ep *_ep)
  11763. -{
  11764. - struct fotg210_ep *ep;
  11765. - struct fotg210_request *req;
  11766. - unsigned long flags;
  11767. -
  11768. - BUG_ON(!_ep);
  11769. -
  11770. - ep = container_of(_ep, struct fotg210_ep, ep);
  11771. -
  11772. - while (!list_empty(&ep->queue)) {
  11773. - req = list_entry(ep->queue.next,
  11774. - struct fotg210_request, queue);
  11775. - spin_lock_irqsave(&ep->fotg210->lock, flags);
  11776. - fotg210_done(ep, req, -ECONNRESET);
  11777. - spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  11778. - }
  11779. -
  11780. - return fotg210_ep_release(ep);
  11781. -}
  11782. -
  11783. -static struct usb_request *fotg210_ep_alloc_request(struct usb_ep *_ep,
  11784. - gfp_t gfp_flags)
  11785. -{
  11786. - struct fotg210_request *req;
  11787. -
  11788. - req = kzalloc(sizeof(struct fotg210_request), gfp_flags);
  11789. - if (!req)
  11790. - return NULL;
  11791. -
  11792. - INIT_LIST_HEAD(&req->queue);
  11793. -
  11794. - return &req->req;
  11795. -}
  11796. -
  11797. -static void fotg210_ep_free_request(struct usb_ep *_ep,
  11798. - struct usb_request *_req)
  11799. -{
  11800. - struct fotg210_request *req;
  11801. -
  11802. - req = container_of(_req, struct fotg210_request, req);
  11803. - kfree(req);
  11804. -}
  11805. -
  11806. -static void fotg210_enable_dma(struct fotg210_ep *ep,
  11807. - dma_addr_t d, u32 len)
  11808. -{
  11809. - u32 value;
  11810. - struct fotg210_udc *fotg210 = ep->fotg210;
  11811. -
  11812. - /* set transfer length and direction */
  11813. - value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
  11814. - value &= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1));
  11815. - value |= DMACPSR1_DMA_LEN(len) | DMACPSR1_DMA_TYPE(ep->dir_in);
  11816. - iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
  11817. -
  11818. - /* set device DMA target FIFO number */
  11819. - value = ioread32(fotg210->reg + FOTG210_DMATFNR);
  11820. - if (ep->epnum)
  11821. - value |= DMATFNR_ACC_FN(ep->epnum - 1);
  11822. - else
  11823. - value |= DMATFNR_ACC_CXF;
  11824. - iowrite32(value, fotg210->reg + FOTG210_DMATFNR);
  11825. -
  11826. - /* set DMA memory address */
  11827. - iowrite32(d, fotg210->reg + FOTG210_DMACPSR2);
  11828. -
  11829. - /* enable MDMA_EROR and MDMA_CMPLT interrupt */
  11830. - value = ioread32(fotg210->reg + FOTG210_DMISGR2);
  11831. - value &= ~(DMISGR2_MDMA_CMPLT | DMISGR2_MDMA_ERROR);
  11832. - iowrite32(value, fotg210->reg + FOTG210_DMISGR2);
  11833. -
  11834. - /* start DMA */
  11835. - value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
  11836. - value |= DMACPSR1_DMA_START;
  11837. - iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
  11838. -}
  11839. -
  11840. -static void fotg210_disable_dma(struct fotg210_ep *ep)
  11841. -{
  11842. - iowrite32(DMATFNR_DISDMA, ep->fotg210->reg + FOTG210_DMATFNR);
  11843. -}
  11844. -
  11845. -static void fotg210_wait_dma_done(struct fotg210_ep *ep)
  11846. -{
  11847. - u32 value;
  11848. -
  11849. - do {
  11850. - value = ioread32(ep->fotg210->reg + FOTG210_DISGR2);
  11851. - if ((value & DISGR2_USBRST_INT) ||
  11852. - (value & DISGR2_DMA_ERROR))
  11853. - goto dma_reset;
  11854. - } while (!(value & DISGR2_DMA_CMPLT));
  11855. -
  11856. - value &= ~DISGR2_DMA_CMPLT;
  11857. - iowrite32(value, ep->fotg210->reg + FOTG210_DISGR2);
  11858. - return;
  11859. -
  11860. -dma_reset:
  11861. - value = ioread32(ep->fotg210->reg + FOTG210_DMACPSR1);
  11862. - value |= DMACPSR1_DMA_ABORT;
  11863. - iowrite32(value, ep->fotg210->reg + FOTG210_DMACPSR1);
  11864. -
  11865. - /* reset fifo */
  11866. - if (ep->epnum) {
  11867. - value = ioread32(ep->fotg210->reg +
  11868. - FOTG210_FIBCR(ep->epnum - 1));
  11869. - value |= FIBCR_FFRST;
  11870. - iowrite32(value, ep->fotg210->reg +
  11871. - FOTG210_FIBCR(ep->epnum - 1));
  11872. - } else {
  11873. - value = ioread32(ep->fotg210->reg + FOTG210_DCFESR);
  11874. - value |= DCFESR_CX_CLR;
  11875. - iowrite32(value, ep->fotg210->reg + FOTG210_DCFESR);
  11876. - }
  11877. -}
  11878. -
  11879. -static void fotg210_start_dma(struct fotg210_ep *ep,
  11880. - struct fotg210_request *req)
  11881. -{
  11882. - struct device *dev = &ep->fotg210->gadget.dev;
  11883. - dma_addr_t d;
  11884. - u8 *buffer;
  11885. - u32 length;
  11886. -
  11887. - if (ep->epnum) {
  11888. - if (ep->dir_in) {
  11889. - buffer = req->req.buf;
  11890. - length = req->req.length;
  11891. - } else {
  11892. - buffer = req->req.buf + req->req.actual;
  11893. - length = ioread32(ep->fotg210->reg +
  11894. - FOTG210_FIBCR(ep->epnum - 1)) & FIBCR_BCFX;
  11895. - if (length > req->req.length - req->req.actual)
  11896. - length = req->req.length - req->req.actual;
  11897. - }
  11898. - } else {
  11899. - buffer = req->req.buf + req->req.actual;
  11900. - if (req->req.length - req->req.actual > ep->ep.maxpacket)
  11901. - length = ep->ep.maxpacket;
  11902. - else
  11903. - length = req->req.length - req->req.actual;
  11904. - }
  11905. -
  11906. - d = dma_map_single(dev, buffer, length,
  11907. - ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  11908. -
  11909. - if (dma_mapping_error(dev, d)) {
  11910. - pr_err("dma_mapping_error\n");
  11911. - return;
  11912. - }
  11913. -
  11914. - fotg210_enable_dma(ep, d, length);
  11915. -
  11916. - /* check if dma is done */
  11917. - fotg210_wait_dma_done(ep);
  11918. -
  11919. - fotg210_disable_dma(ep);
  11920. -
  11921. - /* update actual transfer length */
  11922. - req->req.actual += length;
  11923. -
  11924. - dma_unmap_single(dev, d, length, DMA_TO_DEVICE);
  11925. -}
  11926. -
  11927. -static void fotg210_ep0_queue(struct fotg210_ep *ep,
  11928. - struct fotg210_request *req)
  11929. -{
  11930. - if (!req->req.length) {
  11931. - fotg210_done(ep, req, 0);
  11932. - return;
  11933. - }
  11934. - if (ep->dir_in) { /* if IN */
  11935. - fotg210_start_dma(ep, req);
  11936. - if (req->req.length == req->req.actual)
  11937. - fotg210_done(ep, req, 0);
  11938. - } else { /* OUT */
  11939. - u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR0);
  11940. -
  11941. - value &= ~DMISGR0_MCX_OUT_INT;
  11942. - iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR0);
  11943. - }
  11944. -}
  11945. -
  11946. -static int fotg210_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
  11947. - gfp_t gfp_flags)
  11948. -{
  11949. - struct fotg210_ep *ep;
  11950. - struct fotg210_request *req;
  11951. - unsigned long flags;
  11952. - int request = 0;
  11953. -
  11954. - ep = container_of(_ep, struct fotg210_ep, ep);
  11955. - req = container_of(_req, struct fotg210_request, req);
  11956. -
  11957. - if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
  11958. - return -ESHUTDOWN;
  11959. -
  11960. - spin_lock_irqsave(&ep->fotg210->lock, flags);
  11961. -
  11962. - if (list_empty(&ep->queue))
  11963. - request = 1;
  11964. -
  11965. - list_add_tail(&req->queue, &ep->queue);
  11966. -
  11967. - req->req.actual = 0;
  11968. - req->req.status = -EINPROGRESS;
  11969. -
  11970. - if (!ep->epnum) /* ep0 */
  11971. - fotg210_ep0_queue(ep, req);
  11972. - else if (request && !ep->stall)
  11973. - fotg210_enable_fifo_int(ep);
  11974. -
  11975. - spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  11976. -
  11977. - return 0;
  11978. -}
  11979. -
  11980. -static int fotg210_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  11981. -{
  11982. - struct fotg210_ep *ep;
  11983. - struct fotg210_request *req;
  11984. - unsigned long flags;
  11985. -
  11986. - ep = container_of(_ep, struct fotg210_ep, ep);
  11987. - req = container_of(_req, struct fotg210_request, req);
  11988. -
  11989. - spin_lock_irqsave(&ep->fotg210->lock, flags);
  11990. - if (!list_empty(&ep->queue))
  11991. - fotg210_done(ep, req, -ECONNRESET);
  11992. - spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  11993. -
  11994. - return 0;
  11995. -}
  11996. -
  11997. -static void fotg210_set_epnstall(struct fotg210_ep *ep)
  11998. -{
  11999. - struct fotg210_udc *fotg210 = ep->fotg210;
  12000. - u32 value;
  12001. - void __iomem *reg;
  12002. -
  12003. - /* check if IN FIFO is empty before stall */
  12004. - if (ep->dir_in) {
  12005. - do {
  12006. - value = ioread32(fotg210->reg + FOTG210_DCFESR);
  12007. - } while (!(value & DCFESR_FIFO_EMPTY(ep->epnum - 1)));
  12008. - }
  12009. -
  12010. - reg = (ep->dir_in) ?
  12011. - fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  12012. - fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  12013. - value = ioread32(reg);
  12014. - value |= INOUTEPMPSR_STL_EP;
  12015. - iowrite32(value, reg);
  12016. -}
  12017. -
  12018. -static void fotg210_clear_epnstall(struct fotg210_ep *ep)
  12019. -{
  12020. - struct fotg210_udc *fotg210 = ep->fotg210;
  12021. - u32 value;
  12022. - void __iomem *reg;
  12023. -
  12024. - reg = (ep->dir_in) ?
  12025. - fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  12026. - fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  12027. - value = ioread32(reg);
  12028. - value &= ~INOUTEPMPSR_STL_EP;
  12029. - iowrite32(value, reg);
  12030. -}
  12031. -
  12032. -static int fotg210_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
  12033. -{
  12034. - struct fotg210_ep *ep;
  12035. - struct fotg210_udc *fotg210;
  12036. - unsigned long flags;
  12037. -
  12038. - ep = container_of(_ep, struct fotg210_ep, ep);
  12039. -
  12040. - fotg210 = ep->fotg210;
  12041. -
  12042. - spin_lock_irqsave(&ep->fotg210->lock, flags);
  12043. -
  12044. - if (value) {
  12045. - fotg210_set_epnstall(ep);
  12046. - ep->stall = 1;
  12047. - if (wedge)
  12048. - ep->wedged = 1;
  12049. - } else {
  12050. - fotg210_reset_tseq(fotg210, ep->epnum);
  12051. - fotg210_clear_epnstall(ep);
  12052. - ep->stall = 0;
  12053. - ep->wedged = 0;
  12054. - if (!list_empty(&ep->queue))
  12055. - fotg210_enable_fifo_int(ep);
  12056. - }
  12057. -
  12058. - spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  12059. - return 0;
  12060. -}
  12061. -
  12062. -static int fotg210_ep_set_halt(struct usb_ep *_ep, int value)
  12063. -{
  12064. - return fotg210_set_halt_and_wedge(_ep, value, 0);
  12065. -}
  12066. -
  12067. -static int fotg210_ep_set_wedge(struct usb_ep *_ep)
  12068. -{
  12069. - return fotg210_set_halt_and_wedge(_ep, 1, 1);
  12070. -}
  12071. -
  12072. -static void fotg210_ep_fifo_flush(struct usb_ep *_ep)
  12073. -{
  12074. -}
  12075. -
  12076. -static const struct usb_ep_ops fotg210_ep_ops = {
  12077. - .enable = fotg210_ep_enable,
  12078. - .disable = fotg210_ep_disable,
  12079. -
  12080. - .alloc_request = fotg210_ep_alloc_request,
  12081. - .free_request = fotg210_ep_free_request,
  12082. -
  12083. - .queue = fotg210_ep_queue,
  12084. - .dequeue = fotg210_ep_dequeue,
  12085. -
  12086. - .set_halt = fotg210_ep_set_halt,
  12087. - .fifo_flush = fotg210_ep_fifo_flush,
  12088. - .set_wedge = fotg210_ep_set_wedge,
  12089. -};
  12090. -
  12091. -static void fotg210_clear_tx0byte(struct fotg210_udc *fotg210)
  12092. -{
  12093. - u32 value = ioread32(fotg210->reg + FOTG210_TX0BYTE);
  12094. -
  12095. - value &= ~(TX0BYTE_EP1 | TX0BYTE_EP2 | TX0BYTE_EP3
  12096. - | TX0BYTE_EP4);
  12097. - iowrite32(value, fotg210->reg + FOTG210_TX0BYTE);
  12098. -}
  12099. -
  12100. -static void fotg210_clear_rx0byte(struct fotg210_udc *fotg210)
  12101. -{
  12102. - u32 value = ioread32(fotg210->reg + FOTG210_RX0BYTE);
  12103. -
  12104. - value &= ~(RX0BYTE_EP1 | RX0BYTE_EP2 | RX0BYTE_EP3
  12105. - | RX0BYTE_EP4);
  12106. - iowrite32(value, fotg210->reg + FOTG210_RX0BYTE);
  12107. -}
  12108. -
  12109. -/* read 8-byte setup packet only */
  12110. -static void fotg210_rdsetupp(struct fotg210_udc *fotg210,
  12111. - u8 *buffer)
  12112. -{
  12113. - int i = 0;
  12114. - u8 *tmp = buffer;
  12115. - u32 data;
  12116. - u32 length = 8;
  12117. -
  12118. - iowrite32(DMATFNR_ACC_CXF, fotg210->reg + FOTG210_DMATFNR);
  12119. -
  12120. - for (i = (length >> 2); i > 0; i--) {
  12121. - data = ioread32(fotg210->reg + FOTG210_CXPORT);
  12122. - *tmp = data & 0xFF;
  12123. - *(tmp + 1) = (data >> 8) & 0xFF;
  12124. - *(tmp + 2) = (data >> 16) & 0xFF;
  12125. - *(tmp + 3) = (data >> 24) & 0xFF;
  12126. - tmp = tmp + 4;
  12127. - }
  12128. -
  12129. - switch (length % 4) {
  12130. - case 1:
  12131. - data = ioread32(fotg210->reg + FOTG210_CXPORT);
  12132. - *tmp = data & 0xFF;
  12133. - break;
  12134. - case 2:
  12135. - data = ioread32(fotg210->reg + FOTG210_CXPORT);
  12136. - *tmp = data & 0xFF;
  12137. - *(tmp + 1) = (data >> 8) & 0xFF;
  12138. - break;
  12139. - case 3:
  12140. - data = ioread32(fotg210->reg + FOTG210_CXPORT);
  12141. - *tmp = data & 0xFF;
  12142. - *(tmp + 1) = (data >> 8) & 0xFF;
  12143. - *(tmp + 2) = (data >> 16) & 0xFF;
  12144. - break;
  12145. - default:
  12146. - break;
  12147. - }
  12148. -
  12149. - iowrite32(DMATFNR_DISDMA, fotg210->reg + FOTG210_DMATFNR);
  12150. -}
  12151. -
  12152. -static void fotg210_set_configuration(struct fotg210_udc *fotg210)
  12153. -{
  12154. - u32 value = ioread32(fotg210->reg + FOTG210_DAR);
  12155. -
  12156. - value |= DAR_AFT_CONF;
  12157. - iowrite32(value, fotg210->reg + FOTG210_DAR);
  12158. -}
  12159. -
  12160. -static void fotg210_set_dev_addr(struct fotg210_udc *fotg210, u32 addr)
  12161. -{
  12162. - u32 value = ioread32(fotg210->reg + FOTG210_DAR);
  12163. -
  12164. - value |= (addr & 0x7F);
  12165. - iowrite32(value, fotg210->reg + FOTG210_DAR);
  12166. -}
  12167. -
  12168. -static void fotg210_set_cxstall(struct fotg210_udc *fotg210)
  12169. -{
  12170. - u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
  12171. -
  12172. - value |= DCFESR_CX_STL;
  12173. - iowrite32(value, fotg210->reg + FOTG210_DCFESR);
  12174. -}
  12175. -
  12176. -static void fotg210_request_error(struct fotg210_udc *fotg210)
  12177. -{
  12178. - fotg210_set_cxstall(fotg210);
  12179. - pr_err("request error!!\n");
  12180. -}
  12181. -
  12182. -static void fotg210_set_address(struct fotg210_udc *fotg210,
  12183. - struct usb_ctrlrequest *ctrl)
  12184. -{
  12185. - if (le16_to_cpu(ctrl->wValue) >= 0x0100) {
  12186. - fotg210_request_error(fotg210);
  12187. - } else {
  12188. - fotg210_set_dev_addr(fotg210, le16_to_cpu(ctrl->wValue));
  12189. - fotg210_set_cxdone(fotg210);
  12190. - }
  12191. -}
  12192. -
  12193. -static void fotg210_set_feature(struct fotg210_udc *fotg210,
  12194. - struct usb_ctrlrequest *ctrl)
  12195. -{
  12196. - switch (ctrl->bRequestType & USB_RECIP_MASK) {
  12197. - case USB_RECIP_DEVICE:
  12198. - fotg210_set_cxdone(fotg210);
  12199. - break;
  12200. - case USB_RECIP_INTERFACE:
  12201. - fotg210_set_cxdone(fotg210);
  12202. - break;
  12203. - case USB_RECIP_ENDPOINT: {
  12204. - u8 epnum;
  12205. - epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  12206. - if (epnum)
  12207. - fotg210_set_epnstall(fotg210->ep[epnum]);
  12208. - else
  12209. - fotg210_set_cxstall(fotg210);
  12210. - fotg210_set_cxdone(fotg210);
  12211. - }
  12212. - break;
  12213. - default:
  12214. - fotg210_request_error(fotg210);
  12215. - break;
  12216. - }
  12217. -}
  12218. -
  12219. -static void fotg210_clear_feature(struct fotg210_udc *fotg210,
  12220. - struct usb_ctrlrequest *ctrl)
  12221. -{
  12222. - struct fotg210_ep *ep =
  12223. - fotg210->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
  12224. -
  12225. - switch (ctrl->bRequestType & USB_RECIP_MASK) {
  12226. - case USB_RECIP_DEVICE:
  12227. - fotg210_set_cxdone(fotg210);
  12228. - break;
  12229. - case USB_RECIP_INTERFACE:
  12230. - fotg210_set_cxdone(fotg210);
  12231. - break;
  12232. - case USB_RECIP_ENDPOINT:
  12233. - if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
  12234. - if (ep->wedged) {
  12235. - fotg210_set_cxdone(fotg210);
  12236. - break;
  12237. - }
  12238. - if (ep->stall)
  12239. - fotg210_set_halt_and_wedge(&ep->ep, 0, 0);
  12240. - }
  12241. - fotg210_set_cxdone(fotg210);
  12242. - break;
  12243. - default:
  12244. - fotg210_request_error(fotg210);
  12245. - break;
  12246. - }
  12247. -}
  12248. -
  12249. -static int fotg210_is_epnstall(struct fotg210_ep *ep)
  12250. -{
  12251. - struct fotg210_udc *fotg210 = ep->fotg210;
  12252. - u32 value;
  12253. - void __iomem *reg;
  12254. -
  12255. - reg = (ep->dir_in) ?
  12256. - fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  12257. - fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  12258. - value = ioread32(reg);
  12259. - return value & INOUTEPMPSR_STL_EP ? 1 : 0;
  12260. -}
  12261. -
  12262. -/* For EP0 requests triggered by this driver (currently GET_STATUS response) */
  12263. -static void fotg210_ep0_complete(struct usb_ep *_ep, struct usb_request *req)
  12264. -{
  12265. - struct fotg210_ep *ep;
  12266. - struct fotg210_udc *fotg210;
  12267. -
  12268. - ep = container_of(_ep, struct fotg210_ep, ep);
  12269. - fotg210 = ep->fotg210;
  12270. -
  12271. - if (req->status || req->actual != req->length) {
  12272. - dev_warn(&fotg210->gadget.dev, "EP0 request failed: %d\n", req->status);
  12273. - }
  12274. -}
  12275. -
  12276. -static void fotg210_get_status(struct fotg210_udc *fotg210,
  12277. - struct usb_ctrlrequest *ctrl)
  12278. -{
  12279. - u8 epnum;
  12280. -
  12281. - switch (ctrl->bRequestType & USB_RECIP_MASK) {
  12282. - case USB_RECIP_DEVICE:
  12283. - fotg210->ep0_data = cpu_to_le16(1 << USB_DEVICE_SELF_POWERED);
  12284. - break;
  12285. - case USB_RECIP_INTERFACE:
  12286. - fotg210->ep0_data = cpu_to_le16(0);
  12287. - break;
  12288. - case USB_RECIP_ENDPOINT:
  12289. - epnum = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
  12290. - if (epnum)
  12291. - fotg210->ep0_data =
  12292. - cpu_to_le16(fotg210_is_epnstall(fotg210->ep[epnum])
  12293. - << USB_ENDPOINT_HALT);
  12294. - else
  12295. - fotg210_request_error(fotg210);
  12296. - break;
  12297. -
  12298. - default:
  12299. - fotg210_request_error(fotg210);
  12300. - return; /* exit */
  12301. - }
  12302. -
  12303. - fotg210->ep0_req->buf = &fotg210->ep0_data;
  12304. - fotg210->ep0_req->length = 2;
  12305. -
  12306. - spin_unlock(&fotg210->lock);
  12307. - fotg210_ep_queue(fotg210->gadget.ep0, fotg210->ep0_req, GFP_ATOMIC);
  12308. - spin_lock(&fotg210->lock);
  12309. -}
  12310. -
  12311. -static int fotg210_setup_packet(struct fotg210_udc *fotg210,
  12312. - struct usb_ctrlrequest *ctrl)
  12313. -{
  12314. - u8 *p = (u8 *)ctrl;
  12315. - u8 ret = 0;
  12316. -
  12317. - fotg210_rdsetupp(fotg210, p);
  12318. -
  12319. - fotg210->ep[0]->dir_in = ctrl->bRequestType & USB_DIR_IN;
  12320. -
  12321. - if (fotg210->gadget.speed == USB_SPEED_UNKNOWN) {
  12322. - u32 value = ioread32(fotg210->reg + FOTG210_DMCR);
  12323. - fotg210->gadget.speed = value & DMCR_HS_EN ?
  12324. - USB_SPEED_HIGH : USB_SPEED_FULL;
  12325. - }
  12326. -
  12327. - /* check request */
  12328. - if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  12329. - switch (ctrl->bRequest) {
  12330. - case USB_REQ_GET_STATUS:
  12331. - fotg210_get_status(fotg210, ctrl);
  12332. - break;
  12333. - case USB_REQ_CLEAR_FEATURE:
  12334. - fotg210_clear_feature(fotg210, ctrl);
  12335. - break;
  12336. - case USB_REQ_SET_FEATURE:
  12337. - fotg210_set_feature(fotg210, ctrl);
  12338. - break;
  12339. - case USB_REQ_SET_ADDRESS:
  12340. - fotg210_set_address(fotg210, ctrl);
  12341. - break;
  12342. - case USB_REQ_SET_CONFIGURATION:
  12343. - fotg210_set_configuration(fotg210);
  12344. - ret = 1;
  12345. - break;
  12346. - default:
  12347. - ret = 1;
  12348. - break;
  12349. - }
  12350. - } else {
  12351. - ret = 1;
  12352. - }
  12353. -
  12354. - return ret;
  12355. -}
  12356. -
  12357. -static void fotg210_ep0out(struct fotg210_udc *fotg210)
  12358. -{
  12359. - struct fotg210_ep *ep = fotg210->ep[0];
  12360. -
  12361. - if (!list_empty(&ep->queue) && !ep->dir_in) {
  12362. - struct fotg210_request *req;
  12363. -
  12364. - req = list_first_entry(&ep->queue,
  12365. - struct fotg210_request, queue);
  12366. -
  12367. - if (req->req.length)
  12368. - fotg210_start_dma(ep, req);
  12369. -
  12370. - if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
  12371. - fotg210_done(ep, req, 0);
  12372. - } else {
  12373. - pr_err("%s : empty queue\n", __func__);
  12374. - }
  12375. -}
  12376. -
  12377. -static void fotg210_ep0in(struct fotg210_udc *fotg210)
  12378. -{
  12379. - struct fotg210_ep *ep = fotg210->ep[0];
  12380. -
  12381. - if ((!list_empty(&ep->queue)) && (ep->dir_in)) {
  12382. - struct fotg210_request *req;
  12383. -
  12384. - req = list_entry(ep->queue.next,
  12385. - struct fotg210_request, queue);
  12386. -
  12387. - if (req->req.length)
  12388. - fotg210_start_dma(ep, req);
  12389. -
  12390. - if (req->req.actual == req->req.length)
  12391. - fotg210_done(ep, req, 0);
  12392. - } else {
  12393. - fotg210_set_cxdone(fotg210);
  12394. - }
  12395. -}
  12396. -
  12397. -static void fotg210_clear_comabt_int(struct fotg210_udc *fotg210)
  12398. -{
  12399. - u32 value = ioread32(fotg210->reg + FOTG210_DISGR0);
  12400. -
  12401. - value &= ~DISGR0_CX_COMABT_INT;
  12402. - iowrite32(value, fotg210->reg + FOTG210_DISGR0);
  12403. -}
  12404. -
  12405. -static void fotg210_in_fifo_handler(struct fotg210_ep *ep)
  12406. -{
  12407. - struct fotg210_request *req = list_entry(ep->queue.next,
  12408. - struct fotg210_request, queue);
  12409. -
  12410. - if (req->req.length)
  12411. - fotg210_start_dma(ep, req);
  12412. - fotg210_done(ep, req, 0);
  12413. -}
  12414. -
  12415. -static void fotg210_out_fifo_handler(struct fotg210_ep *ep)
  12416. -{
  12417. - struct fotg210_request *req = list_entry(ep->queue.next,
  12418. - struct fotg210_request, queue);
  12419. - int disgr1 = ioread32(ep->fotg210->reg + FOTG210_DISGR1);
  12420. -
  12421. - fotg210_start_dma(ep, req);
  12422. -
  12423. - /* Complete the request when it's full or a short packet arrived.
  12424. - * Like other drivers, short_not_ok isn't handled.
  12425. - */
  12426. -
  12427. - if (req->req.length == req->req.actual ||
  12428. - (disgr1 & DISGR1_SPK_INT(ep->epnum - 1)))
  12429. - fotg210_done(ep, req, 0);
  12430. -}
  12431. -
  12432. -static irqreturn_t fotg210_irq(int irq, void *_fotg210)
  12433. -{
  12434. - struct fotg210_udc *fotg210 = _fotg210;
  12435. - u32 int_grp = ioread32(fotg210->reg + FOTG210_DIGR);
  12436. - u32 int_msk = ioread32(fotg210->reg + FOTG210_DMIGR);
  12437. -
  12438. - int_grp &= ~int_msk;
  12439. -
  12440. - spin_lock(&fotg210->lock);
  12441. -
  12442. - if (int_grp & DIGR_INT_G2) {
  12443. - void __iomem *reg = fotg210->reg + FOTG210_DISGR2;
  12444. - u32 int_grp2 = ioread32(reg);
  12445. - u32 int_msk2 = ioread32(fotg210->reg + FOTG210_DMISGR2);
  12446. - u32 value;
  12447. -
  12448. - int_grp2 &= ~int_msk2;
  12449. -
  12450. - if (int_grp2 & DISGR2_USBRST_INT) {
  12451. - usb_gadget_udc_reset(&fotg210->gadget,
  12452. - fotg210->driver);
  12453. - value = ioread32(reg);
  12454. - value &= ~DISGR2_USBRST_INT;
  12455. - iowrite32(value, reg);
  12456. - pr_info("fotg210 udc reset\n");
  12457. - }
  12458. - if (int_grp2 & DISGR2_SUSP_INT) {
  12459. - value = ioread32(reg);
  12460. - value &= ~DISGR2_SUSP_INT;
  12461. - iowrite32(value, reg);
  12462. - pr_info("fotg210 udc suspend\n");
  12463. - }
  12464. - if (int_grp2 & DISGR2_RESM_INT) {
  12465. - value = ioread32(reg);
  12466. - value &= ~DISGR2_RESM_INT;
  12467. - iowrite32(value, reg);
  12468. - pr_info("fotg210 udc resume\n");
  12469. - }
  12470. - if (int_grp2 & DISGR2_ISO_SEQ_ERR_INT) {
  12471. - value = ioread32(reg);
  12472. - value &= ~DISGR2_ISO_SEQ_ERR_INT;
  12473. - iowrite32(value, reg);
  12474. - pr_info("fotg210 iso sequence error\n");
  12475. - }
  12476. - if (int_grp2 & DISGR2_ISO_SEQ_ABORT_INT) {
  12477. - value = ioread32(reg);
  12478. - value &= ~DISGR2_ISO_SEQ_ABORT_INT;
  12479. - iowrite32(value, reg);
  12480. - pr_info("fotg210 iso sequence abort\n");
  12481. - }
  12482. - if (int_grp2 & DISGR2_TX0BYTE_INT) {
  12483. - fotg210_clear_tx0byte(fotg210);
  12484. - value = ioread32(reg);
  12485. - value &= ~DISGR2_TX0BYTE_INT;
  12486. - iowrite32(value, reg);
  12487. - pr_info("fotg210 transferred 0 byte\n");
  12488. - }
  12489. - if (int_grp2 & DISGR2_RX0BYTE_INT) {
  12490. - fotg210_clear_rx0byte(fotg210);
  12491. - value = ioread32(reg);
  12492. - value &= ~DISGR2_RX0BYTE_INT;
  12493. - iowrite32(value, reg);
  12494. - pr_info("fotg210 received 0 byte\n");
  12495. - }
  12496. - if (int_grp2 & DISGR2_DMA_ERROR) {
  12497. - value = ioread32(reg);
  12498. - value &= ~DISGR2_DMA_ERROR;
  12499. - iowrite32(value, reg);
  12500. - }
  12501. - }
  12502. -
  12503. - if (int_grp & DIGR_INT_G0) {
  12504. - void __iomem *reg = fotg210->reg + FOTG210_DISGR0;
  12505. - u32 int_grp0 = ioread32(reg);
  12506. - u32 int_msk0 = ioread32(fotg210->reg + FOTG210_DMISGR0);
  12507. - struct usb_ctrlrequest ctrl;
  12508. -
  12509. - int_grp0 &= ~int_msk0;
  12510. -
  12511. - /* the highest priority in this source register */
  12512. - if (int_grp0 & DISGR0_CX_COMABT_INT) {
  12513. - fotg210_clear_comabt_int(fotg210);
  12514. - pr_info("fotg210 CX command abort\n");
  12515. - }
  12516. -
  12517. - if (int_grp0 & DISGR0_CX_SETUP_INT) {
  12518. - if (fotg210_setup_packet(fotg210, &ctrl)) {
  12519. - spin_unlock(&fotg210->lock);
  12520. - if (fotg210->driver->setup(&fotg210->gadget,
  12521. - &ctrl) < 0)
  12522. - fotg210_set_cxstall(fotg210);
  12523. - spin_lock(&fotg210->lock);
  12524. - }
  12525. - }
  12526. - if (int_grp0 & DISGR0_CX_COMEND_INT)
  12527. - pr_info("fotg210 cmd end\n");
  12528. -
  12529. - if (int_grp0 & DISGR0_CX_IN_INT)
  12530. - fotg210_ep0in(fotg210);
  12531. -
  12532. - if (int_grp0 & DISGR0_CX_OUT_INT)
  12533. - fotg210_ep0out(fotg210);
  12534. -
  12535. - if (int_grp0 & DISGR0_CX_COMFAIL_INT) {
  12536. - fotg210_set_cxstall(fotg210);
  12537. - pr_info("fotg210 ep0 fail\n");
  12538. - }
  12539. - }
  12540. -
  12541. - if (int_grp & DIGR_INT_G1) {
  12542. - void __iomem *reg = fotg210->reg + FOTG210_DISGR1;
  12543. - u32 int_grp1 = ioread32(reg);
  12544. - u32 int_msk1 = ioread32(fotg210->reg + FOTG210_DMISGR1);
  12545. - int fifo;
  12546. -
  12547. - int_grp1 &= ~int_msk1;
  12548. -
  12549. - for (fifo = 0; fifo < FOTG210_MAX_FIFO_NUM; fifo++) {
  12550. - if (int_grp1 & DISGR1_IN_INT(fifo))
  12551. - fotg210_in_fifo_handler(fotg210->ep[fifo + 1]);
  12552. -
  12553. - if ((int_grp1 & DISGR1_OUT_INT(fifo)) ||
  12554. - (int_grp1 & DISGR1_SPK_INT(fifo)))
  12555. - fotg210_out_fifo_handler(fotg210->ep[fifo + 1]);
  12556. - }
  12557. - }
  12558. -
  12559. - spin_unlock(&fotg210->lock);
  12560. -
  12561. - return IRQ_HANDLED;
  12562. -}
  12563. -
  12564. -static void fotg210_disable_unplug(struct fotg210_udc *fotg210)
  12565. -{
  12566. - u32 reg = ioread32(fotg210->reg + FOTG210_PHYTMSR);
  12567. -
  12568. - reg &= ~PHYTMSR_UNPLUG;
  12569. - iowrite32(reg, fotg210->reg + FOTG210_PHYTMSR);
  12570. -}
  12571. -
  12572. -static int fotg210_udc_start(struct usb_gadget *g,
  12573. - struct usb_gadget_driver *driver)
  12574. -{
  12575. - struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
  12576. - u32 value;
  12577. -
  12578. - /* hook up the driver */
  12579. - fotg210->driver = driver;
  12580. -
  12581. - /* enable device global interrupt */
  12582. - value = ioread32(fotg210->reg + FOTG210_DMCR);
  12583. - value |= DMCR_GLINT_EN;
  12584. - iowrite32(value, fotg210->reg + FOTG210_DMCR);
  12585. -
  12586. - return 0;
  12587. -}
  12588. -
  12589. -static void fotg210_init(struct fotg210_udc *fotg210)
  12590. -{
  12591. - u32 value;
  12592. -
  12593. - /* disable global interrupt and set int polarity to active high */
  12594. - iowrite32(GMIR_MHC_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
  12595. - fotg210->reg + FOTG210_GMIR);
  12596. -
  12597. - /* disable device global interrupt */
  12598. - value = ioread32(fotg210->reg + FOTG210_DMCR);
  12599. - value &= ~DMCR_GLINT_EN;
  12600. - iowrite32(value, fotg210->reg + FOTG210_DMCR);
  12601. -
  12602. - /* enable only grp2 irqs we handle */
  12603. - iowrite32(~(DISGR2_DMA_ERROR | DISGR2_RX0BYTE_INT | DISGR2_TX0BYTE_INT
  12604. - | DISGR2_ISO_SEQ_ABORT_INT | DISGR2_ISO_SEQ_ERR_INT
  12605. - | DISGR2_RESM_INT | DISGR2_SUSP_INT | DISGR2_USBRST_INT),
  12606. - fotg210->reg + FOTG210_DMISGR2);
  12607. -
  12608. - /* disable all fifo interrupt */
  12609. - iowrite32(~(u32)0, fotg210->reg + FOTG210_DMISGR1);
  12610. -
  12611. - /* disable cmd end */
  12612. - value = ioread32(fotg210->reg + FOTG210_DMISGR0);
  12613. - value |= DMISGR0_MCX_COMEND;
  12614. - iowrite32(value, fotg210->reg + FOTG210_DMISGR0);
  12615. -}
  12616. -
  12617. -static int fotg210_udc_stop(struct usb_gadget *g)
  12618. -{
  12619. - struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
  12620. - unsigned long flags;
  12621. -
  12622. - spin_lock_irqsave(&fotg210->lock, flags);
  12623. -
  12624. - fotg210_init(fotg210);
  12625. - fotg210->driver = NULL;
  12626. -
  12627. - spin_unlock_irqrestore(&fotg210->lock, flags);
  12628. -
  12629. - return 0;
  12630. -}
  12631. -
  12632. -static const struct usb_gadget_ops fotg210_gadget_ops = {
  12633. - .udc_start = fotg210_udc_start,
  12634. - .udc_stop = fotg210_udc_stop,
  12635. -};
  12636. -
  12637. -static int fotg210_udc_remove(struct platform_device *pdev)
  12638. -{
  12639. - struct fotg210_udc *fotg210 = platform_get_drvdata(pdev);
  12640. - int i;
  12641. -
  12642. - usb_del_gadget_udc(&fotg210->gadget);
  12643. - iounmap(fotg210->reg);
  12644. - free_irq(platform_get_irq(pdev, 0), fotg210);
  12645. -
  12646. - fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
  12647. - for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
  12648. - kfree(fotg210->ep[i]);
  12649. - kfree(fotg210);
  12650. -
  12651. - return 0;
  12652. -}
  12653. -
  12654. -static int fotg210_udc_probe(struct platform_device *pdev)
  12655. -{
  12656. - struct resource *res, *ires;
  12657. - struct fotg210_udc *fotg210 = NULL;
  12658. - struct fotg210_ep *_ep[FOTG210_MAX_NUM_EP];
  12659. - int ret = 0;
  12660. - int i;
  12661. -
  12662. - res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  12663. - if (!res) {
  12664. - pr_err("platform_get_resource error.\n");
  12665. - return -ENODEV;
  12666. - }
  12667. -
  12668. - ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  12669. - if (!ires) {
  12670. - pr_err("platform_get_resource IORESOURCE_IRQ error.\n");
  12671. - return -ENODEV;
  12672. - }
  12673. -
  12674. - ret = -ENOMEM;
  12675. -
  12676. - /* initialize udc */
  12677. - fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL);
  12678. - if (fotg210 == NULL)
  12679. - goto err;
  12680. -
  12681. - for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
  12682. - _ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL);
  12683. - if (_ep[i] == NULL)
  12684. - goto err_alloc;
  12685. - fotg210->ep[i] = _ep[i];
  12686. - }
  12687. -
  12688. - fotg210->reg = ioremap(res->start, resource_size(res));
  12689. - if (fotg210->reg == NULL) {
  12690. - pr_err("ioremap error.\n");
  12691. - goto err_alloc;
  12692. - }
  12693. -
  12694. - spin_lock_init(&fotg210->lock);
  12695. -
  12696. - platform_set_drvdata(pdev, fotg210);
  12697. -
  12698. - fotg210->gadget.ops = &fotg210_gadget_ops;
  12699. -
  12700. - fotg210->gadget.max_speed = USB_SPEED_HIGH;
  12701. - fotg210->gadget.dev.parent = &pdev->dev;
  12702. - fotg210->gadget.dev.dma_mask = pdev->dev.dma_mask;
  12703. - fotg210->gadget.name = udc_name;
  12704. -
  12705. - INIT_LIST_HEAD(&fotg210->gadget.ep_list);
  12706. -
  12707. - for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
  12708. - struct fotg210_ep *ep = fotg210->ep[i];
  12709. -
  12710. - if (i) {
  12711. - INIT_LIST_HEAD(&fotg210->ep[i]->ep.ep_list);
  12712. - list_add_tail(&fotg210->ep[i]->ep.ep_list,
  12713. - &fotg210->gadget.ep_list);
  12714. - }
  12715. - ep->fotg210 = fotg210;
  12716. - INIT_LIST_HEAD(&ep->queue);
  12717. - ep->ep.name = fotg210_ep_name[i];
  12718. - ep->ep.ops = &fotg210_ep_ops;
  12719. - usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
  12720. -
  12721. - if (i == 0) {
  12722. - ep->ep.caps.type_control = true;
  12723. - } else {
  12724. - ep->ep.caps.type_iso = true;
  12725. - ep->ep.caps.type_bulk = true;
  12726. - ep->ep.caps.type_int = true;
  12727. - }
  12728. -
  12729. - ep->ep.caps.dir_in = true;
  12730. - ep->ep.caps.dir_out = true;
  12731. - }
  12732. - usb_ep_set_maxpacket_limit(&fotg210->ep[0]->ep, 0x40);
  12733. - fotg210->gadget.ep0 = &fotg210->ep[0]->ep;
  12734. - INIT_LIST_HEAD(&fotg210->gadget.ep0->ep_list);
  12735. -
  12736. - fotg210->ep0_req = fotg210_ep_alloc_request(&fotg210->ep[0]->ep,
  12737. - GFP_KERNEL);
  12738. - if (fotg210->ep0_req == NULL)
  12739. - goto err_map;
  12740. -
  12741. - fotg210->ep0_req->complete = fotg210_ep0_complete;
  12742. -
  12743. - fotg210_init(fotg210);
  12744. -
  12745. - fotg210_disable_unplug(fotg210);
  12746. -
  12747. - ret = request_irq(ires->start, fotg210_irq, IRQF_SHARED,
  12748. - udc_name, fotg210);
  12749. - if (ret < 0) {
  12750. - pr_err("request_irq error (%d)\n", ret);
  12751. - goto err_req;
  12752. - }
  12753. -
  12754. - ret = usb_add_gadget_udc(&pdev->dev, &fotg210->gadget);
  12755. - if (ret)
  12756. - goto err_add_udc;
  12757. -
  12758. - dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
  12759. -
  12760. - return 0;
  12761. -
  12762. -err_add_udc:
  12763. - free_irq(ires->start, fotg210);
  12764. -
  12765. -err_req:
  12766. - fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
  12767. -
  12768. -err_map:
  12769. - iounmap(fotg210->reg);
  12770. -
  12771. -err_alloc:
  12772. - for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
  12773. - kfree(fotg210->ep[i]);
  12774. - kfree(fotg210);
  12775. -
  12776. -err:
  12777. - return ret;
  12778. -}
  12779. -
  12780. -static struct platform_driver fotg210_driver = {
  12781. - .driver = {
  12782. - .name = udc_name,
  12783. - },
  12784. - .probe = fotg210_udc_probe,
  12785. - .remove = fotg210_udc_remove,
  12786. -};
  12787. -
  12788. -module_platform_driver(fotg210_driver);
  12789. -
  12790. -MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang <[email protected]>");
  12791. -MODULE_LICENSE("GPL");
  12792. -MODULE_DESCRIPTION(DRIVER_DESC);
  12793. --- /dev/null
  12794. +++ b/drivers/usb/fotg210/fotg210-udc.c
  12795. @@ -0,0 +1,1239 @@
  12796. +// SPDX-License-Identifier: GPL-2.0
  12797. +/*
  12798. + * FOTG210 UDC Driver supports Bulk transfer so far
  12799. + *
  12800. + * Copyright (C) 2013 Faraday Technology Corporation
  12801. + *
  12802. + * Author : Yuan-Hsin Chen <[email protected]>
  12803. + */
  12804. +
  12805. +#include <linux/dma-mapping.h>
  12806. +#include <linux/err.h>
  12807. +#include <linux/interrupt.h>
  12808. +#include <linux/io.h>
  12809. +#include <linux/module.h>
  12810. +#include <linux/platform_device.h>
  12811. +#include <linux/usb/ch9.h>
  12812. +#include <linux/usb/gadget.h>
  12813. +
  12814. +#include "fotg210-udc.h"
  12815. +
  12816. +#define DRIVER_DESC "FOTG210 USB Device Controller Driver"
  12817. +#define DRIVER_VERSION "30-April-2013"
  12818. +
  12819. +static const char udc_name[] = "fotg210_udc";
  12820. +static const char * const fotg210_ep_name[] = {
  12821. + "ep0", "ep1", "ep2", "ep3", "ep4"};
  12822. +
  12823. +static void fotg210_disable_fifo_int(struct fotg210_ep *ep)
  12824. +{
  12825. + u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
  12826. +
  12827. + if (ep->dir_in)
  12828. + value |= DMISGR1_MF_IN_INT(ep->epnum - 1);
  12829. + else
  12830. + value |= DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
  12831. + iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
  12832. +}
  12833. +
  12834. +static void fotg210_enable_fifo_int(struct fotg210_ep *ep)
  12835. +{
  12836. + u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
  12837. +
  12838. + if (ep->dir_in)
  12839. + value &= ~DMISGR1_MF_IN_INT(ep->epnum - 1);
  12840. + else
  12841. + value &= ~DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
  12842. + iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
  12843. +}
  12844. +
  12845. +static void fotg210_set_cxdone(struct fotg210_udc *fotg210)
  12846. +{
  12847. + u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
  12848. +
  12849. + value |= DCFESR_CX_DONE;
  12850. + iowrite32(value, fotg210->reg + FOTG210_DCFESR);
  12851. +}
  12852. +
  12853. +static void fotg210_done(struct fotg210_ep *ep, struct fotg210_request *req,
  12854. + int status)
  12855. +{
  12856. + list_del_init(&req->queue);
  12857. +
  12858. + /* don't modify queue heads during completion callback */
  12859. + if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
  12860. + req->req.status = -ESHUTDOWN;
  12861. + else
  12862. + req->req.status = status;
  12863. +
  12864. + spin_unlock(&ep->fotg210->lock);
  12865. + usb_gadget_giveback_request(&ep->ep, &req->req);
  12866. + spin_lock(&ep->fotg210->lock);
  12867. +
  12868. + if (ep->epnum) {
  12869. + if (list_empty(&ep->queue))
  12870. + fotg210_disable_fifo_int(ep);
  12871. + } else {
  12872. + fotg210_set_cxdone(ep->fotg210);
  12873. + }
  12874. +}
  12875. +
  12876. +static void fotg210_fifo_ep_mapping(struct fotg210_ep *ep, u32 epnum,
  12877. + u32 dir_in)
  12878. +{
  12879. + struct fotg210_udc *fotg210 = ep->fotg210;
  12880. + u32 val;
  12881. +
  12882. + /* Driver should map an ep to a fifo and then map the fifo
  12883. + * to the ep. What a brain-damaged design!
  12884. + */
  12885. +
  12886. + /* map a fifo to an ep */
  12887. + val = ioread32(fotg210->reg + FOTG210_EPMAP);
  12888. + val &= ~EPMAP_FIFONOMSK(epnum, dir_in);
  12889. + val |= EPMAP_FIFONO(epnum, dir_in);
  12890. + iowrite32(val, fotg210->reg + FOTG210_EPMAP);
  12891. +
  12892. + /* map the ep to the fifo */
  12893. + val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
  12894. + val &= ~FIFOMAP_EPNOMSK(epnum);
  12895. + val |= FIFOMAP_EPNO(epnum);
  12896. + iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
  12897. +
  12898. + /* enable fifo */
  12899. + val = ioread32(fotg210->reg + FOTG210_FIFOCF);
  12900. + val |= FIFOCF_FIFO_EN(epnum - 1);
  12901. + iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
  12902. +}
  12903. +
  12904. +static void fotg210_set_fifo_dir(struct fotg210_ep *ep, u32 epnum, u32 dir_in)
  12905. +{
  12906. + struct fotg210_udc *fotg210 = ep->fotg210;
  12907. + u32 val;
  12908. +
  12909. + val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
  12910. + val |= (dir_in ? FIFOMAP_DIRIN(epnum - 1) : FIFOMAP_DIROUT(epnum - 1));
  12911. + iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
  12912. +}
  12913. +
  12914. +static void fotg210_set_tfrtype(struct fotg210_ep *ep, u32 epnum, u32 type)
  12915. +{
  12916. + struct fotg210_udc *fotg210 = ep->fotg210;
  12917. + u32 val;
  12918. +
  12919. + val = ioread32(fotg210->reg + FOTG210_FIFOCF);
  12920. + val |= FIFOCF_TYPE(type, epnum - 1);
  12921. + iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
  12922. +}
  12923. +
  12924. +static void fotg210_set_mps(struct fotg210_ep *ep, u32 epnum, u32 mps,
  12925. + u32 dir_in)
  12926. +{
  12927. + struct fotg210_udc *fotg210 = ep->fotg210;
  12928. + u32 val;
  12929. + u32 offset = dir_in ? FOTG210_INEPMPSR(epnum) :
  12930. + FOTG210_OUTEPMPSR(epnum);
  12931. +
  12932. + val = ioread32(fotg210->reg + offset);
  12933. + val |= INOUTEPMPSR_MPS(mps);
  12934. + iowrite32(val, fotg210->reg + offset);
  12935. +}
  12936. +
  12937. +static int fotg210_config_ep(struct fotg210_ep *ep,
  12938. + const struct usb_endpoint_descriptor *desc)
  12939. +{
  12940. + struct fotg210_udc *fotg210 = ep->fotg210;
  12941. +
  12942. + fotg210_set_fifo_dir(ep, ep->epnum, ep->dir_in);
  12943. + fotg210_set_tfrtype(ep, ep->epnum, ep->type);
  12944. + fotg210_set_mps(ep, ep->epnum, ep->ep.maxpacket, ep->dir_in);
  12945. + fotg210_fifo_ep_mapping(ep, ep->epnum, ep->dir_in);
  12946. +
  12947. + fotg210->ep[ep->epnum] = ep;
  12948. +
  12949. + return 0;
  12950. +}
  12951. +
  12952. +static int fotg210_ep_enable(struct usb_ep *_ep,
  12953. + const struct usb_endpoint_descriptor *desc)
  12954. +{
  12955. + struct fotg210_ep *ep;
  12956. +
  12957. + ep = container_of(_ep, struct fotg210_ep, ep);
  12958. +
  12959. + ep->desc = desc;
  12960. + ep->epnum = usb_endpoint_num(desc);
  12961. + ep->type = usb_endpoint_type(desc);
  12962. + ep->dir_in = usb_endpoint_dir_in(desc);
  12963. + ep->ep.maxpacket = usb_endpoint_maxp(desc);
  12964. +
  12965. + return fotg210_config_ep(ep, desc);
  12966. +}
  12967. +
  12968. +static void fotg210_reset_tseq(struct fotg210_udc *fotg210, u8 epnum)
  12969. +{
  12970. + struct fotg210_ep *ep = fotg210->ep[epnum];
  12971. + u32 value;
  12972. + void __iomem *reg;
  12973. +
  12974. + reg = (ep->dir_in) ?
  12975. + fotg210->reg + FOTG210_INEPMPSR(epnum) :
  12976. + fotg210->reg + FOTG210_OUTEPMPSR(epnum);
  12977. +
  12978. + /* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ
  12979. + * bit. Controller wouldn't clear this bit. WTF!!!
  12980. + */
  12981. +
  12982. + value = ioread32(reg);
  12983. + value |= INOUTEPMPSR_RESET_TSEQ;
  12984. + iowrite32(value, reg);
  12985. +
  12986. + value = ioread32(reg);
  12987. + value &= ~INOUTEPMPSR_RESET_TSEQ;
  12988. + iowrite32(value, reg);
  12989. +}
  12990. +
  12991. +static int fotg210_ep_release(struct fotg210_ep *ep)
  12992. +{
  12993. + if (!ep->epnum)
  12994. + return 0;
  12995. + ep->epnum = 0;
  12996. + ep->stall = 0;
  12997. + ep->wedged = 0;
  12998. +
  12999. + fotg210_reset_tseq(ep->fotg210, ep->epnum);
  13000. +
  13001. + return 0;
  13002. +}
  13003. +
  13004. +static int fotg210_ep_disable(struct usb_ep *_ep)
  13005. +{
  13006. + struct fotg210_ep *ep;
  13007. + struct fotg210_request *req;
  13008. + unsigned long flags;
  13009. +
  13010. + BUG_ON(!_ep);
  13011. +
  13012. + ep = container_of(_ep, struct fotg210_ep, ep);
  13013. +
  13014. + while (!list_empty(&ep->queue)) {
  13015. + req = list_entry(ep->queue.next,
  13016. + struct fotg210_request, queue);
  13017. + spin_lock_irqsave(&ep->fotg210->lock, flags);
  13018. + fotg210_done(ep, req, -ECONNRESET);
  13019. + spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  13020. + }
  13021. +
  13022. + return fotg210_ep_release(ep);
  13023. +}
  13024. +
  13025. +static struct usb_request *fotg210_ep_alloc_request(struct usb_ep *_ep,
  13026. + gfp_t gfp_flags)
  13027. +{
  13028. + struct fotg210_request *req;
  13029. +
  13030. + req = kzalloc(sizeof(struct fotg210_request), gfp_flags);
  13031. + if (!req)
  13032. + return NULL;
  13033. +
  13034. + INIT_LIST_HEAD(&req->queue);
  13035. +
  13036. + return &req->req;
  13037. +}
  13038. +
  13039. +static void fotg210_ep_free_request(struct usb_ep *_ep,
  13040. + struct usb_request *_req)
  13041. +{
  13042. + struct fotg210_request *req;
  13043. +
  13044. + req = container_of(_req, struct fotg210_request, req);
  13045. + kfree(req);
  13046. +}
  13047. +
  13048. +static void fotg210_enable_dma(struct fotg210_ep *ep,
  13049. + dma_addr_t d, u32 len)
  13050. +{
  13051. + u32 value;
  13052. + struct fotg210_udc *fotg210 = ep->fotg210;
  13053. +
  13054. + /* set transfer length and direction */
  13055. + value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
  13056. + value &= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1));
  13057. + value |= DMACPSR1_DMA_LEN(len) | DMACPSR1_DMA_TYPE(ep->dir_in);
  13058. + iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
  13059. +
  13060. + /* set device DMA target FIFO number */
  13061. + value = ioread32(fotg210->reg + FOTG210_DMATFNR);
  13062. + if (ep->epnum)
  13063. + value |= DMATFNR_ACC_FN(ep->epnum - 1);
  13064. + else
  13065. + value |= DMATFNR_ACC_CXF;
  13066. + iowrite32(value, fotg210->reg + FOTG210_DMATFNR);
  13067. +
  13068. + /* set DMA memory address */
  13069. + iowrite32(d, fotg210->reg + FOTG210_DMACPSR2);
  13070. +
  13071. + /* enable MDMA_EROR and MDMA_CMPLT interrupt */
  13072. + value = ioread32(fotg210->reg + FOTG210_DMISGR2);
  13073. + value &= ~(DMISGR2_MDMA_CMPLT | DMISGR2_MDMA_ERROR);
  13074. + iowrite32(value, fotg210->reg + FOTG210_DMISGR2);
  13075. +
  13076. + /* start DMA */
  13077. + value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
  13078. + value |= DMACPSR1_DMA_START;
  13079. + iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
  13080. +}
  13081. +
  13082. +static void fotg210_disable_dma(struct fotg210_ep *ep)
  13083. +{
  13084. + iowrite32(DMATFNR_DISDMA, ep->fotg210->reg + FOTG210_DMATFNR);
  13085. +}
  13086. +
  13087. +static void fotg210_wait_dma_done(struct fotg210_ep *ep)
  13088. +{
  13089. + u32 value;
  13090. +
  13091. + do {
  13092. + value = ioread32(ep->fotg210->reg + FOTG210_DISGR2);
  13093. + if ((value & DISGR2_USBRST_INT) ||
  13094. + (value & DISGR2_DMA_ERROR))
  13095. + goto dma_reset;
  13096. + } while (!(value & DISGR2_DMA_CMPLT));
  13097. +
  13098. + value &= ~DISGR2_DMA_CMPLT;
  13099. + iowrite32(value, ep->fotg210->reg + FOTG210_DISGR2);
  13100. + return;
  13101. +
  13102. +dma_reset:
  13103. + value = ioread32(ep->fotg210->reg + FOTG210_DMACPSR1);
  13104. + value |= DMACPSR1_DMA_ABORT;
  13105. + iowrite32(value, ep->fotg210->reg + FOTG210_DMACPSR1);
  13106. +
  13107. + /* reset fifo */
  13108. + if (ep->epnum) {
  13109. + value = ioread32(ep->fotg210->reg +
  13110. + FOTG210_FIBCR(ep->epnum - 1));
  13111. + value |= FIBCR_FFRST;
  13112. + iowrite32(value, ep->fotg210->reg +
  13113. + FOTG210_FIBCR(ep->epnum - 1));
  13114. + } else {
  13115. + value = ioread32(ep->fotg210->reg + FOTG210_DCFESR);
  13116. + value |= DCFESR_CX_CLR;
  13117. + iowrite32(value, ep->fotg210->reg + FOTG210_DCFESR);
  13118. + }
  13119. +}
  13120. +
  13121. +static void fotg210_start_dma(struct fotg210_ep *ep,
  13122. + struct fotg210_request *req)
  13123. +{
  13124. + struct device *dev = &ep->fotg210->gadget.dev;
  13125. + dma_addr_t d;
  13126. + u8 *buffer;
  13127. + u32 length;
  13128. +
  13129. + if (ep->epnum) {
  13130. + if (ep->dir_in) {
  13131. + buffer = req->req.buf;
  13132. + length = req->req.length;
  13133. + } else {
  13134. + buffer = req->req.buf + req->req.actual;
  13135. + length = ioread32(ep->fotg210->reg +
  13136. + FOTG210_FIBCR(ep->epnum - 1)) & FIBCR_BCFX;
  13137. + if (length > req->req.length - req->req.actual)
  13138. + length = req->req.length - req->req.actual;
  13139. + }
  13140. + } else {
  13141. + buffer = req->req.buf + req->req.actual;
  13142. + if (req->req.length - req->req.actual > ep->ep.maxpacket)
  13143. + length = ep->ep.maxpacket;
  13144. + else
  13145. + length = req->req.length - req->req.actual;
  13146. + }
  13147. +
  13148. + d = dma_map_single(dev, buffer, length,
  13149. + ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  13150. +
  13151. + if (dma_mapping_error(dev, d)) {
  13152. + pr_err("dma_mapping_error\n");
  13153. + return;
  13154. + }
  13155. +
  13156. + fotg210_enable_dma(ep, d, length);
  13157. +
  13158. + /* check if dma is done */
  13159. + fotg210_wait_dma_done(ep);
  13160. +
  13161. + fotg210_disable_dma(ep);
  13162. +
  13163. + /* update actual transfer length */
  13164. + req->req.actual += length;
  13165. +
  13166. + dma_unmap_single(dev, d, length, DMA_TO_DEVICE);
  13167. +}
  13168. +
  13169. +static void fotg210_ep0_queue(struct fotg210_ep *ep,
  13170. + struct fotg210_request *req)
  13171. +{
  13172. + if (!req->req.length) {
  13173. + fotg210_done(ep, req, 0);
  13174. + return;
  13175. + }
  13176. + if (ep->dir_in) { /* if IN */
  13177. + fotg210_start_dma(ep, req);
  13178. + if (req->req.length == req->req.actual)
  13179. + fotg210_done(ep, req, 0);
  13180. + } else { /* OUT */
  13181. + u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR0);
  13182. +
  13183. + value &= ~DMISGR0_MCX_OUT_INT;
  13184. + iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR0);
  13185. + }
  13186. +}
  13187. +
  13188. +static int fotg210_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
  13189. + gfp_t gfp_flags)
  13190. +{
  13191. + struct fotg210_ep *ep;
  13192. + struct fotg210_request *req;
  13193. + unsigned long flags;
  13194. + int request = 0;
  13195. +
  13196. + ep = container_of(_ep, struct fotg210_ep, ep);
  13197. + req = container_of(_req, struct fotg210_request, req);
  13198. +
  13199. + if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
  13200. + return -ESHUTDOWN;
  13201. +
  13202. + spin_lock_irqsave(&ep->fotg210->lock, flags);
  13203. +
  13204. + if (list_empty(&ep->queue))
  13205. + request = 1;
  13206. +
  13207. + list_add_tail(&req->queue, &ep->queue);
  13208. +
  13209. + req->req.actual = 0;
  13210. + req->req.status = -EINPROGRESS;
  13211. +
  13212. + if (!ep->epnum) /* ep0 */
  13213. + fotg210_ep0_queue(ep, req);
  13214. + else if (request && !ep->stall)
  13215. + fotg210_enable_fifo_int(ep);
  13216. +
  13217. + spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  13218. +
  13219. + return 0;
  13220. +}
  13221. +
  13222. +static int fotg210_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  13223. +{
  13224. + struct fotg210_ep *ep;
  13225. + struct fotg210_request *req;
  13226. + unsigned long flags;
  13227. +
  13228. + ep = container_of(_ep, struct fotg210_ep, ep);
  13229. + req = container_of(_req, struct fotg210_request, req);
  13230. +
  13231. + spin_lock_irqsave(&ep->fotg210->lock, flags);
  13232. + if (!list_empty(&ep->queue))
  13233. + fotg210_done(ep, req, -ECONNRESET);
  13234. + spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  13235. +
  13236. + return 0;
  13237. +}
  13238. +
  13239. +static void fotg210_set_epnstall(struct fotg210_ep *ep)
  13240. +{
  13241. + struct fotg210_udc *fotg210 = ep->fotg210;
  13242. + u32 value;
  13243. + void __iomem *reg;
  13244. +
  13245. + /* check if IN FIFO is empty before stall */
  13246. + if (ep->dir_in) {
  13247. + do {
  13248. + value = ioread32(fotg210->reg + FOTG210_DCFESR);
  13249. + } while (!(value & DCFESR_FIFO_EMPTY(ep->epnum - 1)));
  13250. + }
  13251. +
  13252. + reg = (ep->dir_in) ?
  13253. + fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  13254. + fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  13255. + value = ioread32(reg);
  13256. + value |= INOUTEPMPSR_STL_EP;
  13257. + iowrite32(value, reg);
  13258. +}
  13259. +
  13260. +static void fotg210_clear_epnstall(struct fotg210_ep *ep)
  13261. +{
  13262. + struct fotg210_udc *fotg210 = ep->fotg210;
  13263. + u32 value;
  13264. + void __iomem *reg;
  13265. +
  13266. + reg = (ep->dir_in) ?
  13267. + fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  13268. + fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  13269. + value = ioread32(reg);
  13270. + value &= ~INOUTEPMPSR_STL_EP;
  13271. + iowrite32(value, reg);
  13272. +}
  13273. +
  13274. +static int fotg210_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
  13275. +{
  13276. + struct fotg210_ep *ep;
  13277. + struct fotg210_udc *fotg210;
  13278. + unsigned long flags;
  13279. +
  13280. + ep = container_of(_ep, struct fotg210_ep, ep);
  13281. +
  13282. + fotg210 = ep->fotg210;
  13283. +
  13284. + spin_lock_irqsave(&ep->fotg210->lock, flags);
  13285. +
  13286. + if (value) {
  13287. + fotg210_set_epnstall(ep);
  13288. + ep->stall = 1;
  13289. + if (wedge)
  13290. + ep->wedged = 1;
  13291. + } else {
  13292. + fotg210_reset_tseq(fotg210, ep->epnum);
  13293. + fotg210_clear_epnstall(ep);
  13294. + ep->stall = 0;
  13295. + ep->wedged = 0;
  13296. + if (!list_empty(&ep->queue))
  13297. + fotg210_enable_fifo_int(ep);
  13298. + }
  13299. +
  13300. + spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  13301. + return 0;
  13302. +}
  13303. +
  13304. +static int fotg210_ep_set_halt(struct usb_ep *_ep, int value)
  13305. +{
  13306. + return fotg210_set_halt_and_wedge(_ep, value, 0);
  13307. +}
  13308. +
  13309. +static int fotg210_ep_set_wedge(struct usb_ep *_ep)
  13310. +{
  13311. + return fotg210_set_halt_and_wedge(_ep, 1, 1);
  13312. +}
  13313. +
  13314. +static void fotg210_ep_fifo_flush(struct usb_ep *_ep)
  13315. +{
  13316. +}
  13317. +
  13318. +static const struct usb_ep_ops fotg210_ep_ops = {
  13319. + .enable = fotg210_ep_enable,
  13320. + .disable = fotg210_ep_disable,
  13321. +
  13322. + .alloc_request = fotg210_ep_alloc_request,
  13323. + .free_request = fotg210_ep_free_request,
  13324. +
  13325. + .queue = fotg210_ep_queue,
  13326. + .dequeue = fotg210_ep_dequeue,
  13327. +
  13328. + .set_halt = fotg210_ep_set_halt,
  13329. + .fifo_flush = fotg210_ep_fifo_flush,
  13330. + .set_wedge = fotg210_ep_set_wedge,
  13331. +};
  13332. +
  13333. +static void fotg210_clear_tx0byte(struct fotg210_udc *fotg210)
  13334. +{
  13335. + u32 value = ioread32(fotg210->reg + FOTG210_TX0BYTE);
  13336. +
  13337. + value &= ~(TX0BYTE_EP1 | TX0BYTE_EP2 | TX0BYTE_EP3
  13338. + | TX0BYTE_EP4);
  13339. + iowrite32(value, fotg210->reg + FOTG210_TX0BYTE);
  13340. +}
  13341. +
  13342. +static void fotg210_clear_rx0byte(struct fotg210_udc *fotg210)
  13343. +{
  13344. + u32 value = ioread32(fotg210->reg + FOTG210_RX0BYTE);
  13345. +
  13346. + value &= ~(RX0BYTE_EP1 | RX0BYTE_EP2 | RX0BYTE_EP3
  13347. + | RX0BYTE_EP4);
  13348. + iowrite32(value, fotg210->reg + FOTG210_RX0BYTE);
  13349. +}
  13350. +
  13351. +/* read 8-byte setup packet only */
  13352. +static void fotg210_rdsetupp(struct fotg210_udc *fotg210,
  13353. + u8 *buffer)
  13354. +{
  13355. + int i = 0;
  13356. + u8 *tmp = buffer;
  13357. + u32 data;
  13358. + u32 length = 8;
  13359. +
  13360. + iowrite32(DMATFNR_ACC_CXF, fotg210->reg + FOTG210_DMATFNR);
  13361. +
  13362. + for (i = (length >> 2); i > 0; i--) {
  13363. + data = ioread32(fotg210->reg + FOTG210_CXPORT);
  13364. + *tmp = data & 0xFF;
  13365. + *(tmp + 1) = (data >> 8) & 0xFF;
  13366. + *(tmp + 2) = (data >> 16) & 0xFF;
  13367. + *(tmp + 3) = (data >> 24) & 0xFF;
  13368. + tmp = tmp + 4;
  13369. + }
  13370. +
  13371. + switch (length % 4) {
  13372. + case 1:
  13373. + data = ioread32(fotg210->reg + FOTG210_CXPORT);
  13374. + *tmp = data & 0xFF;
  13375. + break;
  13376. + case 2:
  13377. + data = ioread32(fotg210->reg + FOTG210_CXPORT);
  13378. + *tmp = data & 0xFF;
  13379. + *(tmp + 1) = (data >> 8) & 0xFF;
  13380. + break;
  13381. + case 3:
  13382. + data = ioread32(fotg210->reg + FOTG210_CXPORT);
  13383. + *tmp = data & 0xFF;
  13384. + *(tmp + 1) = (data >> 8) & 0xFF;
  13385. + *(tmp + 2) = (data >> 16) & 0xFF;
  13386. + break;
  13387. + default:
  13388. + break;
  13389. + }
  13390. +
  13391. + iowrite32(DMATFNR_DISDMA, fotg210->reg + FOTG210_DMATFNR);
  13392. +}
  13393. +
  13394. +static void fotg210_set_configuration(struct fotg210_udc *fotg210)
  13395. +{
  13396. + u32 value = ioread32(fotg210->reg + FOTG210_DAR);
  13397. +
  13398. + value |= DAR_AFT_CONF;
  13399. + iowrite32(value, fotg210->reg + FOTG210_DAR);
  13400. +}
  13401. +
  13402. +static void fotg210_set_dev_addr(struct fotg210_udc *fotg210, u32 addr)
  13403. +{
  13404. + u32 value = ioread32(fotg210->reg + FOTG210_DAR);
  13405. +
  13406. + value |= (addr & 0x7F);
  13407. + iowrite32(value, fotg210->reg + FOTG210_DAR);
  13408. +}
  13409. +
  13410. +static void fotg210_set_cxstall(struct fotg210_udc *fotg210)
  13411. +{
  13412. + u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
  13413. +
  13414. + value |= DCFESR_CX_STL;
  13415. + iowrite32(value, fotg210->reg + FOTG210_DCFESR);
  13416. +}
  13417. +
  13418. +static void fotg210_request_error(struct fotg210_udc *fotg210)
  13419. +{
  13420. + fotg210_set_cxstall(fotg210);
  13421. + pr_err("request error!!\n");
  13422. +}
  13423. +
  13424. +static void fotg210_set_address(struct fotg210_udc *fotg210,
  13425. + struct usb_ctrlrequest *ctrl)
  13426. +{
  13427. + if (le16_to_cpu(ctrl->wValue) >= 0x0100) {
  13428. + fotg210_request_error(fotg210);
  13429. + } else {
  13430. + fotg210_set_dev_addr(fotg210, le16_to_cpu(ctrl->wValue));
  13431. + fotg210_set_cxdone(fotg210);
  13432. + }
  13433. +}
  13434. +
  13435. +static void fotg210_set_feature(struct fotg210_udc *fotg210,
  13436. + struct usb_ctrlrequest *ctrl)
  13437. +{
  13438. + switch (ctrl->bRequestType & USB_RECIP_MASK) {
  13439. + case USB_RECIP_DEVICE:
  13440. + fotg210_set_cxdone(fotg210);
  13441. + break;
  13442. + case USB_RECIP_INTERFACE:
  13443. + fotg210_set_cxdone(fotg210);
  13444. + break;
  13445. + case USB_RECIP_ENDPOINT: {
  13446. + u8 epnum;
  13447. + epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  13448. + if (epnum)
  13449. + fotg210_set_epnstall(fotg210->ep[epnum]);
  13450. + else
  13451. + fotg210_set_cxstall(fotg210);
  13452. + fotg210_set_cxdone(fotg210);
  13453. + }
  13454. + break;
  13455. + default:
  13456. + fotg210_request_error(fotg210);
  13457. + break;
  13458. + }
  13459. +}
  13460. +
  13461. +static void fotg210_clear_feature(struct fotg210_udc *fotg210,
  13462. + struct usb_ctrlrequest *ctrl)
  13463. +{
  13464. + struct fotg210_ep *ep =
  13465. + fotg210->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
  13466. +
  13467. + switch (ctrl->bRequestType & USB_RECIP_MASK) {
  13468. + case USB_RECIP_DEVICE:
  13469. + fotg210_set_cxdone(fotg210);
  13470. + break;
  13471. + case USB_RECIP_INTERFACE:
  13472. + fotg210_set_cxdone(fotg210);
  13473. + break;
  13474. + case USB_RECIP_ENDPOINT:
  13475. + if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
  13476. + if (ep->wedged) {
  13477. + fotg210_set_cxdone(fotg210);
  13478. + break;
  13479. + }
  13480. + if (ep->stall)
  13481. + fotg210_set_halt_and_wedge(&ep->ep, 0, 0);
  13482. + }
  13483. + fotg210_set_cxdone(fotg210);
  13484. + break;
  13485. + default:
  13486. + fotg210_request_error(fotg210);
  13487. + break;
  13488. + }
  13489. +}
  13490. +
  13491. +static int fotg210_is_epnstall(struct fotg210_ep *ep)
  13492. +{
  13493. + struct fotg210_udc *fotg210 = ep->fotg210;
  13494. + u32 value;
  13495. + void __iomem *reg;
  13496. +
  13497. + reg = (ep->dir_in) ?
  13498. + fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  13499. + fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  13500. + value = ioread32(reg);
  13501. + return value & INOUTEPMPSR_STL_EP ? 1 : 0;
  13502. +}
  13503. +
  13504. +/* For EP0 requests triggered by this driver (currently GET_STATUS response) */
  13505. +static void fotg210_ep0_complete(struct usb_ep *_ep, struct usb_request *req)
  13506. +{
  13507. + struct fotg210_ep *ep;
  13508. + struct fotg210_udc *fotg210;
  13509. +
  13510. + ep = container_of(_ep, struct fotg210_ep, ep);
  13511. + fotg210 = ep->fotg210;
  13512. +
  13513. + if (req->status || req->actual != req->length) {
  13514. + dev_warn(&fotg210->gadget.dev, "EP0 request failed: %d\n", req->status);
  13515. + }
  13516. +}
  13517. +
  13518. +static void fotg210_get_status(struct fotg210_udc *fotg210,
  13519. + struct usb_ctrlrequest *ctrl)
  13520. +{
  13521. + u8 epnum;
  13522. +
  13523. + switch (ctrl->bRequestType & USB_RECIP_MASK) {
  13524. + case USB_RECIP_DEVICE:
  13525. + fotg210->ep0_data = cpu_to_le16(1 << USB_DEVICE_SELF_POWERED);
  13526. + break;
  13527. + case USB_RECIP_INTERFACE:
  13528. + fotg210->ep0_data = cpu_to_le16(0);
  13529. + break;
  13530. + case USB_RECIP_ENDPOINT:
  13531. + epnum = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
  13532. + if (epnum)
  13533. + fotg210->ep0_data =
  13534. + cpu_to_le16(fotg210_is_epnstall(fotg210->ep[epnum])
  13535. + << USB_ENDPOINT_HALT);
  13536. + else
  13537. + fotg210_request_error(fotg210);
  13538. + break;
  13539. +
  13540. + default:
  13541. + fotg210_request_error(fotg210);
  13542. + return; /* exit */
  13543. + }
  13544. +
  13545. + fotg210->ep0_req->buf = &fotg210->ep0_data;
  13546. + fotg210->ep0_req->length = 2;
  13547. +
  13548. + spin_unlock(&fotg210->lock);
  13549. + fotg210_ep_queue(fotg210->gadget.ep0, fotg210->ep0_req, GFP_ATOMIC);
  13550. + spin_lock(&fotg210->lock);
  13551. +}
  13552. +
  13553. +static int fotg210_setup_packet(struct fotg210_udc *fotg210,
  13554. + struct usb_ctrlrequest *ctrl)
  13555. +{
  13556. + u8 *p = (u8 *)ctrl;
  13557. + u8 ret = 0;
  13558. +
  13559. + fotg210_rdsetupp(fotg210, p);
  13560. +
  13561. + fotg210->ep[0]->dir_in = ctrl->bRequestType & USB_DIR_IN;
  13562. +
  13563. + if (fotg210->gadget.speed == USB_SPEED_UNKNOWN) {
  13564. + u32 value = ioread32(fotg210->reg + FOTG210_DMCR);
  13565. + fotg210->gadget.speed = value & DMCR_HS_EN ?
  13566. + USB_SPEED_HIGH : USB_SPEED_FULL;
  13567. + }
  13568. +
  13569. + /* check request */
  13570. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  13571. + switch (ctrl->bRequest) {
  13572. + case USB_REQ_GET_STATUS:
  13573. + fotg210_get_status(fotg210, ctrl);
  13574. + break;
  13575. + case USB_REQ_CLEAR_FEATURE:
  13576. + fotg210_clear_feature(fotg210, ctrl);
  13577. + break;
  13578. + case USB_REQ_SET_FEATURE:
  13579. + fotg210_set_feature(fotg210, ctrl);
  13580. + break;
  13581. + case USB_REQ_SET_ADDRESS:
  13582. + fotg210_set_address(fotg210, ctrl);
  13583. + break;
  13584. + case USB_REQ_SET_CONFIGURATION:
  13585. + fotg210_set_configuration(fotg210);
  13586. + ret = 1;
  13587. + break;
  13588. + default:
  13589. + ret = 1;
  13590. + break;
  13591. + }
  13592. + } else {
  13593. + ret = 1;
  13594. + }
  13595. +
  13596. + return ret;
  13597. +}
  13598. +
  13599. +static void fotg210_ep0out(struct fotg210_udc *fotg210)
  13600. +{
  13601. + struct fotg210_ep *ep = fotg210->ep[0];
  13602. +
  13603. + if (!list_empty(&ep->queue) && !ep->dir_in) {
  13604. + struct fotg210_request *req;
  13605. +
  13606. + req = list_first_entry(&ep->queue,
  13607. + struct fotg210_request, queue);
  13608. +
  13609. + if (req->req.length)
  13610. + fotg210_start_dma(ep, req);
  13611. +
  13612. + if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
  13613. + fotg210_done(ep, req, 0);
  13614. + } else {
  13615. + pr_err("%s : empty queue\n", __func__);
  13616. + }
  13617. +}
  13618. +
  13619. +static void fotg210_ep0in(struct fotg210_udc *fotg210)
  13620. +{
  13621. + struct fotg210_ep *ep = fotg210->ep[0];
  13622. +
  13623. + if ((!list_empty(&ep->queue)) && (ep->dir_in)) {
  13624. + struct fotg210_request *req;
  13625. +
  13626. + req = list_entry(ep->queue.next,
  13627. + struct fotg210_request, queue);
  13628. +
  13629. + if (req->req.length)
  13630. + fotg210_start_dma(ep, req);
  13631. +
  13632. + if (req->req.actual == req->req.length)
  13633. + fotg210_done(ep, req, 0);
  13634. + } else {
  13635. + fotg210_set_cxdone(fotg210);
  13636. + }
  13637. +}
  13638. +
  13639. +static void fotg210_clear_comabt_int(struct fotg210_udc *fotg210)
  13640. +{
  13641. + u32 value = ioread32(fotg210->reg + FOTG210_DISGR0);
  13642. +
  13643. + value &= ~DISGR0_CX_COMABT_INT;
  13644. + iowrite32(value, fotg210->reg + FOTG210_DISGR0);
  13645. +}
  13646. +
  13647. +static void fotg210_in_fifo_handler(struct fotg210_ep *ep)
  13648. +{
  13649. + struct fotg210_request *req = list_entry(ep->queue.next,
  13650. + struct fotg210_request, queue);
  13651. +
  13652. + if (req->req.length)
  13653. + fotg210_start_dma(ep, req);
  13654. + fotg210_done(ep, req, 0);
  13655. +}
  13656. +
  13657. +static void fotg210_out_fifo_handler(struct fotg210_ep *ep)
  13658. +{
  13659. + struct fotg210_request *req = list_entry(ep->queue.next,
  13660. + struct fotg210_request, queue);
  13661. + int disgr1 = ioread32(ep->fotg210->reg + FOTG210_DISGR1);
  13662. +
  13663. + fotg210_start_dma(ep, req);
  13664. +
  13665. + /* Complete the request when it's full or a short packet arrived.
  13666. + * Like other drivers, short_not_ok isn't handled.
  13667. + */
  13668. +
  13669. + if (req->req.length == req->req.actual ||
  13670. + (disgr1 & DISGR1_SPK_INT(ep->epnum - 1)))
  13671. + fotg210_done(ep, req, 0);
  13672. +}
  13673. +
  13674. +static irqreturn_t fotg210_irq(int irq, void *_fotg210)
  13675. +{
  13676. + struct fotg210_udc *fotg210 = _fotg210;
  13677. + u32 int_grp = ioread32(fotg210->reg + FOTG210_DIGR);
  13678. + u32 int_msk = ioread32(fotg210->reg + FOTG210_DMIGR);
  13679. +
  13680. + int_grp &= ~int_msk;
  13681. +
  13682. + spin_lock(&fotg210->lock);
  13683. +
  13684. + if (int_grp & DIGR_INT_G2) {
  13685. + void __iomem *reg = fotg210->reg + FOTG210_DISGR2;
  13686. + u32 int_grp2 = ioread32(reg);
  13687. + u32 int_msk2 = ioread32(fotg210->reg + FOTG210_DMISGR2);
  13688. + u32 value;
  13689. +
  13690. + int_grp2 &= ~int_msk2;
  13691. +
  13692. + if (int_grp2 & DISGR2_USBRST_INT) {
  13693. + usb_gadget_udc_reset(&fotg210->gadget,
  13694. + fotg210->driver);
  13695. + value = ioread32(reg);
  13696. + value &= ~DISGR2_USBRST_INT;
  13697. + iowrite32(value, reg);
  13698. + pr_info("fotg210 udc reset\n");
  13699. + }
  13700. + if (int_grp2 & DISGR2_SUSP_INT) {
  13701. + value = ioread32(reg);
  13702. + value &= ~DISGR2_SUSP_INT;
  13703. + iowrite32(value, reg);
  13704. + pr_info("fotg210 udc suspend\n");
  13705. + }
  13706. + if (int_grp2 & DISGR2_RESM_INT) {
  13707. + value = ioread32(reg);
  13708. + value &= ~DISGR2_RESM_INT;
  13709. + iowrite32(value, reg);
  13710. + pr_info("fotg210 udc resume\n");
  13711. + }
  13712. + if (int_grp2 & DISGR2_ISO_SEQ_ERR_INT) {
  13713. + value = ioread32(reg);
  13714. + value &= ~DISGR2_ISO_SEQ_ERR_INT;
  13715. + iowrite32(value, reg);
  13716. + pr_info("fotg210 iso sequence error\n");
  13717. + }
  13718. + if (int_grp2 & DISGR2_ISO_SEQ_ABORT_INT) {
  13719. + value = ioread32(reg);
  13720. + value &= ~DISGR2_ISO_SEQ_ABORT_INT;
  13721. + iowrite32(value, reg);
  13722. + pr_info("fotg210 iso sequence abort\n");
  13723. + }
  13724. + if (int_grp2 & DISGR2_TX0BYTE_INT) {
  13725. + fotg210_clear_tx0byte(fotg210);
  13726. + value = ioread32(reg);
  13727. + value &= ~DISGR2_TX0BYTE_INT;
  13728. + iowrite32(value, reg);
  13729. + pr_info("fotg210 transferred 0 byte\n");
  13730. + }
  13731. + if (int_grp2 & DISGR2_RX0BYTE_INT) {
  13732. + fotg210_clear_rx0byte(fotg210);
  13733. + value = ioread32(reg);
  13734. + value &= ~DISGR2_RX0BYTE_INT;
  13735. + iowrite32(value, reg);
  13736. + pr_info("fotg210 received 0 byte\n");
  13737. + }
  13738. + if (int_grp2 & DISGR2_DMA_ERROR) {
  13739. + value = ioread32(reg);
  13740. + value &= ~DISGR2_DMA_ERROR;
  13741. + iowrite32(value, reg);
  13742. + }
  13743. + }
  13744. +
  13745. + if (int_grp & DIGR_INT_G0) {
  13746. + void __iomem *reg = fotg210->reg + FOTG210_DISGR0;
  13747. + u32 int_grp0 = ioread32(reg);
  13748. + u32 int_msk0 = ioread32(fotg210->reg + FOTG210_DMISGR0);
  13749. + struct usb_ctrlrequest ctrl;
  13750. +
  13751. + int_grp0 &= ~int_msk0;
  13752. +
  13753. + /* the highest priority in this source register */
  13754. + if (int_grp0 & DISGR0_CX_COMABT_INT) {
  13755. + fotg210_clear_comabt_int(fotg210);
  13756. + pr_info("fotg210 CX command abort\n");
  13757. + }
  13758. +
  13759. + if (int_grp0 & DISGR0_CX_SETUP_INT) {
  13760. + if (fotg210_setup_packet(fotg210, &ctrl)) {
  13761. + spin_unlock(&fotg210->lock);
  13762. + if (fotg210->driver->setup(&fotg210->gadget,
  13763. + &ctrl) < 0)
  13764. + fotg210_set_cxstall(fotg210);
  13765. + spin_lock(&fotg210->lock);
  13766. + }
  13767. + }
  13768. + if (int_grp0 & DISGR0_CX_COMEND_INT)
  13769. + pr_info("fotg210 cmd end\n");
  13770. +
  13771. + if (int_grp0 & DISGR0_CX_IN_INT)
  13772. + fotg210_ep0in(fotg210);
  13773. +
  13774. + if (int_grp0 & DISGR0_CX_OUT_INT)
  13775. + fotg210_ep0out(fotg210);
  13776. +
  13777. + if (int_grp0 & DISGR0_CX_COMFAIL_INT) {
  13778. + fotg210_set_cxstall(fotg210);
  13779. + pr_info("fotg210 ep0 fail\n");
  13780. + }
  13781. + }
  13782. +
  13783. + if (int_grp & DIGR_INT_G1) {
  13784. + void __iomem *reg = fotg210->reg + FOTG210_DISGR1;
  13785. + u32 int_grp1 = ioread32(reg);
  13786. + u32 int_msk1 = ioread32(fotg210->reg + FOTG210_DMISGR1);
  13787. + int fifo;
  13788. +
  13789. + int_grp1 &= ~int_msk1;
  13790. +
  13791. + for (fifo = 0; fifo < FOTG210_MAX_FIFO_NUM; fifo++) {
  13792. + if (int_grp1 & DISGR1_IN_INT(fifo))
  13793. + fotg210_in_fifo_handler(fotg210->ep[fifo + 1]);
  13794. +
  13795. + if ((int_grp1 & DISGR1_OUT_INT(fifo)) ||
  13796. + (int_grp1 & DISGR1_SPK_INT(fifo)))
  13797. + fotg210_out_fifo_handler(fotg210->ep[fifo + 1]);
  13798. + }
  13799. + }
  13800. +
  13801. + spin_unlock(&fotg210->lock);
  13802. +
  13803. + return IRQ_HANDLED;
  13804. +}
  13805. +
  13806. +static void fotg210_disable_unplug(struct fotg210_udc *fotg210)
  13807. +{
  13808. + u32 reg = ioread32(fotg210->reg + FOTG210_PHYTMSR);
  13809. +
  13810. + reg &= ~PHYTMSR_UNPLUG;
  13811. + iowrite32(reg, fotg210->reg + FOTG210_PHYTMSR);
  13812. +}
  13813. +
  13814. +static int fotg210_udc_start(struct usb_gadget *g,
  13815. + struct usb_gadget_driver *driver)
  13816. +{
  13817. + struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
  13818. + u32 value;
  13819. +
  13820. + /* hook up the driver */
  13821. + fotg210->driver = driver;
  13822. +
  13823. + /* enable device global interrupt */
  13824. + value = ioread32(fotg210->reg + FOTG210_DMCR);
  13825. + value |= DMCR_GLINT_EN;
  13826. + iowrite32(value, fotg210->reg + FOTG210_DMCR);
  13827. +
  13828. + return 0;
  13829. +}
  13830. +
  13831. +static void fotg210_init(struct fotg210_udc *fotg210)
  13832. +{
  13833. + u32 value;
  13834. +
  13835. + /* disable global interrupt and set int polarity to active high */
  13836. + iowrite32(GMIR_MHC_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
  13837. + fotg210->reg + FOTG210_GMIR);
  13838. +
  13839. + /* disable device global interrupt */
  13840. + value = ioread32(fotg210->reg + FOTG210_DMCR);
  13841. + value &= ~DMCR_GLINT_EN;
  13842. + iowrite32(value, fotg210->reg + FOTG210_DMCR);
  13843. +
  13844. + /* enable only grp2 irqs we handle */
  13845. + iowrite32(~(DISGR2_DMA_ERROR | DISGR2_RX0BYTE_INT | DISGR2_TX0BYTE_INT
  13846. + | DISGR2_ISO_SEQ_ABORT_INT | DISGR2_ISO_SEQ_ERR_INT
  13847. + | DISGR2_RESM_INT | DISGR2_SUSP_INT | DISGR2_USBRST_INT),
  13848. + fotg210->reg + FOTG210_DMISGR2);
  13849. +
  13850. + /* disable all fifo interrupt */
  13851. + iowrite32(~(u32)0, fotg210->reg + FOTG210_DMISGR1);
  13852. +
  13853. + /* disable cmd end */
  13854. + value = ioread32(fotg210->reg + FOTG210_DMISGR0);
  13855. + value |= DMISGR0_MCX_COMEND;
  13856. + iowrite32(value, fotg210->reg + FOTG210_DMISGR0);
  13857. +}
  13858. +
  13859. +static int fotg210_udc_stop(struct usb_gadget *g)
  13860. +{
  13861. + struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
  13862. + unsigned long flags;
  13863. +
  13864. + spin_lock_irqsave(&fotg210->lock, flags);
  13865. +
  13866. + fotg210_init(fotg210);
  13867. + fotg210->driver = NULL;
  13868. +
  13869. + spin_unlock_irqrestore(&fotg210->lock, flags);
  13870. +
  13871. + return 0;
  13872. +}
  13873. +
  13874. +static const struct usb_gadget_ops fotg210_gadget_ops = {
  13875. + .udc_start = fotg210_udc_start,
  13876. + .udc_stop = fotg210_udc_stop,
  13877. +};
  13878. +
  13879. +static int fotg210_udc_remove(struct platform_device *pdev)
  13880. +{
  13881. + struct fotg210_udc *fotg210 = platform_get_drvdata(pdev);
  13882. + int i;
  13883. +
  13884. + usb_del_gadget_udc(&fotg210->gadget);
  13885. + iounmap(fotg210->reg);
  13886. + free_irq(platform_get_irq(pdev, 0), fotg210);
  13887. +
  13888. + fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
  13889. + for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
  13890. + kfree(fotg210->ep[i]);
  13891. + kfree(fotg210);
  13892. +
  13893. + return 0;
  13894. +}
  13895. +
  13896. +static int fotg210_udc_probe(struct platform_device *pdev)
  13897. +{
  13898. + struct resource *res, *ires;
  13899. + struct fotg210_udc *fotg210 = NULL;
  13900. + struct fotg210_ep *_ep[FOTG210_MAX_NUM_EP];
  13901. + int ret = 0;
  13902. + int i;
  13903. +
  13904. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  13905. + if (!res) {
  13906. + pr_err("platform_get_resource error.\n");
  13907. + return -ENODEV;
  13908. + }
  13909. +
  13910. + ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  13911. + if (!ires) {
  13912. + pr_err("platform_get_resource IORESOURCE_IRQ error.\n");
  13913. + return -ENODEV;
  13914. + }
  13915. +
  13916. + ret = -ENOMEM;
  13917. +
  13918. + /* initialize udc */
  13919. + fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL);
  13920. + if (fotg210 == NULL)
  13921. + goto err;
  13922. +
  13923. + for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
  13924. + _ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL);
  13925. + if (_ep[i] == NULL)
  13926. + goto err_alloc;
  13927. + fotg210->ep[i] = _ep[i];
  13928. + }
  13929. +
  13930. + fotg210->reg = ioremap(res->start, resource_size(res));
  13931. + if (fotg210->reg == NULL) {
  13932. + pr_err("ioremap error.\n");
  13933. + goto err_alloc;
  13934. + }
  13935. +
  13936. + spin_lock_init(&fotg210->lock);
  13937. +
  13938. + platform_set_drvdata(pdev, fotg210);
  13939. +
  13940. + fotg210->gadget.ops = &fotg210_gadget_ops;
  13941. +
  13942. + fotg210->gadget.max_speed = USB_SPEED_HIGH;
  13943. + fotg210->gadget.dev.parent = &pdev->dev;
  13944. + fotg210->gadget.dev.dma_mask = pdev->dev.dma_mask;
  13945. + fotg210->gadget.name = udc_name;
  13946. +
  13947. + INIT_LIST_HEAD(&fotg210->gadget.ep_list);
  13948. +
  13949. + for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
  13950. + struct fotg210_ep *ep = fotg210->ep[i];
  13951. +
  13952. + if (i) {
  13953. + INIT_LIST_HEAD(&fotg210->ep[i]->ep.ep_list);
  13954. + list_add_tail(&fotg210->ep[i]->ep.ep_list,
  13955. + &fotg210->gadget.ep_list);
  13956. + }
  13957. + ep->fotg210 = fotg210;
  13958. + INIT_LIST_HEAD(&ep->queue);
  13959. + ep->ep.name = fotg210_ep_name[i];
  13960. + ep->ep.ops = &fotg210_ep_ops;
  13961. + usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
  13962. +
  13963. + if (i == 0) {
  13964. + ep->ep.caps.type_control = true;
  13965. + } else {
  13966. + ep->ep.caps.type_iso = true;
  13967. + ep->ep.caps.type_bulk = true;
  13968. + ep->ep.caps.type_int = true;
  13969. + }
  13970. +
  13971. + ep->ep.caps.dir_in = true;
  13972. + ep->ep.caps.dir_out = true;
  13973. + }
  13974. + usb_ep_set_maxpacket_limit(&fotg210->ep[0]->ep, 0x40);
  13975. + fotg210->gadget.ep0 = &fotg210->ep[0]->ep;
  13976. + INIT_LIST_HEAD(&fotg210->gadget.ep0->ep_list);
  13977. +
  13978. + fotg210->ep0_req = fotg210_ep_alloc_request(&fotg210->ep[0]->ep,
  13979. + GFP_KERNEL);
  13980. + if (fotg210->ep0_req == NULL)
  13981. + goto err_map;
  13982. +
  13983. + fotg210->ep0_req->complete = fotg210_ep0_complete;
  13984. +
  13985. + fotg210_init(fotg210);
  13986. +
  13987. + fotg210_disable_unplug(fotg210);
  13988. +
  13989. + ret = request_irq(ires->start, fotg210_irq, IRQF_SHARED,
  13990. + udc_name, fotg210);
  13991. + if (ret < 0) {
  13992. + pr_err("request_irq error (%d)\n", ret);
  13993. + goto err_req;
  13994. + }
  13995. +
  13996. + ret = usb_add_gadget_udc(&pdev->dev, &fotg210->gadget);
  13997. + if (ret)
  13998. + goto err_add_udc;
  13999. +
  14000. + dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
  14001. +
  14002. + return 0;
  14003. +
  14004. +err_add_udc:
  14005. + free_irq(ires->start, fotg210);
  14006. +
  14007. +err_req:
  14008. + fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
  14009. +
  14010. +err_map:
  14011. + iounmap(fotg210->reg);
  14012. +
  14013. +err_alloc:
  14014. + for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
  14015. + kfree(fotg210->ep[i]);
  14016. + kfree(fotg210);
  14017. +
  14018. +err:
  14019. + return ret;
  14020. +}
  14021. +
  14022. +static struct platform_driver fotg210_driver = {
  14023. + .driver = {
  14024. + .name = udc_name,
  14025. + },
  14026. + .probe = fotg210_udc_probe,
  14027. + .remove = fotg210_udc_remove,
  14028. +};
  14029. +
  14030. +module_platform_driver(fotg210_driver);
  14031. +
  14032. +MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang <[email protected]>");
  14033. +MODULE_LICENSE("GPL");
  14034. +MODULE_DESCRIPTION(DRIVER_DESC);
  14035. --- a/drivers/usb/gadget/udc/Kconfig
  14036. +++ b/drivers/usb/gadget/udc/Kconfig
  14037. @@ -108,17 +108,6 @@ config USB_FUSB300
  14038. help
  14039. Faraday usb device controller FUSB300 driver
  14040. -config USB_FOTG210_UDC
  14041. - depends on HAS_DMA
  14042. - tristate "Faraday FOTG210 USB Peripheral Controller"
  14043. - help
  14044. - Faraday USB2.0 OTG controller which can be configured as
  14045. - high speed or full speed USB device. This driver supppors
  14046. - Bulk Transfer so far.
  14047. -
  14048. - Say "y" to link the driver statically, or "m" to build a
  14049. - dynamically linked module called "fotg210_udc".
  14050. -
  14051. config USB_GR_UDC
  14052. tristate "Aeroflex Gaisler GRUSBDC USB Peripheral Controller Driver"
  14053. depends on HAS_DMA
  14054. --- a/drivers/usb/gadget/udc/Makefile
  14055. +++ b/drivers/usb/gadget/udc/Makefile
  14056. @@ -34,7 +34,6 @@ obj-$(CONFIG_USB_EG20T) += pch_udc.o
  14057. obj-$(CONFIG_USB_MV_UDC) += mv_udc.o
  14058. mv_udc-y := mv_udc_core.o
  14059. obj-$(CONFIG_USB_FUSB300) += fusb300_udc.o
  14060. -obj-$(CONFIG_USB_FOTG210_UDC) += fotg210-udc.o
  14061. obj-$(CONFIG_USB_MV_U3D) += mv_u3d_core.o
  14062. obj-$(CONFIG_USB_GR_UDC) += gr_udc.o
  14063. obj-$(CONFIG_USB_GADGET_XILINX) += udc-xilinx.o
  14064. --- a/drivers/usb/host/Kconfig
  14065. +++ b/drivers/usb/host/Kconfig
  14066. @@ -389,17 +389,6 @@ config USB_ISP1362_HCD
  14067. To compile this driver as a module, choose M here: the
  14068. module will be called isp1362-hcd.
  14069. -config USB_FOTG210_HCD
  14070. - tristate "FOTG210 HCD support"
  14071. - depends on USB && HAS_DMA && HAS_IOMEM
  14072. - help
  14073. - Faraday FOTG210 is an OTG controller which can be configured as
  14074. - an USB2.0 host. It is designed to meet USB2.0 EHCI specification
  14075. - with minor modification.
  14076. -
  14077. - To compile this driver as a module, choose M here: the
  14078. - module will be called fotg210-hcd.
  14079. -
  14080. config USB_MAX3421_HCD
  14081. tristate "MAX3421 HCD (USB-over-SPI) support"
  14082. depends on USB && SPI
  14083. --- a/drivers/usb/host/Makefile
  14084. +++ b/drivers/usb/host/Makefile
  14085. @@ -84,6 +84,5 @@ obj-$(CONFIG_USB_EHCI_FSL) += ehci-fsl.o
  14086. obj-$(CONFIG_USB_EHCI_MV) += ehci-mv.o
  14087. obj-$(CONFIG_USB_HCD_BCMA) += bcma-hcd.o
  14088. obj-$(CONFIG_USB_HCD_SSB) += ssb-hcd.o
  14089. -obj-$(CONFIG_USB_FOTG210_HCD) += fotg210-hcd.o
  14090. obj-$(CONFIG_USB_MAX3421_HCD) += max3421-hcd.o
  14091. obj-$(CONFIG_USB_XEN_HCD) += xen-hcd.o
  14092. --- /dev/null
  14093. +++ b/drivers/usb/fotg210/fotg210-hcd.h
  14094. @@ -0,0 +1,688 @@
  14095. +/* SPDX-License-Identifier: GPL-2.0 */
  14096. +#ifndef __LINUX_FOTG210_H
  14097. +#define __LINUX_FOTG210_H
  14098. +
  14099. +#include <linux/usb/ehci-dbgp.h>
  14100. +
  14101. +/* definitions used for the EHCI driver */
  14102. +
  14103. +/*
  14104. + * __hc32 and __hc16 are "Host Controller" types, they may be equivalent to
  14105. + * __leXX (normally) or __beXX (given FOTG210_BIG_ENDIAN_DESC), depending on
  14106. + * the host controller implementation.
  14107. + *
  14108. + * To facilitate the strongest possible byte-order checking from "sparse"
  14109. + * and so on, we use __leXX unless that's not practical.
  14110. + */
  14111. +#define __hc32 __le32
  14112. +#define __hc16 __le16
  14113. +
  14114. +/* statistics can be kept for tuning/monitoring */
  14115. +struct fotg210_stats {
  14116. + /* irq usage */
  14117. + unsigned long normal;
  14118. + unsigned long error;
  14119. + unsigned long iaa;
  14120. + unsigned long lost_iaa;
  14121. +
  14122. + /* termination of urbs from core */
  14123. + unsigned long complete;
  14124. + unsigned long unlink;
  14125. +};
  14126. +
  14127. +/* fotg210_hcd->lock guards shared data against other CPUs:
  14128. + * fotg210_hcd: async, unlink, periodic (and shadow), ...
  14129. + * usb_host_endpoint: hcpriv
  14130. + * fotg210_qh: qh_next, qtd_list
  14131. + * fotg210_qtd: qtd_list
  14132. + *
  14133. + * Also, hold this lock when talking to HC registers or
  14134. + * when updating hw_* fields in shared qh/qtd/... structures.
  14135. + */
  14136. +
  14137. +#define FOTG210_MAX_ROOT_PORTS 1 /* see HCS_N_PORTS */
  14138. +
  14139. +/*
  14140. + * fotg210_rh_state values of FOTG210_RH_RUNNING or above mean that the
  14141. + * controller may be doing DMA. Lower values mean there's no DMA.
  14142. + */
  14143. +enum fotg210_rh_state {
  14144. + FOTG210_RH_HALTED,
  14145. + FOTG210_RH_SUSPENDED,
  14146. + FOTG210_RH_RUNNING,
  14147. + FOTG210_RH_STOPPING
  14148. +};
  14149. +
  14150. +/*
  14151. + * Timer events, ordered by increasing delay length.
  14152. + * Always update event_delays_ns[] and event_handlers[] (defined in
  14153. + * ehci-timer.c) in parallel with this list.
  14154. + */
  14155. +enum fotg210_hrtimer_event {
  14156. + FOTG210_HRTIMER_POLL_ASS, /* Poll for async schedule off */
  14157. + FOTG210_HRTIMER_POLL_PSS, /* Poll for periodic schedule off */
  14158. + FOTG210_HRTIMER_POLL_DEAD, /* Wait for dead controller to stop */
  14159. + FOTG210_HRTIMER_UNLINK_INTR, /* Wait for interrupt QH unlink */
  14160. + FOTG210_HRTIMER_FREE_ITDS, /* Wait for unused iTDs and siTDs */
  14161. + FOTG210_HRTIMER_ASYNC_UNLINKS, /* Unlink empty async QHs */
  14162. + FOTG210_HRTIMER_IAA_WATCHDOG, /* Handle lost IAA interrupts */
  14163. + FOTG210_HRTIMER_DISABLE_PERIODIC, /* Wait to disable periodic sched */
  14164. + FOTG210_HRTIMER_DISABLE_ASYNC, /* Wait to disable async sched */
  14165. + FOTG210_HRTIMER_IO_WATCHDOG, /* Check for missing IRQs */
  14166. + FOTG210_HRTIMER_NUM_EVENTS /* Must come last */
  14167. +};
  14168. +#define FOTG210_HRTIMER_NO_EVENT 99
  14169. +
  14170. +struct fotg210_hcd { /* one per controller */
  14171. + /* timing support */
  14172. + enum fotg210_hrtimer_event next_hrtimer_event;
  14173. + unsigned enabled_hrtimer_events;
  14174. + ktime_t hr_timeouts[FOTG210_HRTIMER_NUM_EVENTS];
  14175. + struct hrtimer hrtimer;
  14176. +
  14177. + int PSS_poll_count;
  14178. + int ASS_poll_count;
  14179. + int died_poll_count;
  14180. +
  14181. + /* glue to PCI and HCD framework */
  14182. + struct fotg210_caps __iomem *caps;
  14183. + struct fotg210_regs __iomem *regs;
  14184. + struct ehci_dbg_port __iomem *debug;
  14185. +
  14186. + __u32 hcs_params; /* cached register copy */
  14187. + spinlock_t lock;
  14188. + enum fotg210_rh_state rh_state;
  14189. +
  14190. + /* general schedule support */
  14191. + bool scanning:1;
  14192. + bool need_rescan:1;
  14193. + bool intr_unlinking:1;
  14194. + bool async_unlinking:1;
  14195. + bool shutdown:1;
  14196. + struct fotg210_qh *qh_scan_next;
  14197. +
  14198. + /* async schedule support */
  14199. + struct fotg210_qh *async;
  14200. + struct fotg210_qh *dummy; /* For AMD quirk use */
  14201. + struct fotg210_qh *async_unlink;
  14202. + struct fotg210_qh *async_unlink_last;
  14203. + struct fotg210_qh *async_iaa;
  14204. + unsigned async_unlink_cycle;
  14205. + unsigned async_count; /* async activity count */
  14206. +
  14207. + /* periodic schedule support */
  14208. +#define DEFAULT_I_TDPS 1024 /* some HCs can do less */
  14209. + unsigned periodic_size;
  14210. + __hc32 *periodic; /* hw periodic table */
  14211. + dma_addr_t periodic_dma;
  14212. + struct list_head intr_qh_list;
  14213. + unsigned i_thresh; /* uframes HC might cache */
  14214. +
  14215. + union fotg210_shadow *pshadow; /* mirror hw periodic table */
  14216. + struct fotg210_qh *intr_unlink;
  14217. + struct fotg210_qh *intr_unlink_last;
  14218. + unsigned intr_unlink_cycle;
  14219. + unsigned now_frame; /* frame from HC hardware */
  14220. + unsigned next_frame; /* scan periodic, start here */
  14221. + unsigned intr_count; /* intr activity count */
  14222. + unsigned isoc_count; /* isoc activity count */
  14223. + unsigned periodic_count; /* periodic activity count */
  14224. + /* max periodic time per uframe */
  14225. + unsigned uframe_periodic_max;
  14226. +
  14227. +
  14228. + /* list of itds completed while now_frame was still active */
  14229. + struct list_head cached_itd_list;
  14230. + struct fotg210_itd *last_itd_to_free;
  14231. +
  14232. + /* per root hub port */
  14233. + unsigned long reset_done[FOTG210_MAX_ROOT_PORTS];
  14234. +
  14235. + /* bit vectors (one bit per port)
  14236. + * which ports were already suspended at the start of a bus suspend
  14237. + */
  14238. + unsigned long bus_suspended;
  14239. +
  14240. + /* which ports are edicated to the companion controller */
  14241. + unsigned long companion_ports;
  14242. +
  14243. + /* which ports are owned by the companion during a bus suspend */
  14244. + unsigned long owned_ports;
  14245. +
  14246. + /* which ports have the change-suspend feature turned on */
  14247. + unsigned long port_c_suspend;
  14248. +
  14249. + /* which ports are suspended */
  14250. + unsigned long suspended_ports;
  14251. +
  14252. + /* which ports have started to resume */
  14253. + unsigned long resuming_ports;
  14254. +
  14255. + /* per-HC memory pools (could be per-bus, but ...) */
  14256. + struct dma_pool *qh_pool; /* qh per active urb */
  14257. + struct dma_pool *qtd_pool; /* one or more per qh */
  14258. + struct dma_pool *itd_pool; /* itd per iso urb */
  14259. +
  14260. + unsigned random_frame;
  14261. + unsigned long next_statechange;
  14262. + ktime_t last_periodic_enable;
  14263. + u32 command;
  14264. +
  14265. + /* SILICON QUIRKS */
  14266. + unsigned need_io_watchdog:1;
  14267. + unsigned fs_i_thresh:1; /* Intel iso scheduling */
  14268. +
  14269. + u8 sbrn; /* packed release number */
  14270. +
  14271. + /* irq statistics */
  14272. +#ifdef FOTG210_STATS
  14273. + struct fotg210_stats stats;
  14274. +# define INCR(x) ((x)++)
  14275. +#else
  14276. +# define INCR(x) do {} while (0)
  14277. +#endif
  14278. +
  14279. + /* silicon clock */
  14280. + struct clk *pclk;
  14281. +};
  14282. +
  14283. +/* convert between an HCD pointer and the corresponding FOTG210_HCD */
  14284. +static inline struct fotg210_hcd *hcd_to_fotg210(struct usb_hcd *hcd)
  14285. +{
  14286. + return (struct fotg210_hcd *)(hcd->hcd_priv);
  14287. +}
  14288. +static inline struct usb_hcd *fotg210_to_hcd(struct fotg210_hcd *fotg210)
  14289. +{
  14290. + return container_of((void *) fotg210, struct usb_hcd, hcd_priv);
  14291. +}
  14292. +
  14293. +/*-------------------------------------------------------------------------*/
  14294. +
  14295. +/* EHCI register interface, corresponds to EHCI Revision 0.95 specification */
  14296. +
  14297. +/* Section 2.2 Host Controller Capability Registers */
  14298. +struct fotg210_caps {
  14299. + /* these fields are specified as 8 and 16 bit registers,
  14300. + * but some hosts can't perform 8 or 16 bit PCI accesses.
  14301. + * some hosts treat caplength and hciversion as parts of a 32-bit
  14302. + * register, others treat them as two separate registers, this
  14303. + * affects the memory map for big endian controllers.
  14304. + */
  14305. + u32 hc_capbase;
  14306. +#define HC_LENGTH(fotg210, p) (0x00ff&((p) >> /* bits 7:0 / offset 00h */ \
  14307. + (fotg210_big_endian_capbase(fotg210) ? 24 : 0)))
  14308. +#define HC_VERSION(fotg210, p) (0xffff&((p) >> /* bits 31:16 / offset 02h */ \
  14309. + (fotg210_big_endian_capbase(fotg210) ? 0 : 16)))
  14310. + u32 hcs_params; /* HCSPARAMS - offset 0x4 */
  14311. +#define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
  14312. +
  14313. + u32 hcc_params; /* HCCPARAMS - offset 0x8 */
  14314. +#define HCC_CANPARK(p) ((p)&(1 << 2)) /* true: can park on async qh */
  14315. +#define HCC_PGM_FRAMELISTLEN(p) ((p)&(1 << 1)) /* true: periodic_size changes*/
  14316. + u8 portroute[8]; /* nibbles for routing - offset 0xC */
  14317. +};
  14318. +
  14319. +
  14320. +/* Section 2.3 Host Controller Operational Registers */
  14321. +struct fotg210_regs {
  14322. +
  14323. + /* USBCMD: offset 0x00 */
  14324. + u32 command;
  14325. +
  14326. +/* EHCI 1.1 addendum */
  14327. +/* 23:16 is r/w intr rate, in microframes; default "8" == 1/msec */
  14328. +#define CMD_PARK (1<<11) /* enable "park" on async qh */
  14329. +#define CMD_PARK_CNT(c) (((c)>>8)&3) /* how many transfers to park for */
  14330. +#define CMD_IAAD (1<<6) /* "doorbell" interrupt async advance */
  14331. +#define CMD_ASE (1<<5) /* async schedule enable */
  14332. +#define CMD_PSE (1<<4) /* periodic schedule enable */
  14333. +/* 3:2 is periodic frame list size */
  14334. +#define CMD_RESET (1<<1) /* reset HC not bus */
  14335. +#define CMD_RUN (1<<0) /* start/stop HC */
  14336. +
  14337. + /* USBSTS: offset 0x04 */
  14338. + u32 status;
  14339. +#define STS_ASS (1<<15) /* Async Schedule Status */
  14340. +#define STS_PSS (1<<14) /* Periodic Schedule Status */
  14341. +#define STS_RECL (1<<13) /* Reclamation */
  14342. +#define STS_HALT (1<<12) /* Not running (any reason) */
  14343. +/* some bits reserved */
  14344. + /* these STS_* flags are also intr_enable bits (USBINTR) */
  14345. +#define STS_IAA (1<<5) /* Interrupted on async advance */
  14346. +#define STS_FATAL (1<<4) /* such as some PCI access errors */
  14347. +#define STS_FLR (1<<3) /* frame list rolled over */
  14348. +#define STS_PCD (1<<2) /* port change detect */
  14349. +#define STS_ERR (1<<1) /* "error" completion (overflow, ...) */
  14350. +#define STS_INT (1<<0) /* "normal" completion (short, ...) */
  14351. +
  14352. + /* USBINTR: offset 0x08 */
  14353. + u32 intr_enable;
  14354. +
  14355. + /* FRINDEX: offset 0x0C */
  14356. + u32 frame_index; /* current microframe number */
  14357. + /* CTRLDSSEGMENT: offset 0x10 */
  14358. + u32 segment; /* address bits 63:32 if needed */
  14359. + /* PERIODICLISTBASE: offset 0x14 */
  14360. + u32 frame_list; /* points to periodic list */
  14361. + /* ASYNCLISTADDR: offset 0x18 */
  14362. + u32 async_next; /* address of next async queue head */
  14363. +
  14364. + u32 reserved1;
  14365. + /* PORTSC: offset 0x20 */
  14366. + u32 port_status;
  14367. +/* 31:23 reserved */
  14368. +#define PORT_USB11(x) (((x)&(3<<10)) == (1<<10)) /* USB 1.1 device */
  14369. +#define PORT_RESET (1<<8) /* reset port */
  14370. +#define PORT_SUSPEND (1<<7) /* suspend port */
  14371. +#define PORT_RESUME (1<<6) /* resume it */
  14372. +#define PORT_PEC (1<<3) /* port enable change */
  14373. +#define PORT_PE (1<<2) /* port enable */
  14374. +#define PORT_CSC (1<<1) /* connect status change */
  14375. +#define PORT_CONNECT (1<<0) /* device connected */
  14376. +#define PORT_RWC_BITS (PORT_CSC | PORT_PEC)
  14377. + u32 reserved2[19];
  14378. +
  14379. + /* OTGCSR: offet 0x70 */
  14380. + u32 otgcsr;
  14381. +#define OTGCSR_HOST_SPD_TYP (3 << 22)
  14382. +#define OTGCSR_A_BUS_DROP (1 << 5)
  14383. +#define OTGCSR_A_BUS_REQ (1 << 4)
  14384. +
  14385. + /* OTGISR: offset 0x74 */
  14386. + u32 otgisr;
  14387. +#define OTGISR_OVC (1 << 10)
  14388. +
  14389. + u32 reserved3[15];
  14390. +
  14391. + /* GMIR: offset 0xB4 */
  14392. + u32 gmir;
  14393. +#define GMIR_INT_POLARITY (1 << 3) /*Active High*/
  14394. +#define GMIR_MHC_INT (1 << 2)
  14395. +#define GMIR_MOTG_INT (1 << 1)
  14396. +#define GMIR_MDEV_INT (1 << 0)
  14397. +};
  14398. +
  14399. +/*-------------------------------------------------------------------------*/
  14400. +
  14401. +#define QTD_NEXT(fotg210, dma) cpu_to_hc32(fotg210, (u32)dma)
  14402. +
  14403. +/*
  14404. + * EHCI Specification 0.95 Section 3.5
  14405. + * QTD: describe data transfer components (buffer, direction, ...)
  14406. + * See Fig 3-6 "Queue Element Transfer Descriptor Block Diagram".
  14407. + *
  14408. + * These are associated only with "QH" (Queue Head) structures,
  14409. + * used with control, bulk, and interrupt transfers.
  14410. + */
  14411. +struct fotg210_qtd {
  14412. + /* first part defined by EHCI spec */
  14413. + __hc32 hw_next; /* see EHCI 3.5.1 */
  14414. + __hc32 hw_alt_next; /* see EHCI 3.5.2 */
  14415. + __hc32 hw_token; /* see EHCI 3.5.3 */
  14416. +#define QTD_TOGGLE (1 << 31) /* data toggle */
  14417. +#define QTD_LENGTH(tok) (((tok)>>16) & 0x7fff)
  14418. +#define QTD_IOC (1 << 15) /* interrupt on complete */
  14419. +#define QTD_CERR(tok) (((tok)>>10) & 0x3)
  14420. +#define QTD_PID(tok) (((tok)>>8) & 0x3)
  14421. +#define QTD_STS_ACTIVE (1 << 7) /* HC may execute this */
  14422. +#define QTD_STS_HALT (1 << 6) /* halted on error */
  14423. +#define QTD_STS_DBE (1 << 5) /* data buffer error (in HC) */
  14424. +#define QTD_STS_BABBLE (1 << 4) /* device was babbling (qtd halted) */
  14425. +#define QTD_STS_XACT (1 << 3) /* device gave illegal response */
  14426. +#define QTD_STS_MMF (1 << 2) /* incomplete split transaction */
  14427. +#define QTD_STS_STS (1 << 1) /* split transaction state */
  14428. +#define QTD_STS_PING (1 << 0) /* issue PING? */
  14429. +
  14430. +#define ACTIVE_BIT(fotg210) cpu_to_hc32(fotg210, QTD_STS_ACTIVE)
  14431. +#define HALT_BIT(fotg210) cpu_to_hc32(fotg210, QTD_STS_HALT)
  14432. +#define STATUS_BIT(fotg210) cpu_to_hc32(fotg210, QTD_STS_STS)
  14433. +
  14434. + __hc32 hw_buf[5]; /* see EHCI 3.5.4 */
  14435. + __hc32 hw_buf_hi[5]; /* Appendix B */
  14436. +
  14437. + /* the rest is HCD-private */
  14438. + dma_addr_t qtd_dma; /* qtd address */
  14439. + struct list_head qtd_list; /* sw qtd list */
  14440. + struct urb *urb; /* qtd's urb */
  14441. + size_t length; /* length of buffer */
  14442. +} __aligned(32);
  14443. +
  14444. +/* mask NakCnt+T in qh->hw_alt_next */
  14445. +#define QTD_MASK(fotg210) cpu_to_hc32(fotg210, ~0x1f)
  14446. +
  14447. +#define IS_SHORT_READ(token) (QTD_LENGTH(token) != 0 && QTD_PID(token) == 1)
  14448. +
  14449. +/*-------------------------------------------------------------------------*/
  14450. +
  14451. +/* type tag from {qh,itd,fstn}->hw_next */
  14452. +#define Q_NEXT_TYPE(fotg210, dma) ((dma) & cpu_to_hc32(fotg210, 3 << 1))
  14453. +
  14454. +/*
  14455. + * Now the following defines are not converted using the
  14456. + * cpu_to_le32() macro anymore, since we have to support
  14457. + * "dynamic" switching between be and le support, so that the driver
  14458. + * can be used on one system with SoC EHCI controller using big-endian
  14459. + * descriptors as well as a normal little-endian PCI EHCI controller.
  14460. + */
  14461. +/* values for that type tag */
  14462. +#define Q_TYPE_ITD (0 << 1)
  14463. +#define Q_TYPE_QH (1 << 1)
  14464. +#define Q_TYPE_SITD (2 << 1)
  14465. +#define Q_TYPE_FSTN (3 << 1)
  14466. +
  14467. +/* next async queue entry, or pointer to interrupt/periodic QH */
  14468. +#define QH_NEXT(fotg210, dma) \
  14469. + (cpu_to_hc32(fotg210, (((u32)dma)&~0x01f)|Q_TYPE_QH))
  14470. +
  14471. +/* for periodic/async schedules and qtd lists, mark end of list */
  14472. +#define FOTG210_LIST_END(fotg210) \
  14473. + cpu_to_hc32(fotg210, 1) /* "null pointer" to hw */
  14474. +
  14475. +/*
  14476. + * Entries in periodic shadow table are pointers to one of four kinds
  14477. + * of data structure. That's dictated by the hardware; a type tag is
  14478. + * encoded in the low bits of the hardware's periodic schedule. Use
  14479. + * Q_NEXT_TYPE to get the tag.
  14480. + *
  14481. + * For entries in the async schedule, the type tag always says "qh".
  14482. + */
  14483. +union fotg210_shadow {
  14484. + struct fotg210_qh *qh; /* Q_TYPE_QH */
  14485. + struct fotg210_itd *itd; /* Q_TYPE_ITD */
  14486. + struct fotg210_fstn *fstn; /* Q_TYPE_FSTN */
  14487. + __hc32 *hw_next; /* (all types) */
  14488. + void *ptr;
  14489. +};
  14490. +
  14491. +/*-------------------------------------------------------------------------*/
  14492. +
  14493. +/*
  14494. + * EHCI Specification 0.95 Section 3.6
  14495. + * QH: describes control/bulk/interrupt endpoints
  14496. + * See Fig 3-7 "Queue Head Structure Layout".
  14497. + *
  14498. + * These appear in both the async and (for interrupt) periodic schedules.
  14499. + */
  14500. +
  14501. +/* first part defined by EHCI spec */
  14502. +struct fotg210_qh_hw {
  14503. + __hc32 hw_next; /* see EHCI 3.6.1 */
  14504. + __hc32 hw_info1; /* see EHCI 3.6.2 */
  14505. +#define QH_CONTROL_EP (1 << 27) /* FS/LS control endpoint */
  14506. +#define QH_HEAD (1 << 15) /* Head of async reclamation list */
  14507. +#define QH_TOGGLE_CTL (1 << 14) /* Data toggle control */
  14508. +#define QH_HIGH_SPEED (2 << 12) /* Endpoint speed */
  14509. +#define QH_LOW_SPEED (1 << 12)
  14510. +#define QH_FULL_SPEED (0 << 12)
  14511. +#define QH_INACTIVATE (1 << 7) /* Inactivate on next transaction */
  14512. + __hc32 hw_info2; /* see EHCI 3.6.2 */
  14513. +#define QH_SMASK 0x000000ff
  14514. +#define QH_CMASK 0x0000ff00
  14515. +#define QH_HUBADDR 0x007f0000
  14516. +#define QH_HUBPORT 0x3f800000
  14517. +#define QH_MULT 0xc0000000
  14518. + __hc32 hw_current; /* qtd list - see EHCI 3.6.4 */
  14519. +
  14520. + /* qtd overlay (hardware parts of a struct fotg210_qtd) */
  14521. + __hc32 hw_qtd_next;
  14522. + __hc32 hw_alt_next;
  14523. + __hc32 hw_token;
  14524. + __hc32 hw_buf[5];
  14525. + __hc32 hw_buf_hi[5];
  14526. +} __aligned(32);
  14527. +
  14528. +struct fotg210_qh {
  14529. + struct fotg210_qh_hw *hw; /* Must come first */
  14530. + /* the rest is HCD-private */
  14531. + dma_addr_t qh_dma; /* address of qh */
  14532. + union fotg210_shadow qh_next; /* ptr to qh; or periodic */
  14533. + struct list_head qtd_list; /* sw qtd list */
  14534. + struct list_head intr_node; /* list of intr QHs */
  14535. + struct fotg210_qtd *dummy;
  14536. + struct fotg210_qh *unlink_next; /* next on unlink list */
  14537. +
  14538. + unsigned unlink_cycle;
  14539. +
  14540. + u8 needs_rescan; /* Dequeue during giveback */
  14541. + u8 qh_state;
  14542. +#define QH_STATE_LINKED 1 /* HC sees this */
  14543. +#define QH_STATE_UNLINK 2 /* HC may still see this */
  14544. +#define QH_STATE_IDLE 3 /* HC doesn't see this */
  14545. +#define QH_STATE_UNLINK_WAIT 4 /* LINKED and on unlink q */
  14546. +#define QH_STATE_COMPLETING 5 /* don't touch token.HALT */
  14547. +
  14548. + u8 xacterrs; /* XactErr retry counter */
  14549. +#define QH_XACTERR_MAX 32 /* XactErr retry limit */
  14550. +
  14551. + /* periodic schedule info */
  14552. + u8 usecs; /* intr bandwidth */
  14553. + u8 gap_uf; /* uframes split/csplit gap */
  14554. + u8 c_usecs; /* ... split completion bw */
  14555. + u16 tt_usecs; /* tt downstream bandwidth */
  14556. + unsigned short period; /* polling interval */
  14557. + unsigned short start; /* where polling starts */
  14558. +#define NO_FRAME ((unsigned short)~0) /* pick new start */
  14559. +
  14560. + struct usb_device *dev; /* access to TT */
  14561. + unsigned is_out:1; /* bulk or intr OUT */
  14562. + unsigned clearing_tt:1; /* Clear-TT-Buf in progress */
  14563. +};
  14564. +
  14565. +/*-------------------------------------------------------------------------*/
  14566. +
  14567. +/* description of one iso transaction (up to 3 KB data if highspeed) */
  14568. +struct fotg210_iso_packet {
  14569. + /* These will be copied to iTD when scheduling */
  14570. + u64 bufp; /* itd->hw_bufp{,_hi}[pg] |= */
  14571. + __hc32 transaction; /* itd->hw_transaction[i] |= */
  14572. + u8 cross; /* buf crosses pages */
  14573. + /* for full speed OUT splits */
  14574. + u32 buf1;
  14575. +};
  14576. +
  14577. +/* temporary schedule data for packets from iso urbs (both speeds)
  14578. + * each packet is one logical usb transaction to the device (not TT),
  14579. + * beginning at stream->next_uframe
  14580. + */
  14581. +struct fotg210_iso_sched {
  14582. + struct list_head td_list;
  14583. + unsigned span;
  14584. + struct fotg210_iso_packet packet[];
  14585. +};
  14586. +
  14587. +/*
  14588. + * fotg210_iso_stream - groups all (s)itds for this endpoint.
  14589. + * acts like a qh would, if EHCI had them for ISO.
  14590. + */
  14591. +struct fotg210_iso_stream {
  14592. + /* first field matches fotg210_hq, but is NULL */
  14593. + struct fotg210_qh_hw *hw;
  14594. +
  14595. + u8 bEndpointAddress;
  14596. + u8 highspeed;
  14597. + struct list_head td_list; /* queued itds */
  14598. + struct list_head free_list; /* list of unused itds */
  14599. + struct usb_device *udev;
  14600. + struct usb_host_endpoint *ep;
  14601. +
  14602. + /* output of (re)scheduling */
  14603. + int next_uframe;
  14604. + __hc32 splits;
  14605. +
  14606. + /* the rest is derived from the endpoint descriptor,
  14607. + * trusting urb->interval == f(epdesc->bInterval) and
  14608. + * including the extra info for hw_bufp[0..2]
  14609. + */
  14610. + u8 usecs, c_usecs;
  14611. + u16 interval;
  14612. + u16 tt_usecs;
  14613. + u16 maxp;
  14614. + u16 raw_mask;
  14615. + unsigned bandwidth;
  14616. +
  14617. + /* This is used to initialize iTD's hw_bufp fields */
  14618. + __hc32 buf0;
  14619. + __hc32 buf1;
  14620. + __hc32 buf2;
  14621. +
  14622. + /* this is used to initialize sITD's tt info */
  14623. + __hc32 address;
  14624. +};
  14625. +
  14626. +/*-------------------------------------------------------------------------*/
  14627. +
  14628. +/*
  14629. + * EHCI Specification 0.95 Section 3.3
  14630. + * Fig 3-4 "Isochronous Transaction Descriptor (iTD)"
  14631. + *
  14632. + * Schedule records for high speed iso xfers
  14633. + */
  14634. +struct fotg210_itd {
  14635. + /* first part defined by EHCI spec */
  14636. + __hc32 hw_next; /* see EHCI 3.3.1 */
  14637. + __hc32 hw_transaction[8]; /* see EHCI 3.3.2 */
  14638. +#define FOTG210_ISOC_ACTIVE (1<<31) /* activate transfer this slot */
  14639. +#define FOTG210_ISOC_BUF_ERR (1<<30) /* Data buffer error */
  14640. +#define FOTG210_ISOC_BABBLE (1<<29) /* babble detected */
  14641. +#define FOTG210_ISOC_XACTERR (1<<28) /* XactErr - transaction error */
  14642. +#define FOTG210_ITD_LENGTH(tok) (((tok)>>16) & 0x0fff)
  14643. +#define FOTG210_ITD_IOC (1 << 15) /* interrupt on complete */
  14644. +
  14645. +#define ITD_ACTIVE(fotg210) cpu_to_hc32(fotg210, FOTG210_ISOC_ACTIVE)
  14646. +
  14647. + __hc32 hw_bufp[7]; /* see EHCI 3.3.3 */
  14648. + __hc32 hw_bufp_hi[7]; /* Appendix B */
  14649. +
  14650. + /* the rest is HCD-private */
  14651. + dma_addr_t itd_dma; /* for this itd */
  14652. + union fotg210_shadow itd_next; /* ptr to periodic q entry */
  14653. +
  14654. + struct urb *urb;
  14655. + struct fotg210_iso_stream *stream; /* endpoint's queue */
  14656. + struct list_head itd_list; /* list of stream's itds */
  14657. +
  14658. + /* any/all hw_transactions here may be used by that urb */
  14659. + unsigned frame; /* where scheduled */
  14660. + unsigned pg;
  14661. + unsigned index[8]; /* in urb->iso_frame_desc */
  14662. +} __aligned(32);
  14663. +
  14664. +/*-------------------------------------------------------------------------*/
  14665. +
  14666. +/*
  14667. + * EHCI Specification 0.96 Section 3.7
  14668. + * Periodic Frame Span Traversal Node (FSTN)
  14669. + *
  14670. + * Manages split interrupt transactions (using TT) that span frame boundaries
  14671. + * into uframes 0/1; see 4.12.2.2. In those uframes, a "save place" FSTN
  14672. + * makes the HC jump (back) to a QH to scan for fs/ls QH completions until
  14673. + * it hits a "restore" FSTN; then it returns to finish other uframe 0/1 work.
  14674. + */
  14675. +struct fotg210_fstn {
  14676. + __hc32 hw_next; /* any periodic q entry */
  14677. + __hc32 hw_prev; /* qh or FOTG210_LIST_END */
  14678. +
  14679. + /* the rest is HCD-private */
  14680. + dma_addr_t fstn_dma;
  14681. + union fotg210_shadow fstn_next; /* ptr to periodic q entry */
  14682. +} __aligned(32);
  14683. +
  14684. +/*-------------------------------------------------------------------------*/
  14685. +
  14686. +/* Prepare the PORTSC wakeup flags during controller suspend/resume */
  14687. +
  14688. +#define fotg210_prepare_ports_for_controller_suspend(fotg210, do_wakeup) \
  14689. + fotg210_adjust_port_wakeup_flags(fotg210, true, do_wakeup)
  14690. +
  14691. +#define fotg210_prepare_ports_for_controller_resume(fotg210) \
  14692. + fotg210_adjust_port_wakeup_flags(fotg210, false, false)
  14693. +
  14694. +/*-------------------------------------------------------------------------*/
  14695. +
  14696. +/*
  14697. + * Some EHCI controllers have a Transaction Translator built into the
  14698. + * root hub. This is a non-standard feature. Each controller will need
  14699. + * to add code to the following inline functions, and call them as
  14700. + * needed (mostly in root hub code).
  14701. + */
  14702. +
  14703. +static inline unsigned int
  14704. +fotg210_get_speed(struct fotg210_hcd *fotg210, unsigned int portsc)
  14705. +{
  14706. + return (readl(&fotg210->regs->otgcsr)
  14707. + & OTGCSR_HOST_SPD_TYP) >> 22;
  14708. +}
  14709. +
  14710. +/* Returns the speed of a device attached to a port on the root hub. */
  14711. +static inline unsigned int
  14712. +fotg210_port_speed(struct fotg210_hcd *fotg210, unsigned int portsc)
  14713. +{
  14714. + switch (fotg210_get_speed(fotg210, portsc)) {
  14715. + case 0:
  14716. + return 0;
  14717. + case 1:
  14718. + return USB_PORT_STAT_LOW_SPEED;
  14719. + case 2:
  14720. + default:
  14721. + return USB_PORT_STAT_HIGH_SPEED;
  14722. + }
  14723. +}
  14724. +
  14725. +/*-------------------------------------------------------------------------*/
  14726. +
  14727. +#define fotg210_has_fsl_portno_bug(e) (0)
  14728. +
  14729. +/*
  14730. + * While most USB host controllers implement their registers in
  14731. + * little-endian format, a minority (celleb companion chip) implement
  14732. + * them in big endian format.
  14733. + *
  14734. + * This attempts to support either format at compile time without a
  14735. + * runtime penalty, or both formats with the additional overhead
  14736. + * of checking a flag bit.
  14737. + *
  14738. + */
  14739. +
  14740. +#define fotg210_big_endian_mmio(e) 0
  14741. +#define fotg210_big_endian_capbase(e) 0
  14742. +
  14743. +static inline unsigned int fotg210_readl(const struct fotg210_hcd *fotg210,
  14744. + __u32 __iomem *regs)
  14745. +{
  14746. + return readl(regs);
  14747. +}
  14748. +
  14749. +static inline void fotg210_writel(const struct fotg210_hcd *fotg210,
  14750. + const unsigned int val, __u32 __iomem *regs)
  14751. +{
  14752. + writel(val, regs);
  14753. +}
  14754. +
  14755. +/* cpu to fotg210 */
  14756. +static inline __hc32 cpu_to_hc32(const struct fotg210_hcd *fotg210, const u32 x)
  14757. +{
  14758. + return cpu_to_le32(x);
  14759. +}
  14760. +
  14761. +/* fotg210 to cpu */
  14762. +static inline u32 hc32_to_cpu(const struct fotg210_hcd *fotg210, const __hc32 x)
  14763. +{
  14764. + return le32_to_cpu(x);
  14765. +}
  14766. +
  14767. +static inline u32 hc32_to_cpup(const struct fotg210_hcd *fotg210,
  14768. + const __hc32 *x)
  14769. +{
  14770. + return le32_to_cpup(x);
  14771. +}
  14772. +
  14773. +/*-------------------------------------------------------------------------*/
  14774. +
  14775. +static inline unsigned fotg210_read_frame_index(struct fotg210_hcd *fotg210)
  14776. +{
  14777. + return fotg210_readl(fotg210, &fotg210->regs->frame_index);
  14778. +}
  14779. +
  14780. +/*-------------------------------------------------------------------------*/
  14781. +
  14782. +#endif /* __LINUX_FOTG210_H */
  14783. --- /dev/null
  14784. +++ b/drivers/usb/fotg210/fotg210-udc.h
  14785. @@ -0,0 +1,249 @@
  14786. +// SPDX-License-Identifier: GPL-2.0+
  14787. +/*
  14788. + * Faraday FOTG210 USB OTG controller
  14789. + *
  14790. + * Copyright (C) 2013 Faraday Technology Corporation
  14791. + * Author: Yuan-Hsin Chen <[email protected]>
  14792. + */
  14793. +
  14794. +#include <linux/kernel.h>
  14795. +
  14796. +#define FOTG210_MAX_NUM_EP 5 /* ep0...ep4 */
  14797. +#define FOTG210_MAX_FIFO_NUM 4 /* fifo0...fifo4 */
  14798. +
  14799. +/* Global Mask of HC/OTG/DEV interrupt Register(0xC4) */
  14800. +#define FOTG210_GMIR 0xC4
  14801. +#define GMIR_INT_POLARITY 0x8 /*Active High*/
  14802. +#define GMIR_MHC_INT 0x4
  14803. +#define GMIR_MOTG_INT 0x2
  14804. +#define GMIR_MDEV_INT 0x1
  14805. +
  14806. +/* Device Main Control Register(0x100) */
  14807. +#define FOTG210_DMCR 0x100
  14808. +#define DMCR_HS_EN (1 << 6)
  14809. +#define DMCR_CHIP_EN (1 << 5)
  14810. +#define DMCR_SFRST (1 << 4)
  14811. +#define DMCR_GOSUSP (1 << 3)
  14812. +#define DMCR_GLINT_EN (1 << 2)
  14813. +#define DMCR_HALF_SPEED (1 << 1)
  14814. +#define DMCR_CAP_RMWAKUP (1 << 0)
  14815. +
  14816. +/* Device Address Register(0x104) */
  14817. +#define FOTG210_DAR 0x104
  14818. +#define DAR_AFT_CONF (1 << 7)
  14819. +
  14820. +/* Device Test Register(0x108) */
  14821. +#define FOTG210_DTR 0x108
  14822. +#define DTR_TST_CLRFF (1 << 0)
  14823. +
  14824. +/* PHY Test Mode Selector register(0x114) */
  14825. +#define FOTG210_PHYTMSR 0x114
  14826. +#define PHYTMSR_TST_PKT (1 << 4)
  14827. +#define PHYTMSR_TST_SE0NAK (1 << 3)
  14828. +#define PHYTMSR_TST_KSTA (1 << 2)
  14829. +#define PHYTMSR_TST_JSTA (1 << 1)
  14830. +#define PHYTMSR_UNPLUG (1 << 0)
  14831. +
  14832. +/* Cx configuration and FIFO Empty Status register(0x120) */
  14833. +#define FOTG210_DCFESR 0x120
  14834. +#define DCFESR_FIFO_EMPTY(fifo) (1 << 8 << (fifo))
  14835. +#define DCFESR_CX_EMP (1 << 5)
  14836. +#define DCFESR_CX_CLR (1 << 3)
  14837. +#define DCFESR_CX_STL (1 << 2)
  14838. +#define DCFESR_TST_PKDONE (1 << 1)
  14839. +#define DCFESR_CX_DONE (1 << 0)
  14840. +
  14841. +/* Device IDLE Counter Register(0x124) */
  14842. +#define FOTG210_DICR 0x124
  14843. +
  14844. +/* Device Mask of Interrupt Group Register (0x130) */
  14845. +#define FOTG210_DMIGR 0x130
  14846. +#define DMIGR_MINT_G0 (1 << 0)
  14847. +
  14848. +/* Device Mask of Interrupt Source Group 0(0x134) */
  14849. +#define FOTG210_DMISGR0 0x134
  14850. +#define DMISGR0_MCX_COMEND (1 << 3)
  14851. +#define DMISGR0_MCX_OUT_INT (1 << 2)
  14852. +#define DMISGR0_MCX_IN_INT (1 << 1)
  14853. +#define DMISGR0_MCX_SETUP_INT (1 << 0)
  14854. +
  14855. +/* Device Mask of Interrupt Source Group 1 Register(0x138)*/
  14856. +#define FOTG210_DMISGR1 0x138
  14857. +#define DMISGR1_MF3_IN_INT (1 << 19)
  14858. +#define DMISGR1_MF2_IN_INT (1 << 18)
  14859. +#define DMISGR1_MF1_IN_INT (1 << 17)
  14860. +#define DMISGR1_MF0_IN_INT (1 << 16)
  14861. +#define DMISGR1_MF_IN_INT(fifo) (1 << (16 + (fifo)))
  14862. +#define DMISGR1_MF3_SPK_INT (1 << 7)
  14863. +#define DMISGR1_MF3_OUT_INT (1 << 6)
  14864. +#define DMISGR1_MF2_SPK_INT (1 << 5)
  14865. +#define DMISGR1_MF2_OUT_INT (1 << 4)
  14866. +#define DMISGR1_MF1_SPK_INT (1 << 3)
  14867. +#define DMISGR1_MF1_OUT_INT (1 << 2)
  14868. +#define DMISGR1_MF0_SPK_INT (1 << 1)
  14869. +#define DMISGR1_MF0_OUT_INT (1 << 0)
  14870. +#define DMISGR1_MF_OUTSPK_INT(fifo) (0x3 << (fifo) * 2)
  14871. +
  14872. +/* Device Mask of Interrupt Source Group 2 Register (0x13C) */
  14873. +#define FOTG210_DMISGR2 0x13C
  14874. +#define DMISGR2_MDMA_ERROR (1 << 8)
  14875. +#define DMISGR2_MDMA_CMPLT (1 << 7)
  14876. +
  14877. +/* Device Interrupt group Register (0x140) */
  14878. +#define FOTG210_DIGR 0x140
  14879. +#define DIGR_INT_G2 (1 << 2)
  14880. +#define DIGR_INT_G1 (1 << 1)
  14881. +#define DIGR_INT_G0 (1 << 0)
  14882. +
  14883. +/* Device Interrupt Source Group 0 Register (0x144) */
  14884. +#define FOTG210_DISGR0 0x144
  14885. +#define DISGR0_CX_COMABT_INT (1 << 5)
  14886. +#define DISGR0_CX_COMFAIL_INT (1 << 4)
  14887. +#define DISGR0_CX_COMEND_INT (1 << 3)
  14888. +#define DISGR0_CX_OUT_INT (1 << 2)
  14889. +#define DISGR0_CX_IN_INT (1 << 1)
  14890. +#define DISGR0_CX_SETUP_INT (1 << 0)
  14891. +
  14892. +/* Device Interrupt Source Group 1 Register (0x148) */
  14893. +#define FOTG210_DISGR1 0x148
  14894. +#define DISGR1_OUT_INT(fifo) (1 << ((fifo) * 2))
  14895. +#define DISGR1_SPK_INT(fifo) (1 << 1 << ((fifo) * 2))
  14896. +#define DISGR1_IN_INT(fifo) (1 << 16 << (fifo))
  14897. +
  14898. +/* Device Interrupt Source Group 2 Register (0x14C) */
  14899. +#define FOTG210_DISGR2 0x14C
  14900. +#define DISGR2_DMA_ERROR (1 << 8)
  14901. +#define DISGR2_DMA_CMPLT (1 << 7)
  14902. +#define DISGR2_RX0BYTE_INT (1 << 6)
  14903. +#define DISGR2_TX0BYTE_INT (1 << 5)
  14904. +#define DISGR2_ISO_SEQ_ABORT_INT (1 << 4)
  14905. +#define DISGR2_ISO_SEQ_ERR_INT (1 << 3)
  14906. +#define DISGR2_RESM_INT (1 << 2)
  14907. +#define DISGR2_SUSP_INT (1 << 1)
  14908. +#define DISGR2_USBRST_INT (1 << 0)
  14909. +
  14910. +/* Device Receive Zero-Length Data Packet Register (0x150)*/
  14911. +#define FOTG210_RX0BYTE 0x150
  14912. +#define RX0BYTE_EP8 (1 << 7)
  14913. +#define RX0BYTE_EP7 (1 << 6)
  14914. +#define RX0BYTE_EP6 (1 << 5)
  14915. +#define RX0BYTE_EP5 (1 << 4)
  14916. +#define RX0BYTE_EP4 (1 << 3)
  14917. +#define RX0BYTE_EP3 (1 << 2)
  14918. +#define RX0BYTE_EP2 (1 << 1)
  14919. +#define RX0BYTE_EP1 (1 << 0)
  14920. +
  14921. +/* Device Transfer Zero-Length Data Packet Register (0x154)*/
  14922. +#define FOTG210_TX0BYTE 0x154
  14923. +#define TX0BYTE_EP8 (1 << 7)
  14924. +#define TX0BYTE_EP7 (1 << 6)
  14925. +#define TX0BYTE_EP6 (1 << 5)
  14926. +#define TX0BYTE_EP5 (1 << 4)
  14927. +#define TX0BYTE_EP4 (1 << 3)
  14928. +#define TX0BYTE_EP3 (1 << 2)
  14929. +#define TX0BYTE_EP2 (1 << 1)
  14930. +#define TX0BYTE_EP1 (1 << 0)
  14931. +
  14932. +/* Device IN Endpoint x MaxPacketSize Register(0x160+4*(x-1)) */
  14933. +#define FOTG210_INEPMPSR(ep) (0x160 + 4 * ((ep) - 1))
  14934. +#define INOUTEPMPSR_MPS(mps) ((mps) & 0x2FF)
  14935. +#define INOUTEPMPSR_STL_EP (1 << 11)
  14936. +#define INOUTEPMPSR_RESET_TSEQ (1 << 12)
  14937. +
  14938. +/* Device OUT Endpoint x MaxPacketSize Register(0x180+4*(x-1)) */
  14939. +#define FOTG210_OUTEPMPSR(ep) (0x180 + 4 * ((ep) - 1))
  14940. +
  14941. +/* Device Endpoint 1~4 Map Register (0x1A0) */
  14942. +#define FOTG210_EPMAP 0x1A0
  14943. +#define EPMAP_FIFONO(ep, dir) \
  14944. + ((((ep) - 1) << ((ep) - 1) * 8) << ((dir) ? 0 : 4))
  14945. +#define EPMAP_FIFONOMSK(ep, dir) \
  14946. + ((3 << ((ep) - 1) * 8) << ((dir) ? 0 : 4))
  14947. +
  14948. +/* Device FIFO Map Register (0x1A8) */
  14949. +#define FOTG210_FIFOMAP 0x1A8
  14950. +#define FIFOMAP_DIROUT(fifo) (0x0 << 4 << (fifo) * 8)
  14951. +#define FIFOMAP_DIRIN(fifo) (0x1 << 4 << (fifo) * 8)
  14952. +#define FIFOMAP_BIDIR(fifo) (0x2 << 4 << (fifo) * 8)
  14953. +#define FIFOMAP_NA(fifo) (0x3 << 4 << (fifo) * 8)
  14954. +#define FIFOMAP_EPNO(ep) ((ep) << ((ep) - 1) * 8)
  14955. +#define FIFOMAP_EPNOMSK(ep) (0xF << ((ep) - 1) * 8)
  14956. +
  14957. +/* Device FIFO Confuguration Register (0x1AC) */
  14958. +#define FOTG210_FIFOCF 0x1AC
  14959. +#define FIFOCF_TYPE(type, fifo) ((type) << (fifo) * 8)
  14960. +#define FIFOCF_BLK_SIN(fifo) (0x0 << (fifo) * 8 << 2)
  14961. +#define FIFOCF_BLK_DUB(fifo) (0x1 << (fifo) * 8 << 2)
  14962. +#define FIFOCF_BLK_TRI(fifo) (0x2 << (fifo) * 8 << 2)
  14963. +#define FIFOCF_BLKSZ_512(fifo) (0x0 << (fifo) * 8 << 4)
  14964. +#define FIFOCF_BLKSZ_1024(fifo) (0x1 << (fifo) * 8 << 4)
  14965. +#define FIFOCF_FIFO_EN(fifo) (0x1 << (fifo) * 8 << 5)
  14966. +
  14967. +/* Device FIFO n Instruction and Byte Count Register (0x1B0+4*n) */
  14968. +#define FOTG210_FIBCR(fifo) (0x1B0 + (fifo) * 4)
  14969. +#define FIBCR_BCFX 0x7FF
  14970. +#define FIBCR_FFRST (1 << 12)
  14971. +
  14972. +/* Device DMA Target FIFO Number Register (0x1C0) */
  14973. +#define FOTG210_DMATFNR 0x1C0
  14974. +#define DMATFNR_ACC_CXF (1 << 4)
  14975. +#define DMATFNR_ACC_F3 (1 << 3)
  14976. +#define DMATFNR_ACC_F2 (1 << 2)
  14977. +#define DMATFNR_ACC_F1 (1 << 1)
  14978. +#define DMATFNR_ACC_F0 (1 << 0)
  14979. +#define DMATFNR_ACC_FN(fifo) (1 << (fifo))
  14980. +#define DMATFNR_DISDMA 0
  14981. +
  14982. +/* Device DMA Controller Parameter setting 1 Register (0x1C8) */
  14983. +#define FOTG210_DMACPSR1 0x1C8
  14984. +#define DMACPSR1_DMA_LEN(len) (((len) & 0xFFFF) << 8)
  14985. +#define DMACPSR1_DMA_ABORT (1 << 3)
  14986. +#define DMACPSR1_DMA_TYPE(dir_in) (((dir_in) ? 1 : 0) << 1)
  14987. +#define DMACPSR1_DMA_START (1 << 0)
  14988. +
  14989. +/* Device DMA Controller Parameter setting 2 Register (0x1CC) */
  14990. +#define FOTG210_DMACPSR2 0x1CC
  14991. +
  14992. +/* Device DMA Controller Parameter setting 3 Register (0x1CC) */
  14993. +#define FOTG210_CXPORT 0x1D0
  14994. +
  14995. +struct fotg210_request {
  14996. + struct usb_request req;
  14997. + struct list_head queue;
  14998. +};
  14999. +
  15000. +struct fotg210_ep {
  15001. + struct usb_ep ep;
  15002. + struct fotg210_udc *fotg210;
  15003. +
  15004. + struct list_head queue;
  15005. + unsigned stall:1;
  15006. + unsigned wedged:1;
  15007. + unsigned use_dma:1;
  15008. +
  15009. + unsigned char epnum;
  15010. + unsigned char type;
  15011. + unsigned char dir_in;
  15012. + unsigned int maxp;
  15013. + const struct usb_endpoint_descriptor *desc;
  15014. +};
  15015. +
  15016. +struct fotg210_udc {
  15017. + spinlock_t lock; /* protect the struct */
  15018. + void __iomem *reg;
  15019. +
  15020. + unsigned long irq_trigger;
  15021. +
  15022. + struct usb_gadget gadget;
  15023. + struct usb_gadget_driver *driver;
  15024. +
  15025. + struct fotg210_ep *ep[FOTG210_MAX_NUM_EP];
  15026. +
  15027. + struct usb_request *ep0_req; /* for internal request */
  15028. + __le16 ep0_data;
  15029. + u8 ep0_dir; /* 0/0x80 out/in */
  15030. +
  15031. + u8 reenum; /* if re-enumeration */
  15032. +};
  15033. +
  15034. +#define gadget_to_fotg210(g) container_of((g), struct fotg210_udc, gadget)
  15035. --- a/drivers/usb/gadget/udc/fotg210.h
  15036. +++ /dev/null
  15037. @@ -1,249 +0,0 @@
  15038. -// SPDX-License-Identifier: GPL-2.0+
  15039. -/*
  15040. - * Faraday FOTG210 USB OTG controller
  15041. - *
  15042. - * Copyright (C) 2013 Faraday Technology Corporation
  15043. - * Author: Yuan-Hsin Chen <[email protected]>
  15044. - */
  15045. -
  15046. -#include <linux/kernel.h>
  15047. -
  15048. -#define FOTG210_MAX_NUM_EP 5 /* ep0...ep4 */
  15049. -#define FOTG210_MAX_FIFO_NUM 4 /* fifo0...fifo4 */
  15050. -
  15051. -/* Global Mask of HC/OTG/DEV interrupt Register(0xC4) */
  15052. -#define FOTG210_GMIR 0xC4
  15053. -#define GMIR_INT_POLARITY 0x8 /*Active High*/
  15054. -#define GMIR_MHC_INT 0x4
  15055. -#define GMIR_MOTG_INT 0x2
  15056. -#define GMIR_MDEV_INT 0x1
  15057. -
  15058. -/* Device Main Control Register(0x100) */
  15059. -#define FOTG210_DMCR 0x100
  15060. -#define DMCR_HS_EN (1 << 6)
  15061. -#define DMCR_CHIP_EN (1 << 5)
  15062. -#define DMCR_SFRST (1 << 4)
  15063. -#define DMCR_GOSUSP (1 << 3)
  15064. -#define DMCR_GLINT_EN (1 << 2)
  15065. -#define DMCR_HALF_SPEED (1 << 1)
  15066. -#define DMCR_CAP_RMWAKUP (1 << 0)
  15067. -
  15068. -/* Device Address Register(0x104) */
  15069. -#define FOTG210_DAR 0x104
  15070. -#define DAR_AFT_CONF (1 << 7)
  15071. -
  15072. -/* Device Test Register(0x108) */
  15073. -#define FOTG210_DTR 0x108
  15074. -#define DTR_TST_CLRFF (1 << 0)
  15075. -
  15076. -/* PHY Test Mode Selector register(0x114) */
  15077. -#define FOTG210_PHYTMSR 0x114
  15078. -#define PHYTMSR_TST_PKT (1 << 4)
  15079. -#define PHYTMSR_TST_SE0NAK (1 << 3)
  15080. -#define PHYTMSR_TST_KSTA (1 << 2)
  15081. -#define PHYTMSR_TST_JSTA (1 << 1)
  15082. -#define PHYTMSR_UNPLUG (1 << 0)
  15083. -
  15084. -/* Cx configuration and FIFO Empty Status register(0x120) */
  15085. -#define FOTG210_DCFESR 0x120
  15086. -#define DCFESR_FIFO_EMPTY(fifo) (1 << 8 << (fifo))
  15087. -#define DCFESR_CX_EMP (1 << 5)
  15088. -#define DCFESR_CX_CLR (1 << 3)
  15089. -#define DCFESR_CX_STL (1 << 2)
  15090. -#define DCFESR_TST_PKDONE (1 << 1)
  15091. -#define DCFESR_CX_DONE (1 << 0)
  15092. -
  15093. -/* Device IDLE Counter Register(0x124) */
  15094. -#define FOTG210_DICR 0x124
  15095. -
  15096. -/* Device Mask of Interrupt Group Register (0x130) */
  15097. -#define FOTG210_DMIGR 0x130
  15098. -#define DMIGR_MINT_G0 (1 << 0)
  15099. -
  15100. -/* Device Mask of Interrupt Source Group 0(0x134) */
  15101. -#define FOTG210_DMISGR0 0x134
  15102. -#define DMISGR0_MCX_COMEND (1 << 3)
  15103. -#define DMISGR0_MCX_OUT_INT (1 << 2)
  15104. -#define DMISGR0_MCX_IN_INT (1 << 1)
  15105. -#define DMISGR0_MCX_SETUP_INT (1 << 0)
  15106. -
  15107. -/* Device Mask of Interrupt Source Group 1 Register(0x138)*/
  15108. -#define FOTG210_DMISGR1 0x138
  15109. -#define DMISGR1_MF3_IN_INT (1 << 19)
  15110. -#define DMISGR1_MF2_IN_INT (1 << 18)
  15111. -#define DMISGR1_MF1_IN_INT (1 << 17)
  15112. -#define DMISGR1_MF0_IN_INT (1 << 16)
  15113. -#define DMISGR1_MF_IN_INT(fifo) (1 << (16 + (fifo)))
  15114. -#define DMISGR1_MF3_SPK_INT (1 << 7)
  15115. -#define DMISGR1_MF3_OUT_INT (1 << 6)
  15116. -#define DMISGR1_MF2_SPK_INT (1 << 5)
  15117. -#define DMISGR1_MF2_OUT_INT (1 << 4)
  15118. -#define DMISGR1_MF1_SPK_INT (1 << 3)
  15119. -#define DMISGR1_MF1_OUT_INT (1 << 2)
  15120. -#define DMISGR1_MF0_SPK_INT (1 << 1)
  15121. -#define DMISGR1_MF0_OUT_INT (1 << 0)
  15122. -#define DMISGR1_MF_OUTSPK_INT(fifo) (0x3 << (fifo) * 2)
  15123. -
  15124. -/* Device Mask of Interrupt Source Group 2 Register (0x13C) */
  15125. -#define FOTG210_DMISGR2 0x13C
  15126. -#define DMISGR2_MDMA_ERROR (1 << 8)
  15127. -#define DMISGR2_MDMA_CMPLT (1 << 7)
  15128. -
  15129. -/* Device Interrupt group Register (0x140) */
  15130. -#define FOTG210_DIGR 0x140
  15131. -#define DIGR_INT_G2 (1 << 2)
  15132. -#define DIGR_INT_G1 (1 << 1)
  15133. -#define DIGR_INT_G0 (1 << 0)
  15134. -
  15135. -/* Device Interrupt Source Group 0 Register (0x144) */
  15136. -#define FOTG210_DISGR0 0x144
  15137. -#define DISGR0_CX_COMABT_INT (1 << 5)
  15138. -#define DISGR0_CX_COMFAIL_INT (1 << 4)
  15139. -#define DISGR0_CX_COMEND_INT (1 << 3)
  15140. -#define DISGR0_CX_OUT_INT (1 << 2)
  15141. -#define DISGR0_CX_IN_INT (1 << 1)
  15142. -#define DISGR0_CX_SETUP_INT (1 << 0)
  15143. -
  15144. -/* Device Interrupt Source Group 1 Register (0x148) */
  15145. -#define FOTG210_DISGR1 0x148
  15146. -#define DISGR1_OUT_INT(fifo) (1 << ((fifo) * 2))
  15147. -#define DISGR1_SPK_INT(fifo) (1 << 1 << ((fifo) * 2))
  15148. -#define DISGR1_IN_INT(fifo) (1 << 16 << (fifo))
  15149. -
  15150. -/* Device Interrupt Source Group 2 Register (0x14C) */
  15151. -#define FOTG210_DISGR2 0x14C
  15152. -#define DISGR2_DMA_ERROR (1 << 8)
  15153. -#define DISGR2_DMA_CMPLT (1 << 7)
  15154. -#define DISGR2_RX0BYTE_INT (1 << 6)
  15155. -#define DISGR2_TX0BYTE_INT (1 << 5)
  15156. -#define DISGR2_ISO_SEQ_ABORT_INT (1 << 4)
  15157. -#define DISGR2_ISO_SEQ_ERR_INT (1 << 3)
  15158. -#define DISGR2_RESM_INT (1 << 2)
  15159. -#define DISGR2_SUSP_INT (1 << 1)
  15160. -#define DISGR2_USBRST_INT (1 << 0)
  15161. -
  15162. -/* Device Receive Zero-Length Data Packet Register (0x150)*/
  15163. -#define FOTG210_RX0BYTE 0x150
  15164. -#define RX0BYTE_EP8 (1 << 7)
  15165. -#define RX0BYTE_EP7 (1 << 6)
  15166. -#define RX0BYTE_EP6 (1 << 5)
  15167. -#define RX0BYTE_EP5 (1 << 4)
  15168. -#define RX0BYTE_EP4 (1 << 3)
  15169. -#define RX0BYTE_EP3 (1 << 2)
  15170. -#define RX0BYTE_EP2 (1 << 1)
  15171. -#define RX0BYTE_EP1 (1 << 0)
  15172. -
  15173. -/* Device Transfer Zero-Length Data Packet Register (0x154)*/
  15174. -#define FOTG210_TX0BYTE 0x154
  15175. -#define TX0BYTE_EP8 (1 << 7)
  15176. -#define TX0BYTE_EP7 (1 << 6)
  15177. -#define TX0BYTE_EP6 (1 << 5)
  15178. -#define TX0BYTE_EP5 (1 << 4)
  15179. -#define TX0BYTE_EP4 (1 << 3)
  15180. -#define TX0BYTE_EP3 (1 << 2)
  15181. -#define TX0BYTE_EP2 (1 << 1)
  15182. -#define TX0BYTE_EP1 (1 << 0)
  15183. -
  15184. -/* Device IN Endpoint x MaxPacketSize Register(0x160+4*(x-1)) */
  15185. -#define FOTG210_INEPMPSR(ep) (0x160 + 4 * ((ep) - 1))
  15186. -#define INOUTEPMPSR_MPS(mps) ((mps) & 0x2FF)
  15187. -#define INOUTEPMPSR_STL_EP (1 << 11)
  15188. -#define INOUTEPMPSR_RESET_TSEQ (1 << 12)
  15189. -
  15190. -/* Device OUT Endpoint x MaxPacketSize Register(0x180+4*(x-1)) */
  15191. -#define FOTG210_OUTEPMPSR(ep) (0x180 + 4 * ((ep) - 1))
  15192. -
  15193. -/* Device Endpoint 1~4 Map Register (0x1A0) */
  15194. -#define FOTG210_EPMAP 0x1A0
  15195. -#define EPMAP_FIFONO(ep, dir) \
  15196. - ((((ep) - 1) << ((ep) - 1) * 8) << ((dir) ? 0 : 4))
  15197. -#define EPMAP_FIFONOMSK(ep, dir) \
  15198. - ((3 << ((ep) - 1) * 8) << ((dir) ? 0 : 4))
  15199. -
  15200. -/* Device FIFO Map Register (0x1A8) */
  15201. -#define FOTG210_FIFOMAP 0x1A8
  15202. -#define FIFOMAP_DIROUT(fifo) (0x0 << 4 << (fifo) * 8)
  15203. -#define FIFOMAP_DIRIN(fifo) (0x1 << 4 << (fifo) * 8)
  15204. -#define FIFOMAP_BIDIR(fifo) (0x2 << 4 << (fifo) * 8)
  15205. -#define FIFOMAP_NA(fifo) (0x3 << 4 << (fifo) * 8)
  15206. -#define FIFOMAP_EPNO(ep) ((ep) << ((ep) - 1) * 8)
  15207. -#define FIFOMAP_EPNOMSK(ep) (0xF << ((ep) - 1) * 8)
  15208. -
  15209. -/* Device FIFO Confuguration Register (0x1AC) */
  15210. -#define FOTG210_FIFOCF 0x1AC
  15211. -#define FIFOCF_TYPE(type, fifo) ((type) << (fifo) * 8)
  15212. -#define FIFOCF_BLK_SIN(fifo) (0x0 << (fifo) * 8 << 2)
  15213. -#define FIFOCF_BLK_DUB(fifo) (0x1 << (fifo) * 8 << 2)
  15214. -#define FIFOCF_BLK_TRI(fifo) (0x2 << (fifo) * 8 << 2)
  15215. -#define FIFOCF_BLKSZ_512(fifo) (0x0 << (fifo) * 8 << 4)
  15216. -#define FIFOCF_BLKSZ_1024(fifo) (0x1 << (fifo) * 8 << 4)
  15217. -#define FIFOCF_FIFO_EN(fifo) (0x1 << (fifo) * 8 << 5)
  15218. -
  15219. -/* Device FIFO n Instruction and Byte Count Register (0x1B0+4*n) */
  15220. -#define FOTG210_FIBCR(fifo) (0x1B0 + (fifo) * 4)
  15221. -#define FIBCR_BCFX 0x7FF
  15222. -#define FIBCR_FFRST (1 << 12)
  15223. -
  15224. -/* Device DMA Target FIFO Number Register (0x1C0) */
  15225. -#define FOTG210_DMATFNR 0x1C0
  15226. -#define DMATFNR_ACC_CXF (1 << 4)
  15227. -#define DMATFNR_ACC_F3 (1 << 3)
  15228. -#define DMATFNR_ACC_F2 (1 << 2)
  15229. -#define DMATFNR_ACC_F1 (1 << 1)
  15230. -#define DMATFNR_ACC_F0 (1 << 0)
  15231. -#define DMATFNR_ACC_FN(fifo) (1 << (fifo))
  15232. -#define DMATFNR_DISDMA 0
  15233. -
  15234. -/* Device DMA Controller Parameter setting 1 Register (0x1C8) */
  15235. -#define FOTG210_DMACPSR1 0x1C8
  15236. -#define DMACPSR1_DMA_LEN(len) (((len) & 0xFFFF) << 8)
  15237. -#define DMACPSR1_DMA_ABORT (1 << 3)
  15238. -#define DMACPSR1_DMA_TYPE(dir_in) (((dir_in) ? 1 : 0) << 1)
  15239. -#define DMACPSR1_DMA_START (1 << 0)
  15240. -
  15241. -/* Device DMA Controller Parameter setting 2 Register (0x1CC) */
  15242. -#define FOTG210_DMACPSR2 0x1CC
  15243. -
  15244. -/* Device DMA Controller Parameter setting 3 Register (0x1CC) */
  15245. -#define FOTG210_CXPORT 0x1D0
  15246. -
  15247. -struct fotg210_request {
  15248. - struct usb_request req;
  15249. - struct list_head queue;
  15250. -};
  15251. -
  15252. -struct fotg210_ep {
  15253. - struct usb_ep ep;
  15254. - struct fotg210_udc *fotg210;
  15255. -
  15256. - struct list_head queue;
  15257. - unsigned stall:1;
  15258. - unsigned wedged:1;
  15259. - unsigned use_dma:1;
  15260. -
  15261. - unsigned char epnum;
  15262. - unsigned char type;
  15263. - unsigned char dir_in;
  15264. - unsigned int maxp;
  15265. - const struct usb_endpoint_descriptor *desc;
  15266. -};
  15267. -
  15268. -struct fotg210_udc {
  15269. - spinlock_t lock; /* protect the struct */
  15270. - void __iomem *reg;
  15271. -
  15272. - unsigned long irq_trigger;
  15273. -
  15274. - struct usb_gadget gadget;
  15275. - struct usb_gadget_driver *driver;
  15276. -
  15277. - struct fotg210_ep *ep[FOTG210_MAX_NUM_EP];
  15278. -
  15279. - struct usb_request *ep0_req; /* for internal request */
  15280. - __le16 ep0_data;
  15281. - u8 ep0_dir; /* 0/0x80 out/in */
  15282. -
  15283. - u8 reenum; /* if re-enumeration */
  15284. -};
  15285. -
  15286. -#define gadget_to_fotg210(g) container_of((g), struct fotg210_udc, gadget)
  15287. --- a/drivers/usb/host/fotg210.h
  15288. +++ /dev/null
  15289. @@ -1,688 +0,0 @@
  15290. -/* SPDX-License-Identifier: GPL-2.0 */
  15291. -#ifndef __LINUX_FOTG210_H
  15292. -#define __LINUX_FOTG210_H
  15293. -
  15294. -#include <linux/usb/ehci-dbgp.h>
  15295. -
  15296. -/* definitions used for the EHCI driver */
  15297. -
  15298. -/*
  15299. - * __hc32 and __hc16 are "Host Controller" types, they may be equivalent to
  15300. - * __leXX (normally) or __beXX (given FOTG210_BIG_ENDIAN_DESC), depending on
  15301. - * the host controller implementation.
  15302. - *
  15303. - * To facilitate the strongest possible byte-order checking from "sparse"
  15304. - * and so on, we use __leXX unless that's not practical.
  15305. - */
  15306. -#define __hc32 __le32
  15307. -#define __hc16 __le16
  15308. -
  15309. -/* statistics can be kept for tuning/monitoring */
  15310. -struct fotg210_stats {
  15311. - /* irq usage */
  15312. - unsigned long normal;
  15313. - unsigned long error;
  15314. - unsigned long iaa;
  15315. - unsigned long lost_iaa;
  15316. -
  15317. - /* termination of urbs from core */
  15318. - unsigned long complete;
  15319. - unsigned long unlink;
  15320. -};
  15321. -
  15322. -/* fotg210_hcd->lock guards shared data against other CPUs:
  15323. - * fotg210_hcd: async, unlink, periodic (and shadow), ...
  15324. - * usb_host_endpoint: hcpriv
  15325. - * fotg210_qh: qh_next, qtd_list
  15326. - * fotg210_qtd: qtd_list
  15327. - *
  15328. - * Also, hold this lock when talking to HC registers or
  15329. - * when updating hw_* fields in shared qh/qtd/... structures.
  15330. - */
  15331. -
  15332. -#define FOTG210_MAX_ROOT_PORTS 1 /* see HCS_N_PORTS */
  15333. -
  15334. -/*
  15335. - * fotg210_rh_state values of FOTG210_RH_RUNNING or above mean that the
  15336. - * controller may be doing DMA. Lower values mean there's no DMA.
  15337. - */
  15338. -enum fotg210_rh_state {
  15339. - FOTG210_RH_HALTED,
  15340. - FOTG210_RH_SUSPENDED,
  15341. - FOTG210_RH_RUNNING,
  15342. - FOTG210_RH_STOPPING
  15343. -};
  15344. -
  15345. -/*
  15346. - * Timer events, ordered by increasing delay length.
  15347. - * Always update event_delays_ns[] and event_handlers[] (defined in
  15348. - * ehci-timer.c) in parallel with this list.
  15349. - */
  15350. -enum fotg210_hrtimer_event {
  15351. - FOTG210_HRTIMER_POLL_ASS, /* Poll for async schedule off */
  15352. - FOTG210_HRTIMER_POLL_PSS, /* Poll for periodic schedule off */
  15353. - FOTG210_HRTIMER_POLL_DEAD, /* Wait for dead controller to stop */
  15354. - FOTG210_HRTIMER_UNLINK_INTR, /* Wait for interrupt QH unlink */
  15355. - FOTG210_HRTIMER_FREE_ITDS, /* Wait for unused iTDs and siTDs */
  15356. - FOTG210_HRTIMER_ASYNC_UNLINKS, /* Unlink empty async QHs */
  15357. - FOTG210_HRTIMER_IAA_WATCHDOG, /* Handle lost IAA interrupts */
  15358. - FOTG210_HRTIMER_DISABLE_PERIODIC, /* Wait to disable periodic sched */
  15359. - FOTG210_HRTIMER_DISABLE_ASYNC, /* Wait to disable async sched */
  15360. - FOTG210_HRTIMER_IO_WATCHDOG, /* Check for missing IRQs */
  15361. - FOTG210_HRTIMER_NUM_EVENTS /* Must come last */
  15362. -};
  15363. -#define FOTG210_HRTIMER_NO_EVENT 99
  15364. -
  15365. -struct fotg210_hcd { /* one per controller */
  15366. - /* timing support */
  15367. - enum fotg210_hrtimer_event next_hrtimer_event;
  15368. - unsigned enabled_hrtimer_events;
  15369. - ktime_t hr_timeouts[FOTG210_HRTIMER_NUM_EVENTS];
  15370. - struct hrtimer hrtimer;
  15371. -
  15372. - int PSS_poll_count;
  15373. - int ASS_poll_count;
  15374. - int died_poll_count;
  15375. -
  15376. - /* glue to PCI and HCD framework */
  15377. - struct fotg210_caps __iomem *caps;
  15378. - struct fotg210_regs __iomem *regs;
  15379. - struct ehci_dbg_port __iomem *debug;
  15380. -
  15381. - __u32 hcs_params; /* cached register copy */
  15382. - spinlock_t lock;
  15383. - enum fotg210_rh_state rh_state;
  15384. -
  15385. - /* general schedule support */
  15386. - bool scanning:1;
  15387. - bool need_rescan:1;
  15388. - bool intr_unlinking:1;
  15389. - bool async_unlinking:1;
  15390. - bool shutdown:1;
  15391. - struct fotg210_qh *qh_scan_next;
  15392. -
  15393. - /* async schedule support */
  15394. - struct fotg210_qh *async;
  15395. - struct fotg210_qh *dummy; /* For AMD quirk use */
  15396. - struct fotg210_qh *async_unlink;
  15397. - struct fotg210_qh *async_unlink_last;
  15398. - struct fotg210_qh *async_iaa;
  15399. - unsigned async_unlink_cycle;
  15400. - unsigned async_count; /* async activity count */
  15401. -
  15402. - /* periodic schedule support */
  15403. -#define DEFAULT_I_TDPS 1024 /* some HCs can do less */
  15404. - unsigned periodic_size;
  15405. - __hc32 *periodic; /* hw periodic table */
  15406. - dma_addr_t periodic_dma;
  15407. - struct list_head intr_qh_list;
  15408. - unsigned i_thresh; /* uframes HC might cache */
  15409. -
  15410. - union fotg210_shadow *pshadow; /* mirror hw periodic table */
  15411. - struct fotg210_qh *intr_unlink;
  15412. - struct fotg210_qh *intr_unlink_last;
  15413. - unsigned intr_unlink_cycle;
  15414. - unsigned now_frame; /* frame from HC hardware */
  15415. - unsigned next_frame; /* scan periodic, start here */
  15416. - unsigned intr_count; /* intr activity count */
  15417. - unsigned isoc_count; /* isoc activity count */
  15418. - unsigned periodic_count; /* periodic activity count */
  15419. - /* max periodic time per uframe */
  15420. - unsigned uframe_periodic_max;
  15421. -
  15422. -
  15423. - /* list of itds completed while now_frame was still active */
  15424. - struct list_head cached_itd_list;
  15425. - struct fotg210_itd *last_itd_to_free;
  15426. -
  15427. - /* per root hub port */
  15428. - unsigned long reset_done[FOTG210_MAX_ROOT_PORTS];
  15429. -
  15430. - /* bit vectors (one bit per port)
  15431. - * which ports were already suspended at the start of a bus suspend
  15432. - */
  15433. - unsigned long bus_suspended;
  15434. -
  15435. - /* which ports are edicated to the companion controller */
  15436. - unsigned long companion_ports;
  15437. -
  15438. - /* which ports are owned by the companion during a bus suspend */
  15439. - unsigned long owned_ports;
  15440. -
  15441. - /* which ports have the change-suspend feature turned on */
  15442. - unsigned long port_c_suspend;
  15443. -
  15444. - /* which ports are suspended */
  15445. - unsigned long suspended_ports;
  15446. -
  15447. - /* which ports have started to resume */
  15448. - unsigned long resuming_ports;
  15449. -
  15450. - /* per-HC memory pools (could be per-bus, but ...) */
  15451. - struct dma_pool *qh_pool; /* qh per active urb */
  15452. - struct dma_pool *qtd_pool; /* one or more per qh */
  15453. - struct dma_pool *itd_pool; /* itd per iso urb */
  15454. -
  15455. - unsigned random_frame;
  15456. - unsigned long next_statechange;
  15457. - ktime_t last_periodic_enable;
  15458. - u32 command;
  15459. -
  15460. - /* SILICON QUIRKS */
  15461. - unsigned need_io_watchdog:1;
  15462. - unsigned fs_i_thresh:1; /* Intel iso scheduling */
  15463. -
  15464. - u8 sbrn; /* packed release number */
  15465. -
  15466. - /* irq statistics */
  15467. -#ifdef FOTG210_STATS
  15468. - struct fotg210_stats stats;
  15469. -# define INCR(x) ((x)++)
  15470. -#else
  15471. -# define INCR(x) do {} while (0)
  15472. -#endif
  15473. -
  15474. - /* silicon clock */
  15475. - struct clk *pclk;
  15476. -};
  15477. -
  15478. -/* convert between an HCD pointer and the corresponding FOTG210_HCD */
  15479. -static inline struct fotg210_hcd *hcd_to_fotg210(struct usb_hcd *hcd)
  15480. -{
  15481. - return (struct fotg210_hcd *)(hcd->hcd_priv);
  15482. -}
  15483. -static inline struct usb_hcd *fotg210_to_hcd(struct fotg210_hcd *fotg210)
  15484. -{
  15485. - return container_of((void *) fotg210, struct usb_hcd, hcd_priv);
  15486. -}
  15487. -
  15488. -/*-------------------------------------------------------------------------*/
  15489. -
  15490. -/* EHCI register interface, corresponds to EHCI Revision 0.95 specification */
  15491. -
  15492. -/* Section 2.2 Host Controller Capability Registers */
  15493. -struct fotg210_caps {
  15494. - /* these fields are specified as 8 and 16 bit registers,
  15495. - * but some hosts can't perform 8 or 16 bit PCI accesses.
  15496. - * some hosts treat caplength and hciversion as parts of a 32-bit
  15497. - * register, others treat them as two separate registers, this
  15498. - * affects the memory map for big endian controllers.
  15499. - */
  15500. - u32 hc_capbase;
  15501. -#define HC_LENGTH(fotg210, p) (0x00ff&((p) >> /* bits 7:0 / offset 00h */ \
  15502. - (fotg210_big_endian_capbase(fotg210) ? 24 : 0)))
  15503. -#define HC_VERSION(fotg210, p) (0xffff&((p) >> /* bits 31:16 / offset 02h */ \
  15504. - (fotg210_big_endian_capbase(fotg210) ? 0 : 16)))
  15505. - u32 hcs_params; /* HCSPARAMS - offset 0x4 */
  15506. -#define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
  15507. -
  15508. - u32 hcc_params; /* HCCPARAMS - offset 0x8 */
  15509. -#define HCC_CANPARK(p) ((p)&(1 << 2)) /* true: can park on async qh */
  15510. -#define HCC_PGM_FRAMELISTLEN(p) ((p)&(1 << 1)) /* true: periodic_size changes*/
  15511. - u8 portroute[8]; /* nibbles for routing - offset 0xC */
  15512. -};
  15513. -
  15514. -
  15515. -/* Section 2.3 Host Controller Operational Registers */
  15516. -struct fotg210_regs {
  15517. -
  15518. - /* USBCMD: offset 0x00 */
  15519. - u32 command;
  15520. -
  15521. -/* EHCI 1.1 addendum */
  15522. -/* 23:16 is r/w intr rate, in microframes; default "8" == 1/msec */
  15523. -#define CMD_PARK (1<<11) /* enable "park" on async qh */
  15524. -#define CMD_PARK_CNT(c) (((c)>>8)&3) /* how many transfers to park for */
  15525. -#define CMD_IAAD (1<<6) /* "doorbell" interrupt async advance */
  15526. -#define CMD_ASE (1<<5) /* async schedule enable */
  15527. -#define CMD_PSE (1<<4) /* periodic schedule enable */
  15528. -/* 3:2 is periodic frame list size */
  15529. -#define CMD_RESET (1<<1) /* reset HC not bus */
  15530. -#define CMD_RUN (1<<0) /* start/stop HC */
  15531. -
  15532. - /* USBSTS: offset 0x04 */
  15533. - u32 status;
  15534. -#define STS_ASS (1<<15) /* Async Schedule Status */
  15535. -#define STS_PSS (1<<14) /* Periodic Schedule Status */
  15536. -#define STS_RECL (1<<13) /* Reclamation */
  15537. -#define STS_HALT (1<<12) /* Not running (any reason) */
  15538. -/* some bits reserved */
  15539. - /* these STS_* flags are also intr_enable bits (USBINTR) */
  15540. -#define STS_IAA (1<<5) /* Interrupted on async advance */
  15541. -#define STS_FATAL (1<<4) /* such as some PCI access errors */
  15542. -#define STS_FLR (1<<3) /* frame list rolled over */
  15543. -#define STS_PCD (1<<2) /* port change detect */
  15544. -#define STS_ERR (1<<1) /* "error" completion (overflow, ...) */
  15545. -#define STS_INT (1<<0) /* "normal" completion (short, ...) */
  15546. -
  15547. - /* USBINTR: offset 0x08 */
  15548. - u32 intr_enable;
  15549. -
  15550. - /* FRINDEX: offset 0x0C */
  15551. - u32 frame_index; /* current microframe number */
  15552. - /* CTRLDSSEGMENT: offset 0x10 */
  15553. - u32 segment; /* address bits 63:32 if needed */
  15554. - /* PERIODICLISTBASE: offset 0x14 */
  15555. - u32 frame_list; /* points to periodic list */
  15556. - /* ASYNCLISTADDR: offset 0x18 */
  15557. - u32 async_next; /* address of next async queue head */
  15558. -
  15559. - u32 reserved1;
  15560. - /* PORTSC: offset 0x20 */
  15561. - u32 port_status;
  15562. -/* 31:23 reserved */
  15563. -#define PORT_USB11(x) (((x)&(3<<10)) == (1<<10)) /* USB 1.1 device */
  15564. -#define PORT_RESET (1<<8) /* reset port */
  15565. -#define PORT_SUSPEND (1<<7) /* suspend port */
  15566. -#define PORT_RESUME (1<<6) /* resume it */
  15567. -#define PORT_PEC (1<<3) /* port enable change */
  15568. -#define PORT_PE (1<<2) /* port enable */
  15569. -#define PORT_CSC (1<<1) /* connect status change */
  15570. -#define PORT_CONNECT (1<<0) /* device connected */
  15571. -#define PORT_RWC_BITS (PORT_CSC | PORT_PEC)
  15572. - u32 reserved2[19];
  15573. -
  15574. - /* OTGCSR: offet 0x70 */
  15575. - u32 otgcsr;
  15576. -#define OTGCSR_HOST_SPD_TYP (3 << 22)
  15577. -#define OTGCSR_A_BUS_DROP (1 << 5)
  15578. -#define OTGCSR_A_BUS_REQ (1 << 4)
  15579. -
  15580. - /* OTGISR: offset 0x74 */
  15581. - u32 otgisr;
  15582. -#define OTGISR_OVC (1 << 10)
  15583. -
  15584. - u32 reserved3[15];
  15585. -
  15586. - /* GMIR: offset 0xB4 */
  15587. - u32 gmir;
  15588. -#define GMIR_INT_POLARITY (1 << 3) /*Active High*/
  15589. -#define GMIR_MHC_INT (1 << 2)
  15590. -#define GMIR_MOTG_INT (1 << 1)
  15591. -#define GMIR_MDEV_INT (1 << 0)
  15592. -};
  15593. -
  15594. -/*-------------------------------------------------------------------------*/
  15595. -
  15596. -#define QTD_NEXT(fotg210, dma) cpu_to_hc32(fotg210, (u32)dma)
  15597. -
  15598. -/*
  15599. - * EHCI Specification 0.95 Section 3.5
  15600. - * QTD: describe data transfer components (buffer, direction, ...)
  15601. - * See Fig 3-6 "Queue Element Transfer Descriptor Block Diagram".
  15602. - *
  15603. - * These are associated only with "QH" (Queue Head) structures,
  15604. - * used with control, bulk, and interrupt transfers.
  15605. - */
  15606. -struct fotg210_qtd {
  15607. - /* first part defined by EHCI spec */
  15608. - __hc32 hw_next; /* see EHCI 3.5.1 */
  15609. - __hc32 hw_alt_next; /* see EHCI 3.5.2 */
  15610. - __hc32 hw_token; /* see EHCI 3.5.3 */
  15611. -#define QTD_TOGGLE (1 << 31) /* data toggle */
  15612. -#define QTD_LENGTH(tok) (((tok)>>16) & 0x7fff)
  15613. -#define QTD_IOC (1 << 15) /* interrupt on complete */
  15614. -#define QTD_CERR(tok) (((tok)>>10) & 0x3)
  15615. -#define QTD_PID(tok) (((tok)>>8) & 0x3)
  15616. -#define QTD_STS_ACTIVE (1 << 7) /* HC may execute this */
  15617. -#define QTD_STS_HALT (1 << 6) /* halted on error */
  15618. -#define QTD_STS_DBE (1 << 5) /* data buffer error (in HC) */
  15619. -#define QTD_STS_BABBLE (1 << 4) /* device was babbling (qtd halted) */
  15620. -#define QTD_STS_XACT (1 << 3) /* device gave illegal response */
  15621. -#define QTD_STS_MMF (1 << 2) /* incomplete split transaction */
  15622. -#define QTD_STS_STS (1 << 1) /* split transaction state */
  15623. -#define QTD_STS_PING (1 << 0) /* issue PING? */
  15624. -
  15625. -#define ACTIVE_BIT(fotg210) cpu_to_hc32(fotg210, QTD_STS_ACTIVE)
  15626. -#define HALT_BIT(fotg210) cpu_to_hc32(fotg210, QTD_STS_HALT)
  15627. -#define STATUS_BIT(fotg210) cpu_to_hc32(fotg210, QTD_STS_STS)
  15628. -
  15629. - __hc32 hw_buf[5]; /* see EHCI 3.5.4 */
  15630. - __hc32 hw_buf_hi[5]; /* Appendix B */
  15631. -
  15632. - /* the rest is HCD-private */
  15633. - dma_addr_t qtd_dma; /* qtd address */
  15634. - struct list_head qtd_list; /* sw qtd list */
  15635. - struct urb *urb; /* qtd's urb */
  15636. - size_t length; /* length of buffer */
  15637. -} __aligned(32);
  15638. -
  15639. -/* mask NakCnt+T in qh->hw_alt_next */
  15640. -#define QTD_MASK(fotg210) cpu_to_hc32(fotg210, ~0x1f)
  15641. -
  15642. -#define IS_SHORT_READ(token) (QTD_LENGTH(token) != 0 && QTD_PID(token) == 1)
  15643. -
  15644. -/*-------------------------------------------------------------------------*/
  15645. -
  15646. -/* type tag from {qh,itd,fstn}->hw_next */
  15647. -#define Q_NEXT_TYPE(fotg210, dma) ((dma) & cpu_to_hc32(fotg210, 3 << 1))
  15648. -
  15649. -/*
  15650. - * Now the following defines are not converted using the
  15651. - * cpu_to_le32() macro anymore, since we have to support
  15652. - * "dynamic" switching between be and le support, so that the driver
  15653. - * can be used on one system with SoC EHCI controller using big-endian
  15654. - * descriptors as well as a normal little-endian PCI EHCI controller.
  15655. - */
  15656. -/* values for that type tag */
  15657. -#define Q_TYPE_ITD (0 << 1)
  15658. -#define Q_TYPE_QH (1 << 1)
  15659. -#define Q_TYPE_SITD (2 << 1)
  15660. -#define Q_TYPE_FSTN (3 << 1)
  15661. -
  15662. -/* next async queue entry, or pointer to interrupt/periodic QH */
  15663. -#define QH_NEXT(fotg210, dma) \
  15664. - (cpu_to_hc32(fotg210, (((u32)dma)&~0x01f)|Q_TYPE_QH))
  15665. -
  15666. -/* for periodic/async schedules and qtd lists, mark end of list */
  15667. -#define FOTG210_LIST_END(fotg210) \
  15668. - cpu_to_hc32(fotg210, 1) /* "null pointer" to hw */
  15669. -
  15670. -/*
  15671. - * Entries in periodic shadow table are pointers to one of four kinds
  15672. - * of data structure. That's dictated by the hardware; a type tag is
  15673. - * encoded in the low bits of the hardware's periodic schedule. Use
  15674. - * Q_NEXT_TYPE to get the tag.
  15675. - *
  15676. - * For entries in the async schedule, the type tag always says "qh".
  15677. - */
  15678. -union fotg210_shadow {
  15679. - struct fotg210_qh *qh; /* Q_TYPE_QH */
  15680. - struct fotg210_itd *itd; /* Q_TYPE_ITD */
  15681. - struct fotg210_fstn *fstn; /* Q_TYPE_FSTN */
  15682. - __hc32 *hw_next; /* (all types) */
  15683. - void *ptr;
  15684. -};
  15685. -
  15686. -/*-------------------------------------------------------------------------*/
  15687. -
  15688. -/*
  15689. - * EHCI Specification 0.95 Section 3.6
  15690. - * QH: describes control/bulk/interrupt endpoints
  15691. - * See Fig 3-7 "Queue Head Structure Layout".
  15692. - *
  15693. - * These appear in both the async and (for interrupt) periodic schedules.
  15694. - */
  15695. -
  15696. -/* first part defined by EHCI spec */
  15697. -struct fotg210_qh_hw {
  15698. - __hc32 hw_next; /* see EHCI 3.6.1 */
  15699. - __hc32 hw_info1; /* see EHCI 3.6.2 */
  15700. -#define QH_CONTROL_EP (1 << 27) /* FS/LS control endpoint */
  15701. -#define QH_HEAD (1 << 15) /* Head of async reclamation list */
  15702. -#define QH_TOGGLE_CTL (1 << 14) /* Data toggle control */
  15703. -#define QH_HIGH_SPEED (2 << 12) /* Endpoint speed */
  15704. -#define QH_LOW_SPEED (1 << 12)
  15705. -#define QH_FULL_SPEED (0 << 12)
  15706. -#define QH_INACTIVATE (1 << 7) /* Inactivate on next transaction */
  15707. - __hc32 hw_info2; /* see EHCI 3.6.2 */
  15708. -#define QH_SMASK 0x000000ff
  15709. -#define QH_CMASK 0x0000ff00
  15710. -#define QH_HUBADDR 0x007f0000
  15711. -#define QH_HUBPORT 0x3f800000
  15712. -#define QH_MULT 0xc0000000
  15713. - __hc32 hw_current; /* qtd list - see EHCI 3.6.4 */
  15714. -
  15715. - /* qtd overlay (hardware parts of a struct fotg210_qtd) */
  15716. - __hc32 hw_qtd_next;
  15717. - __hc32 hw_alt_next;
  15718. - __hc32 hw_token;
  15719. - __hc32 hw_buf[5];
  15720. - __hc32 hw_buf_hi[5];
  15721. -} __aligned(32);
  15722. -
  15723. -struct fotg210_qh {
  15724. - struct fotg210_qh_hw *hw; /* Must come first */
  15725. - /* the rest is HCD-private */
  15726. - dma_addr_t qh_dma; /* address of qh */
  15727. - union fotg210_shadow qh_next; /* ptr to qh; or periodic */
  15728. - struct list_head qtd_list; /* sw qtd list */
  15729. - struct list_head intr_node; /* list of intr QHs */
  15730. - struct fotg210_qtd *dummy;
  15731. - struct fotg210_qh *unlink_next; /* next on unlink list */
  15732. -
  15733. - unsigned unlink_cycle;
  15734. -
  15735. - u8 needs_rescan; /* Dequeue during giveback */
  15736. - u8 qh_state;
  15737. -#define QH_STATE_LINKED 1 /* HC sees this */
  15738. -#define QH_STATE_UNLINK 2 /* HC may still see this */
  15739. -#define QH_STATE_IDLE 3 /* HC doesn't see this */
  15740. -#define QH_STATE_UNLINK_WAIT 4 /* LINKED and on unlink q */
  15741. -#define QH_STATE_COMPLETING 5 /* don't touch token.HALT */
  15742. -
  15743. - u8 xacterrs; /* XactErr retry counter */
  15744. -#define QH_XACTERR_MAX 32 /* XactErr retry limit */
  15745. -
  15746. - /* periodic schedule info */
  15747. - u8 usecs; /* intr bandwidth */
  15748. - u8 gap_uf; /* uframes split/csplit gap */
  15749. - u8 c_usecs; /* ... split completion bw */
  15750. - u16 tt_usecs; /* tt downstream bandwidth */
  15751. - unsigned short period; /* polling interval */
  15752. - unsigned short start; /* where polling starts */
  15753. -#define NO_FRAME ((unsigned short)~0) /* pick new start */
  15754. -
  15755. - struct usb_device *dev; /* access to TT */
  15756. - unsigned is_out:1; /* bulk or intr OUT */
  15757. - unsigned clearing_tt:1; /* Clear-TT-Buf in progress */
  15758. -};
  15759. -
  15760. -/*-------------------------------------------------------------------------*/
  15761. -
  15762. -/* description of one iso transaction (up to 3 KB data if highspeed) */
  15763. -struct fotg210_iso_packet {
  15764. - /* These will be copied to iTD when scheduling */
  15765. - u64 bufp; /* itd->hw_bufp{,_hi}[pg] |= */
  15766. - __hc32 transaction; /* itd->hw_transaction[i] |= */
  15767. - u8 cross; /* buf crosses pages */
  15768. - /* for full speed OUT splits */
  15769. - u32 buf1;
  15770. -};
  15771. -
  15772. -/* temporary schedule data for packets from iso urbs (both speeds)
  15773. - * each packet is one logical usb transaction to the device (not TT),
  15774. - * beginning at stream->next_uframe
  15775. - */
  15776. -struct fotg210_iso_sched {
  15777. - struct list_head td_list;
  15778. - unsigned span;
  15779. - struct fotg210_iso_packet packet[];
  15780. -};
  15781. -
  15782. -/*
  15783. - * fotg210_iso_stream - groups all (s)itds for this endpoint.
  15784. - * acts like a qh would, if EHCI had them for ISO.
  15785. - */
  15786. -struct fotg210_iso_stream {
  15787. - /* first field matches fotg210_hq, but is NULL */
  15788. - struct fotg210_qh_hw *hw;
  15789. -
  15790. - u8 bEndpointAddress;
  15791. - u8 highspeed;
  15792. - struct list_head td_list; /* queued itds */
  15793. - struct list_head free_list; /* list of unused itds */
  15794. - struct usb_device *udev;
  15795. - struct usb_host_endpoint *ep;
  15796. -
  15797. - /* output of (re)scheduling */
  15798. - int next_uframe;
  15799. - __hc32 splits;
  15800. -
  15801. - /* the rest is derived from the endpoint descriptor,
  15802. - * trusting urb->interval == f(epdesc->bInterval) and
  15803. - * including the extra info for hw_bufp[0..2]
  15804. - */
  15805. - u8 usecs, c_usecs;
  15806. - u16 interval;
  15807. - u16 tt_usecs;
  15808. - u16 maxp;
  15809. - u16 raw_mask;
  15810. - unsigned bandwidth;
  15811. -
  15812. - /* This is used to initialize iTD's hw_bufp fields */
  15813. - __hc32 buf0;
  15814. - __hc32 buf1;
  15815. - __hc32 buf2;
  15816. -
  15817. - /* this is used to initialize sITD's tt info */
  15818. - __hc32 address;
  15819. -};
  15820. -
  15821. -/*-------------------------------------------------------------------------*/
  15822. -
  15823. -/*
  15824. - * EHCI Specification 0.95 Section 3.3
  15825. - * Fig 3-4 "Isochronous Transaction Descriptor (iTD)"
  15826. - *
  15827. - * Schedule records for high speed iso xfers
  15828. - */
  15829. -struct fotg210_itd {
  15830. - /* first part defined by EHCI spec */
  15831. - __hc32 hw_next; /* see EHCI 3.3.1 */
  15832. - __hc32 hw_transaction[8]; /* see EHCI 3.3.2 */
  15833. -#define FOTG210_ISOC_ACTIVE (1<<31) /* activate transfer this slot */
  15834. -#define FOTG210_ISOC_BUF_ERR (1<<30) /* Data buffer error */
  15835. -#define FOTG210_ISOC_BABBLE (1<<29) /* babble detected */
  15836. -#define FOTG210_ISOC_XACTERR (1<<28) /* XactErr - transaction error */
  15837. -#define FOTG210_ITD_LENGTH(tok) (((tok)>>16) & 0x0fff)
  15838. -#define FOTG210_ITD_IOC (1 << 15) /* interrupt on complete */
  15839. -
  15840. -#define ITD_ACTIVE(fotg210) cpu_to_hc32(fotg210, FOTG210_ISOC_ACTIVE)
  15841. -
  15842. - __hc32 hw_bufp[7]; /* see EHCI 3.3.3 */
  15843. - __hc32 hw_bufp_hi[7]; /* Appendix B */
  15844. -
  15845. - /* the rest is HCD-private */
  15846. - dma_addr_t itd_dma; /* for this itd */
  15847. - union fotg210_shadow itd_next; /* ptr to periodic q entry */
  15848. -
  15849. - struct urb *urb;
  15850. - struct fotg210_iso_stream *stream; /* endpoint's queue */
  15851. - struct list_head itd_list; /* list of stream's itds */
  15852. -
  15853. - /* any/all hw_transactions here may be used by that urb */
  15854. - unsigned frame; /* where scheduled */
  15855. - unsigned pg;
  15856. - unsigned index[8]; /* in urb->iso_frame_desc */
  15857. -} __aligned(32);
  15858. -
  15859. -/*-------------------------------------------------------------------------*/
  15860. -
  15861. -/*
  15862. - * EHCI Specification 0.96 Section 3.7
  15863. - * Periodic Frame Span Traversal Node (FSTN)
  15864. - *
  15865. - * Manages split interrupt transactions (using TT) that span frame boundaries
  15866. - * into uframes 0/1; see 4.12.2.2. In those uframes, a "save place" FSTN
  15867. - * makes the HC jump (back) to a QH to scan for fs/ls QH completions until
  15868. - * it hits a "restore" FSTN; then it returns to finish other uframe 0/1 work.
  15869. - */
  15870. -struct fotg210_fstn {
  15871. - __hc32 hw_next; /* any periodic q entry */
  15872. - __hc32 hw_prev; /* qh or FOTG210_LIST_END */
  15873. -
  15874. - /* the rest is HCD-private */
  15875. - dma_addr_t fstn_dma;
  15876. - union fotg210_shadow fstn_next; /* ptr to periodic q entry */
  15877. -} __aligned(32);
  15878. -
  15879. -/*-------------------------------------------------------------------------*/
  15880. -
  15881. -/* Prepare the PORTSC wakeup flags during controller suspend/resume */
  15882. -
  15883. -#define fotg210_prepare_ports_for_controller_suspend(fotg210, do_wakeup) \
  15884. - fotg210_adjust_port_wakeup_flags(fotg210, true, do_wakeup)
  15885. -
  15886. -#define fotg210_prepare_ports_for_controller_resume(fotg210) \
  15887. - fotg210_adjust_port_wakeup_flags(fotg210, false, false)
  15888. -
  15889. -/*-------------------------------------------------------------------------*/
  15890. -
  15891. -/*
  15892. - * Some EHCI controllers have a Transaction Translator built into the
  15893. - * root hub. This is a non-standard feature. Each controller will need
  15894. - * to add code to the following inline functions, and call them as
  15895. - * needed (mostly in root hub code).
  15896. - */
  15897. -
  15898. -static inline unsigned int
  15899. -fotg210_get_speed(struct fotg210_hcd *fotg210, unsigned int portsc)
  15900. -{
  15901. - return (readl(&fotg210->regs->otgcsr)
  15902. - & OTGCSR_HOST_SPD_TYP) >> 22;
  15903. -}
  15904. -
  15905. -/* Returns the speed of a device attached to a port on the root hub. */
  15906. -static inline unsigned int
  15907. -fotg210_port_speed(struct fotg210_hcd *fotg210, unsigned int portsc)
  15908. -{
  15909. - switch (fotg210_get_speed(fotg210, portsc)) {
  15910. - case 0:
  15911. - return 0;
  15912. - case 1:
  15913. - return USB_PORT_STAT_LOW_SPEED;
  15914. - case 2:
  15915. - default:
  15916. - return USB_PORT_STAT_HIGH_SPEED;
  15917. - }
  15918. -}
  15919. -
  15920. -/*-------------------------------------------------------------------------*/
  15921. -
  15922. -#define fotg210_has_fsl_portno_bug(e) (0)
  15923. -
  15924. -/*
  15925. - * While most USB host controllers implement their registers in
  15926. - * little-endian format, a minority (celleb companion chip) implement
  15927. - * them in big endian format.
  15928. - *
  15929. - * This attempts to support either format at compile time without a
  15930. - * runtime penalty, or both formats with the additional overhead
  15931. - * of checking a flag bit.
  15932. - *
  15933. - */
  15934. -
  15935. -#define fotg210_big_endian_mmio(e) 0
  15936. -#define fotg210_big_endian_capbase(e) 0
  15937. -
  15938. -static inline unsigned int fotg210_readl(const struct fotg210_hcd *fotg210,
  15939. - __u32 __iomem *regs)
  15940. -{
  15941. - return readl(regs);
  15942. -}
  15943. -
  15944. -static inline void fotg210_writel(const struct fotg210_hcd *fotg210,
  15945. - const unsigned int val, __u32 __iomem *regs)
  15946. -{
  15947. - writel(val, regs);
  15948. -}
  15949. -
  15950. -/* cpu to fotg210 */
  15951. -static inline __hc32 cpu_to_hc32(const struct fotg210_hcd *fotg210, const u32 x)
  15952. -{
  15953. - return cpu_to_le32(x);
  15954. -}
  15955. -
  15956. -/* fotg210 to cpu */
  15957. -static inline u32 hc32_to_cpu(const struct fotg210_hcd *fotg210, const __hc32 x)
  15958. -{
  15959. - return le32_to_cpu(x);
  15960. -}
  15961. -
  15962. -static inline u32 hc32_to_cpup(const struct fotg210_hcd *fotg210,
  15963. - const __hc32 *x)
  15964. -{
  15965. - return le32_to_cpup(x);
  15966. -}
  15967. -
  15968. -/*-------------------------------------------------------------------------*/
  15969. -
  15970. -static inline unsigned fotg210_read_frame_index(struct fotg210_hcd *fotg210)
  15971. -{
  15972. - return fotg210_readl(fotg210, &fotg210->regs->frame_index);
  15973. -}
  15974. -
  15975. -/*-------------------------------------------------------------------------*/
  15976. -
  15977. -#endif /* __LINUX_FOTG210_H */