fotg210-hcd.c 157 KB

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