fotg210-hcd.c 162 KB

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