fotg210-hcd.c 155 KB

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