fotg210-hcd.c 157 KB

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