fotg210-hcd.c 158 KB

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