fotg210-hcd.c 164 KB

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