2
0

950-0037-Add-dwc_otg-driver.patch 1.7 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098
  1. From 451dab6b675762f8889979b04ee3e529eee915e8 Mon Sep 17 00:00:00 2001
  2. From: popcornmix <[email protected]>
  3. Date: Wed, 1 May 2013 19:46:17 +0100
  4. Subject: [PATCH 037/454] Add dwc_otg driver
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Signed-off-by: popcornmix <[email protected]>
  9. usb: dwc: fix lockdep false positive
  10. Signed-off-by: Kari Suvanto <[email protected]>
  11. usb: dwc: fix inconsistent lock state
  12. Signed-off-by: Kari Suvanto <[email protected]>
  13. Add FIQ patch to dwc_otg driver. Enable with dwc_otg.fiq_fix_enable=1. Should give about 10% more ARM performance.
  14. Thanks to Gordon and Costas
  15. Avoid dynamic memory allocation for channel lock in USB driver. Thanks ddv2005.
  16. Add NAK holdoff scheme. Enabled by default, disable with dwc_otg.nak_holdoff_enable=0. Thanks gsh
  17. Make sure we wait for the reset to finish
  18. dwc_otg: fix bug in dwc_otg_hcd.c resulting in silent kernel
  19. memory corruption, escalating to OOPS under high USB load.
  20. dwc_otg: Fix unsafe access of QTD during URB enqueue
  21. In dwc_otg_hcd_urb_enqueue during qtd creation, it was possible that the
  22. transaction could complete almost immediately after the qtd was assigned
  23. to a host channel during URB enqueue, which meant the qtd pointer was no
  24. longer valid having been completed and removed. Usually, this resulted in
  25. an OOPS during URB submission. By predetermining whether transactions
  26. need to be queued or not, this unsafe pointer access is avoided.
  27. This bug was only evident on the Pi model A where a device was attached
  28. that had no periodic endpoints (e.g. USB pendrive or some wlan devices).
  29. dwc_otg: Fix incorrect URB allocation error handling
  30. If the memory allocation for a dwc_otg_urb failed, the kernel would OOPS
  31. because for some reason a member of the *unallocated* struct was set to
  32. zero. Error handling changed to fail correctly.
  33. dwc_otg: fix potential use-after-free case in interrupt handler
  34. If a transaction had previously aborted, certain interrupts are
  35. enabled to track error counts and reset where necessary. On IN
  36. endpoints the host generates an ACK interrupt near-simultaneously
  37. with completion of transfer. In the case where this transfer had
  38. previously had an error, this results in a use-after-free on
  39. the QTD memory space with a 1-byte length being overwritten to
  40. 0x00.
  41. dwc_otg: add handling of SPLIT transaction data toggle errors
  42. Previously a data toggle error on packets from a USB1.1 device behind
  43. a TT would result in the Pi locking up as the driver never handled
  44. the associated interrupt. Patch adds basic retry mechanism and
  45. interrupt acknowledgement to cater for either a chance toggle error or
  46. for devices that have a broken initial toggle state (FT8U232/FT232BM).
  47. dwc_otg: implement tasklet for returning URBs to usbcore hcd layer
  48. The dwc_otg driver interrupt handler for transfer completion will spend
  49. a very long time with interrupts disabled when a URB is completed -
  50. this is because usb_hcd_giveback_urb is called from within the handler
  51. which for a USB device driver with complicated processing (e.g. webcam)
  52. will take an exorbitant amount of time to complete. This results in
  53. missed completion interrupts for other USB packets which lead to them
  54. being dropped due to microframe overruns.
  55. This patch splits returning the URB to the usb hcd layer into a
  56. high-priority tasklet. This will have most benefit for isochronous IN
  57. transfers but will also have incidental benefit where multiple periodic
  58. devices are active at once.
  59. dwc_otg: fix NAK holdoff and allow on split transactions only
  60. This corrects a bug where if a single active non-periodic endpoint
  61. had at least one transaction in its qh, on frnum == MAX_FRNUM the qh
  62. would get skipped and never get queued again. This would result in
  63. a silent device until error detection (automatic or otherwise) would
  64. either reset the device or flush and requeue the URBs.
  65. Additionally the NAK holdoff was enabled for all transactions - this
  66. would potentially stall a HS endpoint for 1ms if a previous error state
  67. enabled this interrupt and the next response was a NAK. Fix so that
  68. only split transactions get held off.
  69. dwc_otg: Call usb_hcd_unlink_urb_from_ep with lock held in completion handler
  70. usb_hcd_unlink_urb_from_ep must be called with the HCD lock held. Calling it
  71. asynchronously in the tasklet was not safe (regression in
  72. c4564d4a1a0a9b10d4419e48239f5d99e88d2667).
  73. This change unlinks it from the endpoint prior to queueing it for handling in
  74. the tasklet, and also adds a check to ensure the urb is OK to be unlinked
  75. before doing so.
  76. NULL pointer dereference kernel oopses had been observed in usb_hcd_giveback_urb
  77. when a USB device was unplugged/replugged during data transfer. This effect
  78. was reproduced using automated USB port power control, hundreds of replug
  79. events were performed during active transfers to confirm that the problem was
  80. eliminated.
  81. USB fix using a FIQ to implement split transactions
  82. This commit adds a FIQ implementaion that schedules
  83. the split transactions using a FIQ so we don't get
  84. held off by the interrupt latency of Linux
  85. dwc_otg: fix device attributes and avoid kernel warnings on boot
  86. dcw_otg: avoid logging function that can cause panics
  87. See: https://github.com/raspberrypi/firmware/issues/21
  88. Thanks to cleverca22 for fix
  89. dwc_otg: mask correct interrupts after transaction error recovery
  90. The dwc_otg driver will unmask certain interrupts on a transaction
  91. that previously halted in the error state in order to reset the
  92. QTD error count. The various fine-grained interrupt handlers do not
  93. consider that other interrupts besides themselves were unmasked.
  94. By disabling the two other interrupts only ever enabled in DMA mode
  95. for this purpose, we can avoid unnecessary function calls in the
  96. IRQ handler. This will also prevent an unneccesary FIQ interrupt
  97. from being generated if the FIQ is enabled.
  98. dwc_otg: fiq: prevent FIQ thrash and incorrect state passing to IRQ
  99. In the case of a transaction to a device that had previously aborted
  100. due to an error, several interrupts are enabled to reset the error
  101. count when a device responds. This has the side-effect of making the
  102. FIQ thrash because the hardware will generate multiple instances of
  103. a NAK on an IN bulk/interrupt endpoint and multiple instances of ACK
  104. on an OUT bulk/interrupt endpoint. Make the FIQ mask and clear the
  105. associated interrupts.
  106. Additionally, on non-split transactions make sure that only unmasked
  107. interrupts are cleared. This caused a hard-to-trigger but serious
  108. race condition when you had the combination of an endpoint awaiting
  109. error recovery and a transaction completed on an endpoint - due to
  110. the sequencing and timing of interrupts generated by the dwc_otg core,
  111. it was possible to confuse the IRQ handler.
  112. Fix function tracing
  113. dwc_otg: whitespace cleanup in dwc_otg_urb_enqueue
  114. dwc_otg: prevent OOPSes during device disconnects
  115. The dwc_otg_urb_enqueue function is thread-unsafe. In particular the
  116. access of urb->hcpriv, usb_hcd_link_urb_to_ep, dwc_otg_urb->qtd and
  117. friends does not occur within a critical section and so if a device
  118. was unplugged during activity there was a high chance that the
  119. usbcore hub_thread would try to disable the endpoint with partially-
  120. formed entries in the URB queue. This would result in BUG() or null
  121. pointer dereferences.
  122. Fix so that access of urb->hcpriv, enqueuing to the hardware and
  123. adding to usbcore endpoint URB lists is contained within a single
  124. critical section.
  125. dwc_otg: prevent BUG() in TT allocation if hub address is > 16
  126. A fixed-size array is used to track TT allocation. This was
  127. previously set to 16 which caused a crash because
  128. dwc_otg_hcd_allocate_port would read past the end of the array.
  129. This was hit if a hub was plugged in which enumerated as addr > 16,
  130. due to previous device resets or unplugs.
  131. Also add #ifdef FIQ_DEBUG around hcd->hub_port_alloc[], which grows
  132. to a large size if 128 hub addresses are supported. This field is
  133. for debug only for tracking which frame an allocate happened in.
  134. dwc_otg: make channel halts with unknown state less damaging
  135. If the IRQ received a channel halt interrupt through the FIQ
  136. with no other bits set, the IRQ would not release the host
  137. channel and never complete the URB.
  138. Add catchall handling to treat as a transaction error and retry.
  139. dwc_otg: fiq_split: use TTs with more granularity
  140. This fixes certain issues with split transaction scheduling.
  141. - Isochronous multi-packet OUT transactions now hog the TT until
  142. they are completed - this prevents hubs aborting transactions
  143. if they get a periodic start-split out-of-order
  144. - Don't perform TT allocation on non-periodic endpoints - this
  145. allows simultaneous use of the TT's bulk/control and periodic
  146. transaction buffers
  147. This commit will mainly affect USB audio playback.
  148. dwc_otg: fix potential sleep while atomic during urb enqueue
  149. Fixes a regression introduced with eb1b482a. Kmalloc called from
  150. dwc_otg_hcd_qtd_add / dwc_otg_hcd_qtd_create did not always have
  151. the GPF_ATOMIC flag set. Force this flag when inside the larger
  152. critical section.
  153. dwc_otg: make fiq_split_enable imply fiq_fix_enable
  154. Failing to set up the FIQ correctly would result in
  155. "IRQ 32: nobody cared" errors in dmesg.
  156. dwc_otg: prevent crashes on host port disconnects
  157. Fix several issues resulting in crashes or inconsistent state
  158. if a Model A root port was disconnected.
  159. - Clean up queue heads properly in kill_urbs_in_qh_list by
  160. removing the empty QHs from the schedule lists
  161. - Set the halt status properly to prevent IRQ handlers from
  162. using freed memory
  163. - Add fiq_split related cleanup for saved registers
  164. - Make microframe scheduling reclaim host channels if
  165. active during a disconnect
  166. - Abort URBs with -ESHUTDOWN status response, informing
  167. device drivers so they respond in a more correct fashion
  168. and don't try to resubmit URBs
  169. - Prevent IRQ handlers from attempting to handle channel
  170. interrupts if the associated URB was dequeued (and the
  171. driver state was cleared)
  172. dwc_otg: prevent leaking URBs during enqueue
  173. A dwc_otg_urb would get leaked if the HCD enqueue function
  174. failed for any reason. Free the URB at the appropriate points.
  175. dwc_otg: Enable NAK holdoff for control split transactions
  176. Certain low-speed devices take a very long time to complete a
  177. data or status stage of a control transaction, producing NAK
  178. responses until they complete internal processing - the USB2.0
  179. spec limit is up to 500mS. This causes the same type of interrupt
  180. storm as seen with USB-serial dongles prior to c8edb238.
  181. In certain circumstances, usually while booting, this interrupt
  182. storm could cause SD card timeouts.
  183. dwc_otg: Fix for occasional lockup on boot when doing a USB reset
  184. dwc_otg: Don't issue traffic to LS devices in FS mode
  185. Issuing low-speed packets when the root port is in full-speed mode
  186. causes the root port to stop responding. Explicitly fail when
  187. enqueuing URBs to a LS endpoint on a FS bus.
  188. Fix ARM architecture issue with local_irq_restore()
  189. If local_fiq_enable() is called before a local_irq_restore(flags) where
  190. the flags variable has the F bit set, the FIQ will be erroneously disabled.
  191. Fixup arch_local_irq_restore to avoid trampling the F bit in CPSR.
  192. Also fix some of the hacks previously implemented for previous dwc_otg
  193. incarnations.
  194. dwc_otg: fiq_fsm: Base commit for driver rewrite
  195. This commit removes the previous FIQ fixes entirely and adds fiq_fsm.
  196. This rewrite features much more complete support for split transactions
  197. and takes into account several OTG hardware bugs. High-speed
  198. isochronous transactions are also capable of being performed by fiq_fsm.
  199. All driver options have been removed and replaced with:
  200. - dwc_otg.fiq_enable (bool)
  201. - dwc_otg.fiq_fsm_enable (bool)
  202. - dwc_otg.fiq_fsm_mask (bitmask)
  203. - dwc_otg.nak_holdoff (unsigned int)
  204. Defaults are specified such that fiq_fsm behaves similarly to the
  205. previously implemented FIQ fixes.
  206. fiq_fsm: Push error recovery into the FIQ when fiq_fsm is used
  207. If the transfer associated with a QTD failed due to a bus error, the HCD
  208. would retry the transfer up to 3 times (implementing the USB2.0
  209. three-strikes retry in software).
  210. Due to the masking mechanism used by fiq_fsm, it is only possible to pass
  211. a single interrupt through to the HCD per-transfer.
  212. In this instance host channels would fall off the radar because the error
  213. reset would function, but the subsequent channel halt would be lost.
  214. Push the error count reset into the FIQ handler.
  215. fiq_fsm: Implement timeout mechanism
  216. For full-speed endpoints with a large packet size, interrupt latency
  217. runs the risk of the FIQ starting a transaction too late in a full-speed
  218. frame. If the device is still transmitting data when EOF2 for the
  219. downstream frame occurs, the hub will disable the port. This change is
  220. not reflected in the hub status endpoint and the device becomes
  221. unresponsive.
  222. Prevent high-bandwidth transactions from being started too late in a
  223. frame. The mechanism is not guaranteed: a combination of bit stuffing
  224. and hub latency may still result in a device overrunning.
  225. fiq_fsm: fix bounce buffer utilisation for Isochronous OUT
  226. Multi-packet isochronous OUT transactions were subject to a few bounday
  227. bugs. Fix them.
  228. Audio playback is now much more robust: however, an issue stands with
  229. devices that have adaptive sinks - ALSA plays samples too fast.
  230. dwc_otg: Return full-speed frame numbers in HS mode
  231. The frame counter increments on every *microframe* in high-speed mode.
  232. Most device drivers expect this number to be in full-speed frames - this
  233. caused considerable confusion to e.g. snd_usb_audio which uses the
  234. frame counter to estimate the number of samples played.
  235. fiq_fsm: save PID on completion of interrupt OUT transfers
  236. Also add edge case handling for interrupt transports.
  237. Note that for periodic split IN, data toggles are unimplemented in the
  238. OTG host hardware - it unconditionally accepts any PID.
  239. fiq_fsm: add missing case for fiq_fsm_tt_in_use()
  240. Certain combinations of bitrate and endpoint activity could
  241. result in a periodic transaction erroneously getting started
  242. while the previous Isochronous OUT was still active.
  243. fiq_fsm: clear hcintmsk for aborted transactions
  244. Prevents the FIQ from erroneously handling interrupts
  245. on a timed out channel.
  246. fiq_fsm: enable by default
  247. fiq_fsm: fix dequeues for non-periodic split transactions
  248. If a dequeue happened between the SSPLIT and CSPLIT phases of the
  249. transaction, the HCD would never receive an interrupt.
  250. fiq_fsm: Disable by default
  251. fiq_fsm: Handle HC babble errors
  252. The HCTSIZ transfer size field raises a babble interrupt if
  253. the counter wraps. Handle the resulting interrupt in this case.
  254. dwc_otg: fix interrupt registration for fiq_enable=0
  255. Additionally make the module parameter conditional for wherever
  256. hcd->fiq_state is touched.
  257. fiq_fsm: Enable by default
  258. dwc_otg: Fix various issues with root port and transaction errors
  259. Process the host port interrupts correctly (and don't trample them).
  260. Root port hotplug now functional again.
  261. Fix a few thinkos with the transaction error passthrough for fiq_fsm.
  262. fiq_fsm: Implement hack for Split Interrupt transactions
  263. Hubs aren't too picky about which endpoint we send Control type split
  264. transactions to. By treating Interrupt transfers as Control, it is
  265. possible to use the non-periodic queue in the OTG core as well as the
  266. non-periodic FIFOs in the hub itself. This massively reduces the
  267. microframe exclusivity/contention that periodic split transactions
  268. otherwise have to enforce.
  269. It goes without saying that this is a fairly egregious USB specification
  270. violation, but it works.
  271. Original idea by Hans Petter Selasky @ FreeBSD.org.
  272. dwc_otg: FIQ support on SMP. Set up FIQ stack and handler on Core 0 only.
  273. dwc_otg: introduce fiq_fsm_spin(un|)lock()
  274. SMP safety for the FIQ relies on register read-modify write cycles being
  275. completed in the correct order. Several places in the DWC code modify
  276. registers also touched by the FIQ. Protect these by a bare-bones lock
  277. mechanism.
  278. This also makes it possible to run the FIQ and IRQ handlers on different
  279. cores.
  280. fiq_fsm: fix build on bcm2708 and bcm2709 platforms
  281. dwc_otg: put some barriers back where they should be for UP
  282. bcm2709/dwc_otg: Setup FIQ on core 1 if >1 core active
  283. dwc_otg: fixup read-modify-write in critical paths
  284. Be more careful about read-modify-write on registers that the FIQ
  285. also touches.
  286. Guard fiq_fsm_spin_lock with fiq_enable check
  287. fiq_fsm: Falling out of the state machine isn't fatal
  288. This edge case can be hit if the port is disabled while the FIQ is
  289. in the middle of a transaction. Make the effects less severe.
  290. Also get rid of the useless return value.
  291. squash: dwc_otg: Allow to build without SMP
  292. usb: core: make overcurrent messages more prominent
  293. Hub overcurrent messages are more serious than "debug". Increase loglevel.
  294. usb: dwc_otg: Don't use dma_to_virt()
  295. Commit 6ce0d20 changes dma_to_virt() which breaks this driver.
  296. Open code the old dma_to_virt() implementation to work around this.
  297. Limit the use of __bus_to_virt() to cases where transfer_buffer_length
  298. is set and transfer_buffer is not set. This is done to increase the
  299. chance that this driver will also work on ARCH_BCM2835.
  300. transfer_buffer should not be NULL if the length is set, but the
  301. comment in the code indicates that there are situations where this
  302. might happen. drivers/usb/isp1760/isp1760-hcd.c also has a similar
  303. comment pointing to a possible: 'usb storage / SCSI bug'.
  304. Signed-off-by: Noralf Trønnes <[email protected]>
  305. dwc_otg: Fix crash when fiq_enable=0
  306. dwc_otg: fiq_fsm: Make high-speed isochronous strided transfers work properly
  307. Certain low-bandwidth high-speed USB devices (specialist audio devices,
  308. compressed-frame webcams) have packet intervals > 1 microframe.
  309. Stride these transfers in the FIQ by using the start-of-frame interrupt
  310. to restart the channel at the right time.
  311. dwc_otg: Force host mode to fix incorrect compute module boards
  312. dwc_otg: Add ARCH_BCM2835 support
  313. Signed-off-by: Noralf Trønnes <[email protected]>
  314. dwc_otg: Simplify FIQ irq number code
  315. Dropping ATAGS means we can simplify the FIQ irq number code.
  316. Also add error checking on the returned irq number.
  317. Signed-off-by: Noralf Trønnes <[email protected]>
  318. dwc_otg: Remove duplicate gadget probe/unregister function
  319. dwc_otg: Properly set the HFIR
  320. Douglas Anderson reported:
  321. According to the most up to date version of the dwc2 databook, the FRINT
  322. field of the HFIR register should be programmed to:
  323. * 125 us * (PHY clock freq for HS) - 1
  324. * 1000 us * (PHY clock freq for FS/LS) - 1
  325. This is opposed to older versions of the doc that claimed it should be:
  326. * 125 us * (PHY clock freq for HS)
  327. * 1000 us * (PHY clock freq for FS/LS)
  328. and reported lower timing jitter on a USB analyser
  329. dcw_otg: trim xfer length when buffer larger than allocated size is received
  330. dwc_otg: Don't free qh align buffers in atomic context
  331. dwc_otg: Enable the hack for Split Interrupt transactions by default
  332. dwc_otg.fiq_fsm_mask=0xF has long been a suggestion for users with audio stutters or other USB bandwidth issues.
  333. So far we are aware of many success stories but no failure caused by this setting.
  334. Make it a default to learn more.
  335. See: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=70437
  336. Signed-off-by: popcornmix <[email protected]>
  337. dwc_otg: Use kzalloc when suitable
  338. dwc_otg: Pass struct device to dma_alloc*()
  339. This makes it possible to get the bus address from Device Tree.
  340. Signed-off-by: Noralf Trønnes <[email protected]>
  341. dwc_otg: fix summarize urb->actual_length for isochronous transfers
  342. Kernel does not copy input data of ISO transfers to userspace
  343. if actual_length is set only in ISO transfers and not summarized
  344. in urb->actual_length. Fixes raspberrypi/linux#903
  345. fiq_fsm: Use correct states when starting isoc OUT transfers
  346. In fiq_fsm_start_next_periodic() if an isochronous OUT transfer
  347. was selected, no regard was given as to whether this was a single-packet
  348. transfer or a multi-packet staged transfer.
  349. For single-packet transfers, this had the effect of repeatedly sending
  350. OUT packets with bogus data and lengths.
  351. Eventually if the channel was repeatedly enabled enough times, this
  352. would lock up the OTG core and no further bus transfers would happen.
  353. Set the FSM state up properly if we select a single-packet transfer.
  354. Fixes https://github.com/raspberrypi/linux/issues/1842
  355. dwc_otg: make nak_holdoff work as intended with empty queues
  356. If URBs reading from non-periodic split endpoints were dequeued and
  357. the last transfer from the endpoint was a NAK handshake, the resulting
  358. qh->nak_frame value was stale which would result in unnecessarily long
  359. polling intervals for the first subsequent transfer with a fresh URB.
  360. Fixup qh->nak_frame in dwc_otg_hcd_urb_dequeue and also guard against
  361. a case where a single URB is submitted to the endpoint, a NAK was
  362. received on the transfer immediately prior to receiving data and the
  363. device subsequently resubmits another URB past the qh->nak_frame interval.
  364. Fixes https://github.com/raspberrypi/linux/issues/1709
  365. dwc_otg: fix split transaction data toggle handling around dequeues
  366. See https://github.com/raspberrypi/linux/issues/1709
  367. Fix several issues regarding endpoint state when URBs are dequeued
  368. - If the HCD is disconnected, flush FIQ-enabled channels properly
  369. - Save the data toggle state for bulk endpoints if the last transfer
  370. from an endpoint where URBs were dequeued returned a data packet
  371. - Reset hc->start_pkt_count properly in assign_and_init_hc()
  372. dwc_otg: fix several potential crash sources
  373. On root port disconnect events, the host driver state is cleared and
  374. in-progress host channels are forcibly stopped. This doesn't play
  375. well with the FIQ running in the background, so:
  376. - Guard the disconnect callback with both the host spinlock and FIQ
  377. spinlock
  378. - Move qtd dereference in dwc_otg_handle_hc_fsm() after the early-out
  379. so we don't dereference a qtd that has gone away
  380. - Turn catch-all BUG()s in dwc_otg_handle_hc_fsm() into warnings.
  381. dwc_otg: delete hcd->channel_lock
  382. The lock serves no purpose as it is only held while the HCD spinlock
  383. is already being held.
  384. dwc_otg: remove unnecessary dma-mode channel halts on disconnect interrupt
  385. Host channels are already halted in kill_urbs_in_qh_list() with the
  386. subsequent interrupt processing behaving as if the URB was dequeued
  387. via HCD callback.
  388. There's no need to clobber the host channel registers a second time
  389. as this exposes races between the driver and host channel resulting
  390. in hcd->free_hc_list becoming corrupted.
  391. dwcotg: Allow to build without FIQ on ARM64
  392. Signed-off-by: popcornmix <[email protected]>
  393. dwc_otg: make periodic scheduling behave properly for FS buses
  394. If the root port is in full-speed mode, transfer times at 12mbit/s
  395. would be calculated but matched against high-speed quotas.
  396. Reinitialise hcd->frame_usecs[i] on each port enable event so that
  397. full-speed bandwidth can be tracked sensibly.
  398. Also, don't bother using the FIQ for transfers when in full-speed
  399. mode - at the slower bus speed, interrupt frequency is reduced by
  400. an order of magnitude.
  401. Related issue: https://github.com/raspberrypi/linux/issues/2020
  402. dwc_otg: fiq_fsm: Make isochronous compatibility checks work properly
  403. Get rid of the spammy printk and local pointer mangling.
  404. Also, there is a nominal benefit for using fiq_fsm for isochronous
  405. transfers in FS mode (~1.1k IRQs per second vs 2.1k IRQs per second)
  406. so remove the root port speed check.
  407. dwc_otg: add module parameter int_ep_interval_min
  408. Add a module parameter (defaulting to ignored) that clamps the polling rate
  409. of high-speed Interrupt endpoints to a minimum microframe interval.
  410. The parameter is modifiable at runtime as it is used when activating new
  411. endpoints (such as on device connect).
  412. dwc_otg: fiq_fsm: Add non-periodic TT exclusivity constraints
  413. Certain hub types do not discriminate between pipe direction (IN or OUT)
  414. when considering non-periodic transfers. Therefore these hubs get confused
  415. if multiple transfers are issued in different directions with the same
  416. device address and endpoint number.
  417. Constrain queuing non-periodic split transactions so they are performed
  418. serially in such cases.
  419. Related: https://github.com/raspberrypi/linux/issues/2024
  420. dwc_otg: Fixup change to DRIVER_ATTR interface
  421. dwc_otg: Fix compilation warnings
  422. Signed-off-by: Phil Elwell <[email protected]>
  423. USB_DWCOTG: Disable building dwc_otg as a module (#2265)
  424. When dwc_otg is built as a module, build will fail with the following
  425. error:
  426. ERROR: "DWC_TASK_HI_SCHEDULE" [drivers/usb/host/dwc_otg/dwc_otg.ko] undefined!
  427. scripts/Makefile.modpost:91: recipe for target '__modpost' failed
  428. make[1]: *** [__modpost] Error 1
  429. Makefile:1199: recipe for target 'modules' failed
  430. make: *** [modules] Error 2
  431. Even if the error is solved by including the missing
  432. DWC_TASK_HI_SCHEDULE function, the kernel will panic when loading
  433. dwc_otg.
  434. As a workaround, simply prevent user from building dwc_otg as a module
  435. as the current kernel does not support it.
  436. See: https://github.com/raspberrypi/linux/issues/2258
  437. Signed-off-by: Malik Olivier Boussejra <[email protected]>
  438. ---
  439. arch/arm/include/asm/irqflags.h | 16 +-
  440. arch/arm/kernel/fiqasm.S | 4 +
  441. drivers/usb/Makefile | 1 +
  442. drivers/usb/core/generic.c | 1 +
  443. drivers/usb/core/hub.c | 2 +-
  444. drivers/usb/core/message.c | 79 +
  445. drivers/usb/core/otg_whitelist.h | 114 +-
  446. drivers/usb/gadget/file_storage.c | 3676 +++++++++
  447. drivers/usb/host/Kconfig | 10 +
  448. drivers/usb/host/Makefile | 2 +
  449. drivers/usb/host/dwc_common_port/Makefile | 58 +
  450. .../usb/host/dwc_common_port/Makefile.fbsd | 17 +
  451. .../usb/host/dwc_common_port/Makefile.linux | 49 +
  452. drivers/usb/host/dwc_common_port/changes.txt | 174 +
  453. .../usb/host/dwc_common_port/doc/doxygen.cfg | 270 +
  454. drivers/usb/host/dwc_common_port/dwc_cc.c | 532 ++
  455. drivers/usb/host/dwc_common_port/dwc_cc.h | 224 +
  456. .../host/dwc_common_port/dwc_common_fbsd.c | 1308 +++
  457. .../host/dwc_common_port/dwc_common_linux.c | 1418 ++++
  458. .../host/dwc_common_port/dwc_common_nbsd.c | 1275 +++
  459. drivers/usb/host/dwc_common_port/dwc_crypto.c | 308 +
  460. drivers/usb/host/dwc_common_port/dwc_crypto.h | 111 +
  461. drivers/usb/host/dwc_common_port/dwc_dh.c | 291 +
  462. drivers/usb/host/dwc_common_port/dwc_dh.h | 106 +
  463. drivers/usb/host/dwc_common_port/dwc_list.h | 594 ++
  464. drivers/usb/host/dwc_common_port/dwc_mem.c | 245 +
  465. drivers/usb/host/dwc_common_port/dwc_modpow.c | 636 ++
  466. drivers/usb/host/dwc_common_port/dwc_modpow.h | 34 +
  467. .../usb/host/dwc_common_port/dwc_notifier.c | 319 +
  468. .../usb/host/dwc_common_port/dwc_notifier.h | 122 +
  469. drivers/usb/host/dwc_common_port/dwc_os.h | 1276 +++
  470. drivers/usb/host/dwc_common_port/usb.h | 946 +++
  471. drivers/usb/host/dwc_otg/Makefile | 82 +
  472. drivers/usb/host/dwc_otg/doc/doxygen.cfg | 224 +
  473. drivers/usb/host/dwc_otg/dummy_audio.c | 1574 ++++
  474. drivers/usb/host/dwc_otg/dwc_cfi_common.h | 142 +
  475. drivers/usb/host/dwc_otg/dwc_otg_adp.c | 854 ++
  476. drivers/usb/host/dwc_otg/dwc_otg_adp.h | 80 +
  477. drivers/usb/host/dwc_otg/dwc_otg_attr.c | 1212 +++
  478. drivers/usb/host/dwc_otg/dwc_otg_attr.h | 89 +
  479. drivers/usb/host/dwc_otg/dwc_otg_cfi.c | 1876 +++++
  480. drivers/usb/host/dwc_otg/dwc_otg_cfi.h | 320 +
  481. drivers/usb/host/dwc_otg/dwc_otg_cil.c | 7141 +++++++++++++++++
  482. drivers/usb/host/dwc_otg/dwc_otg_cil.h | 1464 ++++
  483. drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c | 1596 ++++
  484. drivers/usb/host/dwc_otg/dwc_otg_core_if.h | 705 ++
  485. drivers/usb/host/dwc_otg/dwc_otg_dbg.h | 117 +
  486. drivers/usb/host/dwc_otg/dwc_otg_driver.c | 1760 ++++
  487. drivers/usb/host/dwc_otg/dwc_otg_driver.h | 86 +
  488. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 1389 ++++
  489. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 372 +
  490. drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S | 80 +
  491. drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 4283 ++++++++++
  492. drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 870 ++
  493. drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c | 1134 +++
  494. drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h | 417 +
  495. drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 2752 +++++++
  496. drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 1007 +++
  497. drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c | 971 +++
  498. drivers/usb/host/dwc_otg/dwc_otg_os_dep.h | 188 +
  499. drivers/usb/host/dwc_otg/dwc_otg_pcd.c | 2725 +++++++
  500. drivers/usb/host/dwc_otg/dwc_otg_pcd.h | 273 +
  501. drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h | 361 +
  502. drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c | 5148 ++++++++++++
  503. drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c | 1280 +++
  504. drivers/usb/host/dwc_otg/dwc_otg_regs.h | 2550 ++++++
  505. drivers/usb/host/dwc_otg/test/Makefile | 16 +
  506. drivers/usb/host/dwc_otg/test/dwc_otg_test.pm | 337 +
  507. .../usb/host/dwc_otg/test/test_mod_param.pl | 133 +
  508. drivers/usb/host/dwc_otg/test/test_sysfs.pl | 193 +
  509. 70 files changed, 60003 insertions(+), 16 deletions(-)
  510. create mode 100644 drivers/usb/gadget/file_storage.c
  511. create mode 100644 drivers/usb/host/dwc_common_port/Makefile
  512. create mode 100644 drivers/usb/host/dwc_common_port/Makefile.fbsd
  513. create mode 100644 drivers/usb/host/dwc_common_port/Makefile.linux
  514. create mode 100644 drivers/usb/host/dwc_common_port/changes.txt
  515. create mode 100644 drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  516. create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.c
  517. create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.h
  518. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  519. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_linux.c
  520. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  521. create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.c
  522. create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.h
  523. create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.c
  524. create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.h
  525. create mode 100644 drivers/usb/host/dwc_common_port/dwc_list.h
  526. create mode 100644 drivers/usb/host/dwc_common_port/dwc_mem.c
  527. create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.c
  528. create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.h
  529. create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.c
  530. create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.h
  531. create mode 100644 drivers/usb/host/dwc_common_port/dwc_os.h
  532. create mode 100644 drivers/usb/host/dwc_common_port/usb.h
  533. create mode 100644 drivers/usb/host/dwc_otg/Makefile
  534. create mode 100644 drivers/usb/host/dwc_otg/doc/doxygen.cfg
  535. create mode 100644 drivers/usb/host/dwc_otg/dummy_audio.c
  536. create mode 100644 drivers/usb/host/dwc_otg/dwc_cfi_common.h
  537. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.c
  538. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.h
  539. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.c
  540. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.h
  541. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  542. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  543. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.c
  544. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.h
  545. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  546. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  547. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  548. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.c
  549. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.h
  550. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  551. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  552. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  553. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  554. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  555. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  556. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  557. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  558. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  559. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  560. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  561. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  562. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  563. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  564. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  565. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  566. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_regs.h
  567. create mode 100644 drivers/usb/host/dwc_otg/test/Makefile
  568. create mode 100644 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  569. create mode 100644 drivers/usb/host/dwc_otg/test/test_mod_param.pl
  570. create mode 100644 drivers/usb/host/dwc_otg/test/test_sysfs.pl
  571. --- a/arch/arm/include/asm/irqflags.h
  572. +++ b/arch/arm/include/asm/irqflags.h
  573. @@ -163,13 +163,23 @@ static inline unsigned long arch_local_s
  574. }
  575. /*
  576. - * restore saved IRQ & FIQ state
  577. + * restore saved IRQ state
  578. */
  579. #define arch_local_irq_restore arch_local_irq_restore
  580. static inline void arch_local_irq_restore(unsigned long flags)
  581. {
  582. - asm volatile(
  583. - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
  584. + unsigned long temp = 0;
  585. + flags &= ~(1 << 6);
  586. + asm volatile (
  587. + " mrs %0, cpsr"
  588. + : "=r" (temp)
  589. + :
  590. + : "memory", "cc");
  591. + /* Preserve FIQ bit */
  592. + temp &= (1 << 6);
  593. + flags = flags | temp;
  594. + asm volatile (
  595. + " msr cpsr_c, %0 @ local_irq_restore"
  596. :
  597. : "r" (flags)
  598. : "memory", "cc");
  599. --- a/arch/arm/kernel/fiqasm.S
  600. +++ b/arch/arm/kernel/fiqasm.S
  601. @@ -47,3 +47,7 @@ ENTRY(__get_fiq_regs)
  602. mov r0, r0 @ avoid hazard prior to ARMv4
  603. ret lr
  604. ENDPROC(__get_fiq_regs)
  605. +
  606. +ENTRY(__FIQ_Branch)
  607. + mov pc, r8
  608. +ENDPROC(__FIQ_Branch)
  609. --- a/drivers/usb/Makefile
  610. +++ b/drivers/usb/Makefile
  611. @@ -8,6 +8,7 @@
  612. obj-$(CONFIG_USB) += core/
  613. obj-$(CONFIG_USB_SUPPORT) += phy/
  614. +obj-$(CONFIG_USB_DWCOTG) += host/
  615. obj-$(CONFIG_USB_DWC3) += dwc3/
  616. obj-$(CONFIG_USB_DWC2) += dwc2/
  617. obj-$(CONFIG_USB_ISP1760) += isp1760/
  618. --- a/drivers/usb/core/generic.c
  619. +++ b/drivers/usb/core/generic.c
  620. @@ -154,6 +154,7 @@ int usb_choose_configuration(struct usb_
  621. dev_warn(&udev->dev,
  622. "no configuration chosen from %d choice%s\n",
  623. num_configs, plural(num_configs));
  624. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  625. }
  626. return i;
  627. }
  628. --- a/drivers/usb/core/hub.c
  629. +++ b/drivers/usb/core/hub.c
  630. @@ -5094,7 +5094,7 @@ static void port_event(struct usb_hub *h
  631. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  632. u16 status = 0, unused;
  633. - dev_dbg(&port_dev->dev, "over-current change\n");
  634. + dev_notice(&port_dev->dev, "over-current change\n");
  635. usb_clear_port_feature(hdev, port1,
  636. USB_PORT_FEAT_C_OVER_CURRENT);
  637. msleep(100); /* Cool down */
  638. --- a/drivers/usb/core/message.c
  639. +++ b/drivers/usb/core/message.c
  640. @@ -1924,6 +1924,85 @@ free_interfaces:
  641. if (cp->string == NULL &&
  642. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  643. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  644. +/* Uncomment this define to enable the HS Electrical Test support */
  645. +#define DWC_HS_ELECT_TST 1
  646. +#ifdef DWC_HS_ELECT_TST
  647. + /* Here we implement the HS Electrical Test support. The
  648. + * tester uses a vendor ID of 0x1A0A to indicate we should
  649. + * run a special test sequence. The product ID tells us
  650. + * which sequence to run. We invoke the test sequence by
  651. + * sending a non-standard SetFeature command to our root
  652. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  653. + * recognize the command and perform the desired test
  654. + * sequence.
  655. + */
  656. + if (dev->descriptor.idVendor == 0x1A0A) {
  657. + /* HSOTG Electrical Test */
  658. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  659. +
  660. + if (dev->bus && dev->bus->root_hub) {
  661. + struct usb_device *hdev = dev->bus->root_hub;
  662. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  663. +
  664. + switch (dev->descriptor.idProduct) {
  665. + case 0x0101: /* TEST_SE0_NAK */
  666. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  667. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  668. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  669. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  670. + break;
  671. +
  672. + case 0x0102: /* TEST_J */
  673. + dev_warn(&dev->dev, "TEST_J\n");
  674. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  675. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  676. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  677. + break;
  678. +
  679. + case 0x0103: /* TEST_K */
  680. + dev_warn(&dev->dev, "TEST_K\n");
  681. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  682. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  683. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  684. + break;
  685. +
  686. + case 0x0104: /* TEST_PACKET */
  687. + dev_warn(&dev->dev, "TEST_PACKET\n");
  688. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  689. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  690. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  691. + break;
  692. +
  693. + case 0x0105: /* TEST_FORCE_ENABLE */
  694. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  695. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  696. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  697. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  698. + break;
  699. +
  700. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  701. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  702. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  703. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  704. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  705. + break;
  706. +
  707. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  708. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  709. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  710. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  711. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  712. + break;
  713. +
  714. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  715. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  716. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  717. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  718. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  719. + }
  720. + }
  721. + }
  722. +#endif /* DWC_HS_ELECT_TST */
  723. /* Now that the interfaces are installed, re-enable LPM. */
  724. usb_unlocked_enable_lpm(dev);
  725. --- a/drivers/usb/core/otg_whitelist.h
  726. +++ b/drivers/usb/core/otg_whitelist.h
  727. @@ -19,33 +19,82 @@
  728. static struct usb_device_id whitelist_table[] = {
  729. /* hubs are optional in OTG, but very handy ... */
  730. +#define CERT_WITHOUT_HUBS
  731. +#if defined(CERT_WITHOUT_HUBS)
  732. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  733. +#else
  734. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  735. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  736. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  737. +#endif
  738. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  739. /* FIXME actually, printers are NOT supposed to use device classes;
  740. * they're supposed to use interface classes...
  741. */
  742. -{ USB_DEVICE_INFO(7, 1, 1) },
  743. -{ USB_DEVICE_INFO(7, 1, 2) },
  744. -{ USB_DEVICE_INFO(7, 1, 3) },
  745. +//{ USB_DEVICE_INFO(7, 1, 1) },
  746. +//{ USB_DEVICE_INFO(7, 1, 2) },
  747. +//{ USB_DEVICE_INFO(7, 1, 3) },
  748. #endif
  749. #ifdef CONFIG_USB_NET_CDCETHER
  750. /* Linux-USB CDC Ethernet gadget */
  751. -{ USB_DEVICE(0x0525, 0xa4a1), },
  752. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  753. /* Linux-USB CDC Ethernet + RNDIS gadget */
  754. -{ USB_DEVICE(0x0525, 0xa4a2), },
  755. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  756. #endif
  757. #if IS_ENABLED(CONFIG_USB_TEST)
  758. /* gadget zero, for testing */
  759. -{ USB_DEVICE(0x0525, 0xa4a0), },
  760. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  761. #endif
  762. +/* OPT Tester */
  763. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  764. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  765. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  766. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  767. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  768. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  769. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  770. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  771. +
  772. +/* Sony cameras */
  773. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  774. +
  775. +/* Memory Devices */
  776. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  777. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  778. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  779. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  780. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  781. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  782. +
  783. +/* HP Printers */
  784. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  785. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  786. +
  787. +/* Speakers */
  788. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  789. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  790. +
  791. { } /* Terminating entry */
  792. };
  793. +static inline void report_errors(struct usb_device *dev)
  794. +{
  795. + /* OTG MESSAGE: report errors here, customize to match your product */
  796. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  797. + le16_to_cpu(dev->descriptor.idVendor),
  798. + le16_to_cpu(dev->descriptor.idProduct));
  799. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  800. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  801. + } else {
  802. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  803. + }
  804. +}
  805. +
  806. +
  807. static int is_targeted(struct usb_device *dev)
  808. {
  809. struct usb_device_id *id = whitelist_table;
  810. @@ -95,16 +144,57 @@ static int is_targeted(struct usb_device
  811. continue;
  812. return 1;
  813. - }
  814. + /* NOTE: can't use usb_match_id() since interface caches
  815. + * aren't set up yet. this is cut/paste from that code.
  816. + */
  817. + for (id = whitelist_table; id->match_flags; id++) {
  818. +#ifdef DEBUG
  819. + dev_dbg(&dev->dev,
  820. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  821. + id->idVendor,
  822. + id->idProduct,
  823. + id->bDeviceClass,
  824. + id->bDeviceSubClass,
  825. + id->bDeviceProtocol);
  826. +#endif
  827. - /* add other match criteria here ... */
  828. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  829. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  830. + continue;
  831. +
  832. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  833. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  834. + continue;
  835. +
  836. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  837. + greater than any unsigned number. */
  838. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  839. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  840. + continue;
  841. +
  842. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  843. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  844. + continue;
  845. +
  846. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  847. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  848. + continue;
  849. +
  850. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  851. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  852. + continue;
  853. +
  854. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  855. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  856. + continue;
  857. + return 1;
  858. + }
  859. + }
  860. - /* OTG MESSAGE: report errors here, customize to match your product */
  861. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  862. - le16_to_cpu(dev->descriptor.idVendor),
  863. - le16_to_cpu(dev->descriptor.idProduct));
  864. + /* add other match criteria here ... */
  865. + report_errors(dev);
  866. return 0;
  867. }
  868. --- /dev/null
  869. +++ b/drivers/usb/gadget/file_storage.c
  870. @@ -0,0 +1,3676 @@
  871. +/*
  872. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  873. + *
  874. + * Copyright (C) 2003-2008 Alan Stern
  875. + * All rights reserved.
  876. + *
  877. + * Redistribution and use in source and binary forms, with or without
  878. + * modification, are permitted provided that the following conditions
  879. + * are met:
  880. + * 1. Redistributions of source code must retain the above copyright
  881. + * notice, this list of conditions, and the following disclaimer,
  882. + * without modification.
  883. + * 2. Redistributions in binary form must reproduce the above copyright
  884. + * notice, this list of conditions and the following disclaimer in the
  885. + * documentation and/or other materials provided with the distribution.
  886. + * 3. The names of the above-listed copyright holders may not be used
  887. + * to endorse or promote products derived from this software without
  888. + * specific prior written permission.
  889. + *
  890. + * ALTERNATIVELY, this software may be distributed under the terms of the
  891. + * GNU General Public License ("GPL") as published by the Free Software
  892. + * Foundation, either version 2 of that License or (at your option) any
  893. + * later version.
  894. + *
  895. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  896. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  897. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  898. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  899. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  900. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  901. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  902. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  903. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  904. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  905. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  906. + */
  907. +
  908. +
  909. +/*
  910. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  911. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  912. + * to providing an example of a genuinely useful gadget driver for a USB
  913. + * device, it also illustrates a technique of double-buffering for increased
  914. + * throughput. Last but not least, it gives an easy way to probe the
  915. + * behavior of the Mass Storage drivers in a USB host.
  916. + *
  917. + * Backing storage is provided by a regular file or a block device, specified
  918. + * by the "file" module parameter. Access can be limited to read-only by
  919. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  920. + * access is always read-only.) The gadget will indicate that it has
  921. + * removable media if the optional "removable" module parameter is set.
  922. + *
  923. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  924. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  925. + * by the optional "transport" module parameter. It also supports the
  926. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  927. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  928. + * the optional "protocol" module parameter. In addition, the default
  929. + * Vendor ID, Product ID, release number and serial number can be overridden.
  930. + *
  931. + * There is support for multiple logical units (LUNs), each of which has
  932. + * its own backing file. The number of LUNs can be set using the optional
  933. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  934. + * files are specified using comma-separated lists for "file" and "ro".
  935. + * The default number of LUNs is taken from the number of "file" elements;
  936. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  937. + * file must be specified for each LUN. If it is set, then an unspecified
  938. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  939. + * each LUN would be settable independently as a disk drive or a CD-ROM
  940. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  941. + * emulation includes a single data track and no audio tracks; hence there
  942. + * need be only one backing file per LUN.
  943. + *
  944. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  945. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  946. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  947. + * Support is included for both full-speed and high-speed operation.
  948. + *
  949. + * Note that the driver is slightly non-portable in that it assumes a
  950. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  951. + * interrupt-in endpoints. With most device controllers this isn't an
  952. + * issue, but there may be some with hardware restrictions that prevent
  953. + * a buffer from being used by more than one endpoint.
  954. + *
  955. + * Module options:
  956. + *
  957. + * file=filename[,filename...]
  958. + * Required if "removable" is not set, names of
  959. + * the files or block devices used for
  960. + * backing storage
  961. + * serial=HHHH... Required serial number (string of hex chars)
  962. + * ro=b[,b...] Default false, booleans for read-only access
  963. + * removable Default false, boolean for removable media
  964. + * luns=N Default N = number of filenames, number of
  965. + * LUNs to support
  966. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  967. + * in SCSI WRITE(10,12) commands
  968. + * stall Default determined according to the type of
  969. + * USB device controller (usually true),
  970. + * boolean to permit the driver to halt
  971. + * bulk endpoints
  972. + * cdrom Default false, boolean for whether to emulate
  973. + * a CD-ROM drive
  974. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  975. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  976. + * ATAPI, QIC, UFI, 8070, or SCSI;
  977. + * also 1 - 6)
  978. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  979. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  980. + * release=0xRRRR Override the USB release number (bcdDevice)
  981. + * buflen=N Default N=16384, buffer size used (will be
  982. + * rounded down to a multiple of
  983. + * PAGE_CACHE_SIZE)
  984. + *
  985. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  986. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  987. + * default values are used for everything else.
  988. + *
  989. + * The pathnames of the backing files and the ro settings are available in
  990. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  991. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  992. + * these files will simulate ejecting/loading the medium (writing an empty
  993. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  994. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  995. + * is being used.
  996. + *
  997. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  998. + * The driver's SCSI command interface was based on the "Information
  999. + * technology - Small Computer System Interface - 2" document from
  1000. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  1001. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  1002. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  1003. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  1004. + * document, Revision 1.0, December 14, 1998, available at
  1005. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  1006. + */
  1007. +
  1008. +
  1009. +/*
  1010. + * Driver Design
  1011. + *
  1012. + * The FSG driver is fairly straightforward. There is a main kernel
  1013. + * thread that handles most of the work. Interrupt routines field
  1014. + * callbacks from the controller driver: bulk- and interrupt-request
  1015. + * completion notifications, endpoint-0 events, and disconnect events.
  1016. + * Completion events are passed to the main thread by wakeup calls. Many
  1017. + * ep0 requests are handled at interrupt time, but SetInterface,
  1018. + * SetConfiguration, and device reset requests are forwarded to the
  1019. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  1020. + * should interrupt any ongoing file I/O operations).
  1021. + *
  1022. + * The thread's main routine implements the standard command/data/status
  1023. + * parts of a SCSI interaction. It and its subroutines are full of tests
  1024. + * for pending signals/exceptions -- all this polling is necessary since
  1025. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  1026. + * indication that the driver really wants to be running in userspace.)
  1027. + * An important point is that so long as the thread is alive it keeps an
  1028. + * open reference to the backing file. This will prevent unmounting
  1029. + * the backing file's underlying filesystem and could cause problems
  1030. + * during system shutdown, for example. To prevent such problems, the
  1031. + * thread catches INT, TERM, and KILL signals and converts them into
  1032. + * an EXIT exception.
  1033. + *
  1034. + * In normal operation the main thread is started during the gadget's
  1035. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  1036. + * exit when it receives a signal, and there's no point leaving the
  1037. + * gadget running when the thread is dead. So just before the thread
  1038. + * exits, it deregisters the gadget driver. This makes things a little
  1039. + * tricky: The driver is deregistered at two places, and the exiting
  1040. + * thread can indirectly call fsg_unbind() which in turn can tell the
  1041. + * thread to exit. The first problem is resolved through the use of the
  1042. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  1043. + * The second problem is resolved by having fsg_unbind() check
  1044. + * fsg->state; it won't try to stop the thread if the state is already
  1045. + * FSG_STATE_TERMINATED.
  1046. + *
  1047. + * To provide maximum throughput, the driver uses a circular pipeline of
  1048. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  1049. + * arbitrarily long; in practice the benefits don't justify having more
  1050. + * than 2 stages (i.e., double buffering). But it helps to think of the
  1051. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  1052. + * a bulk-out request pointer (since the buffer can be used for both
  1053. + * output and input -- directions always are given from the host's
  1054. + * point of view) as well as a pointer to the buffer and various state
  1055. + * variables.
  1056. + *
  1057. + * Use of the pipeline follows a simple protocol. There is a variable
  1058. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  1059. + * At any time that buffer head may still be in use from an earlier
  1060. + * request, so each buffer head has a state variable indicating whether
  1061. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  1062. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  1063. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  1064. + * head FULL when the I/O is complete. Then the buffer will be emptied
  1065. + * (again possibly by USB I/O, during which it is marked BUSY) and
  1066. + * finally marked EMPTY again (possibly by a completion routine).
  1067. + *
  1068. + * A module parameter tells the driver to avoid stalling the bulk
  1069. + * endpoints wherever the transport specification allows. This is
  1070. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  1071. + * halt on a bulk endpoint. However, under certain circumstances the
  1072. + * Bulk-only specification requires a stall. In such cases the driver
  1073. + * will halt the endpoint and set a flag indicating that it should clear
  1074. + * the halt in software during the next device reset. Hopefully this
  1075. + * will permit everything to work correctly. Furthermore, although the
  1076. + * specification allows the bulk-out endpoint to halt when the host sends
  1077. + * too much data, implementing this would cause an unavoidable race.
  1078. + * The driver will always use the "no-stall" approach for OUT transfers.
  1079. + *
  1080. + * One subtle point concerns sending status-stage responses for ep0
  1081. + * requests. Some of these requests, such as device reset, can involve
  1082. + * interrupting an ongoing file I/O operation, which might take an
  1083. + * arbitrarily long time. During that delay the host might give up on
  1084. + * the original ep0 request and issue a new one. When that happens the
  1085. + * driver should not notify the host about completion of the original
  1086. + * request, as the host will no longer be waiting for it. So the driver
  1087. + * assigns to each ep0 request a unique tag, and it keeps track of the
  1088. + * tag value of the request associated with a long-running exception
  1089. + * (device-reset, interface-change, or configuration-change). When the
  1090. + * exception handler is finished, the status-stage response is submitted
  1091. + * only if the current ep0 request tag is equal to the exception request
  1092. + * tag. Thus only the most recently received ep0 request will get a
  1093. + * status-stage response.
  1094. + *
  1095. + * Warning: This driver source file is too long. It ought to be split up
  1096. + * into a header file plus about 3 separate .c files, to handle the details
  1097. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  1098. + */
  1099. +
  1100. +
  1101. +/* #define VERBOSE_DEBUG */
  1102. +/* #define DUMP_MSGS */
  1103. +
  1104. +
  1105. +#include <linux/blkdev.h>
  1106. +#include <linux/completion.h>
  1107. +#include <linux/dcache.h>
  1108. +#include <linux/delay.h>
  1109. +#include <linux/device.h>
  1110. +#include <linux/fcntl.h>
  1111. +#include <linux/file.h>
  1112. +#include <linux/fs.h>
  1113. +#include <linux/kref.h>
  1114. +#include <linux/kthread.h>
  1115. +#include <linux/limits.h>
  1116. +#include <linux/module.h>
  1117. +#include <linux/rwsem.h>
  1118. +#include <linux/slab.h>
  1119. +#include <linux/spinlock.h>
  1120. +#include <linux/string.h>
  1121. +#include <linux/freezer.h>
  1122. +#include <linux/utsname.h>
  1123. +
  1124. +#include <linux/usb/ch9.h>
  1125. +#include <linux/usb/gadget.h>
  1126. +
  1127. +#include "gadget_chips.h"
  1128. +
  1129. +
  1130. +
  1131. +/*
  1132. + * Kbuild is not very cooperative with respect to linking separately
  1133. + * compiled library objects into one module. So for now we won't use
  1134. + * separate compilation ... ensuring init/exit sections work to shrink
  1135. + * the runtime footprint, and giving us at least some parts of what
  1136. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  1137. + */
  1138. +#include "usbstring.c"
  1139. +#include "config.c"
  1140. +#include "epautoconf.c"
  1141. +
  1142. +/*-------------------------------------------------------------------------*/
  1143. +
  1144. +#define DRIVER_DESC "File-backed Storage Gadget"
  1145. +#define DRIVER_NAME "g_file_storage"
  1146. +#define DRIVER_VERSION "1 September 2010"
  1147. +
  1148. +static char fsg_string_manufacturer[64];
  1149. +static const char fsg_string_product[] = DRIVER_DESC;
  1150. +static const char fsg_string_config[] = "Self-powered";
  1151. +static const char fsg_string_interface[] = "Mass Storage";
  1152. +
  1153. +
  1154. +#include "storage_common.c"
  1155. +
  1156. +
  1157. +MODULE_DESCRIPTION(DRIVER_DESC);
  1158. +MODULE_AUTHOR("Alan Stern");
  1159. +MODULE_LICENSE("Dual BSD/GPL");
  1160. +
  1161. +/*
  1162. + * This driver assumes self-powered hardware and has no way for users to
  1163. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  1164. + * and endpoint addresses.
  1165. + */
  1166. +
  1167. +
  1168. +/*-------------------------------------------------------------------------*/
  1169. +
  1170. +
  1171. +/* Encapsulate the module parameter settings */
  1172. +
  1173. +static struct {
  1174. + char *file[FSG_MAX_LUNS];
  1175. + char *serial;
  1176. + bool ro[FSG_MAX_LUNS];
  1177. + bool nofua[FSG_MAX_LUNS];
  1178. + unsigned int num_filenames;
  1179. + unsigned int num_ros;
  1180. + unsigned int num_nofuas;
  1181. + unsigned int nluns;
  1182. +
  1183. + bool removable;
  1184. + bool can_stall;
  1185. + bool cdrom;
  1186. +
  1187. + char *transport_parm;
  1188. + char *protocol_parm;
  1189. + unsigned short vendor;
  1190. + unsigned short product;
  1191. + unsigned short release;
  1192. + unsigned int buflen;
  1193. +
  1194. + int transport_type;
  1195. + char *transport_name;
  1196. + int protocol_type;
  1197. + char *protocol_name;
  1198. +
  1199. +} mod_data = { // Default values
  1200. + .transport_parm = "BBB",
  1201. + .protocol_parm = "SCSI",
  1202. + .removable = 0,
  1203. + .can_stall = 1,
  1204. + .cdrom = 0,
  1205. + .vendor = FSG_VENDOR_ID,
  1206. + .product = FSG_PRODUCT_ID,
  1207. + .release = 0xffff, // Use controller chip type
  1208. + .buflen = 16384,
  1209. + };
  1210. +
  1211. +
  1212. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  1213. + S_IRUGO);
  1214. +MODULE_PARM_DESC(file, "names of backing files or devices");
  1215. +
  1216. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  1217. +MODULE_PARM_DESC(serial, "USB serial number");
  1218. +
  1219. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  1220. +MODULE_PARM_DESC(ro, "true to force read-only");
  1221. +
  1222. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  1223. + S_IRUGO);
  1224. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  1225. +
  1226. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  1227. +MODULE_PARM_DESC(luns, "number of LUNs");
  1228. +
  1229. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  1230. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  1231. +
  1232. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  1233. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  1234. +
  1235. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  1236. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  1237. +
  1238. +/* In the non-TEST version, only the module parameters listed above
  1239. + * are available. */
  1240. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1241. +
  1242. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  1243. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  1244. +
  1245. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  1246. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  1247. + "8070, or SCSI)");
  1248. +
  1249. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  1250. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  1251. +
  1252. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  1253. +MODULE_PARM_DESC(product, "USB Product ID");
  1254. +
  1255. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  1256. +MODULE_PARM_DESC(release, "USB release number");
  1257. +
  1258. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  1259. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  1260. +
  1261. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1262. +
  1263. +
  1264. +/*
  1265. + * These definitions will permit the compiler to avoid generating code for
  1266. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  1267. + * can recognize when a test of a constant expression yields a dead code
  1268. + * path.
  1269. + */
  1270. +
  1271. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1272. +
  1273. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  1274. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  1275. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  1276. +
  1277. +#else
  1278. +
  1279. +#define transport_is_bbb() 1
  1280. +#define transport_is_cbi() 0
  1281. +#define protocol_is_scsi() 1
  1282. +
  1283. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1284. +
  1285. +
  1286. +/*-------------------------------------------------------------------------*/
  1287. +
  1288. +
  1289. +struct fsg_dev {
  1290. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  1291. + spinlock_t lock;
  1292. + struct usb_gadget *gadget;
  1293. +
  1294. + /* filesem protects: backing files in use */
  1295. + struct rw_semaphore filesem;
  1296. +
  1297. + /* reference counting: wait until all LUNs are released */
  1298. + struct kref ref;
  1299. +
  1300. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  1301. + struct usb_request *ep0req; // For control responses
  1302. + unsigned int ep0_req_tag;
  1303. + const char *ep0req_name;
  1304. +
  1305. + struct usb_request *intreq; // For interrupt responses
  1306. + int intreq_busy;
  1307. + struct fsg_buffhd *intr_buffhd;
  1308. +
  1309. + unsigned int bulk_out_maxpacket;
  1310. + enum fsg_state state; // For exception handling
  1311. + unsigned int exception_req_tag;
  1312. +
  1313. + u8 config, new_config;
  1314. +
  1315. + unsigned int running : 1;
  1316. + unsigned int bulk_in_enabled : 1;
  1317. + unsigned int bulk_out_enabled : 1;
  1318. + unsigned int intr_in_enabled : 1;
  1319. + unsigned int phase_error : 1;
  1320. + unsigned int short_packet_received : 1;
  1321. + unsigned int bad_lun_okay : 1;
  1322. +
  1323. + unsigned long atomic_bitflags;
  1324. +#define REGISTERED 0
  1325. +#define IGNORE_BULK_OUT 1
  1326. +#define SUSPENDED 2
  1327. +
  1328. + struct usb_ep *bulk_in;
  1329. + struct usb_ep *bulk_out;
  1330. + struct usb_ep *intr_in;
  1331. +
  1332. + struct fsg_buffhd *next_buffhd_to_fill;
  1333. + struct fsg_buffhd *next_buffhd_to_drain;
  1334. +
  1335. + int thread_wakeup_needed;
  1336. + struct completion thread_notifier;
  1337. + struct task_struct *thread_task;
  1338. +
  1339. + int cmnd_size;
  1340. + u8 cmnd[MAX_COMMAND_SIZE];
  1341. + enum data_direction data_dir;
  1342. + u32 data_size;
  1343. + u32 data_size_from_cmnd;
  1344. + u32 tag;
  1345. + unsigned int lun;
  1346. + u32 residue;
  1347. + u32 usb_amount_left;
  1348. +
  1349. + /* The CB protocol offers no way for a host to know when a command
  1350. + * has completed. As a result the next command may arrive early,
  1351. + * and we will still have to handle it. For that reason we need
  1352. + * a buffer to store new commands when using CB (or CBI, which
  1353. + * does not oblige a host to wait for command completion either). */
  1354. + int cbbuf_cmnd_size;
  1355. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  1356. +
  1357. + unsigned int nluns;
  1358. + struct fsg_lun *luns;
  1359. + struct fsg_lun *curlun;
  1360. + /* Must be the last entry */
  1361. + struct fsg_buffhd buffhds[];
  1362. +};
  1363. +
  1364. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  1365. +
  1366. +static int exception_in_progress(struct fsg_dev *fsg)
  1367. +{
  1368. + return (fsg->state > FSG_STATE_IDLE);
  1369. +}
  1370. +
  1371. +/* Make bulk-out requests be divisible by the maxpacket size */
  1372. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  1373. + struct fsg_buffhd *bh, unsigned int length)
  1374. +{
  1375. + unsigned int rem;
  1376. +
  1377. + bh->bulk_out_intended_length = length;
  1378. + rem = length % fsg->bulk_out_maxpacket;
  1379. + if (rem > 0)
  1380. + length += fsg->bulk_out_maxpacket - rem;
  1381. + bh->outreq->length = length;
  1382. +}
  1383. +
  1384. +static struct fsg_dev *the_fsg;
  1385. +static struct usb_gadget_driver fsg_driver;
  1386. +
  1387. +
  1388. +/*-------------------------------------------------------------------------*/
  1389. +
  1390. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  1391. +{
  1392. + const char *name;
  1393. +
  1394. + if (ep == fsg->bulk_in)
  1395. + name = "bulk-in";
  1396. + else if (ep == fsg->bulk_out)
  1397. + name = "bulk-out";
  1398. + else
  1399. + name = ep->name;
  1400. + DBG(fsg, "%s set halt\n", name);
  1401. + return usb_ep_set_halt(ep);
  1402. +}
  1403. +
  1404. +
  1405. +/*-------------------------------------------------------------------------*/
  1406. +
  1407. +/*
  1408. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  1409. + * descriptors are built on demand. Also the (static) config and interface
  1410. + * descriptors are adjusted during fsg_bind().
  1411. + */
  1412. +
  1413. +/* There is only one configuration. */
  1414. +#define CONFIG_VALUE 1
  1415. +
  1416. +static struct usb_device_descriptor
  1417. +device_desc = {
  1418. + .bLength = sizeof device_desc,
  1419. + .bDescriptorType = USB_DT_DEVICE,
  1420. +
  1421. + .bcdUSB = cpu_to_le16(0x0200),
  1422. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1423. +
  1424. + /* The next three values can be overridden by module parameters */
  1425. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  1426. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  1427. + .bcdDevice = cpu_to_le16(0xffff),
  1428. +
  1429. + .iManufacturer = FSG_STRING_MANUFACTURER,
  1430. + .iProduct = FSG_STRING_PRODUCT,
  1431. + .iSerialNumber = FSG_STRING_SERIAL,
  1432. + .bNumConfigurations = 1,
  1433. +};
  1434. +
  1435. +static struct usb_config_descriptor
  1436. +config_desc = {
  1437. + .bLength = sizeof config_desc,
  1438. + .bDescriptorType = USB_DT_CONFIG,
  1439. +
  1440. + /* wTotalLength computed by usb_gadget_config_buf() */
  1441. + .bNumInterfaces = 1,
  1442. + .bConfigurationValue = CONFIG_VALUE,
  1443. + .iConfiguration = FSG_STRING_CONFIG,
  1444. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  1445. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  1446. +};
  1447. +
  1448. +
  1449. +static struct usb_qualifier_descriptor
  1450. +dev_qualifier = {
  1451. + .bLength = sizeof dev_qualifier,
  1452. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  1453. +
  1454. + .bcdUSB = cpu_to_le16(0x0200),
  1455. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1456. +
  1457. + .bNumConfigurations = 1,
  1458. +};
  1459. +
  1460. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  1461. +{
  1462. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  1463. + buf += USB_DT_BOS_SIZE;
  1464. +
  1465. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  1466. + buf += USB_DT_USB_EXT_CAP_SIZE;
  1467. +
  1468. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  1469. +
  1470. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  1471. + + USB_DT_USB_EXT_CAP_SIZE;
  1472. +}
  1473. +
  1474. +/*
  1475. + * Config descriptors must agree with the code that sets configurations
  1476. + * and with code managing interfaces and their altsettings. They must
  1477. + * also handle different speeds and other-speed requests.
  1478. + */
  1479. +static int populate_config_buf(struct usb_gadget *gadget,
  1480. + u8 *buf, u8 type, unsigned index)
  1481. +{
  1482. + enum usb_device_speed speed = gadget->speed;
  1483. + int len;
  1484. + const struct usb_descriptor_header **function;
  1485. +
  1486. + if (index > 0)
  1487. + return -EINVAL;
  1488. +
  1489. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  1490. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  1491. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  1492. + ? (const struct usb_descriptor_header **)fsg_hs_function
  1493. + : (const struct usb_descriptor_header **)fsg_fs_function;
  1494. +
  1495. + /* for now, don't advertise srp-only devices */
  1496. + if (!gadget_is_otg(gadget))
  1497. + function++;
  1498. +
  1499. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  1500. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  1501. + return len;
  1502. +}
  1503. +
  1504. +
  1505. +/*-------------------------------------------------------------------------*/
  1506. +
  1507. +/* These routines may be called in process context or in_irq */
  1508. +
  1509. +/* Caller must hold fsg->lock */
  1510. +static void wakeup_thread(struct fsg_dev *fsg)
  1511. +{
  1512. + /* Tell the main thread that something has happened */
  1513. + fsg->thread_wakeup_needed = 1;
  1514. + if (fsg->thread_task)
  1515. + wake_up_process(fsg->thread_task);
  1516. +}
  1517. +
  1518. +
  1519. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  1520. +{
  1521. + unsigned long flags;
  1522. +
  1523. + /* Do nothing if a higher-priority exception is already in progress.
  1524. + * If a lower-or-equal priority exception is in progress, preempt it
  1525. + * and notify the main thread by sending it a signal. */
  1526. + spin_lock_irqsave(&fsg->lock, flags);
  1527. + if (fsg->state <= new_state) {
  1528. + fsg->exception_req_tag = fsg->ep0_req_tag;
  1529. + fsg->state = new_state;
  1530. + if (fsg->thread_task)
  1531. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  1532. + fsg->thread_task);
  1533. + }
  1534. + spin_unlock_irqrestore(&fsg->lock, flags);
  1535. +}
  1536. +
  1537. +
  1538. +/*-------------------------------------------------------------------------*/
  1539. +
  1540. +/* The disconnect callback and ep0 routines. These always run in_irq,
  1541. + * except that ep0_queue() is called in the main thread to acknowledge
  1542. + * completion of various requests: set config, set interface, and
  1543. + * Bulk-only device reset. */
  1544. +
  1545. +static void fsg_disconnect(struct usb_gadget *gadget)
  1546. +{
  1547. + struct fsg_dev *fsg = get_gadget_data(gadget);
  1548. +
  1549. + DBG(fsg, "disconnect or port reset\n");
  1550. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  1551. +}
  1552. +
  1553. +
  1554. +static int ep0_queue(struct fsg_dev *fsg)
  1555. +{
  1556. + int rc;
  1557. +
  1558. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  1559. + if (rc != 0 && rc != -ESHUTDOWN) {
  1560. +
  1561. + /* We can't do much more than wait for a reset */
  1562. + WARNING(fsg, "error in submission: %s --> %d\n",
  1563. + fsg->ep0->name, rc);
  1564. + }
  1565. + return rc;
  1566. +}
  1567. +
  1568. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  1569. +{
  1570. + struct fsg_dev *fsg = ep->driver_data;
  1571. +
  1572. + if (req->actual > 0)
  1573. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  1574. + if (req->status || req->actual != req->length)
  1575. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1576. + req->status, req->actual, req->length);
  1577. + if (req->status == -ECONNRESET) // Request was cancelled
  1578. + usb_ep_fifo_flush(ep);
  1579. +
  1580. + if (req->status == 0 && req->context)
  1581. + ((fsg_routine_t) (req->context))(fsg);
  1582. +}
  1583. +
  1584. +
  1585. +/*-------------------------------------------------------------------------*/
  1586. +
  1587. +/* Bulk and interrupt endpoint completion handlers.
  1588. + * These always run in_irq. */
  1589. +
  1590. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  1591. +{
  1592. + struct fsg_dev *fsg = ep->driver_data;
  1593. + struct fsg_buffhd *bh = req->context;
  1594. +
  1595. + if (req->status || req->actual != req->length)
  1596. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1597. + req->status, req->actual, req->length);
  1598. + if (req->status == -ECONNRESET) // Request was cancelled
  1599. + usb_ep_fifo_flush(ep);
  1600. +
  1601. + /* Hold the lock while we update the request and buffer states */
  1602. + smp_wmb();
  1603. + spin_lock(&fsg->lock);
  1604. + bh->inreq_busy = 0;
  1605. + bh->state = BUF_STATE_EMPTY;
  1606. + wakeup_thread(fsg);
  1607. + spin_unlock(&fsg->lock);
  1608. +}
  1609. +
  1610. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  1611. +{
  1612. + struct fsg_dev *fsg = ep->driver_data;
  1613. + struct fsg_buffhd *bh = req->context;
  1614. +
  1615. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  1616. + if (req->status || req->actual != bh->bulk_out_intended_length)
  1617. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1618. + req->status, req->actual,
  1619. + bh->bulk_out_intended_length);
  1620. + if (req->status == -ECONNRESET) // Request was cancelled
  1621. + usb_ep_fifo_flush(ep);
  1622. +
  1623. + /* Hold the lock while we update the request and buffer states */
  1624. + smp_wmb();
  1625. + spin_lock(&fsg->lock);
  1626. + bh->outreq_busy = 0;
  1627. + bh->state = BUF_STATE_FULL;
  1628. + wakeup_thread(fsg);
  1629. + spin_unlock(&fsg->lock);
  1630. +}
  1631. +
  1632. +
  1633. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1634. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  1635. +{
  1636. + struct fsg_dev *fsg = ep->driver_data;
  1637. + struct fsg_buffhd *bh = req->context;
  1638. +
  1639. + if (req->status || req->actual != req->length)
  1640. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1641. + req->status, req->actual, req->length);
  1642. + if (req->status == -ECONNRESET) // Request was cancelled
  1643. + usb_ep_fifo_flush(ep);
  1644. +
  1645. + /* Hold the lock while we update the request and buffer states */
  1646. + smp_wmb();
  1647. + spin_lock(&fsg->lock);
  1648. + fsg->intreq_busy = 0;
  1649. + bh->state = BUF_STATE_EMPTY;
  1650. + wakeup_thread(fsg);
  1651. + spin_unlock(&fsg->lock);
  1652. +}
  1653. +
  1654. +#else
  1655. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  1656. +{}
  1657. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1658. +
  1659. +
  1660. +/*-------------------------------------------------------------------------*/
  1661. +
  1662. +/* Ep0 class-specific handlers. These always run in_irq. */
  1663. +
  1664. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1665. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1666. +{
  1667. + struct usb_request *req = fsg->ep0req;
  1668. + static u8 cbi_reset_cmnd[6] = {
  1669. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  1670. +
  1671. + /* Error in command transfer? */
  1672. + if (req->status || req->length != req->actual ||
  1673. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  1674. +
  1675. + /* Not all controllers allow a protocol stall after
  1676. + * receiving control-out data, but we'll try anyway. */
  1677. + fsg_set_halt(fsg, fsg->ep0);
  1678. + return; // Wait for reset
  1679. + }
  1680. +
  1681. + /* Is it the special reset command? */
  1682. + if (req->actual >= sizeof cbi_reset_cmnd &&
  1683. + memcmp(req->buf, cbi_reset_cmnd,
  1684. + sizeof cbi_reset_cmnd) == 0) {
  1685. +
  1686. + /* Raise an exception to stop the current operation
  1687. + * and reinitialize our state. */
  1688. + DBG(fsg, "cbi reset request\n");
  1689. + raise_exception(fsg, FSG_STATE_RESET);
  1690. + return;
  1691. + }
  1692. +
  1693. + VDBG(fsg, "CB[I] accept device-specific command\n");
  1694. + spin_lock(&fsg->lock);
  1695. +
  1696. + /* Save the command for later */
  1697. + if (fsg->cbbuf_cmnd_size)
  1698. + WARNING(fsg, "CB[I] overwriting previous command\n");
  1699. + fsg->cbbuf_cmnd_size = req->actual;
  1700. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  1701. +
  1702. + wakeup_thread(fsg);
  1703. + spin_unlock(&fsg->lock);
  1704. +}
  1705. +
  1706. +#else
  1707. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1708. +{}
  1709. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1710. +
  1711. +
  1712. +static int class_setup_req(struct fsg_dev *fsg,
  1713. + const struct usb_ctrlrequest *ctrl)
  1714. +{
  1715. + struct usb_request *req = fsg->ep0req;
  1716. + int value = -EOPNOTSUPP;
  1717. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  1718. + u16 w_value = le16_to_cpu(ctrl->wValue);
  1719. + u16 w_length = le16_to_cpu(ctrl->wLength);
  1720. +
  1721. + if (!fsg->config)
  1722. + return value;
  1723. +
  1724. + /* Handle Bulk-only class-specific requests */
  1725. + if (transport_is_bbb()) {
  1726. + switch (ctrl->bRequest) {
  1727. +
  1728. + case US_BULK_RESET_REQUEST:
  1729. + if (ctrl->bRequestType != (USB_DIR_OUT |
  1730. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1731. + break;
  1732. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  1733. + value = -EDOM;
  1734. + break;
  1735. + }
  1736. +
  1737. + /* Raise an exception to stop the current operation
  1738. + * and reinitialize our state. */
  1739. + DBG(fsg, "bulk reset request\n");
  1740. + raise_exception(fsg, FSG_STATE_RESET);
  1741. + value = DELAYED_STATUS;
  1742. + break;
  1743. +
  1744. + case US_BULK_GET_MAX_LUN:
  1745. + if (ctrl->bRequestType != (USB_DIR_IN |
  1746. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1747. + break;
  1748. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  1749. + value = -EDOM;
  1750. + break;
  1751. + }
  1752. + VDBG(fsg, "get max LUN\n");
  1753. + *(u8 *) req->buf = fsg->nluns - 1;
  1754. + value = 1;
  1755. + break;
  1756. + }
  1757. + }
  1758. +
  1759. + /* Handle CBI class-specific requests */
  1760. + else {
  1761. + switch (ctrl->bRequest) {
  1762. +
  1763. + case USB_CBI_ADSC_REQUEST:
  1764. + if (ctrl->bRequestType != (USB_DIR_OUT |
  1765. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1766. + break;
  1767. + if (w_index != 0 || w_value != 0) {
  1768. + value = -EDOM;
  1769. + break;
  1770. + }
  1771. + if (w_length > MAX_COMMAND_SIZE) {
  1772. + value = -EOVERFLOW;
  1773. + break;
  1774. + }
  1775. + value = w_length;
  1776. + fsg->ep0req->context = received_cbi_adsc;
  1777. + break;
  1778. + }
  1779. + }
  1780. +
  1781. + if (value == -EOPNOTSUPP)
  1782. + VDBG(fsg,
  1783. + "unknown class-specific control req "
  1784. + "%02x.%02x v%04x i%04x l%u\n",
  1785. + ctrl->bRequestType, ctrl->bRequest,
  1786. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  1787. + return value;
  1788. +}
  1789. +
  1790. +
  1791. +/*-------------------------------------------------------------------------*/
  1792. +
  1793. +/* Ep0 standard request handlers. These always run in_irq. */
  1794. +
  1795. +static int standard_setup_req(struct fsg_dev *fsg,
  1796. + const struct usb_ctrlrequest *ctrl)
  1797. +{
  1798. + struct usb_request *req = fsg->ep0req;
  1799. + int value = -EOPNOTSUPP;
  1800. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  1801. + u16 w_value = le16_to_cpu(ctrl->wValue);
  1802. +
  1803. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  1804. + * but config change events will also reconfigure hardware. */
  1805. + switch (ctrl->bRequest) {
  1806. +
  1807. + case USB_REQ_GET_DESCRIPTOR:
  1808. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1809. + USB_RECIP_DEVICE))
  1810. + break;
  1811. + switch (w_value >> 8) {
  1812. +
  1813. + case USB_DT_DEVICE:
  1814. + VDBG(fsg, "get device descriptor\n");
  1815. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  1816. + value = sizeof device_desc;
  1817. + memcpy(req->buf, &device_desc, value);
  1818. + break;
  1819. + case USB_DT_DEVICE_QUALIFIER:
  1820. + VDBG(fsg, "get device qualifier\n");
  1821. + if (!gadget_is_dualspeed(fsg->gadget) ||
  1822. + fsg->gadget->speed == USB_SPEED_SUPER)
  1823. + break;
  1824. + /*
  1825. + * Assume ep0 uses the same maxpacket value for both
  1826. + * speeds
  1827. + */
  1828. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  1829. + value = sizeof dev_qualifier;
  1830. + memcpy(req->buf, &dev_qualifier, value);
  1831. + break;
  1832. +
  1833. + case USB_DT_OTHER_SPEED_CONFIG:
  1834. + VDBG(fsg, "get other-speed config descriptor\n");
  1835. + if (!gadget_is_dualspeed(fsg->gadget) ||
  1836. + fsg->gadget->speed == USB_SPEED_SUPER)
  1837. + break;
  1838. + goto get_config;
  1839. + case USB_DT_CONFIG:
  1840. + VDBG(fsg, "get configuration descriptor\n");
  1841. +get_config:
  1842. + value = populate_config_buf(fsg->gadget,
  1843. + req->buf,
  1844. + w_value >> 8,
  1845. + w_value & 0xff);
  1846. + break;
  1847. +
  1848. + case USB_DT_STRING:
  1849. + VDBG(fsg, "get string descriptor\n");
  1850. +
  1851. + /* wIndex == language code */
  1852. + value = usb_gadget_get_string(&fsg_stringtab,
  1853. + w_value & 0xff, req->buf);
  1854. + break;
  1855. +
  1856. + case USB_DT_BOS:
  1857. + VDBG(fsg, "get bos descriptor\n");
  1858. +
  1859. + if (gadget_is_superspeed(fsg->gadget))
  1860. + value = populate_bos(fsg, req->buf);
  1861. + break;
  1862. + }
  1863. +
  1864. + break;
  1865. +
  1866. + /* One config, two speeds */
  1867. + case USB_REQ_SET_CONFIGURATION:
  1868. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  1869. + USB_RECIP_DEVICE))
  1870. + break;
  1871. + VDBG(fsg, "set configuration\n");
  1872. + if (w_value == CONFIG_VALUE || w_value == 0) {
  1873. + fsg->new_config = w_value;
  1874. +
  1875. + /* Raise an exception to wipe out previous transaction
  1876. + * state (queued bufs, etc) and set the new config. */
  1877. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  1878. + value = DELAYED_STATUS;
  1879. + }
  1880. + break;
  1881. + case USB_REQ_GET_CONFIGURATION:
  1882. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1883. + USB_RECIP_DEVICE))
  1884. + break;
  1885. + VDBG(fsg, "get configuration\n");
  1886. + *(u8 *) req->buf = fsg->config;
  1887. + value = 1;
  1888. + break;
  1889. +
  1890. + case USB_REQ_SET_INTERFACE:
  1891. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  1892. + USB_RECIP_INTERFACE))
  1893. + break;
  1894. + if (fsg->config && w_index == 0) {
  1895. +
  1896. + /* Raise an exception to wipe out previous transaction
  1897. + * state (queued bufs, etc) and install the new
  1898. + * interface altsetting. */
  1899. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  1900. + value = DELAYED_STATUS;
  1901. + }
  1902. + break;
  1903. + case USB_REQ_GET_INTERFACE:
  1904. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1905. + USB_RECIP_INTERFACE))
  1906. + break;
  1907. + if (!fsg->config)
  1908. + break;
  1909. + if (w_index != 0) {
  1910. + value = -EDOM;
  1911. + break;
  1912. + }
  1913. + VDBG(fsg, "get interface\n");
  1914. + *(u8 *) req->buf = 0;
  1915. + value = 1;
  1916. + break;
  1917. +
  1918. + default:
  1919. + VDBG(fsg,
  1920. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  1921. + ctrl->bRequestType, ctrl->bRequest,
  1922. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  1923. + }
  1924. +
  1925. + return value;
  1926. +}
  1927. +
  1928. +
  1929. +static int fsg_setup(struct usb_gadget *gadget,
  1930. + const struct usb_ctrlrequest *ctrl)
  1931. +{
  1932. + struct fsg_dev *fsg = get_gadget_data(gadget);
  1933. + int rc;
  1934. + int w_length = le16_to_cpu(ctrl->wLength);
  1935. +
  1936. + ++fsg->ep0_req_tag; // Record arrival of a new request
  1937. + fsg->ep0req->context = NULL;
  1938. + fsg->ep0req->length = 0;
  1939. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  1940. +
  1941. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  1942. + rc = class_setup_req(fsg, ctrl);
  1943. + else
  1944. + rc = standard_setup_req(fsg, ctrl);
  1945. +
  1946. + /* Respond with data/status or defer until later? */
  1947. + if (rc >= 0 && rc != DELAYED_STATUS) {
  1948. + rc = min(rc, w_length);
  1949. + fsg->ep0req->length = rc;
  1950. + fsg->ep0req->zero = rc < w_length;
  1951. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  1952. + "ep0-in" : "ep0-out");
  1953. + rc = ep0_queue(fsg);
  1954. + }
  1955. +
  1956. + /* Device either stalls (rc < 0) or reports success */
  1957. + return rc;
  1958. +}
  1959. +
  1960. +
  1961. +/*-------------------------------------------------------------------------*/
  1962. +
  1963. +/* All the following routines run in process context */
  1964. +
  1965. +
  1966. +/* Use this for bulk or interrupt transfers, not ep0 */
  1967. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  1968. + struct usb_request *req, int *pbusy,
  1969. + enum fsg_buffer_state *state)
  1970. +{
  1971. + int rc;
  1972. +
  1973. + if (ep == fsg->bulk_in)
  1974. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  1975. + else if (ep == fsg->intr_in)
  1976. + dump_msg(fsg, "intr-in", req->buf, req->length);
  1977. +
  1978. + spin_lock_irq(&fsg->lock);
  1979. + *pbusy = 1;
  1980. + *state = BUF_STATE_BUSY;
  1981. + spin_unlock_irq(&fsg->lock);
  1982. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  1983. + if (rc != 0) {
  1984. + *pbusy = 0;
  1985. + *state = BUF_STATE_EMPTY;
  1986. +
  1987. + /* We can't do much more than wait for a reset */
  1988. +
  1989. + /* Note: currently the net2280 driver fails zero-length
  1990. + * submissions if DMA is enabled. */
  1991. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  1992. + req->length == 0))
  1993. + WARNING(fsg, "error in submission: %s --> %d\n",
  1994. + ep->name, rc);
  1995. + }
  1996. +}
  1997. +
  1998. +
  1999. +static int sleep_thread(struct fsg_dev *fsg)
  2000. +{
  2001. + int rc = 0;
  2002. +
  2003. + /* Wait until a signal arrives or we are woken up */
  2004. + for (;;) {
  2005. + try_to_freeze();
  2006. + set_current_state(TASK_INTERRUPTIBLE);
  2007. + if (signal_pending(current)) {
  2008. + rc = -EINTR;
  2009. + break;
  2010. + }
  2011. + if (fsg->thread_wakeup_needed)
  2012. + break;
  2013. + schedule();
  2014. + }
  2015. + __set_current_state(TASK_RUNNING);
  2016. + fsg->thread_wakeup_needed = 0;
  2017. + return rc;
  2018. +}
  2019. +
  2020. +
  2021. +/*-------------------------------------------------------------------------*/
  2022. +
  2023. +static int do_read(struct fsg_dev *fsg)
  2024. +{
  2025. + struct fsg_lun *curlun = fsg->curlun;
  2026. + u32 lba;
  2027. + struct fsg_buffhd *bh;
  2028. + int rc;
  2029. + u32 amount_left;
  2030. + loff_t file_offset, file_offset_tmp;
  2031. + unsigned int amount;
  2032. + ssize_t nread;
  2033. +
  2034. + /* Get the starting Logical Block Address and check that it's
  2035. + * not too big */
  2036. + if (fsg->cmnd[0] == READ_6)
  2037. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  2038. + else {
  2039. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2040. +
  2041. + /* We allow DPO (Disable Page Out = don't save data in the
  2042. + * cache) and FUA (Force Unit Access = don't read from the
  2043. + * cache), but we don't implement them. */
  2044. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  2045. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2046. + return -EINVAL;
  2047. + }
  2048. + }
  2049. + if (lba >= curlun->num_sectors) {
  2050. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2051. + return -EINVAL;
  2052. + }
  2053. + file_offset = ((loff_t) lba) << curlun->blkbits;
  2054. +
  2055. + /* Carry out the file reads */
  2056. + amount_left = fsg->data_size_from_cmnd;
  2057. + if (unlikely(amount_left == 0))
  2058. + return -EIO; // No default reply
  2059. +
  2060. + for (;;) {
  2061. +
  2062. + /* Figure out how much we need to read:
  2063. + * Try to read the remaining amount.
  2064. + * But don't read more than the buffer size.
  2065. + * And don't try to read past the end of the file.
  2066. + */
  2067. + amount = min((unsigned int) amount_left, mod_data.buflen);
  2068. + amount = min((loff_t) amount,
  2069. + curlun->file_length - file_offset);
  2070. +
  2071. + /* Wait for the next buffer to become available */
  2072. + bh = fsg->next_buffhd_to_fill;
  2073. + while (bh->state != BUF_STATE_EMPTY) {
  2074. + rc = sleep_thread(fsg);
  2075. + if (rc)
  2076. + return rc;
  2077. + }
  2078. +
  2079. + /* If we were asked to read past the end of file,
  2080. + * end with an empty buffer. */
  2081. + if (amount == 0) {
  2082. + curlun->sense_data =
  2083. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2084. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2085. + curlun->info_valid = 1;
  2086. + bh->inreq->length = 0;
  2087. + bh->state = BUF_STATE_FULL;
  2088. + break;
  2089. + }
  2090. +
  2091. + /* Perform the read */
  2092. + file_offset_tmp = file_offset;
  2093. + nread = vfs_read(curlun->filp,
  2094. + (char __user *) bh->buf,
  2095. + amount, &file_offset_tmp);
  2096. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  2097. + (unsigned long long) file_offset,
  2098. + (int) nread);
  2099. + if (signal_pending(current))
  2100. + return -EINTR;
  2101. +
  2102. + if (nread < 0) {
  2103. + LDBG(curlun, "error in file read: %d\n",
  2104. + (int) nread);
  2105. + nread = 0;
  2106. + } else if (nread < amount) {
  2107. + LDBG(curlun, "partial file read: %d/%u\n",
  2108. + (int) nread, amount);
  2109. + nread = round_down(nread, curlun->blksize);
  2110. + }
  2111. + file_offset += nread;
  2112. + amount_left -= nread;
  2113. + fsg->residue -= nread;
  2114. +
  2115. + /* Except at the end of the transfer, nread will be
  2116. + * equal to the buffer size, which is divisible by the
  2117. + * bulk-in maxpacket size.
  2118. + */
  2119. + bh->inreq->length = nread;
  2120. + bh->state = BUF_STATE_FULL;
  2121. +
  2122. + /* If an error occurred, report it and its position */
  2123. + if (nread < amount) {
  2124. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  2125. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2126. + curlun->info_valid = 1;
  2127. + break;
  2128. + }
  2129. +
  2130. + if (amount_left == 0)
  2131. + break; // No more left to read
  2132. +
  2133. + /* Send this buffer and go read some more */
  2134. + bh->inreq->zero = 0;
  2135. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2136. + &bh->inreq_busy, &bh->state);
  2137. + fsg->next_buffhd_to_fill = bh->next;
  2138. + }
  2139. +
  2140. + return -EIO; // No default reply
  2141. +}
  2142. +
  2143. +
  2144. +/*-------------------------------------------------------------------------*/
  2145. +
  2146. +static int do_write(struct fsg_dev *fsg)
  2147. +{
  2148. + struct fsg_lun *curlun = fsg->curlun;
  2149. + u32 lba;
  2150. + struct fsg_buffhd *bh;
  2151. + int get_some_more;
  2152. + u32 amount_left_to_req, amount_left_to_write;
  2153. + loff_t usb_offset, file_offset, file_offset_tmp;
  2154. + unsigned int amount;
  2155. + ssize_t nwritten;
  2156. + int rc;
  2157. +
  2158. + if (curlun->ro) {
  2159. + curlun->sense_data = SS_WRITE_PROTECTED;
  2160. + return -EINVAL;
  2161. + }
  2162. + spin_lock(&curlun->filp->f_lock);
  2163. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  2164. + spin_unlock(&curlun->filp->f_lock);
  2165. +
  2166. + /* Get the starting Logical Block Address and check that it's
  2167. + * not too big */
  2168. + if (fsg->cmnd[0] == WRITE_6)
  2169. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  2170. + else {
  2171. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2172. +
  2173. + /* We allow DPO (Disable Page Out = don't save data in the
  2174. + * cache) and FUA (Force Unit Access = write directly to the
  2175. + * medium). We don't implement DPO; we implement FUA by
  2176. + * performing synchronous output. */
  2177. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  2178. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2179. + return -EINVAL;
  2180. + }
  2181. + /* FUA */
  2182. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  2183. + spin_lock(&curlun->filp->f_lock);
  2184. + curlun->filp->f_flags |= O_DSYNC;
  2185. + spin_unlock(&curlun->filp->f_lock);
  2186. + }
  2187. + }
  2188. + if (lba >= curlun->num_sectors) {
  2189. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2190. + return -EINVAL;
  2191. + }
  2192. +
  2193. + /* Carry out the file writes */
  2194. + get_some_more = 1;
  2195. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  2196. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  2197. +
  2198. + while (amount_left_to_write > 0) {
  2199. +
  2200. + /* Queue a request for more data from the host */
  2201. + bh = fsg->next_buffhd_to_fill;
  2202. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  2203. +
  2204. + /* Figure out how much we want to get:
  2205. + * Try to get the remaining amount,
  2206. + * but not more than the buffer size.
  2207. + */
  2208. + amount = min(amount_left_to_req, mod_data.buflen);
  2209. +
  2210. + /* Beyond the end of the backing file? */
  2211. + if (usb_offset >= curlun->file_length) {
  2212. + get_some_more = 0;
  2213. + curlun->sense_data =
  2214. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2215. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  2216. + curlun->info_valid = 1;
  2217. + continue;
  2218. + }
  2219. +
  2220. + /* Get the next buffer */
  2221. + usb_offset += amount;
  2222. + fsg->usb_amount_left -= amount;
  2223. + amount_left_to_req -= amount;
  2224. + if (amount_left_to_req == 0)
  2225. + get_some_more = 0;
  2226. +
  2227. + /* Except at the end of the transfer, amount will be
  2228. + * equal to the buffer size, which is divisible by
  2229. + * the bulk-out maxpacket size.
  2230. + */
  2231. + set_bulk_out_req_length(fsg, bh, amount);
  2232. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  2233. + &bh->outreq_busy, &bh->state);
  2234. + fsg->next_buffhd_to_fill = bh->next;
  2235. + continue;
  2236. + }
  2237. +
  2238. + /* Write the received data to the backing file */
  2239. + bh = fsg->next_buffhd_to_drain;
  2240. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  2241. + break; // We stopped early
  2242. + if (bh->state == BUF_STATE_FULL) {
  2243. + smp_rmb();
  2244. + fsg->next_buffhd_to_drain = bh->next;
  2245. + bh->state = BUF_STATE_EMPTY;
  2246. +
  2247. + /* Did something go wrong with the transfer? */
  2248. + if (bh->outreq->status != 0) {
  2249. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  2250. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2251. + curlun->info_valid = 1;
  2252. + break;
  2253. + }
  2254. +
  2255. + amount = bh->outreq->actual;
  2256. + if (curlun->file_length - file_offset < amount) {
  2257. + LERROR(curlun,
  2258. + "write %u @ %llu beyond end %llu\n",
  2259. + amount, (unsigned long long) file_offset,
  2260. + (unsigned long long) curlun->file_length);
  2261. + amount = curlun->file_length - file_offset;
  2262. + }
  2263. +
  2264. + /* Don't accept excess data. The spec doesn't say
  2265. + * what to do in this case. We'll ignore the error.
  2266. + */
  2267. + amount = min(amount, bh->bulk_out_intended_length);
  2268. +
  2269. + /* Don't write a partial block */
  2270. + amount = round_down(amount, curlun->blksize);
  2271. + if (amount == 0)
  2272. + goto empty_write;
  2273. +
  2274. + /* Perform the write */
  2275. + file_offset_tmp = file_offset;
  2276. + nwritten = vfs_write(curlun->filp,
  2277. + (char __user *) bh->buf,
  2278. + amount, &file_offset_tmp);
  2279. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  2280. + (unsigned long long) file_offset,
  2281. + (int) nwritten);
  2282. + if (signal_pending(current))
  2283. + return -EINTR; // Interrupted!
  2284. +
  2285. + if (nwritten < 0) {
  2286. + LDBG(curlun, "error in file write: %d\n",
  2287. + (int) nwritten);
  2288. + nwritten = 0;
  2289. + } else if (nwritten < amount) {
  2290. + LDBG(curlun, "partial file write: %d/%u\n",
  2291. + (int) nwritten, amount);
  2292. + nwritten = round_down(nwritten, curlun->blksize);
  2293. + }
  2294. + file_offset += nwritten;
  2295. + amount_left_to_write -= nwritten;
  2296. + fsg->residue -= nwritten;
  2297. +
  2298. + /* If an error occurred, report it and its position */
  2299. + if (nwritten < amount) {
  2300. + curlun->sense_data = SS_WRITE_ERROR;
  2301. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2302. + curlun->info_valid = 1;
  2303. + break;
  2304. + }
  2305. +
  2306. + empty_write:
  2307. + /* Did the host decide to stop early? */
  2308. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  2309. + fsg->short_packet_received = 1;
  2310. + break;
  2311. + }
  2312. + continue;
  2313. + }
  2314. +
  2315. + /* Wait for something to happen */
  2316. + rc = sleep_thread(fsg);
  2317. + if (rc)
  2318. + return rc;
  2319. + }
  2320. +
  2321. + return -EIO; // No default reply
  2322. +}
  2323. +
  2324. +
  2325. +/*-------------------------------------------------------------------------*/
  2326. +
  2327. +static int do_synchronize_cache(struct fsg_dev *fsg)
  2328. +{
  2329. + struct fsg_lun *curlun = fsg->curlun;
  2330. + int rc;
  2331. +
  2332. + /* We ignore the requested LBA and write out all file's
  2333. + * dirty data buffers. */
  2334. + rc = fsg_lun_fsync_sub(curlun);
  2335. + if (rc)
  2336. + curlun->sense_data = SS_WRITE_ERROR;
  2337. + return 0;
  2338. +}
  2339. +
  2340. +
  2341. +/*-------------------------------------------------------------------------*/
  2342. +
  2343. +static void invalidate_sub(struct fsg_lun *curlun)
  2344. +{
  2345. + struct file *filp = curlun->filp;
  2346. + struct inode *inode = filp->f_path.dentry->d_inode;
  2347. + unsigned long rc;
  2348. +
  2349. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  2350. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  2351. +}
  2352. +
  2353. +static int do_verify(struct fsg_dev *fsg)
  2354. +{
  2355. + struct fsg_lun *curlun = fsg->curlun;
  2356. + u32 lba;
  2357. + u32 verification_length;
  2358. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  2359. + loff_t file_offset, file_offset_tmp;
  2360. + u32 amount_left;
  2361. + unsigned int amount;
  2362. + ssize_t nread;
  2363. +
  2364. + /* Get the starting Logical Block Address and check that it's
  2365. + * not too big */
  2366. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2367. + if (lba >= curlun->num_sectors) {
  2368. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2369. + return -EINVAL;
  2370. + }
  2371. +
  2372. + /* We allow DPO (Disable Page Out = don't save data in the
  2373. + * cache) but we don't implement it. */
  2374. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  2375. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2376. + return -EINVAL;
  2377. + }
  2378. +
  2379. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  2380. + if (unlikely(verification_length == 0))
  2381. + return -EIO; // No default reply
  2382. +
  2383. + /* Prepare to carry out the file verify */
  2384. + amount_left = verification_length << curlun->blkbits;
  2385. + file_offset = ((loff_t) lba) << curlun->blkbits;
  2386. +
  2387. + /* Write out all the dirty buffers before invalidating them */
  2388. + fsg_lun_fsync_sub(curlun);
  2389. + if (signal_pending(current))
  2390. + return -EINTR;
  2391. +
  2392. + invalidate_sub(curlun);
  2393. + if (signal_pending(current))
  2394. + return -EINTR;
  2395. +
  2396. + /* Just try to read the requested blocks */
  2397. + while (amount_left > 0) {
  2398. +
  2399. + /* Figure out how much we need to read:
  2400. + * Try to read the remaining amount, but not more than
  2401. + * the buffer size.
  2402. + * And don't try to read past the end of the file.
  2403. + */
  2404. + amount = min((unsigned int) amount_left, mod_data.buflen);
  2405. + amount = min((loff_t) amount,
  2406. + curlun->file_length - file_offset);
  2407. + if (amount == 0) {
  2408. + curlun->sense_data =
  2409. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2410. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2411. + curlun->info_valid = 1;
  2412. + break;
  2413. + }
  2414. +
  2415. + /* Perform the read */
  2416. + file_offset_tmp = file_offset;
  2417. + nread = vfs_read(curlun->filp,
  2418. + (char __user *) bh->buf,
  2419. + amount, &file_offset_tmp);
  2420. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  2421. + (unsigned long long) file_offset,
  2422. + (int) nread);
  2423. + if (signal_pending(current))
  2424. + return -EINTR;
  2425. +
  2426. + if (nread < 0) {
  2427. + LDBG(curlun, "error in file verify: %d\n",
  2428. + (int) nread);
  2429. + nread = 0;
  2430. + } else if (nread < amount) {
  2431. + LDBG(curlun, "partial file verify: %d/%u\n",
  2432. + (int) nread, amount);
  2433. + nread = round_down(nread, curlun->blksize);
  2434. + }
  2435. + if (nread == 0) {
  2436. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  2437. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2438. + curlun->info_valid = 1;
  2439. + break;
  2440. + }
  2441. + file_offset += nread;
  2442. + amount_left -= nread;
  2443. + }
  2444. + return 0;
  2445. +}
  2446. +
  2447. +
  2448. +/*-------------------------------------------------------------------------*/
  2449. +
  2450. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2451. +{
  2452. + u8 *buf = (u8 *) bh->buf;
  2453. +
  2454. + static char vendor_id[] = "Linux ";
  2455. + static char product_disk_id[] = "File-Stor Gadget";
  2456. + static char product_cdrom_id[] = "File-CD Gadget ";
  2457. +
  2458. + if (!fsg->curlun) { // Unsupported LUNs are okay
  2459. + fsg->bad_lun_okay = 1;
  2460. + memset(buf, 0, 36);
  2461. + buf[0] = 0x7f; // Unsupported, no device-type
  2462. + buf[4] = 31; // Additional length
  2463. + return 36;
  2464. + }
  2465. +
  2466. + memset(buf, 0, 8);
  2467. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  2468. + if (mod_data.removable)
  2469. + buf[1] = 0x80;
  2470. + buf[2] = 2; // ANSI SCSI level 2
  2471. + buf[3] = 2; // SCSI-2 INQUIRY data format
  2472. + buf[4] = 31; // Additional length
  2473. + // No special options
  2474. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  2475. + (mod_data.cdrom ? product_cdrom_id :
  2476. + product_disk_id),
  2477. + mod_data.release);
  2478. + return 36;
  2479. +}
  2480. +
  2481. +
  2482. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2483. +{
  2484. + struct fsg_lun *curlun = fsg->curlun;
  2485. + u8 *buf = (u8 *) bh->buf;
  2486. + u32 sd, sdinfo;
  2487. + int valid;
  2488. +
  2489. + /*
  2490. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  2491. + *
  2492. + * If a REQUEST SENSE command is received from an initiator
  2493. + * with a pending unit attention condition (before the target
  2494. + * generates the contingent allegiance condition), then the
  2495. + * target shall either:
  2496. + * a) report any pending sense data and preserve the unit
  2497. + * attention condition on the logical unit, or,
  2498. + * b) report the unit attention condition, may discard any
  2499. + * pending sense data, and clear the unit attention
  2500. + * condition on the logical unit for that initiator.
  2501. + *
  2502. + * FSG normally uses option a); enable this code to use option b).
  2503. + */
  2504. +#if 0
  2505. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  2506. + curlun->sense_data = curlun->unit_attention_data;
  2507. + curlun->unit_attention_data = SS_NO_SENSE;
  2508. + }
  2509. +#endif
  2510. +
  2511. + if (!curlun) { // Unsupported LUNs are okay
  2512. + fsg->bad_lun_okay = 1;
  2513. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  2514. + sdinfo = 0;
  2515. + valid = 0;
  2516. + } else {
  2517. + sd = curlun->sense_data;
  2518. + sdinfo = curlun->sense_data_info;
  2519. + valid = curlun->info_valid << 7;
  2520. + curlun->sense_data = SS_NO_SENSE;
  2521. + curlun->sense_data_info = 0;
  2522. + curlun->info_valid = 0;
  2523. + }
  2524. +
  2525. + memset(buf, 0, 18);
  2526. + buf[0] = valid | 0x70; // Valid, current error
  2527. + buf[2] = SK(sd);
  2528. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  2529. + buf[7] = 18 - 8; // Additional sense length
  2530. + buf[12] = ASC(sd);
  2531. + buf[13] = ASCQ(sd);
  2532. + return 18;
  2533. +}
  2534. +
  2535. +
  2536. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2537. +{
  2538. + struct fsg_lun *curlun = fsg->curlun;
  2539. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  2540. + int pmi = fsg->cmnd[8];
  2541. + u8 *buf = (u8 *) bh->buf;
  2542. +
  2543. + /* Check the PMI and LBA fields */
  2544. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  2545. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2546. + return -EINVAL;
  2547. + }
  2548. +
  2549. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  2550. + /* Max logical block */
  2551. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  2552. + return 8;
  2553. +}
  2554. +
  2555. +
  2556. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2557. +{
  2558. + struct fsg_lun *curlun = fsg->curlun;
  2559. + int msf = fsg->cmnd[1] & 0x02;
  2560. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  2561. + u8 *buf = (u8 *) bh->buf;
  2562. +
  2563. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  2564. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2565. + return -EINVAL;
  2566. + }
  2567. + if (lba >= curlun->num_sectors) {
  2568. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2569. + return -EINVAL;
  2570. + }
  2571. +
  2572. + memset(buf, 0, 8);
  2573. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  2574. + store_cdrom_address(&buf[4], msf, lba);
  2575. + return 8;
  2576. +}
  2577. +
  2578. +
  2579. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2580. +{
  2581. + struct fsg_lun *curlun = fsg->curlun;
  2582. + int msf = fsg->cmnd[1] & 0x02;
  2583. + int start_track = fsg->cmnd[6];
  2584. + u8 *buf = (u8 *) bh->buf;
  2585. +
  2586. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  2587. + start_track > 1) {
  2588. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2589. + return -EINVAL;
  2590. + }
  2591. +
  2592. + memset(buf, 0, 20);
  2593. + buf[1] = (20-2); /* TOC data length */
  2594. + buf[2] = 1; /* First track number */
  2595. + buf[3] = 1; /* Last track number */
  2596. + buf[5] = 0x16; /* Data track, copying allowed */
  2597. + buf[6] = 0x01; /* Only track is number 1 */
  2598. + store_cdrom_address(&buf[8], msf, 0);
  2599. +
  2600. + buf[13] = 0x16; /* Lead-out track is data */
  2601. + buf[14] = 0xAA; /* Lead-out track number */
  2602. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  2603. + return 20;
  2604. +}
  2605. +
  2606. +
  2607. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2608. +{
  2609. + struct fsg_lun *curlun = fsg->curlun;
  2610. + int mscmnd = fsg->cmnd[0];
  2611. + u8 *buf = (u8 *) bh->buf;
  2612. + u8 *buf0 = buf;
  2613. + int pc, page_code;
  2614. + int changeable_values, all_pages;
  2615. + int valid_page = 0;
  2616. + int len, limit;
  2617. +
  2618. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  2619. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2620. + return -EINVAL;
  2621. + }
  2622. + pc = fsg->cmnd[2] >> 6;
  2623. + page_code = fsg->cmnd[2] & 0x3f;
  2624. + if (pc == 3) {
  2625. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  2626. + return -EINVAL;
  2627. + }
  2628. + changeable_values = (pc == 1);
  2629. + all_pages = (page_code == 0x3f);
  2630. +
  2631. + /* Write the mode parameter header. Fixed values are: default
  2632. + * medium type, no cache control (DPOFUA), and no block descriptors.
  2633. + * The only variable value is the WriteProtect bit. We will fill in
  2634. + * the mode data length later. */
  2635. + memset(buf, 0, 8);
  2636. + if (mscmnd == MODE_SENSE) {
  2637. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  2638. + buf += 4;
  2639. + limit = 255;
  2640. + } else { // MODE_SENSE_10
  2641. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  2642. + buf += 8;
  2643. + limit = 65535; // Should really be mod_data.buflen
  2644. + }
  2645. +
  2646. + /* No block descriptors */
  2647. +
  2648. + /* The mode pages, in numerical order. The only page we support
  2649. + * is the Caching page. */
  2650. + if (page_code == 0x08 || all_pages) {
  2651. + valid_page = 1;
  2652. + buf[0] = 0x08; // Page code
  2653. + buf[1] = 10; // Page length
  2654. + memset(buf+2, 0, 10); // None of the fields are changeable
  2655. +
  2656. + if (!changeable_values) {
  2657. + buf[2] = 0x04; // Write cache enable,
  2658. + // Read cache not disabled
  2659. + // No cache retention priorities
  2660. + put_unaligned_be16(0xffff, &buf[4]);
  2661. + /* Don't disable prefetch */
  2662. + /* Minimum prefetch = 0 */
  2663. + put_unaligned_be16(0xffff, &buf[8]);
  2664. + /* Maximum prefetch */
  2665. + put_unaligned_be16(0xffff, &buf[10]);
  2666. + /* Maximum prefetch ceiling */
  2667. + }
  2668. + buf += 12;
  2669. + }
  2670. +
  2671. + /* Check that a valid page was requested and the mode data length
  2672. + * isn't too long. */
  2673. + len = buf - buf0;
  2674. + if (!valid_page || len > limit) {
  2675. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2676. + return -EINVAL;
  2677. + }
  2678. +
  2679. + /* Store the mode data length */
  2680. + if (mscmnd == MODE_SENSE)
  2681. + buf0[0] = len - 1;
  2682. + else
  2683. + put_unaligned_be16(len - 2, buf0);
  2684. + return len;
  2685. +}
  2686. +
  2687. +
  2688. +static int do_start_stop(struct fsg_dev *fsg)
  2689. +{
  2690. + struct fsg_lun *curlun = fsg->curlun;
  2691. + int loej, start;
  2692. +
  2693. + if (!mod_data.removable) {
  2694. + curlun->sense_data = SS_INVALID_COMMAND;
  2695. + return -EINVAL;
  2696. + }
  2697. +
  2698. + // int immed = fsg->cmnd[1] & 0x01;
  2699. + loej = fsg->cmnd[4] & 0x02;
  2700. + start = fsg->cmnd[4] & 0x01;
  2701. +
  2702. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  2703. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  2704. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  2705. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2706. + return -EINVAL;
  2707. + }
  2708. +
  2709. + if (!start) {
  2710. +
  2711. + /* Are we allowed to unload the media? */
  2712. + if (curlun->prevent_medium_removal) {
  2713. + LDBG(curlun, "unload attempt prevented\n");
  2714. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  2715. + return -EINVAL;
  2716. + }
  2717. + if (loej) { // Simulate an unload/eject
  2718. + up_read(&fsg->filesem);
  2719. + down_write(&fsg->filesem);
  2720. + fsg_lun_close(curlun);
  2721. + up_write(&fsg->filesem);
  2722. + down_read(&fsg->filesem);
  2723. + }
  2724. + } else {
  2725. +
  2726. + /* Our emulation doesn't support mounting; the medium is
  2727. + * available for use as soon as it is loaded. */
  2728. + if (!fsg_lun_is_open(curlun)) {
  2729. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  2730. + return -EINVAL;
  2731. + }
  2732. + }
  2733. +#endif
  2734. + return 0;
  2735. +}
  2736. +
  2737. +
  2738. +static int do_prevent_allow(struct fsg_dev *fsg)
  2739. +{
  2740. + struct fsg_lun *curlun = fsg->curlun;
  2741. + int prevent;
  2742. +
  2743. + if (!mod_data.removable) {
  2744. + curlun->sense_data = SS_INVALID_COMMAND;
  2745. + return -EINVAL;
  2746. + }
  2747. +
  2748. + prevent = fsg->cmnd[4] & 0x01;
  2749. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  2750. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2751. + return -EINVAL;
  2752. + }
  2753. +
  2754. + if (curlun->prevent_medium_removal && !prevent)
  2755. + fsg_lun_fsync_sub(curlun);
  2756. + curlun->prevent_medium_removal = prevent;
  2757. + return 0;
  2758. +}
  2759. +
  2760. +
  2761. +static int do_read_format_capacities(struct fsg_dev *fsg,
  2762. + struct fsg_buffhd *bh)
  2763. +{
  2764. + struct fsg_lun *curlun = fsg->curlun;
  2765. + u8 *buf = (u8 *) bh->buf;
  2766. +
  2767. + buf[0] = buf[1] = buf[2] = 0;
  2768. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  2769. + buf += 4;
  2770. +
  2771. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  2772. + /* Number of blocks */
  2773. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  2774. + buf[4] = 0x02; /* Current capacity */
  2775. + return 12;
  2776. +}
  2777. +
  2778. +
  2779. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2780. +{
  2781. + struct fsg_lun *curlun = fsg->curlun;
  2782. +
  2783. + /* We don't support MODE SELECT */
  2784. + curlun->sense_data = SS_INVALID_COMMAND;
  2785. + return -EINVAL;
  2786. +}
  2787. +
  2788. +
  2789. +/*-------------------------------------------------------------------------*/
  2790. +
  2791. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  2792. +{
  2793. + int rc;
  2794. +
  2795. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  2796. + if (rc == -EAGAIN)
  2797. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  2798. + while (rc != 0) {
  2799. + if (rc != -EAGAIN) {
  2800. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  2801. + rc = 0;
  2802. + break;
  2803. + }
  2804. +
  2805. + /* Wait for a short time and then try again */
  2806. + if (msleep_interruptible(100) != 0)
  2807. + return -EINTR;
  2808. + rc = usb_ep_set_halt(fsg->bulk_in);
  2809. + }
  2810. + return rc;
  2811. +}
  2812. +
  2813. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  2814. +{
  2815. + int rc;
  2816. +
  2817. + DBG(fsg, "bulk-in set wedge\n");
  2818. + rc = usb_ep_set_wedge(fsg->bulk_in);
  2819. + if (rc == -EAGAIN)
  2820. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  2821. + while (rc != 0) {
  2822. + if (rc != -EAGAIN) {
  2823. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  2824. + rc = 0;
  2825. + break;
  2826. + }
  2827. +
  2828. + /* Wait for a short time and then try again */
  2829. + if (msleep_interruptible(100) != 0)
  2830. + return -EINTR;
  2831. + rc = usb_ep_set_wedge(fsg->bulk_in);
  2832. + }
  2833. + return rc;
  2834. +}
  2835. +
  2836. +static int throw_away_data(struct fsg_dev *fsg)
  2837. +{
  2838. + struct fsg_buffhd *bh;
  2839. + u32 amount;
  2840. + int rc;
  2841. +
  2842. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  2843. + fsg->usb_amount_left > 0) {
  2844. +
  2845. + /* Throw away the data in a filled buffer */
  2846. + if (bh->state == BUF_STATE_FULL) {
  2847. + smp_rmb();
  2848. + bh->state = BUF_STATE_EMPTY;
  2849. + fsg->next_buffhd_to_drain = bh->next;
  2850. +
  2851. + /* A short packet or an error ends everything */
  2852. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  2853. + bh->outreq->status != 0) {
  2854. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2855. + return -EINTR;
  2856. + }
  2857. + continue;
  2858. + }
  2859. +
  2860. + /* Try to submit another request if we need one */
  2861. + bh = fsg->next_buffhd_to_fill;
  2862. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  2863. + amount = min(fsg->usb_amount_left,
  2864. + (u32) mod_data.buflen);
  2865. +
  2866. + /* Except at the end of the transfer, amount will be
  2867. + * equal to the buffer size, which is divisible by
  2868. + * the bulk-out maxpacket size.
  2869. + */
  2870. + set_bulk_out_req_length(fsg, bh, amount);
  2871. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  2872. + &bh->outreq_busy, &bh->state);
  2873. + fsg->next_buffhd_to_fill = bh->next;
  2874. + fsg->usb_amount_left -= amount;
  2875. + continue;
  2876. + }
  2877. +
  2878. + /* Otherwise wait for something to happen */
  2879. + rc = sleep_thread(fsg);
  2880. + if (rc)
  2881. + return rc;
  2882. + }
  2883. + return 0;
  2884. +}
  2885. +
  2886. +
  2887. +static int finish_reply(struct fsg_dev *fsg)
  2888. +{
  2889. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  2890. + int rc = 0;
  2891. +
  2892. + switch (fsg->data_dir) {
  2893. + case DATA_DIR_NONE:
  2894. + break; // Nothing to send
  2895. +
  2896. + /* If we don't know whether the host wants to read or write,
  2897. + * this must be CB or CBI with an unknown command. We mustn't
  2898. + * try to send or receive any data. So stall both bulk pipes
  2899. + * if we can and wait for a reset. */
  2900. + case DATA_DIR_UNKNOWN:
  2901. + if (mod_data.can_stall) {
  2902. + fsg_set_halt(fsg, fsg->bulk_out);
  2903. + rc = halt_bulk_in_endpoint(fsg);
  2904. + }
  2905. + break;
  2906. +
  2907. + /* All but the last buffer of data must have already been sent */
  2908. + case DATA_DIR_TO_HOST:
  2909. + if (fsg->data_size == 0)
  2910. + ; // Nothing to send
  2911. +
  2912. + /* If there's no residue, simply send the last buffer */
  2913. + else if (fsg->residue == 0) {
  2914. + bh->inreq->zero = 0;
  2915. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2916. + &bh->inreq_busy, &bh->state);
  2917. + fsg->next_buffhd_to_fill = bh->next;
  2918. + }
  2919. +
  2920. + /* There is a residue. For CB and CBI, simply mark the end
  2921. + * of the data with a short packet. However, if we are
  2922. + * allowed to stall, there was no data at all (residue ==
  2923. + * data_size), and the command failed (invalid LUN or
  2924. + * sense data is set), then halt the bulk-in endpoint
  2925. + * instead. */
  2926. + else if (!transport_is_bbb()) {
  2927. + if (mod_data.can_stall &&
  2928. + fsg->residue == fsg->data_size &&
  2929. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  2930. + bh->state = BUF_STATE_EMPTY;
  2931. + rc = halt_bulk_in_endpoint(fsg);
  2932. + } else {
  2933. + bh->inreq->zero = 1;
  2934. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2935. + &bh->inreq_busy, &bh->state);
  2936. + fsg->next_buffhd_to_fill = bh->next;
  2937. + }
  2938. + }
  2939. +
  2940. + /*
  2941. + * For Bulk-only, mark the end of the data with a short
  2942. + * packet. If we are allowed to stall, halt the bulk-in
  2943. + * endpoint. (Note: This violates the Bulk-Only Transport
  2944. + * specification, which requires us to pad the data if we
  2945. + * don't halt the endpoint. Presumably nobody will mind.)
  2946. + */
  2947. + else {
  2948. + bh->inreq->zero = 1;
  2949. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2950. + &bh->inreq_busy, &bh->state);
  2951. + fsg->next_buffhd_to_fill = bh->next;
  2952. + if (mod_data.can_stall)
  2953. + rc = halt_bulk_in_endpoint(fsg);
  2954. + }
  2955. + break;
  2956. +
  2957. + /* We have processed all we want from the data the host has sent.
  2958. + * There may still be outstanding bulk-out requests. */
  2959. + case DATA_DIR_FROM_HOST:
  2960. + if (fsg->residue == 0)
  2961. + ; // Nothing to receive
  2962. +
  2963. + /* Did the host stop sending unexpectedly early? */
  2964. + else if (fsg->short_packet_received) {
  2965. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2966. + rc = -EINTR;
  2967. + }
  2968. +
  2969. + /* We haven't processed all the incoming data. Even though
  2970. + * we may be allowed to stall, doing so would cause a race.
  2971. + * The controller may already have ACK'ed all the remaining
  2972. + * bulk-out packets, in which case the host wouldn't see a
  2973. + * STALL. Not realizing the endpoint was halted, it wouldn't
  2974. + * clear the halt -- leading to problems later on. */
  2975. +#if 0
  2976. + else if (mod_data.can_stall) {
  2977. + fsg_set_halt(fsg, fsg->bulk_out);
  2978. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2979. + rc = -EINTR;
  2980. + }
  2981. +#endif
  2982. +
  2983. + /* We can't stall. Read in the excess data and throw it
  2984. + * all away. */
  2985. + else
  2986. + rc = throw_away_data(fsg);
  2987. + break;
  2988. + }
  2989. + return rc;
  2990. +}
  2991. +
  2992. +
  2993. +static int send_status(struct fsg_dev *fsg)
  2994. +{
  2995. + struct fsg_lun *curlun = fsg->curlun;
  2996. + struct fsg_buffhd *bh;
  2997. + int rc;
  2998. + u8 status = US_BULK_STAT_OK;
  2999. + u32 sd, sdinfo = 0;
  3000. +
  3001. + /* Wait for the next buffer to become available */
  3002. + bh = fsg->next_buffhd_to_fill;
  3003. + while (bh->state != BUF_STATE_EMPTY) {
  3004. + rc = sleep_thread(fsg);
  3005. + if (rc)
  3006. + return rc;
  3007. + }
  3008. +
  3009. + if (curlun) {
  3010. + sd = curlun->sense_data;
  3011. + sdinfo = curlun->sense_data_info;
  3012. + } else if (fsg->bad_lun_okay)
  3013. + sd = SS_NO_SENSE;
  3014. + else
  3015. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  3016. +
  3017. + if (fsg->phase_error) {
  3018. + DBG(fsg, "sending phase-error status\n");
  3019. + status = US_BULK_STAT_PHASE;
  3020. + sd = SS_INVALID_COMMAND;
  3021. + } else if (sd != SS_NO_SENSE) {
  3022. + DBG(fsg, "sending command-failure status\n");
  3023. + status = US_BULK_STAT_FAIL;
  3024. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  3025. + " info x%x\n",
  3026. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  3027. + }
  3028. +
  3029. + if (transport_is_bbb()) {
  3030. + struct bulk_cs_wrap *csw = bh->buf;
  3031. +
  3032. + /* Store and send the Bulk-only CSW */
  3033. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  3034. + csw->Tag = fsg->tag;
  3035. + csw->Residue = cpu_to_le32(fsg->residue);
  3036. + csw->Status = status;
  3037. +
  3038. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  3039. + bh->inreq->zero = 0;
  3040. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  3041. + &bh->inreq_busy, &bh->state);
  3042. +
  3043. + } else if (mod_data.transport_type == USB_PR_CB) {
  3044. +
  3045. + /* Control-Bulk transport has no status phase! */
  3046. + return 0;
  3047. +
  3048. + } else { // USB_PR_CBI
  3049. + struct interrupt_data *buf = bh->buf;
  3050. +
  3051. + /* Store and send the Interrupt data. UFI sends the ASC
  3052. + * and ASCQ bytes. Everything else sends a Type (which
  3053. + * is always 0) and the status Value. */
  3054. + if (mod_data.protocol_type == USB_SC_UFI) {
  3055. + buf->bType = ASC(sd);
  3056. + buf->bValue = ASCQ(sd);
  3057. + } else {
  3058. + buf->bType = 0;
  3059. + buf->bValue = status;
  3060. + }
  3061. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  3062. +
  3063. + fsg->intr_buffhd = bh; // Point to the right buffhd
  3064. + fsg->intreq->buf = bh->inreq->buf;
  3065. + fsg->intreq->context = bh;
  3066. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  3067. + &fsg->intreq_busy, &bh->state);
  3068. + }
  3069. +
  3070. + fsg->next_buffhd_to_fill = bh->next;
  3071. + return 0;
  3072. +}
  3073. +
  3074. +
  3075. +/*-------------------------------------------------------------------------*/
  3076. +
  3077. +/* Check whether the command is properly formed and whether its data size
  3078. + * and direction agree with the values we already have. */
  3079. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  3080. + enum data_direction data_dir, unsigned int mask,
  3081. + int needs_medium, const char *name)
  3082. +{
  3083. + int i;
  3084. + int lun = fsg->cmnd[1] >> 5;
  3085. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  3086. + char hdlen[20];
  3087. + struct fsg_lun *curlun;
  3088. +
  3089. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  3090. + * Transparent SCSI doesn't pad. */
  3091. + if (protocol_is_scsi())
  3092. + ;
  3093. +
  3094. + /* There's some disagreement as to whether RBC pads commands or not.
  3095. + * We'll play it safe and accept either form. */
  3096. + else if (mod_data.protocol_type == USB_SC_RBC) {
  3097. + if (fsg->cmnd_size == 12)
  3098. + cmnd_size = 12;
  3099. +
  3100. + /* All the other protocols pad to 12 bytes */
  3101. + } else
  3102. + cmnd_size = 12;
  3103. +
  3104. + hdlen[0] = 0;
  3105. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  3106. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  3107. + fsg->data_size);
  3108. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  3109. + name, cmnd_size, dirletter[(int) data_dir],
  3110. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  3111. +
  3112. + /* We can't reply at all until we know the correct data direction
  3113. + * and size. */
  3114. + if (fsg->data_size_from_cmnd == 0)
  3115. + data_dir = DATA_DIR_NONE;
  3116. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  3117. + fsg->data_dir = data_dir;
  3118. + fsg->data_size = fsg->data_size_from_cmnd;
  3119. +
  3120. + } else { // Bulk-only
  3121. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  3122. +
  3123. + /* Host data size < Device data size is a phase error.
  3124. + * Carry out the command, but only transfer as much
  3125. + * as we are allowed. */
  3126. + fsg->data_size_from_cmnd = fsg->data_size;
  3127. + fsg->phase_error = 1;
  3128. + }
  3129. + }
  3130. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  3131. +
  3132. + /* Conflicting data directions is a phase error */
  3133. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  3134. + fsg->phase_error = 1;
  3135. + return -EINVAL;
  3136. + }
  3137. +
  3138. + /* Verify the length of the command itself */
  3139. + if (cmnd_size != fsg->cmnd_size) {
  3140. +
  3141. + /* Special case workaround: There are plenty of buggy SCSI
  3142. + * implementations. Many have issues with cbw->Length
  3143. + * field passing a wrong command size. For those cases we
  3144. + * always try to work around the problem by using the length
  3145. + * sent by the host side provided it is at least as large
  3146. + * as the correct command length.
  3147. + * Examples of such cases would be MS-Windows, which issues
  3148. + * REQUEST SENSE with cbw->Length == 12 where it should
  3149. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  3150. + * REQUEST SENSE with cbw->Length == 10 where it should
  3151. + * be 6 as well.
  3152. + */
  3153. + if (cmnd_size <= fsg->cmnd_size) {
  3154. + DBG(fsg, "%s is buggy! Expected length %d "
  3155. + "but we got %d\n", name,
  3156. + cmnd_size, fsg->cmnd_size);
  3157. + cmnd_size = fsg->cmnd_size;
  3158. + } else {
  3159. + fsg->phase_error = 1;
  3160. + return -EINVAL;
  3161. + }
  3162. + }
  3163. +
  3164. + /* Check that the LUN values are consistent */
  3165. + if (transport_is_bbb()) {
  3166. + if (fsg->lun != lun)
  3167. + DBG(fsg, "using LUN %d from CBW, "
  3168. + "not LUN %d from CDB\n",
  3169. + fsg->lun, lun);
  3170. + }
  3171. +
  3172. + /* Check the LUN */
  3173. + curlun = fsg->curlun;
  3174. + if (curlun) {
  3175. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  3176. + curlun->sense_data = SS_NO_SENSE;
  3177. + curlun->sense_data_info = 0;
  3178. + curlun->info_valid = 0;
  3179. + }
  3180. + } else {
  3181. + fsg->bad_lun_okay = 0;
  3182. +
  3183. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  3184. + * to use unsupported LUNs; all others may not. */
  3185. + if (fsg->cmnd[0] != INQUIRY &&
  3186. + fsg->cmnd[0] != REQUEST_SENSE) {
  3187. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  3188. + return -EINVAL;
  3189. + }
  3190. + }
  3191. +
  3192. + /* If a unit attention condition exists, only INQUIRY and
  3193. + * REQUEST SENSE commands are allowed; anything else must fail. */
  3194. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  3195. + fsg->cmnd[0] != INQUIRY &&
  3196. + fsg->cmnd[0] != REQUEST_SENSE) {
  3197. + curlun->sense_data = curlun->unit_attention_data;
  3198. + curlun->unit_attention_data = SS_NO_SENSE;
  3199. + return -EINVAL;
  3200. + }
  3201. +
  3202. + /* Check that only command bytes listed in the mask are non-zero */
  3203. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  3204. + for (i = 1; i < cmnd_size; ++i) {
  3205. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  3206. + if (curlun)
  3207. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  3208. + return -EINVAL;
  3209. + }
  3210. + }
  3211. +
  3212. + /* If the medium isn't mounted and the command needs to access
  3213. + * it, return an error. */
  3214. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  3215. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  3216. + return -EINVAL;
  3217. + }
  3218. +
  3219. + return 0;
  3220. +}
  3221. +
  3222. +/* wrapper of check_command for data size in blocks handling */
  3223. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  3224. + enum data_direction data_dir, unsigned int mask,
  3225. + int needs_medium, const char *name)
  3226. +{
  3227. + if (fsg->curlun)
  3228. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  3229. + return check_command(fsg, cmnd_size, data_dir,
  3230. + mask, needs_medium, name);
  3231. +}
  3232. +
  3233. +static int do_scsi_command(struct fsg_dev *fsg)
  3234. +{
  3235. + struct fsg_buffhd *bh;
  3236. + int rc;
  3237. + int reply = -EINVAL;
  3238. + int i;
  3239. + static char unknown[16];
  3240. +
  3241. + dump_cdb(fsg);
  3242. +
  3243. + /* Wait for the next buffer to become available for data or status */
  3244. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  3245. + while (bh->state != BUF_STATE_EMPTY) {
  3246. + rc = sleep_thread(fsg);
  3247. + if (rc)
  3248. + return rc;
  3249. + }
  3250. + fsg->phase_error = 0;
  3251. + fsg->short_packet_received = 0;
  3252. +
  3253. + down_read(&fsg->filesem); // We're using the backing file
  3254. + switch (fsg->cmnd[0]) {
  3255. +
  3256. + case INQUIRY:
  3257. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3258. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3259. + (1<<4), 0,
  3260. + "INQUIRY")) == 0)
  3261. + reply = do_inquiry(fsg, bh);
  3262. + break;
  3263. +
  3264. + case MODE_SELECT:
  3265. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3266. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  3267. + (1<<1) | (1<<4), 0,
  3268. + "MODE SELECT(6)")) == 0)
  3269. + reply = do_mode_select(fsg, bh);
  3270. + break;
  3271. +
  3272. + case MODE_SELECT_10:
  3273. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3274. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  3275. + (1<<1) | (3<<7), 0,
  3276. + "MODE SELECT(10)")) == 0)
  3277. + reply = do_mode_select(fsg, bh);
  3278. + break;
  3279. +
  3280. + case MODE_SENSE:
  3281. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3282. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3283. + (1<<1) | (1<<2) | (1<<4), 0,
  3284. + "MODE SENSE(6)")) == 0)
  3285. + reply = do_mode_sense(fsg, bh);
  3286. + break;
  3287. +
  3288. + case MODE_SENSE_10:
  3289. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3290. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3291. + (1<<1) | (1<<2) | (3<<7), 0,
  3292. + "MODE SENSE(10)")) == 0)
  3293. + reply = do_mode_sense(fsg, bh);
  3294. + break;
  3295. +
  3296. + case ALLOW_MEDIUM_REMOVAL:
  3297. + fsg->data_size_from_cmnd = 0;
  3298. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  3299. + (1<<4), 0,
  3300. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  3301. + reply = do_prevent_allow(fsg);
  3302. + break;
  3303. +
  3304. + case READ_6:
  3305. + i = fsg->cmnd[4];
  3306. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  3307. + if ((reply = check_command_size_in_blocks(fsg, 6,
  3308. + DATA_DIR_TO_HOST,
  3309. + (7<<1) | (1<<4), 1,
  3310. + "READ(6)")) == 0)
  3311. + reply = do_read(fsg);
  3312. + break;
  3313. +
  3314. + case READ_10:
  3315. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3316. + if ((reply = check_command_size_in_blocks(fsg, 10,
  3317. + DATA_DIR_TO_HOST,
  3318. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3319. + "READ(10)")) == 0)
  3320. + reply = do_read(fsg);
  3321. + break;
  3322. +
  3323. + case READ_12:
  3324. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  3325. + if ((reply = check_command_size_in_blocks(fsg, 12,
  3326. + DATA_DIR_TO_HOST,
  3327. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  3328. + "READ(12)")) == 0)
  3329. + reply = do_read(fsg);
  3330. + break;
  3331. +
  3332. + case READ_CAPACITY:
  3333. + fsg->data_size_from_cmnd = 8;
  3334. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3335. + (0xf<<2) | (1<<8), 1,
  3336. + "READ CAPACITY")) == 0)
  3337. + reply = do_read_capacity(fsg, bh);
  3338. + break;
  3339. +
  3340. + case READ_HEADER:
  3341. + if (!mod_data.cdrom)
  3342. + goto unknown_cmnd;
  3343. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3344. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3345. + (3<<7) | (0x1f<<1), 1,
  3346. + "READ HEADER")) == 0)
  3347. + reply = do_read_header(fsg, bh);
  3348. + break;
  3349. +
  3350. + case READ_TOC:
  3351. + if (!mod_data.cdrom)
  3352. + goto unknown_cmnd;
  3353. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3354. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3355. + (7<<6) | (1<<1), 1,
  3356. + "READ TOC")) == 0)
  3357. + reply = do_read_toc(fsg, bh);
  3358. + break;
  3359. +
  3360. + case READ_FORMAT_CAPACITIES:
  3361. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3362. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3363. + (3<<7), 1,
  3364. + "READ FORMAT CAPACITIES")) == 0)
  3365. + reply = do_read_format_capacities(fsg, bh);
  3366. + break;
  3367. +
  3368. + case REQUEST_SENSE:
  3369. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3370. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3371. + (1<<4), 0,
  3372. + "REQUEST SENSE")) == 0)
  3373. + reply = do_request_sense(fsg, bh);
  3374. + break;
  3375. +
  3376. + case START_STOP:
  3377. + fsg->data_size_from_cmnd = 0;
  3378. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  3379. + (1<<1) | (1<<4), 0,
  3380. + "START-STOP UNIT")) == 0)
  3381. + reply = do_start_stop(fsg);
  3382. + break;
  3383. +
  3384. + case SYNCHRONIZE_CACHE:
  3385. + fsg->data_size_from_cmnd = 0;
  3386. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  3387. + (0xf<<2) | (3<<7), 1,
  3388. + "SYNCHRONIZE CACHE")) == 0)
  3389. + reply = do_synchronize_cache(fsg);
  3390. + break;
  3391. +
  3392. + case TEST_UNIT_READY:
  3393. + fsg->data_size_from_cmnd = 0;
  3394. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  3395. + 0, 1,
  3396. + "TEST UNIT READY");
  3397. + break;
  3398. +
  3399. + /* Although optional, this command is used by MS-Windows. We
  3400. + * support a minimal version: BytChk must be 0. */
  3401. + case VERIFY:
  3402. + fsg->data_size_from_cmnd = 0;
  3403. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  3404. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3405. + "VERIFY")) == 0)
  3406. + reply = do_verify(fsg);
  3407. + break;
  3408. +
  3409. + case WRITE_6:
  3410. + i = fsg->cmnd[4];
  3411. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  3412. + if ((reply = check_command_size_in_blocks(fsg, 6,
  3413. + DATA_DIR_FROM_HOST,
  3414. + (7<<1) | (1<<4), 1,
  3415. + "WRITE(6)")) == 0)
  3416. + reply = do_write(fsg);
  3417. + break;
  3418. +
  3419. + case WRITE_10:
  3420. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3421. + if ((reply = check_command_size_in_blocks(fsg, 10,
  3422. + DATA_DIR_FROM_HOST,
  3423. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3424. + "WRITE(10)")) == 0)
  3425. + reply = do_write(fsg);
  3426. + break;
  3427. +
  3428. + case WRITE_12:
  3429. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  3430. + if ((reply = check_command_size_in_blocks(fsg, 12,
  3431. + DATA_DIR_FROM_HOST,
  3432. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  3433. + "WRITE(12)")) == 0)
  3434. + reply = do_write(fsg);
  3435. + break;
  3436. +
  3437. + /* Some mandatory commands that we recognize but don't implement.
  3438. + * They don't mean much in this setting. It's left as an exercise
  3439. + * for anyone interested to implement RESERVE and RELEASE in terms
  3440. + * of Posix locks. */
  3441. + case FORMAT_UNIT:
  3442. + case RELEASE:
  3443. + case RESERVE:
  3444. + case SEND_DIAGNOSTIC:
  3445. + // Fall through
  3446. +
  3447. + default:
  3448. + unknown_cmnd:
  3449. + fsg->data_size_from_cmnd = 0;
  3450. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  3451. + if ((reply = check_command(fsg, fsg->cmnd_size,
  3452. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  3453. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  3454. + reply = -EINVAL;
  3455. + }
  3456. + break;
  3457. + }
  3458. + up_read(&fsg->filesem);
  3459. +
  3460. + if (reply == -EINTR || signal_pending(current))
  3461. + return -EINTR;
  3462. +
  3463. + /* Set up the single reply buffer for finish_reply() */
  3464. + if (reply == -EINVAL)
  3465. + reply = 0; // Error reply length
  3466. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  3467. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  3468. + bh->inreq->length = reply;
  3469. + bh->state = BUF_STATE_FULL;
  3470. + fsg->residue -= reply;
  3471. + } // Otherwise it's already set
  3472. +
  3473. + return 0;
  3474. +}
  3475. +
  3476. +
  3477. +/*-------------------------------------------------------------------------*/
  3478. +
  3479. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  3480. +{
  3481. + struct usb_request *req = bh->outreq;
  3482. + struct bulk_cb_wrap *cbw = req->buf;
  3483. +
  3484. + /* Was this a real packet? Should it be ignored? */
  3485. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  3486. + return -EINVAL;
  3487. +
  3488. + /* Is the CBW valid? */
  3489. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  3490. + cbw->Signature != cpu_to_le32(
  3491. + US_BULK_CB_SIGN)) {
  3492. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  3493. + req->actual,
  3494. + le32_to_cpu(cbw->Signature));
  3495. +
  3496. + /* The Bulk-only spec says we MUST stall the IN endpoint
  3497. + * (6.6.1), so it's unavoidable. It also says we must
  3498. + * retain this state until the next reset, but there's
  3499. + * no way to tell the controller driver it should ignore
  3500. + * Clear-Feature(HALT) requests.
  3501. + *
  3502. + * We aren't required to halt the OUT endpoint; instead
  3503. + * we can simply accept and discard any data received
  3504. + * until the next reset. */
  3505. + wedge_bulk_in_endpoint(fsg);
  3506. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  3507. + return -EINVAL;
  3508. + }
  3509. +
  3510. + /* Is the CBW meaningful? */
  3511. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  3512. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  3513. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  3514. + "cmdlen %u\n",
  3515. + cbw->Lun, cbw->Flags, cbw->Length);
  3516. +
  3517. + /* We can do anything we want here, so let's stall the
  3518. + * bulk pipes if we are allowed to. */
  3519. + if (mod_data.can_stall) {
  3520. + fsg_set_halt(fsg, fsg->bulk_out);
  3521. + halt_bulk_in_endpoint(fsg);
  3522. + }
  3523. + return -EINVAL;
  3524. + }
  3525. +
  3526. + /* Save the command for later */
  3527. + fsg->cmnd_size = cbw->Length;
  3528. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  3529. + if (cbw->Flags & US_BULK_FLAG_IN)
  3530. + fsg->data_dir = DATA_DIR_TO_HOST;
  3531. + else
  3532. + fsg->data_dir = DATA_DIR_FROM_HOST;
  3533. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  3534. + if (fsg->data_size == 0)
  3535. + fsg->data_dir = DATA_DIR_NONE;
  3536. + fsg->lun = cbw->Lun;
  3537. + fsg->tag = cbw->Tag;
  3538. + return 0;
  3539. +}
  3540. +
  3541. +
  3542. +static int get_next_command(struct fsg_dev *fsg)
  3543. +{
  3544. + struct fsg_buffhd *bh;
  3545. + int rc = 0;
  3546. +
  3547. + if (transport_is_bbb()) {
  3548. +
  3549. + /* Wait for the next buffer to become available */
  3550. + bh = fsg->next_buffhd_to_fill;
  3551. + while (bh->state != BUF_STATE_EMPTY) {
  3552. + rc = sleep_thread(fsg);
  3553. + if (rc)
  3554. + return rc;
  3555. + }
  3556. +
  3557. + /* Queue a request to read a Bulk-only CBW */
  3558. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  3559. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  3560. + &bh->outreq_busy, &bh->state);
  3561. +
  3562. + /* We will drain the buffer in software, which means we
  3563. + * can reuse it for the next filling. No need to advance
  3564. + * next_buffhd_to_fill. */
  3565. +
  3566. + /* Wait for the CBW to arrive */
  3567. + while (bh->state != BUF_STATE_FULL) {
  3568. + rc = sleep_thread(fsg);
  3569. + if (rc)
  3570. + return rc;
  3571. + }
  3572. + smp_rmb();
  3573. + rc = received_cbw(fsg, bh);
  3574. + bh->state = BUF_STATE_EMPTY;
  3575. +
  3576. + } else { // USB_PR_CB or USB_PR_CBI
  3577. +
  3578. + /* Wait for the next command to arrive */
  3579. + while (fsg->cbbuf_cmnd_size == 0) {
  3580. + rc = sleep_thread(fsg);
  3581. + if (rc)
  3582. + return rc;
  3583. + }
  3584. +
  3585. + /* Is the previous status interrupt request still busy?
  3586. + * The host is allowed to skip reading the status,
  3587. + * so we must cancel it. */
  3588. + if (fsg->intreq_busy)
  3589. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  3590. +
  3591. + /* Copy the command and mark the buffer empty */
  3592. + fsg->data_dir = DATA_DIR_UNKNOWN;
  3593. + spin_lock_irq(&fsg->lock);
  3594. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  3595. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  3596. + fsg->cbbuf_cmnd_size = 0;
  3597. + spin_unlock_irq(&fsg->lock);
  3598. +
  3599. + /* Use LUN from the command */
  3600. + fsg->lun = fsg->cmnd[1] >> 5;
  3601. + }
  3602. +
  3603. + /* Update current lun */
  3604. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  3605. + fsg->curlun = &fsg->luns[fsg->lun];
  3606. + else
  3607. + fsg->curlun = NULL;
  3608. +
  3609. + return rc;
  3610. +}
  3611. +
  3612. +
  3613. +/*-------------------------------------------------------------------------*/
  3614. +
  3615. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  3616. + const struct usb_endpoint_descriptor *d)
  3617. +{
  3618. + int rc;
  3619. +
  3620. + ep->driver_data = fsg;
  3621. + ep->desc = d;
  3622. + rc = usb_ep_enable(ep);
  3623. + if (rc)
  3624. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  3625. + return rc;
  3626. +}
  3627. +
  3628. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  3629. + struct usb_request **preq)
  3630. +{
  3631. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  3632. + if (*preq)
  3633. + return 0;
  3634. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  3635. + return -ENOMEM;
  3636. +}
  3637. +
  3638. +/*
  3639. + * Reset interface setting and re-init endpoint state (toggle etc).
  3640. + * Call with altsetting < 0 to disable the interface. The only other
  3641. + * available altsetting is 0, which enables the interface.
  3642. + */
  3643. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  3644. +{
  3645. + int rc = 0;
  3646. + int i;
  3647. + const struct usb_endpoint_descriptor *d;
  3648. +
  3649. + if (fsg->running)
  3650. + DBG(fsg, "reset interface\n");
  3651. +
  3652. +reset:
  3653. + /* Deallocate the requests */
  3654. + for (i = 0; i < fsg_num_buffers; ++i) {
  3655. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  3656. +
  3657. + if (bh->inreq) {
  3658. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  3659. + bh->inreq = NULL;
  3660. + }
  3661. + if (bh->outreq) {
  3662. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  3663. + bh->outreq = NULL;
  3664. + }
  3665. + }
  3666. + if (fsg->intreq) {
  3667. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  3668. + fsg->intreq = NULL;
  3669. + }
  3670. +
  3671. + /* Disable the endpoints */
  3672. + if (fsg->bulk_in_enabled) {
  3673. + usb_ep_disable(fsg->bulk_in);
  3674. + fsg->bulk_in_enabled = 0;
  3675. + }
  3676. + if (fsg->bulk_out_enabled) {
  3677. + usb_ep_disable(fsg->bulk_out);
  3678. + fsg->bulk_out_enabled = 0;
  3679. + }
  3680. + if (fsg->intr_in_enabled) {
  3681. + usb_ep_disable(fsg->intr_in);
  3682. + fsg->intr_in_enabled = 0;
  3683. + }
  3684. +
  3685. + fsg->running = 0;
  3686. + if (altsetting < 0 || rc != 0)
  3687. + return rc;
  3688. +
  3689. + DBG(fsg, "set interface %d\n", altsetting);
  3690. +
  3691. + /* Enable the endpoints */
  3692. + d = fsg_ep_desc(fsg->gadget,
  3693. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  3694. + &fsg_ss_bulk_in_desc);
  3695. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  3696. + goto reset;
  3697. + fsg->bulk_in_enabled = 1;
  3698. +
  3699. + d = fsg_ep_desc(fsg->gadget,
  3700. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  3701. + &fsg_ss_bulk_out_desc);
  3702. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  3703. + goto reset;
  3704. + fsg->bulk_out_enabled = 1;
  3705. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  3706. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  3707. +
  3708. + if (transport_is_cbi()) {
  3709. + d = fsg_ep_desc(fsg->gadget,
  3710. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  3711. + &fsg_ss_intr_in_desc);
  3712. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  3713. + goto reset;
  3714. + fsg->intr_in_enabled = 1;
  3715. + }
  3716. +
  3717. + /* Allocate the requests */
  3718. + for (i = 0; i < fsg_num_buffers; ++i) {
  3719. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  3720. +
  3721. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  3722. + goto reset;
  3723. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  3724. + goto reset;
  3725. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  3726. + bh->inreq->context = bh->outreq->context = bh;
  3727. + bh->inreq->complete = bulk_in_complete;
  3728. + bh->outreq->complete = bulk_out_complete;
  3729. + }
  3730. + if (transport_is_cbi()) {
  3731. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  3732. + goto reset;
  3733. + fsg->intreq->complete = intr_in_complete;
  3734. + }
  3735. +
  3736. + fsg->running = 1;
  3737. + for (i = 0; i < fsg->nluns; ++i)
  3738. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  3739. + return rc;
  3740. +}
  3741. +
  3742. +
  3743. +/*
  3744. + * Change our operational configuration. This code must agree with the code
  3745. + * that returns config descriptors, and with interface altsetting code.
  3746. + *
  3747. + * It's also responsible for power management interactions. Some
  3748. + * configurations might not work with our current power sources.
  3749. + * For now we just assume the gadget is always self-powered.
  3750. + */
  3751. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  3752. +{
  3753. + int rc = 0;
  3754. +
  3755. + /* Disable the single interface */
  3756. + if (fsg->config != 0) {
  3757. + DBG(fsg, "reset config\n");
  3758. + fsg->config = 0;
  3759. + rc = do_set_interface(fsg, -1);
  3760. + }
  3761. +
  3762. + /* Enable the interface */
  3763. + if (new_config != 0) {
  3764. + fsg->config = new_config;
  3765. + if ((rc = do_set_interface(fsg, 0)) != 0)
  3766. + fsg->config = 0; // Reset on errors
  3767. + else
  3768. + INFO(fsg, "%s config #%d\n",
  3769. + usb_speed_string(fsg->gadget->speed),
  3770. + fsg->config);
  3771. + }
  3772. + return rc;
  3773. +}
  3774. +
  3775. +
  3776. +/*-------------------------------------------------------------------------*/
  3777. +
  3778. +static void handle_exception(struct fsg_dev *fsg)
  3779. +{
  3780. + siginfo_t info;
  3781. + int sig;
  3782. + int i;
  3783. + int num_active;
  3784. + struct fsg_buffhd *bh;
  3785. + enum fsg_state old_state;
  3786. + u8 new_config;
  3787. + struct fsg_lun *curlun;
  3788. + unsigned int exception_req_tag;
  3789. + int rc;
  3790. +
  3791. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  3792. + * into a high-priority EXIT exception. */
  3793. + for (;;) {
  3794. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  3795. + if (!sig)
  3796. + break;
  3797. + if (sig != SIGUSR1) {
  3798. + if (fsg->state < FSG_STATE_EXIT)
  3799. + DBG(fsg, "Main thread exiting on signal\n");
  3800. + raise_exception(fsg, FSG_STATE_EXIT);
  3801. + }
  3802. + }
  3803. +
  3804. + /* Cancel all the pending transfers */
  3805. + if (fsg->intreq_busy)
  3806. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  3807. + for (i = 0; i < fsg_num_buffers; ++i) {
  3808. + bh = &fsg->buffhds[i];
  3809. + if (bh->inreq_busy)
  3810. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  3811. + if (bh->outreq_busy)
  3812. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  3813. + }
  3814. +
  3815. + /* Wait until everything is idle */
  3816. + for (;;) {
  3817. + num_active = fsg->intreq_busy;
  3818. + for (i = 0; i < fsg_num_buffers; ++i) {
  3819. + bh = &fsg->buffhds[i];
  3820. + num_active += bh->inreq_busy + bh->outreq_busy;
  3821. + }
  3822. + if (num_active == 0)
  3823. + break;
  3824. + if (sleep_thread(fsg))
  3825. + return;
  3826. + }
  3827. +
  3828. + /* Clear out the controller's fifos */
  3829. + if (fsg->bulk_in_enabled)
  3830. + usb_ep_fifo_flush(fsg->bulk_in);
  3831. + if (fsg->bulk_out_enabled)
  3832. + usb_ep_fifo_flush(fsg->bulk_out);
  3833. + if (fsg->intr_in_enabled)
  3834. + usb_ep_fifo_flush(fsg->intr_in);
  3835. +
  3836. + /* Reset the I/O buffer states and pointers, the SCSI
  3837. + * state, and the exception. Then invoke the handler. */
  3838. + spin_lock_irq(&fsg->lock);
  3839. +
  3840. + for (i = 0; i < fsg_num_buffers; ++i) {
  3841. + bh = &fsg->buffhds[i];
  3842. + bh->state = BUF_STATE_EMPTY;
  3843. + }
  3844. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  3845. + &fsg->buffhds[0];
  3846. +
  3847. + exception_req_tag = fsg->exception_req_tag;
  3848. + new_config = fsg->new_config;
  3849. + old_state = fsg->state;
  3850. +
  3851. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  3852. + fsg->state = FSG_STATE_STATUS_PHASE;
  3853. + else {
  3854. + for (i = 0; i < fsg->nluns; ++i) {
  3855. + curlun = &fsg->luns[i];
  3856. + curlun->prevent_medium_removal = 0;
  3857. + curlun->sense_data = curlun->unit_attention_data =
  3858. + SS_NO_SENSE;
  3859. + curlun->sense_data_info = 0;
  3860. + curlun->info_valid = 0;
  3861. + }
  3862. + fsg->state = FSG_STATE_IDLE;
  3863. + }
  3864. + spin_unlock_irq(&fsg->lock);
  3865. +
  3866. + /* Carry out any extra actions required for the exception */
  3867. + switch (old_state) {
  3868. + default:
  3869. + break;
  3870. +
  3871. + case FSG_STATE_ABORT_BULK_OUT:
  3872. + send_status(fsg);
  3873. + spin_lock_irq(&fsg->lock);
  3874. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  3875. + fsg->state = FSG_STATE_IDLE;
  3876. + spin_unlock_irq(&fsg->lock);
  3877. + break;
  3878. +
  3879. + case FSG_STATE_RESET:
  3880. + /* In case we were forced against our will to halt a
  3881. + * bulk endpoint, clear the halt now. (The SuperH UDC
  3882. + * requires this.) */
  3883. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  3884. + usb_ep_clear_halt(fsg->bulk_in);
  3885. +
  3886. + if (transport_is_bbb()) {
  3887. + if (fsg->ep0_req_tag == exception_req_tag)
  3888. + ep0_queue(fsg); // Complete the status stage
  3889. +
  3890. + } else if (transport_is_cbi())
  3891. + send_status(fsg); // Status by interrupt pipe
  3892. +
  3893. + /* Technically this should go here, but it would only be
  3894. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  3895. + * CONFIG_CHANGE cases. */
  3896. + // for (i = 0; i < fsg->nluns; ++i)
  3897. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  3898. + break;
  3899. +
  3900. + case FSG_STATE_INTERFACE_CHANGE:
  3901. + rc = do_set_interface(fsg, 0);
  3902. + if (fsg->ep0_req_tag != exception_req_tag)
  3903. + break;
  3904. + if (rc != 0) // STALL on errors
  3905. + fsg_set_halt(fsg, fsg->ep0);
  3906. + else // Complete the status stage
  3907. + ep0_queue(fsg);
  3908. + break;
  3909. +
  3910. + case FSG_STATE_CONFIG_CHANGE:
  3911. + rc = do_set_config(fsg, new_config);
  3912. + if (fsg->ep0_req_tag != exception_req_tag)
  3913. + break;
  3914. + if (rc != 0) // STALL on errors
  3915. + fsg_set_halt(fsg, fsg->ep0);
  3916. + else // Complete the status stage
  3917. + ep0_queue(fsg);
  3918. + break;
  3919. +
  3920. + case FSG_STATE_DISCONNECT:
  3921. + for (i = 0; i < fsg->nluns; ++i)
  3922. + fsg_lun_fsync_sub(fsg->luns + i);
  3923. + do_set_config(fsg, 0); // Unconfigured state
  3924. + break;
  3925. +
  3926. + case FSG_STATE_EXIT:
  3927. + case FSG_STATE_TERMINATED:
  3928. + do_set_config(fsg, 0); // Free resources
  3929. + spin_lock_irq(&fsg->lock);
  3930. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  3931. + spin_unlock_irq(&fsg->lock);
  3932. + break;
  3933. + }
  3934. +}
  3935. +
  3936. +
  3937. +/*-------------------------------------------------------------------------*/
  3938. +
  3939. +static int fsg_main_thread(void *fsg_)
  3940. +{
  3941. + struct fsg_dev *fsg = fsg_;
  3942. +
  3943. + /* Allow the thread to be killed by a signal, but set the signal mask
  3944. + * to block everything but INT, TERM, KILL, and USR1. */
  3945. + allow_signal(SIGINT);
  3946. + allow_signal(SIGTERM);
  3947. + allow_signal(SIGKILL);
  3948. + allow_signal(SIGUSR1);
  3949. +
  3950. + /* Allow the thread to be frozen */
  3951. + set_freezable();
  3952. +
  3953. + /* Arrange for userspace references to be interpreted as kernel
  3954. + * pointers. That way we can pass a kernel pointer to a routine
  3955. + * that expects a __user pointer and it will work okay. */
  3956. + set_fs(get_ds());
  3957. +
  3958. + /* The main loop */
  3959. + while (fsg->state != FSG_STATE_TERMINATED) {
  3960. + if (exception_in_progress(fsg) || signal_pending(current)) {
  3961. + handle_exception(fsg);
  3962. + continue;
  3963. + }
  3964. +
  3965. + if (!fsg->running) {
  3966. + sleep_thread(fsg);
  3967. + continue;
  3968. + }
  3969. +
  3970. + if (get_next_command(fsg))
  3971. + continue;
  3972. +
  3973. + spin_lock_irq(&fsg->lock);
  3974. + if (!exception_in_progress(fsg))
  3975. + fsg->state = FSG_STATE_DATA_PHASE;
  3976. + spin_unlock_irq(&fsg->lock);
  3977. +
  3978. + if (do_scsi_command(fsg) || finish_reply(fsg))
  3979. + continue;
  3980. +
  3981. + spin_lock_irq(&fsg->lock);
  3982. + if (!exception_in_progress(fsg))
  3983. + fsg->state = FSG_STATE_STATUS_PHASE;
  3984. + spin_unlock_irq(&fsg->lock);
  3985. +
  3986. + if (send_status(fsg))
  3987. + continue;
  3988. +
  3989. + spin_lock_irq(&fsg->lock);
  3990. + if (!exception_in_progress(fsg))
  3991. + fsg->state = FSG_STATE_IDLE;
  3992. + spin_unlock_irq(&fsg->lock);
  3993. + }
  3994. +
  3995. + spin_lock_irq(&fsg->lock);
  3996. + fsg->thread_task = NULL;
  3997. + spin_unlock_irq(&fsg->lock);
  3998. +
  3999. + /* If we are exiting because of a signal, unregister the
  4000. + * gadget driver. */
  4001. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  4002. + usb_gadget_unregister_driver(&fsg_driver);
  4003. +
  4004. + /* Let the unbind and cleanup routines know the thread has exited */
  4005. + complete_and_exit(&fsg->thread_notifier, 0);
  4006. +}
  4007. +
  4008. +
  4009. +/*-------------------------------------------------------------------------*/
  4010. +
  4011. +
  4012. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  4013. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  4014. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  4015. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  4016. +
  4017. +
  4018. +/*-------------------------------------------------------------------------*/
  4019. +
  4020. +static void fsg_release(struct kref *ref)
  4021. +{
  4022. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  4023. +
  4024. + kfree(fsg->luns);
  4025. + kfree(fsg);
  4026. +}
  4027. +
  4028. +static void lun_release(struct device *dev)
  4029. +{
  4030. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  4031. + struct fsg_dev *fsg =
  4032. + container_of(filesem, struct fsg_dev, filesem);
  4033. +
  4034. + kref_put(&fsg->ref, fsg_release);
  4035. +}
  4036. +
  4037. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  4038. +{
  4039. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4040. + int i;
  4041. + struct fsg_lun *curlun;
  4042. + struct usb_request *req = fsg->ep0req;
  4043. +
  4044. + DBG(fsg, "unbind\n");
  4045. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  4046. +
  4047. + /* If the thread isn't already dead, tell it to exit now */
  4048. + if (fsg->state != FSG_STATE_TERMINATED) {
  4049. + raise_exception(fsg, FSG_STATE_EXIT);
  4050. + wait_for_completion(&fsg->thread_notifier);
  4051. +
  4052. + /* The cleanup routine waits for this completion also */
  4053. + complete(&fsg->thread_notifier);
  4054. + }
  4055. +
  4056. + /* Unregister the sysfs attribute files and the LUNs */
  4057. + for (i = 0; i < fsg->nluns; ++i) {
  4058. + curlun = &fsg->luns[i];
  4059. + if (curlun->registered) {
  4060. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  4061. + device_remove_file(&curlun->dev, &dev_attr_ro);
  4062. + device_remove_file(&curlun->dev, &dev_attr_file);
  4063. + fsg_lun_close(curlun);
  4064. + device_unregister(&curlun->dev);
  4065. + curlun->registered = 0;
  4066. + }
  4067. + }
  4068. +
  4069. + /* Free the data buffers */
  4070. + for (i = 0; i < fsg_num_buffers; ++i)
  4071. + kfree(fsg->buffhds[i].buf);
  4072. +
  4073. + /* Free the request and buffer for endpoint 0 */
  4074. + if (req) {
  4075. + kfree(req->buf);
  4076. + usb_ep_free_request(fsg->ep0, req);
  4077. + }
  4078. +
  4079. + set_gadget_data(gadget, NULL);
  4080. +}
  4081. +
  4082. +
  4083. +static int __init check_parameters(struct fsg_dev *fsg)
  4084. +{
  4085. + int prot;
  4086. + int gcnum;
  4087. +
  4088. + /* Store the default values */
  4089. + mod_data.transport_type = USB_PR_BULK;
  4090. + mod_data.transport_name = "Bulk-only";
  4091. + mod_data.protocol_type = USB_SC_SCSI;
  4092. + mod_data.protocol_name = "Transparent SCSI";
  4093. +
  4094. + /* Some peripheral controllers are known not to be able to
  4095. + * halt bulk endpoints correctly. If one of them is present,
  4096. + * disable stalls.
  4097. + */
  4098. + if (gadget_is_at91(fsg->gadget))
  4099. + mod_data.can_stall = 0;
  4100. +
  4101. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  4102. + gcnum = usb_gadget_controller_number(fsg->gadget);
  4103. + if (gcnum >= 0)
  4104. + mod_data.release = 0x0300 + gcnum;
  4105. + else {
  4106. + WARNING(fsg, "controller '%s' not recognized\n",
  4107. + fsg->gadget->name);
  4108. + mod_data.release = 0x0399;
  4109. + }
  4110. + }
  4111. +
  4112. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  4113. +
  4114. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  4115. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  4116. + ; // Use default setting
  4117. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  4118. + mod_data.transport_type = USB_PR_CB;
  4119. + mod_data.transport_name = "Control-Bulk";
  4120. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  4121. + mod_data.transport_type = USB_PR_CBI;
  4122. + mod_data.transport_name = "Control-Bulk-Interrupt";
  4123. + } else {
  4124. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  4125. + return -EINVAL;
  4126. + }
  4127. +
  4128. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  4129. + prot == USB_SC_SCSI) {
  4130. + ; // Use default setting
  4131. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  4132. + prot == USB_SC_RBC) {
  4133. + mod_data.protocol_type = USB_SC_RBC;
  4134. + mod_data.protocol_name = "RBC";
  4135. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  4136. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  4137. + prot == USB_SC_8020) {
  4138. + mod_data.protocol_type = USB_SC_8020;
  4139. + mod_data.protocol_name = "8020i (ATAPI)";
  4140. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  4141. + prot == USB_SC_QIC) {
  4142. + mod_data.protocol_type = USB_SC_QIC;
  4143. + mod_data.protocol_name = "QIC-157";
  4144. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  4145. + prot == USB_SC_UFI) {
  4146. + mod_data.protocol_type = USB_SC_UFI;
  4147. + mod_data.protocol_name = "UFI";
  4148. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  4149. + prot == USB_SC_8070) {
  4150. + mod_data.protocol_type = USB_SC_8070;
  4151. + mod_data.protocol_name = "8070i";
  4152. + } else {
  4153. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  4154. + return -EINVAL;
  4155. + }
  4156. +
  4157. + mod_data.buflen &= PAGE_CACHE_MASK;
  4158. + if (mod_data.buflen <= 0) {
  4159. + ERROR(fsg, "invalid buflen\n");
  4160. + return -ETOOSMALL;
  4161. + }
  4162. +
  4163. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  4164. +
  4165. + /* Serial string handling.
  4166. + * On a real device, the serial string would be loaded
  4167. + * from permanent storage. */
  4168. + if (mod_data.serial) {
  4169. + const char *ch;
  4170. + unsigned len = 0;
  4171. +
  4172. + /* Sanity check :
  4173. + * The CB[I] specification limits the serial string to
  4174. + * 12 uppercase hexadecimal characters.
  4175. + * BBB need at least 12 uppercase hexadecimal characters,
  4176. + * with a maximum of 126. */
  4177. + for (ch = mod_data.serial; *ch; ++ch) {
  4178. + ++len;
  4179. + if ((*ch < '0' || *ch > '9') &&
  4180. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  4181. + WARNING(fsg,
  4182. + "Invalid serial string character: %c\n",
  4183. + *ch);
  4184. + goto no_serial;
  4185. + }
  4186. + }
  4187. + if (len > 126 ||
  4188. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  4189. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  4190. + WARNING(fsg, "Invalid serial string length!\n");
  4191. + goto no_serial;
  4192. + }
  4193. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  4194. + } else {
  4195. + WARNING(fsg, "No serial-number string provided!\n");
  4196. + no_serial:
  4197. + device_desc.iSerialNumber = 0;
  4198. + }
  4199. +
  4200. + return 0;
  4201. +}
  4202. +
  4203. +
  4204. +static int __init fsg_bind(struct usb_gadget *gadget)
  4205. +{
  4206. + struct fsg_dev *fsg = the_fsg;
  4207. + int rc;
  4208. + int i;
  4209. + struct fsg_lun *curlun;
  4210. + struct usb_ep *ep;
  4211. + struct usb_request *req;
  4212. + char *pathbuf, *p;
  4213. +
  4214. + fsg->gadget = gadget;
  4215. + set_gadget_data(gadget, fsg);
  4216. + fsg->ep0 = gadget->ep0;
  4217. + fsg->ep0->driver_data = fsg;
  4218. +
  4219. + if ((rc = check_parameters(fsg)) != 0)
  4220. + goto out;
  4221. +
  4222. + if (mod_data.removable) { // Enable the store_xxx attributes
  4223. + dev_attr_file.attr.mode = 0644;
  4224. + dev_attr_file.store = fsg_store_file;
  4225. + if (!mod_data.cdrom) {
  4226. + dev_attr_ro.attr.mode = 0644;
  4227. + dev_attr_ro.store = fsg_store_ro;
  4228. + }
  4229. + }
  4230. +
  4231. + /* Only for removable media? */
  4232. + dev_attr_nofua.attr.mode = 0644;
  4233. + dev_attr_nofua.store = fsg_store_nofua;
  4234. +
  4235. + /* Find out how many LUNs there should be */
  4236. + i = mod_data.nluns;
  4237. + if (i == 0)
  4238. + i = max(mod_data.num_filenames, 1u);
  4239. + if (i > FSG_MAX_LUNS) {
  4240. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  4241. + rc = -EINVAL;
  4242. + goto out;
  4243. + }
  4244. +
  4245. + /* Create the LUNs, open their backing files, and register the
  4246. + * LUN devices in sysfs. */
  4247. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  4248. + if (!fsg->luns) {
  4249. + rc = -ENOMEM;
  4250. + goto out;
  4251. + }
  4252. + fsg->nluns = i;
  4253. +
  4254. + for (i = 0; i < fsg->nluns; ++i) {
  4255. + curlun = &fsg->luns[i];
  4256. + curlun->cdrom = !!mod_data.cdrom;
  4257. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  4258. + curlun->initially_ro = curlun->ro;
  4259. + curlun->removable = mod_data.removable;
  4260. + curlun->nofua = mod_data.nofua[i];
  4261. + curlun->dev.release = lun_release;
  4262. + curlun->dev.parent = &gadget->dev;
  4263. + curlun->dev.driver = &fsg_driver.driver;
  4264. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  4265. + dev_set_name(&curlun->dev,"%s-lun%d",
  4266. + dev_name(&gadget->dev), i);
  4267. +
  4268. + kref_get(&fsg->ref);
  4269. + rc = device_register(&curlun->dev);
  4270. + if (rc) {
  4271. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  4272. + put_device(&curlun->dev);
  4273. + goto out;
  4274. + }
  4275. + curlun->registered = 1;
  4276. +
  4277. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  4278. + if (rc)
  4279. + goto out;
  4280. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  4281. + if (rc)
  4282. + goto out;
  4283. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  4284. + if (rc)
  4285. + goto out;
  4286. +
  4287. + if (mod_data.file[i] && *mod_data.file[i]) {
  4288. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  4289. + if (rc)
  4290. + goto out;
  4291. + } else if (!mod_data.removable) {
  4292. + ERROR(fsg, "no file given for LUN%d\n", i);
  4293. + rc = -EINVAL;
  4294. + goto out;
  4295. + }
  4296. + }
  4297. +
  4298. + /* Find all the endpoints we will use */
  4299. + usb_ep_autoconfig_reset(gadget);
  4300. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  4301. + if (!ep)
  4302. + goto autoconf_fail;
  4303. + ep->driver_data = fsg; // claim the endpoint
  4304. + fsg->bulk_in = ep;
  4305. +
  4306. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  4307. + if (!ep)
  4308. + goto autoconf_fail;
  4309. + ep->driver_data = fsg; // claim the endpoint
  4310. + fsg->bulk_out = ep;
  4311. +
  4312. + if (transport_is_cbi()) {
  4313. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  4314. + if (!ep)
  4315. + goto autoconf_fail;
  4316. + ep->driver_data = fsg; // claim the endpoint
  4317. + fsg->intr_in = ep;
  4318. + }
  4319. +
  4320. + /* Fix up the descriptors */
  4321. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  4322. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  4323. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  4324. +
  4325. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  4326. + fsg_intf_desc.bNumEndpoints = i;
  4327. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  4328. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  4329. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4330. +
  4331. + if (gadget_is_dualspeed(gadget)) {
  4332. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4333. +
  4334. + /* Assume endpoint addresses are the same for both speeds */
  4335. + fsg_hs_bulk_in_desc.bEndpointAddress =
  4336. + fsg_fs_bulk_in_desc.bEndpointAddress;
  4337. + fsg_hs_bulk_out_desc.bEndpointAddress =
  4338. + fsg_fs_bulk_out_desc.bEndpointAddress;
  4339. + fsg_hs_intr_in_desc.bEndpointAddress =
  4340. + fsg_fs_intr_in_desc.bEndpointAddress;
  4341. + }
  4342. +
  4343. + if (gadget_is_superspeed(gadget)) {
  4344. + unsigned max_burst;
  4345. +
  4346. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4347. +
  4348. + /* Calculate bMaxBurst, we know packet size is 1024 */
  4349. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  4350. +
  4351. + /* Assume endpoint addresses are the same for both speeds */
  4352. + fsg_ss_bulk_in_desc.bEndpointAddress =
  4353. + fsg_fs_bulk_in_desc.bEndpointAddress;
  4354. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  4355. +
  4356. + fsg_ss_bulk_out_desc.bEndpointAddress =
  4357. + fsg_fs_bulk_out_desc.bEndpointAddress;
  4358. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  4359. + }
  4360. +
  4361. + if (gadget_is_otg(gadget))
  4362. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  4363. +
  4364. + rc = -ENOMEM;
  4365. +
  4366. + /* Allocate the request and buffer for endpoint 0 */
  4367. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  4368. + if (!req)
  4369. + goto out;
  4370. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  4371. + if (!req->buf)
  4372. + goto out;
  4373. + req->complete = ep0_complete;
  4374. +
  4375. + /* Allocate the data buffers */
  4376. + for (i = 0; i < fsg_num_buffers; ++i) {
  4377. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  4378. +
  4379. + /* Allocate for the bulk-in endpoint. We assume that
  4380. + * the buffer will also work with the bulk-out (and
  4381. + * interrupt-in) endpoint. */
  4382. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  4383. + if (!bh->buf)
  4384. + goto out;
  4385. + bh->next = bh + 1;
  4386. + }
  4387. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  4388. +
  4389. + /* This should reflect the actual gadget power source */
  4390. + usb_gadget_set_selfpowered(gadget);
  4391. +
  4392. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  4393. + "%s %s with %s",
  4394. + init_utsname()->sysname, init_utsname()->release,
  4395. + gadget->name);
  4396. +
  4397. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  4398. + "file-storage-gadget");
  4399. + if (IS_ERR(fsg->thread_task)) {
  4400. + rc = PTR_ERR(fsg->thread_task);
  4401. + goto out;
  4402. + }
  4403. +
  4404. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  4405. + INFO(fsg, "NOTE: This driver is deprecated. "
  4406. + "Consider using g_mass_storage instead.\n");
  4407. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  4408. +
  4409. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  4410. + for (i = 0; i < fsg->nluns; ++i) {
  4411. + curlun = &fsg->luns[i];
  4412. + if (fsg_lun_is_open(curlun)) {
  4413. + p = NULL;
  4414. + if (pathbuf) {
  4415. + p = d_path(&curlun->filp->f_path,
  4416. + pathbuf, PATH_MAX);
  4417. + if (IS_ERR(p))
  4418. + p = NULL;
  4419. + }
  4420. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  4421. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  4422. + }
  4423. + }
  4424. + kfree(pathbuf);
  4425. +
  4426. + DBG(fsg, "transport=%s (x%02x)\n",
  4427. + mod_data.transport_name, mod_data.transport_type);
  4428. + DBG(fsg, "protocol=%s (x%02x)\n",
  4429. + mod_data.protocol_name, mod_data.protocol_type);
  4430. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  4431. + mod_data.vendor, mod_data.product, mod_data.release);
  4432. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  4433. + mod_data.removable, mod_data.can_stall,
  4434. + mod_data.cdrom, mod_data.buflen);
  4435. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  4436. +
  4437. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  4438. +
  4439. + /* Tell the thread to start working */
  4440. + wake_up_process(fsg->thread_task);
  4441. + return 0;
  4442. +
  4443. +autoconf_fail:
  4444. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  4445. + rc = -ENOTSUPP;
  4446. +
  4447. +out:
  4448. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  4449. + fsg_unbind(gadget);
  4450. + complete(&fsg->thread_notifier);
  4451. + return rc;
  4452. +}
  4453. +
  4454. +
  4455. +/*-------------------------------------------------------------------------*/
  4456. +
  4457. +static void fsg_suspend(struct usb_gadget *gadget)
  4458. +{
  4459. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4460. +
  4461. + DBG(fsg, "suspend\n");
  4462. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  4463. +}
  4464. +
  4465. +static void fsg_resume(struct usb_gadget *gadget)
  4466. +{
  4467. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4468. +
  4469. + DBG(fsg, "resume\n");
  4470. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  4471. +}
  4472. +
  4473. +
  4474. +/*-------------------------------------------------------------------------*/
  4475. +
  4476. +static struct usb_gadget_driver fsg_driver = {
  4477. + .max_speed = USB_SPEED_SUPER,
  4478. + .function = (char *) fsg_string_product,
  4479. + .unbind = fsg_unbind,
  4480. + .disconnect = fsg_disconnect,
  4481. + .setup = fsg_setup,
  4482. + .suspend = fsg_suspend,
  4483. + .resume = fsg_resume,
  4484. +
  4485. + .driver = {
  4486. + .name = DRIVER_NAME,
  4487. + .owner = THIS_MODULE,
  4488. + // .release = ...
  4489. + // .suspend = ...
  4490. + // .resume = ...
  4491. + },
  4492. +};
  4493. +
  4494. +
  4495. +static int __init fsg_alloc(void)
  4496. +{
  4497. + struct fsg_dev *fsg;
  4498. +
  4499. + fsg = kzalloc(sizeof *fsg +
  4500. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  4501. +
  4502. + if (!fsg)
  4503. + return -ENOMEM;
  4504. + spin_lock_init(&fsg->lock);
  4505. + init_rwsem(&fsg->filesem);
  4506. + kref_init(&fsg->ref);
  4507. + init_completion(&fsg->thread_notifier);
  4508. +
  4509. + the_fsg = fsg;
  4510. + return 0;
  4511. +}
  4512. +
  4513. +
  4514. +static int __init fsg_init(void)
  4515. +{
  4516. + int rc;
  4517. + struct fsg_dev *fsg;
  4518. +
  4519. + rc = fsg_num_buffers_validate();
  4520. + if (rc != 0)
  4521. + return rc;
  4522. +
  4523. + if ((rc = fsg_alloc()) != 0)
  4524. + return rc;
  4525. + fsg = the_fsg;
  4526. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  4527. + kref_put(&fsg->ref, fsg_release);
  4528. + return rc;
  4529. +}
  4530. +module_init(fsg_init);
  4531. +
  4532. +
  4533. +static void __exit fsg_cleanup(void)
  4534. +{
  4535. + struct fsg_dev *fsg = the_fsg;
  4536. +
  4537. + /* Unregister the driver iff the thread hasn't already done so */
  4538. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  4539. + usb_gadget_unregister_driver(&fsg_driver);
  4540. +
  4541. + /* Wait for the thread to finish up */
  4542. + wait_for_completion(&fsg->thread_notifier);
  4543. +
  4544. + kref_put(&fsg->ref, fsg_release);
  4545. +}
  4546. +module_exit(fsg_cleanup);
  4547. --- a/drivers/usb/host/Kconfig
  4548. +++ b/drivers/usb/host/Kconfig
  4549. @@ -763,6 +763,16 @@ config USB_HWA_HCD
  4550. To compile this driver a module, choose M here: the module
  4551. will be called "hwa-hc".
  4552. +config USB_DWCOTG
  4553. + bool "Synopsis DWC host support"
  4554. + depends on USB && (FIQ || ARM64)
  4555. + help
  4556. + The Synopsis DWC controller is a dual-role
  4557. + host/peripheral/OTG ("On The Go") USB controllers.
  4558. +
  4559. + Enable this option to support this IP in host controller mode.
  4560. + If unsure, say N.
  4561. +
  4562. config USB_IMX21_HCD
  4563. tristate "i.MX21 HCD support"
  4564. depends on ARM && ARCH_MXC
  4565. --- a/drivers/usb/host/Makefile
  4566. +++ b/drivers/usb/host/Makefile
  4567. @@ -73,6 +73,8 @@ obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
  4568. obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
  4569. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  4570. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  4571. +
  4572. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  4573. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  4574. obj-$(CONFIG_USB_FSL_USB2) += fsl-mph-dr-of.o
  4575. obj-$(CONFIG_USB_EHCI_FSL) += fsl-mph-dr-of.o
  4576. --- /dev/null
  4577. +++ b/drivers/usb/host/dwc_common_port/Makefile
  4578. @@ -0,0 +1,58 @@
  4579. +#
  4580. +# Makefile for DWC_common library
  4581. +#
  4582. +
  4583. +ifneq ($(KERNELRELEASE),)
  4584. +
  4585. +ccflags-y += -DDWC_LINUX
  4586. +#ccflags-y += -DDEBUG
  4587. +#ccflags-y += -DDWC_DEBUG_REGS
  4588. +#ccflags-y += -DDWC_DEBUG_MEMORY
  4589. +
  4590. +ccflags-y += -DDWC_LIBMODULE
  4591. +ccflags-y += -DDWC_CCLIB
  4592. +#ccflags-y += -DDWC_CRYPTOLIB
  4593. +ccflags-y += -DDWC_NOTIFYLIB
  4594. +ccflags-y += -DDWC_UTFLIB
  4595. +
  4596. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  4597. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  4598. + dwc_crypto.o dwc_notifier.o \
  4599. + dwc_common_linux.o dwc_mem.o
  4600. +
  4601. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  4602. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  4603. +
  4604. +ifneq ($(kernrel3),2.6.20)
  4605. +# grayg - I only know that we use ccflags-y in 2.6.31 actually
  4606. +ccflags-y += $(CPPFLAGS)
  4607. +endif
  4608. +
  4609. +else
  4610. +
  4611. +#ifeq ($(KDIR),)
  4612. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  4613. +#endif
  4614. +
  4615. +ifeq ($(ARCH),)
  4616. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  4617. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  4618. +endif
  4619. +
  4620. +ifeq ($(DOXYGEN),)
  4621. +DOXYGEN := doxygen
  4622. +endif
  4623. +
  4624. +default:
  4625. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  4626. +
  4627. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  4628. + $(DOXYGEN) doc/doxygen.cfg
  4629. +
  4630. +tags: $(wildcard *.[hc])
  4631. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  4632. +
  4633. +endif
  4634. +
  4635. +clean:
  4636. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  4637. --- /dev/null
  4638. +++ b/drivers/usb/host/dwc_common_port/Makefile.fbsd
  4639. @@ -0,0 +1,17 @@
  4640. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  4641. +CFLAGS += -DDWC_FREEBSD
  4642. +CFLAGS += -DDEBUG
  4643. +#CFLAGS += -DDWC_DEBUG_REGS
  4644. +#CFLAGS += -DDWC_DEBUG_MEMORY
  4645. +
  4646. +#CFLAGS += -DDWC_LIBMODULE
  4647. +#CFLAGS += -DDWC_CCLIB
  4648. +#CFLAGS += -DDWC_CRYPTOLIB
  4649. +#CFLAGS += -DDWC_NOTIFYLIB
  4650. +#CFLAGS += -DDWC_UTFLIB
  4651. +
  4652. +KMOD = dwc_common_port_lib
  4653. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  4654. + dwc_common_fbsd.c dwc_mem.c
  4655. +
  4656. +.include <bsd.kmod.mk>
  4657. --- /dev/null
  4658. +++ b/drivers/usb/host/dwc_common_port/Makefile.linux
  4659. @@ -0,0 +1,49 @@
  4660. +#
  4661. +# Makefile for DWC_common library
  4662. +#
  4663. +ifneq ($(KERNELRELEASE),)
  4664. +
  4665. +ccflags-y += -DDWC_LINUX
  4666. +#ccflags-y += -DDEBUG
  4667. +#ccflags-y += -DDWC_DEBUG_REGS
  4668. +#ccflags-y += -DDWC_DEBUG_MEMORY
  4669. +
  4670. +ccflags-y += -DDWC_LIBMODULE
  4671. +ccflags-y += -DDWC_CCLIB
  4672. +ccflags-y += -DDWC_CRYPTOLIB
  4673. +ccflags-y += -DDWC_NOTIFYLIB
  4674. +ccflags-y += -DDWC_UTFLIB
  4675. +
  4676. +obj-m := dwc_common_port_lib.o
  4677. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  4678. + dwc_crypto.o dwc_notifier.o \
  4679. + dwc_common_linux.o dwc_mem.o
  4680. +
  4681. +else
  4682. +
  4683. +ifeq ($(KDIR),)
  4684. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  4685. +endif
  4686. +
  4687. +ifeq ($(ARCH),)
  4688. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  4689. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  4690. +endif
  4691. +
  4692. +ifeq ($(DOXYGEN),)
  4693. +DOXYGEN := doxygen
  4694. +endif
  4695. +
  4696. +default:
  4697. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  4698. +
  4699. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  4700. + $(DOXYGEN) doc/doxygen.cfg
  4701. +
  4702. +tags: $(wildcard *.[hc])
  4703. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  4704. +
  4705. +endif
  4706. +
  4707. +clean:
  4708. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  4709. --- /dev/null
  4710. +++ b/drivers/usb/host/dwc_common_port/changes.txt
  4711. @@ -0,0 +1,174 @@
  4712. +
  4713. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  4714. +IO context struct. The IO context struct should live in an os-dependent struct
  4715. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  4716. +named 'os_dep' embedded in the main device struct. So there these calls look
  4717. +like this:
  4718. +
  4719. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  4720. +
  4721. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  4722. + &pcd->dev_global_regs->dcfg, 0);
  4723. +
  4724. +Note that for the existing Linux driver ports, it is not necessary to actually
  4725. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  4726. +require an IO context, its macros for dwc_read_reg32() and friends do not
  4727. +use the context pointer, so it is optimized away by the compiler. But it is
  4728. +necessary to add the pointer parameter to all of the call sites, to be ready
  4729. +for any future ports (such as FreeBSD) which do require an IO context.
  4730. +
  4731. +
  4732. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  4733. +take an additional parameter, a pointer to a memory context. Examples:
  4734. +
  4735. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  4736. +
  4737. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  4738. +
  4739. +Again, for the Linux ports, it is not necessary to actually define the memctx
  4740. +member, but it is necessary to add the pointer parameter to all of the call
  4741. +sites.
  4742. +
  4743. +
  4744. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  4745. +
  4746. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  4747. +
  4748. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  4749. +
  4750. +
  4751. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  4752. +
  4753. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  4754. +
  4755. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  4756. +
  4757. +
  4758. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  4759. +
  4760. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  4761. +
  4762. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  4763. +
  4764. +
  4765. +Same for dwc_timer_alloc(). Example:
  4766. +
  4767. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  4768. + cb_func, cb_data);
  4769. +
  4770. +
  4771. +Same for dwc_waitq_alloc(). Example:
  4772. +
  4773. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  4774. +
  4775. +
  4776. +Same for dwc_thread_run(). Example:
  4777. +
  4778. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  4779. + "dwc_usb3_thd1", data);
  4780. +
  4781. +
  4782. +Same for dwc_workq_alloc(). Example:
  4783. +
  4784. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  4785. +
  4786. +
  4787. +Same for dwc_task_alloc(). Example:
  4788. +
  4789. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  4790. + cb_func, cb_data);
  4791. +
  4792. +
  4793. +In addition to the context pointer additions, a few core functions have had
  4794. +other changes made to their parameters:
  4795. +
  4796. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  4797. +has been changed from a uint64_t to a dwc_irqflags_t.
  4798. +
  4799. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  4800. +FreeBSD equivalent of that function requires it.
  4801. +
  4802. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  4803. +'char *name' parameter, to be consistent with dwc_thread_run() and
  4804. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  4805. +requires a unique name.
  4806. +
  4807. +
  4808. +Here is a complete list of the core functions that now take a pointer to a
  4809. +context as their first parameter:
  4810. +
  4811. + dwc_read_reg32
  4812. + dwc_read_reg64
  4813. + dwc_write_reg32
  4814. + dwc_write_reg64
  4815. + dwc_modify_reg32
  4816. + dwc_modify_reg64
  4817. + dwc_alloc
  4818. + dwc_alloc_atomic
  4819. + dwc_strdup
  4820. + dwc_free
  4821. + dwc_dma_alloc
  4822. + dwc_dma_free
  4823. + dwc_mutex_alloc
  4824. + dwc_mutex_free
  4825. + dwc_spinlock_alloc
  4826. + dwc_spinlock_free
  4827. + dwc_timer_alloc
  4828. + dwc_waitq_alloc
  4829. + dwc_thread_run
  4830. + dwc_workq_alloc
  4831. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  4832. +
  4833. +And here are the core functions that have other changes to their parameters:
  4834. +
  4835. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  4836. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  4837. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  4838. +
  4839. +
  4840. +
  4841. +The changes to the core functions also require some of the other library
  4842. +functions to change:
  4843. +
  4844. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  4845. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  4846. + (for mutex allocation) as the 2nd param.
  4847. +
  4848. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  4849. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  4850. + 'void *memctx' as the 1st param.
  4851. +
  4852. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  4853. + 'void *memctx' as the 1st param.
  4854. +
  4855. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  4856. +
  4857. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  4858. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  4859. + param, and also now returns an integer value that is non-zero if
  4860. + allocation of its data structures or work queue fails.
  4861. +
  4862. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  4863. +
  4864. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  4865. + param, and also now returns an integer value that is non-zero if
  4866. + allocation of its data structures fails.
  4867. +
  4868. +
  4869. +
  4870. +Other miscellaneous changes:
  4871. +
  4872. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  4873. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  4874. +
  4875. +The following #define's have been added to allow selectively compiling library
  4876. +features:
  4877. +
  4878. + DWC_CCLIB
  4879. + DWC_CRYPTOLIB
  4880. + DWC_NOTIFYLIB
  4881. + DWC_UTFLIB
  4882. +
  4883. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  4884. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  4885. +library code directly into a driver module, instead of as a standalone module.
  4886. --- /dev/null
  4887. +++ b/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  4888. @@ -0,0 +1,270 @@
  4889. +# Doxyfile 1.4.5
  4890. +
  4891. +#---------------------------------------------------------------------------
  4892. +# Project related configuration options
  4893. +#---------------------------------------------------------------------------
  4894. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  4895. +PROJECT_NUMBER =
  4896. +OUTPUT_DIRECTORY = doc
  4897. +CREATE_SUBDIRS = NO
  4898. +OUTPUT_LANGUAGE = English
  4899. +BRIEF_MEMBER_DESC = YES
  4900. +REPEAT_BRIEF = YES
  4901. +ABBREVIATE_BRIEF = "The $name class" \
  4902. + "The $name widget" \
  4903. + "The $name file" \
  4904. + is \
  4905. + provides \
  4906. + specifies \
  4907. + contains \
  4908. + represents \
  4909. + a \
  4910. + an \
  4911. + the
  4912. +ALWAYS_DETAILED_SEC = YES
  4913. +INLINE_INHERITED_MEMB = NO
  4914. +FULL_PATH_NAMES = NO
  4915. +STRIP_FROM_PATH = ..
  4916. +STRIP_FROM_INC_PATH =
  4917. +SHORT_NAMES = NO
  4918. +JAVADOC_AUTOBRIEF = YES
  4919. +MULTILINE_CPP_IS_BRIEF = NO
  4920. +DETAILS_AT_TOP = YES
  4921. +INHERIT_DOCS = YES
  4922. +SEPARATE_MEMBER_PAGES = NO
  4923. +TAB_SIZE = 8
  4924. +ALIASES =
  4925. +OPTIMIZE_OUTPUT_FOR_C = YES
  4926. +OPTIMIZE_OUTPUT_JAVA = NO
  4927. +BUILTIN_STL_SUPPORT = NO
  4928. +DISTRIBUTE_GROUP_DOC = NO
  4929. +SUBGROUPING = NO
  4930. +#---------------------------------------------------------------------------
  4931. +# Build related configuration options
  4932. +#---------------------------------------------------------------------------
  4933. +EXTRACT_ALL = NO
  4934. +EXTRACT_PRIVATE = NO
  4935. +EXTRACT_STATIC = YES
  4936. +EXTRACT_LOCAL_CLASSES = NO
  4937. +EXTRACT_LOCAL_METHODS = NO
  4938. +HIDE_UNDOC_MEMBERS = NO
  4939. +HIDE_UNDOC_CLASSES = NO
  4940. +HIDE_FRIEND_COMPOUNDS = NO
  4941. +HIDE_IN_BODY_DOCS = NO
  4942. +INTERNAL_DOCS = NO
  4943. +CASE_SENSE_NAMES = YES
  4944. +HIDE_SCOPE_NAMES = NO
  4945. +SHOW_INCLUDE_FILES = NO
  4946. +INLINE_INFO = YES
  4947. +SORT_MEMBER_DOCS = NO
  4948. +SORT_BRIEF_DOCS = NO
  4949. +SORT_BY_SCOPE_NAME = NO
  4950. +GENERATE_TODOLIST = YES
  4951. +GENERATE_TESTLIST = YES
  4952. +GENERATE_BUGLIST = YES
  4953. +GENERATE_DEPRECATEDLIST= YES
  4954. +ENABLED_SECTIONS =
  4955. +MAX_INITIALIZER_LINES = 30
  4956. +SHOW_USED_FILES = YES
  4957. +SHOW_DIRECTORIES = YES
  4958. +FILE_VERSION_FILTER =
  4959. +#---------------------------------------------------------------------------
  4960. +# configuration options related to warning and progress messages
  4961. +#---------------------------------------------------------------------------
  4962. +QUIET = YES
  4963. +WARNINGS = YES
  4964. +WARN_IF_UNDOCUMENTED = NO
  4965. +WARN_IF_DOC_ERROR = YES
  4966. +WARN_NO_PARAMDOC = YES
  4967. +WARN_FORMAT = "$file:$line: $text"
  4968. +WARN_LOGFILE =
  4969. +#---------------------------------------------------------------------------
  4970. +# configuration options related to the input files
  4971. +#---------------------------------------------------------------------------
  4972. +INPUT = .
  4973. +FILE_PATTERNS = *.c \
  4974. + *.cc \
  4975. + *.cxx \
  4976. + *.cpp \
  4977. + *.c++ \
  4978. + *.d \
  4979. + *.java \
  4980. + *.ii \
  4981. + *.ixx \
  4982. + *.ipp \
  4983. + *.i++ \
  4984. + *.inl \
  4985. + *.h \
  4986. + *.hh \
  4987. + *.hxx \
  4988. + *.hpp \
  4989. + *.h++ \
  4990. + *.idl \
  4991. + *.odl \
  4992. + *.cs \
  4993. + *.php \
  4994. + *.php3 \
  4995. + *.inc \
  4996. + *.m \
  4997. + *.mm \
  4998. + *.dox \
  4999. + *.py \
  5000. + *.C \
  5001. + *.CC \
  5002. + *.C++ \
  5003. + *.II \
  5004. + *.I++ \
  5005. + *.H \
  5006. + *.HH \
  5007. + *.H++ \
  5008. + *.CS \
  5009. + *.PHP \
  5010. + *.PHP3 \
  5011. + *.M \
  5012. + *.MM \
  5013. + *.PY
  5014. +RECURSIVE = NO
  5015. +EXCLUDE =
  5016. +EXCLUDE_SYMLINKS = NO
  5017. +EXCLUDE_PATTERNS =
  5018. +EXAMPLE_PATH =
  5019. +EXAMPLE_PATTERNS = *
  5020. +EXAMPLE_RECURSIVE = NO
  5021. +IMAGE_PATH =
  5022. +INPUT_FILTER =
  5023. +FILTER_PATTERNS =
  5024. +FILTER_SOURCE_FILES = NO
  5025. +#---------------------------------------------------------------------------
  5026. +# configuration options related to source browsing
  5027. +#---------------------------------------------------------------------------
  5028. +SOURCE_BROWSER = NO
  5029. +INLINE_SOURCES = NO
  5030. +STRIP_CODE_COMMENTS = YES
  5031. +REFERENCED_BY_RELATION = YES
  5032. +REFERENCES_RELATION = YES
  5033. +USE_HTAGS = NO
  5034. +VERBATIM_HEADERS = NO
  5035. +#---------------------------------------------------------------------------
  5036. +# configuration options related to the alphabetical class index
  5037. +#---------------------------------------------------------------------------
  5038. +ALPHABETICAL_INDEX = NO
  5039. +COLS_IN_ALPHA_INDEX = 5
  5040. +IGNORE_PREFIX =
  5041. +#---------------------------------------------------------------------------
  5042. +# configuration options related to the HTML output
  5043. +#---------------------------------------------------------------------------
  5044. +GENERATE_HTML = YES
  5045. +HTML_OUTPUT = html
  5046. +HTML_FILE_EXTENSION = .html
  5047. +HTML_HEADER =
  5048. +HTML_FOOTER =
  5049. +HTML_STYLESHEET =
  5050. +HTML_ALIGN_MEMBERS = YES
  5051. +GENERATE_HTMLHELP = NO
  5052. +CHM_FILE =
  5053. +HHC_LOCATION =
  5054. +GENERATE_CHI = NO
  5055. +BINARY_TOC = NO
  5056. +TOC_EXPAND = NO
  5057. +DISABLE_INDEX = NO
  5058. +ENUM_VALUES_PER_LINE = 4
  5059. +GENERATE_TREEVIEW = YES
  5060. +TREEVIEW_WIDTH = 250
  5061. +#---------------------------------------------------------------------------
  5062. +# configuration options related to the LaTeX output
  5063. +#---------------------------------------------------------------------------
  5064. +GENERATE_LATEX = NO
  5065. +LATEX_OUTPUT = latex
  5066. +LATEX_CMD_NAME = latex
  5067. +MAKEINDEX_CMD_NAME = makeindex
  5068. +COMPACT_LATEX = NO
  5069. +PAPER_TYPE = a4wide
  5070. +EXTRA_PACKAGES =
  5071. +LATEX_HEADER =
  5072. +PDF_HYPERLINKS = NO
  5073. +USE_PDFLATEX = NO
  5074. +LATEX_BATCHMODE = NO
  5075. +LATEX_HIDE_INDICES = NO
  5076. +#---------------------------------------------------------------------------
  5077. +# configuration options related to the RTF output
  5078. +#---------------------------------------------------------------------------
  5079. +GENERATE_RTF = NO
  5080. +RTF_OUTPUT = rtf
  5081. +COMPACT_RTF = NO
  5082. +RTF_HYPERLINKS = NO
  5083. +RTF_STYLESHEET_FILE =
  5084. +RTF_EXTENSIONS_FILE =
  5085. +#---------------------------------------------------------------------------
  5086. +# configuration options related to the man page output
  5087. +#---------------------------------------------------------------------------
  5088. +GENERATE_MAN = NO
  5089. +MAN_OUTPUT = man
  5090. +MAN_EXTENSION = .3
  5091. +MAN_LINKS = NO
  5092. +#---------------------------------------------------------------------------
  5093. +# configuration options related to the XML output
  5094. +#---------------------------------------------------------------------------
  5095. +GENERATE_XML = NO
  5096. +XML_OUTPUT = xml
  5097. +XML_SCHEMA =
  5098. +XML_DTD =
  5099. +XML_PROGRAMLISTING = YES
  5100. +#---------------------------------------------------------------------------
  5101. +# configuration options for the AutoGen Definitions output
  5102. +#---------------------------------------------------------------------------
  5103. +GENERATE_AUTOGEN_DEF = NO
  5104. +#---------------------------------------------------------------------------
  5105. +# configuration options related to the Perl module output
  5106. +#---------------------------------------------------------------------------
  5107. +GENERATE_PERLMOD = NO
  5108. +PERLMOD_LATEX = NO
  5109. +PERLMOD_PRETTY = YES
  5110. +PERLMOD_MAKEVAR_PREFIX =
  5111. +#---------------------------------------------------------------------------
  5112. +# Configuration options related to the preprocessor
  5113. +#---------------------------------------------------------------------------
  5114. +ENABLE_PREPROCESSING = YES
  5115. +MACRO_EXPANSION = NO
  5116. +EXPAND_ONLY_PREDEF = NO
  5117. +SEARCH_INCLUDES = YES
  5118. +INCLUDE_PATH =
  5119. +INCLUDE_FILE_PATTERNS =
  5120. +PREDEFINED = DEBUG DEBUG_MEMORY
  5121. +EXPAND_AS_DEFINED =
  5122. +SKIP_FUNCTION_MACROS = YES
  5123. +#---------------------------------------------------------------------------
  5124. +# Configuration::additions related to external references
  5125. +#---------------------------------------------------------------------------
  5126. +TAGFILES =
  5127. +GENERATE_TAGFILE =
  5128. +ALLEXTERNALS = NO
  5129. +EXTERNAL_GROUPS = YES
  5130. +PERL_PATH = /usr/bin/perl
  5131. +#---------------------------------------------------------------------------
  5132. +# Configuration options related to the dot tool
  5133. +#---------------------------------------------------------------------------
  5134. +CLASS_DIAGRAMS = YES
  5135. +HIDE_UNDOC_RELATIONS = YES
  5136. +HAVE_DOT = NO
  5137. +CLASS_GRAPH = YES
  5138. +COLLABORATION_GRAPH = YES
  5139. +GROUP_GRAPHS = YES
  5140. +UML_LOOK = NO
  5141. +TEMPLATE_RELATIONS = NO
  5142. +INCLUDE_GRAPH = NO
  5143. +INCLUDED_BY_GRAPH = YES
  5144. +CALL_GRAPH = NO
  5145. +GRAPHICAL_HIERARCHY = YES
  5146. +DIRECTORY_GRAPH = YES
  5147. +DOT_IMAGE_FORMAT = png
  5148. +DOT_PATH =
  5149. +DOTFILE_DIRS =
  5150. +MAX_DOT_GRAPH_DEPTH = 1000
  5151. +DOT_TRANSPARENT = NO
  5152. +DOT_MULTI_TARGETS = NO
  5153. +GENERATE_LEGEND = YES
  5154. +DOT_CLEANUP = YES
  5155. +#---------------------------------------------------------------------------
  5156. +# Configuration::additions related to the search engine
  5157. +#---------------------------------------------------------------------------
  5158. +SEARCHENGINE = NO
  5159. --- /dev/null
  5160. +++ b/drivers/usb/host/dwc_common_port/dwc_cc.c
  5161. @@ -0,0 +1,532 @@
  5162. +/* =========================================================================
  5163. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  5164. + * $Revision: #4 $
  5165. + * $Date: 2010/11/04 $
  5166. + * $Change: 1621692 $
  5167. + *
  5168. + * Synopsys Portability Library Software and documentation
  5169. + * (hereinafter, "Software") is an Unsupported proprietary work of
  5170. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  5171. + * between Synopsys and you.
  5172. + *
  5173. + * The Software IS NOT an item of Licensed Software or Licensed Product
  5174. + * under any End User Software License Agreement or Agreement for
  5175. + * Licensed Product with Synopsys or any supplement thereto. You are
  5176. + * permitted to use and redistribute this Software in source and binary
  5177. + * forms, with or without modification, provided that redistributions
  5178. + * of source code must retain this notice. You may not view, use,
  5179. + * disclose, copy or distribute this file or any information contained
  5180. + * herein except pursuant to this license grant from Synopsys. If you
  5181. + * do not agree with this notice, including the disclaimer below, then
  5182. + * you are not authorized to use the Software.
  5183. + *
  5184. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  5185. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5186. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  5187. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  5188. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5189. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5190. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5191. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  5192. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5193. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  5194. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  5195. + * DAMAGE.
  5196. + * ========================================================================= */
  5197. +#ifdef DWC_CCLIB
  5198. +
  5199. +#include "dwc_cc.h"
  5200. +
  5201. +typedef struct dwc_cc
  5202. +{
  5203. + uint32_t uid;
  5204. + uint8_t chid[16];
  5205. + uint8_t cdid[16];
  5206. + uint8_t ck[16];
  5207. + uint8_t *name;
  5208. + uint8_t length;
  5209. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  5210. +} dwc_cc_t;
  5211. +
  5212. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  5213. +
  5214. +/** The main structure for CC management. */
  5215. +struct dwc_cc_if
  5216. +{
  5217. + dwc_mutex_t *mutex;
  5218. + char *filename;
  5219. +
  5220. + unsigned is_host:1;
  5221. +
  5222. + dwc_notifier_t *notifier;
  5223. +
  5224. + struct context_list list;
  5225. +};
  5226. +
  5227. +#ifdef DEBUG
  5228. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  5229. +{
  5230. + int i;
  5231. + DWC_PRINTF("%s: ", name);
  5232. + for (i=0; i<len; i++) {
  5233. + DWC_PRINTF("%02x ", bytes[i]);
  5234. + }
  5235. + DWC_PRINTF("\n");
  5236. +}
  5237. +#else
  5238. +#define dump_bytes(x...)
  5239. +#endif
  5240. +
  5241. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  5242. +{
  5243. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  5244. + if (!cc) {
  5245. + return NULL;
  5246. + }
  5247. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  5248. +
  5249. + if (name) {
  5250. + cc->length = length;
  5251. + cc->name = dwc_alloc(mem_ctx, length);
  5252. + if (!cc->name) {
  5253. + dwc_free(mem_ctx, cc);
  5254. + return NULL;
  5255. + }
  5256. +
  5257. + DWC_MEMCPY(cc->name, name, length);
  5258. + }
  5259. +
  5260. + return cc;
  5261. +}
  5262. +
  5263. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  5264. +{
  5265. + if (cc->name) {
  5266. + dwc_free(mem_ctx, cc->name);
  5267. + }
  5268. + dwc_free(mem_ctx, cc);
  5269. +}
  5270. +
  5271. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  5272. +{
  5273. + uint32_t uid = 0;
  5274. + dwc_cc_t *cc;
  5275. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5276. + if (cc->uid > uid) {
  5277. + uid = cc->uid;
  5278. + }
  5279. + }
  5280. +
  5281. + if (uid == 0) {
  5282. + uid = 255;
  5283. + }
  5284. +
  5285. + return uid + 1;
  5286. +}
  5287. +
  5288. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  5289. +{
  5290. + dwc_cc_t *cc;
  5291. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5292. + if (cc->uid == uid) {
  5293. + return cc;
  5294. + }
  5295. + }
  5296. + return NULL;
  5297. +}
  5298. +
  5299. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  5300. +{
  5301. + unsigned int size = 0;
  5302. + dwc_cc_t *cc;
  5303. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5304. + size += (48 + 1);
  5305. + if (cc->name) {
  5306. + size += cc->length;
  5307. + }
  5308. + }
  5309. + return size;
  5310. +}
  5311. +
  5312. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  5313. +{
  5314. + uint32_t uid = 0;
  5315. + dwc_cc_t *cc;
  5316. +
  5317. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5318. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  5319. + uid = cc->uid;
  5320. + break;
  5321. + }
  5322. + }
  5323. + return uid;
  5324. +}
  5325. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  5326. +{
  5327. + uint32_t uid = 0;
  5328. + dwc_cc_t *cc;
  5329. +
  5330. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5331. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  5332. + uid = cc->uid;
  5333. + break;
  5334. + }
  5335. + }
  5336. + return uid;
  5337. +}
  5338. +
  5339. +/* Internal cc_add */
  5340. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5341. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5342. +{
  5343. + dwc_cc_t *cc;
  5344. + uint32_t uid;
  5345. +
  5346. + if (cc_if->is_host) {
  5347. + uid = cc_match_cdid(cc_if, cdid);
  5348. + }
  5349. + else {
  5350. + uid = cc_match_chid(cc_if, chid);
  5351. + }
  5352. +
  5353. + if (uid) {
  5354. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  5355. + cc = cc_find(cc_if, uid);
  5356. + }
  5357. + else {
  5358. + cc = alloc_cc(mem_ctx, name, length);
  5359. + cc->uid = next_uid(cc_if);
  5360. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  5361. + }
  5362. +
  5363. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  5364. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  5365. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  5366. +
  5367. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  5368. + dump_bytes("CHID", cc->chid, 16);
  5369. + dump_bytes("CDID", cc->cdid, 16);
  5370. + dump_bytes("CK", cc->ck, 16);
  5371. + return cc->uid;
  5372. +}
  5373. +
  5374. +/* Internal cc_clear */
  5375. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  5376. +{
  5377. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  5378. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  5379. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  5380. + free_cc(mem_ctx, cc);
  5381. + }
  5382. +}
  5383. +
  5384. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  5385. + dwc_notifier_t *notifier, unsigned is_host)
  5386. +{
  5387. + dwc_cc_if_t *cc_if = NULL;
  5388. +
  5389. + /* Allocate a common_cc_if structure */
  5390. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  5391. +
  5392. + if (!cc_if)
  5393. + return NULL;
  5394. +
  5395. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  5396. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  5397. +#else
  5398. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  5399. +#endif
  5400. + if (!cc_if->mutex) {
  5401. + dwc_free(mem_ctx, cc_if);
  5402. + return NULL;
  5403. + }
  5404. +
  5405. + DWC_CIRCLEQ_INIT(&cc_if->list);
  5406. + cc_if->is_host = is_host;
  5407. + cc_if->notifier = notifier;
  5408. + return cc_if;
  5409. +}
  5410. +
  5411. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  5412. +{
  5413. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  5414. + DWC_MUTEX_FREE(cc_if->mutex);
  5415. +#else
  5416. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  5417. +#endif
  5418. + cc_clear(mem_ctx, cc_if);
  5419. + dwc_free(mem_ctx, cc_if);
  5420. +}
  5421. +
  5422. +static void cc_changed(dwc_cc_if_t *cc_if)
  5423. +{
  5424. + if (cc_if->notifier) {
  5425. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  5426. + }
  5427. +}
  5428. +
  5429. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  5430. +{
  5431. + DWC_MUTEX_LOCK(cc_if->mutex);
  5432. + cc_clear(mem_ctx, cc_if);
  5433. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5434. + cc_changed(cc_if);
  5435. +}
  5436. +
  5437. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5438. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5439. +{
  5440. + uint32_t uid;
  5441. +
  5442. + DWC_MUTEX_LOCK(cc_if->mutex);
  5443. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  5444. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5445. + cc_changed(cc_if);
  5446. +
  5447. + return uid;
  5448. +}
  5449. +
  5450. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  5451. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5452. +{
  5453. + dwc_cc_t* cc;
  5454. +
  5455. + DWC_DEBUGC("Change connection context %d", id);
  5456. +
  5457. + DWC_MUTEX_LOCK(cc_if->mutex);
  5458. + cc = cc_find(cc_if, id);
  5459. + if (!cc) {
  5460. + DWC_ERROR("Uid %d not found in cc list\n", id);
  5461. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5462. + return;
  5463. + }
  5464. +
  5465. + if (chid) {
  5466. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  5467. + }
  5468. + if (cdid) {
  5469. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  5470. + }
  5471. + if (ck) {
  5472. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  5473. + }
  5474. +
  5475. + if (name) {
  5476. + if (cc->name) {
  5477. + dwc_free(mem_ctx, cc->name);
  5478. + }
  5479. + cc->name = dwc_alloc(mem_ctx, length);
  5480. + if (!cc->name) {
  5481. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  5482. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5483. + return;
  5484. + }
  5485. + cc->length = length;
  5486. + DWC_MEMCPY(cc->name, name, length);
  5487. + }
  5488. +
  5489. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5490. +
  5491. + cc_changed(cc_if);
  5492. +
  5493. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  5494. + dump_bytes("New CHID", cc->chid, 16);
  5495. + dump_bytes("New CDID", cc->cdid, 16);
  5496. + dump_bytes("New CK", cc->ck, 16);
  5497. +}
  5498. +
  5499. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  5500. +{
  5501. + dwc_cc_t *cc;
  5502. +
  5503. + DWC_DEBUGC("Removing connection context %d", id);
  5504. +
  5505. + DWC_MUTEX_LOCK(cc_if->mutex);
  5506. + cc = cc_find(cc_if, id);
  5507. + if (!cc) {
  5508. + DWC_ERROR("Uid %d not found in cc list\n", id);
  5509. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5510. + return;
  5511. + }
  5512. +
  5513. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  5514. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5515. + free_cc(mem_ctx, cc);
  5516. +
  5517. + cc_changed(cc_if);
  5518. +}
  5519. +
  5520. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  5521. +{
  5522. + uint8_t *buf, *x;
  5523. + uint8_t zero = 0;
  5524. + dwc_cc_t *cc;
  5525. +
  5526. + DWC_MUTEX_LOCK(cc_if->mutex);
  5527. + *length = cc_data_size(cc_if);
  5528. + if (!(*length)) {
  5529. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5530. + return NULL;
  5531. + }
  5532. +
  5533. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  5534. +
  5535. + buf = dwc_alloc(mem_ctx, *length);
  5536. + if (!buf) {
  5537. + *length = 0;
  5538. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5539. + return NULL;
  5540. + }
  5541. +
  5542. + x = buf;
  5543. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5544. + DWC_MEMCPY(x, cc->chid, 16);
  5545. + x += 16;
  5546. + DWC_MEMCPY(x, cc->cdid, 16);
  5547. + x += 16;
  5548. + DWC_MEMCPY(x, cc->ck, 16);
  5549. + x += 16;
  5550. + if (cc->name) {
  5551. + DWC_MEMCPY(x, &cc->length, 1);
  5552. + x += 1;
  5553. + DWC_MEMCPY(x, cc->name, cc->length);
  5554. + x += cc->length;
  5555. + }
  5556. + else {
  5557. + DWC_MEMCPY(x, &zero, 1);
  5558. + x += 1;
  5559. + }
  5560. + }
  5561. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5562. +
  5563. + return buf;
  5564. +}
  5565. +
  5566. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  5567. +{
  5568. + uint8_t name_length;
  5569. + uint8_t *name;
  5570. + uint8_t *chid;
  5571. + uint8_t *cdid;
  5572. + uint8_t *ck;
  5573. + uint32_t i = 0;
  5574. +
  5575. + DWC_MUTEX_LOCK(cc_if->mutex);
  5576. + cc_clear(mem_ctx, cc_if);
  5577. +
  5578. + while (i < length) {
  5579. + chid = &data[i];
  5580. + i += 16;
  5581. + cdid = &data[i];
  5582. + i += 16;
  5583. + ck = &data[i];
  5584. + i += 16;
  5585. +
  5586. + name_length = data[i];
  5587. + i ++;
  5588. +
  5589. + if (name_length) {
  5590. + name = &data[i];
  5591. + i += name_length;
  5592. + }
  5593. + else {
  5594. + name = NULL;
  5595. + }
  5596. +
  5597. + /* check to see if we haven't overflown the buffer */
  5598. + if (i > length) {
  5599. + DWC_ERROR("Data format error while attempting to load CCs "
  5600. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  5601. + break;
  5602. + }
  5603. +
  5604. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  5605. + }
  5606. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5607. +
  5608. + cc_changed(cc_if);
  5609. +}
  5610. +
  5611. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  5612. +{
  5613. + uint32_t uid = 0;
  5614. +
  5615. + DWC_MUTEX_LOCK(cc_if->mutex);
  5616. + uid = cc_match_chid(cc_if, chid);
  5617. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5618. + return uid;
  5619. +}
  5620. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  5621. +{
  5622. + uint32_t uid = 0;
  5623. +
  5624. + DWC_MUTEX_LOCK(cc_if->mutex);
  5625. + uid = cc_match_cdid(cc_if, cdid);
  5626. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5627. + return uid;
  5628. +}
  5629. +
  5630. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  5631. +{
  5632. + uint8_t *ck = NULL;
  5633. + dwc_cc_t *cc;
  5634. +
  5635. + DWC_MUTEX_LOCK(cc_if->mutex);
  5636. + cc = cc_find(cc_if, id);
  5637. + if (cc) {
  5638. + ck = cc->ck;
  5639. + }
  5640. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5641. +
  5642. + return ck;
  5643. +
  5644. +}
  5645. +
  5646. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  5647. +{
  5648. + uint8_t *retval = NULL;
  5649. + dwc_cc_t *cc;
  5650. +
  5651. + DWC_MUTEX_LOCK(cc_if->mutex);
  5652. + cc = cc_find(cc_if, id);
  5653. + if (cc) {
  5654. + retval = cc->chid;
  5655. + }
  5656. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5657. +
  5658. + return retval;
  5659. +}
  5660. +
  5661. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  5662. +{
  5663. + uint8_t *retval = NULL;
  5664. + dwc_cc_t *cc;
  5665. +
  5666. + DWC_MUTEX_LOCK(cc_if->mutex);
  5667. + cc = cc_find(cc_if, id);
  5668. + if (cc) {
  5669. + retval = cc->cdid;
  5670. + }
  5671. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5672. +
  5673. + return retval;
  5674. +}
  5675. +
  5676. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  5677. +{
  5678. + uint8_t *retval = NULL;
  5679. + dwc_cc_t *cc;
  5680. +
  5681. + DWC_MUTEX_LOCK(cc_if->mutex);
  5682. + *length = 0;
  5683. + cc = cc_find(cc_if, id);
  5684. + if (cc) {
  5685. + *length = cc->length;
  5686. + retval = cc->name;
  5687. + }
  5688. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5689. +
  5690. + return retval;
  5691. +}
  5692. +
  5693. +#endif /* DWC_CCLIB */
  5694. --- /dev/null
  5695. +++ b/drivers/usb/host/dwc_common_port/dwc_cc.h
  5696. @@ -0,0 +1,224 @@
  5697. +/* =========================================================================
  5698. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  5699. + * $Revision: #4 $
  5700. + * $Date: 2010/09/28 $
  5701. + * $Change: 1596182 $
  5702. + *
  5703. + * Synopsys Portability Library Software and documentation
  5704. + * (hereinafter, "Software") is an Unsupported proprietary work of
  5705. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  5706. + * between Synopsys and you.
  5707. + *
  5708. + * The Software IS NOT an item of Licensed Software or Licensed Product
  5709. + * under any End User Software License Agreement or Agreement for
  5710. + * Licensed Product with Synopsys or any supplement thereto. You are
  5711. + * permitted to use and redistribute this Software in source and binary
  5712. + * forms, with or without modification, provided that redistributions
  5713. + * of source code must retain this notice. You may not view, use,
  5714. + * disclose, copy or distribute this file or any information contained
  5715. + * herein except pursuant to this license grant from Synopsys. If you
  5716. + * do not agree with this notice, including the disclaimer below, then
  5717. + * you are not authorized to use the Software.
  5718. + *
  5719. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  5720. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5721. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  5722. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  5723. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5724. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5725. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5726. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  5727. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5728. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  5729. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  5730. + * DAMAGE.
  5731. + * ========================================================================= */
  5732. +#ifndef _DWC_CC_H_
  5733. +#define _DWC_CC_H_
  5734. +
  5735. +#ifdef __cplusplus
  5736. +extern "C" {
  5737. +#endif
  5738. +
  5739. +/** @file
  5740. + *
  5741. + * This file defines the Context Context library.
  5742. + *
  5743. + * The main data structure is dwc_cc_if_t which is returned by either the
  5744. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  5745. + * function. The data structure is opaque and should only be manipulated via the
  5746. + * functions provied in this API.
  5747. + *
  5748. + * It manages a list of connection contexts and operations can be performed to
  5749. + * add, remove, query, search, and change, those contexts. Additionally,
  5750. + * a dwc_notifier_t object can be requested from the manager so that
  5751. + * the user can be notified whenever the context list has changed.
  5752. + */
  5753. +
  5754. +#include "dwc_os.h"
  5755. +#include "dwc_list.h"
  5756. +#include "dwc_notifier.h"
  5757. +
  5758. +
  5759. +/* Notifications */
  5760. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  5761. +
  5762. +struct dwc_cc_if;
  5763. +typedef struct dwc_cc_if dwc_cc_if_t;
  5764. +
  5765. +
  5766. +/** @name Connection Context Operations */
  5767. +/** @{ */
  5768. +
  5769. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  5770. + * fields to default values, and returns a pointer to the structure or NULL on
  5771. + * error. */
  5772. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  5773. + dwc_notifier_t *notifier, unsigned is_host);
  5774. +
  5775. +/** Frees the memory for the specified CC structure allocated from
  5776. + * dwc_cc_if_alloc(). */
  5777. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  5778. +
  5779. +/** Removes all contexts from the connection context list */
  5780. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  5781. +
  5782. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  5783. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  5784. + * not overwritten.
  5785. + *
  5786. + * @param cc_if The cc_if structure.
  5787. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  5788. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  5789. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  5790. + * @param name An optional host friendly name as defined in the association model
  5791. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  5792. + * @param length The length othe unicode string.
  5793. + * @return A unique identifier used to refer to this context that is valid for
  5794. + * as long as this context is still in the list. */
  5795. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5796. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  5797. + uint8_t length);
  5798. +
  5799. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  5800. + * list, preserving any accumulated statistics. This would typically be called
  5801. + * if the host decideds to change the context with a SET_CONNECTION request.
  5802. + *
  5803. + * @param cc_if The cc_if structure.
  5804. + * @param id The identifier of the connection context.
  5805. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  5806. + * indicates no change.
  5807. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  5808. + * indicates no change.
  5809. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  5810. + * indicates no change.
  5811. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  5812. + * @param length Length of name. */
  5813. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  5814. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  5815. + uint8_t *name, uint8_t length);
  5816. +
  5817. +/** Remove the specified connection context.
  5818. + * @param cc_if The cc_if structure.
  5819. + * @param id The identifier of the connection context to remove. */
  5820. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  5821. +
  5822. +/** Get a binary block of data for the connection context list and attributes.
  5823. + * This data can be used by the OS specific driver to save the connection
  5824. + * context list into non-volatile memory.
  5825. + *
  5826. + * @param cc_if The cc_if structure.
  5827. + * @param length Return the length of the data buffer.
  5828. + * @return A pointer to the data buffer. The memory for this buffer should be
  5829. + * freed with DWC_FREE() after use. */
  5830. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  5831. + unsigned int *length);
  5832. +
  5833. +/** Restore the connection context list from the binary data that was previously
  5834. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  5835. + * driver to load a connection context list from non-volatile memory.
  5836. + *
  5837. + * @param cc_if The cc_if structure.
  5838. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  5839. + * @param length The length of the data. */
  5840. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  5841. + uint8_t *data, unsigned int length);
  5842. +
  5843. +/** Find the connection context from the specified CHID.
  5844. + *
  5845. + * @param cc_if The cc_if structure.
  5846. + * @param chid A pointer to the CHID data.
  5847. + * @return A non-zero identifier of the connection context if the CHID matches.
  5848. + * Otherwise returns 0. */
  5849. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  5850. +
  5851. +/** Find the connection context from the specified CDID.
  5852. + *
  5853. + * @param cc_if The cc_if structure.
  5854. + * @param cdid A pointer to the CDID data.
  5855. + * @return A non-zero identifier of the connection context if the CHID matches.
  5856. + * Otherwise returns 0. */
  5857. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  5858. +
  5859. +/** Retrieve the CK from the specified connection context.
  5860. + *
  5861. + * @param cc_if The cc_if structure.
  5862. + * @param id The identifier of the connection context.
  5863. + * @return A pointer to the CK data. The memory does not need to be freed. */
  5864. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  5865. +
  5866. +/** Retrieve the CHID from the specified connection context.
  5867. + *
  5868. + * @param cc_if The cc_if structure.
  5869. + * @param id The identifier of the connection context.
  5870. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  5871. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  5872. +
  5873. +/** Retrieve the CDID from the specified connection context.
  5874. + *
  5875. + * @param cc_if The cc_if structure.
  5876. + * @param id The identifier of the connection context.
  5877. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  5878. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  5879. +
  5880. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  5881. +
  5882. +/** Checks a buffer for non-zero.
  5883. + * @param id A pointer to a 16 byte buffer.
  5884. + * @return true if the 16 byte value is non-zero. */
  5885. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  5886. + int i;
  5887. + for (i=0; i<16; i++) {
  5888. + if (id[i]) return 1;
  5889. + }
  5890. + return 0;
  5891. +}
  5892. +
  5893. +/** Checks a buffer for zero.
  5894. + * @param id A pointer to a 16 byte buffer.
  5895. + * @return true if the 16 byte value is zero. */
  5896. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  5897. + return !dwc_assoc_is_not_zero_id(id);
  5898. +}
  5899. +
  5900. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  5901. + * buffer. */
  5902. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  5903. + char *ptr = buffer;
  5904. + int i;
  5905. + for (i=0; i<16; i++) {
  5906. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  5907. + if (i < 15) {
  5908. + ptr += DWC_SPRINTF(ptr, " ");
  5909. + }
  5910. + }
  5911. + return ptr - buffer;
  5912. +}
  5913. +
  5914. +/** @} */
  5915. +
  5916. +#ifdef __cplusplus
  5917. +}
  5918. +#endif
  5919. +
  5920. +#endif /* _DWC_CC_H_ */
  5921. --- /dev/null
  5922. +++ b/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  5923. @@ -0,0 +1,1308 @@
  5924. +#include "dwc_os.h"
  5925. +#include "dwc_list.h"
  5926. +
  5927. +#ifdef DWC_CCLIB
  5928. +# include "dwc_cc.h"
  5929. +#endif
  5930. +
  5931. +#ifdef DWC_CRYPTOLIB
  5932. +# include "dwc_modpow.h"
  5933. +# include "dwc_dh.h"
  5934. +# include "dwc_crypto.h"
  5935. +#endif
  5936. +
  5937. +#ifdef DWC_NOTIFYLIB
  5938. +# include "dwc_notifier.h"
  5939. +#endif
  5940. +
  5941. +/* OS-Level Implementations */
  5942. +
  5943. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  5944. +
  5945. +
  5946. +/* MISC */
  5947. +
  5948. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  5949. +{
  5950. + return memset(dest, byte, size);
  5951. +}
  5952. +
  5953. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  5954. +{
  5955. + return memcpy(dest, src, size);
  5956. +}
  5957. +
  5958. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  5959. +{
  5960. + bcopy(src, dest, size);
  5961. + return dest;
  5962. +}
  5963. +
  5964. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  5965. +{
  5966. + return memcmp(m1, m2, size);
  5967. +}
  5968. +
  5969. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  5970. +{
  5971. + return strncmp(s1, s2, size);
  5972. +}
  5973. +
  5974. +int DWC_STRCMP(void *s1, void *s2)
  5975. +{
  5976. + return strcmp(s1, s2);
  5977. +}
  5978. +
  5979. +int DWC_STRLEN(char const *str)
  5980. +{
  5981. + return strlen(str);
  5982. +}
  5983. +
  5984. +char *DWC_STRCPY(char *to, char const *from)
  5985. +{
  5986. + return strcpy(to, from);
  5987. +}
  5988. +
  5989. +char *DWC_STRDUP(char const *str)
  5990. +{
  5991. + int len = DWC_STRLEN(str) + 1;
  5992. + char *new = DWC_ALLOC_ATOMIC(len);
  5993. +
  5994. + if (!new) {
  5995. + return NULL;
  5996. + }
  5997. +
  5998. + DWC_MEMCPY(new, str, len);
  5999. + return new;
  6000. +}
  6001. +
  6002. +int DWC_ATOI(char *str, int32_t *value)
  6003. +{
  6004. + char *end = NULL;
  6005. +
  6006. + *value = strtol(str, &end, 0);
  6007. + if (*end == '\0') {
  6008. + return 0;
  6009. + }
  6010. +
  6011. + return -1;
  6012. +}
  6013. +
  6014. +int DWC_ATOUI(char *str, uint32_t *value)
  6015. +{
  6016. + char *end = NULL;
  6017. +
  6018. + *value = strtoul(str, &end, 0);
  6019. + if (*end == '\0') {
  6020. + return 0;
  6021. + }
  6022. +
  6023. + return -1;
  6024. +}
  6025. +
  6026. +
  6027. +#ifdef DWC_UTFLIB
  6028. +/* From usbstring.c */
  6029. +
  6030. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  6031. +{
  6032. + int count = 0;
  6033. + u8 c;
  6034. + u16 uchar;
  6035. +
  6036. + /* this insists on correct encodings, though not minimal ones.
  6037. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  6038. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  6039. + */
  6040. + while (len != 0 && (c = (u8) *s++) != 0) {
  6041. + if (unlikely(c & 0x80)) {
  6042. + // 2-byte sequence:
  6043. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  6044. + if ((c & 0xe0) == 0xc0) {
  6045. + uchar = (c & 0x1f) << 6;
  6046. +
  6047. + c = (u8) *s++;
  6048. + if ((c & 0xc0) != 0xc0)
  6049. + goto fail;
  6050. + c &= 0x3f;
  6051. + uchar |= c;
  6052. +
  6053. + // 3-byte sequence (most CJKV characters):
  6054. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  6055. + } else if ((c & 0xf0) == 0xe0) {
  6056. + uchar = (c & 0x0f) << 12;
  6057. +
  6058. + c = (u8) *s++;
  6059. + if ((c & 0xc0) != 0xc0)
  6060. + goto fail;
  6061. + c &= 0x3f;
  6062. + uchar |= c << 6;
  6063. +
  6064. + c = (u8) *s++;
  6065. + if ((c & 0xc0) != 0xc0)
  6066. + goto fail;
  6067. + c &= 0x3f;
  6068. + uchar |= c;
  6069. +
  6070. + /* no bogus surrogates */
  6071. + if (0xd800 <= uchar && uchar <= 0xdfff)
  6072. + goto fail;
  6073. +
  6074. + // 4-byte sequence (surrogate pairs, currently rare):
  6075. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  6076. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  6077. + // (uuuuu = wwww + 1)
  6078. + // FIXME accept the surrogate code points (only)
  6079. + } else
  6080. + goto fail;
  6081. + } else
  6082. + uchar = c;
  6083. + put_unaligned (cpu_to_le16 (uchar), cp++);
  6084. + count++;
  6085. + len--;
  6086. + }
  6087. + return count;
  6088. +fail:
  6089. + return -1;
  6090. +}
  6091. +
  6092. +#endif /* DWC_UTFLIB */
  6093. +
  6094. +
  6095. +/* dwc_debug.h */
  6096. +
  6097. +dwc_bool_t DWC_IN_IRQ(void)
  6098. +{
  6099. +// return in_irq();
  6100. + return 0;
  6101. +}
  6102. +
  6103. +dwc_bool_t DWC_IN_BH(void)
  6104. +{
  6105. +// return in_softirq();
  6106. + return 0;
  6107. +}
  6108. +
  6109. +void DWC_VPRINTF(char *format, va_list args)
  6110. +{
  6111. + vprintf(format, args);
  6112. +}
  6113. +
  6114. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  6115. +{
  6116. + return vsnprintf(str, size, format, args);
  6117. +}
  6118. +
  6119. +void DWC_PRINTF(char *format, ...)
  6120. +{
  6121. + va_list args;
  6122. +
  6123. + va_start(args, format);
  6124. + DWC_VPRINTF(format, args);
  6125. + va_end(args);
  6126. +}
  6127. +
  6128. +int DWC_SPRINTF(char *buffer, char *format, ...)
  6129. +{
  6130. + int retval;
  6131. + va_list args;
  6132. +
  6133. + va_start(args, format);
  6134. + retval = vsprintf(buffer, format, args);
  6135. + va_end(args);
  6136. + return retval;
  6137. +}
  6138. +
  6139. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  6140. +{
  6141. + int retval;
  6142. + va_list args;
  6143. +
  6144. + va_start(args, format);
  6145. + retval = vsnprintf(buffer, size, format, args);
  6146. + va_end(args);
  6147. + return retval;
  6148. +}
  6149. +
  6150. +void __DWC_WARN(char *format, ...)
  6151. +{
  6152. + va_list args;
  6153. +
  6154. + va_start(args, format);
  6155. + DWC_VPRINTF(format, args);
  6156. + va_end(args);
  6157. +}
  6158. +
  6159. +void __DWC_ERROR(char *format, ...)
  6160. +{
  6161. + va_list args;
  6162. +
  6163. + va_start(args, format);
  6164. + DWC_VPRINTF(format, args);
  6165. + va_end(args);
  6166. +}
  6167. +
  6168. +void DWC_EXCEPTION(char *format, ...)
  6169. +{
  6170. + va_list args;
  6171. +
  6172. + va_start(args, format);
  6173. + DWC_VPRINTF(format, args);
  6174. + va_end(args);
  6175. +// BUG_ON(1); ???
  6176. +}
  6177. +
  6178. +#ifdef DEBUG
  6179. +void __DWC_DEBUG(char *format, ...)
  6180. +{
  6181. + va_list args;
  6182. +
  6183. + va_start(args, format);
  6184. + DWC_VPRINTF(format, args);
  6185. + va_end(args);
  6186. +}
  6187. +#endif
  6188. +
  6189. +
  6190. +/* dwc_mem.h */
  6191. +
  6192. +#if 0
  6193. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  6194. + uint32_t align,
  6195. + uint32_t alloc)
  6196. +{
  6197. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  6198. + size, align, alloc);
  6199. + return (dwc_pool_t *)pool;
  6200. +}
  6201. +
  6202. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  6203. +{
  6204. + dma_pool_destroy((struct dma_pool *)pool);
  6205. +}
  6206. +
  6207. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  6208. +{
  6209. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  6210. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  6211. +}
  6212. +
  6213. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  6214. +{
  6215. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  6216. + memset(..);
  6217. +}
  6218. +
  6219. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  6220. +{
  6221. + dma_pool_free(pool, vaddr, daddr);
  6222. +}
  6223. +#endif
  6224. +
  6225. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  6226. +{
  6227. + if (error)
  6228. + return;
  6229. + *(bus_addr_t *)arg = segs[0].ds_addr;
  6230. +}
  6231. +
  6232. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  6233. +{
  6234. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  6235. + int error;
  6236. +
  6237. + error = bus_dma_tag_create(
  6238. +#if __FreeBSD_version >= 700000
  6239. + bus_get_dma_tag(dma->dev), /* parent */
  6240. +#else
  6241. + NULL, /* parent */
  6242. +#endif
  6243. + 4, 0, /* alignment, bounds */
  6244. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  6245. + BUS_SPACE_MAXADDR, /* highaddr */
  6246. + NULL, NULL, /* filter, filterarg */
  6247. + size, /* maxsize */
  6248. + 1, /* nsegments */
  6249. + size, /* maxsegsize */
  6250. + 0, /* flags */
  6251. + NULL, /* lockfunc */
  6252. + NULL, /* lockarg */
  6253. + &dma->dma_tag);
  6254. + if (error) {
  6255. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  6256. + __func__, error);
  6257. + goto fail_0;
  6258. + }
  6259. +
  6260. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  6261. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  6262. + if (error) {
  6263. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  6264. + __func__, (uintmax_t)size, error);
  6265. + goto fail_1;
  6266. + }
  6267. +
  6268. + dma->dma_paddr = 0;
  6269. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  6270. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  6271. + if (error || dma->dma_paddr == 0) {
  6272. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  6273. + __func__, error);
  6274. + goto fail_2;
  6275. + }
  6276. +
  6277. + *dma_addr = dma->dma_paddr;
  6278. + return dma->dma_vaddr;
  6279. +
  6280. +fail_2:
  6281. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  6282. +fail_1:
  6283. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  6284. + bus_dma_tag_destroy(dma->dma_tag);
  6285. +fail_0:
  6286. + dma->dma_map = NULL;
  6287. + dma->dma_tag = NULL;
  6288. +
  6289. + return NULL;
  6290. +}
  6291. +
  6292. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  6293. +{
  6294. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  6295. +
  6296. + if (dma->dma_tag == NULL)
  6297. + return;
  6298. + if (dma->dma_map != NULL) {
  6299. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  6300. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  6301. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  6302. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  6303. + dma->dma_map = NULL;
  6304. + }
  6305. +
  6306. + bus_dma_tag_destroy(dma->dma_tag);
  6307. + dma->dma_tag = NULL;
  6308. +}
  6309. +
  6310. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  6311. +{
  6312. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  6313. +}
  6314. +
  6315. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  6316. +{
  6317. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  6318. +}
  6319. +
  6320. +void __DWC_FREE(void *mem_ctx, void *addr)
  6321. +{
  6322. + free(addr, M_DEVBUF);
  6323. +}
  6324. +
  6325. +
  6326. +#ifdef DWC_CRYPTOLIB
  6327. +/* dwc_crypto.h */
  6328. +
  6329. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  6330. +{
  6331. + get_random_bytes(buffer, length);
  6332. +}
  6333. +
  6334. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  6335. +{
  6336. + struct crypto_blkcipher *tfm;
  6337. + struct blkcipher_desc desc;
  6338. + struct scatterlist sgd;
  6339. + struct scatterlist sgs;
  6340. +
  6341. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  6342. + if (tfm == NULL) {
  6343. + printk("failed to load transform for aes CBC\n");
  6344. + return -1;
  6345. + }
  6346. +
  6347. + crypto_blkcipher_setkey(tfm, key, keylen);
  6348. + crypto_blkcipher_set_iv(tfm, iv, 16);
  6349. +
  6350. + sg_init_one(&sgd, out, messagelen);
  6351. + sg_init_one(&sgs, message, messagelen);
  6352. +
  6353. + desc.tfm = tfm;
  6354. + desc.flags = 0;
  6355. +
  6356. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  6357. + crypto_free_blkcipher(tfm);
  6358. + DWC_ERROR("AES CBC encryption failed");
  6359. + return -1;
  6360. + }
  6361. +
  6362. + crypto_free_blkcipher(tfm);
  6363. + return 0;
  6364. +}
  6365. +
  6366. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  6367. +{
  6368. + struct crypto_hash *tfm;
  6369. + struct hash_desc desc;
  6370. + struct scatterlist sg;
  6371. +
  6372. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  6373. + if (IS_ERR(tfm)) {
  6374. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  6375. + return 0;
  6376. + }
  6377. + desc.tfm = tfm;
  6378. + desc.flags = 0;
  6379. +
  6380. + sg_init_one(&sg, message, len);
  6381. + crypto_hash_digest(&desc, &sg, len, out);
  6382. + crypto_free_hash(tfm);
  6383. +
  6384. + return 1;
  6385. +}
  6386. +
  6387. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  6388. + uint8_t *key, uint32_t keylen, uint8_t *out)
  6389. +{
  6390. + struct crypto_hash *tfm;
  6391. + struct hash_desc desc;
  6392. + struct scatterlist sg;
  6393. +
  6394. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  6395. + if (IS_ERR(tfm)) {
  6396. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  6397. + return 0;
  6398. + }
  6399. + desc.tfm = tfm;
  6400. + desc.flags = 0;
  6401. +
  6402. + sg_init_one(&sg, message, messagelen);
  6403. + crypto_hash_setkey(tfm, key, keylen);
  6404. + crypto_hash_digest(&desc, &sg, messagelen, out);
  6405. + crypto_free_hash(tfm);
  6406. +
  6407. + return 1;
  6408. +}
  6409. +
  6410. +#endif /* DWC_CRYPTOLIB */
  6411. +
  6412. +
  6413. +/* Byte Ordering Conversions */
  6414. +
  6415. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  6416. +{
  6417. +#ifdef __LITTLE_ENDIAN
  6418. + return *p;
  6419. +#else
  6420. + uint8_t *u_p = (uint8_t *)p;
  6421. +
  6422. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6423. +#endif
  6424. +}
  6425. +
  6426. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  6427. +{
  6428. +#ifdef __BIG_ENDIAN
  6429. + return *p;
  6430. +#else
  6431. + uint8_t *u_p = (uint8_t *)p;
  6432. +
  6433. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6434. +#endif
  6435. +}
  6436. +
  6437. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  6438. +{
  6439. +#ifdef __LITTLE_ENDIAN
  6440. + return *p;
  6441. +#else
  6442. + uint8_t *u_p = (uint8_t *)p;
  6443. +
  6444. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6445. +#endif
  6446. +}
  6447. +
  6448. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  6449. +{
  6450. +#ifdef __BIG_ENDIAN
  6451. + return *p;
  6452. +#else
  6453. + uint8_t *u_p = (uint8_t *)p;
  6454. +
  6455. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6456. +#endif
  6457. +}
  6458. +
  6459. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  6460. +{
  6461. +#ifdef __LITTLE_ENDIAN
  6462. + return *p;
  6463. +#else
  6464. + uint8_t *u_p = (uint8_t *)p;
  6465. + return (u_p[1] | (u_p[0] << 8));
  6466. +#endif
  6467. +}
  6468. +
  6469. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  6470. +{
  6471. +#ifdef __BIG_ENDIAN
  6472. + return *p;
  6473. +#else
  6474. + uint8_t *u_p = (uint8_t *)p;
  6475. + return (u_p[1] | (u_p[0] << 8));
  6476. +#endif
  6477. +}
  6478. +
  6479. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  6480. +{
  6481. +#ifdef __LITTLE_ENDIAN
  6482. + return *p;
  6483. +#else
  6484. + uint8_t *u_p = (uint8_t *)p;
  6485. + return (u_p[1] | (u_p[0] << 8));
  6486. +#endif
  6487. +}
  6488. +
  6489. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  6490. +{
  6491. +#ifdef __BIG_ENDIAN
  6492. + return *p;
  6493. +#else
  6494. + uint8_t *u_p = (uint8_t *)p;
  6495. + return (u_p[1] | (u_p[0] << 8));
  6496. +#endif
  6497. +}
  6498. +
  6499. +
  6500. +/* Registers */
  6501. +
  6502. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  6503. +{
  6504. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6505. + bus_size_t ior = (bus_size_t)reg;
  6506. +
  6507. + return bus_space_read_4(io->iot, io->ioh, ior);
  6508. +}
  6509. +
  6510. +#if 0
  6511. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  6512. +{
  6513. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6514. + bus_size_t ior = (bus_size_t)reg;
  6515. +
  6516. + return bus_space_read_8(io->iot, io->ioh, ior);
  6517. +}
  6518. +#endif
  6519. +
  6520. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  6521. +{
  6522. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6523. + bus_size_t ior = (bus_size_t)reg;
  6524. +
  6525. + bus_space_write_4(io->iot, io->ioh, ior, value);
  6526. +}
  6527. +
  6528. +#if 0
  6529. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  6530. +{
  6531. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6532. + bus_size_t ior = (bus_size_t)reg;
  6533. +
  6534. + bus_space_write_8(io->iot, io->ioh, ior, value);
  6535. +}
  6536. +#endif
  6537. +
  6538. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  6539. + uint32_t set_mask)
  6540. +{
  6541. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6542. + bus_size_t ior = (bus_size_t)reg;
  6543. +
  6544. + bus_space_write_4(io->iot, io->ioh, ior,
  6545. + (bus_space_read_4(io->iot, io->ioh, ior) &
  6546. + ~clear_mask) | set_mask);
  6547. +}
  6548. +
  6549. +#if 0
  6550. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  6551. + uint64_t set_mask)
  6552. +{
  6553. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6554. + bus_size_t ior = (bus_size_t)reg;
  6555. +
  6556. + bus_space_write_8(io->iot, io->ioh, ior,
  6557. + (bus_space_read_8(io->iot, io->ioh, ior) &
  6558. + ~clear_mask) | set_mask);
  6559. +}
  6560. +#endif
  6561. +
  6562. +
  6563. +/* Locking */
  6564. +
  6565. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  6566. +{
  6567. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  6568. +
  6569. + if (!sl) {
  6570. + DWC_ERROR("Cannot allocate memory for spinlock");
  6571. + return NULL;
  6572. + }
  6573. +
  6574. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  6575. + return (dwc_spinlock_t *)sl;
  6576. +}
  6577. +
  6578. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  6579. +{
  6580. + struct mtx *sl = (struct mtx *)lock;
  6581. +
  6582. + mtx_destroy(sl);
  6583. + DWC_FREE(sl);
  6584. +}
  6585. +
  6586. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  6587. +{
  6588. + mtx_lock_spin((struct mtx *)lock); // ???
  6589. +}
  6590. +
  6591. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  6592. +{
  6593. + mtx_unlock_spin((struct mtx *)lock); // ???
  6594. +}
  6595. +
  6596. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  6597. +{
  6598. + mtx_lock_spin((struct mtx *)lock);
  6599. +}
  6600. +
  6601. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  6602. +{
  6603. + mtx_unlock_spin((struct mtx *)lock);
  6604. +}
  6605. +
  6606. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  6607. +{
  6608. + struct mtx *m;
  6609. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  6610. +
  6611. + if (!mutex) {
  6612. + DWC_ERROR("Cannot allocate memory for mutex");
  6613. + return NULL;
  6614. + }
  6615. +
  6616. + m = (struct mtx *)mutex;
  6617. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  6618. + return mutex;
  6619. +}
  6620. +
  6621. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  6622. +#else
  6623. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  6624. +{
  6625. + mtx_destroy((struct mtx *)mutex);
  6626. + DWC_FREE(mutex);
  6627. +}
  6628. +#endif
  6629. +
  6630. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  6631. +{
  6632. + struct mtx *m = (struct mtx *)mutex;
  6633. +
  6634. + mtx_lock(m);
  6635. +}
  6636. +
  6637. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  6638. +{
  6639. + struct mtx *m = (struct mtx *)mutex;
  6640. +
  6641. + return mtx_trylock(m);
  6642. +}
  6643. +
  6644. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  6645. +{
  6646. + struct mtx *m = (struct mtx *)mutex;
  6647. +
  6648. + mtx_unlock(m);
  6649. +}
  6650. +
  6651. +
  6652. +/* Timing */
  6653. +
  6654. +void DWC_UDELAY(uint32_t usecs)
  6655. +{
  6656. + DELAY(usecs);
  6657. +}
  6658. +
  6659. +void DWC_MDELAY(uint32_t msecs)
  6660. +{
  6661. + do {
  6662. + DELAY(1000);
  6663. + } while (--msecs);
  6664. +}
  6665. +
  6666. +void DWC_MSLEEP(uint32_t msecs)
  6667. +{
  6668. + struct timeval tv;
  6669. +
  6670. + tv.tv_sec = msecs / 1000;
  6671. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  6672. + pause("dw3slp", tvtohz(&tv));
  6673. +}
  6674. +
  6675. +uint32_t DWC_TIME(void)
  6676. +{
  6677. + struct timeval tv;
  6678. +
  6679. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  6680. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  6681. +}
  6682. +
  6683. +
  6684. +/* Timers */
  6685. +
  6686. +struct dwc_timer {
  6687. + struct callout t;
  6688. + char *name;
  6689. + dwc_spinlock_t *lock;
  6690. + dwc_timer_callback_t cb;
  6691. + void *data;
  6692. +};
  6693. +
  6694. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  6695. +{
  6696. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  6697. +
  6698. + if (!t) {
  6699. + DWC_ERROR("Cannot allocate memory for timer");
  6700. + return NULL;
  6701. + }
  6702. +
  6703. + callout_init(&t->t, 1);
  6704. +
  6705. + t->name = DWC_STRDUP(name);
  6706. + if (!t->name) {
  6707. + DWC_ERROR("Cannot allocate memory for timer->name");
  6708. + goto no_name;
  6709. + }
  6710. +
  6711. + t->lock = DWC_SPINLOCK_ALLOC();
  6712. + if (!t->lock) {
  6713. + DWC_ERROR("Cannot allocate memory for lock");
  6714. + goto no_lock;
  6715. + }
  6716. +
  6717. + t->cb = cb;
  6718. + t->data = data;
  6719. +
  6720. + return t;
  6721. +
  6722. + no_lock:
  6723. + DWC_FREE(t->name);
  6724. + no_name:
  6725. + DWC_FREE(t);
  6726. +
  6727. + return NULL;
  6728. +}
  6729. +
  6730. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  6731. +{
  6732. + callout_stop(&timer->t);
  6733. + DWC_SPINLOCK_FREE(timer->lock);
  6734. + DWC_FREE(timer->name);
  6735. + DWC_FREE(timer);
  6736. +}
  6737. +
  6738. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  6739. +{
  6740. + struct timeval tv;
  6741. +
  6742. + tv.tv_sec = time / 1000;
  6743. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  6744. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  6745. +}
  6746. +
  6747. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  6748. +{
  6749. + callout_stop(&timer->t);
  6750. +}
  6751. +
  6752. +
  6753. +/* Wait Queues */
  6754. +
  6755. +struct dwc_waitq {
  6756. + struct mtx lock;
  6757. + int abort;
  6758. +};
  6759. +
  6760. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  6761. +{
  6762. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  6763. +
  6764. + if (!wq) {
  6765. + DWC_ERROR("Cannot allocate memory for waitqueue");
  6766. + return NULL;
  6767. + }
  6768. +
  6769. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  6770. + wq->abort = 0;
  6771. +
  6772. + return wq;
  6773. +}
  6774. +
  6775. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  6776. +{
  6777. + mtx_destroy(&wq->lock);
  6778. + DWC_FREE(wq);
  6779. +}
  6780. +
  6781. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  6782. +{
  6783. +// intrmask_t ipl;
  6784. + int result = 0;
  6785. +
  6786. + mtx_lock(&wq->lock);
  6787. +// ipl = splbio();
  6788. +
  6789. + /* Skip the sleep if already aborted or triggered */
  6790. + if (!wq->abort && !cond(data)) {
  6791. +// splx(ipl);
  6792. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  6793. +// ipl = splbio();
  6794. + }
  6795. +
  6796. + if (result == ERESTART) { // signaled - restart
  6797. + result = -DWC_E_RESTART;
  6798. +
  6799. + } else if (result == EINTR) { // signaled - interrupt
  6800. + result = -DWC_E_ABORT;
  6801. +
  6802. + } else if (wq->abort) {
  6803. + result = -DWC_E_ABORT;
  6804. +
  6805. + } else {
  6806. + result = 0;
  6807. + }
  6808. +
  6809. + wq->abort = 0;
  6810. +// splx(ipl);
  6811. + mtx_unlock(&wq->lock);
  6812. + return result;
  6813. +}
  6814. +
  6815. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  6816. + void *data, int32_t msecs)
  6817. +{
  6818. + struct timeval tv, tv1, tv2;
  6819. +// intrmask_t ipl;
  6820. + int result = 0;
  6821. +
  6822. + tv.tv_sec = msecs / 1000;
  6823. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  6824. +
  6825. + mtx_lock(&wq->lock);
  6826. +// ipl = splbio();
  6827. +
  6828. + /* Skip the sleep if already aborted or triggered */
  6829. + if (!wq->abort && !cond(data)) {
  6830. +// splx(ipl);
  6831. + getmicrouptime(&tv1);
  6832. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  6833. + getmicrouptime(&tv2);
  6834. +// ipl = splbio();
  6835. + }
  6836. +
  6837. + if (result == 0) { // awoken
  6838. + if (wq->abort) {
  6839. + result = -DWC_E_ABORT;
  6840. + } else {
  6841. + tv2.tv_usec -= tv1.tv_usec;
  6842. + if (tv2.tv_usec < 0) {
  6843. + tv2.tv_usec += 1000000;
  6844. + tv2.tv_sec--;
  6845. + }
  6846. +
  6847. + tv2.tv_sec -= tv1.tv_sec;
  6848. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  6849. + result = msecs - result;
  6850. + if (result <= 0)
  6851. + result = 1;
  6852. + }
  6853. + } else if (result == ERESTART) { // signaled - restart
  6854. + result = -DWC_E_RESTART;
  6855. +
  6856. + } else if (result == EINTR) { // signaled - interrupt
  6857. + result = -DWC_E_ABORT;
  6858. +
  6859. + } else { // timed out
  6860. + result = -DWC_E_TIMEOUT;
  6861. + }
  6862. +
  6863. + wq->abort = 0;
  6864. +// splx(ipl);
  6865. + mtx_unlock(&wq->lock);
  6866. + return result;
  6867. +}
  6868. +
  6869. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  6870. +{
  6871. + wakeup(wq);
  6872. +}
  6873. +
  6874. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  6875. +{
  6876. +// intrmask_t ipl;
  6877. +
  6878. + mtx_lock(&wq->lock);
  6879. +// ipl = splbio();
  6880. + wq->abort = 1;
  6881. + wakeup(wq);
  6882. +// splx(ipl);
  6883. + mtx_unlock(&wq->lock);
  6884. +}
  6885. +
  6886. +
  6887. +/* Threading */
  6888. +
  6889. +struct dwc_thread {
  6890. + struct proc *proc;
  6891. + int abort;
  6892. +};
  6893. +
  6894. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  6895. +{
  6896. + int retval;
  6897. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  6898. +
  6899. + if (!thread) {
  6900. + return NULL;
  6901. + }
  6902. +
  6903. + thread->abort = 0;
  6904. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  6905. + RFPROC | RFNOWAIT, 0, "%s", name);
  6906. + if (retval) {
  6907. + DWC_FREE(thread);
  6908. + return NULL;
  6909. + }
  6910. +
  6911. + return thread;
  6912. +}
  6913. +
  6914. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  6915. +{
  6916. + int retval;
  6917. +
  6918. + thread->abort = 1;
  6919. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  6920. +
  6921. + if (retval == 0) {
  6922. + /* DWC_THREAD_EXIT() will free the thread struct */
  6923. + return 0;
  6924. + }
  6925. +
  6926. + /* NOTE: We leak the thread struct if thread doesn't die */
  6927. +
  6928. + if (retval == EWOULDBLOCK) {
  6929. + return -DWC_E_TIMEOUT;
  6930. + }
  6931. +
  6932. + return -DWC_E_UNKNOWN;
  6933. +}
  6934. +
  6935. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  6936. +{
  6937. + return thread->abort;
  6938. +}
  6939. +
  6940. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  6941. +{
  6942. + wakeup(&thread->abort);
  6943. + DWC_FREE(thread);
  6944. + kthread_exit(0);
  6945. +}
  6946. +
  6947. +
  6948. +/* tasklets
  6949. + - Runs in interrupt context (cannot sleep)
  6950. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  6951. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  6952. + */
  6953. +struct dwc_tasklet {
  6954. + struct task t;
  6955. + dwc_tasklet_callback_t cb;
  6956. + void *data;
  6957. +};
  6958. +
  6959. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  6960. +{
  6961. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  6962. +
  6963. + task->cb(task->data);
  6964. +}
  6965. +
  6966. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  6967. +{
  6968. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  6969. +
  6970. + if (task) {
  6971. + task->cb = cb;
  6972. + task->data = data;
  6973. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  6974. + } else {
  6975. + DWC_ERROR("Cannot allocate memory for tasklet");
  6976. + }
  6977. +
  6978. + return task;
  6979. +}
  6980. +
  6981. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  6982. +{
  6983. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  6984. + DWC_FREE(task);
  6985. +}
  6986. +
  6987. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  6988. +{
  6989. + /* Uses predefined system queue */
  6990. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  6991. +}
  6992. +
  6993. +
  6994. +/* workqueues
  6995. + - Runs in process context (can sleep)
  6996. + */
  6997. +typedef struct work_container {
  6998. + dwc_work_callback_t cb;
  6999. + void *data;
  7000. + dwc_workq_t *wq;
  7001. + char *name;
  7002. + int hz;
  7003. +
  7004. +#ifdef DEBUG
  7005. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  7006. +#endif
  7007. + struct task task;
  7008. +} work_container_t;
  7009. +
  7010. +#ifdef DEBUG
  7011. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  7012. +#endif
  7013. +
  7014. +struct dwc_workq {
  7015. + struct taskqueue *taskq;
  7016. + dwc_spinlock_t *lock;
  7017. + dwc_waitq_t *waitq;
  7018. + int pending;
  7019. +
  7020. +#ifdef DEBUG
  7021. + struct work_container_queue entries;
  7022. +#endif
  7023. +};
  7024. +
  7025. +static void do_work(void *data, int pending) // what to do with pending ???
  7026. +{
  7027. + work_container_t *container = (work_container_t *)data;
  7028. + dwc_workq_t *wq = container->wq;
  7029. + dwc_irqflags_t flags;
  7030. +
  7031. + if (container->hz) {
  7032. + pause("dw3wrk", container->hz);
  7033. + }
  7034. +
  7035. + container->cb(container->data);
  7036. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  7037. +
  7038. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7039. +
  7040. +#ifdef DEBUG
  7041. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  7042. +#endif
  7043. + if (container->name)
  7044. + DWC_FREE(container->name);
  7045. + DWC_FREE(container);
  7046. + wq->pending--;
  7047. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7048. + DWC_WAITQ_TRIGGER(wq->waitq);
  7049. +}
  7050. +
  7051. +static int work_done(void *data)
  7052. +{
  7053. + dwc_workq_t *workq = (dwc_workq_t *)data;
  7054. +
  7055. + return workq->pending == 0;
  7056. +}
  7057. +
  7058. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  7059. +{
  7060. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  7061. +}
  7062. +
  7063. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  7064. +{
  7065. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  7066. +
  7067. + if (!wq) {
  7068. + DWC_ERROR("Cannot allocate memory for workqueue");
  7069. + return NULL;
  7070. + }
  7071. +
  7072. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  7073. + if (!wq->taskq) {
  7074. + DWC_ERROR("Cannot allocate memory for taskqueue");
  7075. + goto no_taskq;
  7076. + }
  7077. +
  7078. + wq->pending = 0;
  7079. +
  7080. + wq->lock = DWC_SPINLOCK_ALLOC();
  7081. + if (!wq->lock) {
  7082. + DWC_ERROR("Cannot allocate memory for spinlock");
  7083. + goto no_lock;
  7084. + }
  7085. +
  7086. + wq->waitq = DWC_WAITQ_ALLOC();
  7087. + if (!wq->waitq) {
  7088. + DWC_ERROR("Cannot allocate memory for waitqueue");
  7089. + goto no_waitq;
  7090. + }
  7091. +
  7092. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  7093. +
  7094. +#ifdef DEBUG
  7095. + DWC_CIRCLEQ_INIT(&wq->entries);
  7096. +#endif
  7097. + return wq;
  7098. +
  7099. + no_waitq:
  7100. + DWC_SPINLOCK_FREE(wq->lock);
  7101. + no_lock:
  7102. + taskqueue_free(wq->taskq);
  7103. + no_taskq:
  7104. + DWC_FREE(wq);
  7105. +
  7106. + return NULL;
  7107. +}
  7108. +
  7109. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  7110. +{
  7111. +#ifdef DEBUG
  7112. + dwc_irqflags_t flags;
  7113. +
  7114. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7115. +
  7116. + if (wq->pending != 0) {
  7117. + struct work_container *container;
  7118. +
  7119. + DWC_ERROR("Destroying work queue with pending work");
  7120. +
  7121. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  7122. + DWC_ERROR("Work %s still pending", container->name);
  7123. + }
  7124. + }
  7125. +
  7126. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7127. +#endif
  7128. + DWC_WAITQ_FREE(wq->waitq);
  7129. + DWC_SPINLOCK_FREE(wq->lock);
  7130. + taskqueue_free(wq->taskq);
  7131. + DWC_FREE(wq);
  7132. +}
  7133. +
  7134. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  7135. + char *format, ...)
  7136. +{
  7137. + dwc_irqflags_t flags;
  7138. + work_container_t *container;
  7139. + static char name[128];
  7140. + va_list args;
  7141. +
  7142. + va_start(args, format);
  7143. + DWC_VSNPRINTF(name, 128, format, args);
  7144. + va_end(args);
  7145. +
  7146. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7147. + wq->pending++;
  7148. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7149. + DWC_WAITQ_TRIGGER(wq->waitq);
  7150. +
  7151. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  7152. + if (!container) {
  7153. + DWC_ERROR("Cannot allocate memory for container");
  7154. + return;
  7155. + }
  7156. +
  7157. + container->name = DWC_STRDUP(name);
  7158. + if (!container->name) {
  7159. + DWC_ERROR("Cannot allocate memory for container->name");
  7160. + DWC_FREE(container);
  7161. + return;
  7162. + }
  7163. +
  7164. + container->cb = cb;
  7165. + container->data = data;
  7166. + container->wq = wq;
  7167. + container->hz = 0;
  7168. +
  7169. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  7170. +
  7171. + TASK_INIT(&container->task, 0, do_work, container);
  7172. +
  7173. +#ifdef DEBUG
  7174. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  7175. +#endif
  7176. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  7177. +}
  7178. +
  7179. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  7180. + void *data, uint32_t time, char *format, ...)
  7181. +{
  7182. + dwc_irqflags_t flags;
  7183. + work_container_t *container;
  7184. + static char name[128];
  7185. + struct timeval tv;
  7186. + va_list args;
  7187. +
  7188. + va_start(args, format);
  7189. + DWC_VSNPRINTF(name, 128, format, args);
  7190. + va_end(args);
  7191. +
  7192. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7193. + wq->pending++;
  7194. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7195. + DWC_WAITQ_TRIGGER(wq->waitq);
  7196. +
  7197. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  7198. + if (!container) {
  7199. + DWC_ERROR("Cannot allocate memory for container");
  7200. + return;
  7201. + }
  7202. +
  7203. + container->name = DWC_STRDUP(name);
  7204. + if (!container->name) {
  7205. + DWC_ERROR("Cannot allocate memory for container->name");
  7206. + DWC_FREE(container);
  7207. + return;
  7208. + }
  7209. +
  7210. + container->cb = cb;
  7211. + container->data = data;
  7212. + container->wq = wq;
  7213. +
  7214. + tv.tv_sec = time / 1000;
  7215. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  7216. + container->hz = tvtohz(&tv);
  7217. +
  7218. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  7219. +
  7220. + TASK_INIT(&container->task, 0, do_work, container);
  7221. +
  7222. +#ifdef DEBUG
  7223. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  7224. +#endif
  7225. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  7226. +}
  7227. +
  7228. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  7229. +{
  7230. + return wq->pending;
  7231. +}
  7232. --- /dev/null
  7233. +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  7234. @@ -0,0 +1,1418 @@
  7235. +#include <linux/kernel.h>
  7236. +#include <linux/init.h>
  7237. +#include <linux/module.h>
  7238. +#include <linux/kthread.h>
  7239. +
  7240. +#ifdef DWC_CCLIB
  7241. +# include "dwc_cc.h"
  7242. +#endif
  7243. +
  7244. +#ifdef DWC_CRYPTOLIB
  7245. +# include "dwc_modpow.h"
  7246. +# include "dwc_dh.h"
  7247. +# include "dwc_crypto.h"
  7248. +#endif
  7249. +
  7250. +#ifdef DWC_NOTIFYLIB
  7251. +# include "dwc_notifier.h"
  7252. +#endif
  7253. +
  7254. +/* OS-Level Implementations */
  7255. +
  7256. +/* This is the Linux kernel implementation of the DWC platform library. */
  7257. +#include <linux/moduleparam.h>
  7258. +#include <linux/ctype.h>
  7259. +#include <linux/crypto.h>
  7260. +#include <linux/delay.h>
  7261. +#include <linux/device.h>
  7262. +#include <linux/dma-mapping.h>
  7263. +#include <linux/cdev.h>
  7264. +#include <linux/errno.h>
  7265. +#include <linux/interrupt.h>
  7266. +#include <linux/jiffies.h>
  7267. +#include <linux/list.h>
  7268. +#include <linux/pci.h>
  7269. +#include <linux/random.h>
  7270. +#include <linux/scatterlist.h>
  7271. +#include <linux/slab.h>
  7272. +#include <linux/stat.h>
  7273. +#include <linux/string.h>
  7274. +#include <linux/timer.h>
  7275. +#include <linux/usb.h>
  7276. +
  7277. +#include <linux/version.h>
  7278. +
  7279. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  7280. +# include <linux/usb/gadget.h>
  7281. +#else
  7282. +# include <linux/usb_gadget.h>
  7283. +#endif
  7284. +
  7285. +#include <asm/io.h>
  7286. +#include <asm/page.h>
  7287. +#include <asm/uaccess.h>
  7288. +#include <asm/unaligned.h>
  7289. +
  7290. +#include "dwc_os.h"
  7291. +#include "dwc_list.h"
  7292. +
  7293. +
  7294. +/* MISC */
  7295. +
  7296. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  7297. +{
  7298. + return memset(dest, byte, size);
  7299. +}
  7300. +
  7301. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  7302. +{
  7303. + return memcpy(dest, src, size);
  7304. +}
  7305. +
  7306. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  7307. +{
  7308. + return memmove(dest, src, size);
  7309. +}
  7310. +
  7311. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  7312. +{
  7313. + return memcmp(m1, m2, size);
  7314. +}
  7315. +
  7316. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  7317. +{
  7318. + return strncmp(s1, s2, size);
  7319. +}
  7320. +
  7321. +int DWC_STRCMP(void *s1, void *s2)
  7322. +{
  7323. + return strcmp(s1, s2);
  7324. +}
  7325. +
  7326. +int DWC_STRLEN(char const *str)
  7327. +{
  7328. + return strlen(str);
  7329. +}
  7330. +
  7331. +char *DWC_STRCPY(char *to, char const *from)
  7332. +{
  7333. + return strcpy(to, from);
  7334. +}
  7335. +
  7336. +char *DWC_STRDUP(char const *str)
  7337. +{
  7338. + int len = DWC_STRLEN(str) + 1;
  7339. + char *new = DWC_ALLOC_ATOMIC(len);
  7340. +
  7341. + if (!new) {
  7342. + return NULL;
  7343. + }
  7344. +
  7345. + DWC_MEMCPY(new, str, len);
  7346. + return new;
  7347. +}
  7348. +
  7349. +int DWC_ATOI(const char *str, int32_t *value)
  7350. +{
  7351. + char *end = NULL;
  7352. +
  7353. + *value = simple_strtol(str, &end, 0);
  7354. + if (*end == '\0') {
  7355. + return 0;
  7356. + }
  7357. +
  7358. + return -1;
  7359. +}
  7360. +
  7361. +int DWC_ATOUI(const char *str, uint32_t *value)
  7362. +{
  7363. + char *end = NULL;
  7364. +
  7365. + *value = simple_strtoul(str, &end, 0);
  7366. + if (*end == '\0') {
  7367. + return 0;
  7368. + }
  7369. +
  7370. + return -1;
  7371. +}
  7372. +
  7373. +
  7374. +#ifdef DWC_UTFLIB
  7375. +/* From usbstring.c */
  7376. +
  7377. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  7378. +{
  7379. + int count = 0;
  7380. + u8 c;
  7381. + u16 uchar;
  7382. +
  7383. + /* this insists on correct encodings, though not minimal ones.
  7384. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  7385. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  7386. + */
  7387. + while (len != 0 && (c = (u8) *s++) != 0) {
  7388. + if (unlikely(c & 0x80)) {
  7389. + // 2-byte sequence:
  7390. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  7391. + if ((c & 0xe0) == 0xc0) {
  7392. + uchar = (c & 0x1f) << 6;
  7393. +
  7394. + c = (u8) *s++;
  7395. + if ((c & 0xc0) != 0xc0)
  7396. + goto fail;
  7397. + c &= 0x3f;
  7398. + uchar |= c;
  7399. +
  7400. + // 3-byte sequence (most CJKV characters):
  7401. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  7402. + } else if ((c & 0xf0) == 0xe0) {
  7403. + uchar = (c & 0x0f) << 12;
  7404. +
  7405. + c = (u8) *s++;
  7406. + if ((c & 0xc0) != 0xc0)
  7407. + goto fail;
  7408. + c &= 0x3f;
  7409. + uchar |= c << 6;
  7410. +
  7411. + c = (u8) *s++;
  7412. + if ((c & 0xc0) != 0xc0)
  7413. + goto fail;
  7414. + c &= 0x3f;
  7415. + uchar |= c;
  7416. +
  7417. + /* no bogus surrogates */
  7418. + if (0xd800 <= uchar && uchar <= 0xdfff)
  7419. + goto fail;
  7420. +
  7421. + // 4-byte sequence (surrogate pairs, currently rare):
  7422. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  7423. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  7424. + // (uuuuu = wwww + 1)
  7425. + // FIXME accept the surrogate code points (only)
  7426. + } else
  7427. + goto fail;
  7428. + } else
  7429. + uchar = c;
  7430. + put_unaligned (cpu_to_le16 (uchar), cp++);
  7431. + count++;
  7432. + len--;
  7433. + }
  7434. + return count;
  7435. +fail:
  7436. + return -1;
  7437. +}
  7438. +#endif /* DWC_UTFLIB */
  7439. +
  7440. +
  7441. +/* dwc_debug.h */
  7442. +
  7443. +dwc_bool_t DWC_IN_IRQ(void)
  7444. +{
  7445. + return in_irq();
  7446. +}
  7447. +
  7448. +dwc_bool_t DWC_IN_BH(void)
  7449. +{
  7450. + return in_softirq();
  7451. +}
  7452. +
  7453. +void DWC_VPRINTF(char *format, va_list args)
  7454. +{
  7455. + vprintk(format, args);
  7456. +}
  7457. +
  7458. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  7459. +{
  7460. + return vsnprintf(str, size, format, args);
  7461. +}
  7462. +
  7463. +void DWC_PRINTF(char *format, ...)
  7464. +{
  7465. + va_list args;
  7466. +
  7467. + va_start(args, format);
  7468. + DWC_VPRINTF(format, args);
  7469. + va_end(args);
  7470. +}
  7471. +
  7472. +int DWC_SPRINTF(char *buffer, char *format, ...)
  7473. +{
  7474. + int retval;
  7475. + va_list args;
  7476. +
  7477. + va_start(args, format);
  7478. + retval = vsprintf(buffer, format, args);
  7479. + va_end(args);
  7480. + return retval;
  7481. +}
  7482. +
  7483. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  7484. +{
  7485. + int retval;
  7486. + va_list args;
  7487. +
  7488. + va_start(args, format);
  7489. + retval = vsnprintf(buffer, size, format, args);
  7490. + va_end(args);
  7491. + return retval;
  7492. +}
  7493. +
  7494. +void __DWC_WARN(char *format, ...)
  7495. +{
  7496. + va_list args;
  7497. +
  7498. + va_start(args, format);
  7499. + DWC_PRINTF(KERN_WARNING);
  7500. + DWC_VPRINTF(format, args);
  7501. + va_end(args);
  7502. +}
  7503. +
  7504. +void __DWC_ERROR(char *format, ...)
  7505. +{
  7506. + va_list args;
  7507. +
  7508. + va_start(args, format);
  7509. + DWC_PRINTF(KERN_ERR);
  7510. + DWC_VPRINTF(format, args);
  7511. + va_end(args);
  7512. +}
  7513. +
  7514. +void DWC_EXCEPTION(char *format, ...)
  7515. +{
  7516. + va_list args;
  7517. +
  7518. + va_start(args, format);
  7519. + DWC_PRINTF(KERN_ERR);
  7520. + DWC_VPRINTF(format, args);
  7521. + va_end(args);
  7522. + BUG_ON(1);
  7523. +}
  7524. +
  7525. +#ifdef DEBUG
  7526. +void __DWC_DEBUG(char *format, ...)
  7527. +{
  7528. + va_list args;
  7529. +
  7530. + va_start(args, format);
  7531. + DWC_PRINTF(KERN_DEBUG);
  7532. + DWC_VPRINTF(format, args);
  7533. + va_end(args);
  7534. +}
  7535. +#endif
  7536. +
  7537. +
  7538. +/* dwc_mem.h */
  7539. +
  7540. +#if 0
  7541. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  7542. + uint32_t align,
  7543. + uint32_t alloc)
  7544. +{
  7545. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  7546. + size, align, alloc);
  7547. + return (dwc_pool_t *)pool;
  7548. +}
  7549. +
  7550. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  7551. +{
  7552. + dma_pool_destroy((struct dma_pool *)pool);
  7553. +}
  7554. +
  7555. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  7556. +{
  7557. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  7558. +}
  7559. +
  7560. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  7561. +{
  7562. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  7563. + memset(..);
  7564. +}
  7565. +
  7566. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  7567. +{
  7568. + dma_pool_free(pool, vaddr, daddr);
  7569. +}
  7570. +#endif
  7571. +
  7572. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  7573. +{
  7574. + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
  7575. +}
  7576. +
  7577. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  7578. +{
  7579. + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
  7580. +}
  7581. +
  7582. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  7583. +{
  7584. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  7585. +}
  7586. +
  7587. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  7588. +{
  7589. + return kzalloc(size, GFP_KERNEL);
  7590. +}
  7591. +
  7592. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  7593. +{
  7594. + return kzalloc(size, GFP_ATOMIC);
  7595. +}
  7596. +
  7597. +void __DWC_FREE(void *mem_ctx, void *addr)
  7598. +{
  7599. + kfree(addr);
  7600. +}
  7601. +
  7602. +
  7603. +#ifdef DWC_CRYPTOLIB
  7604. +/* dwc_crypto.h */
  7605. +
  7606. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  7607. +{
  7608. + get_random_bytes(buffer, length);
  7609. +}
  7610. +
  7611. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  7612. +{
  7613. + struct crypto_blkcipher *tfm;
  7614. + struct blkcipher_desc desc;
  7615. + struct scatterlist sgd;
  7616. + struct scatterlist sgs;
  7617. +
  7618. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  7619. + if (tfm == NULL) {
  7620. + printk("failed to load transform for aes CBC\n");
  7621. + return -1;
  7622. + }
  7623. +
  7624. + crypto_blkcipher_setkey(tfm, key, keylen);
  7625. + crypto_blkcipher_set_iv(tfm, iv, 16);
  7626. +
  7627. + sg_init_one(&sgd, out, messagelen);
  7628. + sg_init_one(&sgs, message, messagelen);
  7629. +
  7630. + desc.tfm = tfm;
  7631. + desc.flags = 0;
  7632. +
  7633. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  7634. + crypto_free_blkcipher(tfm);
  7635. + DWC_ERROR("AES CBC encryption failed");
  7636. + return -1;
  7637. + }
  7638. +
  7639. + crypto_free_blkcipher(tfm);
  7640. + return 0;
  7641. +}
  7642. +
  7643. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  7644. +{
  7645. + struct crypto_hash *tfm;
  7646. + struct hash_desc desc;
  7647. + struct scatterlist sg;
  7648. +
  7649. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  7650. + if (IS_ERR(tfm)) {
  7651. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  7652. + return 0;
  7653. + }
  7654. + desc.tfm = tfm;
  7655. + desc.flags = 0;
  7656. +
  7657. + sg_init_one(&sg, message, len);
  7658. + crypto_hash_digest(&desc, &sg, len, out);
  7659. + crypto_free_hash(tfm);
  7660. +
  7661. + return 1;
  7662. +}
  7663. +
  7664. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  7665. + uint8_t *key, uint32_t keylen, uint8_t *out)
  7666. +{
  7667. + struct crypto_hash *tfm;
  7668. + struct hash_desc desc;
  7669. + struct scatterlist sg;
  7670. +
  7671. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  7672. + if (IS_ERR(tfm)) {
  7673. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  7674. + return 0;
  7675. + }
  7676. + desc.tfm = tfm;
  7677. + desc.flags = 0;
  7678. +
  7679. + sg_init_one(&sg, message, messagelen);
  7680. + crypto_hash_setkey(tfm, key, keylen);
  7681. + crypto_hash_digest(&desc, &sg, messagelen, out);
  7682. + crypto_free_hash(tfm);
  7683. +
  7684. + return 1;
  7685. +}
  7686. +#endif /* DWC_CRYPTOLIB */
  7687. +
  7688. +
  7689. +/* Byte Ordering Conversions */
  7690. +
  7691. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  7692. +{
  7693. +#ifdef __LITTLE_ENDIAN
  7694. + return *p;
  7695. +#else
  7696. + uint8_t *u_p = (uint8_t *)p;
  7697. +
  7698. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7699. +#endif
  7700. +}
  7701. +
  7702. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  7703. +{
  7704. +#ifdef __BIG_ENDIAN
  7705. + return *p;
  7706. +#else
  7707. + uint8_t *u_p = (uint8_t *)p;
  7708. +
  7709. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7710. +#endif
  7711. +}
  7712. +
  7713. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  7714. +{
  7715. +#ifdef __LITTLE_ENDIAN
  7716. + return *p;
  7717. +#else
  7718. + uint8_t *u_p = (uint8_t *)p;
  7719. +
  7720. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7721. +#endif
  7722. +}
  7723. +
  7724. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  7725. +{
  7726. +#ifdef __BIG_ENDIAN
  7727. + return *p;
  7728. +#else
  7729. + uint8_t *u_p = (uint8_t *)p;
  7730. +
  7731. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7732. +#endif
  7733. +}
  7734. +
  7735. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  7736. +{
  7737. +#ifdef __LITTLE_ENDIAN
  7738. + return *p;
  7739. +#else
  7740. + uint8_t *u_p = (uint8_t *)p;
  7741. + return (u_p[1] | (u_p[0] << 8));
  7742. +#endif
  7743. +}
  7744. +
  7745. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  7746. +{
  7747. +#ifdef __BIG_ENDIAN
  7748. + return *p;
  7749. +#else
  7750. + uint8_t *u_p = (uint8_t *)p;
  7751. + return (u_p[1] | (u_p[0] << 8));
  7752. +#endif
  7753. +}
  7754. +
  7755. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  7756. +{
  7757. +#ifdef __LITTLE_ENDIAN
  7758. + return *p;
  7759. +#else
  7760. + uint8_t *u_p = (uint8_t *)p;
  7761. + return (u_p[1] | (u_p[0] << 8));
  7762. +#endif
  7763. +}
  7764. +
  7765. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  7766. +{
  7767. +#ifdef __BIG_ENDIAN
  7768. + return *p;
  7769. +#else
  7770. + uint8_t *u_p = (uint8_t *)p;
  7771. + return (u_p[1] | (u_p[0] << 8));
  7772. +#endif
  7773. +}
  7774. +
  7775. +
  7776. +/* Registers */
  7777. +
  7778. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  7779. +{
  7780. + return readl(reg);
  7781. +}
  7782. +
  7783. +#if 0
  7784. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  7785. +{
  7786. +}
  7787. +#endif
  7788. +
  7789. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  7790. +{
  7791. + writel(value, reg);
  7792. +}
  7793. +
  7794. +#if 0
  7795. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  7796. +{
  7797. +}
  7798. +#endif
  7799. +
  7800. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  7801. +{
  7802. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  7803. +}
  7804. +
  7805. +#if 0
  7806. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  7807. +{
  7808. +}
  7809. +#endif
  7810. +
  7811. +
  7812. +/* Locking */
  7813. +
  7814. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  7815. +{
  7816. + spinlock_t *sl = (spinlock_t *)1;
  7817. +
  7818. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7819. + sl = DWC_ALLOC(sizeof(*sl));
  7820. + if (!sl) {
  7821. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  7822. + return NULL;
  7823. + }
  7824. +
  7825. + spin_lock_init(sl);
  7826. +#endif
  7827. + return (dwc_spinlock_t *)sl;
  7828. +}
  7829. +
  7830. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  7831. +{
  7832. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7833. + DWC_FREE(lock);
  7834. +#endif
  7835. +}
  7836. +
  7837. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  7838. +{
  7839. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7840. + spin_lock((spinlock_t *)lock);
  7841. +#endif
  7842. +}
  7843. +
  7844. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  7845. +{
  7846. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7847. + spin_unlock((spinlock_t *)lock);
  7848. +#endif
  7849. +}
  7850. +
  7851. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  7852. +{
  7853. + dwc_irqflags_t f;
  7854. +
  7855. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7856. + spin_lock_irqsave((spinlock_t *)lock, f);
  7857. +#else
  7858. + local_irq_save(f);
  7859. +#endif
  7860. + *flags = f;
  7861. +}
  7862. +
  7863. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  7864. +{
  7865. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7866. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  7867. +#else
  7868. + local_irq_restore(flags);
  7869. +#endif
  7870. +}
  7871. +
  7872. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  7873. +{
  7874. + struct mutex *m;
  7875. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  7876. +
  7877. + if (!mutex) {
  7878. + DWC_ERROR("Cannot allocate memory for mutex\n");
  7879. + return NULL;
  7880. + }
  7881. +
  7882. + m = (struct mutex *)mutex;
  7883. + mutex_init(m);
  7884. + return mutex;
  7885. +}
  7886. +
  7887. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  7888. +#else
  7889. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  7890. +{
  7891. + mutex_destroy((struct mutex *)mutex);
  7892. + DWC_FREE(mutex);
  7893. +}
  7894. +#endif
  7895. +
  7896. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  7897. +{
  7898. + struct mutex *m = (struct mutex *)mutex;
  7899. + mutex_lock(m);
  7900. +}
  7901. +
  7902. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  7903. +{
  7904. + struct mutex *m = (struct mutex *)mutex;
  7905. + return mutex_trylock(m);
  7906. +}
  7907. +
  7908. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  7909. +{
  7910. + struct mutex *m = (struct mutex *)mutex;
  7911. + mutex_unlock(m);
  7912. +}
  7913. +
  7914. +
  7915. +/* Timing */
  7916. +
  7917. +void DWC_UDELAY(uint32_t usecs)
  7918. +{
  7919. + udelay(usecs);
  7920. +}
  7921. +
  7922. +void DWC_MDELAY(uint32_t msecs)
  7923. +{
  7924. + mdelay(msecs);
  7925. +}
  7926. +
  7927. +void DWC_MSLEEP(uint32_t msecs)
  7928. +{
  7929. + msleep(msecs);
  7930. +}
  7931. +
  7932. +uint32_t DWC_TIME(void)
  7933. +{
  7934. + return jiffies_to_msecs(jiffies);
  7935. +}
  7936. +
  7937. +
  7938. +/* Timers */
  7939. +
  7940. +struct dwc_timer {
  7941. + struct timer_list *t;
  7942. + char *name;
  7943. + dwc_timer_callback_t cb;
  7944. + void *data;
  7945. + uint8_t scheduled;
  7946. + dwc_spinlock_t *lock;
  7947. +};
  7948. +
  7949. +static void timer_callback(unsigned long data)
  7950. +{
  7951. + dwc_timer_t *timer = (dwc_timer_t *)data;
  7952. + dwc_irqflags_t flags;
  7953. +
  7954. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  7955. + timer->scheduled = 0;
  7956. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  7957. + DWC_DEBUGC("Timer %s callback", timer->name);
  7958. + timer->cb(timer->data);
  7959. +}
  7960. +
  7961. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  7962. +{
  7963. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  7964. +
  7965. + if (!t) {
  7966. + DWC_ERROR("Cannot allocate memory for timer");
  7967. + return NULL;
  7968. + }
  7969. +
  7970. + t->t = DWC_ALLOC(sizeof(*t->t));
  7971. + if (!t->t) {
  7972. + DWC_ERROR("Cannot allocate memory for timer->t");
  7973. + goto no_timer;
  7974. + }
  7975. +
  7976. + t->name = DWC_STRDUP(name);
  7977. + if (!t->name) {
  7978. + DWC_ERROR("Cannot allocate memory for timer->name");
  7979. + goto no_name;
  7980. + }
  7981. +
  7982. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  7983. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
  7984. +#else
  7985. + t->lock = DWC_SPINLOCK_ALLOC();
  7986. +#endif
  7987. + if (!t->lock) {
  7988. + DWC_ERROR("Cannot allocate memory for lock");
  7989. + goto no_lock;
  7990. + }
  7991. +
  7992. + t->scheduled = 0;
  7993. + t->t->expires = jiffies;
  7994. + setup_timer(t->t, timer_callback, (unsigned long)t);
  7995. +
  7996. + t->cb = cb;
  7997. + t->data = data;
  7998. +
  7999. + return t;
  8000. +
  8001. + no_lock:
  8002. + DWC_FREE(t->name);
  8003. + no_name:
  8004. + DWC_FREE(t->t);
  8005. + no_timer:
  8006. + DWC_FREE(t);
  8007. + return NULL;
  8008. +}
  8009. +
  8010. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  8011. +{
  8012. + dwc_irqflags_t flags;
  8013. +
  8014. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  8015. +
  8016. + if (timer->scheduled) {
  8017. + del_timer(timer->t);
  8018. + timer->scheduled = 0;
  8019. + }
  8020. +
  8021. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  8022. + DWC_SPINLOCK_FREE(timer->lock);
  8023. + DWC_FREE(timer->t);
  8024. + DWC_FREE(timer->name);
  8025. + DWC_FREE(timer);
  8026. +}
  8027. +
  8028. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  8029. +{
  8030. + dwc_irqflags_t flags;
  8031. +
  8032. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  8033. +
  8034. + if (!timer->scheduled) {
  8035. + timer->scheduled = 1;
  8036. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  8037. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  8038. + add_timer(timer->t);
  8039. + } else {
  8040. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  8041. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  8042. + }
  8043. +
  8044. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  8045. +}
  8046. +
  8047. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  8048. +{
  8049. + del_timer(timer->t);
  8050. +}
  8051. +
  8052. +
  8053. +/* Wait Queues */
  8054. +
  8055. +struct dwc_waitq {
  8056. + wait_queue_head_t queue;
  8057. + int abort;
  8058. +};
  8059. +
  8060. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  8061. +{
  8062. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  8063. +
  8064. + if (!wq) {
  8065. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  8066. + return NULL;
  8067. + }
  8068. +
  8069. + init_waitqueue_head(&wq->queue);
  8070. + wq->abort = 0;
  8071. + return wq;
  8072. +}
  8073. +
  8074. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  8075. +{
  8076. + DWC_FREE(wq);
  8077. +}
  8078. +
  8079. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  8080. +{
  8081. + int result = wait_event_interruptible(wq->queue,
  8082. + cond(data) || wq->abort);
  8083. + if (result == -ERESTARTSYS) {
  8084. + wq->abort = 0;
  8085. + return -DWC_E_RESTART;
  8086. + }
  8087. +
  8088. + if (wq->abort == 1) {
  8089. + wq->abort = 0;
  8090. + return -DWC_E_ABORT;
  8091. + }
  8092. +
  8093. + wq->abort = 0;
  8094. +
  8095. + if (result == 0) {
  8096. + return 0;
  8097. + }
  8098. +
  8099. + return -DWC_E_UNKNOWN;
  8100. +}
  8101. +
  8102. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  8103. + void *data, int32_t msecs)
  8104. +{
  8105. + int32_t tmsecs;
  8106. + int result = wait_event_interruptible_timeout(wq->queue,
  8107. + cond(data) || wq->abort,
  8108. + msecs_to_jiffies(msecs));
  8109. + if (result == -ERESTARTSYS) {
  8110. + wq->abort = 0;
  8111. + return -DWC_E_RESTART;
  8112. + }
  8113. +
  8114. + if (wq->abort == 1) {
  8115. + wq->abort = 0;
  8116. + return -DWC_E_ABORT;
  8117. + }
  8118. +
  8119. + wq->abort = 0;
  8120. +
  8121. + if (result > 0) {
  8122. + tmsecs = jiffies_to_msecs(result);
  8123. + if (!tmsecs) {
  8124. + return 1;
  8125. + }
  8126. +
  8127. + return tmsecs;
  8128. + }
  8129. +
  8130. + if (result == 0) {
  8131. + return -DWC_E_TIMEOUT;
  8132. + }
  8133. +
  8134. + return -DWC_E_UNKNOWN;
  8135. +}
  8136. +
  8137. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  8138. +{
  8139. + wq->abort = 0;
  8140. + wake_up_interruptible(&wq->queue);
  8141. +}
  8142. +
  8143. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  8144. +{
  8145. + wq->abort = 1;
  8146. + wake_up_interruptible(&wq->queue);
  8147. +}
  8148. +
  8149. +
  8150. +/* Threading */
  8151. +
  8152. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  8153. +{
  8154. + struct task_struct *thread = kthread_run(func, data, name);
  8155. +
  8156. + if (thread == ERR_PTR(-ENOMEM)) {
  8157. + return NULL;
  8158. + }
  8159. +
  8160. + return (dwc_thread_t *)thread;
  8161. +}
  8162. +
  8163. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  8164. +{
  8165. + return kthread_stop((struct task_struct *)thread);
  8166. +}
  8167. +
  8168. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  8169. +{
  8170. + return kthread_should_stop();
  8171. +}
  8172. +
  8173. +
  8174. +/* tasklets
  8175. + - run in interrupt context (cannot sleep)
  8176. + - each tasklet runs on a single CPU
  8177. + - different tasklets can be running simultaneously on different CPUs
  8178. + */
  8179. +struct dwc_tasklet {
  8180. + struct tasklet_struct t;
  8181. + dwc_tasklet_callback_t cb;
  8182. + void *data;
  8183. +};
  8184. +
  8185. +static void tasklet_callback(unsigned long data)
  8186. +{
  8187. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  8188. + t->cb(t->data);
  8189. +}
  8190. +
  8191. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  8192. +{
  8193. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  8194. +
  8195. + if (t) {
  8196. + t->cb = cb;
  8197. + t->data = data;
  8198. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  8199. + } else {
  8200. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  8201. + }
  8202. +
  8203. + return t;
  8204. +}
  8205. +
  8206. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  8207. +{
  8208. + DWC_FREE(task);
  8209. +}
  8210. +
  8211. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  8212. +{
  8213. + tasklet_schedule(&task->t);
  8214. +}
  8215. +
  8216. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  8217. +{
  8218. + tasklet_hi_schedule(&task->t);
  8219. +}
  8220. +
  8221. +
  8222. +/* workqueues
  8223. + - run in process context (can sleep)
  8224. + */
  8225. +typedef struct work_container {
  8226. + dwc_work_callback_t cb;
  8227. + void *data;
  8228. + dwc_workq_t *wq;
  8229. + char *name;
  8230. +
  8231. +#ifdef DEBUG
  8232. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  8233. +#endif
  8234. + struct delayed_work work;
  8235. +} work_container_t;
  8236. +
  8237. +#ifdef DEBUG
  8238. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  8239. +#endif
  8240. +
  8241. +struct dwc_workq {
  8242. + struct workqueue_struct *wq;
  8243. + dwc_spinlock_t *lock;
  8244. + dwc_waitq_t *waitq;
  8245. + int pending;
  8246. +
  8247. +#ifdef DEBUG
  8248. + struct work_container_queue entries;
  8249. +#endif
  8250. +};
  8251. +
  8252. +static void do_work(struct work_struct *work)
  8253. +{
  8254. + dwc_irqflags_t flags;
  8255. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  8256. + work_container_t *container = container_of(dw, struct work_container, work);
  8257. + dwc_workq_t *wq = container->wq;
  8258. +
  8259. + container->cb(container->data);
  8260. +
  8261. +#ifdef DEBUG
  8262. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  8263. +#endif
  8264. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  8265. + if (container->name) {
  8266. + DWC_FREE(container->name);
  8267. + }
  8268. + DWC_FREE(container);
  8269. +
  8270. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8271. + wq->pending--;
  8272. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8273. + DWC_WAITQ_TRIGGER(wq->waitq);
  8274. +}
  8275. +
  8276. +static int work_done(void *data)
  8277. +{
  8278. + dwc_workq_t *workq = (dwc_workq_t *)data;
  8279. + return workq->pending == 0;
  8280. +}
  8281. +
  8282. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  8283. +{
  8284. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  8285. +}
  8286. +
  8287. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  8288. +{
  8289. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  8290. +
  8291. + if (!wq) {
  8292. + return NULL;
  8293. + }
  8294. +
  8295. + wq->wq = create_singlethread_workqueue(name);
  8296. + if (!wq->wq) {
  8297. + goto no_wq;
  8298. + }
  8299. +
  8300. + wq->pending = 0;
  8301. +
  8302. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  8303. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
  8304. +#else
  8305. + wq->lock = DWC_SPINLOCK_ALLOC();
  8306. +#endif
  8307. + if (!wq->lock) {
  8308. + goto no_lock;
  8309. + }
  8310. +
  8311. + wq->waitq = DWC_WAITQ_ALLOC();
  8312. + if (!wq->waitq) {
  8313. + goto no_waitq;
  8314. + }
  8315. +
  8316. +#ifdef DEBUG
  8317. + DWC_CIRCLEQ_INIT(&wq->entries);
  8318. +#endif
  8319. + return wq;
  8320. +
  8321. + no_waitq:
  8322. + DWC_SPINLOCK_FREE(wq->lock);
  8323. + no_lock:
  8324. + destroy_workqueue(wq->wq);
  8325. + no_wq:
  8326. + DWC_FREE(wq);
  8327. +
  8328. + return NULL;
  8329. +}
  8330. +
  8331. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  8332. +{
  8333. +#ifdef DEBUG
  8334. + if (wq->pending != 0) {
  8335. + struct work_container *wc;
  8336. + DWC_ERROR("Destroying work queue with pending work");
  8337. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  8338. + DWC_ERROR("Work %s still pending", wc->name);
  8339. + }
  8340. + }
  8341. +#endif
  8342. + destroy_workqueue(wq->wq);
  8343. + DWC_SPINLOCK_FREE(wq->lock);
  8344. + DWC_WAITQ_FREE(wq->waitq);
  8345. + DWC_FREE(wq);
  8346. +}
  8347. +
  8348. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  8349. + char *format, ...)
  8350. +{
  8351. + dwc_irqflags_t flags;
  8352. + work_container_t *container;
  8353. + static char name[128];
  8354. + va_list args;
  8355. +
  8356. + va_start(args, format);
  8357. + DWC_VSNPRINTF(name, 128, format, args);
  8358. + va_end(args);
  8359. +
  8360. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8361. + wq->pending++;
  8362. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8363. + DWC_WAITQ_TRIGGER(wq->waitq);
  8364. +
  8365. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  8366. + if (!container) {
  8367. + DWC_ERROR("Cannot allocate memory for container\n");
  8368. + return;
  8369. + }
  8370. +
  8371. + container->name = DWC_STRDUP(name);
  8372. + if (!container->name) {
  8373. + DWC_ERROR("Cannot allocate memory for container->name\n");
  8374. + DWC_FREE(container);
  8375. + return;
  8376. + }
  8377. +
  8378. + container->cb = cb;
  8379. + container->data = data;
  8380. + container->wq = wq;
  8381. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  8382. + INIT_WORK(&container->work.work, do_work);
  8383. +
  8384. +#ifdef DEBUG
  8385. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  8386. +#endif
  8387. + queue_work(wq->wq, &container->work.work);
  8388. +}
  8389. +
  8390. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  8391. + void *data, uint32_t time, char *format, ...)
  8392. +{
  8393. + dwc_irqflags_t flags;
  8394. + work_container_t *container;
  8395. + static char name[128];
  8396. + va_list args;
  8397. +
  8398. + va_start(args, format);
  8399. + DWC_VSNPRINTF(name, 128, format, args);
  8400. + va_end(args);
  8401. +
  8402. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8403. + wq->pending++;
  8404. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8405. + DWC_WAITQ_TRIGGER(wq->waitq);
  8406. +
  8407. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  8408. + if (!container) {
  8409. + DWC_ERROR("Cannot allocate memory for container\n");
  8410. + return;
  8411. + }
  8412. +
  8413. + container->name = DWC_STRDUP(name);
  8414. + if (!container->name) {
  8415. + DWC_ERROR("Cannot allocate memory for container->name\n");
  8416. + DWC_FREE(container);
  8417. + return;
  8418. + }
  8419. +
  8420. + container->cb = cb;
  8421. + container->data = data;
  8422. + container->wq = wq;
  8423. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  8424. + INIT_DELAYED_WORK(&container->work, do_work);
  8425. +
  8426. +#ifdef DEBUG
  8427. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  8428. +#endif
  8429. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  8430. +}
  8431. +
  8432. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  8433. +{
  8434. + return wq->pending;
  8435. +}
  8436. +
  8437. +
  8438. +#ifdef DWC_LIBMODULE
  8439. +
  8440. +#ifdef DWC_CCLIB
  8441. +/* CC */
  8442. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  8443. +EXPORT_SYMBOL(dwc_cc_if_free);
  8444. +EXPORT_SYMBOL(dwc_cc_clear);
  8445. +EXPORT_SYMBOL(dwc_cc_add);
  8446. +EXPORT_SYMBOL(dwc_cc_remove);
  8447. +EXPORT_SYMBOL(dwc_cc_change);
  8448. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  8449. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  8450. +EXPORT_SYMBOL(dwc_cc_match_chid);
  8451. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  8452. +EXPORT_SYMBOL(dwc_cc_ck);
  8453. +EXPORT_SYMBOL(dwc_cc_chid);
  8454. +EXPORT_SYMBOL(dwc_cc_cdid);
  8455. +EXPORT_SYMBOL(dwc_cc_name);
  8456. +#endif /* DWC_CCLIB */
  8457. +
  8458. +#ifdef DWC_CRYPTOLIB
  8459. +# ifndef CONFIG_MACH_IPMATE
  8460. +/* Modpow */
  8461. +EXPORT_SYMBOL(dwc_modpow);
  8462. +
  8463. +/* DH */
  8464. +EXPORT_SYMBOL(dwc_dh_modpow);
  8465. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  8466. +EXPORT_SYMBOL(dwc_dh_pk);
  8467. +# endif /* CONFIG_MACH_IPMATE */
  8468. +
  8469. +/* Crypto */
  8470. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  8471. +EXPORT_SYMBOL(dwc_wusb_cmf);
  8472. +EXPORT_SYMBOL(dwc_wusb_prf);
  8473. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  8474. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  8475. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  8476. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  8477. +#endif /* DWC_CRYPTOLIB */
  8478. +
  8479. +/* Notification */
  8480. +#ifdef DWC_NOTIFYLIB
  8481. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  8482. +EXPORT_SYMBOL(dwc_free_notification_manager);
  8483. +EXPORT_SYMBOL(dwc_register_notifier);
  8484. +EXPORT_SYMBOL(dwc_unregister_notifier);
  8485. +EXPORT_SYMBOL(dwc_add_observer);
  8486. +EXPORT_SYMBOL(dwc_remove_observer);
  8487. +EXPORT_SYMBOL(dwc_notify);
  8488. +#endif
  8489. +
  8490. +/* Memory Debugging Routines */
  8491. +#ifdef DWC_DEBUG_MEMORY
  8492. +EXPORT_SYMBOL(dwc_alloc_debug);
  8493. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  8494. +EXPORT_SYMBOL(dwc_free_debug);
  8495. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  8496. +EXPORT_SYMBOL(dwc_dma_free_debug);
  8497. +#endif
  8498. +
  8499. +EXPORT_SYMBOL(DWC_MEMSET);
  8500. +EXPORT_SYMBOL(DWC_MEMCPY);
  8501. +EXPORT_SYMBOL(DWC_MEMMOVE);
  8502. +EXPORT_SYMBOL(DWC_MEMCMP);
  8503. +EXPORT_SYMBOL(DWC_STRNCMP);
  8504. +EXPORT_SYMBOL(DWC_STRCMP);
  8505. +EXPORT_SYMBOL(DWC_STRLEN);
  8506. +EXPORT_SYMBOL(DWC_STRCPY);
  8507. +EXPORT_SYMBOL(DWC_STRDUP);
  8508. +EXPORT_SYMBOL(DWC_ATOI);
  8509. +EXPORT_SYMBOL(DWC_ATOUI);
  8510. +
  8511. +#ifdef DWC_UTFLIB
  8512. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  8513. +#endif /* DWC_UTFLIB */
  8514. +
  8515. +EXPORT_SYMBOL(DWC_IN_IRQ);
  8516. +EXPORT_SYMBOL(DWC_IN_BH);
  8517. +EXPORT_SYMBOL(DWC_VPRINTF);
  8518. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  8519. +EXPORT_SYMBOL(DWC_PRINTF);
  8520. +EXPORT_SYMBOL(DWC_SPRINTF);
  8521. +EXPORT_SYMBOL(DWC_SNPRINTF);
  8522. +EXPORT_SYMBOL(__DWC_WARN);
  8523. +EXPORT_SYMBOL(__DWC_ERROR);
  8524. +EXPORT_SYMBOL(DWC_EXCEPTION);
  8525. +
  8526. +#ifdef DEBUG
  8527. +EXPORT_SYMBOL(__DWC_DEBUG);
  8528. +#endif
  8529. +
  8530. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  8531. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  8532. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  8533. +EXPORT_SYMBOL(__DWC_ALLOC);
  8534. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  8535. +EXPORT_SYMBOL(__DWC_FREE);
  8536. +
  8537. +#ifdef DWC_CRYPTOLIB
  8538. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  8539. +EXPORT_SYMBOL(DWC_AES_CBC);
  8540. +EXPORT_SYMBOL(DWC_SHA256);
  8541. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  8542. +#endif
  8543. +
  8544. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  8545. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  8546. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  8547. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  8548. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  8549. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  8550. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  8551. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  8552. +EXPORT_SYMBOL(DWC_READ_REG32);
  8553. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  8554. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  8555. +
  8556. +#if 0
  8557. +EXPORT_SYMBOL(DWC_READ_REG64);
  8558. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  8559. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  8560. +#endif
  8561. +
  8562. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  8563. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  8564. +EXPORT_SYMBOL(DWC_SPINLOCK);
  8565. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  8566. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  8567. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  8568. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  8569. +
  8570. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  8571. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  8572. +#endif
  8573. +
  8574. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  8575. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  8576. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  8577. +EXPORT_SYMBOL(DWC_UDELAY);
  8578. +EXPORT_SYMBOL(DWC_MDELAY);
  8579. +EXPORT_SYMBOL(DWC_MSLEEP);
  8580. +EXPORT_SYMBOL(DWC_TIME);
  8581. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  8582. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  8583. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  8584. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  8585. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  8586. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  8587. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  8588. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  8589. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  8590. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  8591. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  8592. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  8593. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  8594. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  8595. +EXPORT_SYMBOL(DWC_TASK_FREE);
  8596. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  8597. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  8598. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  8599. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  8600. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  8601. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  8602. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  8603. +
  8604. +static int dwc_common_port_init_module(void)
  8605. +{
  8606. + int result = 0;
  8607. +
  8608. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  8609. +
  8610. +#ifdef DWC_DEBUG_MEMORY
  8611. + result = dwc_memory_debug_start(NULL);
  8612. + if (result) {
  8613. + printk(KERN_ERR
  8614. + "dwc_memory_debug_start() failed with error %d\n",
  8615. + result);
  8616. + return result;
  8617. + }
  8618. +#endif
  8619. +
  8620. +#ifdef DWC_NOTIFYLIB
  8621. + result = dwc_alloc_notification_manager(NULL, NULL);
  8622. + if (result) {
  8623. + printk(KERN_ERR
  8624. + "dwc_alloc_notification_manager() failed with error %d\n",
  8625. + result);
  8626. + return result;
  8627. + }
  8628. +#endif
  8629. + return result;
  8630. +}
  8631. +
  8632. +static void dwc_common_port_exit_module(void)
  8633. +{
  8634. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  8635. +
  8636. +#ifdef DWC_NOTIFYLIB
  8637. + dwc_free_notification_manager();
  8638. +#endif
  8639. +
  8640. +#ifdef DWC_DEBUG_MEMORY
  8641. + dwc_memory_debug_stop();
  8642. +#endif
  8643. +}
  8644. +
  8645. +module_init(dwc_common_port_init_module);
  8646. +module_exit(dwc_common_port_exit_module);
  8647. +
  8648. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  8649. +MODULE_AUTHOR("Synopsys Inc.");
  8650. +MODULE_LICENSE ("GPL");
  8651. +
  8652. +#endif /* DWC_LIBMODULE */
  8653. --- /dev/null
  8654. +++ b/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  8655. @@ -0,0 +1,1275 @@
  8656. +#include "dwc_os.h"
  8657. +#include "dwc_list.h"
  8658. +
  8659. +#ifdef DWC_CCLIB
  8660. +# include "dwc_cc.h"
  8661. +#endif
  8662. +
  8663. +#ifdef DWC_CRYPTOLIB
  8664. +# include "dwc_modpow.h"
  8665. +# include "dwc_dh.h"
  8666. +# include "dwc_crypto.h"
  8667. +#endif
  8668. +
  8669. +#ifdef DWC_NOTIFYLIB
  8670. +# include "dwc_notifier.h"
  8671. +#endif
  8672. +
  8673. +/* OS-Level Implementations */
  8674. +
  8675. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  8676. +
  8677. +
  8678. +/* MISC */
  8679. +
  8680. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  8681. +{
  8682. + return memset(dest, byte, size);
  8683. +}
  8684. +
  8685. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  8686. +{
  8687. + return memcpy(dest, src, size);
  8688. +}
  8689. +
  8690. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  8691. +{
  8692. + bcopy(src, dest, size);
  8693. + return dest;
  8694. +}
  8695. +
  8696. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  8697. +{
  8698. + return memcmp(m1, m2, size);
  8699. +}
  8700. +
  8701. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  8702. +{
  8703. + return strncmp(s1, s2, size);
  8704. +}
  8705. +
  8706. +int DWC_STRCMP(void *s1, void *s2)
  8707. +{
  8708. + return strcmp(s1, s2);
  8709. +}
  8710. +
  8711. +int DWC_STRLEN(char const *str)
  8712. +{
  8713. + return strlen(str);
  8714. +}
  8715. +
  8716. +char *DWC_STRCPY(char *to, char const *from)
  8717. +{
  8718. + return strcpy(to, from);
  8719. +}
  8720. +
  8721. +char *DWC_STRDUP(char const *str)
  8722. +{
  8723. + int len = DWC_STRLEN(str) + 1;
  8724. + char *new = DWC_ALLOC_ATOMIC(len);
  8725. +
  8726. + if (!new) {
  8727. + return NULL;
  8728. + }
  8729. +
  8730. + DWC_MEMCPY(new, str, len);
  8731. + return new;
  8732. +}
  8733. +
  8734. +int DWC_ATOI(char *str, int32_t *value)
  8735. +{
  8736. + char *end = NULL;
  8737. +
  8738. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  8739. + * should be equivalent on 2's complement machines
  8740. + */
  8741. + *value = strtoul(str, &end, 0);
  8742. + if (*end == '\0') {
  8743. + return 0;
  8744. + }
  8745. +
  8746. + return -1;
  8747. +}
  8748. +
  8749. +int DWC_ATOUI(char *str, uint32_t *value)
  8750. +{
  8751. + char *end = NULL;
  8752. +
  8753. + *value = strtoul(str, &end, 0);
  8754. + if (*end == '\0') {
  8755. + return 0;
  8756. + }
  8757. +
  8758. + return -1;
  8759. +}
  8760. +
  8761. +
  8762. +#ifdef DWC_UTFLIB
  8763. +/* From usbstring.c */
  8764. +
  8765. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  8766. +{
  8767. + int count = 0;
  8768. + u8 c;
  8769. + u16 uchar;
  8770. +
  8771. + /* this insists on correct encodings, though not minimal ones.
  8772. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  8773. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  8774. + */
  8775. + while (len != 0 && (c = (u8) *s++) != 0) {
  8776. + if (unlikely(c & 0x80)) {
  8777. + // 2-byte sequence:
  8778. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  8779. + if ((c & 0xe0) == 0xc0) {
  8780. + uchar = (c & 0x1f) << 6;
  8781. +
  8782. + c = (u8) *s++;
  8783. + if ((c & 0xc0) != 0xc0)
  8784. + goto fail;
  8785. + c &= 0x3f;
  8786. + uchar |= c;
  8787. +
  8788. + // 3-byte sequence (most CJKV characters):
  8789. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  8790. + } else if ((c & 0xf0) == 0xe0) {
  8791. + uchar = (c & 0x0f) << 12;
  8792. +
  8793. + c = (u8) *s++;
  8794. + if ((c & 0xc0) != 0xc0)
  8795. + goto fail;
  8796. + c &= 0x3f;
  8797. + uchar |= c << 6;
  8798. +
  8799. + c = (u8) *s++;
  8800. + if ((c & 0xc0) != 0xc0)
  8801. + goto fail;
  8802. + c &= 0x3f;
  8803. + uchar |= c;
  8804. +
  8805. + /* no bogus surrogates */
  8806. + if (0xd800 <= uchar && uchar <= 0xdfff)
  8807. + goto fail;
  8808. +
  8809. + // 4-byte sequence (surrogate pairs, currently rare):
  8810. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  8811. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  8812. + // (uuuuu = wwww + 1)
  8813. + // FIXME accept the surrogate code points (only)
  8814. + } else
  8815. + goto fail;
  8816. + } else
  8817. + uchar = c;
  8818. + put_unaligned (cpu_to_le16 (uchar), cp++);
  8819. + count++;
  8820. + len--;
  8821. + }
  8822. + return count;
  8823. +fail:
  8824. + return -1;
  8825. +}
  8826. +
  8827. +#endif /* DWC_UTFLIB */
  8828. +
  8829. +
  8830. +/* dwc_debug.h */
  8831. +
  8832. +dwc_bool_t DWC_IN_IRQ(void)
  8833. +{
  8834. +// return in_irq();
  8835. + return 0;
  8836. +}
  8837. +
  8838. +dwc_bool_t DWC_IN_BH(void)
  8839. +{
  8840. +// return in_softirq();
  8841. + return 0;
  8842. +}
  8843. +
  8844. +void DWC_VPRINTF(char *format, va_list args)
  8845. +{
  8846. + vprintf(format, args);
  8847. +}
  8848. +
  8849. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  8850. +{
  8851. + return vsnprintf(str, size, format, args);
  8852. +}
  8853. +
  8854. +void DWC_PRINTF(char *format, ...)
  8855. +{
  8856. + va_list args;
  8857. +
  8858. + va_start(args, format);
  8859. + DWC_VPRINTF(format, args);
  8860. + va_end(args);
  8861. +}
  8862. +
  8863. +int DWC_SPRINTF(char *buffer, char *format, ...)
  8864. +{
  8865. + int retval;
  8866. + va_list args;
  8867. +
  8868. + va_start(args, format);
  8869. + retval = vsprintf(buffer, format, args);
  8870. + va_end(args);
  8871. + return retval;
  8872. +}
  8873. +
  8874. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  8875. +{
  8876. + int retval;
  8877. + va_list args;
  8878. +
  8879. + va_start(args, format);
  8880. + retval = vsnprintf(buffer, size, format, args);
  8881. + va_end(args);
  8882. + return retval;
  8883. +}
  8884. +
  8885. +void __DWC_WARN(char *format, ...)
  8886. +{
  8887. + va_list args;
  8888. +
  8889. + va_start(args, format);
  8890. + DWC_VPRINTF(format, args);
  8891. + va_end(args);
  8892. +}
  8893. +
  8894. +void __DWC_ERROR(char *format, ...)
  8895. +{
  8896. + va_list args;
  8897. +
  8898. + va_start(args, format);
  8899. + DWC_VPRINTF(format, args);
  8900. + va_end(args);
  8901. +}
  8902. +
  8903. +void DWC_EXCEPTION(char *format, ...)
  8904. +{
  8905. + va_list args;
  8906. +
  8907. + va_start(args, format);
  8908. + DWC_VPRINTF(format, args);
  8909. + va_end(args);
  8910. +// BUG_ON(1); ???
  8911. +}
  8912. +
  8913. +#ifdef DEBUG
  8914. +void __DWC_DEBUG(char *format, ...)
  8915. +{
  8916. + va_list args;
  8917. +
  8918. + va_start(args, format);
  8919. + DWC_VPRINTF(format, args);
  8920. + va_end(args);
  8921. +}
  8922. +#endif
  8923. +
  8924. +
  8925. +/* dwc_mem.h */
  8926. +
  8927. +#if 0
  8928. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  8929. + uint32_t align,
  8930. + uint32_t alloc)
  8931. +{
  8932. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  8933. + size, align, alloc);
  8934. + return (dwc_pool_t *)pool;
  8935. +}
  8936. +
  8937. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  8938. +{
  8939. + dma_pool_destroy((struct dma_pool *)pool);
  8940. +}
  8941. +
  8942. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  8943. +{
  8944. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  8945. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  8946. +}
  8947. +
  8948. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  8949. +{
  8950. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  8951. + memset(..);
  8952. +}
  8953. +
  8954. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  8955. +{
  8956. + dma_pool_free(pool, vaddr, daddr);
  8957. +}
  8958. +#endif
  8959. +
  8960. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  8961. +{
  8962. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  8963. + int error;
  8964. +
  8965. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  8966. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  8967. + &dma->nsegs, BUS_DMA_NOWAIT);
  8968. + if (error) {
  8969. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  8970. + (uintmax_t)size, error);
  8971. + goto fail_0;
  8972. + }
  8973. +
  8974. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  8975. + (caddr_t *)&dma->dma_vaddr,
  8976. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  8977. + if (error) {
  8978. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  8979. + goto fail_1;
  8980. + }
  8981. +
  8982. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  8983. + BUS_DMA_NOWAIT, &dma->dma_map);
  8984. + if (error) {
  8985. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  8986. + goto fail_2;
  8987. + }
  8988. +
  8989. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  8990. + size, NULL, BUS_DMA_NOWAIT);
  8991. + if (error) {
  8992. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  8993. + goto fail_3;
  8994. + }
  8995. +
  8996. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  8997. + *dma_addr = dma->dma_paddr;
  8998. + return dma->dma_vaddr;
  8999. +
  9000. +fail_3:
  9001. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  9002. +fail_2:
  9003. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  9004. +fail_1:
  9005. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  9006. +fail_0:
  9007. + dma->dma_map = NULL;
  9008. + dma->dma_vaddr = NULL;
  9009. + dma->nsegs = 0;
  9010. +
  9011. + return NULL;
  9012. +}
  9013. +
  9014. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  9015. +{
  9016. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  9017. +
  9018. + if (dma->dma_map != NULL) {
  9019. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  9020. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  9021. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  9022. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  9023. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  9024. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  9025. + dma->dma_paddr = 0;
  9026. + dma->dma_map = NULL;
  9027. + dma->dma_vaddr = NULL;
  9028. + dma->nsegs = 0;
  9029. + }
  9030. +}
  9031. +
  9032. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  9033. +{
  9034. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  9035. +}
  9036. +
  9037. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  9038. +{
  9039. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  9040. +}
  9041. +
  9042. +void __DWC_FREE(void *mem_ctx, void *addr)
  9043. +{
  9044. + free(addr, M_DEVBUF);
  9045. +}
  9046. +
  9047. +
  9048. +#ifdef DWC_CRYPTOLIB
  9049. +/* dwc_crypto.h */
  9050. +
  9051. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  9052. +{
  9053. + get_random_bytes(buffer, length);
  9054. +}
  9055. +
  9056. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  9057. +{
  9058. + struct crypto_blkcipher *tfm;
  9059. + struct blkcipher_desc desc;
  9060. + struct scatterlist sgd;
  9061. + struct scatterlist sgs;
  9062. +
  9063. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  9064. + if (tfm == NULL) {
  9065. + printk("failed to load transform for aes CBC\n");
  9066. + return -1;
  9067. + }
  9068. +
  9069. + crypto_blkcipher_setkey(tfm, key, keylen);
  9070. + crypto_blkcipher_set_iv(tfm, iv, 16);
  9071. +
  9072. + sg_init_one(&sgd, out, messagelen);
  9073. + sg_init_one(&sgs, message, messagelen);
  9074. +
  9075. + desc.tfm = tfm;
  9076. + desc.flags = 0;
  9077. +
  9078. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  9079. + crypto_free_blkcipher(tfm);
  9080. + DWC_ERROR("AES CBC encryption failed");
  9081. + return -1;
  9082. + }
  9083. +
  9084. + crypto_free_blkcipher(tfm);
  9085. + return 0;
  9086. +}
  9087. +
  9088. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  9089. +{
  9090. + struct crypto_hash *tfm;
  9091. + struct hash_desc desc;
  9092. + struct scatterlist sg;
  9093. +
  9094. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  9095. + if (IS_ERR(tfm)) {
  9096. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  9097. + return 0;
  9098. + }
  9099. + desc.tfm = tfm;
  9100. + desc.flags = 0;
  9101. +
  9102. + sg_init_one(&sg, message, len);
  9103. + crypto_hash_digest(&desc, &sg, len, out);
  9104. + crypto_free_hash(tfm);
  9105. +
  9106. + return 1;
  9107. +}
  9108. +
  9109. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  9110. + uint8_t *key, uint32_t keylen, uint8_t *out)
  9111. +{
  9112. + struct crypto_hash *tfm;
  9113. + struct hash_desc desc;
  9114. + struct scatterlist sg;
  9115. +
  9116. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  9117. + if (IS_ERR(tfm)) {
  9118. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  9119. + return 0;
  9120. + }
  9121. + desc.tfm = tfm;
  9122. + desc.flags = 0;
  9123. +
  9124. + sg_init_one(&sg, message, messagelen);
  9125. + crypto_hash_setkey(tfm, key, keylen);
  9126. + crypto_hash_digest(&desc, &sg, messagelen, out);
  9127. + crypto_free_hash(tfm);
  9128. +
  9129. + return 1;
  9130. +}
  9131. +
  9132. +#endif /* DWC_CRYPTOLIB */
  9133. +
  9134. +
  9135. +/* Byte Ordering Conversions */
  9136. +
  9137. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  9138. +{
  9139. +#ifdef __LITTLE_ENDIAN
  9140. + return *p;
  9141. +#else
  9142. + uint8_t *u_p = (uint8_t *)p;
  9143. +
  9144. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9145. +#endif
  9146. +}
  9147. +
  9148. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  9149. +{
  9150. +#ifdef __BIG_ENDIAN
  9151. + return *p;
  9152. +#else
  9153. + uint8_t *u_p = (uint8_t *)p;
  9154. +
  9155. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9156. +#endif
  9157. +}
  9158. +
  9159. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  9160. +{
  9161. +#ifdef __LITTLE_ENDIAN
  9162. + return *p;
  9163. +#else
  9164. + uint8_t *u_p = (uint8_t *)p;
  9165. +
  9166. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9167. +#endif
  9168. +}
  9169. +
  9170. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  9171. +{
  9172. +#ifdef __BIG_ENDIAN
  9173. + return *p;
  9174. +#else
  9175. + uint8_t *u_p = (uint8_t *)p;
  9176. +
  9177. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9178. +#endif
  9179. +}
  9180. +
  9181. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  9182. +{
  9183. +#ifdef __LITTLE_ENDIAN
  9184. + return *p;
  9185. +#else
  9186. + uint8_t *u_p = (uint8_t *)p;
  9187. + return (u_p[1] | (u_p[0] << 8));
  9188. +#endif
  9189. +}
  9190. +
  9191. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  9192. +{
  9193. +#ifdef __BIG_ENDIAN
  9194. + return *p;
  9195. +#else
  9196. + uint8_t *u_p = (uint8_t *)p;
  9197. + return (u_p[1] | (u_p[0] << 8));
  9198. +#endif
  9199. +}
  9200. +
  9201. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  9202. +{
  9203. +#ifdef __LITTLE_ENDIAN
  9204. + return *p;
  9205. +#else
  9206. + uint8_t *u_p = (uint8_t *)p;
  9207. + return (u_p[1] | (u_p[0] << 8));
  9208. +#endif
  9209. +}
  9210. +
  9211. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  9212. +{
  9213. +#ifdef __BIG_ENDIAN
  9214. + return *p;
  9215. +#else
  9216. + uint8_t *u_p = (uint8_t *)p;
  9217. + return (u_p[1] | (u_p[0] << 8));
  9218. +#endif
  9219. +}
  9220. +
  9221. +
  9222. +/* Registers */
  9223. +
  9224. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  9225. +{
  9226. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9227. + bus_size_t ior = (bus_size_t)reg;
  9228. +
  9229. + return bus_space_read_4(io->iot, io->ioh, ior);
  9230. +}
  9231. +
  9232. +#if 0
  9233. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  9234. +{
  9235. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9236. + bus_size_t ior = (bus_size_t)reg;
  9237. +
  9238. + return bus_space_read_8(io->iot, io->ioh, ior);
  9239. +}
  9240. +#endif
  9241. +
  9242. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  9243. +{
  9244. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9245. + bus_size_t ior = (bus_size_t)reg;
  9246. +
  9247. + bus_space_write_4(io->iot, io->ioh, ior, value);
  9248. +}
  9249. +
  9250. +#if 0
  9251. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  9252. +{
  9253. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9254. + bus_size_t ior = (bus_size_t)reg;
  9255. +
  9256. + bus_space_write_8(io->iot, io->ioh, ior, value);
  9257. +}
  9258. +#endif
  9259. +
  9260. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  9261. + uint32_t set_mask)
  9262. +{
  9263. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9264. + bus_size_t ior = (bus_size_t)reg;
  9265. +
  9266. + bus_space_write_4(io->iot, io->ioh, ior,
  9267. + (bus_space_read_4(io->iot, io->ioh, ior) &
  9268. + ~clear_mask) | set_mask);
  9269. +}
  9270. +
  9271. +#if 0
  9272. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  9273. + uint64_t set_mask)
  9274. +{
  9275. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9276. + bus_size_t ior = (bus_size_t)reg;
  9277. +
  9278. + bus_space_write_8(io->iot, io->ioh, ior,
  9279. + (bus_space_read_8(io->iot, io->ioh, ior) &
  9280. + ~clear_mask) | set_mask);
  9281. +}
  9282. +#endif
  9283. +
  9284. +
  9285. +/* Locking */
  9286. +
  9287. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  9288. +{
  9289. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  9290. +
  9291. + if (!sl) {
  9292. + DWC_ERROR("Cannot allocate memory for spinlock");
  9293. + return NULL;
  9294. + }
  9295. +
  9296. + simple_lock_init(sl);
  9297. + return (dwc_spinlock_t *)sl;
  9298. +}
  9299. +
  9300. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  9301. +{
  9302. + struct simplelock *sl = (struct simplelock *)lock;
  9303. +
  9304. + DWC_FREE(sl);
  9305. +}
  9306. +
  9307. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  9308. +{
  9309. + simple_lock((struct simplelock *)lock);
  9310. +}
  9311. +
  9312. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  9313. +{
  9314. + simple_unlock((struct simplelock *)lock);
  9315. +}
  9316. +
  9317. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  9318. +{
  9319. + simple_lock((struct simplelock *)lock);
  9320. + *flags = splbio();
  9321. +}
  9322. +
  9323. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  9324. +{
  9325. + splx(flags);
  9326. + simple_unlock((struct simplelock *)lock);
  9327. +}
  9328. +
  9329. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  9330. +{
  9331. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  9332. +
  9333. + if (!mutex) {
  9334. + DWC_ERROR("Cannot allocate memory for mutex");
  9335. + return NULL;
  9336. + }
  9337. +
  9338. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  9339. + return mutex;
  9340. +}
  9341. +
  9342. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  9343. +#else
  9344. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  9345. +{
  9346. + DWC_FREE(mutex);
  9347. +}
  9348. +#endif
  9349. +
  9350. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  9351. +{
  9352. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  9353. +}
  9354. +
  9355. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  9356. +{
  9357. + int status;
  9358. +
  9359. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  9360. + return status == 0;
  9361. +}
  9362. +
  9363. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  9364. +{
  9365. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  9366. +}
  9367. +
  9368. +
  9369. +/* Timing */
  9370. +
  9371. +void DWC_UDELAY(uint32_t usecs)
  9372. +{
  9373. + DELAY(usecs);
  9374. +}
  9375. +
  9376. +void DWC_MDELAY(uint32_t msecs)
  9377. +{
  9378. + do {
  9379. + DELAY(1000);
  9380. + } while (--msecs);
  9381. +}
  9382. +
  9383. +void DWC_MSLEEP(uint32_t msecs)
  9384. +{
  9385. + struct timeval tv;
  9386. +
  9387. + tv.tv_sec = msecs / 1000;
  9388. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  9389. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  9390. +}
  9391. +
  9392. +uint32_t DWC_TIME(void)
  9393. +{
  9394. + struct timeval tv;
  9395. +
  9396. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  9397. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  9398. +}
  9399. +
  9400. +
  9401. +/* Timers */
  9402. +
  9403. +struct dwc_timer {
  9404. + struct callout t;
  9405. + char *name;
  9406. + dwc_spinlock_t *lock;
  9407. + dwc_timer_callback_t cb;
  9408. + void *data;
  9409. +};
  9410. +
  9411. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  9412. +{
  9413. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  9414. +
  9415. + if (!t) {
  9416. + DWC_ERROR("Cannot allocate memory for timer");
  9417. + return NULL;
  9418. + }
  9419. +
  9420. + callout_init(&t->t);
  9421. +
  9422. + t->name = DWC_STRDUP(name);
  9423. + if (!t->name) {
  9424. + DWC_ERROR("Cannot allocate memory for timer->name");
  9425. + goto no_name;
  9426. + }
  9427. +
  9428. + t->lock = DWC_SPINLOCK_ALLOC();
  9429. + if (!t->lock) {
  9430. + DWC_ERROR("Cannot allocate memory for timer->lock");
  9431. + goto no_lock;
  9432. + }
  9433. +
  9434. + t->cb = cb;
  9435. + t->data = data;
  9436. +
  9437. + return t;
  9438. +
  9439. + no_lock:
  9440. + DWC_FREE(t->name);
  9441. + no_name:
  9442. + DWC_FREE(t);
  9443. +
  9444. + return NULL;
  9445. +}
  9446. +
  9447. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  9448. +{
  9449. + callout_stop(&timer->t);
  9450. + DWC_SPINLOCK_FREE(timer->lock);
  9451. + DWC_FREE(timer->name);
  9452. + DWC_FREE(timer);
  9453. +}
  9454. +
  9455. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  9456. +{
  9457. + struct timeval tv;
  9458. +
  9459. + tv.tv_sec = time / 1000;
  9460. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  9461. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  9462. +}
  9463. +
  9464. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  9465. +{
  9466. + callout_stop(&timer->t);
  9467. +}
  9468. +
  9469. +
  9470. +/* Wait Queues */
  9471. +
  9472. +struct dwc_waitq {
  9473. + struct simplelock lock;
  9474. + int abort;
  9475. +};
  9476. +
  9477. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  9478. +{
  9479. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  9480. +
  9481. + if (!wq) {
  9482. + DWC_ERROR("Cannot allocate memory for waitqueue");
  9483. + return NULL;
  9484. + }
  9485. +
  9486. + simple_lock_init(&wq->lock);
  9487. + wq->abort = 0;
  9488. +
  9489. + return wq;
  9490. +}
  9491. +
  9492. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  9493. +{
  9494. + DWC_FREE(wq);
  9495. +}
  9496. +
  9497. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  9498. +{
  9499. + int ipl;
  9500. + int result = 0;
  9501. +
  9502. + simple_lock(&wq->lock);
  9503. + ipl = splbio();
  9504. +
  9505. + /* Skip the sleep if already aborted or triggered */
  9506. + if (!wq->abort && !cond(data)) {
  9507. + splx(ipl);
  9508. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  9509. + ipl = splbio();
  9510. + }
  9511. +
  9512. + if (result == 0) { // awoken
  9513. + if (wq->abort) {
  9514. + wq->abort = 0;
  9515. + result = -DWC_E_ABORT;
  9516. + } else {
  9517. + result = 0;
  9518. + }
  9519. +
  9520. + splx(ipl);
  9521. + simple_unlock(&wq->lock);
  9522. + } else {
  9523. + wq->abort = 0;
  9524. + splx(ipl);
  9525. + simple_unlock(&wq->lock);
  9526. +
  9527. + if (result == ERESTART) { // signaled - restart
  9528. + result = -DWC_E_RESTART;
  9529. + } else { // signaled - must be EINTR
  9530. + result = -DWC_E_ABORT;
  9531. + }
  9532. + }
  9533. +
  9534. + return result;
  9535. +}
  9536. +
  9537. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  9538. + void *data, int32_t msecs)
  9539. +{
  9540. + struct timeval tv, tv1, tv2;
  9541. + int ipl;
  9542. + int result = 0;
  9543. +
  9544. + tv.tv_sec = msecs / 1000;
  9545. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  9546. +
  9547. + simple_lock(&wq->lock);
  9548. + ipl = splbio();
  9549. +
  9550. + /* Skip the sleep if already aborted or triggered */
  9551. + if (!wq->abort && !cond(data)) {
  9552. + splx(ipl);
  9553. + getmicrouptime(&tv1);
  9554. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  9555. + getmicrouptime(&tv2);
  9556. + ipl = splbio();
  9557. + }
  9558. +
  9559. + if (result == 0) { // awoken
  9560. + if (wq->abort) {
  9561. + wq->abort = 0;
  9562. + splx(ipl);
  9563. + simple_unlock(&wq->lock);
  9564. + result = -DWC_E_ABORT;
  9565. + } else {
  9566. + splx(ipl);
  9567. + simple_unlock(&wq->lock);
  9568. +
  9569. + tv2.tv_usec -= tv1.tv_usec;
  9570. + if (tv2.tv_usec < 0) {
  9571. + tv2.tv_usec += 1000000;
  9572. + tv2.tv_sec--;
  9573. + }
  9574. +
  9575. + tv2.tv_sec -= tv1.tv_sec;
  9576. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  9577. + result = msecs - result;
  9578. + if (result <= 0)
  9579. + result = 1;
  9580. + }
  9581. + } else {
  9582. + wq->abort = 0;
  9583. + splx(ipl);
  9584. + simple_unlock(&wq->lock);
  9585. +
  9586. + if (result == ERESTART) { // signaled - restart
  9587. + result = -DWC_E_RESTART;
  9588. +
  9589. + } else if (result == EINTR) { // signaled - interrupt
  9590. + result = -DWC_E_ABORT;
  9591. +
  9592. + } else { // timed out
  9593. + result = -DWC_E_TIMEOUT;
  9594. + }
  9595. + }
  9596. +
  9597. + return result;
  9598. +}
  9599. +
  9600. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  9601. +{
  9602. + wakeup(wq);
  9603. +}
  9604. +
  9605. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  9606. +{
  9607. + int ipl;
  9608. +
  9609. + simple_lock(&wq->lock);
  9610. + ipl = splbio();
  9611. + wq->abort = 1;
  9612. + wakeup(wq);
  9613. + splx(ipl);
  9614. + simple_unlock(&wq->lock);
  9615. +}
  9616. +
  9617. +
  9618. +/* Threading */
  9619. +
  9620. +struct dwc_thread {
  9621. + struct proc *proc;
  9622. + int abort;
  9623. +};
  9624. +
  9625. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  9626. +{
  9627. + int retval;
  9628. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  9629. +
  9630. + if (!thread) {
  9631. + return NULL;
  9632. + }
  9633. +
  9634. + thread->abort = 0;
  9635. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  9636. + "%s", name);
  9637. + if (retval) {
  9638. + DWC_FREE(thread);
  9639. + return NULL;
  9640. + }
  9641. +
  9642. + return thread;
  9643. +}
  9644. +
  9645. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  9646. +{
  9647. + int retval;
  9648. +
  9649. + thread->abort = 1;
  9650. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  9651. +
  9652. + if (retval == 0) {
  9653. + /* DWC_THREAD_EXIT() will free the thread struct */
  9654. + return 0;
  9655. + }
  9656. +
  9657. + /* NOTE: We leak the thread struct if thread doesn't die */
  9658. +
  9659. + if (retval == EWOULDBLOCK) {
  9660. + return -DWC_E_TIMEOUT;
  9661. + }
  9662. +
  9663. + return -DWC_E_UNKNOWN;
  9664. +}
  9665. +
  9666. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  9667. +{
  9668. + return thread->abort;
  9669. +}
  9670. +
  9671. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  9672. +{
  9673. + wakeup(&thread->abort);
  9674. + DWC_FREE(thread);
  9675. + kthread_exit(0);
  9676. +}
  9677. +
  9678. +/* tasklets
  9679. + - Runs in interrupt context (cannot sleep)
  9680. + - Each tasklet runs on a single CPU
  9681. + - Different tasklets can be running simultaneously on different CPUs
  9682. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  9683. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  9684. + */
  9685. +struct dwc_tasklet {
  9686. + dwc_tasklet_callback_t cb;
  9687. + void *data;
  9688. +};
  9689. +
  9690. +static void tasklet_callback(void *data)
  9691. +{
  9692. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  9693. +
  9694. + task->cb(task->data);
  9695. +}
  9696. +
  9697. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  9698. +{
  9699. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  9700. +
  9701. + if (task) {
  9702. + task->cb = cb;
  9703. + task->data = data;
  9704. + } else {
  9705. + DWC_ERROR("Cannot allocate memory for tasklet");
  9706. + }
  9707. +
  9708. + return task;
  9709. +}
  9710. +
  9711. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  9712. +{
  9713. + DWC_FREE(task);
  9714. +}
  9715. +
  9716. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  9717. +{
  9718. + tasklet_callback(task);
  9719. +}
  9720. +
  9721. +
  9722. +/* workqueues
  9723. + - Runs in process context (can sleep)
  9724. + */
  9725. +typedef struct work_container {
  9726. + dwc_work_callback_t cb;
  9727. + void *data;
  9728. + dwc_workq_t *wq;
  9729. + char *name;
  9730. + int hz;
  9731. + struct work task;
  9732. +} work_container_t;
  9733. +
  9734. +struct dwc_workq {
  9735. + struct workqueue *taskq;
  9736. + dwc_spinlock_t *lock;
  9737. + dwc_waitq_t *waitq;
  9738. + int pending;
  9739. + struct work_container *container;
  9740. +};
  9741. +
  9742. +static void do_work(struct work *task, void *data)
  9743. +{
  9744. + dwc_workq_t *wq = (dwc_workq_t *)data;
  9745. + work_container_t *container = wq->container;
  9746. + dwc_irqflags_t flags;
  9747. +
  9748. + if (container->hz) {
  9749. + tsleep(container, 0, "dw3wrk", container->hz);
  9750. + }
  9751. +
  9752. + container->cb(container->data);
  9753. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  9754. +
  9755. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9756. + if (container->name)
  9757. + DWC_FREE(container->name);
  9758. + DWC_FREE(container);
  9759. + wq->pending--;
  9760. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9761. + DWC_WAITQ_TRIGGER(wq->waitq);
  9762. +}
  9763. +
  9764. +static int work_done(void *data)
  9765. +{
  9766. + dwc_workq_t *workq = (dwc_workq_t *)data;
  9767. +
  9768. + return workq->pending == 0;
  9769. +}
  9770. +
  9771. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  9772. +{
  9773. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  9774. +}
  9775. +
  9776. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  9777. +{
  9778. + int result;
  9779. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  9780. +
  9781. + if (!wq) {
  9782. + DWC_ERROR("Cannot allocate memory for workqueue");
  9783. + return NULL;
  9784. + }
  9785. +
  9786. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  9787. + IPL_BIO, 0);
  9788. + if (result) {
  9789. + DWC_ERROR("Cannot create workqueue");
  9790. + goto no_taskq;
  9791. + }
  9792. +
  9793. + wq->pending = 0;
  9794. +
  9795. + wq->lock = DWC_SPINLOCK_ALLOC();
  9796. + if (!wq->lock) {
  9797. + DWC_ERROR("Cannot allocate memory for spinlock");
  9798. + goto no_lock;
  9799. + }
  9800. +
  9801. + wq->waitq = DWC_WAITQ_ALLOC();
  9802. + if (!wq->waitq) {
  9803. + DWC_ERROR("Cannot allocate memory for waitqueue");
  9804. + goto no_waitq;
  9805. + }
  9806. +
  9807. + return wq;
  9808. +
  9809. + no_waitq:
  9810. + DWC_SPINLOCK_FREE(wq->lock);
  9811. + no_lock:
  9812. + workqueue_destroy(wq->taskq);
  9813. + no_taskq:
  9814. + DWC_FREE(wq);
  9815. +
  9816. + return NULL;
  9817. +}
  9818. +
  9819. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  9820. +{
  9821. +#ifdef DEBUG
  9822. + dwc_irqflags_t flags;
  9823. +
  9824. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9825. +
  9826. + if (wq->pending != 0) {
  9827. + struct work_container *container = wq->container;
  9828. +
  9829. + DWC_ERROR("Destroying work queue with pending work");
  9830. +
  9831. + if (container && container->name) {
  9832. + DWC_ERROR("Work %s still pending", container->name);
  9833. + }
  9834. + }
  9835. +
  9836. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9837. +#endif
  9838. + DWC_WAITQ_FREE(wq->waitq);
  9839. + DWC_SPINLOCK_FREE(wq->lock);
  9840. + workqueue_destroy(wq->taskq);
  9841. + DWC_FREE(wq);
  9842. +}
  9843. +
  9844. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  9845. + char *format, ...)
  9846. +{
  9847. + dwc_irqflags_t flags;
  9848. + work_container_t *container;
  9849. + static char name[128];
  9850. + va_list args;
  9851. +
  9852. + va_start(args, format);
  9853. + DWC_VSNPRINTF(name, 128, format, args);
  9854. + va_end(args);
  9855. +
  9856. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9857. + wq->pending++;
  9858. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9859. + DWC_WAITQ_TRIGGER(wq->waitq);
  9860. +
  9861. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  9862. + if (!container) {
  9863. + DWC_ERROR("Cannot allocate memory for container");
  9864. + return;
  9865. + }
  9866. +
  9867. + container->name = DWC_STRDUP(name);
  9868. + if (!container->name) {
  9869. + DWC_ERROR("Cannot allocate memory for container->name");
  9870. + DWC_FREE(container);
  9871. + return;
  9872. + }
  9873. +
  9874. + container->cb = cb;
  9875. + container->data = data;
  9876. + container->wq = wq;
  9877. + container->hz = 0;
  9878. + wq->container = container;
  9879. +
  9880. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  9881. + workqueue_enqueue(wq->taskq, &container->task);
  9882. +}
  9883. +
  9884. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  9885. + void *data, uint32_t time, char *format, ...)
  9886. +{
  9887. + dwc_irqflags_t flags;
  9888. + work_container_t *container;
  9889. + static char name[128];
  9890. + struct timeval tv;
  9891. + va_list args;
  9892. +
  9893. + va_start(args, format);
  9894. + DWC_VSNPRINTF(name, 128, format, args);
  9895. + va_end(args);
  9896. +
  9897. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9898. + wq->pending++;
  9899. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9900. + DWC_WAITQ_TRIGGER(wq->waitq);
  9901. +
  9902. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  9903. + if (!container) {
  9904. + DWC_ERROR("Cannot allocate memory for container");
  9905. + return;
  9906. + }
  9907. +
  9908. + container->name = DWC_STRDUP(name);
  9909. + if (!container->name) {
  9910. + DWC_ERROR("Cannot allocate memory for container->name");
  9911. + DWC_FREE(container);
  9912. + return;
  9913. + }
  9914. +
  9915. + container->cb = cb;
  9916. + container->data = data;
  9917. + container->wq = wq;
  9918. + tv.tv_sec = time / 1000;
  9919. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  9920. + container->hz = tvtohz(&tv);
  9921. + wq->container = container;
  9922. +
  9923. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  9924. + workqueue_enqueue(wq->taskq, &container->task);
  9925. +}
  9926. +
  9927. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  9928. +{
  9929. + return wq->pending;
  9930. +}
  9931. --- /dev/null
  9932. +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.c
  9933. @@ -0,0 +1,308 @@
  9934. +/* =========================================================================
  9935. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  9936. + * $Revision: #5 $
  9937. + * $Date: 2010/09/28 $
  9938. + * $Change: 1596182 $
  9939. + *
  9940. + * Synopsys Portability Library Software and documentation
  9941. + * (hereinafter, "Software") is an Unsupported proprietary work of
  9942. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  9943. + * between Synopsys and you.
  9944. + *
  9945. + * The Software IS NOT an item of Licensed Software or Licensed Product
  9946. + * under any End User Software License Agreement or Agreement for
  9947. + * Licensed Product with Synopsys or any supplement thereto. You are
  9948. + * permitted to use and redistribute this Software in source and binary
  9949. + * forms, with or without modification, provided that redistributions
  9950. + * of source code must retain this notice. You may not view, use,
  9951. + * disclose, copy or distribute this file or any information contained
  9952. + * herein except pursuant to this license grant from Synopsys. If you
  9953. + * do not agree with this notice, including the disclaimer below, then
  9954. + * you are not authorized to use the Software.
  9955. + *
  9956. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  9957. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9958. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  9959. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  9960. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9961. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9962. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9963. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  9964. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  9965. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  9966. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  9967. + * DAMAGE.
  9968. + * ========================================================================= */
  9969. +
  9970. +/** @file
  9971. + * This file contains the WUSB cryptographic routines.
  9972. + */
  9973. +
  9974. +#ifdef DWC_CRYPTOLIB
  9975. +
  9976. +#include "dwc_crypto.h"
  9977. +#include "usb.h"
  9978. +
  9979. +#ifdef DEBUG
  9980. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  9981. +{
  9982. + int i;
  9983. + DWC_PRINTF("%s: ", name);
  9984. + for (i=0; i<len; i++) {
  9985. + DWC_PRINTF("%02x ", bytes[i]);
  9986. + }
  9987. + DWC_PRINTF("\n");
  9988. +}
  9989. +#else
  9990. +#define dump_bytes(x...)
  9991. +#endif
  9992. +
  9993. +/* Display a block */
  9994. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  9995. +{
  9996. +#ifdef DWC_DEBUG_CRYPTO
  9997. + int i, blksize = 16;
  9998. +
  9999. + DWC_DEBUG("%s", prefix);
  10000. +
  10001. + if (suffix == NULL) {
  10002. + suffix = "\n";
  10003. + blksize = a;
  10004. + }
  10005. +
  10006. + for (i = 0; i < blksize; i++)
  10007. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  10008. + DWC_PRINT(suffix);
  10009. +#endif
  10010. +}
  10011. +
  10012. +/**
  10013. + * Encrypts an array of bytes using the AES encryption engine.
  10014. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  10015. + * in-place.
  10016. + *
  10017. + * @return 0 on success, negative error code on error.
  10018. + */
  10019. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  10020. +{
  10021. + u8 block_t[16];
  10022. + DWC_MEMSET(block_t, 0, 16);
  10023. +
  10024. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  10025. +}
  10026. +
  10027. +/**
  10028. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  10029. + * This function takes a data string and returns the encrypted CBC
  10030. + * Counter-mode MIC.
  10031. + *
  10032. + * @param key The 128-bit symmetric key.
  10033. + * @param nonce The CCM nonce.
  10034. + * @param label The unique 14-byte ASCII text label.
  10035. + * @param bytes The byte array to be encrypted.
  10036. + * @param len Length of the byte array.
  10037. + * @param result Byte array to receive the 8-byte encrypted MIC.
  10038. + */
  10039. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  10040. + char *label, u8 *bytes, int len, u8 *result)
  10041. +{
  10042. + u8 block_m[16];
  10043. + u8 block_x[16];
  10044. + u8 block_t[8];
  10045. + int idx, blkNum;
  10046. + u16 la = (u16)(len + 14);
  10047. +
  10048. + /* Set the AES-128 key */
  10049. + //dwc_aes_setkey(tfm, key, 16);
  10050. +
  10051. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  10052. + block_m[0] = 0x59;
  10053. + for (idx = 0; idx < 13; idx++)
  10054. + block_m[idx + 1] = nonce[idx];
  10055. + block_m[14] = 0;
  10056. + block_m[15] = 0;
  10057. +
  10058. + /* Produce the CBC IV */
  10059. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  10060. + show_block(block_m, "CBC IV in: ", "\n", 0);
  10061. + show_block(block_x, "CBC IV out:", "\n", 0);
  10062. +
  10063. + /* Fill block B1 from l(a) = Blen + 14, and A */
  10064. + block_x[0] ^= (u8)(la >> 8);
  10065. + block_x[1] ^= (u8)la;
  10066. + for (idx = 0; idx < 14; idx++)
  10067. + block_x[idx + 2] ^= label[idx];
  10068. + show_block(block_x, "After xor: ", "b1\n", 16);
  10069. +
  10070. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  10071. + show_block(block_x, "After AES: ", "b1\n", 16);
  10072. +
  10073. + idx = 0;
  10074. + blkNum = 0;
  10075. +
  10076. + /* Fill remaining blocks with B */
  10077. + while (len-- > 0) {
  10078. + block_x[idx] ^= *bytes++;
  10079. + if (++idx >= 16) {
  10080. + idx = 0;
  10081. + show_block(block_x, "After xor: ", "\n", blkNum);
  10082. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  10083. + show_block(block_x, "After AES: ", "\n", blkNum);
  10084. + blkNum++;
  10085. + }
  10086. + }
  10087. +
  10088. + /* Handle partial last block */
  10089. + if (idx > 0) {
  10090. + show_block(block_x, "After xor: ", "\n", blkNum);
  10091. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  10092. + show_block(block_x, "After AES: ", "\n", blkNum);
  10093. + }
  10094. +
  10095. + /* Save the MIC tag */
  10096. + DWC_MEMCPY(block_t, block_x, 8);
  10097. + show_block(block_t, "MIC tag : ", NULL, 8);
  10098. +
  10099. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  10100. + block_m[0] = 0x01;
  10101. + block_m[14] = 0;
  10102. + block_m[15] = 0;
  10103. +
  10104. + /* Encrypt the counter */
  10105. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  10106. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  10107. +
  10108. + /* XOR with MIC tag */
  10109. + for (idx = 0; idx < 8; idx++) {
  10110. + block_t[idx] ^= block_x[idx];
  10111. + }
  10112. +
  10113. + /* Return result to caller */
  10114. + DWC_MEMCPY(result, block_t, 8);
  10115. + show_block(result, "CCM-MIC : ", NULL, 8);
  10116. +
  10117. +}
  10118. +
  10119. +/**
  10120. + * The PRF function described in section 6.5 of the WUSB spec. This function
  10121. + * concatenates MIC values returned from dwc_cmf() to create a value of
  10122. + * the requested length.
  10123. + *
  10124. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  10125. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  10126. + * @param result Byte array to receive the result.
  10127. + */
  10128. +void dwc_wusb_prf(int prf_len, u8 *key,
  10129. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  10130. +{
  10131. + int i;
  10132. +
  10133. + nonce[0] = 0;
  10134. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  10135. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  10136. + result += 8;
  10137. + }
  10138. +}
  10139. +
  10140. +/**
  10141. + * Fills in CCM Nonce per the WUSB spec.
  10142. + *
  10143. + * @param[in] haddr Host address.
  10144. + * @param[in] daddr Device address.
  10145. + * @param[in] tkid Session Key(PTK) identifier.
  10146. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  10147. + */
  10148. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  10149. + uint8_t *nonce)
  10150. +{
  10151. +
  10152. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  10153. +
  10154. + DWC_MEMSET(&nonce[0], 0, 16);
  10155. +
  10156. + DWC_MEMCPY(&nonce[6], tkid, 3);
  10157. + nonce[9] = daddr & 0xFF;
  10158. + nonce[10] = (daddr >> 8) & 0xFF;
  10159. + nonce[11] = haddr & 0xFF;
  10160. + nonce[12] = (haddr >> 8) & 0xFF;
  10161. +
  10162. + dump_bytes("CCM nonce", nonce, 16);
  10163. +}
  10164. +
  10165. +/**
  10166. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  10167. + * Nonce.
  10168. + */
  10169. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  10170. +{
  10171. + uint8_t inonce[16];
  10172. + uint32_t temp[4];
  10173. +
  10174. + /* Fill in the Nonce */
  10175. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  10176. + inonce[9] = addr & 0xFF;
  10177. + inonce[10] = (addr >> 8) & 0xFF;
  10178. + inonce[11] = inonce[9];
  10179. + inonce[12] = inonce[10];
  10180. +
  10181. + /* Collect "randomness samples" */
  10182. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  10183. +
  10184. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  10185. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  10186. + nonce);
  10187. +}
  10188. +
  10189. +/**
  10190. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  10191. + * WUSB spec.
  10192. + *
  10193. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  10194. + * @param[in] mk Master Key to derive the session from
  10195. + * @param[in] hnonce Pointer to Host Nonce.
  10196. + * @param[in] dnonce Pointer to Device Nonce.
  10197. + * @param[out] kck Pointer to where the KCK output is to be written.
  10198. + * @param[out] ptk Pointer to where the PTK output is to be written.
  10199. + */
  10200. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  10201. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  10202. +{
  10203. + uint8_t idata[32];
  10204. + uint8_t odata[32];
  10205. +
  10206. + dump_bytes("ck", mk, 16);
  10207. + dump_bytes("hnonce", hnonce, 16);
  10208. + dump_bytes("dnonce", dnonce, 16);
  10209. +
  10210. + /* The data is the HNonce and DNonce concatenated */
  10211. + DWC_MEMCPY(&idata[0], hnonce, 16);
  10212. + DWC_MEMCPY(&idata[16], dnonce, 16);
  10213. +
  10214. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  10215. +
  10216. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  10217. + DWC_MEMCPY(kck, &odata[0], 16);
  10218. + DWC_MEMCPY(ptk, &odata[16], 16);
  10219. +
  10220. + dump_bytes("kck", kck, 16);
  10221. + dump_bytes("ptk", ptk, 16);
  10222. +}
  10223. +
  10224. +/**
  10225. + * Generates the Message Integrity Code over the Handshake data per the
  10226. + * WUSB spec.
  10227. + *
  10228. + * @param ccm_nonce Pointer to CCM Nonce.
  10229. + * @param kck Pointer to Key Confirmation Key.
  10230. + * @param data Pointer to Handshake data to be checked.
  10231. + * @param mic Pointer to where the MIC output is to be written.
  10232. + */
  10233. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  10234. + uint8_t *data, uint8_t *mic)
  10235. +{
  10236. +
  10237. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  10238. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  10239. +}
  10240. +
  10241. +#endif /* DWC_CRYPTOLIB */
  10242. --- /dev/null
  10243. +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.h
  10244. @@ -0,0 +1,111 @@
  10245. +/* =========================================================================
  10246. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  10247. + * $Revision: #3 $
  10248. + * $Date: 2010/09/28 $
  10249. + * $Change: 1596182 $
  10250. + *
  10251. + * Synopsys Portability Library Software and documentation
  10252. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10253. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10254. + * between Synopsys and you.
  10255. + *
  10256. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10257. + * under any End User Software License Agreement or Agreement for
  10258. + * Licensed Product with Synopsys or any supplement thereto. You are
  10259. + * permitted to use and redistribute this Software in source and binary
  10260. + * forms, with or without modification, provided that redistributions
  10261. + * of source code must retain this notice. You may not view, use,
  10262. + * disclose, copy or distribute this file or any information contained
  10263. + * herein except pursuant to this license grant from Synopsys. If you
  10264. + * do not agree with this notice, including the disclaimer below, then
  10265. + * you are not authorized to use the Software.
  10266. + *
  10267. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10268. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10269. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10270. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10271. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10272. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10273. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10274. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10275. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10276. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10277. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10278. + * DAMAGE.
  10279. + * ========================================================================= */
  10280. +
  10281. +#ifndef _DWC_CRYPTO_H_
  10282. +#define _DWC_CRYPTO_H_
  10283. +
  10284. +#ifdef __cplusplus
  10285. +extern "C" {
  10286. +#endif
  10287. +
  10288. +/** @file
  10289. + *
  10290. + * This file contains declarations for the WUSB Cryptographic routines as
  10291. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  10292. + * modules.
  10293. + */
  10294. +
  10295. +#include "dwc_os.h"
  10296. +
  10297. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  10298. +
  10299. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  10300. + char *label, u8 *bytes, int len, u8 *result);
  10301. +void dwc_wusb_prf(int prf_len, u8 *key,
  10302. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  10303. +
  10304. +/**
  10305. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  10306. + *
  10307. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10308. + */
  10309. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  10310. + char *label, u8 *bytes, int len, u8 *result)
  10311. +{
  10312. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  10313. +}
  10314. +
  10315. +/**
  10316. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  10317. + *
  10318. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10319. + */
  10320. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  10321. + char *label, u8 *bytes, int len, u8 *result)
  10322. +{
  10323. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  10324. +}
  10325. +
  10326. +/**
  10327. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  10328. + *
  10329. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10330. + */
  10331. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  10332. + char *label, u8 *bytes, int len, u8 *result)
  10333. +{
  10334. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  10335. +}
  10336. +
  10337. +
  10338. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  10339. + uint8_t *nonce);
  10340. +void dwc_wusb_gen_nonce(uint16_t addr,
  10341. + uint8_t *nonce);
  10342. +
  10343. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  10344. + uint8_t *hnonce, uint8_t *dnonce,
  10345. + uint8_t *kck, uint8_t *ptk);
  10346. +
  10347. +
  10348. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  10349. + *kck, uint8_t *data, uint8_t *mic);
  10350. +
  10351. +#ifdef __cplusplus
  10352. +}
  10353. +#endif
  10354. +
  10355. +#endif /* _DWC_CRYPTO_H_ */
  10356. --- /dev/null
  10357. +++ b/drivers/usb/host/dwc_common_port/dwc_dh.c
  10358. @@ -0,0 +1,291 @@
  10359. +/* =========================================================================
  10360. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  10361. + * $Revision: #3 $
  10362. + * $Date: 2010/09/28 $
  10363. + * $Change: 1596182 $
  10364. + *
  10365. + * Synopsys Portability Library Software and documentation
  10366. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10367. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10368. + * between Synopsys and you.
  10369. + *
  10370. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10371. + * under any End User Software License Agreement or Agreement for
  10372. + * Licensed Product with Synopsys or any supplement thereto. You are
  10373. + * permitted to use and redistribute this Software in source and binary
  10374. + * forms, with or without modification, provided that redistributions
  10375. + * of source code must retain this notice. You may not view, use,
  10376. + * disclose, copy or distribute this file or any information contained
  10377. + * herein except pursuant to this license grant from Synopsys. If you
  10378. + * do not agree with this notice, including the disclaimer below, then
  10379. + * you are not authorized to use the Software.
  10380. + *
  10381. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10382. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10383. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10384. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10385. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10386. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10387. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10388. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10389. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10390. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10391. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10392. + * DAMAGE.
  10393. + * ========================================================================= */
  10394. +#ifdef DWC_CRYPTOLIB
  10395. +
  10396. +#ifndef CONFIG_MACH_IPMATE
  10397. +
  10398. +#include "dwc_dh.h"
  10399. +#include "dwc_modpow.h"
  10400. +
  10401. +#ifdef DEBUG
  10402. +/* This function prints out a buffer in the format described in the Association
  10403. + * Model specification. */
  10404. +static void dh_dump(char *str, void *_num, int len)
  10405. +{
  10406. + uint8_t *num = _num;
  10407. + int i;
  10408. + DWC_PRINTF("%s\n", str);
  10409. + for (i = 0; i < len; i ++) {
  10410. + DWC_PRINTF("%02x", num[i]);
  10411. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  10412. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  10413. + }
  10414. +
  10415. + DWC_PRINTF("\n");
  10416. +}
  10417. +#else
  10418. +#define dh_dump(_x...) do {; } while(0)
  10419. +#endif
  10420. +
  10421. +/* Constant g value */
  10422. +static __u32 dh_g[] = {
  10423. + 0x02000000,
  10424. +};
  10425. +
  10426. +/* Constant p value */
  10427. +static __u32 dh_p[] = {
  10428. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  10429. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  10430. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  10431. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  10432. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  10433. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  10434. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  10435. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  10436. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  10437. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  10438. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  10439. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  10440. +};
  10441. +
  10442. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  10443. +{
  10444. + uint8_t *in = _in;
  10445. + uint8_t *out = _out;
  10446. + int i;
  10447. + for (i=0; i<len; i++) {
  10448. + out[i] = in[len-1-i];
  10449. + }
  10450. +}
  10451. +
  10452. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  10453. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  10454. + * of 4. */
  10455. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  10456. + void *exp, uint32_t exp_len,
  10457. + void *mod, uint32_t mod_len,
  10458. + void *out)
  10459. +{
  10460. + /* modpow() takes little endian numbers. AM uses big-endian. This
  10461. + * function swaps bytes of numbers before passing onto modpow. */
  10462. +
  10463. + int retval = 0;
  10464. + uint32_t *result;
  10465. +
  10466. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  10467. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  10468. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  10469. +
  10470. + dh_swap_bytes(num, &bignum_num[1], num_len);
  10471. + bignum_num[0] = num_len / 4;
  10472. +
  10473. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  10474. + bignum_exp[0] = exp_len / 4;
  10475. +
  10476. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  10477. + bignum_mod[0] = mod_len / 4;
  10478. +
  10479. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  10480. + if (!result) {
  10481. + retval = -1;
  10482. + goto dh_modpow_nomem;
  10483. + }
  10484. +
  10485. + dh_swap_bytes(&result[1], out, result[0] * 4);
  10486. + dwc_free(mem_ctx, result);
  10487. +
  10488. + dh_modpow_nomem:
  10489. + dwc_free(mem_ctx, bignum_num);
  10490. + dwc_free(mem_ctx, bignum_exp);
  10491. + dwc_free(mem_ctx, bignum_mod);
  10492. + return retval;
  10493. +}
  10494. +
  10495. +
  10496. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  10497. +{
  10498. + int retval;
  10499. + uint8_t m3[385];
  10500. +
  10501. +#ifndef DH_TEST_VECTORS
  10502. + DWC_RANDOM_BYTES(exp, 32);
  10503. +#endif
  10504. +
  10505. + /* Compute the pkd */
  10506. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  10507. + exp, 32,
  10508. + dh_p, 384, pk))) {
  10509. + return retval;
  10510. + }
  10511. +
  10512. + m3[384] = nd;
  10513. + DWC_MEMCPY(&m3[0], pk, 384);
  10514. + DWC_SHA256(m3, 385, hash);
  10515. +
  10516. + dh_dump("PK", pk, 384);
  10517. + dh_dump("SHA-256(M3)", hash, 32);
  10518. + return 0;
  10519. +}
  10520. +
  10521. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  10522. + uint8_t *exp, int is_host,
  10523. + char *dd, uint8_t *ck, uint8_t *kdk)
  10524. +{
  10525. + int retval;
  10526. + uint8_t mv[784];
  10527. + uint8_t sha_result[32];
  10528. + uint8_t dhkey[384];
  10529. + uint8_t shared_secret[384];
  10530. + char *message;
  10531. + uint32_t vd;
  10532. +
  10533. + uint8_t *pk;
  10534. +
  10535. + if (is_host) {
  10536. + pk = pkd;
  10537. + }
  10538. + else {
  10539. + pk = pkh;
  10540. + }
  10541. +
  10542. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  10543. + exp, 32,
  10544. + dh_p, 384, shared_secret))) {
  10545. + return retval;
  10546. + }
  10547. + dh_dump("Shared Secret", shared_secret, 384);
  10548. +
  10549. + DWC_SHA256(shared_secret, 384, dhkey);
  10550. + dh_dump("DHKEY", dhkey, 384);
  10551. +
  10552. + DWC_MEMCPY(&mv[0], pkd, 384);
  10553. + DWC_MEMCPY(&mv[384], pkh, 384);
  10554. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  10555. + dh_dump("MV", mv, 784);
  10556. +
  10557. + DWC_SHA256(mv, 784, sha_result);
  10558. + dh_dump("SHA-256(MV)", sha_result, 32);
  10559. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  10560. +
  10561. + dh_swap_bytes(sha_result, &vd, 4);
  10562. +#ifdef DEBUG
  10563. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  10564. +#endif
  10565. +
  10566. + switch (nd) {
  10567. + case 2:
  10568. + vd = vd % 100;
  10569. + DWC_SPRINTF(dd, "%02d", vd);
  10570. + break;
  10571. + case 3:
  10572. + vd = vd % 1000;
  10573. + DWC_SPRINTF(dd, "%03d", vd);
  10574. + break;
  10575. + case 4:
  10576. + vd = vd % 10000;
  10577. + DWC_SPRINTF(dd, "%04d", vd);
  10578. + break;
  10579. + }
  10580. +#ifdef DEBUG
  10581. + DWC_PRINTF("Display Digits: %s\n", dd);
  10582. +#endif
  10583. +
  10584. + message = "connection key";
  10585. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  10586. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  10587. + DWC_MEMCPY(ck, sha_result, 16);
  10588. +
  10589. + message = "key derivation key";
  10590. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  10591. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  10592. + DWC_MEMCPY(kdk, sha_result, 32);
  10593. +
  10594. + return 0;
  10595. +}
  10596. +
  10597. +
  10598. +#ifdef DH_TEST_VECTORS
  10599. +
  10600. +static __u8 dh_a[] = {
  10601. + 0x44, 0x00, 0x51, 0xd6,
  10602. + 0xf0, 0xb5, 0x5e, 0xa9,
  10603. + 0x67, 0xab, 0x31, 0xc6,
  10604. + 0x8a, 0x8b, 0x5e, 0x37,
  10605. + 0xd9, 0x10, 0xda, 0xe0,
  10606. + 0xe2, 0xd4, 0x59, 0xa4,
  10607. + 0x86, 0x45, 0x9c, 0xaa,
  10608. + 0xdf, 0x36, 0x75, 0x16,
  10609. +};
  10610. +
  10611. +static __u8 dh_b[] = {
  10612. + 0x5d, 0xae, 0xc7, 0x86,
  10613. + 0x79, 0x80, 0xa3, 0x24,
  10614. + 0x8c, 0xe3, 0x57, 0x8f,
  10615. + 0xc7, 0x5f, 0x1b, 0x0f,
  10616. + 0x2d, 0xf8, 0x9d, 0x30,
  10617. + 0x6f, 0xa4, 0x52, 0xcd,
  10618. + 0xe0, 0x7a, 0x04, 0x8a,
  10619. + 0xde, 0xd9, 0x26, 0x56,
  10620. +};
  10621. +
  10622. +void dwc_run_dh_test_vectors(void *mem_ctx)
  10623. +{
  10624. + uint8_t pkd[384];
  10625. + uint8_t pkh[384];
  10626. + uint8_t hashd[32];
  10627. + uint8_t hashh[32];
  10628. + uint8_t ck[16];
  10629. + uint8_t kdk[32];
  10630. + char dd[5];
  10631. +
  10632. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  10633. +
  10634. + /* compute the PKd and SHA-256(PKd || Nd) */
  10635. + DWC_PRINTF("Computing PKd\n");
  10636. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  10637. +
  10638. + /* compute the PKd and SHA-256(PKh || Nd) */
  10639. + DWC_PRINTF("Computing PKh\n");
  10640. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  10641. +
  10642. + /* compute the dhkey */
  10643. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  10644. +}
  10645. +#endif /* DH_TEST_VECTORS */
  10646. +
  10647. +#endif /* !CONFIG_MACH_IPMATE */
  10648. +
  10649. +#endif /* DWC_CRYPTOLIB */
  10650. --- /dev/null
  10651. +++ b/drivers/usb/host/dwc_common_port/dwc_dh.h
  10652. @@ -0,0 +1,106 @@
  10653. +/* =========================================================================
  10654. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  10655. + * $Revision: #4 $
  10656. + * $Date: 2010/09/28 $
  10657. + * $Change: 1596182 $
  10658. + *
  10659. + * Synopsys Portability Library Software and documentation
  10660. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10661. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10662. + * between Synopsys and you.
  10663. + *
  10664. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10665. + * under any End User Software License Agreement or Agreement for
  10666. + * Licensed Product with Synopsys or any supplement thereto. You are
  10667. + * permitted to use and redistribute this Software in source and binary
  10668. + * forms, with or without modification, provided that redistributions
  10669. + * of source code must retain this notice. You may not view, use,
  10670. + * disclose, copy or distribute this file or any information contained
  10671. + * herein except pursuant to this license grant from Synopsys. If you
  10672. + * do not agree with this notice, including the disclaimer below, then
  10673. + * you are not authorized to use the Software.
  10674. + *
  10675. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10676. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10677. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10678. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10679. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10680. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10681. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10682. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10683. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10684. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10685. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10686. + * DAMAGE.
  10687. + * ========================================================================= */
  10688. +#ifndef _DWC_DH_H_
  10689. +#define _DWC_DH_H_
  10690. +
  10691. +#ifdef __cplusplus
  10692. +extern "C" {
  10693. +#endif
  10694. +
  10695. +#include "dwc_os.h"
  10696. +
  10697. +/** @file
  10698. + *
  10699. + * This file defines the common functions on device and host for performing
  10700. + * numeric association as defined in the WUSB spec. They are only to be
  10701. + * used internally by the DWC UWB modules. */
  10702. +
  10703. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  10704. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  10705. + uint8_t *key, uint32_t keylen,
  10706. + uint8_t *out);
  10707. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  10708. + void *exp, uint32_t exp_len,
  10709. + void *mod, uint32_t mod_len,
  10710. + void *out);
  10711. +
  10712. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  10713. + *
  10714. + * PK = g^exp mod p.
  10715. + *
  10716. + * Input:
  10717. + * Nd = Number of digits on the device.
  10718. + *
  10719. + * Output:
  10720. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  10721. + * used as either A or B.
  10722. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  10723. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  10724. + */
  10725. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  10726. +
  10727. +/** Computes the DHKEY, and VD.
  10728. + *
  10729. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  10730. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  10731. + *
  10732. + * Input:
  10733. + * pkd = The PKD value.
  10734. + * pkh = The PKH value.
  10735. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  10736. + * is_host = Set to non zero if a WUSB host is calling this function.
  10737. + *
  10738. + * Output:
  10739. +
  10740. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  10741. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  10742. + * null termination character. This buffer can be used directly for display.
  10743. + * ck = A 16-byte buffer to be filled with the CK.
  10744. + * kdk = A 32-byte buffer to be filled with the KDK.
  10745. + */
  10746. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  10747. + uint8_t *exp, int is_host,
  10748. + char *dd, uint8_t *ck, uint8_t *kdk);
  10749. +
  10750. +#ifdef DH_TEST_VECTORS
  10751. +extern void dwc_run_dh_test_vectors(void);
  10752. +#endif
  10753. +
  10754. +#ifdef __cplusplus
  10755. +}
  10756. +#endif
  10757. +
  10758. +#endif /* _DWC_DH_H_ */
  10759. --- /dev/null
  10760. +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
  10761. @@ -0,0 +1,594 @@
  10762. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  10763. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  10764. +
  10765. +/*
  10766. + * Copyright (c) 1991, 1993
  10767. + * The Regents of the University of California. All rights reserved.
  10768. + *
  10769. + * Redistribution and use in source and binary forms, with or without
  10770. + * modification, are permitted provided that the following conditions
  10771. + * are met:
  10772. + * 1. Redistributions of source code must retain the above copyright
  10773. + * notice, this list of conditions and the following disclaimer.
  10774. + * 2. Redistributions in binary form must reproduce the above copyright
  10775. + * notice, this list of conditions and the following disclaimer in the
  10776. + * documentation and/or other materials provided with the distribution.
  10777. + * 3. Neither the name of the University nor the names of its contributors
  10778. + * may be used to endorse or promote products derived from this software
  10779. + * without specific prior written permission.
  10780. + *
  10781. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  10782. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  10783. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  10784. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  10785. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  10786. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  10787. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  10788. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  10789. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  10790. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  10791. + * SUCH DAMAGE.
  10792. + *
  10793. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  10794. + */
  10795. +
  10796. +#ifndef _DWC_LIST_H_
  10797. +#define _DWC_LIST_H_
  10798. +
  10799. +#ifdef __cplusplus
  10800. +extern "C" {
  10801. +#endif
  10802. +
  10803. +/** @file
  10804. + *
  10805. + * This file defines linked list operations. It is derived from BSD with
  10806. + * only the MACRO names being prefixed with DWC_. This is because a few of
  10807. + * these names conflict with those on Linux. For documentation on use, see the
  10808. + * inline comments in the source code. The original license for this source
  10809. + * code applies and is preserved in the dwc_list.h source file.
  10810. + */
  10811. +
  10812. +/*
  10813. + * This file defines five types of data structures: singly-linked lists,
  10814. + * lists, simple queues, tail queues, and circular queues.
  10815. + *
  10816. + *
  10817. + * A singly-linked list is headed by a single forward pointer. The elements
  10818. + * are singly linked for minimum space and pointer manipulation overhead at
  10819. + * the expense of O(n) removal for arbitrary elements. New elements can be
  10820. + * added to the list after an existing element or at the head of the list.
  10821. + * Elements being removed from the head of the list should use the explicit
  10822. + * macro for this purpose for optimum efficiency. A singly-linked list may
  10823. + * only be traversed in the forward direction. Singly-linked lists are ideal
  10824. + * for applications with large datasets and few or no removals or for
  10825. + * implementing a LIFO queue.
  10826. + *
  10827. + * A list is headed by a single forward pointer (or an array of forward
  10828. + * pointers for a hash table header). The elements are doubly linked
  10829. + * so that an arbitrary element can be removed without a need to
  10830. + * traverse the list. New elements can be added to the list before
  10831. + * or after an existing element or at the head of the list. A list
  10832. + * may only be traversed in the forward direction.
  10833. + *
  10834. + * A simple queue is headed by a pair of pointers, one the head of the
  10835. + * list and the other to the tail of the list. The elements are singly
  10836. + * linked to save space, so elements can only be removed from the
  10837. + * head of the list. New elements can be added to the list before or after
  10838. + * an existing element, at the head of the list, or at the end of the
  10839. + * list. A simple queue may only be traversed in the forward direction.
  10840. + *
  10841. + * A tail queue is headed by a pair of pointers, one to the head of the
  10842. + * list and the other to the tail of the list. The elements are doubly
  10843. + * linked so that an arbitrary element can be removed without a need to
  10844. + * traverse the list. New elements can be added to the list before or
  10845. + * after an existing element, at the head of the list, or at the end of
  10846. + * the list. A tail queue may be traversed in either direction.
  10847. + *
  10848. + * A circle queue is headed by a pair of pointers, one to the head of the
  10849. + * list and the other to the tail of the list. The elements are doubly
  10850. + * linked so that an arbitrary element can be removed without a need to
  10851. + * traverse the list. New elements can be added to the list before or after
  10852. + * an existing element, at the head of the list, or at the end of the list.
  10853. + * A circle queue may be traversed in either direction, but has a more
  10854. + * complex end of list detection.
  10855. + *
  10856. + * For details on the use of these macros, see the queue(3) manual page.
  10857. + */
  10858. +
  10859. +/*
  10860. + * Double-linked List.
  10861. + */
  10862. +
  10863. +typedef struct dwc_list_link {
  10864. + struct dwc_list_link *next;
  10865. + struct dwc_list_link *prev;
  10866. +} dwc_list_link_t;
  10867. +
  10868. +#define DWC_LIST_INIT(link) do { \
  10869. + (link)->next = (link); \
  10870. + (link)->prev = (link); \
  10871. +} while (0)
  10872. +
  10873. +#define DWC_LIST_FIRST(link) ((link)->next)
  10874. +#define DWC_LIST_LAST(link) ((link)->prev)
  10875. +#define DWC_LIST_END(link) (link)
  10876. +#define DWC_LIST_NEXT(link) ((link)->next)
  10877. +#define DWC_LIST_PREV(link) ((link)->prev)
  10878. +#define DWC_LIST_EMPTY(link) \
  10879. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  10880. +#define DWC_LIST_ENTRY(link, type, field) \
  10881. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  10882. +
  10883. +#if 0
  10884. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  10885. + (link)->next = (list)->next; \
  10886. + (link)->prev = (list); \
  10887. + (list)->next->prev = (link); \
  10888. + (list)->next = (link); \
  10889. +} while (0)
  10890. +
  10891. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  10892. + (link)->next = (list); \
  10893. + (link)->prev = (list)->prev; \
  10894. + (list)->prev->next = (link); \
  10895. + (list)->prev = (link); \
  10896. +} while (0)
  10897. +#else
  10898. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  10899. + dwc_list_link_t *__next__ = (list)->next; \
  10900. + __next__->prev = (link); \
  10901. + (link)->next = __next__; \
  10902. + (link)->prev = (list); \
  10903. + (list)->next = (link); \
  10904. +} while (0)
  10905. +
  10906. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  10907. + dwc_list_link_t *__prev__ = (list)->prev; \
  10908. + (list)->prev = (link); \
  10909. + (link)->next = (list); \
  10910. + (link)->prev = __prev__; \
  10911. + __prev__->next = (link); \
  10912. +} while (0)
  10913. +#endif
  10914. +
  10915. +#if 0
  10916. +static inline void __list_add(struct list_head *new,
  10917. + struct list_head *prev,
  10918. + struct list_head *next)
  10919. +{
  10920. + next->prev = new;
  10921. + new->next = next;
  10922. + new->prev = prev;
  10923. + prev->next = new;
  10924. +}
  10925. +
  10926. +static inline void list_add(struct list_head *new, struct list_head *head)
  10927. +{
  10928. + __list_add(new, head, head->next);
  10929. +}
  10930. +
  10931. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  10932. +{
  10933. + __list_add(new, head->prev, head);
  10934. +}
  10935. +
  10936. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  10937. +{
  10938. + next->prev = prev;
  10939. + prev->next = next;
  10940. +}
  10941. +
  10942. +static inline void list_del(struct list_head *entry)
  10943. +{
  10944. + __list_del(entry->prev, entry->next);
  10945. + entry->next = LIST_POISON1;
  10946. + entry->prev = LIST_POISON2;
  10947. +}
  10948. +#endif
  10949. +
  10950. +#define DWC_LIST_REMOVE(link) do { \
  10951. + (link)->next->prev = (link)->prev; \
  10952. + (link)->prev->next = (link)->next; \
  10953. +} while (0)
  10954. +
  10955. +#define DWC_LIST_REMOVE_INIT(link) do { \
  10956. + DWC_LIST_REMOVE(link); \
  10957. + DWC_LIST_INIT(link); \
  10958. +} while (0)
  10959. +
  10960. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  10961. + DWC_LIST_REMOVE(link); \
  10962. + DWC_LIST_INSERT_HEAD(list, link); \
  10963. +} while (0)
  10964. +
  10965. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  10966. + DWC_LIST_REMOVE(link); \
  10967. + DWC_LIST_INSERT_TAIL(list, link); \
  10968. +} while (0)
  10969. +
  10970. +#define DWC_LIST_FOREACH(var, list) \
  10971. + for((var) = DWC_LIST_FIRST(list); \
  10972. + (var) != DWC_LIST_END(list); \
  10973. + (var) = DWC_LIST_NEXT(var))
  10974. +
  10975. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  10976. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  10977. + (var) != DWC_LIST_END(list); \
  10978. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  10979. +
  10980. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  10981. + for((var) = DWC_LIST_LAST(list); \
  10982. + (var) != DWC_LIST_END(list); \
  10983. + (var) = DWC_LIST_PREV(var))
  10984. +
  10985. +/*
  10986. + * Singly-linked List definitions.
  10987. + */
  10988. +#define DWC_SLIST_HEAD(name, type) \
  10989. +struct name { \
  10990. + struct type *slh_first; /* first element */ \
  10991. +}
  10992. +
  10993. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  10994. + { NULL }
  10995. +
  10996. +#define DWC_SLIST_ENTRY(type) \
  10997. +struct { \
  10998. + struct type *sle_next; /* next element */ \
  10999. +}
  11000. +
  11001. +/*
  11002. + * Singly-linked List access methods.
  11003. + */
  11004. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  11005. +#define DWC_SLIST_END(head) NULL
  11006. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  11007. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  11008. +
  11009. +#define DWC_SLIST_FOREACH(var, head, field) \
  11010. + for((var) = SLIST_FIRST(head); \
  11011. + (var) != SLIST_END(head); \
  11012. + (var) = SLIST_NEXT(var, field))
  11013. +
  11014. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  11015. + for((varp) = &SLIST_FIRST((head)); \
  11016. + ((var) = *(varp)) != SLIST_END(head); \
  11017. + (varp) = &SLIST_NEXT((var), field))
  11018. +
  11019. +/*
  11020. + * Singly-linked List functions.
  11021. + */
  11022. +#define DWC_SLIST_INIT(head) { \
  11023. + SLIST_FIRST(head) = SLIST_END(head); \
  11024. +}
  11025. +
  11026. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  11027. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  11028. + (slistelm)->field.sle_next = (elm); \
  11029. +} while (0)
  11030. +
  11031. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  11032. + (elm)->field.sle_next = (head)->slh_first; \
  11033. + (head)->slh_first = (elm); \
  11034. +} while (0)
  11035. +
  11036. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  11037. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  11038. +} while (0)
  11039. +
  11040. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  11041. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  11042. +} while (0)
  11043. +
  11044. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  11045. + if ((head)->slh_first == (elm)) { \
  11046. + SLIST_REMOVE_HEAD((head), field); \
  11047. + } \
  11048. + else { \
  11049. + struct type *curelm = (head)->slh_first; \
  11050. + while( curelm->field.sle_next != (elm) ) \
  11051. + curelm = curelm->field.sle_next; \
  11052. + curelm->field.sle_next = \
  11053. + curelm->field.sle_next->field.sle_next; \
  11054. + } \
  11055. +} while (0)
  11056. +
  11057. +/*
  11058. + * Simple queue definitions.
  11059. + */
  11060. +#define DWC_SIMPLEQ_HEAD(name, type) \
  11061. +struct name { \
  11062. + struct type *sqh_first; /* first element */ \
  11063. + struct type **sqh_last; /* addr of last next element */ \
  11064. +}
  11065. +
  11066. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  11067. + { NULL, &(head).sqh_first }
  11068. +
  11069. +#define DWC_SIMPLEQ_ENTRY(type) \
  11070. +struct { \
  11071. + struct type *sqe_next; /* next element */ \
  11072. +}
  11073. +
  11074. +/*
  11075. + * Simple queue access methods.
  11076. + */
  11077. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  11078. +#define DWC_SIMPLEQ_END(head) NULL
  11079. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  11080. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  11081. +
  11082. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  11083. + for((var) = SIMPLEQ_FIRST(head); \
  11084. + (var) != SIMPLEQ_END(head); \
  11085. + (var) = SIMPLEQ_NEXT(var, field))
  11086. +
  11087. +/*
  11088. + * Simple queue functions.
  11089. + */
  11090. +#define DWC_SIMPLEQ_INIT(head) do { \
  11091. + (head)->sqh_first = NULL; \
  11092. + (head)->sqh_last = &(head)->sqh_first; \
  11093. +} while (0)
  11094. +
  11095. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  11096. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  11097. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11098. + (head)->sqh_first = (elm); \
  11099. +} while (0)
  11100. +
  11101. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  11102. + (elm)->field.sqe_next = NULL; \
  11103. + *(head)->sqh_last = (elm); \
  11104. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11105. +} while (0)
  11106. +
  11107. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11108. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  11109. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11110. + (listelm)->field.sqe_next = (elm); \
  11111. +} while (0)
  11112. +
  11113. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  11114. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  11115. + (head)->sqh_last = &(head)->sqh_first; \
  11116. +} while (0)
  11117. +
  11118. +/*
  11119. + * Tail queue definitions.
  11120. + */
  11121. +#define DWC_TAILQ_HEAD(name, type) \
  11122. +struct name { \
  11123. + struct type *tqh_first; /* first element */ \
  11124. + struct type **tqh_last; /* addr of last next element */ \
  11125. +}
  11126. +
  11127. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  11128. + { NULL, &(head).tqh_first }
  11129. +
  11130. +#define DWC_TAILQ_ENTRY(type) \
  11131. +struct { \
  11132. + struct type *tqe_next; /* next element */ \
  11133. + struct type **tqe_prev; /* address of previous next element */ \
  11134. +}
  11135. +
  11136. +/*
  11137. + * tail queue access methods
  11138. + */
  11139. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  11140. +#define DWC_TAILQ_END(head) NULL
  11141. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  11142. +#define DWC_TAILQ_LAST(head, headname) \
  11143. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  11144. +/* XXX */
  11145. +#define DWC_TAILQ_PREV(elm, headname, field) \
  11146. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  11147. +#define DWC_TAILQ_EMPTY(head) \
  11148. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  11149. +
  11150. +#define DWC_TAILQ_FOREACH(var, head, field) \
  11151. + for ((var) = DWC_TAILQ_FIRST(head); \
  11152. + (var) != DWC_TAILQ_END(head); \
  11153. + (var) = DWC_TAILQ_NEXT(var, field))
  11154. +
  11155. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  11156. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  11157. + (var) != DWC_TAILQ_END(head); \
  11158. + (var) = DWC_TAILQ_PREV(var, headname, field))
  11159. +
  11160. +/*
  11161. + * Tail queue functions.
  11162. + */
  11163. +#define DWC_TAILQ_INIT(head) do { \
  11164. + (head)->tqh_first = NULL; \
  11165. + (head)->tqh_last = &(head)->tqh_first; \
  11166. +} while (0)
  11167. +
  11168. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  11169. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  11170. + (head)->tqh_first->field.tqe_prev = \
  11171. + &(elm)->field.tqe_next; \
  11172. + else \
  11173. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11174. + (head)->tqh_first = (elm); \
  11175. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  11176. +} while (0)
  11177. +
  11178. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  11179. + (elm)->field.tqe_next = NULL; \
  11180. + (elm)->field.tqe_prev = (head)->tqh_last; \
  11181. + *(head)->tqh_last = (elm); \
  11182. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11183. +} while (0)
  11184. +
  11185. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11186. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  11187. + (elm)->field.tqe_next->field.tqe_prev = \
  11188. + &(elm)->field.tqe_next; \
  11189. + else \
  11190. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11191. + (listelm)->field.tqe_next = (elm); \
  11192. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  11193. +} while (0)
  11194. +
  11195. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  11196. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  11197. + (elm)->field.tqe_next = (listelm); \
  11198. + *(listelm)->field.tqe_prev = (elm); \
  11199. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  11200. +} while (0)
  11201. +
  11202. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  11203. + if (((elm)->field.tqe_next) != NULL) \
  11204. + (elm)->field.tqe_next->field.tqe_prev = \
  11205. + (elm)->field.tqe_prev; \
  11206. + else \
  11207. + (head)->tqh_last = (elm)->field.tqe_prev; \
  11208. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  11209. +} while (0)
  11210. +
  11211. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  11212. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  11213. + (elm2)->field.tqe_next->field.tqe_prev = \
  11214. + &(elm2)->field.tqe_next; \
  11215. + else \
  11216. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  11217. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  11218. + *(elm2)->field.tqe_prev = (elm2); \
  11219. +} while (0)
  11220. +
  11221. +/*
  11222. + * Circular queue definitions.
  11223. + */
  11224. +#define DWC_CIRCLEQ_HEAD(name, type) \
  11225. +struct name { \
  11226. + struct type *cqh_first; /* first element */ \
  11227. + struct type *cqh_last; /* last element */ \
  11228. +}
  11229. +
  11230. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  11231. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  11232. +
  11233. +#define DWC_CIRCLEQ_ENTRY(type) \
  11234. +struct { \
  11235. + struct type *cqe_next; /* next element */ \
  11236. + struct type *cqe_prev; /* previous element */ \
  11237. +}
  11238. +
  11239. +/*
  11240. + * Circular queue access methods
  11241. + */
  11242. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  11243. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  11244. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  11245. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  11246. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  11247. +#define DWC_CIRCLEQ_EMPTY(head) \
  11248. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  11249. +
  11250. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  11251. +
  11252. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  11253. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  11254. + (var) != DWC_CIRCLEQ_END(head); \
  11255. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  11256. +
  11257. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  11258. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  11259. + (var) != DWC_CIRCLEQ_END(head); \
  11260. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  11261. +
  11262. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  11263. + for((var) = DWC_CIRCLEQ_LAST(head); \
  11264. + (var) != DWC_CIRCLEQ_END(head); \
  11265. + (var) = DWC_CIRCLEQ_PREV(var, field))
  11266. +
  11267. +/*
  11268. + * Circular queue functions.
  11269. + */
  11270. +#define DWC_CIRCLEQ_INIT(head) do { \
  11271. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  11272. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  11273. +} while (0)
  11274. +
  11275. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  11276. + (elm)->field.cqe_next = NULL; \
  11277. + (elm)->field.cqe_prev = NULL; \
  11278. +} while (0)
  11279. +
  11280. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11281. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  11282. + (elm)->field.cqe_prev = (listelm); \
  11283. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  11284. + (head)->cqh_last = (elm); \
  11285. + else \
  11286. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  11287. + (listelm)->field.cqe_next = (elm); \
  11288. +} while (0)
  11289. +
  11290. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  11291. + (elm)->field.cqe_next = (listelm); \
  11292. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  11293. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  11294. + (head)->cqh_first = (elm); \
  11295. + else \
  11296. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  11297. + (listelm)->field.cqe_prev = (elm); \
  11298. +} while (0)
  11299. +
  11300. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  11301. + (elm)->field.cqe_next = (head)->cqh_first; \
  11302. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  11303. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  11304. + (head)->cqh_last = (elm); \
  11305. + else \
  11306. + (head)->cqh_first->field.cqe_prev = (elm); \
  11307. + (head)->cqh_first = (elm); \
  11308. +} while (0)
  11309. +
  11310. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  11311. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  11312. + (elm)->field.cqe_prev = (head)->cqh_last; \
  11313. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  11314. + (head)->cqh_first = (elm); \
  11315. + else \
  11316. + (head)->cqh_last->field.cqe_next = (elm); \
  11317. + (head)->cqh_last = (elm); \
  11318. +} while (0)
  11319. +
  11320. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  11321. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  11322. + (head)->cqh_last = (elm)->field.cqe_prev; \
  11323. + else \
  11324. + (elm)->field.cqe_next->field.cqe_prev = \
  11325. + (elm)->field.cqe_prev; \
  11326. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  11327. + (head)->cqh_first = (elm)->field.cqe_next; \
  11328. + else \
  11329. + (elm)->field.cqe_prev->field.cqe_next = \
  11330. + (elm)->field.cqe_next; \
  11331. +} while (0)
  11332. +
  11333. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  11334. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  11335. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  11336. +} while (0)
  11337. +
  11338. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  11339. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  11340. + DWC_CIRCLEQ_END(head)) \
  11341. + (head).cqh_last = (elm2); \
  11342. + else \
  11343. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  11344. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  11345. + DWC_CIRCLEQ_END(head)) \
  11346. + (head).cqh_first = (elm2); \
  11347. + else \
  11348. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  11349. +} while (0)
  11350. +
  11351. +#ifdef __cplusplus
  11352. +}
  11353. +#endif
  11354. +
  11355. +#endif /* _DWC_LIST_H_ */
  11356. --- /dev/null
  11357. +++ b/drivers/usb/host/dwc_common_port/dwc_mem.c
  11358. @@ -0,0 +1,245 @@
  11359. +/* Memory Debugging */
  11360. +#ifdef DWC_DEBUG_MEMORY
  11361. +
  11362. +#include "dwc_os.h"
  11363. +#include "dwc_list.h"
  11364. +
  11365. +struct allocation {
  11366. + void *addr;
  11367. + void *ctx;
  11368. + char *func;
  11369. + int line;
  11370. + uint32_t size;
  11371. + int dma;
  11372. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  11373. +};
  11374. +
  11375. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  11376. +
  11377. +struct allocation_manager {
  11378. + void *mem_ctx;
  11379. + struct allocation_queue allocations;
  11380. +
  11381. + /* statistics */
  11382. + int num;
  11383. + int num_freed;
  11384. + int num_active;
  11385. + uint32_t total;
  11386. + uint32_t cur;
  11387. + uint32_t max;
  11388. +};
  11389. +
  11390. +static struct allocation_manager *manager = NULL;
  11391. +
  11392. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  11393. + int dma)
  11394. +{
  11395. + struct allocation *a;
  11396. +
  11397. + DWC_ASSERT(manager != NULL, "manager not allocated");
  11398. +
  11399. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  11400. + if (!a) {
  11401. + return -DWC_E_NO_MEMORY;
  11402. + }
  11403. +
  11404. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  11405. + if (!a->func) {
  11406. + __DWC_FREE(manager->mem_ctx, a);
  11407. + return -DWC_E_NO_MEMORY;
  11408. + }
  11409. +
  11410. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  11411. + a->addr = addr;
  11412. + a->ctx = ctx;
  11413. + a->line = line;
  11414. + a->size = size;
  11415. + a->dma = dma;
  11416. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  11417. +
  11418. + /* Update stats */
  11419. + manager->num++;
  11420. + manager->num_active++;
  11421. + manager->total += size;
  11422. + manager->cur += size;
  11423. +
  11424. + if (manager->max < manager->cur) {
  11425. + manager->max = manager->cur;
  11426. + }
  11427. +
  11428. + return 0;
  11429. +}
  11430. +
  11431. +static struct allocation *find_allocation(void *ctx, void *addr)
  11432. +{
  11433. + struct allocation *a;
  11434. +
  11435. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11436. + if (a->ctx == ctx && a->addr == addr) {
  11437. + return a;
  11438. + }
  11439. + }
  11440. +
  11441. + return NULL;
  11442. +}
  11443. +
  11444. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  11445. +{
  11446. + struct allocation *a = find_allocation(ctx, addr);
  11447. +
  11448. + if (!a) {
  11449. + DWC_ASSERT(0,
  11450. + "Free of address %p that was never allocated or already freed %s:%d",
  11451. + addr, func, line);
  11452. + return;
  11453. + }
  11454. +
  11455. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  11456. +
  11457. + manager->num_active--;
  11458. + manager->num_freed++;
  11459. + manager->cur -= a->size;
  11460. + __DWC_FREE(manager->mem_ctx, a->func);
  11461. + __DWC_FREE(manager->mem_ctx, a);
  11462. +}
  11463. +
  11464. +int dwc_memory_debug_start(void *mem_ctx)
  11465. +{
  11466. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  11467. +
  11468. + if (manager) {
  11469. + return -DWC_E_BUSY;
  11470. + }
  11471. +
  11472. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  11473. + if (!manager) {
  11474. + return -DWC_E_NO_MEMORY;
  11475. + }
  11476. +
  11477. + DWC_CIRCLEQ_INIT(&manager->allocations);
  11478. + manager->mem_ctx = mem_ctx;
  11479. + manager->num = 0;
  11480. + manager->num_freed = 0;
  11481. + manager->num_active = 0;
  11482. + manager->total = 0;
  11483. + manager->cur = 0;
  11484. + manager->max = 0;
  11485. +
  11486. + return 0;
  11487. +}
  11488. +
  11489. +void dwc_memory_debug_stop(void)
  11490. +{
  11491. + struct allocation *a;
  11492. +
  11493. + dwc_memory_debug_report();
  11494. +
  11495. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11496. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  11497. + free_allocation(a->ctx, a->addr, NULL, -1);
  11498. + }
  11499. +
  11500. + __DWC_FREE(manager->mem_ctx, manager);
  11501. +}
  11502. +
  11503. +void dwc_memory_debug_report(void)
  11504. +{
  11505. + struct allocation *a;
  11506. +
  11507. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  11508. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  11509. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  11510. + DWC_PRINTF("Active = %d\n", manager->num_active);
  11511. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  11512. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  11513. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  11514. + DWC_PRINTF("Unfreed allocations:\n");
  11515. +
  11516. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11517. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  11518. + a->addr, a->size, a->func, a->line, a->dma);
  11519. + }
  11520. +}
  11521. +
  11522. +/* The replacement functions */
  11523. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  11524. +{
  11525. + void *addr = __DWC_ALLOC(mem_ctx, size);
  11526. +
  11527. + if (!addr) {
  11528. + return NULL;
  11529. + }
  11530. +
  11531. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  11532. + __DWC_FREE(mem_ctx, addr);
  11533. + return NULL;
  11534. + }
  11535. +
  11536. + return addr;
  11537. +}
  11538. +
  11539. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  11540. + int line)
  11541. +{
  11542. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  11543. +
  11544. + if (!addr) {
  11545. + return NULL;
  11546. + }
  11547. +
  11548. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  11549. + __DWC_FREE(mem_ctx, addr);
  11550. + return NULL;
  11551. + }
  11552. +
  11553. + return addr;
  11554. +}
  11555. +
  11556. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  11557. +{
  11558. + free_allocation(mem_ctx, addr, func, line);
  11559. + __DWC_FREE(mem_ctx, addr);
  11560. +}
  11561. +
  11562. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  11563. + char const *func, int line)
  11564. +{
  11565. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  11566. +
  11567. + if (!addr) {
  11568. + return NULL;
  11569. + }
  11570. +
  11571. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  11572. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  11573. + return NULL;
  11574. + }
  11575. +
  11576. + return addr;
  11577. +}
  11578. +
  11579. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  11580. + dwc_dma_t *dma_addr, char const *func, int line)
  11581. +{
  11582. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  11583. +
  11584. + if (!addr) {
  11585. + return NULL;
  11586. + }
  11587. +
  11588. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  11589. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  11590. + return NULL;
  11591. + }
  11592. +
  11593. + return addr;
  11594. +}
  11595. +
  11596. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  11597. + dwc_dma_t dma_addr, char const *func, int line)
  11598. +{
  11599. + free_allocation(dma_ctx, virt_addr, func, line);
  11600. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  11601. +}
  11602. +
  11603. +#endif /* DWC_DEBUG_MEMORY */
  11604. --- /dev/null
  11605. +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.c
  11606. @@ -0,0 +1,636 @@
  11607. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  11608. + *
  11609. + * PuTTY is copyright 1997-2007 Simon Tatham.
  11610. + *
  11611. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  11612. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  11613. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  11614. + * Kuhn, and CORE SDI S.A.
  11615. + *
  11616. + * Permission is hereby granted, free of charge, to any person
  11617. + * obtaining a copy of this software and associated documentation files
  11618. + * (the "Software"), to deal in the Software without restriction,
  11619. + * including without limitation the rights to use, copy, modify, merge,
  11620. + * publish, distribute, sublicense, and/or sell copies of the Software,
  11621. + * and to permit persons to whom the Software is furnished to do so,
  11622. + * subject to the following conditions:
  11623. + *
  11624. + * The above copyright notice and this permission notice shall be
  11625. + * included in all copies or substantial portions of the Software.
  11626. +
  11627. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11628. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11629. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11630. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  11631. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11632. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  11633. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11634. + *
  11635. + */
  11636. +#ifdef DWC_CRYPTOLIB
  11637. +
  11638. +#ifndef CONFIG_MACH_IPMATE
  11639. +
  11640. +#include "dwc_modpow.h"
  11641. +
  11642. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  11643. +#define BIGNUM_TOP_BIT 0x80000000UL
  11644. +#define BIGNUM_INT_BITS 32
  11645. +
  11646. +
  11647. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  11648. +{
  11649. + void *p;
  11650. + size *= n;
  11651. + if (size == 0) size = 1;
  11652. + p = dwc_alloc(mem_ctx, size);
  11653. + return p;
  11654. +}
  11655. +
  11656. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  11657. +#define sfree dwc_free
  11658. +
  11659. +/*
  11660. + * Usage notes:
  11661. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  11662. + * subscripts, as some implementations object to this (see below).
  11663. + * * Note that none of the division methods below will cope if the
  11664. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  11665. + * to avoid this case.
  11666. + * If this condition occurs, in the case of the x86 DIV instruction,
  11667. + * an overflow exception will occur, which (according to a correspondent)
  11668. + * will manifest on Windows as something like
  11669. + * 0xC0000095: Integer overflow
  11670. + * The C variant won't give the right answer, either.
  11671. + */
  11672. +
  11673. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  11674. +
  11675. +#if defined __GNUC__ && defined __i386__
  11676. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  11677. + __asm__("div %2" : \
  11678. + "=d" (r), "=a" (q) : \
  11679. + "r" (w), "d" (hi), "a" (lo))
  11680. +#else
  11681. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  11682. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  11683. + q = n / w; \
  11684. + r = n % w; \
  11685. +} while (0)
  11686. +#endif
  11687. +
  11688. +// q = n / w;
  11689. +// r = n % w;
  11690. +
  11691. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  11692. +
  11693. +#define BIGNUM_INTERNAL
  11694. +
  11695. +static Bignum newbn(void *mem_ctx, int length)
  11696. +{
  11697. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  11698. + //if (!b)
  11699. + //abort(); /* FIXME */
  11700. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  11701. + b[0] = length;
  11702. + return b;
  11703. +}
  11704. +
  11705. +void freebn(void *mem_ctx, Bignum b)
  11706. +{
  11707. + /*
  11708. + * Burn the evidence, just in case.
  11709. + */
  11710. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  11711. + sfree(mem_ctx, b);
  11712. +}
  11713. +
  11714. +/*
  11715. + * Compute c = a * b.
  11716. + * Input is in the first len words of a and b.
  11717. + * Result is returned in the first 2*len words of c.
  11718. + */
  11719. +static void internal_mul(BignumInt *a, BignumInt *b,
  11720. + BignumInt *c, int len)
  11721. +{
  11722. + int i, j;
  11723. + BignumDblInt t;
  11724. +
  11725. + for (j = 0; j < 2 * len; j++)
  11726. + c[j] = 0;
  11727. +
  11728. + for (i = len - 1; i >= 0; i--) {
  11729. + t = 0;
  11730. + for (j = len - 1; j >= 0; j--) {
  11731. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  11732. + t += (BignumDblInt) c[i + j + 1];
  11733. + c[i + j + 1] = (BignumInt) t;
  11734. + t = t >> BIGNUM_INT_BITS;
  11735. + }
  11736. + c[i] = (BignumInt) t;
  11737. + }
  11738. +}
  11739. +
  11740. +static void internal_add_shifted(BignumInt *number,
  11741. + unsigned n, int shift)
  11742. +{
  11743. + int word = 1 + (shift / BIGNUM_INT_BITS);
  11744. + int bshift = shift % BIGNUM_INT_BITS;
  11745. + BignumDblInt addend;
  11746. +
  11747. + addend = (BignumDblInt)n << bshift;
  11748. +
  11749. + while (addend) {
  11750. + addend += number[word];
  11751. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  11752. + addend >>= BIGNUM_INT_BITS;
  11753. + word++;
  11754. + }
  11755. +}
  11756. +
  11757. +/*
  11758. + * Compute a = a % m.
  11759. + * Input in first alen words of a and first mlen words of m.
  11760. + * Output in first alen words of a
  11761. + * (of which first alen-mlen words will be zero).
  11762. + * The MSW of m MUST have its high bit set.
  11763. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  11764. + * rather than the internal bigendian format. Quotient parts are shifted
  11765. + * left by `qshift' before adding into quot.
  11766. + */
  11767. +static void internal_mod(BignumInt *a, int alen,
  11768. + BignumInt *m, int mlen,
  11769. + BignumInt *quot, int qshift)
  11770. +{
  11771. + BignumInt m0, m1;
  11772. + unsigned int h;
  11773. + int i, k;
  11774. +
  11775. + m0 = m[0];
  11776. + if (mlen > 1)
  11777. + m1 = m[1];
  11778. + else
  11779. + m1 = 0;
  11780. +
  11781. + for (i = 0; i <= alen - mlen; i++) {
  11782. + BignumDblInt t;
  11783. + unsigned int q, r, c, ai1;
  11784. +
  11785. + if (i == 0) {
  11786. + h = 0;
  11787. + } else {
  11788. + h = a[i - 1];
  11789. + a[i - 1] = 0;
  11790. + }
  11791. +
  11792. + if (i == alen - 1)
  11793. + ai1 = 0;
  11794. + else
  11795. + ai1 = a[i + 1];
  11796. +
  11797. + /* Find q = h:a[i] / m0 */
  11798. + if (h >= m0) {
  11799. + /*
  11800. + * Special case.
  11801. + *
  11802. + * To illustrate it, suppose a BignumInt is 8 bits, and
  11803. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  11804. + * our initial division will be 0xA123 / 0xA1, which
  11805. + * will give a quotient of 0x100 and a divide overflow.
  11806. + * However, the invariants in this division algorithm
  11807. + * are not violated, since the full number A1:23:... is
  11808. + * _less_ than the quotient prefix A1:B2:... and so the
  11809. + * following correction loop would have sorted it out.
  11810. + *
  11811. + * In this situation we set q to be the largest
  11812. + * quotient we _can_ stomach (0xFF, of course).
  11813. + */
  11814. + q = BIGNUM_INT_MASK;
  11815. + } else {
  11816. + /* Macro doesn't want an array subscript expression passed
  11817. + * into it (see definition), so use a temporary. */
  11818. + BignumInt tmplo = a[i];
  11819. + DIVMOD_WORD(q, r, h, tmplo, m0);
  11820. +
  11821. + /* Refine our estimate of q by looking at
  11822. + h:a[i]:a[i+1] / m0:m1 */
  11823. + t = MUL_WORD(m1, q);
  11824. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  11825. + q--;
  11826. + t -= m1;
  11827. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  11828. + if (r >= (BignumDblInt) m0 &&
  11829. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  11830. + }
  11831. + }
  11832. +
  11833. + /* Subtract q * m from a[i...] */
  11834. + c = 0;
  11835. + for (k = mlen - 1; k >= 0; k--) {
  11836. + t = MUL_WORD(q, m[k]);
  11837. + t += c;
  11838. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  11839. + if ((BignumInt) t > a[i + k])
  11840. + c++;
  11841. + a[i + k] -= (BignumInt) t;
  11842. + }
  11843. +
  11844. + /* Add back m in case of borrow */
  11845. + if (c != h) {
  11846. + t = 0;
  11847. + for (k = mlen - 1; k >= 0; k--) {
  11848. + t += m[k];
  11849. + t += a[i + k];
  11850. + a[i + k] = (BignumInt) t;
  11851. + t = t >> BIGNUM_INT_BITS;
  11852. + }
  11853. + q--;
  11854. + }
  11855. + if (quot)
  11856. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  11857. + }
  11858. +}
  11859. +
  11860. +/*
  11861. + * Compute p % mod.
  11862. + * The most significant word of mod MUST be non-zero.
  11863. + * We assume that the result array is the same size as the mod array.
  11864. + * We optionally write out a quotient if `quotient' is non-NULL.
  11865. + * We can avoid writing out the result if `result' is NULL.
  11866. + */
  11867. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  11868. +{
  11869. + BignumInt *n, *m;
  11870. + int mshift;
  11871. + int plen, mlen, i, j;
  11872. +
  11873. + /* Allocate m of size mlen, copy mod to m */
  11874. + /* We use big endian internally */
  11875. + mlen = mod[0];
  11876. + m = snewn(mem_ctx, mlen, BignumInt);
  11877. + //if (!m)
  11878. + //abort(); /* FIXME */
  11879. + for (j = 0; j < mlen; j++)
  11880. + m[j] = mod[mod[0] - j];
  11881. +
  11882. + /* Shift m left to make msb bit set */
  11883. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  11884. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  11885. + break;
  11886. + if (mshift) {
  11887. + for (i = 0; i < mlen - 1; i++)
  11888. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  11889. + m[mlen - 1] = m[mlen - 1] << mshift;
  11890. + }
  11891. +
  11892. + plen = p[0];
  11893. + /* Ensure plen > mlen */
  11894. + if (plen <= mlen)
  11895. + plen = mlen + 1;
  11896. +
  11897. + /* Allocate n of size plen, copy p to n */
  11898. + n = snewn(mem_ctx, plen, BignumInt);
  11899. + //if (!n)
  11900. + //abort(); /* FIXME */
  11901. + for (j = 0; j < plen; j++)
  11902. + n[j] = 0;
  11903. + for (j = 1; j <= (int)p[0]; j++)
  11904. + n[plen - j] = p[j];
  11905. +
  11906. + /* Main computation */
  11907. + internal_mod(n, plen, m, mlen, quotient, mshift);
  11908. +
  11909. + /* Fixup result in case the modulus was shifted */
  11910. + if (mshift) {
  11911. + for (i = plen - mlen - 1; i < plen - 1; i++)
  11912. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  11913. + n[plen - 1] = n[plen - 1] << mshift;
  11914. + internal_mod(n, plen, m, mlen, quotient, 0);
  11915. + for (i = plen - 1; i >= plen - mlen; i--)
  11916. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  11917. + }
  11918. +
  11919. + /* Copy result to buffer */
  11920. + if (result) {
  11921. + for (i = 1; i <= (int)result[0]; i++) {
  11922. + int j = plen - i;
  11923. + result[i] = j >= 0 ? n[j] : 0;
  11924. + }
  11925. + }
  11926. +
  11927. + /* Free temporary arrays */
  11928. + for (i = 0; i < mlen; i++)
  11929. + m[i] = 0;
  11930. + sfree(mem_ctx, m);
  11931. + for (i = 0; i < plen; i++)
  11932. + n[i] = 0;
  11933. + sfree(mem_ctx, n);
  11934. +}
  11935. +
  11936. +/*
  11937. + * Simple remainder.
  11938. + */
  11939. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  11940. +{
  11941. + Bignum r = newbn(mem_ctx, b[0]);
  11942. + bigdivmod(mem_ctx, a, b, r, NULL);
  11943. + return r;
  11944. +}
  11945. +
  11946. +/*
  11947. + * Compute (base ^ exp) % mod.
  11948. + */
  11949. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  11950. +{
  11951. + BignumInt *a, *b, *n, *m;
  11952. + int mshift;
  11953. + int mlen, i, j;
  11954. + Bignum base, result;
  11955. +
  11956. + /*
  11957. + * The most significant word of mod needs to be non-zero. It
  11958. + * should already be, but let's make sure.
  11959. + */
  11960. + //assert(mod[mod[0]] != 0);
  11961. +
  11962. + /*
  11963. + * Make sure the base is smaller than the modulus, by reducing
  11964. + * it modulo the modulus if not.
  11965. + */
  11966. + base = bigmod(mem_ctx, base_in, mod);
  11967. +
  11968. + /* Allocate m of size mlen, copy mod to m */
  11969. + /* We use big endian internally */
  11970. + mlen = mod[0];
  11971. + m = snewn(mem_ctx, mlen, BignumInt);
  11972. + //if (!m)
  11973. + //abort(); /* FIXME */
  11974. + for (j = 0; j < mlen; j++)
  11975. + m[j] = mod[mod[0] - j];
  11976. +
  11977. + /* Shift m left to make msb bit set */
  11978. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  11979. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  11980. + break;
  11981. + if (mshift) {
  11982. + for (i = 0; i < mlen - 1; i++)
  11983. + m[i] =
  11984. + (m[i] << mshift) | (m[i + 1] >>
  11985. + (BIGNUM_INT_BITS - mshift));
  11986. + m[mlen - 1] = m[mlen - 1] << mshift;
  11987. + }
  11988. +
  11989. + /* Allocate n of size mlen, copy base to n */
  11990. + n = snewn(mem_ctx, mlen, BignumInt);
  11991. + //if (!n)
  11992. + //abort(); /* FIXME */
  11993. + i = mlen - base[0];
  11994. + for (j = 0; j < i; j++)
  11995. + n[j] = 0;
  11996. + for (j = 0; j < base[0]; j++)
  11997. + n[i + j] = base[base[0] - j];
  11998. +
  11999. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  12000. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  12001. + //if (!a)
  12002. + //abort(); /* FIXME */
  12003. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  12004. + //if (!b)
  12005. + //abort(); /* FIXME */
  12006. + for (i = 0; i < 2 * mlen; i++)
  12007. + a[i] = 0;
  12008. + a[2 * mlen - 1] = 1;
  12009. +
  12010. + /* Skip leading zero bits of exp. */
  12011. + i = 0;
  12012. + j = BIGNUM_INT_BITS - 1;
  12013. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  12014. + j--;
  12015. + if (j < 0) {
  12016. + i++;
  12017. + j = BIGNUM_INT_BITS - 1;
  12018. + }
  12019. + }
  12020. +
  12021. + /* Main computation */
  12022. + while (i < exp[0]) {
  12023. + while (j >= 0) {
  12024. + internal_mul(a + mlen, a + mlen, b, mlen);
  12025. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  12026. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  12027. + internal_mul(b + mlen, n, a, mlen);
  12028. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  12029. + } else {
  12030. + BignumInt *t;
  12031. + t = a;
  12032. + a = b;
  12033. + b = t;
  12034. + }
  12035. + j--;
  12036. + }
  12037. + i++;
  12038. + j = BIGNUM_INT_BITS - 1;
  12039. + }
  12040. +
  12041. + /* Fixup result in case the modulus was shifted */
  12042. + if (mshift) {
  12043. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  12044. + a[i] =
  12045. + (a[i] << mshift) | (a[i + 1] >>
  12046. + (BIGNUM_INT_BITS - mshift));
  12047. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  12048. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  12049. + for (i = 2 * mlen - 1; i >= mlen; i--)
  12050. + a[i] =
  12051. + (a[i] >> mshift) | (a[i - 1] <<
  12052. + (BIGNUM_INT_BITS - mshift));
  12053. + }
  12054. +
  12055. + /* Copy result to buffer */
  12056. + result = newbn(mem_ctx, mod[0]);
  12057. + for (i = 0; i < mlen; i++)
  12058. + result[result[0] - i] = a[i + mlen];
  12059. + while (result[0] > 1 && result[result[0]] == 0)
  12060. + result[0]--;
  12061. +
  12062. + /* Free temporary arrays */
  12063. + for (i = 0; i < 2 * mlen; i++)
  12064. + a[i] = 0;
  12065. + sfree(mem_ctx, a);
  12066. + for (i = 0; i < 2 * mlen; i++)
  12067. + b[i] = 0;
  12068. + sfree(mem_ctx, b);
  12069. + for (i = 0; i < mlen; i++)
  12070. + m[i] = 0;
  12071. + sfree(mem_ctx, m);
  12072. + for (i = 0; i < mlen; i++)
  12073. + n[i] = 0;
  12074. + sfree(mem_ctx, n);
  12075. +
  12076. + freebn(mem_ctx, base);
  12077. +
  12078. + return result;
  12079. +}
  12080. +
  12081. +
  12082. +#ifdef UNITTEST
  12083. +
  12084. +static __u32 dh_p[] = {
  12085. + 96,
  12086. + 0xFFFFFFFF,
  12087. + 0xFFFFFFFF,
  12088. + 0xA93AD2CA,
  12089. + 0x4B82D120,
  12090. + 0xE0FD108E,
  12091. + 0x43DB5BFC,
  12092. + 0x74E5AB31,
  12093. + 0x08E24FA0,
  12094. + 0xBAD946E2,
  12095. + 0x770988C0,
  12096. + 0x7A615D6C,
  12097. + 0xBBE11757,
  12098. + 0x177B200C,
  12099. + 0x521F2B18,
  12100. + 0x3EC86A64,
  12101. + 0xD8760273,
  12102. + 0xD98A0864,
  12103. + 0xF12FFA06,
  12104. + 0x1AD2EE6B,
  12105. + 0xCEE3D226,
  12106. + 0x4A25619D,
  12107. + 0x1E8C94E0,
  12108. + 0xDB0933D7,
  12109. + 0xABF5AE8C,
  12110. + 0xA6E1E4C7,
  12111. + 0xB3970F85,
  12112. + 0x5D060C7D,
  12113. + 0x8AEA7157,
  12114. + 0x58DBEF0A,
  12115. + 0xECFB8504,
  12116. + 0xDF1CBA64,
  12117. + 0xA85521AB,
  12118. + 0x04507A33,
  12119. + 0xAD33170D,
  12120. + 0x8AAAC42D,
  12121. + 0x15728E5A,
  12122. + 0x98FA0510,
  12123. + 0x15D22618,
  12124. + 0xEA956AE5,
  12125. + 0x3995497C,
  12126. + 0x95581718,
  12127. + 0xDE2BCBF6,
  12128. + 0x6F4C52C9,
  12129. + 0xB5C55DF0,
  12130. + 0xEC07A28F,
  12131. + 0x9B2783A2,
  12132. + 0x180E8603,
  12133. + 0xE39E772C,
  12134. + 0x2E36CE3B,
  12135. + 0x32905E46,
  12136. + 0xCA18217C,
  12137. + 0xF1746C08,
  12138. + 0x4ABC9804,
  12139. + 0x670C354E,
  12140. + 0x7096966D,
  12141. + 0x9ED52907,
  12142. + 0x208552BB,
  12143. + 0x1C62F356,
  12144. + 0xDCA3AD96,
  12145. + 0x83655D23,
  12146. + 0xFD24CF5F,
  12147. + 0x69163FA8,
  12148. + 0x1C55D39A,
  12149. + 0x98DA4836,
  12150. + 0xA163BF05,
  12151. + 0xC2007CB8,
  12152. + 0xECE45B3D,
  12153. + 0x49286651,
  12154. + 0x7C4B1FE6,
  12155. + 0xAE9F2411,
  12156. + 0x5A899FA5,
  12157. + 0xEE386BFB,
  12158. + 0xF406B7ED,
  12159. + 0x0BFF5CB6,
  12160. + 0xA637ED6B,
  12161. + 0xF44C42E9,
  12162. + 0x625E7EC6,
  12163. + 0xE485B576,
  12164. + 0x6D51C245,
  12165. + 0x4FE1356D,
  12166. + 0xF25F1437,
  12167. + 0x302B0A6D,
  12168. + 0xCD3A431B,
  12169. + 0xEF9519B3,
  12170. + 0x8E3404DD,
  12171. + 0x514A0879,
  12172. + 0x3B139B22,
  12173. + 0x020BBEA6,
  12174. + 0x8A67CC74,
  12175. + 0x29024E08,
  12176. + 0x80DC1CD1,
  12177. + 0xC4C6628B,
  12178. + 0x2168C234,
  12179. + 0xC90FDAA2,
  12180. + 0xFFFFFFFF,
  12181. + 0xFFFFFFFF,
  12182. +};
  12183. +
  12184. +static __u32 dh_a[] = {
  12185. + 8,
  12186. + 0xdf367516,
  12187. + 0x86459caa,
  12188. + 0xe2d459a4,
  12189. + 0xd910dae0,
  12190. + 0x8a8b5e37,
  12191. + 0x67ab31c6,
  12192. + 0xf0b55ea9,
  12193. + 0x440051d6,
  12194. +};
  12195. +
  12196. +static __u32 dh_b[] = {
  12197. + 8,
  12198. + 0xded92656,
  12199. + 0xe07a048a,
  12200. + 0x6fa452cd,
  12201. + 0x2df89d30,
  12202. + 0xc75f1b0f,
  12203. + 0x8ce3578f,
  12204. + 0x7980a324,
  12205. + 0x5daec786,
  12206. +};
  12207. +
  12208. +static __u32 dh_g[] = {
  12209. + 1,
  12210. + 2,
  12211. +};
  12212. +
  12213. +int main(void)
  12214. +{
  12215. + int i;
  12216. + __u32 *k;
  12217. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  12218. +
  12219. + printf("\n\n");
  12220. + for (i=0; i<k[0]; i++) {
  12221. + __u32 word32 = k[k[0] - i];
  12222. + __u16 l = word32 & 0xffff;
  12223. + __u16 m = (word32 & 0xffff0000) >> 16;
  12224. + printf("%04x %04x ", m, l);
  12225. + if (!((i + 1)%13)) printf("\n");
  12226. + }
  12227. + printf("\n\n");
  12228. +
  12229. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  12230. + printf("PASS\n\n");
  12231. + }
  12232. + else {
  12233. + printf("FAIL\n\n");
  12234. + }
  12235. +
  12236. +}
  12237. +
  12238. +#endif /* UNITTEST */
  12239. +
  12240. +#endif /* CONFIG_MACH_IPMATE */
  12241. +
  12242. +#endif /*DWC_CRYPTOLIB */
  12243. --- /dev/null
  12244. +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.h
  12245. @@ -0,0 +1,34 @@
  12246. +/*
  12247. + * dwc_modpow.h
  12248. + * See dwc_modpow.c for license and changes
  12249. + */
  12250. +#ifndef _DWC_MODPOW_H
  12251. +#define _DWC_MODPOW_H
  12252. +
  12253. +#ifdef __cplusplus
  12254. +extern "C" {
  12255. +#endif
  12256. +
  12257. +#include "dwc_os.h"
  12258. +
  12259. +/** @file
  12260. + *
  12261. + * This file defines the module exponentiation function which is only used
  12262. + * internally by the DWC UWB modules for calculation of PKs during numeric
  12263. + * association. The routine is taken from the PUTTY, an open source terminal
  12264. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  12265. + *
  12266. + */
  12267. +
  12268. +typedef uint32_t BignumInt;
  12269. +typedef uint64_t BignumDblInt;
  12270. +typedef BignumInt *Bignum;
  12271. +
  12272. +/* Compute modular exponentiaion */
  12273. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  12274. +
  12275. +#ifdef __cplusplus
  12276. +}
  12277. +#endif
  12278. +
  12279. +#endif /* _LINUX_BIGNUM_H */
  12280. --- /dev/null
  12281. +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.c
  12282. @@ -0,0 +1,319 @@
  12283. +#ifdef DWC_NOTIFYLIB
  12284. +
  12285. +#include "dwc_notifier.h"
  12286. +#include "dwc_list.h"
  12287. +
  12288. +typedef struct dwc_observer {
  12289. + void *observer;
  12290. + dwc_notifier_callback_t callback;
  12291. + void *data;
  12292. + char *notification;
  12293. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  12294. +} observer_t;
  12295. +
  12296. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  12297. +
  12298. +typedef struct dwc_notifier {
  12299. + void *mem_ctx;
  12300. + void *object;
  12301. + struct observer_queue observers;
  12302. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  12303. +} notifier_t;
  12304. +
  12305. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  12306. +
  12307. +typedef struct manager {
  12308. + void *mem_ctx;
  12309. + void *wkq_ctx;
  12310. + dwc_workq_t *wq;
  12311. +// dwc_mutex_t *mutex;
  12312. + struct notifier_queue notifiers;
  12313. +} manager_t;
  12314. +
  12315. +static manager_t *manager = NULL;
  12316. +
  12317. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  12318. +{
  12319. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  12320. + if (!manager) {
  12321. + return -DWC_E_NO_MEMORY;
  12322. + }
  12323. +
  12324. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  12325. +
  12326. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  12327. + if (!manager->wq) {
  12328. + return -DWC_E_NO_MEMORY;
  12329. + }
  12330. +
  12331. + return 0;
  12332. +}
  12333. +
  12334. +static void free_manager(void)
  12335. +{
  12336. + dwc_workq_free(manager->wq);
  12337. +
  12338. + /* All notifiers must have unregistered themselves before this module
  12339. + * can be removed. Hitting this assertion indicates a programmer
  12340. + * error. */
  12341. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  12342. + "Notification manager being freed before all notifiers have been removed");
  12343. + dwc_free(manager->mem_ctx, manager);
  12344. +}
  12345. +
  12346. +#ifdef DEBUG
  12347. +static void dump_manager(void)
  12348. +{
  12349. + notifier_t *n;
  12350. + observer_t *o;
  12351. +
  12352. + DWC_ASSERT(manager, "Notification manager not found");
  12353. +
  12354. + DWC_DEBUG("List of all notifiers and observers:\n");
  12355. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  12356. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  12357. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  12358. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  12359. + }
  12360. + }
  12361. +}
  12362. +#else
  12363. +#define dump_manager(...)
  12364. +#endif
  12365. +
  12366. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  12367. + dwc_notifier_callback_t callback, void *data)
  12368. +{
  12369. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  12370. +
  12371. + if (!new_observer) {
  12372. + return NULL;
  12373. + }
  12374. +
  12375. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  12376. + new_observer->observer = observer;
  12377. + new_observer->notification = notification;
  12378. + new_observer->callback = callback;
  12379. + new_observer->data = data;
  12380. + return new_observer;
  12381. +}
  12382. +
  12383. +static void free_observer(void *mem_ctx, observer_t *observer)
  12384. +{
  12385. + dwc_free(mem_ctx, observer);
  12386. +}
  12387. +
  12388. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  12389. +{
  12390. + notifier_t *notifier;
  12391. +
  12392. + if (!object) {
  12393. + return NULL;
  12394. + }
  12395. +
  12396. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  12397. + if (!notifier) {
  12398. + return NULL;
  12399. + }
  12400. +
  12401. + DWC_CIRCLEQ_INIT(&notifier->observers);
  12402. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  12403. +
  12404. + notifier->mem_ctx = mem_ctx;
  12405. + notifier->object = object;
  12406. + return notifier;
  12407. +}
  12408. +
  12409. +static void free_notifier(notifier_t *notifier)
  12410. +{
  12411. + observer_t *observer;
  12412. +
  12413. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  12414. + free_observer(notifier->mem_ctx, observer);
  12415. + }
  12416. +
  12417. + dwc_free(notifier->mem_ctx, notifier);
  12418. +}
  12419. +
  12420. +static notifier_t *find_notifier(void *object)
  12421. +{
  12422. + notifier_t *notifier;
  12423. +
  12424. + DWC_ASSERT(manager, "Notification manager not found");
  12425. +
  12426. + if (!object) {
  12427. + return NULL;
  12428. + }
  12429. +
  12430. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  12431. + if (notifier->object == object) {
  12432. + return notifier;
  12433. + }
  12434. + }
  12435. +
  12436. + return NULL;
  12437. +}
  12438. +
  12439. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  12440. +{
  12441. + return create_manager(mem_ctx, wkq_ctx);
  12442. +}
  12443. +
  12444. +void dwc_free_notification_manager(void)
  12445. +{
  12446. + free_manager();
  12447. +}
  12448. +
  12449. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  12450. +{
  12451. + notifier_t *notifier;
  12452. +
  12453. + DWC_ASSERT(manager, "Notification manager not found");
  12454. +
  12455. + notifier = find_notifier(object);
  12456. + if (notifier) {
  12457. + DWC_ERROR("Notifier %p is already registered\n", object);
  12458. + return NULL;
  12459. + }
  12460. +
  12461. + notifier = alloc_notifier(mem_ctx, object);
  12462. + if (!notifier) {
  12463. + return NULL;
  12464. + }
  12465. +
  12466. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  12467. +
  12468. + DWC_INFO("Notifier %p registered", object);
  12469. + dump_manager();
  12470. +
  12471. + return notifier;
  12472. +}
  12473. +
  12474. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  12475. +{
  12476. + DWC_ASSERT(manager, "Notification manager not found");
  12477. +
  12478. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  12479. + observer_t *o;
  12480. +
  12481. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  12482. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  12483. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  12484. + }
  12485. +
  12486. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  12487. + "Notifier %p has active observers when removing", notifier);
  12488. + }
  12489. +
  12490. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  12491. + free_notifier(notifier);
  12492. +
  12493. + DWC_INFO("Notifier unregistered");
  12494. + dump_manager();
  12495. +}
  12496. +
  12497. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  12498. +int dwc_add_observer(void *observer, void *object, char *notification,
  12499. + dwc_notifier_callback_t callback, void *data)
  12500. +{
  12501. + notifier_t *notifier = find_notifier(object);
  12502. + observer_t *new_observer;
  12503. +
  12504. + if (!notifier) {
  12505. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  12506. + return -DWC_E_INVALID;
  12507. + }
  12508. +
  12509. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  12510. + if (!new_observer) {
  12511. + return -DWC_E_NO_MEMORY;
  12512. + }
  12513. +
  12514. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  12515. +
  12516. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  12517. + observer, object, notification, callback, data);
  12518. +
  12519. + dump_manager();
  12520. + return 0;
  12521. +}
  12522. +
  12523. +int dwc_remove_observer(void *observer)
  12524. +{
  12525. + notifier_t *n;
  12526. +
  12527. + DWC_ASSERT(manager, "Notification manager not found");
  12528. +
  12529. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  12530. + observer_t *o;
  12531. + observer_t *o2;
  12532. +
  12533. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  12534. + if (o->observer == observer) {
  12535. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  12536. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  12537. + o->observer, n->object, o->notification);
  12538. + free_observer(n->mem_ctx, o);
  12539. + }
  12540. + }
  12541. + }
  12542. +
  12543. + dump_manager();
  12544. + return 0;
  12545. +}
  12546. +
  12547. +typedef struct callback_data {
  12548. + void *mem_ctx;
  12549. + dwc_notifier_callback_t cb;
  12550. + void *observer;
  12551. + void *data;
  12552. + void *object;
  12553. + char *notification;
  12554. + void *notification_data;
  12555. +} cb_data_t;
  12556. +
  12557. +static void cb_task(void *data)
  12558. +{
  12559. + cb_data_t *cb = (cb_data_t *)data;
  12560. +
  12561. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  12562. + dwc_free(cb->mem_ctx, cb);
  12563. +}
  12564. +
  12565. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  12566. +{
  12567. + observer_t *o;
  12568. +
  12569. + DWC_ASSERT(manager, "Notification manager not found");
  12570. +
  12571. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  12572. + int len = DWC_STRLEN(notification);
  12573. +
  12574. + if (DWC_STRLEN(o->notification) != len) {
  12575. + continue;
  12576. + }
  12577. +
  12578. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  12579. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  12580. +
  12581. + if (!cb_data) {
  12582. + DWC_ERROR("Failed to allocate callback data\n");
  12583. + return;
  12584. + }
  12585. +
  12586. + cb_data->mem_ctx = notifier->mem_ctx;
  12587. + cb_data->cb = o->callback;
  12588. + cb_data->observer = o->observer;
  12589. + cb_data->data = o->data;
  12590. + cb_data->object = notifier->object;
  12591. + cb_data->notification = notification;
  12592. + cb_data->notification_data = notification_data;
  12593. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  12594. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  12595. + "Notify callback from %p for Notification %s, to observer %p",
  12596. + cb_data->object, notification, cb_data->observer);
  12597. + }
  12598. + }
  12599. +}
  12600. +
  12601. +#endif /* DWC_NOTIFYLIB */
  12602. --- /dev/null
  12603. +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.h
  12604. @@ -0,0 +1,122 @@
  12605. +
  12606. +#ifndef __DWC_NOTIFIER_H__
  12607. +#define __DWC_NOTIFIER_H__
  12608. +
  12609. +#ifdef __cplusplus
  12610. +extern "C" {
  12611. +#endif
  12612. +
  12613. +#include "dwc_os.h"
  12614. +
  12615. +/** @file
  12616. + *
  12617. + * A simple implementation of the Observer pattern. Any "module" can
  12618. + * register as an observer or notifier. The notion of "module" is abstract and
  12619. + * can mean anything used to identify either an observer or notifier. Usually
  12620. + * it will be a pointer to a data structure which contains some state, ie an
  12621. + * object.
  12622. + *
  12623. + * Before any notifiers can be added, the global notification manager must be
  12624. + * brought up with dwc_alloc_notification_manager().
  12625. + * dwc_free_notification_manager() will bring it down and free all resources.
  12626. + * These would typically be called upon module load and unload. The
  12627. + * notification manager is a single global instance that handles all registered
  12628. + * observable modules and observers so this should be done only once.
  12629. + *
  12630. + * A module can be observable by using Notifications to publicize some general
  12631. + * information about it's state or operation. It does not care who listens, or
  12632. + * even if anyone listens, or what they do with the information. The observable
  12633. + * modules do not need to know any information about it's observers or their
  12634. + * interface, or their state or data.
  12635. + *
  12636. + * Any module can register to emit Notifications. It should publish a list of
  12637. + * notifications that it can emit and their behavior, such as when they will get
  12638. + * triggered, and what information will be provided to the observer. Then it
  12639. + * should register itself as an observable module. See dwc_register_notifier().
  12640. + *
  12641. + * Any module can observe any observable, registered module, provided it has a
  12642. + * handle to the other module and knows what notifications to observe. See
  12643. + * dwc_add_observer().
  12644. + *
  12645. + * A function of type dwc_notifier_callback_t is called whenever a notification
  12646. + * is triggered with one or more observers observing it. This function is
  12647. + * called in it's own process so it may sleep or block if needed. It is
  12648. + * guaranteed to be called sometime after the notification has occurred and will
  12649. + * be called once per each time the notification is triggered. It will NOT be
  12650. + * called in the same process context used to trigger the notification.
  12651. + *
  12652. + * @section Limitiations
  12653. + *
  12654. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  12655. + * schedule too many processes too handle. Be aware of this limitation when
  12656. + * designing to use notifications, and only add notifications for appropriate
  12657. + * observable information.
  12658. + *
  12659. + * Also Notification callbacks are not synchronous. If you need to synchronize
  12660. + * the behavior between module/observer you must use other means. And perhaps
  12661. + * that will mean Notifications are not the proper solution.
  12662. + */
  12663. +
  12664. +struct dwc_notifier;
  12665. +typedef struct dwc_notifier dwc_notifier_t;
  12666. +
  12667. +/** The callback function must be of this type.
  12668. + *
  12669. + * @param object This is the object that is being observed.
  12670. + * @param notification This is the notification that was triggered.
  12671. + * @param observer This is the observer
  12672. + * @param notification_data This is notification-specific data that the notifier
  12673. + * has included in this notification. The value of this should be published in
  12674. + * the documentation of the observable module with the notifications.
  12675. + * @param user_data This is any custom data that the observer provided when
  12676. + * adding itself as an observer to the notification. */
  12677. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  12678. + void *notification_data, void *user_data);
  12679. +
  12680. +/** Brings up the notification manager. */
  12681. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  12682. +/** Brings down the notification manager. */
  12683. +extern void dwc_free_notification_manager(void);
  12684. +
  12685. +/** This function registers an observable module. A dwc_notifier_t object is
  12686. + * returned to the observable module. This is an opaque object that is used by
  12687. + * the observable module to trigger notifications. This object should only be
  12688. + * accessible to functions that are authorized to trigger notifications for this
  12689. + * module. Observers do not need this object. */
  12690. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  12691. +
  12692. +/** This function unregisters an observable module. All observers have to be
  12693. + * removed prior to unregistration. */
  12694. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  12695. +
  12696. +/** Add a module as an observer to the observable module. The observable module
  12697. + * needs to have previously registered with the notification manager.
  12698. + *
  12699. + * @param observer The observer module
  12700. + * @param object The module to observe
  12701. + * @param notification The notification to observe
  12702. + * @param callback The callback function to call
  12703. + * @param user_data Any additional user data to pass into the callback function */
  12704. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  12705. + dwc_notifier_callback_t callback, void *user_data);
  12706. +
  12707. +/** Removes the specified observer from all notifications that it is currently
  12708. + * observing. */
  12709. +extern int dwc_remove_observer(void *observer);
  12710. +
  12711. +/** This function triggers a Notification. It should be called by the
  12712. + * observable module, or any module or library which the observable module
  12713. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  12714. + *
  12715. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  12716. + * their own process context for each trigger. Callbacks can be blocking.
  12717. + * dwc_notify can be called from interrupt context if needed.
  12718. + *
  12719. + */
  12720. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  12721. +
  12722. +#ifdef __cplusplus
  12723. +}
  12724. +#endif
  12725. +
  12726. +#endif /* __DWC_NOTIFIER_H__ */
  12727. --- /dev/null
  12728. +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
  12729. @@ -0,0 +1,1276 @@
  12730. +/* =========================================================================
  12731. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  12732. + * $Revision: #14 $
  12733. + * $Date: 2010/11/04 $
  12734. + * $Change: 1621695 $
  12735. + *
  12736. + * Synopsys Portability Library Software and documentation
  12737. + * (hereinafter, "Software") is an Unsupported proprietary work of
  12738. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  12739. + * between Synopsys and you.
  12740. + *
  12741. + * The Software IS NOT an item of Licensed Software or Licensed Product
  12742. + * under any End User Software License Agreement or Agreement for
  12743. + * Licensed Product with Synopsys or any supplement thereto. You are
  12744. + * permitted to use and redistribute this Software in source and binary
  12745. + * forms, with or without modification, provided that redistributions
  12746. + * of source code must retain this notice. You may not view, use,
  12747. + * disclose, copy or distribute this file or any information contained
  12748. + * herein except pursuant to this license grant from Synopsys. If you
  12749. + * do not agree with this notice, including the disclaimer below, then
  12750. + * you are not authorized to use the Software.
  12751. + *
  12752. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  12753. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  12754. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  12755. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  12756. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12757. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12758. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12759. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  12760. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12761. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  12762. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  12763. + * DAMAGE.
  12764. + * ========================================================================= */
  12765. +#ifndef _DWC_OS_H_
  12766. +#define _DWC_OS_H_
  12767. +
  12768. +#ifdef __cplusplus
  12769. +extern "C" {
  12770. +#endif
  12771. +
  12772. +/** @file
  12773. + *
  12774. + * DWC portability library, low level os-wrapper functions
  12775. + *
  12776. + */
  12777. +
  12778. +/* These basic types need to be defined by some OS header file or custom header
  12779. + * file for your specific target architecture.
  12780. + *
  12781. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  12782. + *
  12783. + * Any custom or alternate header file must be added and enabled here.
  12784. + */
  12785. +
  12786. +#ifdef DWC_LINUX
  12787. +# include <linux/types.h>
  12788. +# ifdef CONFIG_DEBUG_MUTEXES
  12789. +# include <linux/mutex.h>
  12790. +# endif
  12791. +# include <linux/spinlock.h>
  12792. +# include <linux/errno.h>
  12793. +# include <stdarg.h>
  12794. +#endif
  12795. +
  12796. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  12797. +# include <os_dep.h>
  12798. +#endif
  12799. +
  12800. +
  12801. +/** @name Primitive Types and Values */
  12802. +
  12803. +/** We define a boolean type for consistency. Can be either YES or NO */
  12804. +typedef uint8_t dwc_bool_t;
  12805. +#define YES 1
  12806. +#define NO 0
  12807. +
  12808. +#ifdef DWC_LINUX
  12809. +
  12810. +/** @name Error Codes */
  12811. +#define DWC_E_INVALID EINVAL
  12812. +#define DWC_E_NO_MEMORY ENOMEM
  12813. +#define DWC_E_NO_DEVICE ENODEV
  12814. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  12815. +#define DWC_E_TIMEOUT ETIMEDOUT
  12816. +#define DWC_E_BUSY EBUSY
  12817. +#define DWC_E_AGAIN EAGAIN
  12818. +#define DWC_E_RESTART ERESTART
  12819. +#define DWC_E_ABORT ECONNABORTED
  12820. +#define DWC_E_SHUTDOWN ESHUTDOWN
  12821. +#define DWC_E_NO_DATA ENODATA
  12822. +#define DWC_E_DISCONNECT ECONNRESET
  12823. +#define DWC_E_UNKNOWN EINVAL
  12824. +#define DWC_E_NO_STREAM_RES ENOSR
  12825. +#define DWC_E_COMMUNICATION ECOMM
  12826. +#define DWC_E_OVERFLOW EOVERFLOW
  12827. +#define DWC_E_PROTOCOL EPROTO
  12828. +#define DWC_E_IN_PROGRESS EINPROGRESS
  12829. +#define DWC_E_PIPE EPIPE
  12830. +#define DWC_E_IO EIO
  12831. +#define DWC_E_NO_SPACE ENOSPC
  12832. +
  12833. +#else
  12834. +
  12835. +/** @name Error Codes */
  12836. +#define DWC_E_INVALID 1001
  12837. +#define DWC_E_NO_MEMORY 1002
  12838. +#define DWC_E_NO_DEVICE 1003
  12839. +#define DWC_E_NOT_SUPPORTED 1004
  12840. +#define DWC_E_TIMEOUT 1005
  12841. +#define DWC_E_BUSY 1006
  12842. +#define DWC_E_AGAIN 1007
  12843. +#define DWC_E_RESTART 1008
  12844. +#define DWC_E_ABORT 1009
  12845. +#define DWC_E_SHUTDOWN 1010
  12846. +#define DWC_E_NO_DATA 1011
  12847. +#define DWC_E_DISCONNECT 2000
  12848. +#define DWC_E_UNKNOWN 3000
  12849. +#define DWC_E_NO_STREAM_RES 4001
  12850. +#define DWC_E_COMMUNICATION 4002
  12851. +#define DWC_E_OVERFLOW 4003
  12852. +#define DWC_E_PROTOCOL 4004
  12853. +#define DWC_E_IN_PROGRESS 4005
  12854. +#define DWC_E_PIPE 4006
  12855. +#define DWC_E_IO 4007
  12856. +#define DWC_E_NO_SPACE 4008
  12857. +
  12858. +#endif
  12859. +
  12860. +
  12861. +/** @name Tracing/Logging Functions
  12862. + *
  12863. + * These function provide the capability to add tracing, debugging, and error
  12864. + * messages, as well exceptions as assertions. The WUDEV uses these
  12865. + * extensively. These could be logged to the main console, the serial port, an
  12866. + * internal buffer, etc. These functions could also be no-op if they are too
  12867. + * expensive on your system. By default undefining the DEBUG macro already
  12868. + * no-ops some of these functions. */
  12869. +
  12870. +/** Returns non-zero if in interrupt context. */
  12871. +extern dwc_bool_t DWC_IN_IRQ(void);
  12872. +#define dwc_in_irq DWC_IN_IRQ
  12873. +
  12874. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  12875. +static inline char *dwc_irq(void) {
  12876. + return DWC_IN_IRQ() ? "IRQ" : "";
  12877. +}
  12878. +
  12879. +/** Returns non-zero if in bottom-half context. */
  12880. +extern dwc_bool_t DWC_IN_BH(void);
  12881. +#define dwc_in_bh DWC_IN_BH
  12882. +
  12883. +/** Returns "BH" if DWC_IN_BH is true. */
  12884. +static inline char *dwc_bh(void) {
  12885. + return DWC_IN_BH() ? "BH" : "";
  12886. +}
  12887. +
  12888. +/**
  12889. + * A vprintf() clone. Just call vprintf if you've got it.
  12890. + */
  12891. +extern void DWC_VPRINTF(char *format, va_list args);
  12892. +#define dwc_vprintf DWC_VPRINTF
  12893. +
  12894. +/**
  12895. + * A vsnprintf() clone. Just call vprintf if you've got it.
  12896. + */
  12897. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  12898. +#define dwc_vsnprintf DWC_VSNPRINTF
  12899. +
  12900. +/**
  12901. + * printf() clone. Just call printf if you've go it.
  12902. + */
  12903. +extern void DWC_PRINTF(char *format, ...)
  12904. +/* This provides compiler level static checking of the parameters if you're
  12905. + * using GCC. */
  12906. +#ifdef __GNUC__
  12907. + __attribute__ ((format(printf, 1, 2)));
  12908. +#else
  12909. + ;
  12910. +#endif
  12911. +#define dwc_printf DWC_PRINTF
  12912. +
  12913. +/**
  12914. + * sprintf() clone. Just call sprintf if you've got it.
  12915. + */
  12916. +extern int DWC_SPRINTF(char *string, char *format, ...)
  12917. +#ifdef __GNUC__
  12918. + __attribute__ ((format(printf, 2, 3)));
  12919. +#else
  12920. + ;
  12921. +#endif
  12922. +#define dwc_sprintf DWC_SPRINTF
  12923. +
  12924. +/**
  12925. + * snprintf() clone. Just call snprintf if you've got it.
  12926. + */
  12927. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  12928. +#ifdef __GNUC__
  12929. + __attribute__ ((format(printf, 3, 4)));
  12930. +#else
  12931. + ;
  12932. +#endif
  12933. +#define dwc_snprintf DWC_SNPRINTF
  12934. +
  12935. +/**
  12936. + * Prints a WARNING message. On systems that don't differentiate between
  12937. + * warnings and regular log messages, just print it. Indicates that something
  12938. + * may be wrong with the driver. Works like printf().
  12939. + *
  12940. + * Use the DWC_WARN macro to call this function.
  12941. + */
  12942. +extern void __DWC_WARN(char *format, ...)
  12943. +#ifdef __GNUC__
  12944. + __attribute__ ((format(printf, 1, 2)));
  12945. +#else
  12946. + ;
  12947. +#endif
  12948. +
  12949. +/**
  12950. + * Prints an error message. On systems that don't differentiate between errors
  12951. + * and regular log messages, just print it. Indicates that something went wrong
  12952. + * with the driver. Works like printf().
  12953. + *
  12954. + * Use the DWC_ERROR macro to call this function.
  12955. + */
  12956. +extern void __DWC_ERROR(char *format, ...)
  12957. +#ifdef __GNUC__
  12958. + __attribute__ ((format(printf, 1, 2)));
  12959. +#else
  12960. + ;
  12961. +#endif
  12962. +
  12963. +/**
  12964. + * Prints an exception error message and takes some user-defined action such as
  12965. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  12966. + * abnormally wrong with the driver such as programmer error, or other
  12967. + * exceptional condition. It should not be ignored so even on systems without
  12968. + * printing capability, some action should be taken to notify the developer of
  12969. + * it. Works like printf().
  12970. + */
  12971. +extern void DWC_EXCEPTION(char *format, ...)
  12972. +#ifdef __GNUC__
  12973. + __attribute__ ((format(printf, 1, 2)));
  12974. +#else
  12975. + ;
  12976. +#endif
  12977. +#define dwc_exception DWC_EXCEPTION
  12978. +
  12979. +#ifndef DWC_OTG_DEBUG_LEV
  12980. +#define DWC_OTG_DEBUG_LEV 0
  12981. +#endif
  12982. +
  12983. +#ifdef DEBUG
  12984. +/**
  12985. + * Prints out a debug message. Used for logging/trace messages.
  12986. + *
  12987. + * Use the DWC_DEBUG macro to call this function
  12988. + */
  12989. +extern void __DWC_DEBUG(char *format, ...)
  12990. +#ifdef __GNUC__
  12991. + __attribute__ ((format(printf, 1, 2)));
  12992. +#else
  12993. + ;
  12994. +#endif
  12995. +#else
  12996. +#define __DWC_DEBUG printk
  12997. +#endif
  12998. +
  12999. +/**
  13000. + * Prints out a Debug message.
  13001. + */
  13002. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  13003. + __func__, dwc_irq(), ## _args)
  13004. +#define dwc_debug DWC_DEBUG
  13005. +/**
  13006. + * Prints out a Debug message if enabled at compile time.
  13007. + */
  13008. +#if DWC_OTG_DEBUG_LEV > 0
  13009. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  13010. +#else
  13011. +#define DWC_DEBUGC(_format, _args...)
  13012. +#endif
  13013. +#define dwc_debugc DWC_DEBUGC
  13014. +/**
  13015. + * Prints out an informative message.
  13016. + */
  13017. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  13018. + dwc_irq(), ## _args)
  13019. +#define dwc_info DWC_INFO
  13020. +/**
  13021. + * Prints out an informative message if enabled at compile time.
  13022. + */
  13023. +#if DWC_OTG_DEBUG_LEV > 1
  13024. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  13025. +#else
  13026. +#define DWC_INFOC(_format, _args...)
  13027. +#endif
  13028. +#define dwc_infoc DWC_INFOC
  13029. +/**
  13030. + * Prints out a warning message.
  13031. + */
  13032. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  13033. + dwc_irq(), __func__, __LINE__, ## _args)
  13034. +#define dwc_warn DWC_WARN
  13035. +/**
  13036. + * Prints out an error message.
  13037. + */
  13038. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  13039. + dwc_irq(), __func__, __LINE__, ## _args)
  13040. +#define dwc_error DWC_ERROR
  13041. +
  13042. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  13043. + dwc_irq(), __func__, __LINE__, ## _args)
  13044. +#define dwc_proto_error DWC_PROTO_ERROR
  13045. +
  13046. +#ifdef DEBUG
  13047. +/** Prints out a exception error message if the _expr expression fails. Disabled
  13048. + * if DEBUG is not enabled. */
  13049. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  13050. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  13051. + __FILE__, __LINE__, ## _args); } \
  13052. + } while (0)
  13053. +#else
  13054. +#define DWC_ASSERT(_x...)
  13055. +#endif
  13056. +#define dwc_assert DWC_ASSERT
  13057. +
  13058. +
  13059. +/** @name Byte Ordering
  13060. + * The following functions are for conversions between processor's byte ordering
  13061. + * and specific ordering you want.
  13062. + */
  13063. +
  13064. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  13065. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  13066. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  13067. +
  13068. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  13069. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  13070. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  13071. +
  13072. +/** Converts 32 bit little endian data to CPU byte ordering. */
  13073. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  13074. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  13075. +
  13076. +/** Converts 32 bit big endian data to CPU byte ordering. */
  13077. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  13078. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  13079. +
  13080. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  13081. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  13082. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  13083. +
  13084. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  13085. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  13086. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  13087. +
  13088. +/** Converts 16 bit little endian data to CPU byte ordering. */
  13089. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  13090. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  13091. +
  13092. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  13093. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  13094. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  13095. +
  13096. +
  13097. +/** @name Register Read/Write
  13098. + *
  13099. + * The following six functions should be implemented to read/write registers of
  13100. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  13101. + * The reg value is a pointer to the register calculated from the void *base
  13102. + * variable passed into the driver when it is started. */
  13103. +
  13104. +#ifdef DWC_LINUX
  13105. +/* Linux doesn't need any extra parameters for register read/write, so we
  13106. + * just throw away the IO context parameter.
  13107. + */
  13108. +/** Reads the content of a 32-bit register. */
  13109. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  13110. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  13111. +
  13112. +/** Reads the content of a 64-bit register. */
  13113. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  13114. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  13115. +
  13116. +/** Writes to a 32-bit register. */
  13117. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  13118. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  13119. +
  13120. +/** Writes to a 64-bit register. */
  13121. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  13122. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  13123. +
  13124. +/**
  13125. + * Modify bit values in a register. Using the
  13126. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  13127. + */
  13128. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  13129. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  13130. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  13131. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  13132. +
  13133. +#endif /* DWC_LINUX */
  13134. +
  13135. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13136. +typedef struct dwc_ioctx {
  13137. + struct device *dev;
  13138. + bus_space_tag_t iot;
  13139. + bus_space_handle_t ioh;
  13140. +} dwc_ioctx_t;
  13141. +
  13142. +/** BSD needs two extra parameters for register read/write, so we pass
  13143. + * them in using the IO context parameter.
  13144. + */
  13145. +/** Reads the content of a 32-bit register. */
  13146. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  13147. +#define dwc_read_reg32 DWC_READ_REG32
  13148. +
  13149. +/** Reads the content of a 64-bit register. */
  13150. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  13151. +#define dwc_read_reg64 DWC_READ_REG64
  13152. +
  13153. +/** Writes to a 32-bit register. */
  13154. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  13155. +#define dwc_write_reg32 DWC_WRITE_REG32
  13156. +
  13157. +/** Writes to a 64-bit register. */
  13158. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  13159. +#define dwc_write_reg64 DWC_WRITE_REG64
  13160. +
  13161. +/**
  13162. + * Modify bit values in a register. Using the
  13163. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  13164. + */
  13165. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  13166. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  13167. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  13168. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  13169. +
  13170. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  13171. +
  13172. +/** @cond */
  13173. +
  13174. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  13175. + * register writes. */
  13176. +
  13177. +#ifdef DWC_LINUX
  13178. +
  13179. +# ifdef DWC_DEBUG_REGS
  13180. +
  13181. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13182. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  13183. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  13184. +} \
  13185. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  13186. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  13187. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  13188. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  13189. +}
  13190. +
  13191. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13192. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  13193. + return DWC_READ_REG32(&container->regs->_reg); \
  13194. +} \
  13195. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  13196. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  13197. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  13198. +}
  13199. +
  13200. +# else /* DWC_DEBUG_REGS */
  13201. +
  13202. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13203. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  13204. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  13205. +} \
  13206. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  13207. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  13208. +}
  13209. +
  13210. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13211. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  13212. + return DWC_READ_REG32(&container->regs->_reg); \
  13213. +} \
  13214. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  13215. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  13216. +}
  13217. +
  13218. +# endif /* DWC_DEBUG_REGS */
  13219. +
  13220. +#endif /* DWC_LINUX */
  13221. +
  13222. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13223. +
  13224. +# ifdef DWC_DEBUG_REGS
  13225. +
  13226. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13227. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  13228. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  13229. +} \
  13230. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  13231. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  13232. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  13233. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  13234. +}
  13235. +
  13236. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13237. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  13238. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  13239. +} \
  13240. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  13241. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  13242. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  13243. +}
  13244. +
  13245. +# else /* DWC_DEBUG_REGS */
  13246. +
  13247. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13248. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  13249. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  13250. +} \
  13251. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  13252. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  13253. +}
  13254. +
  13255. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13256. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  13257. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  13258. +} \
  13259. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  13260. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  13261. +}
  13262. +
  13263. +# endif /* DWC_DEBUG_REGS */
  13264. +
  13265. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  13266. +
  13267. +/** @endcond */
  13268. +
  13269. +
  13270. +#ifdef DWC_CRYPTOLIB
  13271. +/** @name Crypto Functions
  13272. + *
  13273. + * These are the low-level cryptographic functions used by the driver. */
  13274. +
  13275. +/** Perform AES CBC */
  13276. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  13277. +#define dwc_aes_cbc DWC_AES_CBC
  13278. +
  13279. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  13280. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  13281. +#define dwc_random_bytes DWC_RANDOM_BYTES
  13282. +
  13283. +/** Perform the SHA-256 hash function */
  13284. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  13285. +#define dwc_sha256 DWC_SHA256
  13286. +
  13287. +/** Calculated the HMAC-SHA256 */
  13288. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  13289. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  13290. +
  13291. +#endif /* DWC_CRYPTOLIB */
  13292. +
  13293. +
  13294. +/** @name Memory Allocation
  13295. + *
  13296. + * These function provide access to memory allocation. There are only 2 DMA
  13297. + * functions and 3 Regular memory functions that need to be implemented. None
  13298. + * of the memory debugging routines need to be implemented. The allocation
  13299. + * routines all ZERO the contents of the memory.
  13300. + *
  13301. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  13302. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  13303. + * keeps track of how much memory the driver is using at any given time. */
  13304. +
  13305. +#define DWC_PAGE_SIZE 4096
  13306. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  13307. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  13308. +
  13309. +#define DWC_INVALID_DMA_ADDR 0x0
  13310. +
  13311. +#ifdef DWC_LINUX
  13312. +/** Type for a DMA address */
  13313. +typedef dma_addr_t dwc_dma_t;
  13314. +#endif
  13315. +
  13316. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13317. +typedef bus_addr_t dwc_dma_t;
  13318. +#endif
  13319. +
  13320. +#ifdef DWC_FREEBSD
  13321. +typedef struct dwc_dmactx {
  13322. + struct device *dev;
  13323. + bus_dma_tag_t dma_tag;
  13324. + bus_dmamap_t dma_map;
  13325. + bus_addr_t dma_paddr;
  13326. + void *dma_vaddr;
  13327. +} dwc_dmactx_t;
  13328. +#endif
  13329. +
  13330. +#ifdef DWC_NETBSD
  13331. +typedef struct dwc_dmactx {
  13332. + struct device *dev;
  13333. + bus_dma_tag_t dma_tag;
  13334. + bus_dmamap_t dma_map;
  13335. + bus_dma_segment_t segs[1];
  13336. + int nsegs;
  13337. + bus_addr_t dma_paddr;
  13338. + void *dma_vaddr;
  13339. +} dwc_dmactx_t;
  13340. +#endif
  13341. +
  13342. +/* @todo these functions will be added in the future */
  13343. +#if 0
  13344. +/**
  13345. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  13346. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  13347. + * boundary requirements specified.
  13348. + *
  13349. + * @param[in] size Specifies the size of the buffers that will be allocated from
  13350. + * this pool.
  13351. + * @param[in] align Specifies the byte alignment requirements of the buffers
  13352. + * allocated from this pool. Must be a power of 2.
  13353. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  13354. + * this pool must not cross.
  13355. + *
  13356. + * @returns A pointer to an internal opaque structure which is not to be
  13357. + * accessed outside of these library functions. Use this handle to specify
  13358. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  13359. + * when you are done with it.
  13360. + */
  13361. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  13362. +
  13363. +/**
  13364. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  13365. + */
  13366. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  13367. +
  13368. +/**
  13369. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  13370. + */
  13371. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  13372. +
  13373. +/**
  13374. + * Free a previously allocated buffer from the DMA pool.
  13375. + */
  13376. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  13377. +#endif
  13378. +
  13379. +/** Allocates a DMA capable buffer and zeroes its contents. */
  13380. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  13381. +
  13382. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  13383. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  13384. +
  13385. +/** Frees a previously allocated buffer. */
  13386. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  13387. +
  13388. +/** Allocates a block of memory and zeroes its contents. */
  13389. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  13390. +
  13391. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  13392. + * which can be used inside interrupt context. The size should be sufficiently
  13393. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  13394. + * __DWC_ALLOC if it is atomic. */
  13395. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  13396. +
  13397. +/** Frees a previously allocated buffer. */
  13398. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  13399. +
  13400. +#ifndef DWC_DEBUG_MEMORY
  13401. +
  13402. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  13403. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  13404. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  13405. +
  13406. +# ifdef DWC_LINUX
  13407. +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) __DWC_DMA_ALLOC(_dev, _size_, _dma_)
  13408. +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) __DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_)
  13409. +#define DWC_DMA_FREE(_dev, _size_,_virt_, _dma_) __DWC_DMA_FREE(_dev, _size_, _virt_, _dma_)
  13410. +# endif
  13411. +
  13412. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13413. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  13414. +#define DWC_DMA_FREE __DWC_DMA_FREE
  13415. +# endif
  13416. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  13417. +
  13418. +#else /* DWC_DEBUG_MEMORY */
  13419. +
  13420. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  13421. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  13422. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  13423. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  13424. + char const *func, int line);
  13425. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  13426. + char const *func, int line);
  13427. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  13428. + dwc_dma_t dma_addr, char const *func, int line);
  13429. +
  13430. +extern int dwc_memory_debug_start(void *mem_ctx);
  13431. +extern void dwc_memory_debug_stop(void);
  13432. +extern void dwc_memory_debug_report(void);
  13433. +
  13434. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  13435. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  13436. + __func__, __LINE__)
  13437. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  13438. +
  13439. +# ifdef DWC_LINUX
  13440. +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) \
  13441. + dwc_dma_alloc_debug(_dev, _size_, _dma_, __func__, __LINE__)
  13442. +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) \
  13443. + dwc_dma_alloc_atomic_debug(_dev, _size_, _dma_, __func__, __LINE__)
  13444. +#define DWC_DMA_FREE(_dev, _size_, _virt_, _dma_) \
  13445. + dwc_dma_free_debug(_dev, _size_, _virt_, _dma_, __func__, __LINE__)
  13446. +# endif
  13447. +
  13448. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13449. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  13450. + _dma_, __func__, __LINE__)
  13451. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  13452. + _virt_, _dma_, __func__, __LINE__)
  13453. +# endif
  13454. +
  13455. +#endif /* DWC_DEBUG_MEMORY */
  13456. +
  13457. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  13458. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  13459. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  13460. +
  13461. +#ifdef DWC_LINUX
  13462. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  13463. + * just throw away the DMA context parameter.
  13464. + */
  13465. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  13466. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  13467. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  13468. +#endif
  13469. +
  13470. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13471. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  13472. + * them in using the DMA context parameter.
  13473. + */
  13474. +#define dwc_dma_alloc DWC_DMA_ALLOC
  13475. +#define dwc_dma_free DWC_DMA_FREE
  13476. +#endif
  13477. +
  13478. +
  13479. +/** @name Memory and String Processing */
  13480. +
  13481. +/** memset() clone */
  13482. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  13483. +#define dwc_memset DWC_MEMSET
  13484. +
  13485. +/** memcpy() clone */
  13486. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  13487. +#define dwc_memcpy DWC_MEMCPY
  13488. +
  13489. +/** memmove() clone */
  13490. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  13491. +#define dwc_memmove DWC_MEMMOVE
  13492. +
  13493. +/** memcmp() clone */
  13494. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  13495. +#define dwc_memcmp DWC_MEMCMP
  13496. +
  13497. +/** strcmp() clone */
  13498. +extern int DWC_STRCMP(void *s1, void *s2);
  13499. +#define dwc_strcmp DWC_STRCMP
  13500. +
  13501. +/** strncmp() clone */
  13502. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  13503. +#define dwc_strncmp DWC_STRNCMP
  13504. +
  13505. +/** strlen() clone, for NULL terminated ASCII strings */
  13506. +extern int DWC_STRLEN(char const *str);
  13507. +#define dwc_strlen DWC_STRLEN
  13508. +
  13509. +/** strcpy() clone, for NULL terminated ASCII strings */
  13510. +extern char *DWC_STRCPY(char *to, const char *from);
  13511. +#define dwc_strcpy DWC_STRCPY
  13512. +
  13513. +/** strdup() clone. If you wish to use memory allocation debugging, this
  13514. + * implementation of strdup should use the DWC_* memory routines instead of
  13515. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  13516. + * will not be seen by the debugging routines. */
  13517. +extern char *DWC_STRDUP(char const *str);
  13518. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  13519. +
  13520. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  13521. + * converted from the string str in base 10 unless the string begins with a "0x"
  13522. + * in which case it is base 16. String must be a NULL terminated sequence of
  13523. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  13524. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  13525. + * the number and end with a NULL character. If any invalid characters are
  13526. + * encountered or it returns with a negative error code and the results of the
  13527. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  13528. + * undefined. An example implementation using atoi() can be referenced from the
  13529. + * Linux implementation. */
  13530. +extern int DWC_ATOI(const char *str, int32_t *value);
  13531. +#define dwc_atoi DWC_ATOI
  13532. +
  13533. +/** Same as above but for unsigned. */
  13534. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  13535. +#define dwc_atoui DWC_ATOUI
  13536. +
  13537. +#ifdef DWC_UTFLIB
  13538. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  13539. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  13540. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  13541. +#endif
  13542. +
  13543. +
  13544. +/** @name Wait queues
  13545. + *
  13546. + * Wait queues provide a means of synchronizing between threads or processes. A
  13547. + * process can block on a waitq if some condition is not true, waiting for it to
  13548. + * become true. When the waitq is triggered all waiting process will get
  13549. + * unblocked and the condition will be check again. Waitqs should be triggered
  13550. + * every time a condition can potentially change.*/
  13551. +struct dwc_waitq;
  13552. +
  13553. +/** Type for a waitq */
  13554. +typedef struct dwc_waitq dwc_waitq_t;
  13555. +
  13556. +/** The type of waitq condition callback function. This is called every time
  13557. + * condition is evaluated. */
  13558. +typedef int (*dwc_waitq_condition_t)(void *data);
  13559. +
  13560. +/** Allocate a waitq */
  13561. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  13562. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  13563. +
  13564. +/** Free a waitq */
  13565. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  13566. +#define dwc_waitq_free DWC_WAITQ_FREE
  13567. +
  13568. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  13569. + * condition again. The function returns when the condition becomes true. The return value
  13570. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  13571. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  13572. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  13573. +
  13574. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  13575. + * check the condition again. The function returns when the condition become
  13576. + * true or the timeout has passed. The return value is 0 on condition true or
  13577. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  13578. + * error. */
  13579. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  13580. + void *data, int32_t msecs);
  13581. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  13582. +
  13583. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  13584. + * has potentially changed. */
  13585. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  13586. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  13587. +
  13588. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  13589. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  13590. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  13591. +
  13592. +
  13593. +/** @name Threads
  13594. + *
  13595. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  13596. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  13597. + * returns the value from the thread.
  13598. + */
  13599. +
  13600. +struct dwc_thread;
  13601. +
  13602. +/** Type for a thread */
  13603. +typedef struct dwc_thread dwc_thread_t;
  13604. +
  13605. +/** The thread function */
  13606. +typedef int (*dwc_thread_function_t)(void *data);
  13607. +
  13608. +/** Create a thread and start it running the thread_function. Returns a handle
  13609. + * to the thread */
  13610. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  13611. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  13612. +
  13613. +/** Stops a thread. Return the value returned by the thread. Or will return
  13614. + * DWC_ABORT if the thread never started. */
  13615. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  13616. +#define dwc_thread_stop DWC_THREAD_STOP
  13617. +
  13618. +/** Signifies to the thread that it must stop. */
  13619. +#ifdef DWC_LINUX
  13620. +/* Linux doesn't need any parameters for kthread_should_stop() */
  13621. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  13622. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  13623. +
  13624. +/* No thread_exit function in Linux */
  13625. +#define dwc_thread_exit(_thrd_)
  13626. +#endif
  13627. +
  13628. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13629. +/** BSD needs the thread pointer for kthread_suspend_check() */
  13630. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  13631. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  13632. +
  13633. +/** The thread must call this to exit. */
  13634. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  13635. +#define dwc_thread_exit DWC_THREAD_EXIT
  13636. +#endif
  13637. +
  13638. +
  13639. +/** @name Work queues
  13640. + *
  13641. + * Workqs are used to queue a callback function to be called at some later time,
  13642. + * in another thread. */
  13643. +struct dwc_workq;
  13644. +
  13645. +/** Type for a workq */
  13646. +typedef struct dwc_workq dwc_workq_t;
  13647. +
  13648. +/** The type of the callback function to be called. */
  13649. +typedef void (*dwc_work_callback_t)(void *data);
  13650. +
  13651. +/** Allocate a workq */
  13652. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  13653. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  13654. +
  13655. +/** Free a workq. All work must be completed before being freed. */
  13656. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  13657. +#define dwc_workq_free DWC_WORKQ_FREE
  13658. +
  13659. +/** Schedule a callback on the workq, passing in data. The function will be
  13660. + * scheduled at some later time. */
  13661. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  13662. + void *data, char *format, ...)
  13663. +#ifdef __GNUC__
  13664. + __attribute__ ((format(printf, 4, 5)));
  13665. +#else
  13666. + ;
  13667. +#endif
  13668. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  13669. +
  13670. +/** Schedule a callback on the workq, that will be called until at least
  13671. + * given number miliseconds have passed. */
  13672. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  13673. + void *data, uint32_t time, char *format, ...)
  13674. +#ifdef __GNUC__
  13675. + __attribute__ ((format(printf, 5, 6)));
  13676. +#else
  13677. + ;
  13678. +#endif
  13679. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  13680. +
  13681. +/** The number of processes in the workq */
  13682. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  13683. +#define dwc_workq_pending DWC_WORKQ_PENDING
  13684. +
  13685. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  13686. + * 0 on timeout. */
  13687. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  13688. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  13689. +
  13690. +
  13691. +/** @name Tasklets
  13692. + *
  13693. + */
  13694. +struct dwc_tasklet;
  13695. +
  13696. +/** Type for a tasklet */
  13697. +typedef struct dwc_tasklet dwc_tasklet_t;
  13698. +
  13699. +/** The type of the callback function to be called */
  13700. +typedef void (*dwc_tasklet_callback_t)(void *data);
  13701. +
  13702. +/** Allocates a tasklet */
  13703. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  13704. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  13705. +
  13706. +/** Frees a tasklet */
  13707. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  13708. +#define dwc_task_free DWC_TASK_FREE
  13709. +
  13710. +/** Schedules a tasklet to run */
  13711. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  13712. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  13713. +
  13714. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  13715. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  13716. +
  13717. +/** @name Timer
  13718. + *
  13719. + * Callbacks must be small and atomic.
  13720. + */
  13721. +struct dwc_timer;
  13722. +
  13723. +/** Type for a timer */
  13724. +typedef struct dwc_timer dwc_timer_t;
  13725. +
  13726. +/** The type of the callback function to be called */
  13727. +typedef void (*dwc_timer_callback_t)(void *data);
  13728. +
  13729. +/** Allocates a timer */
  13730. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  13731. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  13732. +
  13733. +/** Frees a timer */
  13734. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  13735. +#define dwc_timer_free DWC_TIMER_FREE
  13736. +
  13737. +/** Schedules the timer to run at time ms from now. And will repeat at every
  13738. + * repeat_interval msec therafter
  13739. + *
  13740. + * Modifies a timer that is still awaiting execution to a new expiration time.
  13741. + * The mod_time is added to the old time. */
  13742. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  13743. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  13744. +
  13745. +/** Disables the timer from execution. */
  13746. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  13747. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  13748. +
  13749. +
  13750. +/** @name Spinlocks
  13751. + *
  13752. + * These locks are used when the work between the lock/unlock is atomic and
  13753. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  13754. + * suitable to lock between interrupt/non-interrupt context. They also lock
  13755. + * between processes if you have multiple CPUs or Preemption. If you don't have
  13756. + * multiple CPUS or Preemption, then the you can simply implement the
  13757. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  13758. + * the work between the lock/unlock is atomic, the process context will never
  13759. + * change, and so you never have to lock between processes. */
  13760. +
  13761. +struct dwc_spinlock;
  13762. +
  13763. +/** Type for a spinlock */
  13764. +typedef struct dwc_spinlock dwc_spinlock_t;
  13765. +
  13766. +/** Type for the 'flags' argument to spinlock funtions */
  13767. +typedef unsigned long dwc_irqflags_t;
  13768. +
  13769. +/** Returns an initialized lock variable. This function should allocate and
  13770. + * initialize the OS-specific data structure used for locking. This data
  13771. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  13772. + * be freed by the DWC_FREE_LOCK when it is no longer used.
  13773. + *
  13774. + * For Linux Spinlock Debugging make it macro because the debugging routines use
  13775. + * the symbol name to determine recursive locking. Using a wrapper function
  13776. + * makes it falsely think recursive locking occurs. */
  13777. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
  13778. +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
  13779. + lock = DWC_ALLOC(sizeof(spinlock_t)); \
  13780. + if (lock) { \
  13781. + spin_lock_init((spinlock_t *)lock); \
  13782. + } \
  13783. +})
  13784. +#else
  13785. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  13786. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  13787. +#endif
  13788. +
  13789. +/** Frees an initialized lock variable. */
  13790. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  13791. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  13792. +
  13793. +/** Disables interrupts and blocks until it acquires the lock.
  13794. + *
  13795. + * @param lock Pointer to the spinlock.
  13796. + * @param flags Unsigned long for irq flags storage.
  13797. + */
  13798. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  13799. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  13800. +
  13801. +/** Re-enables the interrupt and releases the lock.
  13802. + *
  13803. + * @param lock Pointer to the spinlock.
  13804. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  13805. + * passed into DWC_LOCK.
  13806. + */
  13807. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  13808. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  13809. +
  13810. +/** Blocks until it acquires the lock.
  13811. + *
  13812. + * @param lock Pointer to the spinlock.
  13813. + */
  13814. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  13815. +#define dwc_spinlock DWC_SPINLOCK
  13816. +
  13817. +/** Releases the lock.
  13818. + *
  13819. + * @param lock Pointer to the spinlock.
  13820. + */
  13821. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  13822. +#define dwc_spinunlock DWC_SPINUNLOCK
  13823. +
  13824. +
  13825. +/** @name Mutexes
  13826. + *
  13827. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  13828. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  13829. + */
  13830. +
  13831. +struct dwc_mutex;
  13832. +
  13833. +/** Type for a mutex */
  13834. +typedef struct dwc_mutex dwc_mutex_t;
  13835. +
  13836. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  13837. + * the symbol to determine recursive locking. This makes it falsely think
  13838. + * recursive locking occurs. */
  13839. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  13840. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  13841. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  13842. + mutex_init((struct mutex *)__mutexp); \
  13843. +})
  13844. +#endif
  13845. +
  13846. +/** Allocate a mutex */
  13847. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  13848. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  13849. +
  13850. +/* For memory leak debugging when using Linux Mutex Debugging */
  13851. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  13852. +#define DWC_MUTEX_FREE(__mutexp) do { \
  13853. + mutex_destroy((struct mutex *)__mutexp); \
  13854. + DWC_FREE(__mutexp); \
  13855. +} while(0)
  13856. +#else
  13857. +/** Free a mutex */
  13858. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  13859. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  13860. +#endif
  13861. +
  13862. +/** Lock a mutex */
  13863. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  13864. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  13865. +
  13866. +/** Non-blocking lock returns 1 on successful lock. */
  13867. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  13868. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  13869. +
  13870. +/** Unlock a mutex */
  13871. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  13872. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  13873. +
  13874. +
  13875. +/** @name Time */
  13876. +
  13877. +/** Microsecond delay.
  13878. + *
  13879. + * @param usecs Microseconds to delay.
  13880. + */
  13881. +extern void DWC_UDELAY(uint32_t usecs);
  13882. +#define dwc_udelay DWC_UDELAY
  13883. +
  13884. +/** Millisecond delay.
  13885. + *
  13886. + * @param msecs Milliseconds to delay.
  13887. + */
  13888. +extern void DWC_MDELAY(uint32_t msecs);
  13889. +#define dwc_mdelay DWC_MDELAY
  13890. +
  13891. +/** Non-busy waiting.
  13892. + * Sleeps for specified number of milliseconds.
  13893. + *
  13894. + * @param msecs Milliseconds to sleep.
  13895. + */
  13896. +extern void DWC_MSLEEP(uint32_t msecs);
  13897. +#define dwc_msleep DWC_MSLEEP
  13898. +
  13899. +/**
  13900. + * Returns number of milliseconds since boot.
  13901. + */
  13902. +extern uint32_t DWC_TIME(void);
  13903. +#define dwc_time DWC_TIME
  13904. +
  13905. +
  13906. +
  13907. +
  13908. +/* @mainpage DWC Portability and Common Library
  13909. + *
  13910. + * This is the documentation for the DWC Portability and Common Library.
  13911. + *
  13912. + * @section intro Introduction
  13913. + *
  13914. + * The DWC Portability library consists of wrapper calls and data structures to
  13915. + * all low-level functions which are typically provided by the OS. The WUDEV
  13916. + * driver uses only these functions. In order to port the WUDEV driver, only
  13917. + * the functions in this library need to be re-implemented, with the same
  13918. + * behavior as documented here.
  13919. + *
  13920. + * The Common library consists of higher level functions, which rely only on
  13921. + * calling the functions from the DWC Portability library. These common
  13922. + * routines are shared across modules. Some of the common libraries need to be
  13923. + * used directly by the driver programmer when porting WUDEV. Such as the
  13924. + * parameter and notification libraries.
  13925. + *
  13926. + * @section low Portability Library OS Wrapper Functions
  13927. + *
  13928. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  13929. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  13930. + * these functions are included in the dwc_os.h file.
  13931. + *
  13932. + * There are many functions here covering a wide array of OS services. Please
  13933. + * see dwc_os.h for details, and implementation notes for each function.
  13934. + *
  13935. + * @section common Common Library Functions
  13936. + *
  13937. + * Any function starting with dwc and in all lowercase is a common library
  13938. + * routine. These functions have a portable implementation and do not need to
  13939. + * be reimplemented when porting. The common routines can be used by any
  13940. + * driver, and some must be used by the end user to control the drivers. For
  13941. + * example, you must use the Parameter common library in order to set the
  13942. + * parameters in the WUDEV module.
  13943. + *
  13944. + * The common libraries consist of the following:
  13945. + *
  13946. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  13947. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  13948. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  13949. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  13950. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  13951. + * - Modpow - Used internally only. See dwc_modpow.h
  13952. + * - DH - Used internally only. See dwc_dh.h
  13953. + * - Crypto - Used internally only. See dwc_crypto.h
  13954. + *
  13955. + *
  13956. + * @section prereq Prerequistes For dwc_os.h
  13957. + * @subsection types Data Types
  13958. + *
  13959. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  13960. + * compilation environment. These data types are:
  13961. + *
  13962. + * - uint8_t - unsigned 8-bit data type
  13963. + * - int8_t - signed 8-bit data type
  13964. + * - uint16_t - unsigned 16-bit data type
  13965. + * - int16_t - signed 16-bit data type
  13966. + * - uint32_t - unsigned 32-bit data type
  13967. + * - int32_t - signed 32-bit data type
  13968. + * - uint64_t - unsigned 64-bit data type
  13969. + * - int64_t - signed 64-bit data type
  13970. + *
  13971. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  13972. + * that is to modify the top of the file to include the appropriate header.
  13973. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  13974. + * defined, the correct header will be added. A standard header <stdint.h> is
  13975. + * also used for environments where standard C headers are available.
  13976. + *
  13977. + * @subsection stdarg Variable Arguments
  13978. + *
  13979. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  13980. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  13981. + * provided in your enviornment in order to use dwc_os.h with the debug and
  13982. + * tracing message functionality.
  13983. + *
  13984. + * @subsection thread Threading
  13985. + *
  13986. + * WUDEV Core must be run on an operating system that provides for multiple
  13987. + * threads/processes. Threading can be implemented in many ways, even in
  13988. + * embedded systems without an operating system. At the bare minimum, the
  13989. + * system should be able to start any number of processes at any time to handle
  13990. + * special work. It need not be a pre-emptive system. Process context can
  13991. + * change upon a call to a blocking function. The hardware interrupt context
  13992. + * that calls the module's ISR() function must be differentiable from process
  13993. + * context, even if your processes are impemented via a hardware interrupt.
  13994. + * Further locking mechanism between process must exist (or be implemented), and
  13995. + * process context must have a way to disable interrupts for a period of time to
  13996. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  13997. + * threading should be able to be implemented with the defined behavior.
  13998. + *
  13999. + */
  14000. +
  14001. +#ifdef __cplusplus
  14002. +}
  14003. +#endif
  14004. +
  14005. +#endif /* _DWC_OS_H_ */
  14006. --- /dev/null
  14007. +++ b/drivers/usb/host/dwc_common_port/usb.h
  14008. @@ -0,0 +1,946 @@
  14009. +/*
  14010. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  14011. + * All rights reserved.
  14012. + *
  14013. + * This code is derived from software contributed to The NetBSD Foundation
  14014. + * by Lennart Augustsson ([email protected]) at
  14015. + * Carlstedt Research & Technology.
  14016. + *
  14017. + * Redistribution and use in source and binary forms, with or without
  14018. + * modification, are permitted provided that the following conditions
  14019. + * are met:
  14020. + * 1. Redistributions of source code must retain the above copyright
  14021. + * notice, this list of conditions and the following disclaimer.
  14022. + * 2. Redistributions in binary form must reproduce the above copyright
  14023. + * notice, this list of conditions and the following disclaimer in the
  14024. + * documentation and/or other materials provided with the distribution.
  14025. + * 3. All advertising materials mentioning features or use of this software
  14026. + * must display the following acknowledgement:
  14027. + * This product includes software developed by the NetBSD
  14028. + * Foundation, Inc. and its contributors.
  14029. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  14030. + * contributors may be used to endorse or promote products derived
  14031. + * from this software without specific prior written permission.
  14032. + *
  14033. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  14034. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  14035. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  14036. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  14037. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  14038. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  14039. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  14040. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  14041. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  14042. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  14043. + * POSSIBILITY OF SUCH DAMAGE.
  14044. + */
  14045. +
  14046. +/* Modified by Synopsys, Inc, 12/12/2007 */
  14047. +
  14048. +
  14049. +#ifndef _USB_H_
  14050. +#define _USB_H_
  14051. +
  14052. +#ifdef __cplusplus
  14053. +extern "C" {
  14054. +#endif
  14055. +
  14056. +/*
  14057. + * The USB records contain some unaligned little-endian word
  14058. + * components. The U[SG]ETW macros take care of both the alignment
  14059. + * and endian problem and should always be used to access non-byte
  14060. + * values.
  14061. + */
  14062. +typedef u_int8_t uByte;
  14063. +typedef u_int8_t uWord[2];
  14064. +typedef u_int8_t uDWord[4];
  14065. +
  14066. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  14067. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  14068. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  14069. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  14070. +
  14071. +#if 1
  14072. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  14073. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  14074. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  14075. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  14076. + (w)[1] = (u_int8_t)((v) >> 8), \
  14077. + (w)[2] = (u_int8_t)((v) >> 16), \
  14078. + (w)[3] = (u_int8_t)((v) >> 24))
  14079. +#else
  14080. +/*
  14081. + * On little-endian machines that can handle unanliged accesses
  14082. + * (e.g. i386) these macros can be replaced by the following.
  14083. + */
  14084. +#define UGETW(w) (*(u_int16_t *)(w))
  14085. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  14086. +#define UGETDW(w) (*(u_int32_t *)(w))
  14087. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  14088. +#endif
  14089. +
  14090. +/*
  14091. + * Macros for accessing UAS IU fields, which are big-endian
  14092. + */
  14093. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  14094. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  14095. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  14096. + ((x) >> 8) & 0xff, (x) & 0xff }
  14097. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  14098. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  14099. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  14100. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  14101. + (w)[1] = (u_int8_t)((v) >> 16), \
  14102. + (w)[2] = (u_int8_t)((v) >> 8), \
  14103. + (w)[3] = (u_int8_t)(v))
  14104. +
  14105. +#define UPACKED __attribute__((__packed__))
  14106. +
  14107. +typedef struct {
  14108. + uByte bmRequestType;
  14109. + uByte bRequest;
  14110. + uWord wValue;
  14111. + uWord wIndex;
  14112. + uWord wLength;
  14113. +} UPACKED usb_device_request_t;
  14114. +
  14115. +#define UT_GET_DIR(a) ((a) & 0x80)
  14116. +#define UT_WRITE 0x00
  14117. +#define UT_READ 0x80
  14118. +
  14119. +#define UT_GET_TYPE(a) ((a) & 0x60)
  14120. +#define UT_STANDARD 0x00
  14121. +#define UT_CLASS 0x20
  14122. +#define UT_VENDOR 0x40
  14123. +
  14124. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  14125. +#define UT_DEVICE 0x00
  14126. +#define UT_INTERFACE 0x01
  14127. +#define UT_ENDPOINT 0x02
  14128. +#define UT_OTHER 0x03
  14129. +
  14130. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  14131. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  14132. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  14133. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  14134. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  14135. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  14136. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  14137. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  14138. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  14139. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  14140. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  14141. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  14142. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  14143. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  14144. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  14145. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  14146. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  14147. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  14148. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  14149. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  14150. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  14151. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  14152. +
  14153. +/* Requests */
  14154. +#define UR_GET_STATUS 0x00
  14155. +#define USTAT_STANDARD_STATUS 0x00
  14156. +#define WUSTAT_WUSB_FEATURE 0x01
  14157. +#define WUSTAT_CHANNEL_INFO 0x02
  14158. +#define WUSTAT_RECEIVED_DATA 0x03
  14159. +#define WUSTAT_MAS_AVAILABILITY 0x04
  14160. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  14161. +#define UR_CLEAR_FEATURE 0x01
  14162. +#define UR_SET_FEATURE 0x03
  14163. +#define UR_SET_AND_TEST_FEATURE 0x0c
  14164. +#define UR_SET_ADDRESS 0x05
  14165. +#define UR_GET_DESCRIPTOR 0x06
  14166. +#define UDESC_DEVICE 0x01
  14167. +#define UDESC_CONFIG 0x02
  14168. +#define UDESC_STRING 0x03
  14169. +#define UDESC_INTERFACE 0x04
  14170. +#define UDESC_ENDPOINT 0x05
  14171. +#define UDESC_SS_USB_COMPANION 0x30
  14172. +#define UDESC_DEVICE_QUALIFIER 0x06
  14173. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  14174. +#define UDESC_INTERFACE_POWER 0x08
  14175. +#define UDESC_OTG 0x09
  14176. +#define WUDESC_SECURITY 0x0c
  14177. +#define WUDESC_KEY 0x0d
  14178. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  14179. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  14180. +#define WUD_KEY_TYPE_ASSOC 0x01
  14181. +#define WUD_KEY_TYPE_GTK 0x02
  14182. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  14183. +#define WUD_KEY_ORIGIN_HOST 0x00
  14184. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  14185. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  14186. +#define WUDESC_BOS 0x0f
  14187. +#define WUDESC_DEVICE_CAPABILITY 0x10
  14188. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  14189. +#define UDESC_BOS 0x0f
  14190. +#define UDESC_DEVICE_CAPABILITY 0x10
  14191. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  14192. +#define UDESC_CS_CONFIG 0x22
  14193. +#define UDESC_CS_STRING 0x23
  14194. +#define UDESC_CS_INTERFACE 0x24
  14195. +#define UDESC_CS_ENDPOINT 0x25
  14196. +#define UDESC_HUB 0x29
  14197. +#define UR_SET_DESCRIPTOR 0x07
  14198. +#define UR_GET_CONFIG 0x08
  14199. +#define UR_SET_CONFIG 0x09
  14200. +#define UR_GET_INTERFACE 0x0a
  14201. +#define UR_SET_INTERFACE 0x0b
  14202. +#define UR_SYNCH_FRAME 0x0c
  14203. +#define WUR_SET_ENCRYPTION 0x0d
  14204. +#define WUR_GET_ENCRYPTION 0x0e
  14205. +#define WUR_SET_HANDSHAKE 0x0f
  14206. +#define WUR_GET_HANDSHAKE 0x10
  14207. +#define WUR_SET_CONNECTION 0x11
  14208. +#define WUR_SET_SECURITY_DATA 0x12
  14209. +#define WUR_GET_SECURITY_DATA 0x13
  14210. +#define WUR_SET_WUSB_DATA 0x14
  14211. +#define WUDATA_DRPIE_INFO 0x01
  14212. +#define WUDATA_TRANSMIT_DATA 0x02
  14213. +#define WUDATA_TRANSMIT_PARAMS 0x03
  14214. +#define WUDATA_RECEIVE_PARAMS 0x04
  14215. +#define WUDATA_TRANSMIT_POWER 0x05
  14216. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  14217. +#define WUR_LOOPBACK_DATA_READ 0x16
  14218. +#define WUR_SET_INTERFACE_DS 0x17
  14219. +
  14220. +/* Feature numbers */
  14221. +#define UF_ENDPOINT_HALT 0
  14222. +#define UF_DEVICE_REMOTE_WAKEUP 1
  14223. +#define UF_TEST_MODE 2
  14224. +#define UF_DEVICE_B_HNP_ENABLE 3
  14225. +#define UF_DEVICE_A_HNP_SUPPORT 4
  14226. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  14227. +#define WUF_WUSB 3
  14228. +#define WUF_TX_DRPIE 0x0
  14229. +#define WUF_DEV_XMIT_PACKET 0x1
  14230. +#define WUF_COUNT_PACKETS 0x2
  14231. +#define WUF_CAPTURE_PACKETS 0x3
  14232. +#define UF_FUNCTION_SUSPEND 0
  14233. +#define UF_U1_ENABLE 48
  14234. +#define UF_U2_ENABLE 49
  14235. +#define UF_LTM_ENABLE 50
  14236. +
  14237. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  14238. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  14239. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  14240. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  14241. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  14242. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  14243. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  14244. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  14245. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  14246. +
  14247. +#ifdef _MSC_VER
  14248. +#include <pshpack1.h>
  14249. +#endif
  14250. +
  14251. +typedef struct {
  14252. + uByte bLength;
  14253. + uByte bDescriptorType;
  14254. + uByte bDescriptorSubtype;
  14255. +} UPACKED usb_descriptor_t;
  14256. +
  14257. +typedef struct {
  14258. + uByte bLength;
  14259. + uByte bDescriptorType;
  14260. +} UPACKED usb_descriptor_header_t;
  14261. +
  14262. +typedef struct {
  14263. + uByte bLength;
  14264. + uByte bDescriptorType;
  14265. + uWord bcdUSB;
  14266. +#define UD_USB_2_0 0x0200
  14267. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  14268. + uByte bDeviceClass;
  14269. + uByte bDeviceSubClass;
  14270. + uByte bDeviceProtocol;
  14271. + uByte bMaxPacketSize;
  14272. + /* The fields below are not part of the initial descriptor. */
  14273. + uWord idVendor;
  14274. + uWord idProduct;
  14275. + uWord bcdDevice;
  14276. + uByte iManufacturer;
  14277. + uByte iProduct;
  14278. + uByte iSerialNumber;
  14279. + uByte bNumConfigurations;
  14280. +} UPACKED usb_device_descriptor_t;
  14281. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  14282. +
  14283. +typedef struct {
  14284. + uByte bLength;
  14285. + uByte bDescriptorType;
  14286. + uWord wTotalLength;
  14287. + uByte bNumInterface;
  14288. + uByte bConfigurationValue;
  14289. + uByte iConfiguration;
  14290. +#define UC_ATT_ONE (1 << 7) /* must be set */
  14291. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  14292. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  14293. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  14294. + uByte bmAttributes;
  14295. +#define UC_BUS_POWERED 0x80
  14296. +#define UC_SELF_POWERED 0x40
  14297. +#define UC_REMOTE_WAKEUP 0x20
  14298. + uByte bMaxPower; /* max current in 2 mA units */
  14299. +#define UC_POWER_FACTOR 2
  14300. +} UPACKED usb_config_descriptor_t;
  14301. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  14302. +
  14303. +typedef struct {
  14304. + uByte bLength;
  14305. + uByte bDescriptorType;
  14306. + uByte bInterfaceNumber;
  14307. + uByte bAlternateSetting;
  14308. + uByte bNumEndpoints;
  14309. + uByte bInterfaceClass;
  14310. + uByte bInterfaceSubClass;
  14311. + uByte bInterfaceProtocol;
  14312. + uByte iInterface;
  14313. +} UPACKED usb_interface_descriptor_t;
  14314. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  14315. +
  14316. +typedef struct {
  14317. + uByte bLength;
  14318. + uByte bDescriptorType;
  14319. + uByte bEndpointAddress;
  14320. +#define UE_GET_DIR(a) ((a) & 0x80)
  14321. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  14322. +#define UE_DIR_IN 0x80
  14323. +#define UE_DIR_OUT 0x00
  14324. +#define UE_ADDR 0x0f
  14325. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  14326. + uByte bmAttributes;
  14327. +#define UE_XFERTYPE 0x03
  14328. +#define UE_CONTROL 0x00
  14329. +#define UE_ISOCHRONOUS 0x01
  14330. +#define UE_BULK 0x02
  14331. +#define UE_INTERRUPT 0x03
  14332. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  14333. +#define UE_ISO_TYPE 0x0c
  14334. +#define UE_ISO_ASYNC 0x04
  14335. +#define UE_ISO_ADAPT 0x08
  14336. +#define UE_ISO_SYNC 0x0c
  14337. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  14338. + uWord wMaxPacketSize;
  14339. + uByte bInterval;
  14340. +} UPACKED usb_endpoint_descriptor_t;
  14341. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  14342. +
  14343. +typedef struct ss_endpoint_companion_descriptor {
  14344. + uByte bLength;
  14345. + uByte bDescriptorType;
  14346. + uByte bMaxBurst;
  14347. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  14348. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  14349. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  14350. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  14351. + uByte bmAttributes;
  14352. + uWord wBytesPerInterval;
  14353. +} UPACKED ss_endpoint_companion_descriptor_t;
  14354. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  14355. +
  14356. +typedef struct {
  14357. + uByte bLength;
  14358. + uByte bDescriptorType;
  14359. + uWord bString[127];
  14360. +} UPACKED usb_string_descriptor_t;
  14361. +#define USB_MAX_STRING_LEN 128
  14362. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  14363. +
  14364. +/* Hub specific request */
  14365. +#define UR_GET_BUS_STATE 0x02
  14366. +#define UR_CLEAR_TT_BUFFER 0x08
  14367. +#define UR_RESET_TT 0x09
  14368. +#define UR_GET_TT_STATE 0x0a
  14369. +#define UR_STOP_TT 0x0b
  14370. +
  14371. +/* Hub features */
  14372. +#define UHF_C_HUB_LOCAL_POWER 0
  14373. +#define UHF_C_HUB_OVER_CURRENT 1
  14374. +#define UHF_PORT_CONNECTION 0
  14375. +#define UHF_PORT_ENABLE 1
  14376. +#define UHF_PORT_SUSPEND 2
  14377. +#define UHF_PORT_OVER_CURRENT 3
  14378. +#define UHF_PORT_RESET 4
  14379. +#define UHF_PORT_L1 5
  14380. +#define UHF_PORT_POWER 8
  14381. +#define UHF_PORT_LOW_SPEED 9
  14382. +#define UHF_PORT_HIGH_SPEED 10
  14383. +#define UHF_C_PORT_CONNECTION 16
  14384. +#define UHF_C_PORT_ENABLE 17
  14385. +#define UHF_C_PORT_SUSPEND 18
  14386. +#define UHF_C_PORT_OVER_CURRENT 19
  14387. +#define UHF_C_PORT_RESET 20
  14388. +#define UHF_C_PORT_L1 23
  14389. +#define UHF_PORT_TEST 21
  14390. +#define UHF_PORT_INDICATOR 22
  14391. +
  14392. +typedef struct {
  14393. + uByte bDescLength;
  14394. + uByte bDescriptorType;
  14395. + uByte bNbrPorts;
  14396. + uWord wHubCharacteristics;
  14397. +#define UHD_PWR 0x0003
  14398. +#define UHD_PWR_GANGED 0x0000
  14399. +#define UHD_PWR_INDIVIDUAL 0x0001
  14400. +#define UHD_PWR_NO_SWITCH 0x0002
  14401. +#define UHD_COMPOUND 0x0004
  14402. +#define UHD_OC 0x0018
  14403. +#define UHD_OC_GLOBAL 0x0000
  14404. +#define UHD_OC_INDIVIDUAL 0x0008
  14405. +#define UHD_OC_NONE 0x0010
  14406. +#define UHD_TT_THINK 0x0060
  14407. +#define UHD_TT_THINK_8 0x0000
  14408. +#define UHD_TT_THINK_16 0x0020
  14409. +#define UHD_TT_THINK_24 0x0040
  14410. +#define UHD_TT_THINK_32 0x0060
  14411. +#define UHD_PORT_IND 0x0080
  14412. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  14413. +#define UHD_PWRON_FACTOR 2
  14414. + uByte bHubContrCurrent;
  14415. + uByte DeviceRemovable[32]; /* max 255 ports */
  14416. +#define UHD_NOT_REMOV(desc, i) \
  14417. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  14418. + /* deprecated */ uByte PortPowerCtrlMask[1];
  14419. +} UPACKED usb_hub_descriptor_t;
  14420. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  14421. +
  14422. +typedef struct {
  14423. + uByte bLength;
  14424. + uByte bDescriptorType;
  14425. + uWord bcdUSB;
  14426. + uByte bDeviceClass;
  14427. + uByte bDeviceSubClass;
  14428. + uByte bDeviceProtocol;
  14429. + uByte bMaxPacketSize0;
  14430. + uByte bNumConfigurations;
  14431. + uByte bReserved;
  14432. +} UPACKED usb_device_qualifier_t;
  14433. +#define USB_DEVICE_QUALIFIER_SIZE 10
  14434. +
  14435. +typedef struct {
  14436. + uByte bLength;
  14437. + uByte bDescriptorType;
  14438. + uByte bmAttributes;
  14439. +#define UOTG_SRP 0x01
  14440. +#define UOTG_HNP 0x02
  14441. +} UPACKED usb_otg_descriptor_t;
  14442. +
  14443. +/* OTG feature selectors */
  14444. +#define UOTG_B_HNP_ENABLE 3
  14445. +#define UOTG_A_HNP_SUPPORT 4
  14446. +#define UOTG_A_ALT_HNP_SUPPORT 5
  14447. +
  14448. +typedef struct {
  14449. + uWord wStatus;
  14450. +/* Device status flags */
  14451. +#define UDS_SELF_POWERED 0x0001
  14452. +#define UDS_REMOTE_WAKEUP 0x0002
  14453. +/* Endpoint status flags */
  14454. +#define UES_HALT 0x0001
  14455. +} UPACKED usb_status_t;
  14456. +
  14457. +typedef struct {
  14458. + uWord wHubStatus;
  14459. +#define UHS_LOCAL_POWER 0x0001
  14460. +#define UHS_OVER_CURRENT 0x0002
  14461. + uWord wHubChange;
  14462. +} UPACKED usb_hub_status_t;
  14463. +
  14464. +typedef struct {
  14465. + uWord wPortStatus;
  14466. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  14467. +#define UPS_PORT_ENABLED 0x0002
  14468. +#define UPS_SUSPEND 0x0004
  14469. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  14470. +#define UPS_RESET 0x0010
  14471. +#define UPS_PORT_POWER 0x0100
  14472. +#define UPS_LOW_SPEED 0x0200
  14473. +#define UPS_HIGH_SPEED 0x0400
  14474. +#define UPS_PORT_TEST 0x0800
  14475. +#define UPS_PORT_INDICATOR 0x1000
  14476. + uWord wPortChange;
  14477. +#define UPS_C_CONNECT_STATUS 0x0001
  14478. +#define UPS_C_PORT_ENABLED 0x0002
  14479. +#define UPS_C_SUSPEND 0x0004
  14480. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  14481. +#define UPS_C_PORT_RESET 0x0010
  14482. +} UPACKED usb_port_status_t;
  14483. +
  14484. +#ifdef _MSC_VER
  14485. +#include <poppack.h>
  14486. +#endif
  14487. +
  14488. +/* Device class codes */
  14489. +#define UDCLASS_IN_INTERFACE 0x00
  14490. +#define UDCLASS_COMM 0x02
  14491. +#define UDCLASS_HUB 0x09
  14492. +#define UDSUBCLASS_HUB 0x00
  14493. +#define UDPROTO_FSHUB 0x00
  14494. +#define UDPROTO_HSHUBSTT 0x01
  14495. +#define UDPROTO_HSHUBMTT 0x02
  14496. +#define UDCLASS_DIAGNOSTIC 0xdc
  14497. +#define UDCLASS_WIRELESS 0xe0
  14498. +#define UDSUBCLASS_RF 0x01
  14499. +#define UDPROTO_BLUETOOTH 0x01
  14500. +#define UDCLASS_VENDOR 0xff
  14501. +
  14502. +/* Interface class codes */
  14503. +#define UICLASS_UNSPEC 0x00
  14504. +
  14505. +#define UICLASS_AUDIO 0x01
  14506. +#define UISUBCLASS_AUDIOCONTROL 1
  14507. +#define UISUBCLASS_AUDIOSTREAM 2
  14508. +#define UISUBCLASS_MIDISTREAM 3
  14509. +
  14510. +#define UICLASS_CDC 0x02 /* communication */
  14511. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  14512. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  14513. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  14514. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  14515. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  14516. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  14517. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  14518. +#define UIPROTO_CDC_AT 1
  14519. +
  14520. +#define UICLASS_HID 0x03
  14521. +#define UISUBCLASS_BOOT 1
  14522. +#define UIPROTO_BOOT_KEYBOARD 1
  14523. +
  14524. +#define UICLASS_PHYSICAL 0x05
  14525. +
  14526. +#define UICLASS_IMAGE 0x06
  14527. +
  14528. +#define UICLASS_PRINTER 0x07
  14529. +#define UISUBCLASS_PRINTER 1
  14530. +#define UIPROTO_PRINTER_UNI 1
  14531. +#define UIPROTO_PRINTER_BI 2
  14532. +#define UIPROTO_PRINTER_1284 3
  14533. +
  14534. +#define UICLASS_MASS 0x08
  14535. +#define UISUBCLASS_RBC 1
  14536. +#define UISUBCLASS_SFF8020I 2
  14537. +#define UISUBCLASS_QIC157 3
  14538. +#define UISUBCLASS_UFI 4
  14539. +#define UISUBCLASS_SFF8070I 5
  14540. +#define UISUBCLASS_SCSI 6
  14541. +#define UIPROTO_MASS_CBI_I 0
  14542. +#define UIPROTO_MASS_CBI 1
  14543. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  14544. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  14545. +
  14546. +#define UICLASS_HUB 0x09
  14547. +#define UISUBCLASS_HUB 0
  14548. +#define UIPROTO_FSHUB 0
  14549. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  14550. +#define UIPROTO_HSHUBMTT 1
  14551. +
  14552. +#define UICLASS_CDC_DATA 0x0a
  14553. +#define UISUBCLASS_DATA 0
  14554. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  14555. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  14556. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  14557. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  14558. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  14559. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  14560. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  14561. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  14562. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  14563. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  14564. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  14565. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  14566. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  14567. +
  14568. +#define UICLASS_SMARTCARD 0x0b
  14569. +
  14570. +/*#define UICLASS_FIRM_UPD 0x0c*/
  14571. +
  14572. +#define UICLASS_SECURITY 0x0d
  14573. +
  14574. +#define UICLASS_DIAGNOSTIC 0xdc
  14575. +
  14576. +#define UICLASS_WIRELESS 0xe0
  14577. +#define UISUBCLASS_RF 0x01
  14578. +#define UIPROTO_BLUETOOTH 0x01
  14579. +
  14580. +#define UICLASS_APPL_SPEC 0xfe
  14581. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  14582. +#define UISUBCLASS_IRDA 2
  14583. +#define UIPROTO_IRDA 0
  14584. +
  14585. +#define UICLASS_VENDOR 0xff
  14586. +
  14587. +#define USB_HUB_MAX_DEPTH 5
  14588. +
  14589. +/*
  14590. + * Minimum time a device needs to be powered down to go through
  14591. + * a power cycle. XXX Are these time in the spec?
  14592. + */
  14593. +#define USB_POWER_DOWN_TIME 200 /* ms */
  14594. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  14595. +
  14596. +#if 0
  14597. +/* These are the values from the spec. */
  14598. +#define USB_PORT_RESET_DELAY 10 /* ms */
  14599. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  14600. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  14601. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  14602. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  14603. +#define USB_RESUME_DELAY (20*5) /* ms */
  14604. +#define USB_RESUME_WAIT 10 /* ms */
  14605. +#define USB_RESUME_RECOVERY 10 /* ms */
  14606. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  14607. +#else
  14608. +/* Allow for marginal (i.e. non-conforming) devices. */
  14609. +#define USB_PORT_RESET_DELAY 50 /* ms */
  14610. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  14611. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  14612. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  14613. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  14614. +#define USB_RESUME_DELAY (50*5) /* ms */
  14615. +#define USB_RESUME_WAIT 50 /* ms */
  14616. +#define USB_RESUME_RECOVERY 50 /* ms */
  14617. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  14618. +#endif
  14619. +
  14620. +#define USB_MIN_POWER 100 /* mA */
  14621. +#define USB_MAX_POWER 500 /* mA */
  14622. +
  14623. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  14624. +
  14625. +#define USB_UNCONFIG_NO 0
  14626. +#define USB_UNCONFIG_INDEX (-1)
  14627. +
  14628. +/*** ioctl() related stuff ***/
  14629. +
  14630. +struct usb_ctl_request {
  14631. + int ucr_addr;
  14632. + usb_device_request_t ucr_request;
  14633. + void *ucr_data;
  14634. + int ucr_flags;
  14635. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  14636. + int ucr_actlen; /* actual length transferred */
  14637. +};
  14638. +
  14639. +struct usb_alt_interface {
  14640. + int uai_config_index;
  14641. + int uai_interface_index;
  14642. + int uai_alt_no;
  14643. +};
  14644. +
  14645. +#define USB_CURRENT_CONFIG_INDEX (-1)
  14646. +#define USB_CURRENT_ALT_INDEX (-1)
  14647. +
  14648. +struct usb_config_desc {
  14649. + int ucd_config_index;
  14650. + usb_config_descriptor_t ucd_desc;
  14651. +};
  14652. +
  14653. +struct usb_interface_desc {
  14654. + int uid_config_index;
  14655. + int uid_interface_index;
  14656. + int uid_alt_index;
  14657. + usb_interface_descriptor_t uid_desc;
  14658. +};
  14659. +
  14660. +struct usb_endpoint_desc {
  14661. + int ued_config_index;
  14662. + int ued_interface_index;
  14663. + int ued_alt_index;
  14664. + int ued_endpoint_index;
  14665. + usb_endpoint_descriptor_t ued_desc;
  14666. +};
  14667. +
  14668. +struct usb_full_desc {
  14669. + int ufd_config_index;
  14670. + u_int ufd_size;
  14671. + u_char *ufd_data;
  14672. +};
  14673. +
  14674. +struct usb_string_desc {
  14675. + int usd_string_index;
  14676. + int usd_language_id;
  14677. + usb_string_descriptor_t usd_desc;
  14678. +};
  14679. +
  14680. +struct usb_ctl_report_desc {
  14681. + int ucrd_size;
  14682. + u_char ucrd_data[1024]; /* filled data size will vary */
  14683. +};
  14684. +
  14685. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  14686. +
  14687. +#define USB_MAX_DEVNAMES 4
  14688. +#define USB_MAX_DEVNAMELEN 16
  14689. +struct usb_device_info {
  14690. + u_int8_t udi_bus;
  14691. + u_int8_t udi_addr; /* device address */
  14692. + usb_event_cookie_t udi_cookie;
  14693. + char udi_product[USB_MAX_STRING_LEN];
  14694. + char udi_vendor[USB_MAX_STRING_LEN];
  14695. + char udi_release[8];
  14696. + u_int16_t udi_productNo;
  14697. + u_int16_t udi_vendorNo;
  14698. + u_int16_t udi_releaseNo;
  14699. + u_int8_t udi_class;
  14700. + u_int8_t udi_subclass;
  14701. + u_int8_t udi_protocol;
  14702. + u_int8_t udi_config;
  14703. + u_int8_t udi_speed;
  14704. +#define USB_SPEED_UNKNOWN 0
  14705. +#define USB_SPEED_LOW 1
  14706. +#define USB_SPEED_FULL 2
  14707. +#define USB_SPEED_HIGH 3
  14708. +#define USB_SPEED_VARIABLE 4
  14709. +#define USB_SPEED_SUPER 5
  14710. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  14711. + int udi_nports;
  14712. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  14713. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  14714. +#define USB_PORT_ENABLED 0xff
  14715. +#define USB_PORT_SUSPENDED 0xfe
  14716. +#define USB_PORT_POWERED 0xfd
  14717. +#define USB_PORT_DISABLED 0xfc
  14718. +};
  14719. +
  14720. +struct usb_ctl_report {
  14721. + int ucr_report;
  14722. + u_char ucr_data[1024]; /* filled data size will vary */
  14723. +};
  14724. +
  14725. +struct usb_device_stats {
  14726. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  14727. +};
  14728. +
  14729. +#define WUSB_MIN_IE 0x80
  14730. +#define WUSB_WCTA_IE 0x80
  14731. +#define WUSB_WCONNECTACK_IE 0x81
  14732. +#define WUSB_WHOSTINFO_IE 0x82
  14733. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  14734. +#define WUHI_CA_RECONN 0x00
  14735. +#define WUHI_CA_LIMITED 0x01
  14736. +#define WUHI_CA_ALL 0x03
  14737. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  14738. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  14739. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  14740. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  14741. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  14742. +#define WUSB_WWORK_IE 0x87
  14743. +#define WUSB_WCHANNEL_STOP_IE 0x88
  14744. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  14745. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  14746. +#define WUSB_WRESETDEVICE_IE 0x8B
  14747. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  14748. +#define WUSB_MAX_IE 0x8C
  14749. +
  14750. +/* Device Notification Types */
  14751. +
  14752. +#define WUSB_DN_MIN 0x01
  14753. +#define WUSB_DN_CONNECT 0x01
  14754. +# define WUSB_DA_OLDCONN 0x00
  14755. +# define WUSB_DA_NEWCONN 0x01
  14756. +# define WUSB_DA_SELF_BEACON 0x02
  14757. +# define WUSB_DA_DIR_BEACON 0x04
  14758. +# define WUSB_DA_NO_BEACON 0x06
  14759. +#define WUSB_DN_DISCONNECT 0x02
  14760. +#define WUSB_DN_EPRDY 0x03
  14761. +#define WUSB_DN_MASAVAILCHANGED 0x04
  14762. +#define WUSB_DN_REMOTEWAKEUP 0x05
  14763. +#define WUSB_DN_SLEEP 0x06
  14764. +#define WUSB_DN_ALIVE 0x07
  14765. +#define WUSB_DN_MAX 0x07
  14766. +
  14767. +#ifdef _MSC_VER
  14768. +#include <pshpack1.h>
  14769. +#endif
  14770. +
  14771. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  14772. +typedef struct wusb_hndshk_data {
  14773. + uByte bMessageNumber;
  14774. + uByte bStatus;
  14775. + uByte tTKID[3];
  14776. + uByte bReserved;
  14777. + uByte CDID[16];
  14778. + uByte Nonce[16];
  14779. + uByte MIC[8];
  14780. +} UPACKED wusb_hndshk_data_t;
  14781. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  14782. +
  14783. +/* WUSB Connection Context */
  14784. +typedef struct wusb_conn_context {
  14785. + uByte CHID [16];
  14786. + uByte CDID [16];
  14787. + uByte CK [16];
  14788. +} UPACKED wusb_conn_context_t;
  14789. +
  14790. +/* WUSB Security Descriptor */
  14791. +typedef struct wusb_security_desc {
  14792. + uByte bLength;
  14793. + uByte bDescriptorType;
  14794. + uWord wTotalLength;
  14795. + uByte bNumEncryptionTypes;
  14796. +} UPACKED wusb_security_desc_t;
  14797. +
  14798. +/* WUSB Encryption Type Descriptor */
  14799. +typedef struct wusb_encrypt_type_desc {
  14800. + uByte bLength;
  14801. + uByte bDescriptorType;
  14802. +
  14803. + uByte bEncryptionType;
  14804. +#define WUETD_UNSECURE 0
  14805. +#define WUETD_WIRED 1
  14806. +#define WUETD_CCM_1 2
  14807. +#define WUETD_RSA_1 3
  14808. +
  14809. + uByte bEncryptionValue;
  14810. + uByte bAuthKeyIndex;
  14811. +} UPACKED wusb_encrypt_type_desc_t;
  14812. +
  14813. +/* WUSB Key Descriptor */
  14814. +typedef struct wusb_key_desc {
  14815. + uByte bLength;
  14816. + uByte bDescriptorType;
  14817. + uByte tTKID[3];
  14818. + uByte bReserved;
  14819. + uByte KeyData[1]; /* variable length */
  14820. +} UPACKED wusb_key_desc_t;
  14821. +
  14822. +/* WUSB BOS Descriptor (Binary device Object Store) */
  14823. +typedef struct wusb_bos_desc {
  14824. + uByte bLength;
  14825. + uByte bDescriptorType;
  14826. + uWord wTotalLength;
  14827. + uByte bNumDeviceCaps;
  14828. +} UPACKED wusb_bos_desc_t;
  14829. +
  14830. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  14831. +typedef struct usb_dev_cap_20_ext_desc {
  14832. + uByte bLength;
  14833. + uByte bDescriptorType;
  14834. + uByte bDevCapabilityType;
  14835. +#define USB_20_EXT_LPM 0x02
  14836. + uDWord bmAttributes;
  14837. +} UPACKED usb_dev_cap_20_ext_desc_t;
  14838. +
  14839. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  14840. +typedef struct usb_dev_cap_ss_usb {
  14841. + uByte bLength;
  14842. + uByte bDescriptorType;
  14843. + uByte bDevCapabilityType;
  14844. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  14845. + uByte bmAttributes;
  14846. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  14847. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  14848. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  14849. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  14850. + uWord wSpeedsSupported;
  14851. + uByte bFunctionalitySupport;
  14852. + uByte bU1DevExitLat;
  14853. + uWord wU2DevExitLat;
  14854. +} UPACKED usb_dev_cap_ss_usb_t;
  14855. +
  14856. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  14857. +typedef struct usb_dev_cap_container_id {
  14858. + uByte bLength;
  14859. + uByte bDescriptorType;
  14860. + uByte bDevCapabilityType;
  14861. + uByte bReserved;
  14862. + uByte containerID[16];
  14863. +} UPACKED usb_dev_cap_container_id_t;
  14864. +
  14865. +/* Device Capability Type Codes */
  14866. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  14867. +
  14868. +/* Device Capability Descriptor */
  14869. +typedef struct wusb_dev_cap_desc {
  14870. + uByte bLength;
  14871. + uByte bDescriptorType;
  14872. + uByte bDevCapabilityType;
  14873. + uByte caps[1]; /* Variable length */
  14874. +} UPACKED wusb_dev_cap_desc_t;
  14875. +
  14876. +/* Device Capability Descriptor */
  14877. +typedef struct wusb_dev_cap_uwb_desc {
  14878. + uByte bLength;
  14879. + uByte bDescriptorType;
  14880. + uByte bDevCapabilityType;
  14881. + uByte bmAttributes;
  14882. + uWord wPHYRates; /* Bitmap */
  14883. + uByte bmTFITXPowerInfo;
  14884. + uByte bmFFITXPowerInfo;
  14885. + uWord bmBandGroup;
  14886. + uByte bReserved;
  14887. +} UPACKED wusb_dev_cap_uwb_desc_t;
  14888. +
  14889. +/* Wireless USB Endpoint Companion Descriptor */
  14890. +typedef struct wusb_endpoint_companion_desc {
  14891. + uByte bLength;
  14892. + uByte bDescriptorType;
  14893. + uByte bMaxBurst;
  14894. + uByte bMaxSequence;
  14895. + uWord wMaxStreamDelay;
  14896. + uWord wOverTheAirPacketSize;
  14897. + uByte bOverTheAirInterval;
  14898. + uByte bmCompAttributes;
  14899. +} UPACKED wusb_endpoint_companion_desc_t;
  14900. +
  14901. +/* Wireless USB Numeric Association M1 Data Structure */
  14902. +typedef struct wusb_m1_data {
  14903. + uByte version;
  14904. + uWord langId;
  14905. + uByte deviceFriendlyNameLength;
  14906. + uByte sha_256_m3[32];
  14907. + uByte deviceFriendlyName[256];
  14908. +} UPACKED wusb_m1_data_t;
  14909. +
  14910. +typedef struct wusb_m2_data {
  14911. + uByte version;
  14912. + uWord langId;
  14913. + uByte hostFriendlyNameLength;
  14914. + uByte pkh[384];
  14915. + uByte hostFriendlyName[256];
  14916. +} UPACKED wusb_m2_data_t;
  14917. +
  14918. +typedef struct wusb_m3_data {
  14919. + uByte pkd[384];
  14920. + uByte nd;
  14921. +} UPACKED wusb_m3_data_t;
  14922. +
  14923. +typedef struct wusb_m4_data {
  14924. + uDWord _attributeTypeIdAndLength_1;
  14925. + uWord associationTypeId;
  14926. +
  14927. + uDWord _attributeTypeIdAndLength_2;
  14928. + uWord associationSubTypeId;
  14929. +
  14930. + uDWord _attributeTypeIdAndLength_3;
  14931. + uDWord length;
  14932. +
  14933. + uDWord _attributeTypeIdAndLength_4;
  14934. + uDWord associationStatus;
  14935. +
  14936. + uDWord _attributeTypeIdAndLength_5;
  14937. + uByte chid[16];
  14938. +
  14939. + uDWord _attributeTypeIdAndLength_6;
  14940. + uByte cdid[16];
  14941. +
  14942. + uDWord _attributeTypeIdAndLength_7;
  14943. + uByte bandGroups[2];
  14944. +} UPACKED wusb_m4_data_t;
  14945. +
  14946. +#ifdef _MSC_VER
  14947. +#include <poppack.h>
  14948. +#endif
  14949. +
  14950. +#ifdef __cplusplus
  14951. +}
  14952. +#endif
  14953. +
  14954. +#endif /* _USB_H_ */
  14955. --- /dev/null
  14956. +++ b/drivers/usb/host/dwc_otg/Makefile
  14957. @@ -0,0 +1,82 @@
  14958. +#
  14959. +# Makefile for DWC_otg Highspeed USB controller driver
  14960. +#
  14961. +
  14962. +ifneq ($(KERNELRELEASE),)
  14963. +
  14964. +# Use the BUS_INTERFACE variable to compile the software for either
  14965. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  14966. +ifeq ($(BUS_INTERFACE),)
  14967. +# BUS_INTERFACE = -DPCI_INTERFACE
  14968. +# BUS_INTERFACE = -DLM_INTERFACE
  14969. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  14970. +endif
  14971. +
  14972. +#ccflags-y += -DDEBUG
  14973. +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  14974. +
  14975. +# Use one of the following flags to compile the software in host-only or
  14976. +# device-only mode.
  14977. +#ccflags-y += -DDWC_HOST_ONLY
  14978. +#ccflags-y += -DDWC_DEVICE_ONLY
  14979. +
  14980. +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
  14981. +#ccflags-y += -DDWC_EN_ISOC
  14982. +ccflags-y += -I$(obj)/../dwc_common_port
  14983. +#ccflags-y += -I$(PORTLIB)
  14984. +ccflags-y += -DDWC_LINUX
  14985. +ccflags-y += $(CFI)
  14986. +ccflags-y += $(BUS_INTERFACE)
  14987. +#ccflags-y += -DDWC_DEV_SRPCAP
  14988. +
  14989. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  14990. +
  14991. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  14992. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  14993. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  14994. +dwc_otg-objs += dwc_otg_hcd.o dwc_otg_hcd_linux.o dwc_otg_hcd_intr.o dwc_otg_hcd_queue.o dwc_otg_hcd_ddma.o
  14995. +dwc_otg-objs += dwc_otg_adp.o
  14996. +dwc_otg-objs += dwc_otg_fiq_fsm.o
  14997. +dwc_otg-objs += dwc_otg_fiq_stub.o
  14998. +ifneq ($(CFI),)
  14999. +dwc_otg-objs += dwc_otg_cfi.o
  15000. +endif
  15001. +
  15002. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  15003. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  15004. +
  15005. +ifneq ($(kernrel3),2.6.20)
  15006. +ccflags-y += $(CPPFLAGS)
  15007. +endif
  15008. +
  15009. +else
  15010. +
  15011. +PWD := $(shell pwd)
  15012. +PORTLIB := $(PWD)/../dwc_common_port
  15013. +
  15014. +# Command paths
  15015. +CTAGS := $(CTAGS)
  15016. +DOXYGEN := $(DOXYGEN)
  15017. +
  15018. +default: portlib
  15019. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  15020. +
  15021. +install: default
  15022. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  15023. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  15024. +
  15025. +portlib:
  15026. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  15027. + cp $(PORTLIB)/Module.symvers $(PWD)/
  15028. +
  15029. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  15030. + $(DOXYGEN) doc/doxygen.cfg
  15031. +
  15032. +tags: $(wildcard *.[hc])
  15033. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  15034. +
  15035. +
  15036. +clean:
  15037. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  15038. +
  15039. +endif
  15040. --- /dev/null
  15041. +++ b/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  15042. @@ -0,0 +1,224 @@
  15043. +# Doxyfile 1.3.9.1
  15044. +
  15045. +#---------------------------------------------------------------------------
  15046. +# Project related configuration options
  15047. +#---------------------------------------------------------------------------
  15048. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  15049. +PROJECT_NUMBER = v3.00a
  15050. +OUTPUT_DIRECTORY = ./doc/
  15051. +CREATE_SUBDIRS = NO
  15052. +OUTPUT_LANGUAGE = English
  15053. +BRIEF_MEMBER_DESC = YES
  15054. +REPEAT_BRIEF = YES
  15055. +ABBREVIATE_BRIEF = "The $name class" \
  15056. + "The $name widget" \
  15057. + "The $name file" \
  15058. + is \
  15059. + provides \
  15060. + specifies \
  15061. + contains \
  15062. + represents \
  15063. + a \
  15064. + an \
  15065. + the
  15066. +ALWAYS_DETAILED_SEC = NO
  15067. +INLINE_INHERITED_MEMB = NO
  15068. +FULL_PATH_NAMES = NO
  15069. +STRIP_FROM_PATH =
  15070. +STRIP_FROM_INC_PATH =
  15071. +SHORT_NAMES = NO
  15072. +JAVADOC_AUTOBRIEF = YES
  15073. +MULTILINE_CPP_IS_BRIEF = NO
  15074. +INHERIT_DOCS = YES
  15075. +DISTRIBUTE_GROUP_DOC = NO
  15076. +TAB_SIZE = 8
  15077. +ALIASES =
  15078. +OPTIMIZE_OUTPUT_FOR_C = YES
  15079. +OPTIMIZE_OUTPUT_JAVA = NO
  15080. +SUBGROUPING = YES
  15081. +#---------------------------------------------------------------------------
  15082. +# Build related configuration options
  15083. +#---------------------------------------------------------------------------
  15084. +EXTRACT_ALL = NO
  15085. +EXTRACT_PRIVATE = YES
  15086. +EXTRACT_STATIC = YES
  15087. +EXTRACT_LOCAL_CLASSES = YES
  15088. +EXTRACT_LOCAL_METHODS = NO
  15089. +HIDE_UNDOC_MEMBERS = NO
  15090. +HIDE_UNDOC_CLASSES = NO
  15091. +HIDE_FRIEND_COMPOUNDS = NO
  15092. +HIDE_IN_BODY_DOCS = NO
  15093. +INTERNAL_DOCS = NO
  15094. +CASE_SENSE_NAMES = NO
  15095. +HIDE_SCOPE_NAMES = NO
  15096. +SHOW_INCLUDE_FILES = YES
  15097. +INLINE_INFO = YES
  15098. +SORT_MEMBER_DOCS = NO
  15099. +SORT_BRIEF_DOCS = NO
  15100. +SORT_BY_SCOPE_NAME = NO
  15101. +GENERATE_TODOLIST = YES
  15102. +GENERATE_TESTLIST = YES
  15103. +GENERATE_BUGLIST = YES
  15104. +GENERATE_DEPRECATEDLIST= YES
  15105. +ENABLED_SECTIONS =
  15106. +MAX_INITIALIZER_LINES = 30
  15107. +SHOW_USED_FILES = YES
  15108. +SHOW_DIRECTORIES = YES
  15109. +#---------------------------------------------------------------------------
  15110. +# configuration options related to warning and progress messages
  15111. +#---------------------------------------------------------------------------
  15112. +QUIET = YES
  15113. +WARNINGS = YES
  15114. +WARN_IF_UNDOCUMENTED = NO
  15115. +WARN_IF_DOC_ERROR = YES
  15116. +WARN_FORMAT = "$file:$line: $text"
  15117. +WARN_LOGFILE =
  15118. +#---------------------------------------------------------------------------
  15119. +# configuration options related to the input files
  15120. +#---------------------------------------------------------------------------
  15121. +INPUT = .
  15122. +FILE_PATTERNS = *.c \
  15123. + *.h \
  15124. + ./linux/*.c \
  15125. + ./linux/*.h
  15126. +RECURSIVE = NO
  15127. +EXCLUDE = ./test/ \
  15128. + ./dwc_otg/.AppleDouble/
  15129. +EXCLUDE_SYMLINKS = YES
  15130. +EXCLUDE_PATTERNS = *.mod.*
  15131. +EXAMPLE_PATH =
  15132. +EXAMPLE_PATTERNS = *
  15133. +EXAMPLE_RECURSIVE = NO
  15134. +IMAGE_PATH =
  15135. +INPUT_FILTER =
  15136. +FILTER_PATTERNS =
  15137. +FILTER_SOURCE_FILES = NO
  15138. +#---------------------------------------------------------------------------
  15139. +# configuration options related to source browsing
  15140. +#---------------------------------------------------------------------------
  15141. +SOURCE_BROWSER = YES
  15142. +INLINE_SOURCES = NO
  15143. +STRIP_CODE_COMMENTS = YES
  15144. +REFERENCED_BY_RELATION = NO
  15145. +REFERENCES_RELATION = NO
  15146. +VERBATIM_HEADERS = NO
  15147. +#---------------------------------------------------------------------------
  15148. +# configuration options related to the alphabetical class index
  15149. +#---------------------------------------------------------------------------
  15150. +ALPHABETICAL_INDEX = NO
  15151. +COLS_IN_ALPHA_INDEX = 5
  15152. +IGNORE_PREFIX =
  15153. +#---------------------------------------------------------------------------
  15154. +# configuration options related to the HTML output
  15155. +#---------------------------------------------------------------------------
  15156. +GENERATE_HTML = YES
  15157. +HTML_OUTPUT = html
  15158. +HTML_FILE_EXTENSION = .html
  15159. +HTML_HEADER =
  15160. +HTML_FOOTER =
  15161. +HTML_STYLESHEET =
  15162. +HTML_ALIGN_MEMBERS = YES
  15163. +GENERATE_HTMLHELP = NO
  15164. +CHM_FILE =
  15165. +HHC_LOCATION =
  15166. +GENERATE_CHI = NO
  15167. +BINARY_TOC = NO
  15168. +TOC_EXPAND = NO
  15169. +DISABLE_INDEX = NO
  15170. +ENUM_VALUES_PER_LINE = 4
  15171. +GENERATE_TREEVIEW = YES
  15172. +TREEVIEW_WIDTH = 250
  15173. +#---------------------------------------------------------------------------
  15174. +# configuration options related to the LaTeX output
  15175. +#---------------------------------------------------------------------------
  15176. +GENERATE_LATEX = NO
  15177. +LATEX_OUTPUT = latex
  15178. +LATEX_CMD_NAME = latex
  15179. +MAKEINDEX_CMD_NAME = makeindex
  15180. +COMPACT_LATEX = NO
  15181. +PAPER_TYPE = a4wide
  15182. +EXTRA_PACKAGES =
  15183. +LATEX_HEADER =
  15184. +PDF_HYPERLINKS = NO
  15185. +USE_PDFLATEX = NO
  15186. +LATEX_BATCHMODE = NO
  15187. +LATEX_HIDE_INDICES = NO
  15188. +#---------------------------------------------------------------------------
  15189. +# configuration options related to the RTF output
  15190. +#---------------------------------------------------------------------------
  15191. +GENERATE_RTF = NO
  15192. +RTF_OUTPUT = rtf
  15193. +COMPACT_RTF = NO
  15194. +RTF_HYPERLINKS = NO
  15195. +RTF_STYLESHEET_FILE =
  15196. +RTF_EXTENSIONS_FILE =
  15197. +#---------------------------------------------------------------------------
  15198. +# configuration options related to the man page output
  15199. +#---------------------------------------------------------------------------
  15200. +GENERATE_MAN = NO
  15201. +MAN_OUTPUT = man
  15202. +MAN_EXTENSION = .3
  15203. +MAN_LINKS = NO
  15204. +#---------------------------------------------------------------------------
  15205. +# configuration options related to the XML output
  15206. +#---------------------------------------------------------------------------
  15207. +GENERATE_XML = NO
  15208. +XML_OUTPUT = xml
  15209. +XML_SCHEMA =
  15210. +XML_DTD =
  15211. +XML_PROGRAMLISTING = YES
  15212. +#---------------------------------------------------------------------------
  15213. +# configuration options for the AutoGen Definitions output
  15214. +#---------------------------------------------------------------------------
  15215. +GENERATE_AUTOGEN_DEF = NO
  15216. +#---------------------------------------------------------------------------
  15217. +# configuration options related to the Perl module output
  15218. +#---------------------------------------------------------------------------
  15219. +GENERATE_PERLMOD = NO
  15220. +PERLMOD_LATEX = NO
  15221. +PERLMOD_PRETTY = YES
  15222. +PERLMOD_MAKEVAR_PREFIX =
  15223. +#---------------------------------------------------------------------------
  15224. +# Configuration options related to the preprocessor
  15225. +#---------------------------------------------------------------------------
  15226. +ENABLE_PREPROCESSING = YES
  15227. +MACRO_EXPANSION = YES
  15228. +EXPAND_ONLY_PREDEF = YES
  15229. +SEARCH_INCLUDES = YES
  15230. +INCLUDE_PATH =
  15231. +INCLUDE_FILE_PATTERNS =
  15232. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  15233. +EXPAND_AS_DEFINED = DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW DWC_OTG_DEVICE_ATTR_BITFIELD_STORE DWC_OTG_DEVICE_ATTR_BITFIELD_RW DWC_OTG_DEVICE_ATTR_BITFIELD_RO DWC_OTG_DEVICE_ATTR_REG_SHOW DWC_OTG_DEVICE_ATTR_REG_STORE DWC_OTG_DEVICE_ATTR_REG32_RW DWC_OTG_DEVICE_ATTR_REG32_RO DWC_EN_ISOC
  15234. +SKIP_FUNCTION_MACROS = NO
  15235. +#---------------------------------------------------------------------------
  15236. +# Configuration::additions related to external references
  15237. +#---------------------------------------------------------------------------
  15238. +TAGFILES =
  15239. +GENERATE_TAGFILE =
  15240. +ALLEXTERNALS = NO
  15241. +EXTERNAL_GROUPS = YES
  15242. +PERL_PATH = /usr/bin/perl
  15243. +#---------------------------------------------------------------------------
  15244. +# Configuration options related to the dot tool
  15245. +#---------------------------------------------------------------------------
  15246. +CLASS_DIAGRAMS = YES
  15247. +HIDE_UNDOC_RELATIONS = YES
  15248. +HAVE_DOT = NO
  15249. +CLASS_GRAPH = YES
  15250. +COLLABORATION_GRAPH = YES
  15251. +UML_LOOK = NO
  15252. +TEMPLATE_RELATIONS = NO
  15253. +INCLUDE_GRAPH = YES
  15254. +INCLUDED_BY_GRAPH = YES
  15255. +CALL_GRAPH = NO
  15256. +GRAPHICAL_HIERARCHY = YES
  15257. +DOT_IMAGE_FORMAT = png
  15258. +DOT_PATH =
  15259. +DOTFILE_DIRS =
  15260. +MAX_DOT_GRAPH_DEPTH = 1000
  15261. +GENERATE_LEGEND = YES
  15262. +DOT_CLEANUP = YES
  15263. +#---------------------------------------------------------------------------
  15264. +# Configuration::additions related to the search engine
  15265. +#---------------------------------------------------------------------------
  15266. +SEARCHENGINE = NO
  15267. --- /dev/null
  15268. +++ b/drivers/usb/host/dwc_otg/dummy_audio.c
  15269. @@ -0,0 +1,1574 @@
  15270. +/*
  15271. + * zero.c -- Gadget Zero, for USB development
  15272. + *
  15273. + * Copyright (C) 2003-2004 David Brownell
  15274. + * All rights reserved.
  15275. + *
  15276. + * Redistribution and use in source and binary forms, with or without
  15277. + * modification, are permitted provided that the following conditions
  15278. + * are met:
  15279. + * 1. Redistributions of source code must retain the above copyright
  15280. + * notice, this list of conditions, and the following disclaimer,
  15281. + * without modification.
  15282. + * 2. Redistributions in binary form must reproduce the above copyright
  15283. + * notice, this list of conditions and the following disclaimer in the
  15284. + * documentation and/or other materials provided with the distribution.
  15285. + * 3. The names of the above-listed copyright holders may not be used
  15286. + * to endorse or promote products derived from this software without
  15287. + * specific prior written permission.
  15288. + *
  15289. + * ALTERNATIVELY, this software may be distributed under the terms of the
  15290. + * GNU General Public License ("GPL") as published by the Free Software
  15291. + * Foundation, either version 2 of that License or (at your option) any
  15292. + * later version.
  15293. + *
  15294. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  15295. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15296. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  15297. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  15298. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  15299. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  15300. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15301. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  15302. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  15303. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  15304. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15305. + */
  15306. +
  15307. +
  15308. +/*
  15309. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  15310. + * can write a hardware-agnostic gadget driver running inside a USB device.
  15311. + *
  15312. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  15313. + * affect most of the driver.
  15314. + *
  15315. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  15316. + * functional test of your device-side usb stack, or with "usb-skeleton".
  15317. + *
  15318. + * It supports two similar configurations. One sinks whatever the usb host
  15319. + * writes, and in return sources zeroes. The other loops whatever the host
  15320. + * writes back, so the host can read it. Module options include:
  15321. + *
  15322. + * buflen=N default N=4096, buffer size used
  15323. + * qlen=N default N=32, how many buffers in the loopback queue
  15324. + * loopdefault default false, list loopback config first
  15325. + *
  15326. + * Many drivers will only have one configuration, letting them be much
  15327. + * simpler if they also don't support high speed operation (like this
  15328. + * driver does).
  15329. + */
  15330. +
  15331. +#include <linux/config.h>
  15332. +#include <linux/module.h>
  15333. +#include <linux/kernel.h>
  15334. +#include <linux/delay.h>
  15335. +#include <linux/ioport.h>
  15336. +#include <linux/sched.h>
  15337. +#include <linux/slab.h>
  15338. +#include <linux/smp_lock.h>
  15339. +#include <linux/errno.h>
  15340. +#include <linux/init.h>
  15341. +#include <linux/timer.h>
  15342. +#include <linux/list.h>
  15343. +#include <linux/interrupt.h>
  15344. +#include <linux/uts.h>
  15345. +#include <linux/version.h>
  15346. +#include <linux/device.h>
  15347. +#include <linux/moduleparam.h>
  15348. +#include <linux/proc_fs.h>
  15349. +
  15350. +#include <asm/byteorder.h>
  15351. +#include <asm/io.h>
  15352. +#include <asm/irq.h>
  15353. +#include <asm/system.h>
  15354. +#include <asm/unaligned.h>
  15355. +
  15356. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  15357. +# include <linux/usb/ch9.h>
  15358. +#else
  15359. +# include <linux/usb_ch9.h>
  15360. +#endif
  15361. +
  15362. +#include <linux/usb_gadget.h>
  15363. +
  15364. +
  15365. +/*-------------------------------------------------------------------------*/
  15366. +/*-------------------------------------------------------------------------*/
  15367. +
  15368. +
  15369. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  15370. +{
  15371. + int count = 0;
  15372. + u8 c;
  15373. + u16 uchar;
  15374. +
  15375. + /* this insists on correct encodings, though not minimal ones.
  15376. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  15377. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  15378. + */
  15379. + while (len != 0 && (c = (u8) *s++) != 0) {
  15380. + if (unlikely(c & 0x80)) {
  15381. + // 2-byte sequence:
  15382. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  15383. + if ((c & 0xe0) == 0xc0) {
  15384. + uchar = (c & 0x1f) << 6;
  15385. +
  15386. + c = (u8) *s++;
  15387. + if ((c & 0xc0) != 0xc0)
  15388. + goto fail;
  15389. + c &= 0x3f;
  15390. + uchar |= c;
  15391. +
  15392. + // 3-byte sequence (most CJKV characters):
  15393. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  15394. + } else if ((c & 0xf0) == 0xe0) {
  15395. + uchar = (c & 0x0f) << 12;
  15396. +
  15397. + c = (u8) *s++;
  15398. + if ((c & 0xc0) != 0xc0)
  15399. + goto fail;
  15400. + c &= 0x3f;
  15401. + uchar |= c << 6;
  15402. +
  15403. + c = (u8) *s++;
  15404. + if ((c & 0xc0) != 0xc0)
  15405. + goto fail;
  15406. + c &= 0x3f;
  15407. + uchar |= c;
  15408. +
  15409. + /* no bogus surrogates */
  15410. + if (0xd800 <= uchar && uchar <= 0xdfff)
  15411. + goto fail;
  15412. +
  15413. + // 4-byte sequence (surrogate pairs, currently rare):
  15414. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  15415. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  15416. + // (uuuuu = wwww + 1)
  15417. + // FIXME accept the surrogate code points (only)
  15418. +
  15419. + } else
  15420. + goto fail;
  15421. + } else
  15422. + uchar = c;
  15423. + put_unaligned (cpu_to_le16 (uchar), cp++);
  15424. + count++;
  15425. + len--;
  15426. + }
  15427. + return count;
  15428. +fail:
  15429. + return -1;
  15430. +}
  15431. +
  15432. +
  15433. +/**
  15434. + * usb_gadget_get_string - fill out a string descriptor
  15435. + * @table: of c strings encoded using UTF-8
  15436. + * @id: string id, from low byte of wValue in get string descriptor
  15437. + * @buf: at least 256 bytes
  15438. + *
  15439. + * Finds the UTF-8 string matching the ID, and converts it into a
  15440. + * string descriptor in utf16-le.
  15441. + * Returns length of descriptor (always even) or negative errno
  15442. + *
  15443. + * If your driver needs stings in multiple languages, you'll probably
  15444. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  15445. + * using this routine after choosing which set of UTF-8 strings to use.
  15446. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  15447. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  15448. + * characters (which are also widely used in C strings).
  15449. + */
  15450. +int
  15451. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  15452. +{
  15453. + struct usb_string *s;
  15454. + int len;
  15455. +
  15456. + /* descriptor 0 has the language id */
  15457. + if (id == 0) {
  15458. + buf [0] = 4;
  15459. + buf [1] = USB_DT_STRING;
  15460. + buf [2] = (u8) table->language;
  15461. + buf [3] = (u8) (table->language >> 8);
  15462. + return 4;
  15463. + }
  15464. + for (s = table->strings; s && s->s; s++)
  15465. + if (s->id == id)
  15466. + break;
  15467. +
  15468. + /* unrecognized: stall. */
  15469. + if (!s || !s->s)
  15470. + return -EINVAL;
  15471. +
  15472. + /* string descriptors have length, tag, then UTF16-LE text */
  15473. + len = min ((size_t) 126, strlen (s->s));
  15474. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  15475. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  15476. + if (len < 0)
  15477. + return -EINVAL;
  15478. + buf [0] = (len + 1) * 2;
  15479. + buf [1] = USB_DT_STRING;
  15480. + return buf [0];
  15481. +}
  15482. +
  15483. +
  15484. +/*-------------------------------------------------------------------------*/
  15485. +/*-------------------------------------------------------------------------*/
  15486. +
  15487. +
  15488. +/**
  15489. + * usb_descriptor_fillbuf - fill buffer with descriptors
  15490. + * @buf: Buffer to be filled
  15491. + * @buflen: Size of buf
  15492. + * @src: Array of descriptor pointers, terminated by null pointer.
  15493. + *
  15494. + * Copies descriptors into the buffer, returning the length or a
  15495. + * negative error code if they can't all be copied. Useful when
  15496. + * assembling descriptors for an associated set of interfaces used
  15497. + * as part of configuring a composite device; or in other cases where
  15498. + * sets of descriptors need to be marshaled.
  15499. + */
  15500. +int
  15501. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  15502. + const struct usb_descriptor_header **src)
  15503. +{
  15504. + u8 *dest = buf;
  15505. +
  15506. + if (!src)
  15507. + return -EINVAL;
  15508. +
  15509. + /* fill buffer from src[] until null descriptor ptr */
  15510. + for (; 0 != *src; src++) {
  15511. + unsigned len = (*src)->bLength;
  15512. +
  15513. + if (len > buflen)
  15514. + return -EINVAL;
  15515. + memcpy(dest, *src, len);
  15516. + buflen -= len;
  15517. + dest += len;
  15518. + }
  15519. + return dest - (u8 *)buf;
  15520. +}
  15521. +
  15522. +
  15523. +/**
  15524. + * usb_gadget_config_buf - builts a complete configuration descriptor
  15525. + * @config: Header for the descriptor, including characteristics such
  15526. + * as power requirements and number of interfaces.
  15527. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  15528. + * endpoint, etc) defining all functions in this device configuration.
  15529. + * @buf: Buffer for the resulting configuration descriptor.
  15530. + * @length: Length of buffer. If this is not big enough to hold the
  15531. + * entire configuration descriptor, an error code will be returned.
  15532. + *
  15533. + * This copies descriptors into the response buffer, building a descriptor
  15534. + * for that configuration. It returns the buffer length or a negative
  15535. + * status code. The config.wTotalLength field is set to match the length
  15536. + * of the result, but other descriptor fields (including power usage and
  15537. + * interface count) must be set by the caller.
  15538. + *
  15539. + * Gadget drivers could use this when constructing a config descriptor
  15540. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  15541. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  15542. + */
  15543. +int usb_gadget_config_buf(
  15544. + const struct usb_config_descriptor *config,
  15545. + void *buf,
  15546. + unsigned length,
  15547. + const struct usb_descriptor_header **desc
  15548. +)
  15549. +{
  15550. + struct usb_config_descriptor *cp = buf;
  15551. + int len;
  15552. +
  15553. + /* config descriptor first */
  15554. + if (length < USB_DT_CONFIG_SIZE || !desc)
  15555. + return -EINVAL;
  15556. + *cp = *config;
  15557. +
  15558. + /* then interface/endpoint/class/vendor/... */
  15559. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  15560. + length - USB_DT_CONFIG_SIZE, desc);
  15561. + if (len < 0)
  15562. + return len;
  15563. + len += USB_DT_CONFIG_SIZE;
  15564. + if (len > 0xffff)
  15565. + return -EINVAL;
  15566. +
  15567. + /* patch up the config descriptor */
  15568. + cp->bLength = USB_DT_CONFIG_SIZE;
  15569. + cp->bDescriptorType = USB_DT_CONFIG;
  15570. + cp->wTotalLength = cpu_to_le16(len);
  15571. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  15572. + return len;
  15573. +}
  15574. +
  15575. +/*-------------------------------------------------------------------------*/
  15576. +/*-------------------------------------------------------------------------*/
  15577. +
  15578. +
  15579. +#define RBUF_LEN (1024*1024)
  15580. +static int rbuf_start;
  15581. +static int rbuf_len;
  15582. +static __u8 rbuf[RBUF_LEN];
  15583. +
  15584. +/*-------------------------------------------------------------------------*/
  15585. +
  15586. +#define DRIVER_VERSION "St Patrick's Day 2004"
  15587. +
  15588. +static const char shortname [] = "zero";
  15589. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  15590. +
  15591. +static const char source_sink [] = "source and sink data";
  15592. +static const char loopback [] = "loop input to output";
  15593. +
  15594. +/*-------------------------------------------------------------------------*/
  15595. +
  15596. +/*
  15597. + * driver assumes self-powered hardware, and
  15598. + * has no way for users to trigger remote wakeup.
  15599. + *
  15600. + * this version autoconfigures as much as possible,
  15601. + * which is reasonable for most "bulk-only" drivers.
  15602. + */
  15603. +static const char *EP_IN_NAME; /* source */
  15604. +static const char *EP_OUT_NAME; /* sink */
  15605. +
  15606. +/*-------------------------------------------------------------------------*/
  15607. +
  15608. +/* big enough to hold our biggest descriptor */
  15609. +#define USB_BUFSIZ 512
  15610. +
  15611. +struct zero_dev {
  15612. + spinlock_t lock;
  15613. + struct usb_gadget *gadget;
  15614. + struct usb_request *req; /* for control responses */
  15615. +
  15616. + /* when configured, we have one of two configs:
  15617. + * - source data (in to host) and sink it (out from host)
  15618. + * - or loop it back (out from host back in to host)
  15619. + */
  15620. + u8 config;
  15621. + struct usb_ep *in_ep, *out_ep;
  15622. +
  15623. + /* autoresume timer */
  15624. + struct timer_list resume;
  15625. +};
  15626. +
  15627. +#define xprintk(d,level,fmt,args...) \
  15628. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  15629. +
  15630. +#ifdef DEBUG
  15631. +#define DBG(dev,fmt,args...) \
  15632. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  15633. +#else
  15634. +#define DBG(dev,fmt,args...) \
  15635. + do { } while (0)
  15636. +#endif /* DEBUG */
  15637. +
  15638. +#ifdef VERBOSE
  15639. +#define VDBG DBG
  15640. +#else
  15641. +#define VDBG(dev,fmt,args...) \
  15642. + do { } while (0)
  15643. +#endif /* VERBOSE */
  15644. +
  15645. +#define ERROR(dev,fmt,args...) \
  15646. + xprintk(dev , KERN_ERR , fmt , ## args)
  15647. +#define WARN(dev,fmt,args...) \
  15648. + xprintk(dev , KERN_WARNING , fmt , ## args)
  15649. +#define INFO(dev,fmt,args...) \
  15650. + xprintk(dev , KERN_INFO , fmt , ## args)
  15651. +
  15652. +/*-------------------------------------------------------------------------*/
  15653. +
  15654. +static unsigned buflen = 4096;
  15655. +static unsigned qlen = 32;
  15656. +static unsigned pattern = 0;
  15657. +
  15658. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  15659. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  15660. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  15661. +
  15662. +/*
  15663. + * if it's nonzero, autoresume says how many seconds to wait
  15664. + * before trying to wake up the host after suspend.
  15665. + */
  15666. +static unsigned autoresume = 0;
  15667. +module_param (autoresume, uint, 0);
  15668. +
  15669. +/*
  15670. + * Normally the "loopback" configuration is second (index 1) so
  15671. + * it's not the default. Here's where to change that order, to
  15672. + * work better with hosts where config changes are problematic.
  15673. + * Or controllers (like superh) that only support one config.
  15674. + */
  15675. +static int loopdefault = 0;
  15676. +
  15677. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  15678. +
  15679. +/*-------------------------------------------------------------------------*/
  15680. +
  15681. +/* Thanks to NetChip Technologies for donating this product ID.
  15682. + *
  15683. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  15684. + * Instead: allocate your own, using normal USB-IF procedures.
  15685. + */
  15686. +#ifndef CONFIG_USB_ZERO_HNPTEST
  15687. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  15688. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  15689. +#else
  15690. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  15691. +#define DRIVER_PRODUCT_NUM 0xbadd
  15692. +#endif
  15693. +
  15694. +/*-------------------------------------------------------------------------*/
  15695. +
  15696. +/*
  15697. + * DESCRIPTORS ... most are static, but strings and (full)
  15698. + * configuration descriptors are built on demand.
  15699. + */
  15700. +
  15701. +/*
  15702. +#define STRING_MANUFACTURER 25
  15703. +#define STRING_PRODUCT 42
  15704. +#define STRING_SERIAL 101
  15705. +*/
  15706. +#define STRING_MANUFACTURER 1
  15707. +#define STRING_PRODUCT 2
  15708. +#define STRING_SERIAL 3
  15709. +
  15710. +#define STRING_SOURCE_SINK 250
  15711. +#define STRING_LOOPBACK 251
  15712. +
  15713. +/*
  15714. + * This device advertises two configurations; these numbers work
  15715. + * on a pxa250 as well as more flexible hardware.
  15716. + */
  15717. +#define CONFIG_SOURCE_SINK 3
  15718. +#define CONFIG_LOOPBACK 2
  15719. +
  15720. +/*
  15721. +static struct usb_device_descriptor
  15722. +device_desc = {
  15723. + .bLength = sizeof device_desc,
  15724. + .bDescriptorType = USB_DT_DEVICE,
  15725. +
  15726. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  15727. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  15728. +
  15729. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  15730. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  15731. + .iManufacturer = STRING_MANUFACTURER,
  15732. + .iProduct = STRING_PRODUCT,
  15733. + .iSerialNumber = STRING_SERIAL,
  15734. + .bNumConfigurations = 2,
  15735. +};
  15736. +*/
  15737. +static struct usb_device_descriptor
  15738. +device_desc = {
  15739. + .bLength = sizeof device_desc,
  15740. + .bDescriptorType = USB_DT_DEVICE,
  15741. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  15742. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  15743. + .bDeviceSubClass = 0,
  15744. + .bDeviceProtocol = 0,
  15745. + .bMaxPacketSize0 = 64,
  15746. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  15747. + .idVendor = __constant_cpu_to_le16 (0x0499),
  15748. + .idProduct = __constant_cpu_to_le16 (0x3002),
  15749. + .iManufacturer = STRING_MANUFACTURER,
  15750. + .iProduct = STRING_PRODUCT,
  15751. + .iSerialNumber = STRING_SERIAL,
  15752. + .bNumConfigurations = 1,
  15753. +};
  15754. +
  15755. +static struct usb_config_descriptor
  15756. +z_config = {
  15757. + .bLength = sizeof z_config,
  15758. + .bDescriptorType = USB_DT_CONFIG,
  15759. +
  15760. + /* compute wTotalLength on the fly */
  15761. + .bNumInterfaces = 2,
  15762. + .bConfigurationValue = 1,
  15763. + .iConfiguration = 0,
  15764. + .bmAttributes = 0x40,
  15765. + .bMaxPower = 0, /* self-powered */
  15766. +};
  15767. +
  15768. +
  15769. +static struct usb_otg_descriptor
  15770. +otg_descriptor = {
  15771. + .bLength = sizeof otg_descriptor,
  15772. + .bDescriptorType = USB_DT_OTG,
  15773. +
  15774. + .bmAttributes = USB_OTG_SRP,
  15775. +};
  15776. +
  15777. +/* one interface in each configuration */
  15778. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  15779. +
  15780. +/*
  15781. + * usb 2.0 devices need to expose both high speed and full speed
  15782. + * descriptors, unless they only run at full speed.
  15783. + *
  15784. + * that means alternate endpoint descriptors (bigger packets)
  15785. + * and a "device qualifier" ... plus more construction options
  15786. + * for the config descriptor.
  15787. + */
  15788. +
  15789. +static struct usb_qualifier_descriptor
  15790. +dev_qualifier = {
  15791. + .bLength = sizeof dev_qualifier,
  15792. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  15793. +
  15794. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  15795. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  15796. +
  15797. + .bNumConfigurations = 2,
  15798. +};
  15799. +
  15800. +
  15801. +struct usb_cs_as_general_descriptor {
  15802. + __u8 bLength;
  15803. + __u8 bDescriptorType;
  15804. +
  15805. + __u8 bDescriptorSubType;
  15806. + __u8 bTerminalLink;
  15807. + __u8 bDelay;
  15808. + __u16 wFormatTag;
  15809. +} __attribute__ ((packed));
  15810. +
  15811. +struct usb_cs_as_format_descriptor {
  15812. + __u8 bLength;
  15813. + __u8 bDescriptorType;
  15814. +
  15815. + __u8 bDescriptorSubType;
  15816. + __u8 bFormatType;
  15817. + __u8 bNrChannels;
  15818. + __u8 bSubframeSize;
  15819. + __u8 bBitResolution;
  15820. + __u8 bSamfreqType;
  15821. + __u8 tLowerSamFreq[3];
  15822. + __u8 tUpperSamFreq[3];
  15823. +} __attribute__ ((packed));
  15824. +
  15825. +static const struct usb_interface_descriptor
  15826. +z_audio_control_if_desc = {
  15827. + .bLength = sizeof z_audio_control_if_desc,
  15828. + .bDescriptorType = USB_DT_INTERFACE,
  15829. + .bInterfaceNumber = 0,
  15830. + .bAlternateSetting = 0,
  15831. + .bNumEndpoints = 0,
  15832. + .bInterfaceClass = USB_CLASS_AUDIO,
  15833. + .bInterfaceSubClass = 0x1,
  15834. + .bInterfaceProtocol = 0,
  15835. + .iInterface = 0,
  15836. +};
  15837. +
  15838. +static const struct usb_interface_descriptor
  15839. +z_audio_if_desc = {
  15840. + .bLength = sizeof z_audio_if_desc,
  15841. + .bDescriptorType = USB_DT_INTERFACE,
  15842. + .bInterfaceNumber = 1,
  15843. + .bAlternateSetting = 0,
  15844. + .bNumEndpoints = 0,
  15845. + .bInterfaceClass = USB_CLASS_AUDIO,
  15846. + .bInterfaceSubClass = 0x2,
  15847. + .bInterfaceProtocol = 0,
  15848. + .iInterface = 0,
  15849. +};
  15850. +
  15851. +static const struct usb_interface_descriptor
  15852. +z_audio_if_desc2 = {
  15853. + .bLength = sizeof z_audio_if_desc,
  15854. + .bDescriptorType = USB_DT_INTERFACE,
  15855. + .bInterfaceNumber = 1,
  15856. + .bAlternateSetting = 1,
  15857. + .bNumEndpoints = 1,
  15858. + .bInterfaceClass = USB_CLASS_AUDIO,
  15859. + .bInterfaceSubClass = 0x2,
  15860. + .bInterfaceProtocol = 0,
  15861. + .iInterface = 0,
  15862. +};
  15863. +
  15864. +static const struct usb_cs_as_general_descriptor
  15865. +z_audio_cs_as_if_desc = {
  15866. + .bLength = 7,
  15867. + .bDescriptorType = 0x24,
  15868. +
  15869. + .bDescriptorSubType = 0x01,
  15870. + .bTerminalLink = 0x01,
  15871. + .bDelay = 0x0,
  15872. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  15873. +};
  15874. +
  15875. +
  15876. +static const struct usb_cs_as_format_descriptor
  15877. +z_audio_cs_as_format_desc = {
  15878. + .bLength = 0xe,
  15879. + .bDescriptorType = 0x24,
  15880. +
  15881. + .bDescriptorSubType = 2,
  15882. + .bFormatType = 1,
  15883. + .bNrChannels = 1,
  15884. + .bSubframeSize = 1,
  15885. + .bBitResolution = 8,
  15886. + .bSamfreqType = 0,
  15887. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  15888. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  15889. +};
  15890. +
  15891. +static const struct usb_endpoint_descriptor
  15892. +z_iso_ep = {
  15893. + .bLength = 0x09,
  15894. + .bDescriptorType = 0x05,
  15895. + .bEndpointAddress = 0x04,
  15896. + .bmAttributes = 0x09,
  15897. + .wMaxPacketSize = 0x0038,
  15898. + .bInterval = 0x01,
  15899. + .bRefresh = 0x00,
  15900. + .bSynchAddress = 0x00,
  15901. +};
  15902. +
  15903. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15904. +
  15905. +// 9 bytes
  15906. +static char z_ac_interface_header_desc[] =
  15907. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  15908. +
  15909. +// 12 bytes
  15910. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  15911. + 0x03, 0x00, 0x00, 0x00};
  15912. +// 13 bytes
  15913. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  15914. + 0x02, 0x00, 0x02, 0x00, 0x00};
  15915. +// 9 bytes
  15916. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  15917. + 0x00};
  15918. +
  15919. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  15920. + 0x00};
  15921. +
  15922. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15923. +
  15924. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  15925. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15926. +
  15927. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  15928. + 0x00};
  15929. +
  15930. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15931. +
  15932. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  15933. + 0x00};
  15934. +
  15935. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15936. +
  15937. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  15938. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15939. +
  15940. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  15941. + 0x00};
  15942. +
  15943. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15944. +
  15945. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  15946. + 0x00};
  15947. +
  15948. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15949. +
  15950. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  15951. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15952. +
  15953. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  15954. + 0x00};
  15955. +
  15956. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15957. +
  15958. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  15959. + 0x00};
  15960. +
  15961. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15962. +
  15963. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  15964. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15965. +
  15966. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  15967. + 0x00};
  15968. +
  15969. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15970. +
  15971. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  15972. + 0x00};
  15973. +
  15974. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15975. +
  15976. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  15977. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15978. +
  15979. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  15980. + 0x00};
  15981. +
  15982. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15983. +
  15984. +
  15985. +
  15986. +static const struct usb_descriptor_header *z_function [] = {
  15987. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  15988. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  15989. + (struct usb_descriptor_header *) &z_0,
  15990. + (struct usb_descriptor_header *) &z_1,
  15991. + (struct usb_descriptor_header *) &z_2,
  15992. + (struct usb_descriptor_header *) &z_audio_if_desc,
  15993. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  15994. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  15995. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  15996. + (struct usb_descriptor_header *) &z_iso_ep,
  15997. + (struct usb_descriptor_header *) &z_iso_ep2,
  15998. + (struct usb_descriptor_header *) &za_0,
  15999. + (struct usb_descriptor_header *) &za_1,
  16000. + (struct usb_descriptor_header *) &za_2,
  16001. + (struct usb_descriptor_header *) &za_3,
  16002. + (struct usb_descriptor_header *) &za_4,
  16003. + (struct usb_descriptor_header *) &za_5,
  16004. + (struct usb_descriptor_header *) &za_6,
  16005. + (struct usb_descriptor_header *) &za_7,
  16006. + (struct usb_descriptor_header *) &za_8,
  16007. + (struct usb_descriptor_header *) &za_9,
  16008. + (struct usb_descriptor_header *) &za_10,
  16009. + (struct usb_descriptor_header *) &za_11,
  16010. + (struct usb_descriptor_header *) &za_12,
  16011. + (struct usb_descriptor_header *) &za_13,
  16012. + (struct usb_descriptor_header *) &za_14,
  16013. + (struct usb_descriptor_header *) &za_15,
  16014. + (struct usb_descriptor_header *) &za_16,
  16015. + (struct usb_descriptor_header *) &za_17,
  16016. + (struct usb_descriptor_header *) &za_18,
  16017. + (struct usb_descriptor_header *) &za_19,
  16018. + (struct usb_descriptor_header *) &za_20,
  16019. + (struct usb_descriptor_header *) &za_21,
  16020. + (struct usb_descriptor_header *) &za_22,
  16021. + (struct usb_descriptor_header *) &za_23,
  16022. + (struct usb_descriptor_header *) &za_24,
  16023. + NULL,
  16024. +};
  16025. +
  16026. +/* maxpacket and other transfer characteristics vary by speed. */
  16027. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  16028. +
  16029. +#else
  16030. +
  16031. +/* if there's no high speed support, maxpacket doesn't change. */
  16032. +#define ep_desc(g,hs,fs) fs
  16033. +
  16034. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  16035. +
  16036. +static char manufacturer [40];
  16037. +//static char serial [40];
  16038. +static char serial [] = "Ser 00 em";
  16039. +
  16040. +/* static strings, in UTF-8 */
  16041. +static struct usb_string strings [] = {
  16042. + { STRING_MANUFACTURER, manufacturer, },
  16043. + { STRING_PRODUCT, longname, },
  16044. + { STRING_SERIAL, serial, },
  16045. + { STRING_LOOPBACK, loopback, },
  16046. + { STRING_SOURCE_SINK, source_sink, },
  16047. + { } /* end of list */
  16048. +};
  16049. +
  16050. +static struct usb_gadget_strings stringtab = {
  16051. + .language = 0x0409, /* en-us */
  16052. + .strings = strings,
  16053. +};
  16054. +
  16055. +/*
  16056. + * config descriptors are also handcrafted. these must agree with code
  16057. + * that sets configurations, and with code managing interfaces and their
  16058. + * altsettings. other complexity may come from:
  16059. + *
  16060. + * - high speed support, including "other speed config" rules
  16061. + * - multiple configurations
  16062. + * - interfaces with alternate settings
  16063. + * - embedded class or vendor-specific descriptors
  16064. + *
  16065. + * this handles high speed, and has a second config that could as easily
  16066. + * have been an alternate interface setting (on most hardware).
  16067. + *
  16068. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  16069. + * should include an altsetting to test interrupt transfers, including
  16070. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  16071. + * device?)
  16072. + */
  16073. +static int
  16074. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  16075. +{
  16076. + int len;
  16077. + const struct usb_descriptor_header **function;
  16078. +
  16079. + function = z_function;
  16080. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  16081. + if (len < 0)
  16082. + return len;
  16083. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  16084. + return len;
  16085. +}
  16086. +
  16087. +/*-------------------------------------------------------------------------*/
  16088. +
  16089. +static struct usb_request *
  16090. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  16091. +{
  16092. + struct usb_request *req;
  16093. +
  16094. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  16095. + if (req) {
  16096. + req->length = length;
  16097. + req->buf = usb_ep_alloc_buffer (ep, length,
  16098. + &req->dma, GFP_ATOMIC);
  16099. + if (!req->buf) {
  16100. + usb_ep_free_request (ep, req);
  16101. + req = NULL;
  16102. + }
  16103. + }
  16104. + return req;
  16105. +}
  16106. +
  16107. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  16108. +{
  16109. + if (req->buf)
  16110. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  16111. + usb_ep_free_request (ep, req);
  16112. +}
  16113. +
  16114. +/*-------------------------------------------------------------------------*/
  16115. +
  16116. +/* optionally require specific source/sink data patterns */
  16117. +
  16118. +static int
  16119. +check_read_data (
  16120. + struct zero_dev *dev,
  16121. + struct usb_ep *ep,
  16122. + struct usb_request *req
  16123. +)
  16124. +{
  16125. + unsigned i;
  16126. + u8 *buf = req->buf;
  16127. +
  16128. + for (i = 0; i < req->actual; i++, buf++) {
  16129. + switch (pattern) {
  16130. + /* all-zeroes has no synchronization issues */
  16131. + case 0:
  16132. + if (*buf == 0)
  16133. + continue;
  16134. + break;
  16135. + /* mod63 stays in sync with short-terminated transfers,
  16136. + * or otherwise when host and gadget agree on how large
  16137. + * each usb transfer request should be. resync is done
  16138. + * with set_interface or set_config.
  16139. + */
  16140. + case 1:
  16141. + if (*buf == (u8)(i % 63))
  16142. + continue;
  16143. + break;
  16144. + }
  16145. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  16146. + usb_ep_set_halt (ep);
  16147. + return -EINVAL;
  16148. + }
  16149. + return 0;
  16150. +}
  16151. +
  16152. +/*-------------------------------------------------------------------------*/
  16153. +
  16154. +static void zero_reset_config (struct zero_dev *dev)
  16155. +{
  16156. + if (dev->config == 0)
  16157. + return;
  16158. +
  16159. + DBG (dev, "reset config\n");
  16160. +
  16161. + /* just disable endpoints, forcing completion of pending i/o.
  16162. + * all our completion handlers free their requests in this case.
  16163. + */
  16164. + if (dev->in_ep) {
  16165. + usb_ep_disable (dev->in_ep);
  16166. + dev->in_ep = NULL;
  16167. + }
  16168. + if (dev->out_ep) {
  16169. + usb_ep_disable (dev->out_ep);
  16170. + dev->out_ep = NULL;
  16171. + }
  16172. + dev->config = 0;
  16173. + del_timer (&dev->resume);
  16174. +}
  16175. +
  16176. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  16177. +
  16178. +static void
  16179. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  16180. +{
  16181. + struct zero_dev *dev = ep->driver_data;
  16182. + int status = req->status;
  16183. + int i, j;
  16184. +
  16185. + switch (status) {
  16186. +
  16187. + case 0: /* normal completion? */
  16188. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  16189. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  16190. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  16191. + rbuf[j] = ((__u8*)req->buf)[i];
  16192. + j++;
  16193. + if (j >= RBUF_LEN) j=0;
  16194. + }
  16195. + rbuf_start = j;
  16196. + //printk ("\n\n");
  16197. +
  16198. + if (rbuf_len < RBUF_LEN) {
  16199. + rbuf_len += req->actual;
  16200. + if (rbuf_len > RBUF_LEN) {
  16201. + rbuf_len = RBUF_LEN;
  16202. + }
  16203. + }
  16204. +
  16205. + break;
  16206. +
  16207. + /* this endpoint is normally active while we're configured */
  16208. + case -ECONNABORTED: /* hardware forced ep reset */
  16209. + case -ECONNRESET: /* request dequeued */
  16210. + case -ESHUTDOWN: /* disconnect from host */
  16211. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  16212. + req->actual, req->length);
  16213. + if (ep == dev->out_ep)
  16214. + check_read_data (dev, ep, req);
  16215. + free_ep_req (ep, req);
  16216. + return;
  16217. +
  16218. + case -EOVERFLOW: /* buffer overrun on read means that
  16219. + * we didn't provide a big enough
  16220. + * buffer.
  16221. + */
  16222. + default:
  16223. +#if 1
  16224. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  16225. + status, req->actual, req->length);
  16226. +#endif
  16227. + case -EREMOTEIO: /* short read */
  16228. + break;
  16229. + }
  16230. +
  16231. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  16232. + if (status) {
  16233. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  16234. + ep->name, req->length, status);
  16235. + usb_ep_set_halt (ep);
  16236. + /* FIXME recover later ... somehow */
  16237. + }
  16238. +}
  16239. +
  16240. +static struct usb_request *
  16241. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  16242. +{
  16243. + struct usb_request *req;
  16244. + int status;
  16245. +
  16246. + req = alloc_ep_req (ep, 512);
  16247. + if (!req)
  16248. + return NULL;
  16249. +
  16250. + req->complete = zero_isoc_complete;
  16251. +
  16252. + status = usb_ep_queue (ep, req, gfp_flags);
  16253. + if (status) {
  16254. + struct zero_dev *dev = ep->driver_data;
  16255. +
  16256. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  16257. + free_ep_req (ep, req);
  16258. + req = NULL;
  16259. + }
  16260. +
  16261. + return req;
  16262. +}
  16263. +
  16264. +/* change our operational config. this code must agree with the code
  16265. + * that returns config descriptors, and altsetting code.
  16266. + *
  16267. + * it's also responsible for power management interactions. some
  16268. + * configurations might not work with our current power sources.
  16269. + *
  16270. + * note that some device controller hardware will constrain what this
  16271. + * code can do, perhaps by disallowing more than one configuration or
  16272. + * by limiting configuration choices (like the pxa2xx).
  16273. + */
  16274. +static int
  16275. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  16276. +{
  16277. + int result = 0;
  16278. + struct usb_gadget *gadget = dev->gadget;
  16279. + const struct usb_endpoint_descriptor *d;
  16280. + struct usb_ep *ep;
  16281. +
  16282. + if (number == dev->config)
  16283. + return 0;
  16284. +
  16285. + zero_reset_config (dev);
  16286. +
  16287. + gadget_for_each_ep (ep, gadget) {
  16288. +
  16289. + if (strcmp (ep->name, "ep4") == 0) {
  16290. +
  16291. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  16292. + result = usb_ep_enable (ep, d);
  16293. +
  16294. + if (result == 0) {
  16295. + ep->driver_data = dev;
  16296. + dev->in_ep = ep;
  16297. +
  16298. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  16299. +
  16300. + dev->in_ep = ep;
  16301. + continue;
  16302. + }
  16303. +
  16304. + usb_ep_disable (ep);
  16305. + result = -EIO;
  16306. + }
  16307. + }
  16308. +
  16309. + }
  16310. +
  16311. + dev->config = number;
  16312. + return result;
  16313. +}
  16314. +
  16315. +/*-------------------------------------------------------------------------*/
  16316. +
  16317. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  16318. +{
  16319. + if (req->status || req->actual != req->length)
  16320. + DBG ((struct zero_dev *) ep->driver_data,
  16321. + "setup complete --> %d, %d/%d\n",
  16322. + req->status, req->actual, req->length);
  16323. +}
  16324. +
  16325. +/*
  16326. + * The setup() callback implements all the ep0 functionality that's
  16327. + * not handled lower down, in hardware or the hardware driver (like
  16328. + * device and endpoint feature flags, and their status). It's all
  16329. + * housekeeping for the gadget function we're implementing. Most of
  16330. + * the work is in config-specific setup.
  16331. + */
  16332. +static int
  16333. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  16334. +{
  16335. + struct zero_dev *dev = get_gadget_data (gadget);
  16336. + struct usb_request *req = dev->req;
  16337. + int value = -EOPNOTSUPP;
  16338. +
  16339. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  16340. + * but config change events will reconfigure hardware.
  16341. + */
  16342. + req->zero = 0;
  16343. + switch (ctrl->bRequest) {
  16344. +
  16345. + case USB_REQ_GET_DESCRIPTOR:
  16346. +
  16347. + switch (ctrl->wValue >> 8) {
  16348. +
  16349. + case USB_DT_DEVICE:
  16350. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  16351. + memcpy (req->buf, &device_desc, value);
  16352. + break;
  16353. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16354. + case USB_DT_DEVICE_QUALIFIER:
  16355. + if (!gadget->is_dualspeed)
  16356. + break;
  16357. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  16358. + memcpy (req->buf, &dev_qualifier, value);
  16359. + break;
  16360. +
  16361. + case USB_DT_OTHER_SPEED_CONFIG:
  16362. + if (!gadget->is_dualspeed)
  16363. + break;
  16364. + // FALLTHROUGH
  16365. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  16366. + case USB_DT_CONFIG:
  16367. + value = config_buf (gadget, req->buf,
  16368. + ctrl->wValue >> 8,
  16369. + ctrl->wValue & 0xff);
  16370. + if (value >= 0)
  16371. + value = min (ctrl->wLength, (u16) value);
  16372. + break;
  16373. +
  16374. + case USB_DT_STRING:
  16375. + /* wIndex == language code.
  16376. + * this driver only handles one language, you can
  16377. + * add string tables for other languages, using
  16378. + * any UTF-8 characters
  16379. + */
  16380. + value = usb_gadget_get_string (&stringtab,
  16381. + ctrl->wValue & 0xff, req->buf);
  16382. + if (value >= 0) {
  16383. + value = min (ctrl->wLength, (u16) value);
  16384. + }
  16385. + break;
  16386. + }
  16387. + break;
  16388. +
  16389. + /* currently two configs, two speeds */
  16390. + case USB_REQ_SET_CONFIGURATION:
  16391. + if (ctrl->bRequestType != 0)
  16392. + goto unknown;
  16393. +
  16394. + spin_lock (&dev->lock);
  16395. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  16396. + spin_unlock (&dev->lock);
  16397. + break;
  16398. + case USB_REQ_GET_CONFIGURATION:
  16399. + if (ctrl->bRequestType != USB_DIR_IN)
  16400. + goto unknown;
  16401. + *(u8 *)req->buf = dev->config;
  16402. + value = min (ctrl->wLength, (u16) 1);
  16403. + break;
  16404. +
  16405. + /* until we add altsetting support, or other interfaces,
  16406. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  16407. + * and already killed pending endpoint I/O.
  16408. + */
  16409. + case USB_REQ_SET_INTERFACE:
  16410. +
  16411. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  16412. + goto unknown;
  16413. + spin_lock (&dev->lock);
  16414. + if (dev->config) {
  16415. + u8 config = dev->config;
  16416. +
  16417. + /* resets interface configuration, forgets about
  16418. + * previous transaction state (queued bufs, etc)
  16419. + * and re-inits endpoint state (toggle etc)
  16420. + * no response queued, just zero status == success.
  16421. + * if we had more than one interface we couldn't
  16422. + * use this "reset the config" shortcut.
  16423. + */
  16424. + zero_reset_config (dev);
  16425. + zero_set_config (dev, config, GFP_ATOMIC);
  16426. + value = 0;
  16427. + }
  16428. + spin_unlock (&dev->lock);
  16429. + break;
  16430. + case USB_REQ_GET_INTERFACE:
  16431. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  16432. + value = ctrl->wLength;
  16433. + break;
  16434. + }
  16435. + else {
  16436. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  16437. + goto unknown;
  16438. + if (!dev->config)
  16439. + break;
  16440. + if (ctrl->wIndex != 0) {
  16441. + value = -EDOM;
  16442. + break;
  16443. + }
  16444. + *(u8 *)req->buf = 0;
  16445. + value = min (ctrl->wLength, (u16) 1);
  16446. + }
  16447. + break;
  16448. +
  16449. + /*
  16450. + * These are the same vendor-specific requests supported by
  16451. + * Intel's USB 2.0 compliance test devices. We exceed that
  16452. + * device spec by allowing multiple-packet requests.
  16453. + */
  16454. + case 0x5b: /* control WRITE test -- fill the buffer */
  16455. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  16456. + goto unknown;
  16457. + if (ctrl->wValue || ctrl->wIndex)
  16458. + break;
  16459. + /* just read that many bytes into the buffer */
  16460. + if (ctrl->wLength > USB_BUFSIZ)
  16461. + break;
  16462. + value = ctrl->wLength;
  16463. + break;
  16464. + case 0x5c: /* control READ test -- return the buffer */
  16465. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  16466. + goto unknown;
  16467. + if (ctrl->wValue || ctrl->wIndex)
  16468. + break;
  16469. + /* expect those bytes are still in the buffer; send back */
  16470. + if (ctrl->wLength > USB_BUFSIZ
  16471. + || ctrl->wLength != req->length)
  16472. + break;
  16473. + value = ctrl->wLength;
  16474. + break;
  16475. +
  16476. + case 0x01: // SET_CUR
  16477. + case 0x02:
  16478. + case 0x03:
  16479. + case 0x04:
  16480. + case 0x05:
  16481. + value = ctrl->wLength;
  16482. + break;
  16483. + case 0x81:
  16484. + switch (ctrl->wValue) {
  16485. + case 0x0201:
  16486. + case 0x0202:
  16487. + ((u8*)req->buf)[0] = 0x00;
  16488. + ((u8*)req->buf)[1] = 0xe3;
  16489. + break;
  16490. + case 0x0300:
  16491. + case 0x0500:
  16492. + ((u8*)req->buf)[0] = 0x00;
  16493. + break;
  16494. + }
  16495. + //((u8*)req->buf)[0] = 0x81;
  16496. + //((u8*)req->buf)[1] = 0x81;
  16497. + value = ctrl->wLength;
  16498. + break;
  16499. + case 0x82:
  16500. + switch (ctrl->wValue) {
  16501. + case 0x0201:
  16502. + case 0x0202:
  16503. + ((u8*)req->buf)[0] = 0x00;
  16504. + ((u8*)req->buf)[1] = 0xc3;
  16505. + break;
  16506. + case 0x0300:
  16507. + case 0x0500:
  16508. + ((u8*)req->buf)[0] = 0x00;
  16509. + break;
  16510. + }
  16511. + //((u8*)req->buf)[0] = 0x82;
  16512. + //((u8*)req->buf)[1] = 0x82;
  16513. + value = ctrl->wLength;
  16514. + break;
  16515. + case 0x83:
  16516. + switch (ctrl->wValue) {
  16517. + case 0x0201:
  16518. + case 0x0202:
  16519. + ((u8*)req->buf)[0] = 0x00;
  16520. + ((u8*)req->buf)[1] = 0x00;
  16521. + break;
  16522. + case 0x0300:
  16523. + ((u8*)req->buf)[0] = 0x60;
  16524. + break;
  16525. + case 0x0500:
  16526. + ((u8*)req->buf)[0] = 0x18;
  16527. + break;
  16528. + }
  16529. + //((u8*)req->buf)[0] = 0x83;
  16530. + //((u8*)req->buf)[1] = 0x83;
  16531. + value = ctrl->wLength;
  16532. + break;
  16533. + case 0x84:
  16534. + switch (ctrl->wValue) {
  16535. + case 0x0201:
  16536. + case 0x0202:
  16537. + ((u8*)req->buf)[0] = 0x00;
  16538. + ((u8*)req->buf)[1] = 0x01;
  16539. + break;
  16540. + case 0x0300:
  16541. + case 0x0500:
  16542. + ((u8*)req->buf)[0] = 0x08;
  16543. + break;
  16544. + }
  16545. + //((u8*)req->buf)[0] = 0x84;
  16546. + //((u8*)req->buf)[1] = 0x84;
  16547. + value = ctrl->wLength;
  16548. + break;
  16549. + case 0x85:
  16550. + ((u8*)req->buf)[0] = 0x85;
  16551. + ((u8*)req->buf)[1] = 0x85;
  16552. + value = ctrl->wLength;
  16553. + break;
  16554. +
  16555. +
  16556. + default:
  16557. +unknown:
  16558. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  16559. + ctrl->bRequestType, ctrl->bRequest,
  16560. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  16561. + }
  16562. +
  16563. + /* respond with data transfer before status phase? */
  16564. + if (value >= 0) {
  16565. + req->length = value;
  16566. + req->zero = value < ctrl->wLength
  16567. + && (value % gadget->ep0->maxpacket) == 0;
  16568. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  16569. + if (value < 0) {
  16570. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  16571. + req->status = 0;
  16572. + zero_setup_complete (gadget->ep0, req);
  16573. + }
  16574. + }
  16575. +
  16576. + /* device either stalls (value < 0) or reports success */
  16577. + return value;
  16578. +}
  16579. +
  16580. +static void
  16581. +zero_disconnect (struct usb_gadget *gadget)
  16582. +{
  16583. + struct zero_dev *dev = get_gadget_data (gadget);
  16584. + unsigned long flags;
  16585. +
  16586. + spin_lock_irqsave (&dev->lock, flags);
  16587. + zero_reset_config (dev);
  16588. +
  16589. + /* a more significant application might have some non-usb
  16590. + * activities to quiesce here, saving resources like power
  16591. + * or pushing the notification up a network stack.
  16592. + */
  16593. + spin_unlock_irqrestore (&dev->lock, flags);
  16594. +
  16595. + /* next we may get setup() calls to enumerate new connections;
  16596. + * or an unbind() during shutdown (including removing module).
  16597. + */
  16598. +}
  16599. +
  16600. +static void
  16601. +zero_autoresume (unsigned long _dev)
  16602. +{
  16603. + struct zero_dev *dev = (struct zero_dev *) _dev;
  16604. + int status;
  16605. +
  16606. + /* normally the host would be woken up for something
  16607. + * more significant than just a timer firing...
  16608. + */
  16609. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  16610. + status = usb_gadget_wakeup (dev->gadget);
  16611. + DBG (dev, "wakeup --> %d\n", status);
  16612. + }
  16613. +}
  16614. +
  16615. +/*-------------------------------------------------------------------------*/
  16616. +
  16617. +static void
  16618. +zero_unbind (struct usb_gadget *gadget)
  16619. +{
  16620. + struct zero_dev *dev = get_gadget_data (gadget);
  16621. +
  16622. + DBG (dev, "unbind\n");
  16623. +
  16624. + /* we've already been disconnected ... no i/o is active */
  16625. + if (dev->req)
  16626. + free_ep_req (gadget->ep0, dev->req);
  16627. + del_timer_sync (&dev->resume);
  16628. + kfree (dev);
  16629. + set_gadget_data (gadget, NULL);
  16630. +}
  16631. +
  16632. +static int
  16633. +zero_bind (struct usb_gadget *gadget)
  16634. +{
  16635. + struct zero_dev *dev;
  16636. + //struct usb_ep *ep;
  16637. +
  16638. + printk("binding\n");
  16639. + /*
  16640. + * DRIVER POLICY CHOICE: you may want to do this differently.
  16641. + * One thing to avoid is reusing a bcdDevice revision code
  16642. + * with different host-visible configurations or behavior
  16643. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  16644. + */
  16645. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  16646. +
  16647. +
  16648. + /* ok, we made sense of the hardware ... */
  16649. + dev = kzalloc (sizeof *dev, SLAB_KERNEL);
  16650. + if (!dev)
  16651. + return -ENOMEM;
  16652. + spin_lock_init (&dev->lock);
  16653. + dev->gadget = gadget;
  16654. + set_gadget_data (gadget, dev);
  16655. +
  16656. + /* preallocate control response and buffer */
  16657. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  16658. + if (!dev->req)
  16659. + goto enomem;
  16660. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  16661. + &dev->req->dma, GFP_KERNEL);
  16662. + if (!dev->req->buf)
  16663. + goto enomem;
  16664. +
  16665. + dev->req->complete = zero_setup_complete;
  16666. +
  16667. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  16668. +
  16669. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16670. + /* assume ep0 uses the same value for both speeds ... */
  16671. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  16672. +
  16673. + /* and that all endpoints are dual-speed */
  16674. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  16675. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  16676. +#endif
  16677. +
  16678. + usb_gadget_set_selfpowered (gadget);
  16679. +
  16680. + init_timer (&dev->resume);
  16681. + dev->resume.function = zero_autoresume;
  16682. + dev->resume.data = (unsigned long) dev;
  16683. +
  16684. + gadget->ep0->driver_data = dev;
  16685. +
  16686. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  16687. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  16688. + EP_OUT_NAME, EP_IN_NAME);
  16689. +
  16690. + snprintf (manufacturer, sizeof manufacturer,
  16691. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  16692. + gadget->name);
  16693. +
  16694. + return 0;
  16695. +
  16696. +enomem:
  16697. + zero_unbind (gadget);
  16698. + return -ENOMEM;
  16699. +}
  16700. +
  16701. +/*-------------------------------------------------------------------------*/
  16702. +
  16703. +static void
  16704. +zero_suspend (struct usb_gadget *gadget)
  16705. +{
  16706. + struct zero_dev *dev = get_gadget_data (gadget);
  16707. +
  16708. + if (gadget->speed == USB_SPEED_UNKNOWN)
  16709. + return;
  16710. +
  16711. + if (autoresume) {
  16712. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  16713. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  16714. + } else
  16715. + DBG (dev, "suspend\n");
  16716. +}
  16717. +
  16718. +static void
  16719. +zero_resume (struct usb_gadget *gadget)
  16720. +{
  16721. + struct zero_dev *dev = get_gadget_data (gadget);
  16722. +
  16723. + DBG (dev, "resume\n");
  16724. + del_timer (&dev->resume);
  16725. +}
  16726. +
  16727. +
  16728. +/*-------------------------------------------------------------------------*/
  16729. +
  16730. +static struct usb_gadget_driver zero_driver = {
  16731. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16732. + .speed = USB_SPEED_HIGH,
  16733. +#else
  16734. + .speed = USB_SPEED_FULL,
  16735. +#endif
  16736. + .function = (char *) longname,
  16737. + .bind = zero_bind,
  16738. + .unbind = zero_unbind,
  16739. +
  16740. + .setup = zero_setup,
  16741. + .disconnect = zero_disconnect,
  16742. +
  16743. + .suspend = zero_suspend,
  16744. + .resume = zero_resume,
  16745. +
  16746. + .driver = {
  16747. + .name = (char *) shortname,
  16748. + // .shutdown = ...
  16749. + // .suspend = ...
  16750. + // .resume = ...
  16751. + },
  16752. +};
  16753. +
  16754. +MODULE_AUTHOR ("David Brownell");
  16755. +MODULE_LICENSE ("Dual BSD/GPL");
  16756. +
  16757. +static struct proc_dir_entry *pdir, *pfile;
  16758. +
  16759. +static int isoc_read_data (char *page, char **start,
  16760. + off_t off, int count,
  16761. + int *eof, void *data)
  16762. +{
  16763. + int i;
  16764. + static int c = 0;
  16765. + static int done = 0;
  16766. + static int s = 0;
  16767. +
  16768. +/*
  16769. + printk ("\ncount: %d\n", count);
  16770. + printk ("rbuf_start: %d\n", rbuf_start);
  16771. + printk ("rbuf_len: %d\n", rbuf_len);
  16772. + printk ("off: %d\n", off);
  16773. + printk ("start: %p\n\n", *start);
  16774. +*/
  16775. + if (done) {
  16776. + c = 0;
  16777. + done = 0;
  16778. + *eof = 1;
  16779. + return 0;
  16780. + }
  16781. +
  16782. + if (c == 0) {
  16783. + if (rbuf_len == RBUF_LEN)
  16784. + s = rbuf_start;
  16785. + else s = 0;
  16786. + }
  16787. +
  16788. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  16789. + page[i] = rbuf[(c+s) % RBUF_LEN];
  16790. + }
  16791. + *start = page;
  16792. +
  16793. + if (c >= rbuf_len) {
  16794. + *eof = 1;
  16795. + done = 1;
  16796. + }
  16797. +
  16798. +
  16799. + return i;
  16800. +}
  16801. +
  16802. +static int __init init (void)
  16803. +{
  16804. +
  16805. + int retval = 0;
  16806. +
  16807. + pdir = proc_mkdir("isoc_test", NULL);
  16808. + if(pdir == NULL) {
  16809. + retval = -ENOMEM;
  16810. + printk("Error creating dir\n");
  16811. + goto done;
  16812. + }
  16813. + pdir->owner = THIS_MODULE;
  16814. +
  16815. + pfile = create_proc_read_entry("isoc_data",
  16816. + 0444, pdir,
  16817. + isoc_read_data,
  16818. + NULL);
  16819. + if (pfile == NULL) {
  16820. + retval = -ENOMEM;
  16821. + printk("Error creating file\n");
  16822. + goto no_file;
  16823. + }
  16824. + pfile->owner = THIS_MODULE;
  16825. +
  16826. + return usb_gadget_register_driver (&zero_driver);
  16827. +
  16828. + no_file:
  16829. + remove_proc_entry("isoc_data", NULL);
  16830. + done:
  16831. + return retval;
  16832. +}
  16833. +module_init (init);
  16834. +
  16835. +static void __exit cleanup (void)
  16836. +{
  16837. +
  16838. + usb_gadget_unregister_driver (&zero_driver);
  16839. +
  16840. + remove_proc_entry("isoc_data", pdir);
  16841. + remove_proc_entry("isoc_test", NULL);
  16842. +}
  16843. +module_exit (cleanup);
  16844. --- /dev/null
  16845. +++ b/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  16846. @@ -0,0 +1,142 @@
  16847. +/* ==========================================================================
  16848. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  16849. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  16850. + * otherwise expressly agreed to in writing between Synopsys and you.
  16851. + *
  16852. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  16853. + * any End User Software License Agreement or Agreement for Licensed Product
  16854. + * with Synopsys or any supplement thereto. You are permitted to use and
  16855. + * redistribute this Software in source and binary forms, with or without
  16856. + * modification, provided that redistributions of source code must retain this
  16857. + * notice. You may not view, use, disclose, copy or distribute this file or
  16858. + * any information contained herein except pursuant to this license grant from
  16859. + * Synopsys. If you do not agree with this notice, including the disclaimer
  16860. + * below, then you are not authorized to use the Software.
  16861. + *
  16862. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  16863. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16864. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16865. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  16866. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16867. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16868. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  16869. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16870. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16871. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  16872. + * DAMAGE.
  16873. + * ========================================================================== */
  16874. +
  16875. +#if !defined(__DWC_CFI_COMMON_H__)
  16876. +#define __DWC_CFI_COMMON_H__
  16877. +
  16878. +//#include <linux/types.h>
  16879. +
  16880. +/**
  16881. + * @file
  16882. + *
  16883. + * This file contains the CFI specific common constants, interfaces
  16884. + * (functions and macros) and structures for Linux. No PCD specific
  16885. + * data structure or definition is to be included in this file.
  16886. + *
  16887. + */
  16888. +
  16889. +/** This is a request for all Core Features */
  16890. +#define VEN_CORE_GET_FEATURES 0xB1
  16891. +
  16892. +/** This is a request to get the value of a specific Core Feature */
  16893. +#define VEN_CORE_GET_FEATURE 0xB2
  16894. +
  16895. +/** This command allows the host to set the value of a specific Core Feature */
  16896. +#define VEN_CORE_SET_FEATURE 0xB3
  16897. +
  16898. +/** This command allows the host to set the default values of
  16899. + * either all or any specific Core Feature
  16900. + */
  16901. +#define VEN_CORE_RESET_FEATURES 0xB4
  16902. +
  16903. +/** This command forces the PCD to write the deferred values of a Core Features */
  16904. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  16905. +
  16906. +/** This request reads a DWORD value from a register at the specified offset */
  16907. +#define VEN_CORE_READ_REGISTER 0xB6
  16908. +
  16909. +/** This request writes a DWORD value into a register at the specified offset */
  16910. +#define VEN_CORE_WRITE_REGISTER 0xB7
  16911. +
  16912. +/** This structure is the header of the Core Features dataset returned to
  16913. + * the Host
  16914. + */
  16915. +struct cfi_all_features_header {
  16916. +/** The features header structure length is */
  16917. +#define CFI_ALL_FEATURES_HDR_LEN 8
  16918. + /**
  16919. + * The total length of the features dataset returned to the Host
  16920. + */
  16921. + uint16_t wTotalLen;
  16922. +
  16923. + /**
  16924. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  16925. + * This field identifies the version of the CFI Specification with which
  16926. + * the device is compliant.
  16927. + */
  16928. + uint16_t wVersion;
  16929. +
  16930. + /** The ID of the Core */
  16931. + uint16_t wCoreID;
  16932. +#define CFI_CORE_ID_UDC 1
  16933. +#define CFI_CORE_ID_OTG 2
  16934. +#define CFI_CORE_ID_WUDEV 3
  16935. +
  16936. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  16937. + uint16_t wNumFeatures;
  16938. +} UPACKED;
  16939. +
  16940. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  16941. +
  16942. +/** This structure is a header of the Core Feature descriptor dataset returned to
  16943. + * the Host after the VEN_CORE_GET_FEATURES request
  16944. + */
  16945. +struct cfi_feature_desc_header {
  16946. +#define CFI_FEATURE_DESC_HDR_LEN 8
  16947. +
  16948. + /** The feature ID */
  16949. + uint16_t wFeatureID;
  16950. +
  16951. + /** Length of this feature descriptor in bytes - including the
  16952. + * length of the feature name string
  16953. + */
  16954. + uint16_t wLength;
  16955. +
  16956. + /** The data length of this feature in bytes */
  16957. + uint16_t wDataLength;
  16958. +
  16959. + /**
  16960. + * Attributes of this features
  16961. + * D0: Access rights
  16962. + * 0 - Read/Write
  16963. + * 1 - Read only
  16964. + */
  16965. + uint8_t bmAttributes;
  16966. +#define CFI_FEATURE_ATTR_RO 1
  16967. +#define CFI_FEATURE_ATTR_RW 0
  16968. +
  16969. + /** Length of the feature name in bytes */
  16970. + uint8_t bNameLen;
  16971. +
  16972. + /** The feature name buffer */
  16973. + //uint8_t *name;
  16974. +} UPACKED;
  16975. +
  16976. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  16977. +
  16978. +/**
  16979. + * This structure describes a NULL terminated string referenced by its id field.
  16980. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  16981. + */
  16982. +struct cfi_string {
  16983. + uint16_t id;
  16984. + const uint8_t *s;
  16985. +};
  16986. +typedef struct cfi_string cfi_string_t;
  16987. +
  16988. +#endif
  16989. --- /dev/null
  16990. +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  16991. @@ -0,0 +1,854 @@
  16992. +/* ==========================================================================
  16993. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  16994. + * $Revision: #12 $
  16995. + * $Date: 2011/10/26 $
  16996. + * $Change: 1873028 $
  16997. + *
  16998. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  16999. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17000. + * otherwise expressly agreed to in writing between Synopsys and you.
  17001. + *
  17002. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17003. + * any End User Software License Agreement or Agreement for Licensed Product
  17004. + * with Synopsys or any supplement thereto. You are permitted to use and
  17005. + * redistribute this Software in source and binary forms, with or without
  17006. + * modification, provided that redistributions of source code must retain this
  17007. + * notice. You may not view, use, disclose, copy or distribute this file or
  17008. + * any information contained herein except pursuant to this license grant from
  17009. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17010. + * below, then you are not authorized to use the Software.
  17011. + *
  17012. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17013. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17014. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17015. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17016. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17017. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17018. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17019. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17020. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17021. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17022. + * DAMAGE.
  17023. + * ========================================================================== */
  17024. +
  17025. +#include "dwc_os.h"
  17026. +#include "dwc_otg_regs.h"
  17027. +#include "dwc_otg_cil.h"
  17028. +#include "dwc_otg_adp.h"
  17029. +
  17030. +/** @file
  17031. + *
  17032. + * This file contains the most of the Attach Detect Protocol implementation for
  17033. + * the driver to support OTG Rev2.0.
  17034. + *
  17035. + */
  17036. +
  17037. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  17038. +{
  17039. + adpctl_data_t adpctl;
  17040. +
  17041. + adpctl.d32 = value;
  17042. + adpctl.b.ar = 0x2;
  17043. +
  17044. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  17045. +
  17046. + while (adpctl.b.ar) {
  17047. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  17048. + }
  17049. +
  17050. +}
  17051. +
  17052. +/**
  17053. + * Function is called to read ADP registers
  17054. + */
  17055. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  17056. +{
  17057. + adpctl_data_t adpctl;
  17058. +
  17059. + adpctl.d32 = 0;
  17060. + adpctl.b.ar = 0x1;
  17061. +
  17062. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  17063. +
  17064. + while (adpctl.b.ar) {
  17065. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  17066. + }
  17067. +
  17068. + return adpctl.d32;
  17069. +}
  17070. +
  17071. +/**
  17072. + * Function is called to read ADPCTL register and filter Write-clear bits
  17073. + */
  17074. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  17075. +{
  17076. + adpctl_data_t adpctl;
  17077. +
  17078. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17079. + adpctl.b.adp_tmout_int = 0;
  17080. + adpctl.b.adp_prb_int = 0;
  17081. + adpctl.b.adp_tmout_int = 0;
  17082. +
  17083. + return adpctl.d32;
  17084. +}
  17085. +
  17086. +/**
  17087. + * Function is called to write ADP registers
  17088. + */
  17089. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  17090. + uint32_t set)
  17091. +{
  17092. + dwc_otg_adp_write_reg(core_if,
  17093. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  17094. +}
  17095. +
  17096. +static void adp_sense_timeout(void *ptr)
  17097. +{
  17098. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  17099. + core_if->adp.sense_timer_started = 0;
  17100. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  17101. + if (core_if->adp_enable) {
  17102. + dwc_otg_adp_sense_stop(core_if);
  17103. + dwc_otg_adp_probe_start(core_if);
  17104. + }
  17105. +}
  17106. +
  17107. +/**
  17108. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  17109. + */
  17110. +static void adp_vbuson_timeout(void *ptr)
  17111. +{
  17112. + gpwrdn_data_t gpwrdn;
  17113. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  17114. + hprt0_data_t hprt0 = {.d32 = 0 };
  17115. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  17116. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  17117. + if (core_if) {
  17118. + core_if->adp.vbuson_timer_started = 0;
  17119. + /* Turn off vbus */
  17120. + hprt0.b.prtpwr = 1;
  17121. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  17122. + gpwrdn.d32 = 0;
  17123. +
  17124. + /* Power off the core */
  17125. + if (core_if->power_down == 2) {
  17126. + /* Enable Wakeup Logic */
  17127. +// gpwrdn.b.wkupactiv = 1;
  17128. + gpwrdn.b.pmuactv = 0;
  17129. + gpwrdn.b.pwrdnrstn = 1;
  17130. + gpwrdn.b.pwrdnclmp = 1;
  17131. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17132. + gpwrdn.d32);
  17133. +
  17134. + /* Suspend the Phy Clock */
  17135. + pcgcctl.b.stoppclk = 1;
  17136. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  17137. +
  17138. + /* Switch on VDD */
  17139. +// gpwrdn.b.wkupactiv = 1;
  17140. + gpwrdn.b.pmuactv = 1;
  17141. + gpwrdn.b.pwrdnrstn = 1;
  17142. + gpwrdn.b.pwrdnclmp = 1;
  17143. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17144. + gpwrdn.d32);
  17145. + } else {
  17146. + /* Enable Power Down Logic */
  17147. + gpwrdn.b.pmuintsel = 1;
  17148. + gpwrdn.b.pmuactv = 1;
  17149. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17150. + }
  17151. +
  17152. + /* Power off the core */
  17153. + if (core_if->power_down == 2) {
  17154. + gpwrdn.d32 = 0;
  17155. + gpwrdn.b.pwrdnswtch = 1;
  17156. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  17157. + gpwrdn.d32, 0);
  17158. + }
  17159. +
  17160. + /* Unmask SRP detected interrupt from Power Down Logic */
  17161. + gpwrdn.d32 = 0;
  17162. + gpwrdn.b.srp_det_msk = 1;
  17163. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17164. +
  17165. + dwc_otg_adp_probe_start(core_if);
  17166. + dwc_otg_dump_global_registers(core_if);
  17167. + dwc_otg_dump_host_registers(core_if);
  17168. + }
  17169. +
  17170. +}
  17171. +
  17172. +/**
  17173. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  17174. + * not asserted within 1.1 seconds.
  17175. + *
  17176. + * @param core_if the pointer to core_if strucure.
  17177. + */
  17178. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  17179. +{
  17180. + core_if->adp.vbuson_timer_started = 1;
  17181. + if (core_if->adp.vbuson_timer)
  17182. + {
  17183. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  17184. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  17185. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  17186. + } else {
  17187. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  17188. + }
  17189. +}
  17190. +
  17191. +#if 0
  17192. +/**
  17193. + * Masks all DWC OTG core interrupts
  17194. + *
  17195. + */
  17196. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  17197. +{
  17198. + int i;
  17199. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  17200. +
  17201. + /* Mask Host Interrupts */
  17202. +
  17203. + /* Clear and disable HCINTs */
  17204. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  17205. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  17206. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  17207. +
  17208. + }
  17209. +
  17210. + /* Clear and disable HAINT */
  17211. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  17212. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  17213. +
  17214. + /* Mask Device Interrupts */
  17215. + if (!core_if->multiproc_int_enable) {
  17216. + /* Clear and disable IN Endpoint interrupts */
  17217. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  17218. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  17219. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  17220. + diepint, 0xFFFFFFFF);
  17221. + }
  17222. +
  17223. + /* Clear and disable OUT Endpoint interrupts */
  17224. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  17225. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  17226. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  17227. + doepint, 0xFFFFFFFF);
  17228. + }
  17229. +
  17230. + /* Clear and disable DAINT */
  17231. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  17232. + 0xFFFFFFFF);
  17233. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  17234. + } else {
  17235. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  17236. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  17237. + diepeachintmsk[i], 0);
  17238. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  17239. + diepint, 0xFFFFFFFF);
  17240. + }
  17241. +
  17242. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  17243. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  17244. + doepeachintmsk[i], 0);
  17245. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  17246. + doepint, 0xFFFFFFFF);
  17247. + }
  17248. +
  17249. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  17250. + 0);
  17251. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  17252. + 0xFFFFFFFF);
  17253. +
  17254. + }
  17255. +
  17256. + /* Disable interrupts */
  17257. + ahbcfg.b.glblintrmsk = 1;
  17258. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  17259. +
  17260. + /* Disable all interrupts. */
  17261. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  17262. +
  17263. + /* Clear any pending interrupts */
  17264. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17265. +
  17266. + /* Clear any pending OTG Interrupts */
  17267. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  17268. +}
  17269. +
  17270. +/**
  17271. + * Unmask Port Connection Detected interrupt
  17272. + *
  17273. + */
  17274. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  17275. +{
  17276. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  17277. +
  17278. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  17279. +}
  17280. +#endif
  17281. +
  17282. +/**
  17283. + * Starts the ADP Probing
  17284. + *
  17285. + * @param core_if the pointer to core_if structure.
  17286. + */
  17287. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  17288. +{
  17289. +
  17290. + adpctl_data_t adpctl = {.d32 = 0};
  17291. + gpwrdn_data_t gpwrdn;
  17292. +#if 0
  17293. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  17294. + .b.adp_sns_int = 1, b.adp_tmout_int};
  17295. +#endif
  17296. + dwc_otg_disable_global_interrupts(core_if);
  17297. + DWC_PRINTF("ADP Probe Start\n");
  17298. + core_if->adp.probe_enabled = 1;
  17299. +
  17300. + adpctl.b.adpres = 1;
  17301. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17302. +
  17303. + while (adpctl.b.adpres) {
  17304. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17305. + }
  17306. +
  17307. + adpctl.d32 = 0;
  17308. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17309. +
  17310. + /* In Host mode unmask SRP detected interrupt */
  17311. + gpwrdn.d32 = 0;
  17312. + gpwrdn.b.sts_chngint_msk = 1;
  17313. + if (!gpwrdn.b.idsts) {
  17314. + gpwrdn.b.srp_det_msk = 1;
  17315. + }
  17316. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17317. +
  17318. + adpctl.b.adp_tmout_int_msk = 1;
  17319. + adpctl.b.adp_prb_int_msk = 1;
  17320. + adpctl.b.prb_dschg = 1;
  17321. + adpctl.b.prb_delta = 1;
  17322. + adpctl.b.prb_per = 1;
  17323. + adpctl.b.adpen = 1;
  17324. + adpctl.b.enaprb = 1;
  17325. +
  17326. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17327. + DWC_PRINTF("ADP Probe Finish\n");
  17328. + return 0;
  17329. +}
  17330. +
  17331. +/**
  17332. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  17333. + * within 3 seconds.
  17334. + *
  17335. + * @param core_if the pointer to core_if strucure.
  17336. + */
  17337. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  17338. +{
  17339. + core_if->adp.sense_timer_started = 1;
  17340. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  17341. +}
  17342. +
  17343. +/**
  17344. + * Starts the ADP Sense
  17345. + *
  17346. + * @param core_if the pointer to core_if strucure.
  17347. + */
  17348. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  17349. +{
  17350. + adpctl_data_t adpctl;
  17351. +
  17352. + DWC_PRINTF("ADP Sense Start\n");
  17353. +
  17354. + /* Unmask ADP sense interrupt and mask all other from the core */
  17355. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17356. + adpctl.b.adp_sns_int_msk = 1;
  17357. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17358. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  17359. +
  17360. + /* Set ADP reset bit*/
  17361. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17362. + adpctl.b.adpres = 1;
  17363. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17364. +
  17365. + while (adpctl.b.adpres) {
  17366. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17367. + }
  17368. +
  17369. + adpctl.b.adpres = 0;
  17370. + adpctl.b.adpen = 1;
  17371. + adpctl.b.enasns = 1;
  17372. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17373. +
  17374. + dwc_otg_adp_sense_timer_start(core_if);
  17375. +
  17376. + return 0;
  17377. +}
  17378. +
  17379. +/**
  17380. + * Stops the ADP Probing
  17381. + *
  17382. + * @param core_if the pointer to core_if strucure.
  17383. + */
  17384. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  17385. +{
  17386. +
  17387. + adpctl_data_t adpctl;
  17388. + DWC_PRINTF("Stop ADP probe\n");
  17389. + core_if->adp.probe_enabled = 0;
  17390. + core_if->adp.probe_counter = 0;
  17391. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17392. +
  17393. + adpctl.b.adpen = 0;
  17394. + adpctl.b.adp_prb_int = 1;
  17395. + adpctl.b.adp_tmout_int = 1;
  17396. + adpctl.b.adp_sns_int = 1;
  17397. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17398. +
  17399. + return 0;
  17400. +}
  17401. +
  17402. +/**
  17403. + * Stops the ADP Sensing
  17404. + *
  17405. + * @param core_if the pointer to core_if strucure.
  17406. + */
  17407. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  17408. +{
  17409. + adpctl_data_t adpctl;
  17410. +
  17411. + core_if->adp.sense_enabled = 0;
  17412. +
  17413. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17414. + adpctl.b.enasns = 0;
  17415. + adpctl.b.adp_sns_int = 1;
  17416. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17417. +
  17418. + return 0;
  17419. +}
  17420. +
  17421. +/**
  17422. + * Called to turn on the VBUS after initial ADP probe in host mode.
  17423. + * If port power was already enabled in cil_hcd_start function then
  17424. + * only schedule a timer.
  17425. + *
  17426. + * @param core_if the pointer to core_if structure.
  17427. + */
  17428. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  17429. +{
  17430. + hprt0_data_t hprt0 = {.d32 = 0 };
  17431. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  17432. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  17433. +
  17434. + if (hprt0.b.prtpwr == 0) {
  17435. + hprt0.b.prtpwr = 1;
  17436. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  17437. + }
  17438. +
  17439. + dwc_otg_adp_vbuson_timer_start(core_if);
  17440. +}
  17441. +
  17442. +/**
  17443. + * Called right after driver is loaded
  17444. + * to perform initial actions for ADP
  17445. + *
  17446. + * @param core_if the pointer to core_if structure.
  17447. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  17448. + */
  17449. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  17450. +{
  17451. + gpwrdn_data_t gpwrdn;
  17452. +
  17453. + DWC_PRINTF("ADP Initial Start\n");
  17454. + core_if->adp.adp_started = 1;
  17455. +
  17456. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17457. + dwc_otg_disable_global_interrupts(core_if);
  17458. + if (is_host) {
  17459. + DWC_PRINTF("HOST MODE\n");
  17460. + /* Enable Power Down Logic Interrupt*/
  17461. + gpwrdn.d32 = 0;
  17462. + gpwrdn.b.pmuintsel = 1;
  17463. + gpwrdn.b.pmuactv = 1;
  17464. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17465. + /* Initialize first ADP probe to obtain Ramp Time value */
  17466. + core_if->adp.initial_probe = 1;
  17467. + dwc_otg_adp_probe_start(core_if);
  17468. + } else {
  17469. + gotgctl_data_t gotgctl;
  17470. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  17471. + DWC_PRINTF("DEVICE MODE\n");
  17472. + if (gotgctl.b.bsesvld == 0) {
  17473. + /* Enable Power Down Logic Interrupt*/
  17474. + gpwrdn.d32 = 0;
  17475. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  17476. + gpwrdn.b.pmuintsel = 1;
  17477. + gpwrdn.b.pmuactv = 1;
  17478. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17479. + core_if->adp.initial_probe = 1;
  17480. + dwc_otg_adp_probe_start(core_if);
  17481. + } else {
  17482. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  17483. + core_if->op_state = B_PERIPHERAL;
  17484. + dwc_otg_core_init(core_if);
  17485. + dwc_otg_enable_global_interrupts(core_if);
  17486. + cil_pcd_start(core_if);
  17487. + dwc_otg_dump_global_registers(core_if);
  17488. + dwc_otg_dump_dev_registers(core_if);
  17489. + }
  17490. + }
  17491. +}
  17492. +
  17493. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  17494. +{
  17495. + core_if->adp.adp_started = 0;
  17496. + core_if->adp.initial_probe = 0;
  17497. + core_if->adp.probe_timer_values[0] = -1;
  17498. + core_if->adp.probe_timer_values[1] = -1;
  17499. + core_if->adp.probe_enabled = 0;
  17500. + core_if->adp.sense_enabled = 0;
  17501. + core_if->adp.sense_timer_started = 0;
  17502. + core_if->adp.vbuson_timer_started = 0;
  17503. + core_if->adp.probe_counter = 0;
  17504. + core_if->adp.gpwrdn = 0;
  17505. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  17506. + /* Initialize timers */
  17507. + core_if->adp.sense_timer =
  17508. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  17509. + core_if->adp.vbuson_timer =
  17510. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  17511. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  17512. + {
  17513. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  17514. + }
  17515. +}
  17516. +
  17517. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  17518. +{
  17519. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  17520. + gpwrdn.b.pmuintsel = 1;
  17521. + gpwrdn.b.pmuactv = 1;
  17522. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17523. +
  17524. + if (core_if->adp.probe_enabled)
  17525. + dwc_otg_adp_probe_stop(core_if);
  17526. + if (core_if->adp.sense_enabled)
  17527. + dwc_otg_adp_sense_stop(core_if);
  17528. + if (core_if->adp.sense_timer_started)
  17529. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  17530. + if (core_if->adp.vbuson_timer_started)
  17531. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  17532. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  17533. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  17534. +}
  17535. +
  17536. +/////////////////////////////////////////////////////////////////////
  17537. +////////////// ADP Interrupt Handlers ///////////////////////////////
  17538. +/////////////////////////////////////////////////////////////////////
  17539. +/**
  17540. + * This function sets Ramp Timer values
  17541. + */
  17542. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  17543. +{
  17544. + if (core_if->adp.probe_timer_values[0] == -1) {
  17545. + core_if->adp.probe_timer_values[0] = val;
  17546. + core_if->adp.probe_timer_values[1] = -1;
  17547. + return 1;
  17548. + } else {
  17549. + core_if->adp.probe_timer_values[1] =
  17550. + core_if->adp.probe_timer_values[0];
  17551. + core_if->adp.probe_timer_values[0] = val;
  17552. + return 0;
  17553. + }
  17554. +}
  17555. +
  17556. +/**
  17557. + * This function compares Ramp Timer values
  17558. + */
  17559. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  17560. +{
  17561. + uint32_t diff;
  17562. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  17563. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  17564. + else
  17565. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  17566. + if(diff < 2) {
  17567. + return 0;
  17568. + } else {
  17569. + return 1;
  17570. + }
  17571. +}
  17572. +
  17573. +/**
  17574. + * This function handles ADP Probe Interrupts
  17575. + */
  17576. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  17577. + uint32_t val)
  17578. +{
  17579. + adpctl_data_t adpctl = {.d32 = 0 };
  17580. + gpwrdn_data_t gpwrdn, temp;
  17581. + adpctl.d32 = val;
  17582. +
  17583. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17584. + core_if->adp.probe_counter++;
  17585. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17586. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  17587. + DWC_PRINTF("RTIM value is 0\n");
  17588. + goto exit;
  17589. + }
  17590. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  17591. + core_if->adp.initial_probe) {
  17592. + core_if->adp.initial_probe = 0;
  17593. + dwc_otg_adp_probe_stop(core_if);
  17594. + gpwrdn.d32 = 0;
  17595. + gpwrdn.b.pmuactv = 1;
  17596. + gpwrdn.b.pmuintsel = 1;
  17597. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17598. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17599. +
  17600. + /* check which value is for device mode and which for Host mode */
  17601. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  17602. + /*
  17603. + * Turn on VBUS after initial ADP probe.
  17604. + */
  17605. + core_if->op_state = A_HOST;
  17606. + dwc_otg_enable_global_interrupts(core_if);
  17607. + DWC_SPINUNLOCK(core_if->lock);
  17608. + cil_hcd_start(core_if);
  17609. + dwc_otg_adp_turnon_vbus(core_if);
  17610. + DWC_SPINLOCK(core_if->lock);
  17611. + } else {
  17612. + /*
  17613. + * Initiate SRP after initial ADP probe.
  17614. + */
  17615. + dwc_otg_enable_global_interrupts(core_if);
  17616. + dwc_otg_initiate_srp(core_if);
  17617. + }
  17618. + } else if (core_if->adp.probe_counter > 2){
  17619. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17620. + if (compare_timer_values(core_if)) {
  17621. + DWC_PRINTF("Difference in timer values !!! \n");
  17622. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  17623. + dwc_otg_adp_probe_stop(core_if);
  17624. +
  17625. + /* Power on the core */
  17626. + if (core_if->power_down == 2) {
  17627. + gpwrdn.b.pwrdnswtch = 1;
  17628. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17629. + gpwrdn, 0, gpwrdn.d32);
  17630. + }
  17631. +
  17632. + /* check which value is for device mode and which for Host mode */
  17633. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  17634. + /* Disable Interrupt from Power Down Logic */
  17635. + gpwrdn.d32 = 0;
  17636. + gpwrdn.b.pmuintsel = 1;
  17637. + gpwrdn.b.pmuactv = 1;
  17638. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17639. + gpwrdn, gpwrdn.d32, 0);
  17640. +
  17641. + /*
  17642. + * Initialize the Core for Host mode.
  17643. + */
  17644. + core_if->op_state = A_HOST;
  17645. + dwc_otg_core_init(core_if);
  17646. + dwc_otg_enable_global_interrupts(core_if);
  17647. + cil_hcd_start(core_if);
  17648. + } else {
  17649. + gotgctl_data_t gotgctl;
  17650. + /* Mask SRP detected interrupt from Power Down Logic */
  17651. + gpwrdn.d32 = 0;
  17652. + gpwrdn.b.srp_det_msk = 1;
  17653. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17654. + gpwrdn, gpwrdn.d32, 0);
  17655. +
  17656. + /* Disable Power Down Logic */
  17657. + gpwrdn.d32 = 0;
  17658. + gpwrdn.b.pmuintsel = 1;
  17659. + gpwrdn.b.pmuactv = 1;
  17660. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17661. + gpwrdn, gpwrdn.d32, 0);
  17662. +
  17663. + /*
  17664. + * Initialize the Core for Device mode.
  17665. + */
  17666. + core_if->op_state = B_PERIPHERAL;
  17667. + dwc_otg_core_init(core_if);
  17668. + dwc_otg_enable_global_interrupts(core_if);
  17669. + cil_pcd_start(core_if);
  17670. +
  17671. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  17672. + if (!gotgctl.b.bsesvld) {
  17673. + dwc_otg_initiate_srp(core_if);
  17674. + }
  17675. + }
  17676. + }
  17677. + if (core_if->power_down == 2) {
  17678. + if (gpwrdn.b.bsessvld) {
  17679. + /* Mask SRP detected interrupt from Power Down Logic */
  17680. + gpwrdn.d32 = 0;
  17681. + gpwrdn.b.srp_det_msk = 1;
  17682. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17683. +
  17684. + /* Disable Power Down Logic */
  17685. + gpwrdn.d32 = 0;
  17686. + gpwrdn.b.pmuactv = 1;
  17687. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17688. +
  17689. + /*
  17690. + * Initialize the Core for Device mode.
  17691. + */
  17692. + core_if->op_state = B_PERIPHERAL;
  17693. + dwc_otg_core_init(core_if);
  17694. + dwc_otg_enable_global_interrupts(core_if);
  17695. + cil_pcd_start(core_if);
  17696. + }
  17697. + }
  17698. + }
  17699. +exit:
  17700. + /* Clear interrupt */
  17701. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17702. + adpctl.b.adp_prb_int = 1;
  17703. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17704. +
  17705. + return 0;
  17706. +}
  17707. +
  17708. +/**
  17709. + * This function hadles ADP Sense Interrupt
  17710. + */
  17711. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  17712. +{
  17713. + adpctl_data_t adpctl;
  17714. + /* Stop ADP Sense timer */
  17715. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  17716. +
  17717. + /* Restart ADP Sense timer */
  17718. + dwc_otg_adp_sense_timer_start(core_if);
  17719. +
  17720. + /* Clear interrupt */
  17721. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17722. + adpctl.b.adp_sns_int = 1;
  17723. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17724. +
  17725. + return 0;
  17726. +}
  17727. +
  17728. +/**
  17729. + * This function handles ADP Probe Interrupts
  17730. + */
  17731. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  17732. + uint32_t val)
  17733. +{
  17734. + adpctl_data_t adpctl = {.d32 = 0 };
  17735. + adpctl.d32 = val;
  17736. + set_timer_value(core_if, adpctl.b.rtim);
  17737. +
  17738. + /* Clear interrupt */
  17739. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17740. + adpctl.b.adp_tmout_int = 1;
  17741. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17742. +
  17743. + return 0;
  17744. +}
  17745. +
  17746. +/**
  17747. + * ADP Interrupt handler.
  17748. + *
  17749. + */
  17750. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  17751. +{
  17752. + int retval = 0;
  17753. + adpctl_data_t adpctl = {.d32 = 0};
  17754. +
  17755. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17756. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  17757. +
  17758. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  17759. + DWC_PRINTF("ADP Sense interrupt\n");
  17760. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  17761. + }
  17762. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  17763. + DWC_PRINTF("ADP timeout interrupt\n");
  17764. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  17765. + }
  17766. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  17767. + DWC_PRINTF("ADP Probe interrupt\n");
  17768. + adpctl.b.adp_prb_int = 1;
  17769. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  17770. + }
  17771. +
  17772. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  17773. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17774. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  17775. +
  17776. + return retval;
  17777. +}
  17778. +
  17779. +/**
  17780. + *
  17781. + * @param core_if Programming view of DWC_otg controller.
  17782. + */
  17783. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  17784. +{
  17785. +
  17786. +#ifndef DWC_HOST_ONLY
  17787. + hprt0_data_t hprt0;
  17788. + gpwrdn_data_t gpwrdn;
  17789. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  17790. +
  17791. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17792. + /* check which value is for device mode and which for Host mode */
  17793. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  17794. + DWC_PRINTF("SRP: Host mode\n");
  17795. +
  17796. + if (core_if->adp_enable) {
  17797. + dwc_otg_adp_probe_stop(core_if);
  17798. +
  17799. + /* Power on the core */
  17800. + if (core_if->power_down == 2) {
  17801. + gpwrdn.b.pwrdnswtch = 1;
  17802. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17803. + gpwrdn, 0, gpwrdn.d32);
  17804. + }
  17805. +
  17806. + core_if->op_state = A_HOST;
  17807. + dwc_otg_core_init(core_if);
  17808. + dwc_otg_enable_global_interrupts(core_if);
  17809. + cil_hcd_start(core_if);
  17810. + }
  17811. +
  17812. + /* Turn on the port power bit. */
  17813. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  17814. + hprt0.b.prtpwr = 1;
  17815. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  17816. +
  17817. + /* Start the Connection timer. So a message can be displayed
  17818. + * if connect does not occur within 10 seconds. */
  17819. + cil_hcd_session_start(core_if);
  17820. + } else {
  17821. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  17822. + if (core_if->adp_enable) {
  17823. + dwc_otg_adp_probe_stop(core_if);
  17824. +
  17825. + /* Power on the core */
  17826. + if (core_if->power_down == 2) {
  17827. + gpwrdn.b.pwrdnswtch = 1;
  17828. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17829. + gpwrdn, 0, gpwrdn.d32);
  17830. + }
  17831. +
  17832. + gpwrdn.d32 = 0;
  17833. + gpwrdn.b.pmuactv = 0;
  17834. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17835. + gpwrdn.d32);
  17836. +
  17837. + core_if->op_state = B_PERIPHERAL;
  17838. + dwc_otg_core_init(core_if);
  17839. + dwc_otg_enable_global_interrupts(core_if);
  17840. + cil_pcd_start(core_if);
  17841. + }
  17842. + }
  17843. +#endif
  17844. + return 1;
  17845. +}
  17846. --- /dev/null
  17847. +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  17848. @@ -0,0 +1,80 @@
  17849. +/* ==========================================================================
  17850. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  17851. + * $Revision: #7 $
  17852. + * $Date: 2011/10/24 $
  17853. + * $Change: 1871159 $
  17854. + *
  17855. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  17856. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17857. + * otherwise expressly agreed to in writing between Synopsys and you.
  17858. + *
  17859. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17860. + * any End User Software License Agreement or Agreement for Licensed Product
  17861. + * with Synopsys or any supplement thereto. You are permitted to use and
  17862. + * redistribute this Software in source and binary forms, with or without
  17863. + * modification, provided that redistributions of source code must retain this
  17864. + * notice. You may not view, use, disclose, copy or distribute this file or
  17865. + * any information contained herein except pursuant to this license grant from
  17866. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17867. + * below, then you are not authorized to use the Software.
  17868. + *
  17869. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17870. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17871. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17872. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17873. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17874. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17875. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17876. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17877. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17878. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17879. + * DAMAGE.
  17880. + * ========================================================================== */
  17881. +
  17882. +#ifndef __DWC_OTG_ADP_H__
  17883. +#define __DWC_OTG_ADP_H__
  17884. +
  17885. +/**
  17886. + * @file
  17887. + *
  17888. + * This file contains the Attach Detect Protocol interfaces and defines
  17889. + * (functions) and structures for Linux.
  17890. + *
  17891. + */
  17892. +
  17893. +#define DWC_OTG_ADP_UNATTACHED 0
  17894. +#define DWC_OTG_ADP_ATTACHED 1
  17895. +#define DWC_OTG_ADP_UNKOWN 2
  17896. +
  17897. +typedef struct dwc_otg_adp {
  17898. + uint32_t adp_started;
  17899. + uint32_t initial_probe;
  17900. + int32_t probe_timer_values[2];
  17901. + uint32_t probe_enabled;
  17902. + uint32_t sense_enabled;
  17903. + dwc_timer_t *sense_timer;
  17904. + uint32_t sense_timer_started;
  17905. + dwc_timer_t *vbuson_timer;
  17906. + uint32_t vbuson_timer_started;
  17907. + uint32_t attached;
  17908. + uint32_t probe_counter;
  17909. + uint32_t gpwrdn;
  17910. +} dwc_otg_adp_t;
  17911. +
  17912. +/**
  17913. + * Attach Detect Protocol functions
  17914. + */
  17915. +
  17916. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  17917. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  17918. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  17919. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  17920. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  17921. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  17922. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  17923. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  17924. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  17925. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  17926. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  17927. +
  17928. +#endif //__DWC_OTG_ADP_H__
  17929. --- /dev/null
  17930. +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  17931. @@ -0,0 +1,1212 @@
  17932. +/* ==========================================================================
  17933. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  17934. + * $Revision: #44 $
  17935. + * $Date: 2010/11/29 $
  17936. + * $Change: 1636033 $
  17937. + *
  17938. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  17939. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17940. + * otherwise expressly agreed to in writing between Synopsys and you.
  17941. + *
  17942. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17943. + * any End User Software License Agreement or Agreement for Licensed Product
  17944. + * with Synopsys or any supplement thereto. You are permitted to use and
  17945. + * redistribute this Software in source and binary forms, with or without
  17946. + * modification, provided that redistributions of source code must retain this
  17947. + * notice. You may not view, use, disclose, copy or distribute this file or
  17948. + * any information contained herein except pursuant to this license grant from
  17949. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17950. + * below, then you are not authorized to use the Software.
  17951. + *
  17952. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17953. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17954. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17955. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17956. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17957. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17958. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17959. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17960. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17961. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17962. + * DAMAGE.
  17963. + * ========================================================================== */
  17964. +
  17965. +/** @file
  17966. + *
  17967. + * The diagnostic interface will provide access to the controller for
  17968. + * bringing up the hardware and testing. The Linux driver attributes
  17969. + * feature will be used to provide the Linux Diagnostic
  17970. + * Interface. These attributes are accessed through sysfs.
  17971. + */
  17972. +
  17973. +/** @page "Linux Module Attributes"
  17974. + *
  17975. + * The Linux module attributes feature is used to provide the Linux
  17976. + * Diagnostic Interface. These attributes are accessed through sysfs.
  17977. + * The diagnostic interface will provide access to the controller for
  17978. + * bringing up the hardware and testing.
  17979. +
  17980. + The following table shows the attributes.
  17981. + <table>
  17982. + <tr>
  17983. + <td><b> Name</b></td>
  17984. + <td><b> Description</b></td>
  17985. + <td><b> Access</b></td>
  17986. + </tr>
  17987. +
  17988. + <tr>
  17989. + <td> mode </td>
  17990. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  17991. + <td> Read</td>
  17992. + </tr>
  17993. +
  17994. + <tr>
  17995. + <td> hnpcapable </td>
  17996. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  17997. + Read returns the current value.</td>
  17998. + <td> Read/Write</td>
  17999. + </tr>
  18000. +
  18001. + <tr>
  18002. + <td> srpcapable </td>
  18003. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  18004. + Read returns the current value.</td>
  18005. + <td> Read/Write</td>
  18006. + </tr>
  18007. +
  18008. + <tr>
  18009. + <td> hsic_connect </td>
  18010. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  18011. + Read returns the current value.</td>
  18012. + <td> Read/Write</td>
  18013. + </tr>
  18014. +
  18015. + <tr>
  18016. + <td> inv_sel_hsic </td>
  18017. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  18018. + Read returns the current value.</td>
  18019. + <td> Read/Write</td>
  18020. + </tr>
  18021. +
  18022. + <tr>
  18023. + <td> hnp </td>
  18024. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  18025. + <td> Read/Write</td>
  18026. + </tr>
  18027. +
  18028. + <tr>
  18029. + <td> srp </td>
  18030. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  18031. + <td> Read/Write</td>
  18032. + </tr>
  18033. +
  18034. + <tr>
  18035. + <td> buspower </td>
  18036. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  18037. + <td> Read/Write</td>
  18038. + </tr>
  18039. +
  18040. + <tr>
  18041. + <td> bussuspend </td>
  18042. + <td> Suspends the USB bus.</td>
  18043. + <td> Read/Write</td>
  18044. + </tr>
  18045. +
  18046. + <tr>
  18047. + <td> busconnected </td>
  18048. + <td> Gets the connection status of the bus</td>
  18049. + <td> Read</td>
  18050. + </tr>
  18051. +
  18052. + <tr>
  18053. + <td> gotgctl </td>
  18054. + <td> Gets or sets the Core Control Status Register.</td>
  18055. + <td> Read/Write</td>
  18056. + </tr>
  18057. +
  18058. + <tr>
  18059. + <td> gusbcfg </td>
  18060. + <td> Gets or sets the Core USB Configuration Register</td>
  18061. + <td> Read/Write</td>
  18062. + </tr>
  18063. +
  18064. + <tr>
  18065. + <td> grxfsiz </td>
  18066. + <td> Gets or sets the Receive FIFO Size Register</td>
  18067. + <td> Read/Write</td>
  18068. + </tr>
  18069. +
  18070. + <tr>
  18071. + <td> gnptxfsiz </td>
  18072. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  18073. + <td> Read/Write</td>
  18074. + </tr>
  18075. +
  18076. + <tr>
  18077. + <td> gpvndctl </td>
  18078. + <td> Gets or sets the PHY Vendor Control Register</td>
  18079. + <td> Read/Write</td>
  18080. + </tr>
  18081. +
  18082. + <tr>
  18083. + <td> ggpio </td>
  18084. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  18085. + or sets the upper 16 bits.</td>
  18086. + <td> Read/Write</td>
  18087. + </tr>
  18088. +
  18089. + <tr>
  18090. + <td> guid </td>
  18091. + <td> Gets or sets the value of the User ID Register</td>
  18092. + <td> Read/Write</td>
  18093. + </tr>
  18094. +
  18095. + <tr>
  18096. + <td> gsnpsid </td>
  18097. + <td> Gets the value of the Synopsys ID Regester</td>
  18098. + <td> Read</td>
  18099. + </tr>
  18100. +
  18101. + <tr>
  18102. + <td> devspeed </td>
  18103. + <td> Gets or sets the device speed setting in the DCFG register</td>
  18104. + <td> Read/Write</td>
  18105. + </tr>
  18106. +
  18107. + <tr>
  18108. + <td> enumspeed </td>
  18109. + <td> Gets the device enumeration Speed.</td>
  18110. + <td> Read</td>
  18111. + </tr>
  18112. +
  18113. + <tr>
  18114. + <td> hptxfsiz </td>
  18115. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  18116. + <td> Read</td>
  18117. + </tr>
  18118. +
  18119. + <tr>
  18120. + <td> hprt0 </td>
  18121. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  18122. + <td> Read/Write</td>
  18123. + </tr>
  18124. +
  18125. + <tr>
  18126. + <td> regoffset </td>
  18127. + <td> Sets the register offset for the next Register Access</td>
  18128. + <td> Read/Write</td>
  18129. + </tr>
  18130. +
  18131. + <tr>
  18132. + <td> regvalue </td>
  18133. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  18134. + <td> Read/Write</td>
  18135. + </tr>
  18136. +
  18137. + <tr>
  18138. + <td> remote_wakeup </td>
  18139. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  18140. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  18141. + Wakeup signalling bit in the Device Control Register is set for 1
  18142. + milli-second.</td>
  18143. + <td> Read/Write</td>
  18144. + </tr>
  18145. +
  18146. + <tr>
  18147. + <td> rem_wakeup_pwrdn </td>
  18148. + <td> On read, shows the status core - hibernated or not. On write, initiates
  18149. + a remote wakeup of the device from Hibernation. </td>
  18150. + <td> Read/Write</td>
  18151. + </tr>
  18152. +
  18153. + <tr>
  18154. + <td> mode_ch_tim_en </td>
  18155. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  18156. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  18157. + after Suspend or LPM. </td>
  18158. + <td> Read/Write</td>
  18159. + </tr>
  18160. +
  18161. + <tr>
  18162. + <td> fr_interval </td>
  18163. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  18164. + reload HFIR register during runtime. The application can write a value to this
  18165. + register only after the Port Enable bit of the Host Port Control and Status
  18166. + register (HPRT.PrtEnaPort) has been set </td>
  18167. + <td> Read/Write</td>
  18168. + </tr>
  18169. +
  18170. + <tr>
  18171. + <td> disconnect_us </td>
  18172. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  18173. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  18174. + <td> Read/Write</td>
  18175. + </tr>
  18176. +
  18177. + <tr>
  18178. + <td> regdump </td>
  18179. + <td> Dumps the contents of core registers.</td>
  18180. + <td> Read</td>
  18181. + </tr>
  18182. +
  18183. + <tr>
  18184. + <td> spramdump </td>
  18185. + <td> Dumps the contents of core registers.</td>
  18186. + <td> Read</td>
  18187. + </tr>
  18188. +
  18189. + <tr>
  18190. + <td> hcddump </td>
  18191. + <td> Dumps the current HCD state.</td>
  18192. + <td> Read</td>
  18193. + </tr>
  18194. +
  18195. + <tr>
  18196. + <td> hcd_frrem </td>
  18197. + <td> Shows the average value of the Frame Remaining
  18198. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  18199. + occurs. This can be used to determine the average interrupt latency. Also
  18200. + shows the average Frame Remaining value for start_transfer and the "a" and
  18201. + "b" sample points. The "a" and "b" sample points may be used during debugging
  18202. + bto determine how long it takes to execute a section of the HCD code.</td>
  18203. + <td> Read</td>
  18204. + </tr>
  18205. +
  18206. + <tr>
  18207. + <td> rd_reg_test </td>
  18208. + <td> Displays the time required to read the GNPTXFSIZ register many times
  18209. + (the output shows the number of times the register is read).
  18210. + <td> Read</td>
  18211. + </tr>
  18212. +
  18213. + <tr>
  18214. + <td> wr_reg_test </td>
  18215. + <td> Displays the time required to write the GNPTXFSIZ register many times
  18216. + (the output shows the number of times the register is written).
  18217. + <td> Read</td>
  18218. + </tr>
  18219. +
  18220. + <tr>
  18221. + <td> lpm_response </td>
  18222. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  18223. + <td> Write</td>
  18224. + </tr>
  18225. +
  18226. + <tr>
  18227. + <td> sleep_status </td>
  18228. + <td> Shows sleep status of device.
  18229. + <td> Read</td>
  18230. + </tr>
  18231. +
  18232. + </table>
  18233. +
  18234. + Example usage:
  18235. + To get the current mode:
  18236. + cat /sys/devices/lm0/mode
  18237. +
  18238. + To power down the USB:
  18239. + echo 0 > /sys/devices/lm0/buspower
  18240. + */
  18241. +
  18242. +#include "dwc_otg_os_dep.h"
  18243. +#include "dwc_os.h"
  18244. +#include "dwc_otg_driver.h"
  18245. +#include "dwc_otg_attr.h"
  18246. +#include "dwc_otg_core_if.h"
  18247. +#include "dwc_otg_pcd_if.h"
  18248. +#include "dwc_otg_hcd_if.h"
  18249. +
  18250. +/*
  18251. + * MACROs for defining sysfs attribute
  18252. + */
  18253. +#ifdef LM_INTERFACE
  18254. +
  18255. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18256. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18257. +{ \
  18258. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18259. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18260. + uint32_t val; \
  18261. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18262. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18263. +}
  18264. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18265. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18266. + const char *buf, size_t count) \
  18267. +{ \
  18268. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18269. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18270. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18271. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18272. + return count; \
  18273. +}
  18274. +
  18275. +#elif defined(PCI_INTERFACE)
  18276. +
  18277. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18278. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18279. +{ \
  18280. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18281. + uint32_t val; \
  18282. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18283. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18284. +}
  18285. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18286. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18287. + const char *buf, size_t count) \
  18288. +{ \
  18289. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18290. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18291. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18292. + return count; \
  18293. +}
  18294. +
  18295. +#elif defined(PLATFORM_INTERFACE)
  18296. +
  18297. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18298. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18299. +{ \
  18300. + struct platform_device *platform_dev = \
  18301. + container_of(_dev, struct platform_device, dev); \
  18302. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18303. + uint32_t val; \
  18304. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  18305. + __func__, _dev, platform_dev, otg_dev); \
  18306. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18307. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18308. +}
  18309. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18310. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18311. + const char *buf, size_t count) \
  18312. +{ \
  18313. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18314. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18315. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18316. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18317. + return count; \
  18318. +}
  18319. +#endif
  18320. +
  18321. +/*
  18322. + * MACROs for defining sysfs attribute for 32-bit registers
  18323. + */
  18324. +#ifdef LM_INTERFACE
  18325. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18326. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18327. +{ \
  18328. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18329. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18330. + uint32_t val; \
  18331. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18332. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18333. +}
  18334. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18335. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18336. + const char *buf, size_t count) \
  18337. +{ \
  18338. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18339. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18340. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18341. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18342. + return count; \
  18343. +}
  18344. +#elif defined(PCI_INTERFACE)
  18345. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18346. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18347. +{ \
  18348. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18349. + uint32_t val; \
  18350. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18351. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18352. +}
  18353. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18354. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18355. + const char *buf, size_t count) \
  18356. +{ \
  18357. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18358. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18359. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18360. + return count; \
  18361. +}
  18362. +
  18363. +#elif defined(PLATFORM_INTERFACE)
  18364. +#include "dwc_otg_dbg.h"
  18365. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18366. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18367. +{ \
  18368. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18369. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18370. + uint32_t val; \
  18371. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  18372. + __func__, _dev, platform_dev, otg_dev); \
  18373. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18374. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18375. +}
  18376. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18377. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18378. + const char *buf, size_t count) \
  18379. +{ \
  18380. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18381. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18382. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18383. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18384. + return count; \
  18385. +}
  18386. +
  18387. +#endif
  18388. +
  18389. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  18390. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18391. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18392. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  18393. +
  18394. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  18395. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18396. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  18397. +
  18398. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  18399. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18400. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18401. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  18402. +
  18403. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  18404. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18405. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  18406. +
  18407. +/** @name Functions for Show/Store of Attributes */
  18408. +/**@{*/
  18409. +
  18410. +/**
  18411. + * Helper function returning the otg_device structure of the given device
  18412. + */
  18413. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  18414. +{
  18415. + dwc_otg_device_t *otg_dev;
  18416. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  18417. + return otg_dev;
  18418. +}
  18419. +
  18420. +/**
  18421. + * Show the register offset of the Register Access.
  18422. + */
  18423. +static ssize_t regoffset_show(struct device *_dev,
  18424. + struct device_attribute *attr, char *buf)
  18425. +{
  18426. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18427. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  18428. + otg_dev->os_dep.reg_offset);
  18429. +}
  18430. +
  18431. +/**
  18432. + * Set the register offset for the next Register Access Read/Write
  18433. + */
  18434. +static ssize_t regoffset_store(struct device *_dev,
  18435. + struct device_attribute *attr,
  18436. + const char *buf, size_t count)
  18437. +{
  18438. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18439. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  18440. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  18441. + if (offset < SZ_256K) {
  18442. +#elif defined(PCI_INTERFACE)
  18443. + if (offset < 0x00040000) {
  18444. +#endif
  18445. + otg_dev->os_dep.reg_offset = offset;
  18446. + } else {
  18447. + dev_err(_dev, "invalid offset\n");
  18448. + }
  18449. +
  18450. + return count;
  18451. +}
  18452. +
  18453. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  18454. +
  18455. +/**
  18456. + * Show the value of the register at the offset in the reg_offset
  18457. + * attribute.
  18458. + */
  18459. +static ssize_t regvalue_show(struct device *_dev,
  18460. + struct device_attribute *attr, char *buf)
  18461. +{
  18462. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18463. + uint32_t val;
  18464. + volatile uint32_t *addr;
  18465. +
  18466. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  18467. + /* Calculate the address */
  18468. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  18469. + (uint8_t *) otg_dev->os_dep.base);
  18470. + val = DWC_READ_REG32(addr);
  18471. + return snprintf(buf,
  18472. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  18473. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  18474. + val);
  18475. + } else {
  18476. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  18477. + return sprintf(buf, "invalid offset\n");
  18478. + }
  18479. +}
  18480. +
  18481. +/**
  18482. + * Store the value in the register at the offset in the reg_offset
  18483. + * attribute.
  18484. + *
  18485. + */
  18486. +static ssize_t regvalue_store(struct device *_dev,
  18487. + struct device_attribute *attr,
  18488. + const char *buf, size_t count)
  18489. +{
  18490. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18491. + volatile uint32_t *addr;
  18492. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18493. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  18494. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  18495. + /* Calculate the address */
  18496. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  18497. + (uint8_t *) otg_dev->os_dep.base);
  18498. + DWC_WRITE_REG32(addr, val);
  18499. + } else {
  18500. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  18501. + otg_dev->os_dep.reg_offset);
  18502. + }
  18503. + return count;
  18504. +}
  18505. +
  18506. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  18507. +
  18508. +/*
  18509. + * Attributes
  18510. + */
  18511. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  18512. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  18513. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  18514. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  18515. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  18516. +
  18517. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  18518. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  18519. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  18520. +
  18521. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  18522. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  18523. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  18524. + "GUSBCFG");
  18525. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  18526. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  18527. + "GRXFSIZ");
  18528. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  18529. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  18530. + "GNPTXFSIZ");
  18531. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  18532. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  18533. + "GPVNDCTL");
  18534. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  18535. + &(otg_dev->core_if->core_global_regs->ggpio),
  18536. + "GGPIO");
  18537. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  18538. + "GUID");
  18539. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  18540. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  18541. + "GSNPSID");
  18542. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  18543. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  18544. +
  18545. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  18546. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  18547. + "HPTXFSIZ");
  18548. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  18549. +
  18550. +/**
  18551. + * @todo Add code to initiate the HNP.
  18552. + */
  18553. +/**
  18554. + * Show the HNP status bit
  18555. + */
  18556. +static ssize_t hnp_show(struct device *_dev,
  18557. + struct device_attribute *attr, char *buf)
  18558. +{
  18559. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18560. + return sprintf(buf, "HstNegScs = 0x%x\n",
  18561. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  18562. +}
  18563. +
  18564. +/**
  18565. + * Set the HNP Request bit
  18566. + */
  18567. +static ssize_t hnp_store(struct device *_dev,
  18568. + struct device_attribute *attr,
  18569. + const char *buf, size_t count)
  18570. +{
  18571. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18572. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18573. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  18574. + return count;
  18575. +}
  18576. +
  18577. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  18578. +
  18579. +/**
  18580. + * @todo Add code to initiate the SRP.
  18581. + */
  18582. +/**
  18583. + * Show the SRP status bit
  18584. + */
  18585. +static ssize_t srp_show(struct device *_dev,
  18586. + struct device_attribute *attr, char *buf)
  18587. +{
  18588. +#ifndef DWC_HOST_ONLY
  18589. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18590. + return sprintf(buf, "SesReqScs = 0x%x\n",
  18591. + dwc_otg_get_srpstatus(otg_dev->core_if));
  18592. +#else
  18593. + return sprintf(buf, "Host Only Mode!\n");
  18594. +#endif
  18595. +}
  18596. +
  18597. +/**
  18598. + * Set the SRP Request bit
  18599. + */
  18600. +static ssize_t srp_store(struct device *_dev,
  18601. + struct device_attribute *attr,
  18602. + const char *buf, size_t count)
  18603. +{
  18604. +#ifndef DWC_HOST_ONLY
  18605. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18606. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  18607. +#endif
  18608. + return count;
  18609. +}
  18610. +
  18611. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  18612. +
  18613. +/**
  18614. + * @todo Need to do more for power on/off?
  18615. + */
  18616. +/**
  18617. + * Show the Bus Power status
  18618. + */
  18619. +static ssize_t buspower_show(struct device *_dev,
  18620. + struct device_attribute *attr, char *buf)
  18621. +{
  18622. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18623. + return sprintf(buf, "Bus Power = 0x%x\n",
  18624. + dwc_otg_get_prtpower(otg_dev->core_if));
  18625. +}
  18626. +
  18627. +/**
  18628. + * Set the Bus Power status
  18629. + */
  18630. +static ssize_t buspower_store(struct device *_dev,
  18631. + struct device_attribute *attr,
  18632. + const char *buf, size_t count)
  18633. +{
  18634. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18635. + uint32_t on = simple_strtoul(buf, NULL, 16);
  18636. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  18637. + return count;
  18638. +}
  18639. +
  18640. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  18641. +
  18642. +/**
  18643. + * @todo Need to do more for suspend?
  18644. + */
  18645. +/**
  18646. + * Show the Bus Suspend status
  18647. + */
  18648. +static ssize_t bussuspend_show(struct device *_dev,
  18649. + struct device_attribute *attr, char *buf)
  18650. +{
  18651. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18652. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  18653. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  18654. +}
  18655. +
  18656. +/**
  18657. + * Set the Bus Suspend status
  18658. + */
  18659. +static ssize_t bussuspend_store(struct device *_dev,
  18660. + struct device_attribute *attr,
  18661. + const char *buf, size_t count)
  18662. +{
  18663. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18664. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18665. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  18666. + return count;
  18667. +}
  18668. +
  18669. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  18670. +
  18671. +/**
  18672. + * Show the Mode Change Ready Timer status
  18673. + */
  18674. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  18675. + struct device_attribute *attr, char *buf)
  18676. +{
  18677. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18678. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  18679. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  18680. +}
  18681. +
  18682. +/**
  18683. + * Set the Mode Change Ready Timer status
  18684. + */
  18685. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  18686. + struct device_attribute *attr,
  18687. + const char *buf, size_t count)
  18688. +{
  18689. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18690. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18691. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  18692. + return count;
  18693. +}
  18694. +
  18695. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  18696. +
  18697. +/**
  18698. + * Show the value of HFIR Frame Interval bitfield
  18699. + */
  18700. +static ssize_t fr_interval_show(struct device *_dev,
  18701. + struct device_attribute *attr, char *buf)
  18702. +{
  18703. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18704. + return sprintf(buf, "Frame Interval = 0x%x\n",
  18705. + dwc_otg_get_fr_interval(otg_dev->core_if));
  18706. +}
  18707. +
  18708. +/**
  18709. + * Set the HFIR Frame Interval value
  18710. + */
  18711. +static ssize_t fr_interval_store(struct device *_dev,
  18712. + struct device_attribute *attr,
  18713. + const char *buf, size_t count)
  18714. +{
  18715. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18716. + uint32_t in = simple_strtoul(buf, NULL, 10);
  18717. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  18718. + return count;
  18719. +}
  18720. +
  18721. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  18722. +
  18723. +/**
  18724. + * Show the status of Remote Wakeup.
  18725. + */
  18726. +static ssize_t remote_wakeup_show(struct device *_dev,
  18727. + struct device_attribute *attr, char *buf)
  18728. +{
  18729. +#ifndef DWC_HOST_ONLY
  18730. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18731. +
  18732. + return sprintf(buf,
  18733. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  18734. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  18735. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  18736. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  18737. +#else
  18738. + return sprintf(buf, "Host Only Mode!\n");
  18739. +#endif /* DWC_HOST_ONLY */
  18740. +}
  18741. +
  18742. +/**
  18743. + * Initiate a remote wakeup of the host. The Device control register
  18744. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  18745. + * flag is set.
  18746. + *
  18747. + */
  18748. +static ssize_t remote_wakeup_store(struct device *_dev,
  18749. + struct device_attribute *attr,
  18750. + const char *buf, size_t count)
  18751. +{
  18752. +#ifndef DWC_HOST_ONLY
  18753. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18754. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18755. +
  18756. + if (val & 1) {
  18757. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  18758. + } else {
  18759. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  18760. + }
  18761. +#endif /* DWC_HOST_ONLY */
  18762. + return count;
  18763. +}
  18764. +
  18765. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  18766. + remote_wakeup_store);
  18767. +
  18768. +/**
  18769. + * Show the whether core is hibernated or not.
  18770. + */
  18771. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  18772. + struct device_attribute *attr, char *buf)
  18773. +{
  18774. +#ifndef DWC_HOST_ONLY
  18775. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18776. +
  18777. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  18778. + DWC_PRINTF("Core is in hibernation\n");
  18779. + } else {
  18780. + DWC_PRINTF("Core is not in hibernation\n");
  18781. + }
  18782. +#endif /* DWC_HOST_ONLY */
  18783. + return 0;
  18784. +}
  18785. +
  18786. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  18787. + int rem_wakeup, int reset);
  18788. +
  18789. +/**
  18790. + * Initiate a remote wakeup of the device to exit from hibernation.
  18791. + */
  18792. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  18793. + struct device_attribute *attr,
  18794. + const char *buf, size_t count)
  18795. +{
  18796. +#ifndef DWC_HOST_ONLY
  18797. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18798. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  18799. +#endif
  18800. + return count;
  18801. +}
  18802. +
  18803. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  18804. + rem_wakeup_pwrdn_store);
  18805. +
  18806. +static ssize_t disconnect_us(struct device *_dev,
  18807. + struct device_attribute *attr,
  18808. + const char *buf, size_t count)
  18809. +{
  18810. +
  18811. +#ifndef DWC_HOST_ONLY
  18812. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18813. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18814. + DWC_PRINTF("The Passed value is %04x\n", val);
  18815. +
  18816. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  18817. +
  18818. +#endif /* DWC_HOST_ONLY */
  18819. + return count;
  18820. +}
  18821. +
  18822. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  18823. +
  18824. +/**
  18825. + * Dump global registers and either host or device registers (depending on the
  18826. + * current mode of the core).
  18827. + */
  18828. +static ssize_t regdump_show(struct device *_dev,
  18829. + struct device_attribute *attr, char *buf)
  18830. +{
  18831. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18832. +
  18833. + dwc_otg_dump_global_registers(otg_dev->core_if);
  18834. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  18835. + dwc_otg_dump_host_registers(otg_dev->core_if);
  18836. + } else {
  18837. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  18838. +
  18839. + }
  18840. + return sprintf(buf, "Register Dump\n");
  18841. +}
  18842. +
  18843. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  18844. +
  18845. +/**
  18846. + * Dump global registers and either host or device registers (depending on the
  18847. + * current mode of the core).
  18848. + */
  18849. +static ssize_t spramdump_show(struct device *_dev,
  18850. + struct device_attribute *attr, char *buf)
  18851. +{
  18852. +#if 0
  18853. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18854. +
  18855. + dwc_otg_dump_spram(otg_dev->core_if);
  18856. +#endif
  18857. +
  18858. + return sprintf(buf, "SPRAM Dump\n");
  18859. +}
  18860. +
  18861. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  18862. +
  18863. +/**
  18864. + * Dump the current hcd state.
  18865. + */
  18866. +static ssize_t hcddump_show(struct device *_dev,
  18867. + struct device_attribute *attr, char *buf)
  18868. +{
  18869. +#ifndef DWC_DEVICE_ONLY
  18870. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18871. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  18872. +#endif /* DWC_DEVICE_ONLY */
  18873. + return sprintf(buf, "HCD Dump\n");
  18874. +}
  18875. +
  18876. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  18877. +
  18878. +/**
  18879. + * Dump the average frame remaining at SOF. This can be used to
  18880. + * determine average interrupt latency. Frame remaining is also shown for
  18881. + * start transfer and two additional sample points.
  18882. + */
  18883. +static ssize_t hcd_frrem_show(struct device *_dev,
  18884. + struct device_attribute *attr, char *buf)
  18885. +{
  18886. +#ifndef DWC_DEVICE_ONLY
  18887. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18888. +
  18889. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  18890. +#endif /* DWC_DEVICE_ONLY */
  18891. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  18892. +}
  18893. +
  18894. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  18895. +
  18896. +/**
  18897. + * Displays the time required to read the GNPTXFSIZ register many times (the
  18898. + * output shows the number of times the register is read).
  18899. + */
  18900. +#define RW_REG_COUNT 10000000
  18901. +#define MSEC_PER_JIFFIE 1000/HZ
  18902. +static ssize_t rd_reg_test_show(struct device *_dev,
  18903. + struct device_attribute *attr, char *buf)
  18904. +{
  18905. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18906. + int i;
  18907. + int time;
  18908. + int start_jiffies;
  18909. +
  18910. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  18911. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  18912. + start_jiffies = jiffies;
  18913. + for (i = 0; i < RW_REG_COUNT; i++) {
  18914. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  18915. + }
  18916. + time = jiffies - start_jiffies;
  18917. + return sprintf(buf,
  18918. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  18919. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  18920. +}
  18921. +
  18922. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  18923. +
  18924. +/**
  18925. + * Displays the time required to write the GNPTXFSIZ register many times (the
  18926. + * output shows the number of times the register is written).
  18927. + */
  18928. +static ssize_t wr_reg_test_show(struct device *_dev,
  18929. + struct device_attribute *attr, char *buf)
  18930. +{
  18931. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18932. + uint32_t reg_val;
  18933. + int i;
  18934. + int time;
  18935. + int start_jiffies;
  18936. +
  18937. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  18938. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  18939. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  18940. + start_jiffies = jiffies;
  18941. + for (i = 0; i < RW_REG_COUNT; i++) {
  18942. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  18943. + }
  18944. + time = jiffies - start_jiffies;
  18945. + return sprintf(buf,
  18946. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  18947. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  18948. +}
  18949. +
  18950. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  18951. +
  18952. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18953. +
  18954. +/**
  18955. +* Show the lpm_response attribute.
  18956. +*/
  18957. +static ssize_t lpmresp_show(struct device *_dev,
  18958. + struct device_attribute *attr, char *buf)
  18959. +{
  18960. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18961. +
  18962. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  18963. + return sprintf(buf, "** LPM is DISABLED **\n");
  18964. +
  18965. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  18966. + return sprintf(buf, "** Current mode is not device mode\n");
  18967. + }
  18968. + return sprintf(buf, "lpm_response = %d\n",
  18969. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  18970. +}
  18971. +
  18972. +/**
  18973. +* Store the lpm_response attribute.
  18974. +*/
  18975. +static ssize_t lpmresp_store(struct device *_dev,
  18976. + struct device_attribute *attr,
  18977. + const char *buf, size_t count)
  18978. +{
  18979. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18980. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18981. +
  18982. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  18983. + return 0;
  18984. + }
  18985. +
  18986. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  18987. + return 0;
  18988. + }
  18989. +
  18990. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  18991. + return count;
  18992. +}
  18993. +
  18994. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  18995. +
  18996. +/**
  18997. +* Show the sleep_status attribute.
  18998. +*/
  18999. +static ssize_t sleepstatus_show(struct device *_dev,
  19000. + struct device_attribute *attr, char *buf)
  19001. +{
  19002. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  19003. + return sprintf(buf, "Sleep Status = %d\n",
  19004. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  19005. +}
  19006. +
  19007. +/**
  19008. + * Store the sleep_status attribure.
  19009. + */
  19010. +static ssize_t sleepstatus_store(struct device *_dev,
  19011. + struct device_attribute *attr,
  19012. + const char *buf, size_t count)
  19013. +{
  19014. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  19015. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  19016. +
  19017. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  19018. + if (dwc_otg_is_host_mode(core_if)) {
  19019. +
  19020. + DWC_PRINTF("Host initiated resume\n");
  19021. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  19022. + }
  19023. + }
  19024. +
  19025. + return count;
  19026. +}
  19027. +
  19028. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  19029. + sleepstatus_store);
  19030. +
  19031. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  19032. +
  19033. +/**@}*/
  19034. +
  19035. +/**
  19036. + * Create the device files
  19037. + */
  19038. +void dwc_otg_attr_create(
  19039. +#ifdef LM_INTERFACE
  19040. + struct lm_device *dev
  19041. +#elif defined(PCI_INTERFACE)
  19042. + struct pci_dev *dev
  19043. +#elif defined(PLATFORM_INTERFACE)
  19044. + struct platform_device *dev
  19045. +#endif
  19046. + )
  19047. +{
  19048. + int error;
  19049. +
  19050. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  19051. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  19052. + error = device_create_file(&dev->dev, &dev_attr_mode);
  19053. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  19054. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  19055. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  19056. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  19057. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  19058. + error = device_create_file(&dev->dev, &dev_attr_srp);
  19059. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  19060. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  19061. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  19062. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  19063. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  19064. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  19065. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  19066. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  19067. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  19068. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  19069. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  19070. + error = device_create_file(&dev->dev, &dev_attr_guid);
  19071. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  19072. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  19073. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  19074. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  19075. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  19076. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  19077. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  19078. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  19079. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  19080. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  19081. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  19082. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  19083. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  19084. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  19085. +#ifdef CONFIG_USB_DWC_OTG_LPM
  19086. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  19087. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  19088. +#endif
  19089. +}
  19090. +
  19091. +/**
  19092. + * Remove the device files
  19093. + */
  19094. +void dwc_otg_attr_remove(
  19095. +#ifdef LM_INTERFACE
  19096. + struct lm_device *dev
  19097. +#elif defined(PCI_INTERFACE)
  19098. + struct pci_dev *dev
  19099. +#elif defined(PLATFORM_INTERFACE)
  19100. + struct platform_device *dev
  19101. +#endif
  19102. + )
  19103. +{
  19104. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  19105. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  19106. + device_remove_file(&dev->dev, &dev_attr_mode);
  19107. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  19108. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  19109. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  19110. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  19111. + device_remove_file(&dev->dev, &dev_attr_hnp);
  19112. + device_remove_file(&dev->dev, &dev_attr_srp);
  19113. + device_remove_file(&dev->dev, &dev_attr_buspower);
  19114. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  19115. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  19116. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  19117. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  19118. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  19119. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  19120. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  19121. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  19122. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  19123. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  19124. + device_remove_file(&dev->dev, &dev_attr_guid);
  19125. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  19126. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  19127. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  19128. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  19129. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  19130. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  19131. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  19132. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  19133. + device_remove_file(&dev->dev, &dev_attr_regdump);
  19134. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  19135. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  19136. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  19137. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  19138. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  19139. +#ifdef CONFIG_USB_DWC_OTG_LPM
  19140. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  19141. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  19142. +#endif
  19143. +}
  19144. --- /dev/null
  19145. +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  19146. @@ -0,0 +1,89 @@
  19147. +/* ==========================================================================
  19148. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  19149. + * $Revision: #13 $
  19150. + * $Date: 2010/06/21 $
  19151. + * $Change: 1532021 $
  19152. + *
  19153. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  19154. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  19155. + * otherwise expressly agreed to in writing between Synopsys and you.
  19156. + *
  19157. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  19158. + * any End User Software License Agreement or Agreement for Licensed Product
  19159. + * with Synopsys or any supplement thereto. You are permitted to use and
  19160. + * redistribute this Software in source and binary forms, with or without
  19161. + * modification, provided that redistributions of source code must retain this
  19162. + * notice. You may not view, use, disclose, copy or distribute this file or
  19163. + * any information contained herein except pursuant to this license grant from
  19164. + * Synopsys. If you do not agree with this notice, including the disclaimer
  19165. + * below, then you are not authorized to use the Software.
  19166. + *
  19167. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  19168. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19169. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19170. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  19171. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19172. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19173. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19174. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19175. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19176. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  19177. + * DAMAGE.
  19178. + * ========================================================================== */
  19179. +
  19180. +#if !defined(__DWC_OTG_ATTR_H__)
  19181. +#define __DWC_OTG_ATTR_H__
  19182. +
  19183. +/** @file
  19184. + * This file contains the interface to the Linux device attributes.
  19185. + */
  19186. +extern struct device_attribute dev_attr_regoffset;
  19187. +extern struct device_attribute dev_attr_regvalue;
  19188. +
  19189. +extern struct device_attribute dev_attr_mode;
  19190. +extern struct device_attribute dev_attr_hnpcapable;
  19191. +extern struct device_attribute dev_attr_srpcapable;
  19192. +extern struct device_attribute dev_attr_hnp;
  19193. +extern struct device_attribute dev_attr_srp;
  19194. +extern struct device_attribute dev_attr_buspower;
  19195. +extern struct device_attribute dev_attr_bussuspend;
  19196. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  19197. +extern struct device_attribute dev_attr_fr_interval;
  19198. +extern struct device_attribute dev_attr_busconnected;
  19199. +extern struct device_attribute dev_attr_gotgctl;
  19200. +extern struct device_attribute dev_attr_gusbcfg;
  19201. +extern struct device_attribute dev_attr_grxfsiz;
  19202. +extern struct device_attribute dev_attr_gnptxfsiz;
  19203. +extern struct device_attribute dev_attr_gpvndctl;
  19204. +extern struct device_attribute dev_attr_ggpio;
  19205. +extern struct device_attribute dev_attr_guid;
  19206. +extern struct device_attribute dev_attr_gsnpsid;
  19207. +extern struct device_attribute dev_attr_devspeed;
  19208. +extern struct device_attribute dev_attr_enumspeed;
  19209. +extern struct device_attribute dev_attr_hptxfsiz;
  19210. +extern struct device_attribute dev_attr_hprt0;
  19211. +#ifdef CONFIG_USB_DWC_OTG_LPM
  19212. +extern struct device_attribute dev_attr_lpm_response;
  19213. +extern struct device_attribute devi_attr_sleep_status;
  19214. +#endif
  19215. +
  19216. +void dwc_otg_attr_create(
  19217. +#ifdef LM_INTERFACE
  19218. + struct lm_device *dev
  19219. +#elif defined(PCI_INTERFACE)
  19220. + struct pci_dev *dev
  19221. +#elif defined(PLATFORM_INTERFACE)
  19222. + struct platform_device *dev
  19223. +#endif
  19224. + );
  19225. +
  19226. +void dwc_otg_attr_remove(
  19227. +#ifdef LM_INTERFACE
  19228. + struct lm_device *dev
  19229. +#elif defined(PCI_INTERFACE)
  19230. + struct pci_dev *dev
  19231. +#elif defined(PLATFORM_INTERFACE)
  19232. + struct platform_device *dev
  19233. +#endif
  19234. + );
  19235. +#endif
  19236. --- /dev/null
  19237. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  19238. @@ -0,0 +1,1876 @@
  19239. +/* ==========================================================================
  19240. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  19241. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  19242. + * otherwise expressly agreed to in writing between Synopsys and you.
  19243. + *
  19244. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  19245. + * any End User Software License Agreement or Agreement for Licensed Product
  19246. + * with Synopsys or any supplement thereto. You are permitted to use and
  19247. + * redistribute this Software in source and binary forms, with or without
  19248. + * modification, provided that redistributions of source code must retain this
  19249. + * notice. You may not view, use, disclose, copy or distribute this file or
  19250. + * any information contained herein except pursuant to this license grant from
  19251. + * Synopsys. If you do not agree with this notice, including the disclaimer
  19252. + * below, then you are not authorized to use the Software.
  19253. + *
  19254. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  19255. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19256. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19257. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  19258. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19259. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19260. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19261. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19262. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19263. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  19264. + * DAMAGE.
  19265. + * ========================================================================== */
  19266. +
  19267. +/** @file
  19268. + *
  19269. + * This file contains the most of the CFI(Core Feature Interface)
  19270. + * implementation for the OTG.
  19271. + */
  19272. +
  19273. +#ifdef DWC_UTE_CFI
  19274. +
  19275. +#include "dwc_otg_pcd.h"
  19276. +#include "dwc_otg_cfi.h"
  19277. +
  19278. +/** This definition should actually migrate to the Portability Library */
  19279. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  19280. +
  19281. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  19282. +
  19283. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  19284. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  19285. + struct dwc_otg_pcd *pcd,
  19286. + struct cfi_usb_ctrlrequest *ctrl_req);
  19287. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  19288. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19289. + struct cfi_usb_ctrlrequest *req);
  19290. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19291. + struct cfi_usb_ctrlrequest *req);
  19292. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19293. + struct cfi_usb_ctrlrequest *req);
  19294. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  19295. + struct cfi_usb_ctrlrequest *req);
  19296. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  19297. +
  19298. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  19299. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  19300. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  19301. +
  19302. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  19303. +
  19304. +/** This is the header of the all features descriptor */
  19305. +static cfi_all_features_header_t all_props_desc_header = {
  19306. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  19307. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  19308. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  19309. +};
  19310. +
  19311. +/** This is an array of statically allocated feature descriptors */
  19312. +static cfi_feature_desc_header_t prop_descs[] = {
  19313. +
  19314. + /* FT_ID_DMA_MODE */
  19315. + {
  19316. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  19317. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19318. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  19319. + },
  19320. +
  19321. + /* FT_ID_DMA_BUFFER_SETUP */
  19322. + {
  19323. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  19324. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19325. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19326. + },
  19327. +
  19328. + /* FT_ID_DMA_BUFF_ALIGN */
  19329. + {
  19330. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  19331. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19332. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19333. + },
  19334. +
  19335. + /* FT_ID_DMA_CONCAT_SETUP */
  19336. + {
  19337. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  19338. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19339. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19340. + },
  19341. +
  19342. + /* FT_ID_DMA_CIRCULAR */
  19343. + {
  19344. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  19345. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19346. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19347. + },
  19348. +
  19349. + /* FT_ID_THRESHOLD_SETUP */
  19350. + {
  19351. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  19352. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19353. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19354. + },
  19355. +
  19356. + /* FT_ID_DFIFO_DEPTH */
  19357. + {
  19358. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  19359. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  19360. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19361. + },
  19362. +
  19363. + /* FT_ID_TX_FIFO_DEPTH */
  19364. + {
  19365. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  19366. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19367. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19368. + },
  19369. +
  19370. + /* FT_ID_RX_FIFO_DEPTH */
  19371. + {
  19372. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  19373. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19374. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19375. + }
  19376. +};
  19377. +
  19378. +/** The table of feature names */
  19379. +cfi_string_t prop_name_table[] = {
  19380. + {FT_ID_DMA_MODE, "dma_mode"},
  19381. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  19382. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  19383. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  19384. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  19385. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  19386. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  19387. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  19388. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  19389. + {}
  19390. +};
  19391. +
  19392. +/************************************************************************/
  19393. +
  19394. +/**
  19395. + * Returns the name of the feature by its ID
  19396. + * or NULL if no featute ID matches.
  19397. + *
  19398. + */
  19399. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  19400. +{
  19401. + cfi_string_t *pstr;
  19402. + *len = 0;
  19403. +
  19404. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  19405. + if (pstr->id == prop_id) {
  19406. + *len = DWC_STRLEN(pstr->s);
  19407. + return pstr->s;
  19408. + }
  19409. + }
  19410. + return NULL;
  19411. +}
  19412. +
  19413. +/**
  19414. + * This function handles all CFI specific control requests.
  19415. + *
  19416. + * Return a negative value to stall the DCE.
  19417. + */
  19418. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  19419. +{
  19420. + int retval = 0;
  19421. + dwc_otg_pcd_ep_t *ep = NULL;
  19422. + cfiobject_t *cfi = pcd->cfi;
  19423. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  19424. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  19425. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  19426. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  19427. + uint32_t regaddr = 0;
  19428. + uint32_t regval = 0;
  19429. +
  19430. + /* Save this Control Request in the CFI object.
  19431. + * The data field will be assigned in the data stage completion CB function.
  19432. + */
  19433. + cfi->ctrl_req = *ctrl;
  19434. + cfi->ctrl_req.data = NULL;
  19435. +
  19436. + cfi->need_gadget_att = 0;
  19437. + cfi->need_status_in_complete = 0;
  19438. +
  19439. + switch (ctrl->bRequest) {
  19440. + case VEN_CORE_GET_FEATURES:
  19441. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  19442. + if (retval >= 0) {
  19443. + //dump_msg(cfi->buf_in.buf, retval);
  19444. + ep = &pcd->ep0;
  19445. +
  19446. + retval = min((uint16_t) retval, wLen);
  19447. + /* Transfer this buffer to the host through the EP0-IN EP */
  19448. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19449. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19450. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19451. + ep->dwc_ep.xfer_len = retval;
  19452. + ep->dwc_ep.xfer_count = 0;
  19453. + ep->dwc_ep.sent_zlp = 0;
  19454. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19455. +
  19456. + pcd->ep0_pending = 1;
  19457. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19458. + }
  19459. + retval = 0;
  19460. + break;
  19461. +
  19462. + case VEN_CORE_GET_FEATURE:
  19463. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  19464. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  19465. + pcd, ctrl);
  19466. + if (retval >= 0) {
  19467. + ep = &pcd->ep0;
  19468. +
  19469. + retval = min((uint16_t) retval, wLen);
  19470. + /* Transfer this buffer to the host through the EP0-IN EP */
  19471. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19472. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19473. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19474. + ep->dwc_ep.xfer_len = retval;
  19475. + ep->dwc_ep.xfer_count = 0;
  19476. + ep->dwc_ep.sent_zlp = 0;
  19477. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19478. +
  19479. + pcd->ep0_pending = 1;
  19480. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19481. + }
  19482. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  19483. + dump_msg(cfi->buf_in.buf, retval);
  19484. + break;
  19485. +
  19486. + case VEN_CORE_SET_FEATURE:
  19487. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  19488. + /* Set up an XFER to get the data stage of the control request,
  19489. + * which is the new value of the feature to be modified.
  19490. + */
  19491. + ep = &pcd->ep0;
  19492. + ep->dwc_ep.is_in = 0;
  19493. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  19494. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  19495. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  19496. + ep->dwc_ep.xfer_len = wLen;
  19497. + ep->dwc_ep.xfer_count = 0;
  19498. + ep->dwc_ep.sent_zlp = 0;
  19499. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19500. +
  19501. + pcd->ep0_pending = 1;
  19502. + /* Read the control write's data stage */
  19503. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19504. + retval = 0;
  19505. + break;
  19506. +
  19507. + case VEN_CORE_RESET_FEATURES:
  19508. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  19509. + cfi->need_gadget_att = 1;
  19510. + cfi->need_status_in_complete = 1;
  19511. + retval = cfi_preproc_reset(pcd, ctrl);
  19512. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  19513. + break;
  19514. +
  19515. + case VEN_CORE_ACTIVATE_FEATURES:
  19516. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  19517. + break;
  19518. +
  19519. + case VEN_CORE_READ_REGISTER:
  19520. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  19521. + /* wValue optionally contains the HI WORD of the register offset and
  19522. + * wIndex contains the LOW WORD of the register offset
  19523. + */
  19524. + if (wValue == 0) {
  19525. + /* @TODO - MAS - fix the access to the base field */
  19526. + regaddr = 0;
  19527. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  19528. + //GET_CORE_IF(pcd)->co
  19529. + regaddr |= wIndex;
  19530. + } else {
  19531. + regaddr = (wValue << 16) | wIndex;
  19532. + }
  19533. +
  19534. + /* Read a 32-bit value of the memory at the regaddr */
  19535. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  19536. +
  19537. + ep = &pcd->ep0;
  19538. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  19539. + ep->dwc_ep.is_in = 1;
  19540. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19541. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19542. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19543. + ep->dwc_ep.xfer_len = wLen;
  19544. + ep->dwc_ep.xfer_count = 0;
  19545. + ep->dwc_ep.sent_zlp = 0;
  19546. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19547. +
  19548. + pcd->ep0_pending = 1;
  19549. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19550. + cfi->need_gadget_att = 0;
  19551. + retval = 0;
  19552. + break;
  19553. +
  19554. + case VEN_CORE_WRITE_REGISTER:
  19555. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  19556. + /* Set up an XFER to get the data stage of the control request,
  19557. + * which is the new value of the register to be modified.
  19558. + */
  19559. + ep = &pcd->ep0;
  19560. + ep->dwc_ep.is_in = 0;
  19561. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  19562. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  19563. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  19564. + ep->dwc_ep.xfer_len = wLen;
  19565. + ep->dwc_ep.xfer_count = 0;
  19566. + ep->dwc_ep.sent_zlp = 0;
  19567. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19568. +
  19569. + pcd->ep0_pending = 1;
  19570. + /* Read the control write's data stage */
  19571. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19572. + retval = 0;
  19573. + break;
  19574. +
  19575. + default:
  19576. + retval = -DWC_E_NOT_SUPPORTED;
  19577. + break;
  19578. + }
  19579. +
  19580. + return retval;
  19581. +}
  19582. +
  19583. +/**
  19584. + * This function prepares the core features descriptors and copies its
  19585. + * raw representation into the buffer <buf>.
  19586. + *
  19587. + * The buffer structure is as follows:
  19588. + * all_features_header (8 bytes)
  19589. + * features_#1 (8 bytes + feature name string length)
  19590. + * features_#2 (8 bytes + feature name string length)
  19591. + * .....
  19592. + * features_#n - where n=the total count of feature descriptors
  19593. + */
  19594. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  19595. +{
  19596. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  19597. + cfi_feature_desc_header_t *prop;
  19598. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  19599. + cfi_all_features_header_t *tmp;
  19600. + uint8_t *tmpbuf = buf;
  19601. + const uint8_t *pname = NULL;
  19602. + int i, j, namelen = 0, totlen;
  19603. +
  19604. + /* Prepare and copy the core features into the buffer */
  19605. + CFI_INFO("%s:\n", __func__);
  19606. +
  19607. + tmp = (cfi_all_features_header_t *) tmpbuf;
  19608. + *tmp = *all_props_hdr;
  19609. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  19610. +
  19611. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  19612. + for (i = 0; i < j; i++, prop_hdr++) {
  19613. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  19614. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  19615. + *prop = *prop_hdr;
  19616. +
  19617. + prop->bNameLen = namelen;
  19618. + prop->wLength =
  19619. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  19620. + namelen);
  19621. +
  19622. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  19623. + dwc_memcpy(tmpbuf, pname, namelen);
  19624. + tmpbuf += namelen;
  19625. + }
  19626. +
  19627. + totlen = tmpbuf - buf;
  19628. +
  19629. + if (totlen > 0) {
  19630. + tmp = (cfi_all_features_header_t *) buf;
  19631. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  19632. + }
  19633. +
  19634. + return totlen;
  19635. +}
  19636. +
  19637. +/**
  19638. + * This function releases all the dynamic memory in the CFI object.
  19639. + */
  19640. +static void cfi_release(cfiobject_t * cfiobj)
  19641. +{
  19642. + cfi_ep_t *cfiep;
  19643. + dwc_list_link_t *tmp;
  19644. +
  19645. + CFI_INFO("%s\n", __func__);
  19646. +
  19647. + if (cfiobj->buf_in.buf) {
  19648. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  19649. + cfiobj->buf_in.addr);
  19650. + cfiobj->buf_in.buf = NULL;
  19651. + }
  19652. +
  19653. + if (cfiobj->buf_out.buf) {
  19654. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  19655. + cfiobj->buf_out.addr);
  19656. + cfiobj->buf_out.buf = NULL;
  19657. + }
  19658. +
  19659. + /* Free the Buffer Setup values for each EP */
  19660. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  19661. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  19662. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  19663. + cfi_free_ep_bs_dyn_data(cfiep);
  19664. + }
  19665. +}
  19666. +
  19667. +/**
  19668. + * This function frees the dynamically allocated EP buffer setup data.
  19669. + */
  19670. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  19671. +{
  19672. + if (cfiep->bm_sg) {
  19673. + DWC_FREE(cfiep->bm_sg);
  19674. + cfiep->bm_sg = NULL;
  19675. + }
  19676. +
  19677. + if (cfiep->bm_align) {
  19678. + DWC_FREE(cfiep->bm_align);
  19679. + cfiep->bm_align = NULL;
  19680. + }
  19681. +
  19682. + if (cfiep->bm_concat) {
  19683. + if (NULL != cfiep->bm_concat->wTxBytes) {
  19684. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  19685. + cfiep->bm_concat->wTxBytes = NULL;
  19686. + }
  19687. + DWC_FREE(cfiep->bm_concat);
  19688. + cfiep->bm_concat = NULL;
  19689. + }
  19690. +}
  19691. +
  19692. +/**
  19693. + * This function initializes the default values of the features
  19694. + * for a specific endpoint and should be called only once when
  19695. + * the EP is enabled first time.
  19696. + */
  19697. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  19698. +{
  19699. + int retval = 0;
  19700. +
  19701. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  19702. + if (NULL == cfiep->bm_sg) {
  19703. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  19704. + return -DWC_E_NO_MEMORY;
  19705. + }
  19706. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  19707. +
  19708. + /* For the Concatenation feature's default value we do not allocate
  19709. + * memory for the wTxBytes field - it will be done in the set_feature_value
  19710. + * request handler.
  19711. + */
  19712. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  19713. + if (NULL == cfiep->bm_concat) {
  19714. + CFI_INFO
  19715. + ("Failed to allocate memory for CONCATENATION feature value\n");
  19716. + DWC_FREE(cfiep->bm_sg);
  19717. + return -DWC_E_NO_MEMORY;
  19718. + }
  19719. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  19720. +
  19721. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  19722. + if (NULL == cfiep->bm_align) {
  19723. + CFI_INFO
  19724. + ("Failed to allocate memory for Alignment feature value\n");
  19725. + DWC_FREE(cfiep->bm_sg);
  19726. + DWC_FREE(cfiep->bm_concat);
  19727. + return -DWC_E_NO_MEMORY;
  19728. + }
  19729. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  19730. +
  19731. + return retval;
  19732. +}
  19733. +
  19734. +/**
  19735. + * The callback function that notifies the CFI on the activation of
  19736. + * an endpoint in the PCD. The following steps are done in this function:
  19737. + *
  19738. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  19739. + * active endpoint)
  19740. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  19741. + * Set the Buffer Mode to standard
  19742. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  19743. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  19744. + */
  19745. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  19746. + struct dwc_otg_pcd_ep *ep)
  19747. +{
  19748. + cfi_ep_t *cfiep;
  19749. + int retval = -DWC_E_NOT_SUPPORTED;
  19750. +
  19751. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  19752. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  19753. + /* MAS - Check whether this endpoint already is in the list */
  19754. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  19755. +
  19756. + if (NULL == cfiep) {
  19757. + /* Allocate a cfi_ep_t object */
  19758. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  19759. + if (NULL == cfiep) {
  19760. + CFI_INFO
  19761. + ("Unable to allocate memory for <cfiep> in function %s\n",
  19762. + __func__);
  19763. + return -DWC_E_NO_MEMORY;
  19764. + }
  19765. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  19766. +
  19767. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  19768. + cfiep->ep = ep;
  19769. +
  19770. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  19771. + ep->dwc_ep.descs =
  19772. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  19773. + sizeof(dwc_otg_dma_desc_t),
  19774. + &ep->dwc_ep.descs_dma_addr);
  19775. +
  19776. + if (NULL == ep->dwc_ep.descs) {
  19777. + DWC_FREE(cfiep);
  19778. + return -DWC_E_NO_MEMORY;
  19779. + }
  19780. +
  19781. + DWC_LIST_INIT(&cfiep->lh);
  19782. +
  19783. + /* Set the buffer mode to BM_STANDARD. It will be modified
  19784. + * when building descriptors for a specific buffer mode */
  19785. + ep->dwc_ep.buff_mode = BM_STANDARD;
  19786. +
  19787. + /* Create and initialize the default values for this EP's Buffer modes */
  19788. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  19789. + return retval;
  19790. +
  19791. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  19792. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  19793. + retval = 0;
  19794. + } else { /* The sought EP already is in the list */
  19795. + CFI_INFO("%s: The sought EP already is in the list\n",
  19796. + __func__);
  19797. + }
  19798. +
  19799. + return retval;
  19800. +}
  19801. +
  19802. +/**
  19803. + * This function is called when the data stage of a 3-stage Control Write request
  19804. + * is complete.
  19805. + *
  19806. + */
  19807. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  19808. + struct dwc_otg_pcd *pcd)
  19809. +{
  19810. + uint32_t addr, reg_value;
  19811. + uint16_t wIndex, wValue;
  19812. + uint8_t bRequest;
  19813. + uint8_t *buf = cfi->buf_out.buf;
  19814. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  19815. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  19816. + int retval = -DWC_E_NOT_SUPPORTED;
  19817. +
  19818. + CFI_INFO("%s\n", __func__);
  19819. +
  19820. + bRequest = ctrl_req->bRequest;
  19821. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  19822. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  19823. +
  19824. + /*
  19825. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  19826. + * The request should be already saved in the command stage by now.
  19827. + */
  19828. + ctrl_req->data = cfi->buf_out.buf;
  19829. + cfi->need_status_in_complete = 0;
  19830. + cfi->need_gadget_att = 0;
  19831. +
  19832. + switch (bRequest) {
  19833. + case VEN_CORE_WRITE_REGISTER:
  19834. + /* The buffer contains raw data of the new value for the register */
  19835. + reg_value = *((uint32_t *) buf);
  19836. + if (wValue == 0) {
  19837. + addr = 0;
  19838. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  19839. + addr += wIndex;
  19840. + } else {
  19841. + addr = (wValue << 16) | wIndex;
  19842. + }
  19843. +
  19844. + //writel(reg_value, addr);
  19845. +
  19846. + retval = 0;
  19847. + cfi->need_status_in_complete = 1;
  19848. + break;
  19849. +
  19850. + case VEN_CORE_SET_FEATURE:
  19851. + /* The buffer contains raw data of the new value of the feature */
  19852. + retval = cfi_set_feature_value(pcd);
  19853. + if (retval < 0)
  19854. + return retval;
  19855. +
  19856. + cfi->need_status_in_complete = 1;
  19857. + break;
  19858. +
  19859. + default:
  19860. + break;
  19861. + }
  19862. +
  19863. + return retval;
  19864. +}
  19865. +
  19866. +/**
  19867. + * This function builds the DMA descriptors for the SG buffer mode.
  19868. + */
  19869. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19870. + dwc_otg_pcd_request_t * req)
  19871. +{
  19872. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19873. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  19874. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19875. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  19876. + dma_addr_t buff_addr = req->dma;
  19877. + int i;
  19878. + uint32_t txsize, off;
  19879. +
  19880. + txsize = sgval->wSize;
  19881. + off = sgval->bOffset;
  19882. +
  19883. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  19884. +// __func__, cfiep->ep->ep.name, txsize, off);
  19885. +
  19886. + for (i = 0; i < sgval->bCount; i++) {
  19887. + desc->status.b.bs = BS_HOST_BUSY;
  19888. + desc->buf = buff_addr;
  19889. + desc->status.b.l = 0;
  19890. + desc->status.b.ioc = 0;
  19891. + desc->status.b.sp = 0;
  19892. + desc->status.b.bytes = txsize;
  19893. + desc->status.b.bs = BS_HOST_READY;
  19894. +
  19895. + /* Set the next address of the buffer */
  19896. + buff_addr += txsize + off;
  19897. + desc_last = desc;
  19898. + desc++;
  19899. + }
  19900. +
  19901. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  19902. + desc_last->status.b.l = 1;
  19903. + desc_last->status.b.ioc = 1;
  19904. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  19905. + /* Save the last DMA descriptor pointer */
  19906. + cfiep->dma_desc_last = desc_last;
  19907. + cfiep->desc_count = sgval->bCount;
  19908. +}
  19909. +
  19910. +/**
  19911. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  19912. + */
  19913. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19914. + dwc_otg_pcd_request_t * req)
  19915. +{
  19916. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19917. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  19918. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19919. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  19920. + dma_addr_t buff_addr = req->dma;
  19921. + int i;
  19922. + uint16_t *txsize;
  19923. +
  19924. + txsize = concatval->wTxBytes;
  19925. +
  19926. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  19927. + desc->buf = buff_addr;
  19928. + desc->status.b.bs = BS_HOST_BUSY;
  19929. + desc->status.b.l = 0;
  19930. + desc->status.b.ioc = 0;
  19931. + desc->status.b.sp = 0;
  19932. + desc->status.b.bytes = *txsize;
  19933. + desc->status.b.bs = BS_HOST_READY;
  19934. +
  19935. + txsize++;
  19936. + /* Set the next address of the buffer */
  19937. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  19938. + desc_last = desc;
  19939. + desc++;
  19940. + }
  19941. +
  19942. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  19943. + desc_last->status.b.l = 1;
  19944. + desc_last->status.b.ioc = 1;
  19945. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  19946. + cfiep->dma_desc_last = desc_last;
  19947. + cfiep->desc_count = concatval->hdr.bDescCount;
  19948. +}
  19949. +
  19950. +/**
  19951. + * This function builds the DMA descriptors for the Circular buffer mode
  19952. + */
  19953. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19954. + dwc_otg_pcd_request_t * req)
  19955. +{
  19956. + /* @todo: MAS - add implementation when this feature needs to be tested */
  19957. +}
  19958. +
  19959. +/**
  19960. + * This function builds the DMA descriptors for the Alignment buffer mode
  19961. + */
  19962. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19963. + dwc_otg_pcd_request_t * req)
  19964. +{
  19965. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19966. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  19967. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19968. + dma_addr_t buff_addr = req->dma;
  19969. +
  19970. + desc->status.b.bs = BS_HOST_BUSY;
  19971. + desc->status.b.l = 1;
  19972. + desc->status.b.ioc = 1;
  19973. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  19974. + desc->status.b.bytes = req->length;
  19975. + /* Adjust the buffer alignment */
  19976. + desc->buf = (buff_addr + alignval->bAlign);
  19977. + desc->status.b.bs = BS_HOST_READY;
  19978. + cfiep->dma_desc_last = desc;
  19979. + cfiep->desc_count = 1;
  19980. +}
  19981. +
  19982. +/**
  19983. + * This function builds the DMA descriptors chain for different modes of the
  19984. + * buffer setup of an endpoint.
  19985. + */
  19986. +static void cfi_build_descriptors(struct cfiobject *cfi,
  19987. + struct dwc_otg_pcd *pcd,
  19988. + struct dwc_otg_pcd_ep *ep,
  19989. + dwc_otg_pcd_request_t * req)
  19990. +{
  19991. + cfi_ep_t *cfiep;
  19992. +
  19993. + /* Get the cfiep by the dwc_otg_pcd_ep */
  19994. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  19995. + if (NULL == cfiep) {
  19996. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  19997. + __func__);
  19998. + return;
  19999. + }
  20000. +
  20001. + cfiep->xfer_len = req->length;
  20002. +
  20003. + /* Iterate through all the DMA descriptors */
  20004. + switch (cfiep->ep->dwc_ep.buff_mode) {
  20005. + case BM_SG:
  20006. + cfi_build_sg_descs(cfi, cfiep, req);
  20007. + break;
  20008. +
  20009. + case BM_CONCAT:
  20010. + cfi_build_concat_descs(cfi, cfiep, req);
  20011. + break;
  20012. +
  20013. + case BM_CIRCULAR:
  20014. + cfi_build_circ_descs(cfi, cfiep, req);
  20015. + break;
  20016. +
  20017. + case BM_ALIGN:
  20018. + cfi_build_align_descs(cfi, cfiep, req);
  20019. + break;
  20020. +
  20021. + default:
  20022. + break;
  20023. + }
  20024. +}
  20025. +
  20026. +/**
  20027. + * Allocate DMA buffer for different Buffer modes.
  20028. + */
  20029. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  20030. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  20031. + unsigned size, gfp_t flags)
  20032. +{
  20033. + return DWC_DMA_ALLOC(size, dma);
  20034. +}
  20035. +
  20036. +/**
  20037. + * This function initializes the CFI object.
  20038. + */
  20039. +int init_cfi(cfiobject_t * cfiobj)
  20040. +{
  20041. + CFI_INFO("%s\n", __func__);
  20042. +
  20043. + /* Allocate a buffer for IN XFERs */
  20044. + cfiobj->buf_in.buf =
  20045. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  20046. + if (NULL == cfiobj->buf_in.buf) {
  20047. + CFI_INFO("Unable to allocate buffer for INs\n");
  20048. + return -DWC_E_NO_MEMORY;
  20049. + }
  20050. +
  20051. + /* Allocate a buffer for OUT XFERs */
  20052. + cfiobj->buf_out.buf =
  20053. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  20054. + if (NULL == cfiobj->buf_out.buf) {
  20055. + CFI_INFO("Unable to allocate buffer for OUT\n");
  20056. + return -DWC_E_NO_MEMORY;
  20057. + }
  20058. +
  20059. + /* Initialize the callback function pointers */
  20060. + cfiobj->ops.release = cfi_release;
  20061. + cfiobj->ops.ep_enable = cfi_ep_enable;
  20062. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  20063. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  20064. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  20065. +
  20066. + /* Initialize the list of active endpoints in the CFI object */
  20067. + DWC_LIST_INIT(&cfiobj->active_eps);
  20068. +
  20069. + return 0;
  20070. +}
  20071. +
  20072. +/**
  20073. + * This function reads the required feature's current value into the buffer
  20074. + *
  20075. + * @retval: Returns negative as error, or the data length of the feature
  20076. + */
  20077. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  20078. + struct dwc_otg_pcd *pcd,
  20079. + struct cfi_usb_ctrlrequest *ctrl_req)
  20080. +{
  20081. + int retval = -DWC_E_NOT_SUPPORTED;
  20082. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  20083. + uint16_t dfifo, rxfifo, txfifo;
  20084. +
  20085. + switch (ctrl_req->wIndex) {
  20086. + /* Whether the DDMA is enabled or not */
  20087. + case FT_ID_DMA_MODE:
  20088. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  20089. + retval = 1;
  20090. + break;
  20091. +
  20092. + case FT_ID_DMA_BUFFER_SETUP:
  20093. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  20094. + break;
  20095. +
  20096. + case FT_ID_DMA_BUFF_ALIGN:
  20097. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  20098. + break;
  20099. +
  20100. + case FT_ID_DMA_CONCAT_SETUP:
  20101. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  20102. + break;
  20103. +
  20104. + case FT_ID_DMA_CIRCULAR:
  20105. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  20106. + break;
  20107. +
  20108. + case FT_ID_THRESHOLD_SETUP:
  20109. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  20110. + break;
  20111. +
  20112. + case FT_ID_DFIFO_DEPTH:
  20113. + dfifo = get_dfifo_size(coreif);
  20114. + *((uint16_t *) buf) = dfifo;
  20115. + retval = sizeof(uint16_t);
  20116. + break;
  20117. +
  20118. + case FT_ID_TX_FIFO_DEPTH:
  20119. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  20120. + if (retval >= 0) {
  20121. + txfifo = retval;
  20122. + *((uint16_t *) buf) = txfifo;
  20123. + retval = sizeof(uint16_t);
  20124. + }
  20125. + break;
  20126. +
  20127. + case FT_ID_RX_FIFO_DEPTH:
  20128. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  20129. + if (retval >= 0) {
  20130. + rxfifo = retval;
  20131. + *((uint16_t *) buf) = rxfifo;
  20132. + retval = sizeof(uint16_t);
  20133. + }
  20134. + break;
  20135. + }
  20136. +
  20137. + return retval;
  20138. +}
  20139. +
  20140. +/**
  20141. + * This function resets the SG for the specified EP to its default value
  20142. + */
  20143. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  20144. +{
  20145. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  20146. + return 0;
  20147. +}
  20148. +
  20149. +/**
  20150. + * This function resets the Alignment for the specified EP to its default value
  20151. + */
  20152. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  20153. +{
  20154. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  20155. + return 0;
  20156. +}
  20157. +
  20158. +/**
  20159. + * This function resets the Concatenation for the specified EP to its default value
  20160. + * This function will also set the value of the wTxBytes field to NULL after
  20161. + * freeing the memory previously allocated for this field.
  20162. + */
  20163. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  20164. +{
  20165. + /* First we need to free the wTxBytes field */
  20166. + if (cfiep->bm_concat->wTxBytes) {
  20167. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  20168. + cfiep->bm_concat->wTxBytes = NULL;
  20169. + }
  20170. +
  20171. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  20172. + return 0;
  20173. +}
  20174. +
  20175. +/**
  20176. + * This function resets all the buffer setups of the specified endpoint
  20177. + */
  20178. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  20179. +{
  20180. + cfi_reset_sg_val(cfiep);
  20181. + cfi_reset_align_val(cfiep);
  20182. + cfi_reset_concat_val(cfiep);
  20183. + return 0;
  20184. +}
  20185. +
  20186. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  20187. + uint8_t rx_rst, uint8_t tx_rst)
  20188. +{
  20189. + int retval = -DWC_E_INVALID;
  20190. + uint16_t tx_siz[15];
  20191. + uint16_t rx_siz = 0;
  20192. + dwc_otg_pcd_ep_t *ep = NULL;
  20193. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  20194. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20195. +
  20196. + if (rx_rst) {
  20197. + rx_siz = params->dev_rx_fifo_size;
  20198. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  20199. + }
  20200. +
  20201. + if (tx_rst) {
  20202. + if (ep_addr == 0) {
  20203. + int i;
  20204. +
  20205. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20206. + tx_siz[i] =
  20207. + core_if->core_params->dev_tx_fifo_size[i];
  20208. + core_if->core_params->dev_tx_fifo_size[i] =
  20209. + core_if->init_txfsiz[i];
  20210. + }
  20211. + } else {
  20212. +
  20213. + ep = get_ep_by_addr(pcd, ep_addr);
  20214. +
  20215. + if (NULL == ep) {
  20216. + CFI_INFO
  20217. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  20218. + __func__, ep_addr);
  20219. + return -DWC_E_INVALID;
  20220. + }
  20221. +
  20222. + tx_siz[0] =
  20223. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  20224. + 1];
  20225. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  20226. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  20227. + dwc_ep.tx_fifo_num -
  20228. + 1];
  20229. + }
  20230. + }
  20231. +
  20232. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20233. + retval = 0;
  20234. + } else {
  20235. + CFI_INFO
  20236. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  20237. + __func__);
  20238. + if (rx_rst) {
  20239. + params->dev_rx_fifo_size = rx_siz;
  20240. + }
  20241. +
  20242. + if (tx_rst) {
  20243. + if (ep_addr == 0) {
  20244. + int i;
  20245. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  20246. + i++) {
  20247. + core_if->
  20248. + core_params->dev_tx_fifo_size[i] =
  20249. + tx_siz[i];
  20250. + }
  20251. + } else {
  20252. + params->dev_tx_fifo_size[ep->
  20253. + dwc_ep.tx_fifo_num -
  20254. + 1] = tx_siz[0];
  20255. + }
  20256. + }
  20257. + retval = -DWC_E_INVALID;
  20258. + }
  20259. + return retval;
  20260. +}
  20261. +
  20262. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  20263. +{
  20264. + int retval = 0;
  20265. + cfi_ep_t *cfiep;
  20266. + cfiobject_t *cfi = pcd->cfi;
  20267. + dwc_list_link_t *tmp;
  20268. +
  20269. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  20270. + if (retval < 0) {
  20271. + return retval;
  20272. + }
  20273. +
  20274. + /* If the EP address is known then reset the features for only that EP */
  20275. + if (addr) {
  20276. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20277. + if (NULL == cfiep) {
  20278. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20279. + __func__, addr);
  20280. + return -DWC_E_INVALID;
  20281. + }
  20282. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  20283. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  20284. + }
  20285. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20286. + else {
  20287. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20288. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20289. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20290. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20291. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  20292. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  20293. + if (retval < 0) {
  20294. + CFI_INFO
  20295. + ("%s: Error resetting the feature Reset All\n",
  20296. + __func__);
  20297. + return retval;
  20298. + }
  20299. + }
  20300. + }
  20301. + return retval;
  20302. +}
  20303. +
  20304. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  20305. + uint8_t addr)
  20306. +{
  20307. + int retval = 0;
  20308. + cfi_ep_t *cfiep;
  20309. + cfiobject_t *cfi = pcd->cfi;
  20310. + dwc_list_link_t *tmp;
  20311. +
  20312. + /* If the EP address is known then reset the features for only that EP */
  20313. + if (addr) {
  20314. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20315. + if (NULL == cfiep) {
  20316. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20317. + __func__, addr);
  20318. + return -DWC_E_INVALID;
  20319. + }
  20320. + retval = cfi_reset_sg_val(cfiep);
  20321. + }
  20322. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20323. + else {
  20324. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20325. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20326. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20327. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20328. + retval = cfi_reset_sg_val(cfiep);
  20329. + if (retval < 0) {
  20330. + CFI_INFO
  20331. + ("%s: Error resetting the feature Buffer Setup\n",
  20332. + __func__);
  20333. + return retval;
  20334. + }
  20335. + }
  20336. + }
  20337. + return retval;
  20338. +}
  20339. +
  20340. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  20341. +{
  20342. + int retval = 0;
  20343. + cfi_ep_t *cfiep;
  20344. + cfiobject_t *cfi = pcd->cfi;
  20345. + dwc_list_link_t *tmp;
  20346. +
  20347. + /* If the EP address is known then reset the features for only that EP */
  20348. + if (addr) {
  20349. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20350. + if (NULL == cfiep) {
  20351. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20352. + __func__, addr);
  20353. + return -DWC_E_INVALID;
  20354. + }
  20355. + retval = cfi_reset_concat_val(cfiep);
  20356. + }
  20357. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20358. + else {
  20359. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20360. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20361. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20362. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20363. + retval = cfi_reset_concat_val(cfiep);
  20364. + if (retval < 0) {
  20365. + CFI_INFO
  20366. + ("%s: Error resetting the feature Concatenation Value\n",
  20367. + __func__);
  20368. + return retval;
  20369. + }
  20370. + }
  20371. + }
  20372. + return retval;
  20373. +}
  20374. +
  20375. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  20376. +{
  20377. + int retval = 0;
  20378. + cfi_ep_t *cfiep;
  20379. + cfiobject_t *cfi = pcd->cfi;
  20380. + dwc_list_link_t *tmp;
  20381. +
  20382. + /* If the EP address is known then reset the features for only that EP */
  20383. + if (addr) {
  20384. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20385. + if (NULL == cfiep) {
  20386. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20387. + __func__, addr);
  20388. + return -DWC_E_INVALID;
  20389. + }
  20390. + retval = cfi_reset_align_val(cfiep);
  20391. + }
  20392. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20393. + else {
  20394. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20395. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20396. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20397. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20398. + retval = cfi_reset_align_val(cfiep);
  20399. + if (retval < 0) {
  20400. + CFI_INFO
  20401. + ("%s: Error resetting the feature Aliignment Value\n",
  20402. + __func__);
  20403. + return retval;
  20404. + }
  20405. + }
  20406. + }
  20407. + return retval;
  20408. +
  20409. +}
  20410. +
  20411. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  20412. + struct cfi_usb_ctrlrequest *req)
  20413. +{
  20414. + int retval = 0;
  20415. +
  20416. + switch (req->wIndex) {
  20417. + case 0:
  20418. + /* Reset all features */
  20419. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  20420. + break;
  20421. +
  20422. + case FT_ID_DMA_BUFFER_SETUP:
  20423. + /* Reset the SG buffer setup */
  20424. + retval =
  20425. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  20426. + break;
  20427. +
  20428. + case FT_ID_DMA_CONCAT_SETUP:
  20429. + /* Reset the Concatenation buffer setup */
  20430. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  20431. + break;
  20432. +
  20433. + case FT_ID_DMA_BUFF_ALIGN:
  20434. + /* Reset the Alignment buffer setup */
  20435. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  20436. + break;
  20437. +
  20438. + case FT_ID_TX_FIFO_DEPTH:
  20439. + retval =
  20440. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  20441. + pcd->cfi->need_gadget_att = 0;
  20442. + break;
  20443. +
  20444. + case FT_ID_RX_FIFO_DEPTH:
  20445. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  20446. + pcd->cfi->need_gadget_att = 0;
  20447. + break;
  20448. + default:
  20449. + break;
  20450. + }
  20451. + return retval;
  20452. +}
  20453. +
  20454. +/**
  20455. + * This function sets a new value for the SG buffer setup.
  20456. + */
  20457. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20458. +{
  20459. + uint8_t inaddr, outaddr;
  20460. + cfi_ep_t *epin, *epout;
  20461. + ddma_sg_buffer_setup_t *psgval;
  20462. + uint32_t desccount, size;
  20463. +
  20464. + CFI_INFO("%s\n", __func__);
  20465. +
  20466. + psgval = (ddma_sg_buffer_setup_t *) buf;
  20467. + desccount = (uint32_t) psgval->bCount;
  20468. + size = (uint32_t) psgval->wSize;
  20469. +
  20470. + /* Check the DMA descriptor count */
  20471. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  20472. + CFI_INFO
  20473. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  20474. + __func__, MAX_DMA_DESCS_PER_EP);
  20475. + return -DWC_E_INVALID;
  20476. + }
  20477. +
  20478. + /* Check the DMA descriptor count */
  20479. +
  20480. + if (size == 0) {
  20481. +
  20482. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  20483. + __func__);
  20484. +
  20485. + return -DWC_E_INVALID;
  20486. +
  20487. + }
  20488. +
  20489. + inaddr = psgval->bInEndpointAddress;
  20490. + outaddr = psgval->bOutEndpointAddress;
  20491. +
  20492. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  20493. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  20494. +
  20495. + if (NULL == epin || NULL == epout) {
  20496. + CFI_INFO
  20497. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  20498. + __func__, inaddr, outaddr);
  20499. + return -DWC_E_INVALID;
  20500. + }
  20501. +
  20502. + epin->ep->dwc_ep.buff_mode = BM_SG;
  20503. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  20504. +
  20505. + epout->ep->dwc_ep.buff_mode = BM_SG;
  20506. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  20507. +
  20508. + return 0;
  20509. +}
  20510. +
  20511. +/**
  20512. + * This function sets a new value for the buffer Alignment setup.
  20513. + */
  20514. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20515. +{
  20516. + cfi_ep_t *ep;
  20517. + uint8_t addr;
  20518. + ddma_align_buffer_setup_t *palignval;
  20519. +
  20520. + palignval = (ddma_align_buffer_setup_t *) buf;
  20521. + addr = palignval->bEndpointAddress;
  20522. +
  20523. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20524. +
  20525. + if (NULL == ep) {
  20526. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20527. + __func__, addr);
  20528. + return -DWC_E_INVALID;
  20529. + }
  20530. +
  20531. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  20532. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  20533. +
  20534. + return 0;
  20535. +}
  20536. +
  20537. +/**
  20538. + * This function sets a new value for the Concatenation buffer setup.
  20539. + */
  20540. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20541. +{
  20542. + uint8_t addr;
  20543. + cfi_ep_t *ep;
  20544. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  20545. + uint16_t *pVals;
  20546. + uint32_t desccount;
  20547. + int i;
  20548. + uint16_t mps;
  20549. +
  20550. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  20551. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  20552. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  20553. +
  20554. + /* Check the DMA descriptor count */
  20555. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  20556. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  20557. + __func__, MAX_DMA_DESCS_PER_EP);
  20558. + return -DWC_E_INVALID;
  20559. + }
  20560. +
  20561. + addr = pConcatValHdr->bEndpointAddress;
  20562. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20563. + if (NULL == ep) {
  20564. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20565. + __func__, addr);
  20566. + return -DWC_E_INVALID;
  20567. + }
  20568. +
  20569. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  20570. +
  20571. +#if 0
  20572. + for (i = 0; i < desccount; i++) {
  20573. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  20574. + }
  20575. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  20576. +#endif
  20577. +
  20578. + /* Check the wTxSizes to be less than or equal to the mps */
  20579. + for (i = 0; i < desccount; i++) {
  20580. + if (pVals[i] > mps) {
  20581. + CFI_INFO
  20582. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  20583. + __func__, i, pVals[i]);
  20584. + return -DWC_E_INVALID;
  20585. + }
  20586. + }
  20587. +
  20588. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  20589. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  20590. +
  20591. + /* Free the previously allocated storage for the wTxBytes */
  20592. + if (ep->bm_concat->wTxBytes) {
  20593. + DWC_FREE(ep->bm_concat->wTxBytes);
  20594. + }
  20595. +
  20596. + /* Allocate a new storage for the wTxBytes field */
  20597. + ep->bm_concat->wTxBytes =
  20598. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  20599. + if (NULL == ep->bm_concat->wTxBytes) {
  20600. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  20601. + return -DWC_E_NO_MEMORY;
  20602. + }
  20603. +
  20604. + /* Copy the new values into the wTxBytes filed */
  20605. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  20606. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  20607. +
  20608. + return 0;
  20609. +}
  20610. +
  20611. +/**
  20612. + * This function calculates the total of all FIFO sizes
  20613. + *
  20614. + * @param core_if Programming view of DWC_otg controller
  20615. + *
  20616. + * @return The total of data FIFO sizes.
  20617. + *
  20618. + */
  20619. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  20620. +{
  20621. + dwc_otg_core_params_t *params = core_if->core_params;
  20622. + uint16_t dfifo_total = 0;
  20623. + int i;
  20624. +
  20625. + /* The shared RxFIFO size */
  20626. + dfifo_total =
  20627. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  20628. +
  20629. + /* Add up each TxFIFO size to the total */
  20630. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20631. + dfifo_total += params->dev_tx_fifo_size[i];
  20632. + }
  20633. +
  20634. + return dfifo_total;
  20635. +}
  20636. +
  20637. +/**
  20638. + * This function returns Rx FIFO size
  20639. + *
  20640. + * @param core_if Programming view of DWC_otg controller
  20641. + *
  20642. + * @return The total of data FIFO sizes.
  20643. + *
  20644. + */
  20645. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  20646. +{
  20647. + switch (wValue >> 8) {
  20648. + case 0:
  20649. + return (core_if->pwron_rxfsiz <
  20650. + 32768) ? core_if->pwron_rxfsiz : 32768;
  20651. + break;
  20652. + case 1:
  20653. + return core_if->core_params->dev_rx_fifo_size;
  20654. + break;
  20655. + default:
  20656. + return -DWC_E_INVALID;
  20657. + break;
  20658. + }
  20659. +}
  20660. +
  20661. +/**
  20662. + * This function returns Tx FIFO size for IN EP
  20663. + *
  20664. + * @param core_if Programming view of DWC_otg controller
  20665. + *
  20666. + * @return The total of data FIFO sizes.
  20667. + *
  20668. + */
  20669. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  20670. +{
  20671. + dwc_otg_pcd_ep_t *ep;
  20672. +
  20673. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  20674. +
  20675. + if (NULL == ep) {
  20676. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20677. + __func__, wValue & 0xff);
  20678. + return -DWC_E_INVALID;
  20679. + }
  20680. +
  20681. + if (!ep->dwc_ep.is_in) {
  20682. + CFI_INFO
  20683. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  20684. + __func__, wValue & 0xff);
  20685. + return -DWC_E_INVALID;
  20686. + }
  20687. +
  20688. + switch (wValue >> 8) {
  20689. + case 0:
  20690. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  20691. + [ep->dwc_ep.tx_fifo_num - 1] <
  20692. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  20693. + dwc_ep.tx_fifo_num
  20694. + - 1] : 32768;
  20695. + break;
  20696. + case 1:
  20697. + return GET_CORE_IF(pcd)->core_params->
  20698. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  20699. + break;
  20700. + default:
  20701. + return -DWC_E_INVALID;
  20702. + break;
  20703. + }
  20704. +}
  20705. +
  20706. +/**
  20707. + * This function checks if the submitted combination of
  20708. + * device mode FIFO sizes is possible or not.
  20709. + *
  20710. + * @param core_if Programming view of DWC_otg controller
  20711. + *
  20712. + * @return 1 if possible, 0 otherwise.
  20713. + *
  20714. + */
  20715. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  20716. +{
  20717. + uint16_t dfifo_actual = 0;
  20718. + dwc_otg_core_params_t *params = core_if->core_params;
  20719. + uint16_t start_addr = 0;
  20720. + int i;
  20721. +
  20722. + dfifo_actual =
  20723. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  20724. +
  20725. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20726. + dfifo_actual += params->dev_tx_fifo_size[i];
  20727. + }
  20728. +
  20729. + if (dfifo_actual > core_if->total_fifo_size) {
  20730. + return 0;
  20731. + }
  20732. +
  20733. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  20734. + return 0;
  20735. +
  20736. + if (params->dev_nperio_tx_fifo_size > 32768
  20737. + || params->dev_nperio_tx_fifo_size < 16)
  20738. + return 0;
  20739. +
  20740. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20741. +
  20742. + if (params->dev_tx_fifo_size[i] > 768
  20743. + || params->dev_tx_fifo_size[i] < 4)
  20744. + return 0;
  20745. + }
  20746. +
  20747. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  20748. + return 0;
  20749. + start_addr = params->dev_rx_fifo_size;
  20750. +
  20751. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  20752. + return 0;
  20753. + start_addr += params->dev_nperio_tx_fifo_size;
  20754. +
  20755. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20756. +
  20757. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  20758. + return 0;
  20759. + start_addr += params->dev_tx_fifo_size[i];
  20760. + }
  20761. +
  20762. + return 1;
  20763. +}
  20764. +
  20765. +/**
  20766. + * This function resizes Device mode FIFOs
  20767. + *
  20768. + * @param core_if Programming view of DWC_otg controller
  20769. + *
  20770. + * @return 1 if successful, 0 otherwise
  20771. + *
  20772. + */
  20773. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  20774. +{
  20775. + int i = 0;
  20776. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  20777. + dwc_otg_core_params_t *params = core_if->core_params;
  20778. + uint32_t rx_fifo_size;
  20779. + fifosize_data_t nptxfifosize;
  20780. + fifosize_data_t txfifosize[15];
  20781. +
  20782. + uint32_t rx_fsz_bak;
  20783. + uint32_t nptxfsz_bak;
  20784. + uint32_t txfsz_bak[15];
  20785. +
  20786. + uint16_t start_address;
  20787. + uint8_t retval = 1;
  20788. +
  20789. + if (!check_fifo_sizes(core_if)) {
  20790. + return 0;
  20791. + }
  20792. +
  20793. + /* Configure data FIFO sizes */
  20794. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  20795. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  20796. + rx_fifo_size = params->dev_rx_fifo_size;
  20797. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  20798. +
  20799. + /*
  20800. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  20801. + * Indexes of the FIFO size module parameters in the
  20802. + * dev_tx_fifo_size array and the FIFO size registers in
  20803. + * the dtxfsiz array run from 0 to 14.
  20804. + */
  20805. +
  20806. + /* Non-periodic Tx FIFO */
  20807. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  20808. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  20809. + start_address = params->dev_rx_fifo_size;
  20810. + nptxfifosize.b.startaddr = start_address;
  20811. +
  20812. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  20813. +
  20814. + start_address += nptxfifosize.b.depth;
  20815. +
  20816. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20817. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  20818. +
  20819. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  20820. + txfifosize[i].b.startaddr = start_address;
  20821. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  20822. + txfifosize[i].d32);
  20823. +
  20824. + start_address += txfifosize[i].b.depth;
  20825. + }
  20826. +
  20827. + /** Check if register values are set correctly */
  20828. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  20829. + retval = 0;
  20830. + }
  20831. +
  20832. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  20833. + retval = 0;
  20834. + }
  20835. +
  20836. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20837. + if (txfifosize[i].d32 !=
  20838. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  20839. + retval = 0;
  20840. + }
  20841. + }
  20842. +
  20843. + /** If register values are not set correctly, reset old values */
  20844. + if (retval == 0) {
  20845. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  20846. +
  20847. + /* Non-periodic Tx FIFO */
  20848. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  20849. +
  20850. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20851. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  20852. + txfsz_bak[i]);
  20853. + }
  20854. + }
  20855. + } else {
  20856. + return 0;
  20857. + }
  20858. +
  20859. + /* Flush the FIFOs */
  20860. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  20861. + dwc_otg_flush_rx_fifo(core_if);
  20862. +
  20863. + return retval;
  20864. +}
  20865. +
  20866. +/**
  20867. + * This function sets a new value for the buffer Alignment setup.
  20868. + */
  20869. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  20870. +{
  20871. + int retval;
  20872. + uint32_t fsiz;
  20873. + uint16_t size;
  20874. + uint16_t ep_addr;
  20875. + dwc_otg_pcd_ep_t *ep;
  20876. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20877. + tx_fifo_size_setup_t *ptxfifoval;
  20878. +
  20879. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  20880. + ep_addr = ptxfifoval->bEndpointAddress;
  20881. + size = ptxfifoval->wDepth;
  20882. +
  20883. + ep = get_ep_by_addr(pcd, ep_addr);
  20884. +
  20885. + CFI_INFO
  20886. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  20887. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  20888. +
  20889. + if (NULL == ep) {
  20890. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20891. + __func__, ep_addr);
  20892. + return -DWC_E_INVALID;
  20893. + }
  20894. +
  20895. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  20896. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  20897. +
  20898. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20899. + retval = 0;
  20900. + } else {
  20901. + CFI_INFO
  20902. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  20903. + __func__, ep_addr);
  20904. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  20905. + retval = -DWC_E_INVALID;
  20906. + }
  20907. +
  20908. + return retval;
  20909. +}
  20910. +
  20911. +/**
  20912. + * This function sets a new value for the buffer Alignment setup.
  20913. + */
  20914. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  20915. +{
  20916. + int retval;
  20917. + uint32_t fsiz;
  20918. + uint16_t size;
  20919. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20920. + rx_fifo_size_setup_t *prxfifoval;
  20921. +
  20922. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  20923. + size = prxfifoval->wDepth;
  20924. +
  20925. + fsiz = params->dev_rx_fifo_size;
  20926. + params->dev_rx_fifo_size = size;
  20927. +
  20928. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20929. + retval = 0;
  20930. + } else {
  20931. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  20932. + __func__);
  20933. + params->dev_rx_fifo_size = fsiz;
  20934. + retval = -DWC_E_INVALID;
  20935. + }
  20936. +
  20937. + return retval;
  20938. +}
  20939. +
  20940. +/**
  20941. + * This function reads the SG of an EP's buffer setup into the buffer buf
  20942. + */
  20943. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20944. + struct cfi_usb_ctrlrequest *req)
  20945. +{
  20946. + int retval = -DWC_E_INVALID;
  20947. + uint8_t addr;
  20948. + cfi_ep_t *ep;
  20949. +
  20950. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20951. + addr = req->wValue & 0xFF;
  20952. + if (addr == 0) /* The address should be non-zero */
  20953. + return retval;
  20954. +
  20955. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20956. + if (NULL == ep) {
  20957. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20958. + __func__, addr);
  20959. + return retval;
  20960. + }
  20961. +
  20962. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  20963. + retval = BS_SG_VAL_DESC_LEN;
  20964. + return retval;
  20965. +}
  20966. +
  20967. +/**
  20968. + * This function reads the Concatenation value of an EP's buffer mode into
  20969. + * the buffer buf
  20970. + */
  20971. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20972. + struct cfi_usb_ctrlrequest *req)
  20973. +{
  20974. + int retval = -DWC_E_INVALID;
  20975. + uint8_t addr;
  20976. + cfi_ep_t *ep;
  20977. + uint8_t desc_count;
  20978. +
  20979. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20980. + addr = req->wValue & 0xFF;
  20981. + if (addr == 0) /* The address should be non-zero */
  20982. + return retval;
  20983. +
  20984. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20985. + if (NULL == ep) {
  20986. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20987. + __func__, addr);
  20988. + return retval;
  20989. + }
  20990. +
  20991. + /* Copy the header to the buffer */
  20992. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  20993. + /* Advance the buffer pointer by the header size */
  20994. + buf += BS_CONCAT_VAL_HDR_LEN;
  20995. +
  20996. + desc_count = ep->bm_concat->hdr.bDescCount;
  20997. + /* Copy alll the wTxBytes to the buffer */
  20998. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  20999. +
  21000. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  21001. + return retval;
  21002. +}
  21003. +
  21004. +/**
  21005. + * This function reads the buffer Alignment value of an EP's buffer mode into
  21006. + * the buffer buf
  21007. + *
  21008. + * @return The total number of bytes copied to the buffer or negative error code.
  21009. + */
  21010. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  21011. + struct cfi_usb_ctrlrequest *req)
  21012. +{
  21013. + int retval = -DWC_E_INVALID;
  21014. + uint8_t addr;
  21015. + cfi_ep_t *ep;
  21016. +
  21017. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  21018. + addr = req->wValue & 0xFF;
  21019. + if (addr == 0) /* The address should be non-zero */
  21020. + return retval;
  21021. +
  21022. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  21023. + if (NULL == ep) {
  21024. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  21025. + __func__, addr);
  21026. + return retval;
  21027. + }
  21028. +
  21029. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  21030. + retval = BS_ALIGN_VAL_HDR_LEN;
  21031. +
  21032. + return retval;
  21033. +}
  21034. +
  21035. +/**
  21036. + * This function sets a new value for the specified feature
  21037. + *
  21038. + * @param pcd A pointer to the PCD object
  21039. + *
  21040. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  21041. + */
  21042. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  21043. +{
  21044. + int retval = -DWC_E_NOT_SUPPORTED;
  21045. + uint16_t wIndex, wValue;
  21046. + uint8_t bRequest;
  21047. + struct dwc_otg_core_if *coreif;
  21048. + cfiobject_t *cfi = pcd->cfi;
  21049. + struct cfi_usb_ctrlrequest *ctrl_req;
  21050. + uint8_t *buf;
  21051. + ctrl_req = &cfi->ctrl_req;
  21052. +
  21053. + buf = pcd->cfi->ctrl_req.data;
  21054. +
  21055. + coreif = GET_CORE_IF(pcd);
  21056. + bRequest = ctrl_req->bRequest;
  21057. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  21058. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  21059. +
  21060. + /* See which feature is to be modified */
  21061. + switch (wIndex) {
  21062. + case FT_ID_DMA_BUFFER_SETUP:
  21063. + /* Modify the feature */
  21064. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  21065. + return retval;
  21066. +
  21067. + /* And send this request to the gadget */
  21068. + cfi->need_gadget_att = 1;
  21069. + break;
  21070. +
  21071. + case FT_ID_DMA_BUFF_ALIGN:
  21072. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  21073. + return retval;
  21074. + cfi->need_gadget_att = 1;
  21075. + break;
  21076. +
  21077. + case FT_ID_DMA_CONCAT_SETUP:
  21078. + /* Modify the feature */
  21079. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  21080. + return retval;
  21081. + cfi->need_gadget_att = 1;
  21082. + break;
  21083. +
  21084. + case FT_ID_DMA_CIRCULAR:
  21085. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  21086. + break;
  21087. +
  21088. + case FT_ID_THRESHOLD_SETUP:
  21089. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  21090. + break;
  21091. +
  21092. + case FT_ID_DFIFO_DEPTH:
  21093. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  21094. + break;
  21095. +
  21096. + case FT_ID_TX_FIFO_DEPTH:
  21097. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  21098. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  21099. + return retval;
  21100. + cfi->need_gadget_att = 0;
  21101. + break;
  21102. +
  21103. + case FT_ID_RX_FIFO_DEPTH:
  21104. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  21105. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  21106. + return retval;
  21107. + cfi->need_gadget_att = 0;
  21108. + break;
  21109. + }
  21110. +
  21111. + return retval;
  21112. +}
  21113. +
  21114. +#endif //DWC_UTE_CFI
  21115. --- /dev/null
  21116. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  21117. @@ -0,0 +1,320 @@
  21118. +/* ==========================================================================
  21119. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  21120. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  21121. + * otherwise expressly agreed to in writing between Synopsys and you.
  21122. + *
  21123. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  21124. + * any End User Software License Agreement or Agreement for Licensed Product
  21125. + * with Synopsys or any supplement thereto. You are permitted to use and
  21126. + * redistribute this Software in source and binary forms, with or without
  21127. + * modification, provided that redistributions of source code must retain this
  21128. + * notice. You may not view, use, disclose, copy or distribute this file or
  21129. + * any information contained herein except pursuant to this license grant from
  21130. + * Synopsys. If you do not agree with this notice, including the disclaimer
  21131. + * below, then you are not authorized to use the Software.
  21132. + *
  21133. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  21134. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21135. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21136. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  21137. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21138. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21139. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21140. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21141. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21142. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  21143. + * DAMAGE.
  21144. + * ========================================================================== */
  21145. +
  21146. +#if !defined(__DWC_OTG_CFI_H__)
  21147. +#define __DWC_OTG_CFI_H__
  21148. +
  21149. +#include "dwc_otg_pcd.h"
  21150. +#include "dwc_cfi_common.h"
  21151. +
  21152. +/**
  21153. + * @file
  21154. + * This file contains the CFI related OTG PCD specific common constants,
  21155. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  21156. + * optional interface for internal testing purposes that a DUT may implement to
  21157. + * support testing of configurable features.
  21158. + *
  21159. + */
  21160. +
  21161. +struct dwc_otg_pcd;
  21162. +struct dwc_otg_pcd_ep;
  21163. +
  21164. +/** OTG CFI Features (properties) ID constants */
  21165. +/** This is a request for all Core Features */
  21166. +#define FT_ID_DMA_MODE 0x0001
  21167. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  21168. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  21169. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  21170. +#define FT_ID_DMA_CIRCULAR 0x0005
  21171. +#define FT_ID_THRESHOLD_SETUP 0x0006
  21172. +#define FT_ID_DFIFO_DEPTH 0x0007
  21173. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  21174. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  21175. +
  21176. +/**********************************************************/
  21177. +#define CFI_INFO_DEF
  21178. +
  21179. +#ifdef CFI_INFO_DEF
  21180. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  21181. +#else
  21182. +#define CFI_INFO(fmt...)
  21183. +#endif
  21184. +
  21185. +#define min(x,y) ({ \
  21186. + x < y ? x : y; })
  21187. +
  21188. +#define max(x,y) ({ \
  21189. + x > y ? x : y; })
  21190. +
  21191. +/**
  21192. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  21193. + * also used for setting up a buffer for Circular DDMA.
  21194. + */
  21195. +struct _ddma_sg_buffer_setup {
  21196. +#define BS_SG_VAL_DESC_LEN 6
  21197. + /* The OUT EP address */
  21198. + uint8_t bOutEndpointAddress;
  21199. + /* The IN EP address */
  21200. + uint8_t bInEndpointAddress;
  21201. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  21202. + uint8_t bOffset;
  21203. + /* The number of transfer segments (a DMA descriptors per each segment) */
  21204. + uint8_t bCount;
  21205. + /* Size (in byte) of each transfer segment */
  21206. + uint16_t wSize;
  21207. +} __attribute__ ((packed));
  21208. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  21209. +
  21210. +/** Descriptor DMA Concatenation Buffer setup structure */
  21211. +struct _ddma_concat_buffer_setup_hdr {
  21212. +#define BS_CONCAT_VAL_HDR_LEN 4
  21213. + /* The endpoint for which the buffer is to be set up */
  21214. + uint8_t bEndpointAddress;
  21215. + /* The count of descriptors to be used */
  21216. + uint8_t bDescCount;
  21217. + /* The total size of the transfer */
  21218. + uint16_t wSize;
  21219. +} __attribute__ ((packed));
  21220. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  21221. +
  21222. +/** Descriptor DMA Concatenation Buffer setup structure */
  21223. +struct _ddma_concat_buffer_setup {
  21224. + /* The SG header */
  21225. + ddma_concat_buffer_setup_hdr_t hdr;
  21226. +
  21227. + /* The XFER sizes pointer (allocated dynamically) */
  21228. + uint16_t *wTxBytes;
  21229. +} __attribute__ ((packed));
  21230. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  21231. +
  21232. +/** Descriptor DMA Alignment Buffer setup structure */
  21233. +struct _ddma_align_buffer_setup {
  21234. +#define BS_ALIGN_VAL_HDR_LEN 2
  21235. + uint8_t bEndpointAddress;
  21236. + uint8_t bAlign;
  21237. +} __attribute__ ((packed));
  21238. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  21239. +
  21240. +/** Transmit FIFO Size setup structure */
  21241. +struct _tx_fifo_size_setup {
  21242. + uint8_t bEndpointAddress;
  21243. + uint16_t wDepth;
  21244. +} __attribute__ ((packed));
  21245. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  21246. +
  21247. +/** Transmit FIFO Size setup structure */
  21248. +struct _rx_fifo_size_setup {
  21249. + uint16_t wDepth;
  21250. +} __attribute__ ((packed));
  21251. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  21252. +
  21253. +/**
  21254. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  21255. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  21256. + * to the data returned in the data stage of a 3-stage Control Write requests.
  21257. + */
  21258. +struct cfi_usb_ctrlrequest {
  21259. + uint8_t bRequestType;
  21260. + uint8_t bRequest;
  21261. + uint16_t wValue;
  21262. + uint16_t wIndex;
  21263. + uint16_t wLength;
  21264. + uint8_t *data;
  21265. +} UPACKED;
  21266. +
  21267. +/*---------------------------------------------------------------------------*/
  21268. +
  21269. +/**
  21270. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  21271. + * This structure is used to store the buffer setup data for any
  21272. + * enabled endpoint in the PCD.
  21273. + */
  21274. +struct cfi_ep {
  21275. + /* Entry for the list container */
  21276. + dwc_list_link_t lh;
  21277. + /* Pointer to the active PCD endpoint structure */
  21278. + struct dwc_otg_pcd_ep *ep;
  21279. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  21280. + struct dwc_otg_dma_desc *dma_desc_last;
  21281. + /* The SG feature value */
  21282. + ddma_sg_buffer_setup_t *bm_sg;
  21283. + /* The Circular feature value */
  21284. + ddma_sg_buffer_setup_t *bm_circ;
  21285. + /* The Concatenation feature value */
  21286. + ddma_concat_buffer_setup_t *bm_concat;
  21287. + /* The Alignment feature value */
  21288. + ddma_align_buffer_setup_t *bm_align;
  21289. + /* XFER length */
  21290. + uint32_t xfer_len;
  21291. + /*
  21292. + * Count of DMA descriptors currently used.
  21293. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  21294. + * defined in the dwc_otg_cil.h
  21295. + */
  21296. + uint32_t desc_count;
  21297. +};
  21298. +typedef struct cfi_ep cfi_ep_t;
  21299. +
  21300. +typedef struct cfi_dma_buff {
  21301. +#define CFI_IN_BUF_LEN 1024
  21302. +#define CFI_OUT_BUF_LEN 1024
  21303. + dma_addr_t addr;
  21304. + uint8_t *buf;
  21305. +} cfi_dma_buff_t;
  21306. +
  21307. +struct cfiobject;
  21308. +
  21309. +/**
  21310. + * This is the interface for the CFI operations.
  21311. + *
  21312. + * @param ep_enable Called when any endpoint is enabled and activated.
  21313. + * @param release Called when the CFI object is released and it needs to correctly
  21314. + * deallocate the dynamic memory
  21315. + * @param ctrl_write_complete Called when the data stage of the request is complete
  21316. + */
  21317. +typedef struct cfi_ops {
  21318. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  21319. + struct dwc_otg_pcd_ep * ep);
  21320. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  21321. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  21322. + unsigned size, gfp_t flags);
  21323. + void (*release) (struct cfiobject * cfi);
  21324. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  21325. + struct dwc_otg_pcd * pcd);
  21326. + void (*build_descriptors) (struct cfiobject * cfi,
  21327. + struct dwc_otg_pcd * pcd,
  21328. + struct dwc_otg_pcd_ep * ep,
  21329. + dwc_otg_pcd_request_t * req);
  21330. +} cfi_ops_t;
  21331. +
  21332. +struct cfiobject {
  21333. + cfi_ops_t ops;
  21334. + struct dwc_otg_pcd *pcd;
  21335. + struct usb_gadget *gadget;
  21336. +
  21337. + /* Buffers used to send/receive CFI-related request data */
  21338. + cfi_dma_buff_t buf_in;
  21339. + cfi_dma_buff_t buf_out;
  21340. +
  21341. + /* CFI specific Control request wrapper */
  21342. + struct cfi_usb_ctrlrequest ctrl_req;
  21343. +
  21344. + /* The list of active EP's in the PCD of type cfi_ep_t */
  21345. + dwc_list_link_t active_eps;
  21346. +
  21347. + /* This flag shall control the propagation of a specific request
  21348. + * to the gadget's processing routines.
  21349. + * 0 - no gadget handling
  21350. + * 1 - the gadget needs to know about this request (w/o completing a status
  21351. + * phase - just return a 0 to the _setup callback)
  21352. + */
  21353. + uint8_t need_gadget_att;
  21354. +
  21355. + /* Flag indicating whether the status IN phase needs to be
  21356. + * completed by the PCD
  21357. + */
  21358. + uint8_t need_status_in_complete;
  21359. +};
  21360. +typedef struct cfiobject cfiobject_t;
  21361. +
  21362. +#define DUMP_MSG
  21363. +
  21364. +#if defined(DUMP_MSG)
  21365. +static inline void dump_msg(const u8 * buf, unsigned int length)
  21366. +{
  21367. + unsigned int start, num, i;
  21368. + char line[52], *p;
  21369. +
  21370. + if (length >= 512)
  21371. + return;
  21372. +
  21373. + start = 0;
  21374. + while (length > 0) {
  21375. + num = min(length, 16u);
  21376. + p = line;
  21377. + for (i = 0; i < num; ++i) {
  21378. + if (i == 8)
  21379. + *p++ = ' ';
  21380. + DWC_SPRINTF(p, " %02x", buf[i]);
  21381. + p += 3;
  21382. + }
  21383. + *p = 0;
  21384. + DWC_DEBUG("%6x: %s\n", start, line);
  21385. + buf += num;
  21386. + start += num;
  21387. + length -= num;
  21388. + }
  21389. +}
  21390. +#else
  21391. +static inline void dump_msg(const u8 * buf, unsigned int length)
  21392. +{
  21393. +}
  21394. +#endif
  21395. +
  21396. +/**
  21397. + * This function returns a pointer to cfi_ep_t object with the addr address.
  21398. + */
  21399. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  21400. + uint8_t addr)
  21401. +{
  21402. + struct cfi_ep *pcfiep;
  21403. + dwc_list_link_t *tmp;
  21404. +
  21405. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  21406. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  21407. +
  21408. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  21409. + return pcfiep;
  21410. + }
  21411. + }
  21412. +
  21413. + return NULL;
  21414. +}
  21415. +
  21416. +/**
  21417. + * This function returns a pointer to cfi_ep_t object that matches
  21418. + * the dwc_otg_pcd_ep object.
  21419. + */
  21420. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  21421. + struct dwc_otg_pcd_ep *ep)
  21422. +{
  21423. + struct cfi_ep *pcfiep = NULL;
  21424. + dwc_list_link_t *tmp;
  21425. +
  21426. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  21427. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  21428. + if (pcfiep->ep == ep) {
  21429. + return pcfiep;
  21430. + }
  21431. + }
  21432. + return NULL;
  21433. +}
  21434. +
  21435. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  21436. +
  21437. +#endif /* (__DWC_OTG_CFI_H__) */
  21438. --- /dev/null
  21439. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  21440. @@ -0,0 +1,7141 @@
  21441. +/* ==========================================================================
  21442. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  21443. + * $Revision: #191 $
  21444. + * $Date: 2012/08/10 $
  21445. + * $Change: 2047372 $
  21446. + *
  21447. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  21448. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  21449. + * otherwise expressly agreed to in writing between Synopsys and you.
  21450. + *
  21451. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  21452. + * any End User Software License Agreement or Agreement for Licensed Product
  21453. + * with Synopsys or any supplement thereto. You are permitted to use and
  21454. + * redistribute this Software in source and binary forms, with or without
  21455. + * modification, provided that redistributions of source code must retain this
  21456. + * notice. You may not view, use, disclose, copy or distribute this file or
  21457. + * any information contained herein except pursuant to this license grant from
  21458. + * Synopsys. If you do not agree with this notice, including the disclaimer
  21459. + * below, then you are not authorized to use the Software.
  21460. + *
  21461. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  21462. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21463. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21464. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  21465. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21466. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21467. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21468. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21469. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21470. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  21471. + * DAMAGE.
  21472. + * ========================================================================== */
  21473. +
  21474. +/** @file
  21475. + *
  21476. + * The Core Interface Layer provides basic services for accessing and
  21477. + * managing the DWC_otg hardware. These services are used by both the
  21478. + * Host Controller Driver and the Peripheral Controller Driver.
  21479. + *
  21480. + * The CIL manages the memory map for the core so that the HCD and PCD
  21481. + * don't have to do this separately. It also handles basic tasks like
  21482. + * reading/writing the registers and data FIFOs in the controller.
  21483. + * Some of the data access functions provide encapsulation of several
  21484. + * operations required to perform a task, such as writing multiple
  21485. + * registers to start a transfer. Finally, the CIL performs basic
  21486. + * services that are not specific to either the host or device modes
  21487. + * of operation. These services include management of the OTG Host
  21488. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  21489. + * Diagnostic API is also provided to allow testing of the controller
  21490. + * hardware.
  21491. + *
  21492. + * The Core Interface Layer has the following requirements:
  21493. + * - Provides basic controller operations.
  21494. + * - Minimal use of OS services.
  21495. + * - The OS services used will be abstracted by using inline functions
  21496. + * or macros.
  21497. + *
  21498. + */
  21499. +
  21500. +#include "dwc_os.h"
  21501. +#include "dwc_otg_regs.h"
  21502. +#include "dwc_otg_cil.h"
  21503. +
  21504. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  21505. +
  21506. +/**
  21507. + * This function is called to initialize the DWC_otg CSR data
  21508. + * structures. The register addresses in the device and host
  21509. + * structures are initialized from the base address supplied by the
  21510. + * caller. The calling function must make the OS calls to get the
  21511. + * base address of the DWC_otg controller registers. The core_params
  21512. + * argument holds the parameters that specify how the core should be
  21513. + * configured.
  21514. + *
  21515. + * @param reg_base_addr Base address of DWC_otg core registers
  21516. + *
  21517. + */
  21518. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  21519. +{
  21520. + dwc_otg_core_if_t *core_if = 0;
  21521. + dwc_otg_dev_if_t *dev_if = 0;
  21522. + dwc_otg_host_if_t *host_if = 0;
  21523. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  21524. + int i = 0;
  21525. +
  21526. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  21527. +
  21528. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  21529. +
  21530. + if (core_if == NULL) {
  21531. + DWC_DEBUGPL(DBG_CIL,
  21532. + "Allocation of dwc_otg_core_if_t failed\n");
  21533. + return 0;
  21534. + }
  21535. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  21536. +
  21537. + /*
  21538. + * Allocate the Device Mode structures.
  21539. + */
  21540. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  21541. +
  21542. + if (dev_if == NULL) {
  21543. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  21544. + DWC_FREE(core_if);
  21545. + return 0;
  21546. + }
  21547. +
  21548. + dev_if->dev_global_regs =
  21549. + (dwc_otg_device_global_regs_t *) (reg_base +
  21550. + DWC_DEV_GLOBAL_REG_OFFSET);
  21551. +
  21552. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21553. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  21554. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  21555. + (i * DWC_EP_REG_OFFSET));
  21556. +
  21557. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  21558. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  21559. + (i * DWC_EP_REG_OFFSET));
  21560. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  21561. + i, &dev_if->in_ep_regs[i]->diepctl);
  21562. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  21563. + i, &dev_if->out_ep_regs[i]->doepctl);
  21564. + }
  21565. +
  21566. + dev_if->speed = 0; // unknown
  21567. +
  21568. + core_if->dev_if = dev_if;
  21569. +
  21570. + /*
  21571. + * Allocate the Host Mode structures.
  21572. + */
  21573. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  21574. +
  21575. + if (host_if == NULL) {
  21576. + DWC_DEBUGPL(DBG_CIL,
  21577. + "Allocation of dwc_otg_host_if_t failed\n");
  21578. + DWC_FREE(dev_if);
  21579. + DWC_FREE(core_if);
  21580. + return 0;
  21581. + }
  21582. +
  21583. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  21584. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  21585. +
  21586. + host_if->hprt0 =
  21587. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  21588. +
  21589. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21590. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  21591. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  21592. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  21593. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  21594. + i, &host_if->hc_regs[i]->hcchar);
  21595. + }
  21596. +
  21597. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  21598. + core_if->host_if = host_if;
  21599. +
  21600. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21601. + core_if->data_fifo[i] =
  21602. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  21603. + (i * DWC_OTG_DATA_FIFO_SIZE));
  21604. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  21605. + i, (unsigned long)core_if->data_fifo[i]);
  21606. + }
  21607. +
  21608. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  21609. +
  21610. + /* Initiate lx_state to L3 disconnected state */
  21611. + core_if->lx_state = DWC_OTG_L3;
  21612. + /*
  21613. + * Store the contents of the hardware configuration registers here for
  21614. + * easy access later.
  21615. + */
  21616. + core_if->hwcfg1.d32 =
  21617. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  21618. + core_if->hwcfg2.d32 =
  21619. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  21620. + core_if->hwcfg3.d32 =
  21621. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  21622. + core_if->hwcfg4.d32 =
  21623. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  21624. +
  21625. + /* Force host mode to get HPTXFSIZ exact power on value */
  21626. + {
  21627. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  21628. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21629. + gusbcfg.b.force_host_mode = 1;
  21630. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  21631. + dwc_mdelay(100);
  21632. + core_if->hptxfsiz.d32 =
  21633. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  21634. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21635. + gusbcfg.b.force_host_mode = 1;
  21636. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  21637. + dwc_mdelay(100);
  21638. + }
  21639. +
  21640. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  21641. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  21642. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  21643. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  21644. +
  21645. + core_if->hcfg.d32 =
  21646. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  21647. + core_if->dcfg.d32 =
  21648. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  21649. +
  21650. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  21651. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  21652. +
  21653. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  21654. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  21655. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  21656. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  21657. + core_if->hwcfg2.b.num_host_chan);
  21658. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  21659. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  21660. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  21661. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  21662. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  21663. + core_if->hwcfg2.b.dev_token_q_depth);
  21664. +
  21665. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  21666. + core_if->hwcfg3.b.dfifo_depth);
  21667. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  21668. + core_if->hwcfg3.b.xfer_size_cntr_width);
  21669. +
  21670. + /*
  21671. + * Set the SRP sucess bit for FS-I2c
  21672. + */
  21673. + core_if->srp_success = 0;
  21674. + core_if->srp_timer_started = 0;
  21675. +
  21676. + /*
  21677. + * Create new workqueue and init works
  21678. + */
  21679. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  21680. + if (core_if->wq_otg == 0) {
  21681. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  21682. + DWC_FREE(host_if);
  21683. + DWC_FREE(dev_if);
  21684. + DWC_FREE(core_if);
  21685. + return 0;
  21686. + }
  21687. +
  21688. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  21689. +
  21690. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  21691. + (core_if->snpsid >> 12 & 0xF),
  21692. + (core_if->snpsid >> 8 & 0xF),
  21693. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  21694. +
  21695. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  21696. + w_wakeup_detected, core_if);
  21697. + if (core_if->wkp_timer == 0) {
  21698. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  21699. + DWC_FREE(host_if);
  21700. + DWC_FREE(dev_if);
  21701. + DWC_WORKQ_FREE(core_if->wq_otg);
  21702. + DWC_FREE(core_if);
  21703. + return 0;
  21704. + }
  21705. +
  21706. + if (dwc_otg_setup_params(core_if)) {
  21707. + DWC_WARN("Error while setting core params\n");
  21708. + }
  21709. +
  21710. + core_if->hibernation_suspend = 0;
  21711. +
  21712. + /** ADP initialization */
  21713. + dwc_otg_adp_init(core_if);
  21714. +
  21715. + return core_if;
  21716. +}
  21717. +
  21718. +/**
  21719. + * This function frees the structures allocated by dwc_otg_cil_init().
  21720. + *
  21721. + * @param core_if The core interface pointer returned from
  21722. + * dwc_otg_cil_init().
  21723. + *
  21724. + */
  21725. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  21726. +{
  21727. + dctl_data_t dctl = {.d32 = 0 };
  21728. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  21729. +
  21730. + /* Disable all interrupts */
  21731. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  21732. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  21733. +
  21734. + dctl.b.sftdiscon = 1;
  21735. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  21736. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  21737. + dctl.d32);
  21738. + }
  21739. +
  21740. + if (core_if->wq_otg) {
  21741. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  21742. + DWC_WORKQ_FREE(core_if->wq_otg);
  21743. + }
  21744. + if (core_if->dev_if) {
  21745. + DWC_FREE(core_if->dev_if);
  21746. + }
  21747. + if (core_if->host_if) {
  21748. + DWC_FREE(core_if->host_if);
  21749. + }
  21750. +
  21751. + /** Remove ADP Stuff */
  21752. + dwc_otg_adp_remove(core_if);
  21753. + if (core_if->core_params) {
  21754. + DWC_FREE(core_if->core_params);
  21755. + }
  21756. + if (core_if->wkp_timer) {
  21757. + DWC_TIMER_FREE(core_if->wkp_timer);
  21758. + }
  21759. + if (core_if->srp_timer) {
  21760. + DWC_TIMER_FREE(core_if->srp_timer);
  21761. + }
  21762. + DWC_FREE(core_if);
  21763. +}
  21764. +
  21765. +/**
  21766. + * This function enables the controller's Global Interrupt in the AHB Config
  21767. + * register.
  21768. + *
  21769. + * @param core_if Programming view of DWC_otg controller.
  21770. + */
  21771. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  21772. +{
  21773. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  21774. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  21775. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  21776. +}
  21777. +
  21778. +/**
  21779. + * This function disables the controller's Global Interrupt in the AHB Config
  21780. + * register.
  21781. + *
  21782. + * @param core_if Programming view of DWC_otg controller.
  21783. + */
  21784. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  21785. +{
  21786. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  21787. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  21788. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  21789. +}
  21790. +
  21791. +/**
  21792. + * This function initializes the commmon interrupts, used in both
  21793. + * device and host modes.
  21794. + *
  21795. + * @param core_if Programming view of the DWC_otg controller
  21796. + *
  21797. + */
  21798. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  21799. +{
  21800. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  21801. + gintmsk_data_t intr_mask = {.d32 = 0 };
  21802. +
  21803. + /* Clear any pending OTG Interrupts */
  21804. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  21805. +
  21806. + /* Clear any pending interrupts */
  21807. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  21808. +
  21809. + /*
  21810. + * Enable the interrupts in the GINTMSK.
  21811. + */
  21812. + intr_mask.b.modemismatch = 1;
  21813. + intr_mask.b.otgintr = 1;
  21814. +
  21815. + if (!core_if->dma_enable) {
  21816. + intr_mask.b.rxstsqlvl = 1;
  21817. + }
  21818. +
  21819. + intr_mask.b.conidstschng = 1;
  21820. + intr_mask.b.wkupintr = 1;
  21821. + intr_mask.b.disconnect = 0;
  21822. + intr_mask.b.usbsuspend = 1;
  21823. + intr_mask.b.sessreqintr = 1;
  21824. +#ifdef CONFIG_USB_DWC_OTG_LPM
  21825. + if (core_if->core_params->lpm_enable) {
  21826. + intr_mask.b.lpmtranrcvd = 1;
  21827. + }
  21828. +#endif
  21829. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  21830. +}
  21831. +
  21832. +/*
  21833. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  21834. + * Hibernation. This function is for exiting from Device mode hibernation by
  21835. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  21836. + * @param core_if Programming view of DWC_otg controller.
  21837. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  21838. + * @param reset - indicates whether resume is initiated by Reset.
  21839. + */
  21840. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  21841. + int rem_wakeup, int reset)
  21842. +{
  21843. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  21844. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  21845. + dctl_data_t dctl = {.d32 = 0 };
  21846. +
  21847. + int timeout = 2000;
  21848. +
  21849. + if (!core_if->hibernation_suspend) {
  21850. + DWC_PRINTF("Already exited from Hibernation\n");
  21851. + return 1;
  21852. + }
  21853. +
  21854. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  21855. + /* Switch-on voltage to the core */
  21856. + gpwrdn.b.pwrdnswtch = 1;
  21857. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21858. + dwc_udelay(10);
  21859. +
  21860. + /* Reset core */
  21861. + gpwrdn.d32 = 0;
  21862. + gpwrdn.b.pwrdnrstn = 1;
  21863. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21864. + dwc_udelay(10);
  21865. +
  21866. + /* Assert Restore signal */
  21867. + gpwrdn.d32 = 0;
  21868. + gpwrdn.b.restore = 1;
  21869. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21870. + dwc_udelay(10);
  21871. +
  21872. + /* Disable power clamps */
  21873. + gpwrdn.d32 = 0;
  21874. + gpwrdn.b.pwrdnclmp = 1;
  21875. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21876. +
  21877. + if (rem_wakeup) {
  21878. + dwc_udelay(70);
  21879. + }
  21880. +
  21881. + /* Deassert Reset core */
  21882. + gpwrdn.d32 = 0;
  21883. + gpwrdn.b.pwrdnrstn = 1;
  21884. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21885. + dwc_udelay(10);
  21886. +
  21887. + /* Disable PMU interrupt */
  21888. + gpwrdn.d32 = 0;
  21889. + gpwrdn.b.pmuintsel = 1;
  21890. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21891. +
  21892. + /* Mask interrupts from gpwrdn */
  21893. + gpwrdn.d32 = 0;
  21894. + gpwrdn.b.connect_det_msk = 1;
  21895. + gpwrdn.b.srp_det_msk = 1;
  21896. + gpwrdn.b.disconn_det_msk = 1;
  21897. + gpwrdn.b.rst_det_msk = 1;
  21898. + gpwrdn.b.lnstchng_msk = 1;
  21899. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21900. +
  21901. + /* Indicates that we are going out from hibernation */
  21902. + core_if->hibernation_suspend = 0;
  21903. +
  21904. + /*
  21905. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  21906. + * indicates restore from remote_wakeup
  21907. + */
  21908. + restore_essential_regs(core_if, rem_wakeup, 0);
  21909. +
  21910. + /*
  21911. + * Wait a little for seeing new value of variable hibernation_suspend if
  21912. + * Restore done interrupt received before polling
  21913. + */
  21914. + dwc_udelay(10);
  21915. +
  21916. + if (core_if->hibernation_suspend == 0) {
  21917. + /*
  21918. + * Wait For Restore_done Interrupt. This mechanism of polling the
  21919. + * interrupt is introduced to avoid any possible race conditions
  21920. + */
  21921. + do {
  21922. + gintsts_data_t gintsts;
  21923. + gintsts.d32 =
  21924. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  21925. + if (gintsts.b.restoredone) {
  21926. + gintsts.d32 = 0;
  21927. + gintsts.b.restoredone = 1;
  21928. + DWC_WRITE_REG32(&core_if->core_global_regs->
  21929. + gintsts, gintsts.d32);
  21930. + DWC_PRINTF("Restore Done Interrupt seen\n");
  21931. + break;
  21932. + }
  21933. + dwc_udelay(10);
  21934. + } while (--timeout);
  21935. + if (!timeout) {
  21936. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  21937. + }
  21938. + }
  21939. + /* Clear all pending interupts */
  21940. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21941. +
  21942. + /* De-assert Restore */
  21943. + gpwrdn.d32 = 0;
  21944. + gpwrdn.b.restore = 1;
  21945. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21946. + dwc_udelay(10);
  21947. +
  21948. + if (!rem_wakeup) {
  21949. + pcgcctl.d32 = 0;
  21950. + pcgcctl.b.rstpdwnmodule = 1;
  21951. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  21952. + }
  21953. +
  21954. + /* Restore GUSBCFG and DCFG */
  21955. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  21956. + core_if->gr_backup->gusbcfg_local);
  21957. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  21958. + core_if->dr_backup->dcfg);
  21959. +
  21960. + /* De-assert Wakeup Logic */
  21961. + gpwrdn.d32 = 0;
  21962. + gpwrdn.b.pmuactv = 1;
  21963. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21964. + dwc_udelay(10);
  21965. +
  21966. + if (!rem_wakeup) {
  21967. + /* Set Device programming done bit */
  21968. + dctl.b.pwronprgdone = 1;
  21969. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  21970. + } else {
  21971. + /* Start Remote Wakeup Signaling */
  21972. + dctl.d32 = core_if->dr_backup->dctl;
  21973. + dctl.b.rmtwkupsig = 1;
  21974. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  21975. + }
  21976. +
  21977. + dwc_mdelay(2);
  21978. + /* Clear all pending interupts */
  21979. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21980. +
  21981. + /* Restore global registers */
  21982. + dwc_otg_restore_global_regs(core_if);
  21983. + /* Restore device global registers */
  21984. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  21985. +
  21986. + if (rem_wakeup) {
  21987. + dwc_mdelay(7);
  21988. + dctl.d32 = 0;
  21989. + dctl.b.rmtwkupsig = 1;
  21990. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  21991. + }
  21992. +
  21993. + core_if->hibernation_suspend = 0;
  21994. + /* The core will be in ON STATE */
  21995. + core_if->lx_state = DWC_OTG_L0;
  21996. + DWC_PRINTF("Hibernation recovery completes here\n");
  21997. +
  21998. + return 1;
  21999. +}
  22000. +
  22001. +/*
  22002. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  22003. + * Hibernation. This function is for exiting from Host mode hibernation by
  22004. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  22005. + * @param core_if Programming view of DWC_otg controller.
  22006. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  22007. + * @param reset - indicates whether resume is initiated by Reset.
  22008. + */
  22009. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  22010. + int rem_wakeup, int reset)
  22011. +{
  22012. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  22013. + hprt0_data_t hprt0 = {.d32 = 0 };
  22014. +
  22015. + int timeout = 2000;
  22016. +
  22017. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  22018. + /* Switch-on voltage to the core */
  22019. + gpwrdn.b.pwrdnswtch = 1;
  22020. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22021. + dwc_udelay(10);
  22022. +
  22023. + /* Reset core */
  22024. + gpwrdn.d32 = 0;
  22025. + gpwrdn.b.pwrdnrstn = 1;
  22026. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22027. + dwc_udelay(10);
  22028. +
  22029. + /* Assert Restore signal */
  22030. + gpwrdn.d32 = 0;
  22031. + gpwrdn.b.restore = 1;
  22032. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  22033. + dwc_udelay(10);
  22034. +
  22035. + /* Disable power clamps */
  22036. + gpwrdn.d32 = 0;
  22037. + gpwrdn.b.pwrdnclmp = 1;
  22038. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22039. +
  22040. + if (!rem_wakeup) {
  22041. + dwc_udelay(50);
  22042. + }
  22043. +
  22044. + /* Deassert Reset core */
  22045. + gpwrdn.d32 = 0;
  22046. + gpwrdn.b.pwrdnrstn = 1;
  22047. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  22048. + dwc_udelay(10);
  22049. +
  22050. + /* Disable PMU interrupt */
  22051. + gpwrdn.d32 = 0;
  22052. + gpwrdn.b.pmuintsel = 1;
  22053. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22054. +
  22055. + gpwrdn.d32 = 0;
  22056. + gpwrdn.b.connect_det_msk = 1;
  22057. + gpwrdn.b.srp_det_msk = 1;
  22058. + gpwrdn.b.disconn_det_msk = 1;
  22059. + gpwrdn.b.rst_det_msk = 1;
  22060. + gpwrdn.b.lnstchng_msk = 1;
  22061. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22062. +
  22063. + /* Indicates that we are going out from hibernation */
  22064. + core_if->hibernation_suspend = 0;
  22065. +
  22066. + /* Set Restore Essential Regs bit in PCGCCTL register */
  22067. + restore_essential_regs(core_if, rem_wakeup, 1);
  22068. +
  22069. + /* Wait a little for seeing new value of variable hibernation_suspend if
  22070. + * Restore done interrupt received before polling */
  22071. + dwc_udelay(10);
  22072. +
  22073. + if (core_if->hibernation_suspend == 0) {
  22074. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  22075. + * interrupt is introduced to avoid any possible race conditions
  22076. + */
  22077. + do {
  22078. + gintsts_data_t gintsts;
  22079. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  22080. + if (gintsts.b.restoredone) {
  22081. + gintsts.d32 = 0;
  22082. + gintsts.b.restoredone = 1;
  22083. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  22084. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  22085. + break;
  22086. + }
  22087. + dwc_udelay(10);
  22088. + } while (--timeout);
  22089. + if (!timeout) {
  22090. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  22091. + }
  22092. + }
  22093. +
  22094. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  22095. + core_if->hibernation_suspend = 0;
  22096. +
  22097. + /* This step is not described in functional spec but if not wait for this
  22098. + * delay, mismatch interrupts occurred because just after restore core is
  22099. + * in Device mode(gintsts.curmode == 0) */
  22100. + dwc_mdelay(100);
  22101. +
  22102. + /* Clear all pending interrupts */
  22103. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22104. +
  22105. + /* De-assert Restore */
  22106. + gpwrdn.d32 = 0;
  22107. + gpwrdn.b.restore = 1;
  22108. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22109. + dwc_udelay(10);
  22110. +
  22111. + /* Restore GUSBCFG and HCFG */
  22112. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  22113. + core_if->gr_backup->gusbcfg_local);
  22114. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  22115. + core_if->hr_backup->hcfg_local);
  22116. +
  22117. + /* De-assert Wakeup Logic */
  22118. + gpwrdn.d32 = 0;
  22119. + gpwrdn.b.pmuactv = 1;
  22120. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22121. + dwc_udelay(10);
  22122. +
  22123. + /* Start the Resume operation by programming HPRT0 */
  22124. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22125. + hprt0.b.prtpwr = 1;
  22126. + hprt0.b.prtena = 0;
  22127. + hprt0.b.prtsusp = 0;
  22128. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22129. +
  22130. + DWC_PRINTF("Resume Starts Now\n");
  22131. + if (!reset) { // Indicates it is Resume Operation
  22132. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22133. + hprt0.b.prtres = 1;
  22134. + hprt0.b.prtpwr = 1;
  22135. + hprt0.b.prtena = 0;
  22136. + hprt0.b.prtsusp = 0;
  22137. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22138. +
  22139. + if (!rem_wakeup)
  22140. + hprt0.b.prtres = 0;
  22141. + /* Wait for Resume time and then program HPRT again */
  22142. + dwc_mdelay(100);
  22143. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22144. +
  22145. + } else { // Indicates it is Reset Operation
  22146. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22147. + hprt0.b.prtrst = 1;
  22148. + hprt0.b.prtpwr = 1;
  22149. + hprt0.b.prtena = 0;
  22150. + hprt0.b.prtsusp = 0;
  22151. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22152. + /* Wait for Reset time and then program HPRT again */
  22153. + dwc_mdelay(60);
  22154. + hprt0.b.prtrst = 0;
  22155. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22156. + }
  22157. + /* Clear all interrupt status */
  22158. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  22159. + hprt0.b.prtconndet = 1;
  22160. + hprt0.b.prtenchng = 1;
  22161. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22162. +
  22163. + /* Clear all pending interupts */
  22164. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22165. +
  22166. + /* Restore global registers */
  22167. + dwc_otg_restore_global_regs(core_if);
  22168. + /* Restore host global registers */
  22169. + dwc_otg_restore_host_regs(core_if, reset);
  22170. +
  22171. + /* The core will be in ON STATE */
  22172. + core_if->lx_state = DWC_OTG_L0;
  22173. + DWC_PRINTF("Hibernation recovery is complete here\n");
  22174. + return 0;
  22175. +}
  22176. +
  22177. +/** Saves some register values into system memory. */
  22178. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  22179. +{
  22180. + struct dwc_otg_global_regs_backup *gr;
  22181. + int i;
  22182. +
  22183. + gr = core_if->gr_backup;
  22184. + if (!gr) {
  22185. + gr = DWC_ALLOC(sizeof(*gr));
  22186. + if (!gr) {
  22187. + return -DWC_E_NO_MEMORY;
  22188. + }
  22189. + core_if->gr_backup = gr;
  22190. + }
  22191. +
  22192. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  22193. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  22194. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  22195. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  22196. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  22197. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  22198. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  22199. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22200. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  22201. +#endif
  22202. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  22203. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  22204. + gr->gdfifocfg_local =
  22205. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  22206. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  22207. + gr->dtxfsiz_local[i] =
  22208. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  22209. + }
  22210. +
  22211. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  22212. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  22213. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  22214. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  22215. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  22216. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  22217. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  22218. + gr->gnptxfsiz_local);
  22219. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  22220. + gr->hptxfsiz_local);
  22221. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22222. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  22223. +#endif
  22224. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  22225. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  22226. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  22227. +
  22228. + return 0;
  22229. +}
  22230. +
  22231. +/** Saves GINTMSK register before setting the msk bits. */
  22232. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  22233. +{
  22234. + struct dwc_otg_global_regs_backup *gr;
  22235. +
  22236. + gr = core_if->gr_backup;
  22237. + if (!gr) {
  22238. + gr = DWC_ALLOC(sizeof(*gr));
  22239. + if (!gr) {
  22240. + return -DWC_E_NO_MEMORY;
  22241. + }
  22242. + core_if->gr_backup = gr;
  22243. + }
  22244. +
  22245. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  22246. +
  22247. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  22248. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  22249. +
  22250. + return 0;
  22251. +}
  22252. +
  22253. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  22254. +{
  22255. + struct dwc_otg_dev_regs_backup *dr;
  22256. + int i;
  22257. +
  22258. + dr = core_if->dr_backup;
  22259. + if (!dr) {
  22260. + dr = DWC_ALLOC(sizeof(*dr));
  22261. + if (!dr) {
  22262. + return -DWC_E_NO_MEMORY;
  22263. + }
  22264. + core_if->dr_backup = dr;
  22265. + }
  22266. +
  22267. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  22268. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  22269. + dr->daintmsk =
  22270. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  22271. + dr->diepmsk =
  22272. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  22273. + dr->doepmsk =
  22274. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  22275. +
  22276. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22277. + dr->diepctl[i] =
  22278. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  22279. + dr->dieptsiz[i] =
  22280. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  22281. + dr->diepdma[i] =
  22282. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  22283. + }
  22284. +
  22285. + DWC_DEBUGPL(DBG_ANY,
  22286. + "=============Backing Host registers==============\n");
  22287. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  22288. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  22289. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  22290. + dr->daintmsk);
  22291. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  22292. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  22293. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22294. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  22295. + dr->diepctl[i]);
  22296. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  22297. + i, dr->dieptsiz[i]);
  22298. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  22299. + dr->diepdma[i]);
  22300. + }
  22301. +
  22302. + return 0;
  22303. +}
  22304. +
  22305. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  22306. +{
  22307. + struct dwc_otg_host_regs_backup *hr;
  22308. + int i;
  22309. +
  22310. + hr = core_if->hr_backup;
  22311. + if (!hr) {
  22312. + hr = DWC_ALLOC(sizeof(*hr));
  22313. + if (!hr) {
  22314. + return -DWC_E_NO_MEMORY;
  22315. + }
  22316. + core_if->hr_backup = hr;
  22317. + }
  22318. +
  22319. + hr->hcfg_local =
  22320. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  22321. + hr->haintmsk_local =
  22322. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  22323. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22324. + hr->hcintmsk_local[i] =
  22325. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  22326. + }
  22327. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  22328. + hr->hfir_local =
  22329. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  22330. +
  22331. + DWC_DEBUGPL(DBG_ANY,
  22332. + "=============Backing Host registers===============\n");
  22333. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  22334. + hr->hcfg_local);
  22335. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  22336. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22337. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  22338. + hr->hcintmsk_local[i]);
  22339. + }
  22340. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  22341. + hr->hprt0_local);
  22342. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  22343. + hr->hfir_local);
  22344. +
  22345. + return 0;
  22346. +}
  22347. +
  22348. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  22349. +{
  22350. + struct dwc_otg_global_regs_backup *gr;
  22351. + int i;
  22352. +
  22353. + gr = core_if->gr_backup;
  22354. + if (!gr) {
  22355. + return -DWC_E_INVALID;
  22356. + }
  22357. +
  22358. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  22359. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  22360. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  22361. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  22362. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  22363. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  22364. + gr->gnptxfsiz_local);
  22365. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  22366. + gr->hptxfsiz_local);
  22367. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  22368. + gr->gdfifocfg_local);
  22369. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  22370. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  22371. + gr->dtxfsiz_local[i]);
  22372. + }
  22373. +
  22374. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22375. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  22376. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  22377. + (gr->gahbcfg_local));
  22378. + return 0;
  22379. +}
  22380. +
  22381. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  22382. +{
  22383. + struct dwc_otg_dev_regs_backup *dr;
  22384. + int i;
  22385. +
  22386. + dr = core_if->dr_backup;
  22387. +
  22388. + if (!dr) {
  22389. + return -DWC_E_INVALID;
  22390. + }
  22391. +
  22392. + if (!rem_wakeup) {
  22393. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  22394. + dr->dctl);
  22395. + }
  22396. +
  22397. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  22398. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  22399. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  22400. +
  22401. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22402. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  22403. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  22404. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  22405. + }
  22406. +
  22407. + return 0;
  22408. +}
  22409. +
  22410. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  22411. +{
  22412. + struct dwc_otg_host_regs_backup *hr;
  22413. + int i;
  22414. + hr = core_if->hr_backup;
  22415. +
  22416. + if (!hr) {
  22417. + return -DWC_E_INVALID;
  22418. + }
  22419. +
  22420. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  22421. + //if (!reset)
  22422. + //{
  22423. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  22424. + //}
  22425. +
  22426. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  22427. + hr->haintmsk_local);
  22428. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22429. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  22430. + hr->hcintmsk_local[i]);
  22431. + }
  22432. +
  22433. + return 0;
  22434. +}
  22435. +
  22436. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  22437. +{
  22438. + struct dwc_otg_global_regs_backup *gr;
  22439. +
  22440. + gr = core_if->gr_backup;
  22441. +
  22442. + /* Restore values for LPM and I2C */
  22443. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22444. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  22445. +#endif
  22446. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  22447. +
  22448. + return 0;
  22449. +}
  22450. +
  22451. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  22452. +{
  22453. + struct dwc_otg_global_regs_backup *gr;
  22454. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  22455. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  22456. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  22457. + gintmsk_data_t gintmsk = {.d32 = 0 };
  22458. +
  22459. + /* Restore LPM and I2C registers */
  22460. + restore_lpm_i2c_regs(core_if);
  22461. +
  22462. + /* Set PCGCCTL to 0 */
  22463. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  22464. +
  22465. + gr = core_if->gr_backup;
  22466. + /* Load restore values for [31:14] bits */
  22467. + DWC_WRITE_REG32(core_if->pcgcctl,
  22468. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  22469. +
  22470. + /* Umnask global Interrupt in GAHBCFG and restore it */
  22471. + gahbcfg.d32 = gr->gahbcfg_local;
  22472. + gahbcfg.b.glblintrmsk = 1;
  22473. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  22474. +
  22475. + /* Clear all pending interupts */
  22476. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22477. +
  22478. + /* Unmask restore done interrupt */
  22479. + gintmsk.b.restoredone = 1;
  22480. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  22481. +
  22482. + /* Restore GUSBCFG and HCFG/DCFG */
  22483. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  22484. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  22485. +
  22486. + if (is_host) {
  22487. + hcfg_data_t hcfg = {.d32 = 0 };
  22488. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  22489. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  22490. + hcfg.d32);
  22491. +
  22492. + /* Load restore values for [31:14] bits */
  22493. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22494. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22495. +
  22496. + if (rmode)
  22497. + pcgcctl.b.restoremode = 1;
  22498. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22499. + dwc_udelay(10);
  22500. +
  22501. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  22502. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  22503. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22504. + pcgcctl.b.ess_reg_restored = 1;
  22505. + if (rmode)
  22506. + pcgcctl.b.restoremode = 1;
  22507. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22508. + } else {
  22509. + dcfg_data_t dcfg = {.d32 = 0 };
  22510. + dcfg.d32 = core_if->dr_backup->dcfg;
  22511. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  22512. +
  22513. + /* Load restore values for [31:14] bits */
  22514. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22515. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22516. + if (!rmode) {
  22517. + pcgcctl.d32 |= 0x208;
  22518. + }
  22519. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22520. + dwc_udelay(10);
  22521. +
  22522. + /* Load restore values for [31:14] bits */
  22523. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22524. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22525. + pcgcctl.b.ess_reg_restored = 1;
  22526. + if (!rmode)
  22527. + pcgcctl.d32 |= 0x208;
  22528. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22529. + }
  22530. +
  22531. + return 0;
  22532. +}
  22533. +
  22534. +/**
  22535. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  22536. + * type.
  22537. + */
  22538. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  22539. +{
  22540. + uint32_t val;
  22541. + hcfg_data_t hcfg;
  22542. +
  22543. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22544. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22545. + (core_if->core_params->ulpi_fs_ls)) ||
  22546. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22547. + /* Full speed PHY */
  22548. + val = DWC_HCFG_48_MHZ;
  22549. + } else {
  22550. + /* High speed PHY running at full speed or high speed */
  22551. + val = DWC_HCFG_30_60_MHZ;
  22552. + }
  22553. +
  22554. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  22555. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  22556. + hcfg.b.fslspclksel = val;
  22557. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  22558. +}
  22559. +
  22560. +/**
  22561. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  22562. + * and the enumeration speed of the device.
  22563. + */
  22564. +static void init_devspd(dwc_otg_core_if_t * core_if)
  22565. +{
  22566. + uint32_t val;
  22567. + dcfg_data_t dcfg;
  22568. +
  22569. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22570. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22571. + (core_if->core_params->ulpi_fs_ls)) ||
  22572. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22573. + /* Full speed PHY */
  22574. + val = 0x3;
  22575. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  22576. + /* High speed PHY running at full speed */
  22577. + val = 0x1;
  22578. + } else {
  22579. + /* High speed PHY running at high speed */
  22580. + val = 0x0;
  22581. + }
  22582. +
  22583. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  22584. +
  22585. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  22586. + dcfg.b.devspd = val;
  22587. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  22588. +}
  22589. +
  22590. +/**
  22591. + * This function calculates the number of IN EPS
  22592. + * using GHWCFG1 and GHWCFG2 registers values
  22593. + *
  22594. + * @param core_if Programming view of the DWC_otg controller
  22595. + */
  22596. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  22597. +{
  22598. + uint32_t num_in_eps = 0;
  22599. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  22600. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  22601. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  22602. + int i;
  22603. +
  22604. + for (i = 0; i < num_eps; ++i) {
  22605. + if (!(hwcfg1 & 0x1))
  22606. + num_in_eps++;
  22607. +
  22608. + hwcfg1 >>= 2;
  22609. + }
  22610. +
  22611. + if (core_if->hwcfg4.b.ded_fifo_en) {
  22612. + num_in_eps =
  22613. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  22614. + }
  22615. +
  22616. + return num_in_eps;
  22617. +}
  22618. +
  22619. +/**
  22620. + * This function calculates the number of OUT EPS
  22621. + * using GHWCFG1 and GHWCFG2 registers values
  22622. + *
  22623. + * @param core_if Programming view of the DWC_otg controller
  22624. + */
  22625. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  22626. +{
  22627. + uint32_t num_out_eps = 0;
  22628. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  22629. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  22630. + int i;
  22631. +
  22632. + for (i = 0; i < num_eps; ++i) {
  22633. + if (!(hwcfg1 & 0x1))
  22634. + num_out_eps++;
  22635. +
  22636. + hwcfg1 >>= 2;
  22637. + }
  22638. + return num_out_eps;
  22639. +}
  22640. +
  22641. +/**
  22642. + * This function initializes the DWC_otg controller registers and
  22643. + * prepares the core for device mode or host mode operation.
  22644. + *
  22645. + * @param core_if Programming view of the DWC_otg controller
  22646. + *
  22647. + */
  22648. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  22649. +{
  22650. + int i = 0;
  22651. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22652. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  22653. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  22654. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  22655. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  22656. +
  22657. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  22658. + core_if, global_regs);
  22659. +
  22660. + /* Common Initialization */
  22661. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22662. +
  22663. + /* Program the ULPI External VBUS bit if needed */
  22664. + usbcfg.b.ulpi_ext_vbus_drv =
  22665. + (core_if->core_params->phy_ulpi_ext_vbus ==
  22666. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  22667. +
  22668. + /* Set external TS Dline pulsing */
  22669. + usbcfg.b.term_sel_dl_pulse =
  22670. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  22671. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22672. +
  22673. + /* Reset the Controller */
  22674. + dwc_otg_core_reset(core_if);
  22675. +
  22676. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  22677. + core_if->power_down = core_if->core_params->power_down;
  22678. + core_if->otg_sts = 0;
  22679. +
  22680. + /* Initialize parameters from Hardware configuration registers. */
  22681. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  22682. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  22683. +
  22684. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  22685. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  22686. +
  22687. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  22688. + dev_if->perio_tx_fifo_size[i] =
  22689. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  22690. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  22691. + i, dev_if->perio_tx_fifo_size[i]);
  22692. + }
  22693. +
  22694. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  22695. + dev_if->tx_fifo_size[i] =
  22696. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  22697. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  22698. + i, dev_if->tx_fifo_size[i]);
  22699. + }
  22700. +
  22701. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  22702. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  22703. + core_if->nperio_tx_fifo_size =
  22704. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  22705. +
  22706. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  22707. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  22708. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  22709. + core_if->nperio_tx_fifo_size);
  22710. +
  22711. + /* This programming sequence needs to happen in FS mode before any other
  22712. + * programming occurs */
  22713. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  22714. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22715. + /* If FS mode with FS PHY */
  22716. +
  22717. + /* core_init() is now called on every switch so only call the
  22718. + * following for the first time through. */
  22719. + if (!core_if->phy_init_done) {
  22720. + core_if->phy_init_done = 1;
  22721. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  22722. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22723. + usbcfg.b.physel = 1;
  22724. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22725. +
  22726. + /* Reset after a PHY select */
  22727. + dwc_otg_core_reset(core_if);
  22728. + }
  22729. +
  22730. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  22731. + * do this on HNP Dev/Host mode switches (done in dev_init and
  22732. + * host_init). */
  22733. + if (dwc_otg_is_host_mode(core_if)) {
  22734. + init_fslspclksel(core_if);
  22735. + } else {
  22736. + init_devspd(core_if);
  22737. + }
  22738. +
  22739. + if (core_if->core_params->i2c_enable) {
  22740. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  22741. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  22742. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22743. + usbcfg.b.otgutmifssel = 1;
  22744. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22745. +
  22746. + /* Program GI2CCTL.I2CEn */
  22747. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  22748. + i2cctl.b.i2cdevaddr = 1;
  22749. + i2cctl.b.i2cen = 0;
  22750. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  22751. + i2cctl.b.i2cen = 1;
  22752. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  22753. + }
  22754. +
  22755. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  22756. + else {
  22757. + /* High speed PHY. */
  22758. + if (!core_if->phy_init_done) {
  22759. + core_if->phy_init_done = 1;
  22760. + /* HS PHY parameters. These parameters are preserved
  22761. + * during soft reset so only program the first time. Do
  22762. + * a soft reset immediately after setting phyif. */
  22763. +
  22764. + if (core_if->core_params->phy_type == 2) {
  22765. + /* ULPI interface */
  22766. + usbcfg.b.ulpi_utmi_sel = 1;
  22767. + usbcfg.b.phyif = 0;
  22768. + usbcfg.b.ddrsel =
  22769. + core_if->core_params->phy_ulpi_ddr;
  22770. + } else if (core_if->core_params->phy_type == 1) {
  22771. + /* UTMI+ interface */
  22772. + usbcfg.b.ulpi_utmi_sel = 0;
  22773. + if (core_if->core_params->phy_utmi_width == 16) {
  22774. + usbcfg.b.phyif = 1;
  22775. +
  22776. + } else {
  22777. + usbcfg.b.phyif = 0;
  22778. + }
  22779. + } else {
  22780. + DWC_ERROR("FS PHY TYPE\n");
  22781. + }
  22782. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22783. + /* Reset after setting the PHY parameters */
  22784. + dwc_otg_core_reset(core_if);
  22785. + }
  22786. + }
  22787. +
  22788. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22789. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22790. + (core_if->core_params->ulpi_fs_ls)) {
  22791. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  22792. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22793. + usbcfg.b.ulpi_fsls = 1;
  22794. + usbcfg.b.ulpi_clk_sus_m = 1;
  22795. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22796. + } else {
  22797. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22798. + usbcfg.b.ulpi_fsls = 0;
  22799. + usbcfg.b.ulpi_clk_sus_m = 0;
  22800. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22801. + }
  22802. +
  22803. + /* Program the GAHBCFG Register. */
  22804. + switch (core_if->hwcfg2.b.architecture) {
  22805. +
  22806. + case DWC_SLAVE_ONLY_ARCH:
  22807. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  22808. + ahbcfg.b.nptxfemplvl_txfemplvl =
  22809. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  22810. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  22811. + core_if->dma_enable = 0;
  22812. + core_if->dma_desc_enable = 0;
  22813. + break;
  22814. +
  22815. + case DWC_EXT_DMA_ARCH:
  22816. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  22817. + {
  22818. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  22819. + ahbcfg.b.hburstlen = 0;
  22820. + while (brst_sz > 1) {
  22821. + ahbcfg.b.hburstlen++;
  22822. + brst_sz >>= 1;
  22823. + }
  22824. + }
  22825. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  22826. + core_if->dma_desc_enable =
  22827. + (core_if->core_params->dma_desc_enable != 0);
  22828. + break;
  22829. +
  22830. + case DWC_INT_DMA_ARCH:
  22831. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  22832. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  22833. + Host mode ISOC in issue fix - vahrama */
  22834. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  22835. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  22836. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  22837. + core_if->dma_desc_enable =
  22838. + (core_if->core_params->dma_desc_enable != 0);
  22839. + break;
  22840. +
  22841. + }
  22842. + if (core_if->dma_enable) {
  22843. + if (core_if->dma_desc_enable) {
  22844. + DWC_PRINTF("Using Descriptor DMA mode\n");
  22845. + } else {
  22846. + DWC_PRINTF("Using Buffer DMA mode\n");
  22847. +
  22848. + }
  22849. + } else {
  22850. + DWC_PRINTF("Using Slave mode\n");
  22851. + core_if->dma_desc_enable = 0;
  22852. + }
  22853. +
  22854. + if (core_if->core_params->ahb_single) {
  22855. + ahbcfg.b.ahbsingle = 1;
  22856. + }
  22857. +
  22858. + ahbcfg.b.dmaenable = core_if->dma_enable;
  22859. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  22860. +
  22861. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  22862. +
  22863. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  22864. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  22865. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  22866. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  22867. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  22868. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  22869. +
  22870. + /*
  22871. + * Program the GUSBCFG register.
  22872. + */
  22873. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22874. +
  22875. + switch (core_if->hwcfg2.b.op_mode) {
  22876. + case DWC_MODE_HNP_SRP_CAPABLE:
  22877. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  22878. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  22879. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22880. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22881. + break;
  22882. +
  22883. + case DWC_MODE_SRP_ONLY_CAPABLE:
  22884. + usbcfg.b.hnpcap = 0;
  22885. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22886. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22887. + break;
  22888. +
  22889. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  22890. + usbcfg.b.hnpcap = 0;
  22891. + usbcfg.b.srpcap = 0;
  22892. + break;
  22893. +
  22894. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  22895. + usbcfg.b.hnpcap = 0;
  22896. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22897. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22898. + break;
  22899. +
  22900. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  22901. + usbcfg.b.hnpcap = 0;
  22902. + usbcfg.b.srpcap = 0;
  22903. + break;
  22904. +
  22905. + case DWC_MODE_SRP_CAPABLE_HOST:
  22906. + usbcfg.b.hnpcap = 0;
  22907. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22908. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22909. + break;
  22910. +
  22911. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  22912. + usbcfg.b.hnpcap = 0;
  22913. + usbcfg.b.srpcap = 0;
  22914. + break;
  22915. + }
  22916. +
  22917. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22918. +
  22919. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22920. + if (core_if->core_params->lpm_enable) {
  22921. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  22922. +
  22923. + /* To enable LPM support set lpm_cap_en bit */
  22924. + lpmcfg.b.lpm_cap_en = 1;
  22925. +
  22926. + /* Make AppL1Res ACK */
  22927. + lpmcfg.b.appl_resp = 1;
  22928. +
  22929. + /* Retry 3 times */
  22930. + lpmcfg.b.retry_count = 3;
  22931. +
  22932. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  22933. + 0, lpmcfg.d32);
  22934. +
  22935. + }
  22936. +#endif
  22937. + if (core_if->core_params->ic_usb_cap) {
  22938. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  22939. + gusbcfg.b.ic_usb_cap = 1;
  22940. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  22941. + 0, gusbcfg.d32);
  22942. + }
  22943. + {
  22944. + gotgctl_data_t gotgctl = {.d32 = 0 };
  22945. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  22946. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  22947. + gotgctl.d32);
  22948. + /* Set OTG version supported */
  22949. + core_if->otg_ver = core_if->core_params->otg_ver;
  22950. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  22951. + core_if->core_params->otg_ver, core_if->otg_ver);
  22952. + }
  22953. +
  22954. +
  22955. + /* Enable common interrupts */
  22956. + dwc_otg_enable_common_interrupts(core_if);
  22957. +
  22958. + /* Do device or host intialization based on mode during PCD
  22959. + * and HCD initialization */
  22960. + if (dwc_otg_is_host_mode(core_if)) {
  22961. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  22962. + core_if->op_state = A_HOST;
  22963. + } else {
  22964. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  22965. + core_if->op_state = B_PERIPHERAL;
  22966. +#ifdef DWC_DEVICE_ONLY
  22967. + dwc_otg_core_dev_init(core_if);
  22968. +#endif
  22969. + }
  22970. +}
  22971. +
  22972. +/**
  22973. + * This function enables the Device mode interrupts.
  22974. + *
  22975. + * @param core_if Programming view of DWC_otg controller
  22976. + */
  22977. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  22978. +{
  22979. + gintmsk_data_t intr_mask = {.d32 = 0 };
  22980. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22981. +
  22982. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  22983. +
  22984. + /* Disable all interrupts. */
  22985. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  22986. +
  22987. + /* Clear any pending interrupts */
  22988. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  22989. +
  22990. + /* Enable the common interrupts */
  22991. + dwc_otg_enable_common_interrupts(core_if);
  22992. +
  22993. + /* Enable interrupts */
  22994. + intr_mask.b.usbreset = 1;
  22995. + intr_mask.b.enumdone = 1;
  22996. + /* Disable Disconnect interrupt in Device mode */
  22997. + intr_mask.b.disconnect = 0;
  22998. +
  22999. + if (!core_if->multiproc_int_enable) {
  23000. + intr_mask.b.inepintr = 1;
  23001. + intr_mask.b.outepintr = 1;
  23002. + }
  23003. +
  23004. + intr_mask.b.erlysuspend = 1;
  23005. +
  23006. + if (core_if->en_multiple_tx_fifo == 0) {
  23007. + intr_mask.b.epmismatch = 1;
  23008. + }
  23009. +
  23010. + //intr_mask.b.incomplisoout = 1;
  23011. + intr_mask.b.incomplisoin = 1;
  23012. +
  23013. +/* Enable the ignore frame number for ISOC xfers - MAS */
  23014. +/* Disable to support high bandwith ISOC transfers - manukz */
  23015. +#if 0
  23016. +#ifdef DWC_UTE_PER_IO
  23017. + if (core_if->dma_enable) {
  23018. + if (core_if->dma_desc_enable) {
  23019. + dctl_data_t dctl1 = {.d32 = 0 };
  23020. + dctl1.b.ifrmnum = 1;
  23021. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  23022. + dctl, 0, dctl1.d32);
  23023. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  23024. + DWC_READ_REG32(&core_if->dev_if->
  23025. + dev_global_regs->dctl));
  23026. + }
  23027. + }
  23028. +#endif
  23029. +#endif
  23030. +#ifdef DWC_EN_ISOC
  23031. + if (core_if->dma_enable) {
  23032. + if (core_if->dma_desc_enable == 0) {
  23033. + if (core_if->pti_enh_enable) {
  23034. + dctl_data_t dctl = {.d32 = 0 };
  23035. + dctl.b.ifrmnum = 1;
  23036. + DWC_MODIFY_REG32(&core_if->
  23037. + dev_if->dev_global_regs->dctl,
  23038. + 0, dctl.d32);
  23039. + } else {
  23040. + intr_mask.b.incomplisoin = 1;
  23041. + intr_mask.b.incomplisoout = 1;
  23042. + }
  23043. + }
  23044. + } else {
  23045. + intr_mask.b.incomplisoin = 1;
  23046. + intr_mask.b.incomplisoout = 1;
  23047. + }
  23048. +#endif /* DWC_EN_ISOC */
  23049. +
  23050. + /** @todo NGS: Should this be a module parameter? */
  23051. +#ifdef USE_PERIODIC_EP
  23052. + intr_mask.b.isooutdrop = 1;
  23053. + intr_mask.b.eopframe = 1;
  23054. + intr_mask.b.incomplisoin = 1;
  23055. + intr_mask.b.incomplisoout = 1;
  23056. +#endif
  23057. +
  23058. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  23059. +
  23060. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  23061. + DWC_READ_REG32(&global_regs->gintmsk));
  23062. +}
  23063. +
  23064. +/**
  23065. + * This function initializes the DWC_otg controller registers for
  23066. + * device mode.
  23067. + *
  23068. + * @param core_if Programming view of DWC_otg controller
  23069. + *
  23070. + */
  23071. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  23072. +{
  23073. + int i;
  23074. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23075. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  23076. + dwc_otg_core_params_t *params = core_if->core_params;
  23077. + dcfg_data_t dcfg = {.d32 = 0 };
  23078. + depctl_data_t diepctl = {.d32 = 0 };
  23079. + grstctl_t resetctl = {.d32 = 0 };
  23080. + uint32_t rx_fifo_size;
  23081. + fifosize_data_t nptxfifosize;
  23082. + fifosize_data_t txfifosize;
  23083. + dthrctl_data_t dthrctl;
  23084. + fifosize_data_t ptxfifosize;
  23085. + uint16_t rxfsiz, nptxfsiz;
  23086. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  23087. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  23088. +
  23089. + /* Restart the Phy Clock */
  23090. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  23091. +
  23092. + /* Device configuration register */
  23093. + init_devspd(core_if);
  23094. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  23095. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  23096. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  23097. + /* Enable Device OUT NAK in case of DDMA mode*/
  23098. + if (core_if->core_params->dev_out_nak) {
  23099. + dcfg.b.endevoutnak = 1;
  23100. + }
  23101. +
  23102. + if (core_if->core_params->cont_on_bna) {
  23103. + dctl_data_t dctl = {.d32 = 0 };
  23104. + dctl.b.encontonbna = 1;
  23105. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23106. + }
  23107. +
  23108. +
  23109. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  23110. +
  23111. + /* Configure data FIFO sizes */
  23112. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  23113. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  23114. + core_if->total_fifo_size);
  23115. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  23116. + params->dev_rx_fifo_size);
  23117. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  23118. + params->dev_nperio_tx_fifo_size);
  23119. +
  23120. + /* Rx FIFO */
  23121. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  23122. + DWC_READ_REG32(&global_regs->grxfsiz));
  23123. +
  23124. +#ifdef DWC_UTE_CFI
  23125. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  23126. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  23127. +#endif
  23128. + rx_fifo_size = params->dev_rx_fifo_size;
  23129. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  23130. +
  23131. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  23132. + DWC_READ_REG32(&global_regs->grxfsiz));
  23133. +
  23134. + /** Set Periodic Tx FIFO Mask all bits 0 */
  23135. + core_if->p_tx_msk = 0;
  23136. +
  23137. + /** Set Tx FIFO Mask all bits 0 */
  23138. + core_if->tx_msk = 0;
  23139. +
  23140. + if (core_if->en_multiple_tx_fifo == 0) {
  23141. + /* Non-periodic Tx FIFO */
  23142. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23143. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23144. +
  23145. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  23146. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  23147. +
  23148. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  23149. + nptxfifosize.d32);
  23150. +
  23151. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23152. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23153. +
  23154. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  23155. + /*
  23156. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  23157. + * Indexes of the FIFO size module parameters in the
  23158. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  23159. + * the dptxfsiz array run from 0 to 14.
  23160. + */
  23161. + /** @todo Finish debug of this */
  23162. + ptxfifosize.b.startaddr =
  23163. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23164. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  23165. + ptxfifosize.b.depth =
  23166. + params->dev_perio_tx_fifo_size[i];
  23167. + DWC_DEBUGPL(DBG_CIL,
  23168. + "initial dtxfsiz[%d]=%08x\n", i,
  23169. + DWC_READ_REG32(&global_regs->dtxfsiz
  23170. + [i]));
  23171. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  23172. + ptxfifosize.d32);
  23173. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  23174. + i,
  23175. + DWC_READ_REG32(&global_regs->dtxfsiz
  23176. + [i]));
  23177. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  23178. + }
  23179. + } else {
  23180. + /*
  23181. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  23182. + * Indexes of the FIFO size module parameters in the
  23183. + * dev_tx_fifo_size array and the FIFO size registers in
  23184. + * the dtxfsiz array run from 0 to 14.
  23185. + */
  23186. +
  23187. + /* Non-periodic Tx FIFO */
  23188. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23189. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23190. +
  23191. +#ifdef DWC_UTE_CFI
  23192. + core_if->pwron_gnptxfsiz =
  23193. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23194. + core_if->init_gnptxfsiz =
  23195. + params->dev_nperio_tx_fifo_size;
  23196. +#endif
  23197. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  23198. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  23199. +
  23200. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  23201. + nptxfifosize.d32);
  23202. +
  23203. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23204. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23205. +
  23206. + txfifosize.b.startaddr =
  23207. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23208. +
  23209. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  23210. +
  23211. + txfifosize.b.depth =
  23212. + params->dev_tx_fifo_size[i];
  23213. +
  23214. + DWC_DEBUGPL(DBG_CIL,
  23215. + "initial dtxfsiz[%d]=%08x\n",
  23216. + i,
  23217. + DWC_READ_REG32(&global_regs->dtxfsiz
  23218. + [i]));
  23219. +
  23220. +#ifdef DWC_UTE_CFI
  23221. + core_if->pwron_txfsiz[i] =
  23222. + (DWC_READ_REG32
  23223. + (&global_regs->dtxfsiz[i]) >> 16);
  23224. + core_if->init_txfsiz[i] =
  23225. + params->dev_tx_fifo_size[i];
  23226. +#endif
  23227. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  23228. + txfifosize.d32);
  23229. +
  23230. + DWC_DEBUGPL(DBG_CIL,
  23231. + "new dtxfsiz[%d]=%08x\n",
  23232. + i,
  23233. + DWC_READ_REG32(&global_regs->dtxfsiz
  23234. + [i]));
  23235. +
  23236. + txfifosize.b.startaddr += txfifosize.b.depth;
  23237. + }
  23238. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  23239. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  23240. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  23241. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  23242. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  23243. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23244. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  23245. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23246. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  23247. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23248. + }
  23249. + }
  23250. +
  23251. + /* Flush the FIFOs */
  23252. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  23253. + dwc_otg_flush_rx_fifo(core_if);
  23254. +
  23255. + /* Flush the Learning Queue. */
  23256. + resetctl.b.intknqflsh = 1;
  23257. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  23258. +
  23259. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  23260. + core_if->start_predict = 0;
  23261. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  23262. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  23263. + }
  23264. + core_if->nextep_seq[0] = 0;
  23265. + core_if->first_in_nextep_seq = 0;
  23266. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  23267. + diepctl.b.nextep = 0;
  23268. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  23269. +
  23270. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  23271. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  23272. + dcfg.b.epmscnt = 2;
  23273. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  23274. +
  23275. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  23276. + __func__, core_if->first_in_nextep_seq);
  23277. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  23278. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  23279. + }
  23280. + DWC_DEBUGPL(DBG_CILV,"\n");
  23281. + }
  23282. +
  23283. + /* Clear all pending Device Interrupts */
  23284. + /** @todo - if the condition needed to be checked
  23285. + * or in any case all pending interrutps should be cleared?
  23286. + */
  23287. + if (core_if->multiproc_int_enable) {
  23288. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  23289. + DWC_WRITE_REG32(&dev_if->
  23290. + dev_global_regs->diepeachintmsk[i], 0);
  23291. + }
  23292. + }
  23293. +
  23294. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  23295. + DWC_WRITE_REG32(&dev_if->
  23296. + dev_global_regs->doepeachintmsk[i], 0);
  23297. + }
  23298. +
  23299. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  23300. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  23301. + } else {
  23302. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  23303. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  23304. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  23305. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  23306. + }
  23307. +
  23308. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  23309. + depctl_data_t depctl;
  23310. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  23311. + if (depctl.b.epena) {
  23312. + depctl.d32 = 0;
  23313. + depctl.b.epdis = 1;
  23314. + depctl.b.snak = 1;
  23315. + } else {
  23316. + depctl.d32 = 0;
  23317. + }
  23318. +
  23319. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  23320. +
  23321. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  23322. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  23323. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  23324. + }
  23325. +
  23326. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  23327. + depctl_data_t depctl;
  23328. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  23329. + if (depctl.b.epena) {
  23330. + dctl_data_t dctl = {.d32 = 0 };
  23331. + gintmsk_data_t gintsts = {.d32 = 0 };
  23332. + doepint_data_t doepint = {.d32 = 0 };
  23333. + dctl.b.sgoutnak = 1;
  23334. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23335. + do {
  23336. + dwc_udelay(10);
  23337. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  23338. + } while (!gintsts.b.goutnakeff);
  23339. + gintsts.d32 = 0;
  23340. + gintsts.b.goutnakeff = 1;
  23341. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  23342. +
  23343. + depctl.d32 = 0;
  23344. + depctl.b.epdis = 1;
  23345. + depctl.b.snak = 1;
  23346. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  23347. + do {
  23348. + dwc_udelay(10);
  23349. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  23350. + out_ep_regs[i]->doepint);
  23351. + } while (!doepint.b.epdisabled);
  23352. +
  23353. + doepint.b.epdisabled = 1;
  23354. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  23355. +
  23356. + dctl.d32 = 0;
  23357. + dctl.b.cgoutnak = 1;
  23358. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23359. + } else {
  23360. + depctl.d32 = 0;
  23361. + }
  23362. +
  23363. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  23364. +
  23365. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  23366. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  23367. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  23368. + }
  23369. +
  23370. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  23371. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  23372. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  23373. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  23374. +
  23375. + dev_if->rx_thr_length = params->rx_thr_length;
  23376. + dev_if->tx_thr_length = params->tx_thr_length;
  23377. +
  23378. + dev_if->setup_desc_index = 0;
  23379. +
  23380. + dthrctl.d32 = 0;
  23381. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  23382. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  23383. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  23384. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  23385. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  23386. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  23387. +
  23388. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  23389. + dthrctl.d32);
  23390. +
  23391. + DWC_DEBUGPL(DBG_CIL,
  23392. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  23393. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  23394. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  23395. + dthrctl.b.rx_thr_len);
  23396. +
  23397. + }
  23398. +
  23399. + dwc_otg_enable_device_interrupts(core_if);
  23400. +
  23401. + {
  23402. + diepmsk_data_t msk = {.d32 = 0 };
  23403. + msk.b.txfifoundrn = 1;
  23404. + if (core_if->multiproc_int_enable) {
  23405. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  23406. + diepeachintmsk[0], msk.d32, msk.d32);
  23407. + } else {
  23408. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  23409. + msk.d32, msk.d32);
  23410. + }
  23411. + }
  23412. +
  23413. + if (core_if->multiproc_int_enable) {
  23414. + /* Set NAK on Babble */
  23415. + dctl_data_t dctl = {.d32 = 0 };
  23416. + dctl.b.nakonbble = 1;
  23417. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23418. + }
  23419. +
  23420. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  23421. + dctl_data_t dctl = {.d32 = 0 };
  23422. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  23423. + dctl.b.sftdiscon = 0;
  23424. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  23425. + }
  23426. +}
  23427. +
  23428. +/**
  23429. + * This function enables the Host mode interrupts.
  23430. + *
  23431. + * @param core_if Programming view of DWC_otg controller
  23432. + */
  23433. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  23434. +{
  23435. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23436. + gintmsk_data_t intr_mask = {.d32 = 0 };
  23437. +
  23438. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  23439. +
  23440. + /* Disable all interrupts. */
  23441. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  23442. +
  23443. + /* Clear any pending interrupts. */
  23444. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  23445. +
  23446. + /* Enable the common interrupts */
  23447. + dwc_otg_enable_common_interrupts(core_if);
  23448. +
  23449. + /*
  23450. + * Enable host mode interrupts without disturbing common
  23451. + * interrupts.
  23452. + */
  23453. +
  23454. + intr_mask.b.disconnect = 1;
  23455. + intr_mask.b.portintr = 1;
  23456. + intr_mask.b.hcintr = 1;
  23457. +
  23458. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  23459. +}
  23460. +
  23461. +/**
  23462. + * This function disables the Host Mode interrupts.
  23463. + *
  23464. + * @param core_if Programming view of DWC_otg controller
  23465. + */
  23466. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  23467. +{
  23468. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23469. + gintmsk_data_t intr_mask = {.d32 = 0 };
  23470. +
  23471. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  23472. +
  23473. + /*
  23474. + * Disable host mode interrupts without disturbing common
  23475. + * interrupts.
  23476. + */
  23477. + intr_mask.b.sofintr = 1;
  23478. + intr_mask.b.portintr = 1;
  23479. + intr_mask.b.hcintr = 1;
  23480. + intr_mask.b.ptxfempty = 1;
  23481. + intr_mask.b.nptxfempty = 1;
  23482. +
  23483. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  23484. +}
  23485. +
  23486. +/**
  23487. + * This function initializes the DWC_otg controller registers for
  23488. + * host mode.
  23489. + *
  23490. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  23491. + * request queues. Host channels are reset to ensure that they are ready for
  23492. + * performing transfers.
  23493. + *
  23494. + * @param core_if Programming view of DWC_otg controller
  23495. + *
  23496. + */
  23497. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  23498. +{
  23499. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23500. + dwc_otg_host_if_t *host_if = core_if->host_if;
  23501. + dwc_otg_core_params_t *params = core_if->core_params;
  23502. + hprt0_data_t hprt0 = {.d32 = 0 };
  23503. + fifosize_data_t nptxfifosize;
  23504. + fifosize_data_t ptxfifosize;
  23505. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  23506. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  23507. + int i;
  23508. + hcchar_data_t hcchar;
  23509. + hcfg_data_t hcfg;
  23510. + hfir_data_t hfir;
  23511. + dwc_otg_hc_regs_t *hc_regs;
  23512. + int num_channels;
  23513. + gotgctl_data_t gotgctl = {.d32 = 0 };
  23514. +
  23515. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  23516. +
  23517. + /* Restart the Phy Clock */
  23518. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  23519. +
  23520. + /* Initialize Host Configuration Register */
  23521. + init_fslspclksel(core_if);
  23522. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  23523. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  23524. + hcfg.b.fslssupp = 1;
  23525. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  23526. +
  23527. + }
  23528. +
  23529. + /* This bit allows dynamic reloading of the HFIR register
  23530. + * during runtime. This bit needs to be programmed during
  23531. + * initial configuration and its value must not be changed
  23532. + * during runtime.*/
  23533. + if (core_if->core_params->reload_ctl == 1) {
  23534. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  23535. + hfir.b.hfirrldctrl = 1;
  23536. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  23537. + }
  23538. +
  23539. + if (core_if->core_params->dma_desc_enable) {
  23540. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  23541. + if (!
  23542. + (core_if->hwcfg4.b.desc_dma
  23543. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  23544. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  23545. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  23546. + || (op_mode ==
  23547. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  23548. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  23549. + || (op_mode ==
  23550. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  23551. +
  23552. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  23553. + "Either core version is below 2.90a or "
  23554. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  23555. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  23556. + "module parameter to 0.\n");
  23557. + return;
  23558. + }
  23559. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  23560. + hcfg.b.descdma = 1;
  23561. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  23562. + }
  23563. +
  23564. + /* Configure data FIFO sizes */
  23565. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  23566. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  23567. + core_if->total_fifo_size);
  23568. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  23569. + params->host_rx_fifo_size);
  23570. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  23571. + params->host_nperio_tx_fifo_size);
  23572. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  23573. + params->host_perio_tx_fifo_size);
  23574. +
  23575. + /* Rx FIFO */
  23576. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  23577. + DWC_READ_REG32(&global_regs->grxfsiz));
  23578. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  23579. + params->host_rx_fifo_size);
  23580. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  23581. + DWC_READ_REG32(&global_regs->grxfsiz));
  23582. +
  23583. + /* Non-periodic Tx FIFO */
  23584. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23585. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23586. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  23587. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  23588. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  23589. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23590. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23591. +
  23592. + /* Periodic Tx FIFO */
  23593. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  23594. + DWC_READ_REG32(&global_regs->hptxfsiz));
  23595. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  23596. + ptxfifosize.b.startaddr =
  23597. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23598. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  23599. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  23600. + DWC_READ_REG32(&global_regs->hptxfsiz));
  23601. +
  23602. + if (core_if->en_multiple_tx_fifo
  23603. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  23604. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  23605. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  23606. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  23607. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23608. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  23609. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  23610. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23611. + }
  23612. + }
  23613. +
  23614. + /* TODO - check this */
  23615. + /* Clear Host Set HNP Enable in the OTG Control Register */
  23616. + gotgctl.b.hstsethnpen = 1;
  23617. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  23618. + /* Make sure the FIFOs are flushed. */
  23619. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  23620. + dwc_otg_flush_rx_fifo(core_if);
  23621. +
  23622. + /* Clear Host Set HNP Enable in the OTG Control Register */
  23623. + gotgctl.b.hstsethnpen = 1;
  23624. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  23625. +
  23626. + if (!core_if->core_params->dma_desc_enable) {
  23627. + /* Flush out any leftover queued requests. */
  23628. + num_channels = core_if->core_params->host_channels;
  23629. +
  23630. + for (i = 0; i < num_channels; i++) {
  23631. + hc_regs = core_if->host_if->hc_regs[i];
  23632. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23633. + hcchar.b.chen = 0;
  23634. + hcchar.b.chdis = 1;
  23635. + hcchar.b.epdir = 0;
  23636. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23637. + }
  23638. +
  23639. + /* Halt all channels to put them into a known state. */
  23640. + for (i = 0; i < num_channels; i++) {
  23641. + int count = 0;
  23642. + hc_regs = core_if->host_if->hc_regs[i];
  23643. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23644. + hcchar.b.chen = 1;
  23645. + hcchar.b.chdis = 1;
  23646. + hcchar.b.epdir = 0;
  23647. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23648. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  23649. + do {
  23650. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23651. + if (++count > 1000) {
  23652. + DWC_ERROR
  23653. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  23654. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  23655. + break;
  23656. + }
  23657. + dwc_udelay(1);
  23658. + } while (hcchar.b.chen);
  23659. + }
  23660. + }
  23661. +
  23662. + /* Turn on the vbus power. */
  23663. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  23664. + if (core_if->op_state == A_HOST) {
  23665. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  23666. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  23667. + if (hprt0.b.prtpwr == 0) {
  23668. + hprt0.b.prtpwr = 1;
  23669. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  23670. + }
  23671. + }
  23672. +
  23673. + dwc_otg_enable_host_interrupts(core_if);
  23674. +}
  23675. +
  23676. +/**
  23677. + * Prepares a host channel for transferring packets to/from a specific
  23678. + * endpoint. The HCCHARn register is set up with the characteristics specified
  23679. + * in _hc. Host channel interrupts that may need to be serviced while this
  23680. + * transfer is in progress are enabled.
  23681. + *
  23682. + * @param core_if Programming view of DWC_otg controller
  23683. + * @param hc Information needed to initialize the host channel
  23684. + */
  23685. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23686. +{
  23687. + hcintmsk_data_t hc_intr_mask;
  23688. + hcchar_data_t hcchar;
  23689. + hcsplt_data_t hcsplt;
  23690. +
  23691. + uint8_t hc_num = hc->hc_num;
  23692. + dwc_otg_host_if_t *host_if = core_if->host_if;
  23693. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  23694. +
  23695. + /* Clear old interrupt conditions for this host channel. */
  23696. + hc_intr_mask.d32 = 0xFFFFFFFF;
  23697. + hc_intr_mask.b.reserved14_31 = 0;
  23698. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  23699. +
  23700. + /* Enable channel interrupts required for this transfer. */
  23701. + hc_intr_mask.d32 = 0;
  23702. + hc_intr_mask.b.chhltd = 1;
  23703. + if (core_if->dma_enable) {
  23704. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  23705. + if (!core_if->dma_desc_enable)
  23706. + hc_intr_mask.b.ahberr = 1;
  23707. + else {
  23708. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  23709. + hc_intr_mask.b.xfercompl = 1;
  23710. + }
  23711. +
  23712. + if (hc->error_state && !hc->do_split &&
  23713. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  23714. + hc_intr_mask.b.ack = 1;
  23715. + if (hc->ep_is_in) {
  23716. + hc_intr_mask.b.datatglerr = 1;
  23717. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  23718. + hc_intr_mask.b.nak = 1;
  23719. + }
  23720. + }
  23721. + }
  23722. + } else {
  23723. + switch (hc->ep_type) {
  23724. + case DWC_OTG_EP_TYPE_CONTROL:
  23725. + case DWC_OTG_EP_TYPE_BULK:
  23726. + hc_intr_mask.b.xfercompl = 1;
  23727. + hc_intr_mask.b.stall = 1;
  23728. + hc_intr_mask.b.xacterr = 1;
  23729. + hc_intr_mask.b.datatglerr = 1;
  23730. + if (hc->ep_is_in) {
  23731. + hc_intr_mask.b.bblerr = 1;
  23732. + } else {
  23733. + hc_intr_mask.b.nak = 1;
  23734. + hc_intr_mask.b.nyet = 1;
  23735. + if (hc->do_ping) {
  23736. + hc_intr_mask.b.ack = 1;
  23737. + }
  23738. + }
  23739. +
  23740. + if (hc->do_split) {
  23741. + hc_intr_mask.b.nak = 1;
  23742. + if (hc->complete_split) {
  23743. + hc_intr_mask.b.nyet = 1;
  23744. + } else {
  23745. + hc_intr_mask.b.ack = 1;
  23746. + }
  23747. + }
  23748. +
  23749. + if (hc->error_state) {
  23750. + hc_intr_mask.b.ack = 1;
  23751. + }
  23752. + break;
  23753. + case DWC_OTG_EP_TYPE_INTR:
  23754. + hc_intr_mask.b.xfercompl = 1;
  23755. + hc_intr_mask.b.nak = 1;
  23756. + hc_intr_mask.b.stall = 1;
  23757. + hc_intr_mask.b.xacterr = 1;
  23758. + hc_intr_mask.b.datatglerr = 1;
  23759. + hc_intr_mask.b.frmovrun = 1;
  23760. +
  23761. + if (hc->ep_is_in) {
  23762. + hc_intr_mask.b.bblerr = 1;
  23763. + }
  23764. + if (hc->error_state) {
  23765. + hc_intr_mask.b.ack = 1;
  23766. + }
  23767. + if (hc->do_split) {
  23768. + if (hc->complete_split) {
  23769. + hc_intr_mask.b.nyet = 1;
  23770. + } else {
  23771. + hc_intr_mask.b.ack = 1;
  23772. + }
  23773. + }
  23774. + break;
  23775. + case DWC_OTG_EP_TYPE_ISOC:
  23776. + hc_intr_mask.b.xfercompl = 1;
  23777. + hc_intr_mask.b.frmovrun = 1;
  23778. + hc_intr_mask.b.ack = 1;
  23779. +
  23780. + if (hc->ep_is_in) {
  23781. + hc_intr_mask.b.xacterr = 1;
  23782. + hc_intr_mask.b.bblerr = 1;
  23783. + }
  23784. + break;
  23785. + }
  23786. + }
  23787. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  23788. +
  23789. + /*
  23790. + * Program the HCCHARn register with the endpoint characteristics for
  23791. + * the current transfer.
  23792. + */
  23793. + hcchar.d32 = 0;
  23794. + hcchar.b.devaddr = hc->dev_addr;
  23795. + hcchar.b.epnum = hc->ep_num;
  23796. + hcchar.b.epdir = hc->ep_is_in;
  23797. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  23798. + hcchar.b.eptype = hc->ep_type;
  23799. + hcchar.b.mps = hc->max_packet;
  23800. +
  23801. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  23802. +
  23803. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  23804. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  23805. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  23806. + "Max Pkt %d, Multi Cnt %d\n",
  23807. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  23808. + hcchar.b.mps, hcchar.b.multicnt);
  23809. +
  23810. + /*
  23811. + * Program the HCSPLIT register for SPLITs
  23812. + */
  23813. + hcsplt.d32 = 0;
  23814. + if (hc->do_split) {
  23815. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  23816. + hc->hc_num,
  23817. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  23818. + hcsplt.b.compsplt = hc->complete_split;
  23819. + hcsplt.b.xactpos = hc->xact_pos;
  23820. + hcsplt.b.hubaddr = hc->hub_addr;
  23821. + hcsplt.b.prtaddr = hc->port_addr;
  23822. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  23823. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  23824. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  23825. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  23826. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  23827. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  23828. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  23829. + }
  23830. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  23831. +
  23832. +}
  23833. +
  23834. +/**
  23835. + * Attempts to halt a host channel. This function should only be called in
  23836. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  23837. + * normal circumstances in DMA mode, the controller halts the channel when the
  23838. + * transfer is complete or a condition occurs that requires application
  23839. + * intervention.
  23840. + *
  23841. + * In slave mode, checks for a free request queue entry, then sets the Channel
  23842. + * Enable and Channel Disable bits of the Host Channel Characteristics
  23843. + * register of the specified channel to intiate the halt. If there is no free
  23844. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  23845. + * register to flush requests for this channel. In the latter case, sets a
  23846. + * flag to indicate that the host channel needs to be halted when a request
  23847. + * queue slot is open.
  23848. + *
  23849. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  23850. + * HCCHARn register. The controller ensures there is space in the request
  23851. + * queue before submitting the halt request.
  23852. + *
  23853. + * Some time may elapse before the core flushes any posted requests for this
  23854. + * host channel and halts. The Channel Halted interrupt handler completes the
  23855. + * deactivation of the host channel.
  23856. + *
  23857. + * @param core_if Controller register interface.
  23858. + * @param hc Host channel to halt.
  23859. + * @param halt_status Reason for halting the channel.
  23860. + */
  23861. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  23862. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  23863. +{
  23864. + gnptxsts_data_t nptxsts;
  23865. + hptxsts_data_t hptxsts;
  23866. + hcchar_data_t hcchar;
  23867. + dwc_otg_hc_regs_t *hc_regs;
  23868. + dwc_otg_core_global_regs_t *global_regs;
  23869. + dwc_otg_host_global_regs_t *host_global_regs;
  23870. +
  23871. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23872. + global_regs = core_if->core_global_regs;
  23873. + host_global_regs = core_if->host_if->host_global_regs;
  23874. +
  23875. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  23876. + "halt_status = %d\n", halt_status);
  23877. +
  23878. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  23879. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  23880. + /*
  23881. + * Disable all channel interrupts except Ch Halted. The QTD
  23882. + * and QH state associated with this transfer has been cleared
  23883. + * (in the case of URB_DEQUEUE), so the channel needs to be
  23884. + * shut down carefully to prevent crashes.
  23885. + */
  23886. + hcintmsk_data_t hcintmsk;
  23887. + hcintmsk.d32 = 0;
  23888. + hcintmsk.b.chhltd = 1;
  23889. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  23890. +
  23891. + /*
  23892. + * Make sure no other interrupts besides halt are currently
  23893. + * pending. Handling another interrupt could cause a crash due
  23894. + * to the QTD and QH state.
  23895. + */
  23896. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  23897. +
  23898. + /*
  23899. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  23900. + * even if the channel was already halted for some other
  23901. + * reason.
  23902. + */
  23903. + hc->halt_status = halt_status;
  23904. +
  23905. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23906. + if (hcchar.b.chen == 0) {
  23907. + /*
  23908. + * The channel is either already halted or it hasn't
  23909. + * started yet. In DMA mode, the transfer may halt if
  23910. + * it finishes normally or a condition occurs that
  23911. + * requires driver intervention. Don't want to halt
  23912. + * the channel again. In either Slave or DMA mode,
  23913. + * it's possible that the transfer has been assigned
  23914. + * to a channel, but not started yet when an URB is
  23915. + * dequeued. Don't want to halt a channel that hasn't
  23916. + * started yet.
  23917. + */
  23918. + return;
  23919. + }
  23920. + }
  23921. + if (hc->halt_pending) {
  23922. + /*
  23923. + * A halt has already been issued for this channel. This might
  23924. + * happen when a transfer is aborted by a higher level in
  23925. + * the stack.
  23926. + */
  23927. +#ifdef DEBUG
  23928. + DWC_PRINTF
  23929. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  23930. + __func__, hc->hc_num);
  23931. +
  23932. +#endif
  23933. + return;
  23934. + }
  23935. +
  23936. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23937. +
  23938. + /* No need to set the bit in DDMA for disabling the channel */
  23939. + //TODO check it everywhere channel is disabled
  23940. + if (!core_if->core_params->dma_desc_enable)
  23941. + hcchar.b.chen = 1;
  23942. + hcchar.b.chdis = 1;
  23943. +
  23944. + if (!core_if->dma_enable) {
  23945. + /* Check for space in the request queue to issue the halt. */
  23946. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  23947. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  23948. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  23949. + if (nptxsts.b.nptxqspcavail == 0) {
  23950. + hcchar.b.chen = 0;
  23951. + }
  23952. + } else {
  23953. + hptxsts.d32 =
  23954. + DWC_READ_REG32(&host_global_regs->hptxsts);
  23955. + if ((hptxsts.b.ptxqspcavail == 0)
  23956. + || (core_if->queuing_high_bandwidth)) {
  23957. + hcchar.b.chen = 0;
  23958. + }
  23959. + }
  23960. + }
  23961. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23962. +
  23963. + hc->halt_status = halt_status;
  23964. +
  23965. + if (hcchar.b.chen) {
  23966. + hc->halt_pending = 1;
  23967. + hc->halt_on_queue = 0;
  23968. + } else {
  23969. + hc->halt_on_queue = 1;
  23970. + }
  23971. +
  23972. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  23973. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  23974. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  23975. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  23976. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  23977. +
  23978. + return;
  23979. +}
  23980. +
  23981. +/**
  23982. + * Clears the transfer state for a host channel. This function is normally
  23983. + * called after a transfer is done and the host channel is being released.
  23984. + *
  23985. + * @param core_if Programming view of DWC_otg controller.
  23986. + * @param hc Identifies the host channel to clean up.
  23987. + */
  23988. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23989. +{
  23990. + dwc_otg_hc_regs_t *hc_regs;
  23991. +
  23992. + hc->xfer_started = 0;
  23993. +
  23994. + /*
  23995. + * Clear channel interrupt enables and any unhandled channel interrupt
  23996. + * conditions.
  23997. + */
  23998. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23999. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  24000. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  24001. +#ifdef DEBUG
  24002. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  24003. +#endif
  24004. +}
  24005. +
  24006. +/**
  24007. + * Sets the channel property that indicates in which frame a periodic transfer
  24008. + * should occur. This is always set to the _next_ frame. This function has no
  24009. + * effect on non-periodic transfers.
  24010. + *
  24011. + * @param core_if Programming view of DWC_otg controller.
  24012. + * @param hc Identifies the host channel to set up and its properties.
  24013. + * @param hcchar Current value of the HCCHAR register for the specified host
  24014. + * channel.
  24015. + */
  24016. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  24017. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  24018. +{
  24019. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24020. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24021. + hfnum_data_t hfnum;
  24022. + hfnum.d32 =
  24023. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  24024. +
  24025. + /* 1 if _next_ frame is odd, 0 if it's even */
  24026. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  24027. +#ifdef DEBUG
  24028. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  24029. + && !hc->complete_split) {
  24030. + switch (hfnum.b.frnum & 0x7) {
  24031. + case 7:
  24032. + core_if->hfnum_7_samples++;
  24033. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  24034. + break;
  24035. + case 0:
  24036. + core_if->hfnum_0_samples++;
  24037. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  24038. + break;
  24039. + default:
  24040. + core_if->hfnum_other_samples++;
  24041. + core_if->hfnum_other_frrem_accum +=
  24042. + hfnum.b.frrem;
  24043. + break;
  24044. + }
  24045. + }
  24046. +#endif
  24047. + }
  24048. +}
  24049. +
  24050. +#ifdef DEBUG
  24051. +void hc_xfer_timeout(void *ptr)
  24052. +{
  24053. + hc_xfer_info_t *xfer_info = NULL;
  24054. + int hc_num = 0;
  24055. +
  24056. + if (ptr)
  24057. + xfer_info = (hc_xfer_info_t *) ptr;
  24058. +
  24059. + if (!xfer_info->hc) {
  24060. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  24061. + return;
  24062. + }
  24063. +
  24064. + hc_num = xfer_info->hc->hc_num;
  24065. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  24066. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  24067. + xfer_info->core_if->start_hcchar_val[hc_num]);
  24068. +}
  24069. +#endif
  24070. +
  24071. +void ep_xfer_timeout(void *ptr)
  24072. +{
  24073. + ep_xfer_info_t *xfer_info = NULL;
  24074. + int ep_num = 0;
  24075. + dctl_data_t dctl = {.d32 = 0 };
  24076. + gintsts_data_t gintsts = {.d32 = 0 };
  24077. + gintmsk_data_t gintmsk = {.d32 = 0 };
  24078. +
  24079. + if (ptr)
  24080. + xfer_info = (ep_xfer_info_t *) ptr;
  24081. +
  24082. + if (!xfer_info->ep) {
  24083. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  24084. + return;
  24085. + }
  24086. +
  24087. + ep_num = xfer_info->ep->num;
  24088. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  24089. + /* Put the sate to 2 as it was time outed */
  24090. + xfer_info->state = 2;
  24091. +
  24092. + dctl.d32 =
  24093. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  24094. + gintsts.d32 =
  24095. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  24096. + gintmsk.d32 =
  24097. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  24098. +
  24099. + if (!gintmsk.b.goutnakeff) {
  24100. + /* Unmask it */
  24101. + gintmsk.b.goutnakeff = 1;
  24102. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  24103. + gintmsk.d32);
  24104. +
  24105. + }
  24106. +
  24107. + if (!gintsts.b.goutnakeff) {
  24108. + dctl.b.sgoutnak = 1;
  24109. + }
  24110. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  24111. + dctl.d32);
  24112. +
  24113. +}
  24114. +
  24115. +void set_pid_isoc(dwc_hc_t * hc)
  24116. +{
  24117. + /* Set up the initial PID for the transfer. */
  24118. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  24119. + if (hc->ep_is_in) {
  24120. + if (hc->multi_count == 1) {
  24121. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24122. + } else if (hc->multi_count == 2) {
  24123. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  24124. + } else {
  24125. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  24126. + }
  24127. + } else {
  24128. + if (hc->multi_count == 1) {
  24129. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24130. + } else {
  24131. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  24132. + }
  24133. + }
  24134. + } else {
  24135. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24136. + }
  24137. +}
  24138. +
  24139. +/**
  24140. + * This function does the setup for a data transfer for a host channel and
  24141. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  24142. + * Slave mode, the caller must ensure that there is sufficient space in the
  24143. + * request queue and Tx Data FIFO.
  24144. + *
  24145. + * For an OUT transfer in Slave mode, it loads a data packet into the
  24146. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  24147. + * the Host ISR.
  24148. + *
  24149. + * For an IN transfer in Slave mode, a data packet is requested. The data
  24150. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  24151. + * additional data packets are requested in the Host ISR.
  24152. + *
  24153. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  24154. + * register along with a packet count of 1 and the channel is enabled. This
  24155. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  24156. + * simply set to 0 since no data transfer occurs in this case.
  24157. + *
  24158. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  24159. + * all the information required to perform the subsequent data transfer. In
  24160. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  24161. + * controller performs the entire PING protocol, then starts the data
  24162. + * transfer.
  24163. + *
  24164. + * @param core_if Programming view of DWC_otg controller.
  24165. + * @param hc Information needed to initialize the host channel. The xfer_len
  24166. + * value may be reduced to accommodate the max widths of the XferSize and
  24167. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  24168. + * to reflect the final xfer_len value.
  24169. + */
  24170. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24171. +{
  24172. + hcchar_data_t hcchar;
  24173. + hctsiz_data_t hctsiz;
  24174. + uint16_t num_packets;
  24175. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  24176. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  24177. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24178. +
  24179. + hctsiz.d32 = 0;
  24180. +
  24181. + if (hc->do_ping) {
  24182. + if (!core_if->dma_enable) {
  24183. + dwc_otg_hc_do_ping(core_if, hc);
  24184. + hc->xfer_started = 1;
  24185. + return;
  24186. + } else {
  24187. + hctsiz.b.dopng = 1;
  24188. + }
  24189. + }
  24190. +
  24191. + if (hc->do_split) {
  24192. + num_packets = 1;
  24193. +
  24194. + if (hc->complete_split && !hc->ep_is_in) {
  24195. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  24196. + * core doesn't expect any data written to the FIFO */
  24197. + hc->xfer_len = 0;
  24198. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  24199. + hc->xfer_len = hc->max_packet;
  24200. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  24201. + hc->xfer_len = 188;
  24202. + }
  24203. +
  24204. + hctsiz.b.xfersize = hc->xfer_len;
  24205. + } else {
  24206. + /*
  24207. + * Ensure that the transfer length and packet count will fit
  24208. + * in the widths allocated for them in the HCTSIZn register.
  24209. + */
  24210. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24211. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24212. + /*
  24213. + * Make sure the transfer size is no larger than one
  24214. + * (micro)frame's worth of data. (A check was done
  24215. + * when the periodic transfer was accepted to ensure
  24216. + * that a (micro)frame's worth of data can be
  24217. + * programmed into a channel.)
  24218. + */
  24219. + uint32_t max_periodic_len =
  24220. + hc->multi_count * hc->max_packet;
  24221. + if (hc->xfer_len > max_periodic_len) {
  24222. + hc->xfer_len = max_periodic_len;
  24223. + } else {
  24224. + }
  24225. + } else if (hc->xfer_len > max_hc_xfer_size) {
  24226. + /* Make sure that xfer_len is a multiple of max packet size. */
  24227. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  24228. + }
  24229. +
  24230. + if (hc->xfer_len > 0) {
  24231. + num_packets =
  24232. + (hc->xfer_len + hc->max_packet -
  24233. + 1) / hc->max_packet;
  24234. + if (num_packets > max_hc_pkt_count) {
  24235. + num_packets = max_hc_pkt_count;
  24236. + hc->xfer_len = num_packets * hc->max_packet;
  24237. + }
  24238. + } else {
  24239. + /* Need 1 packet for transfer length of 0. */
  24240. + num_packets = 1;
  24241. + }
  24242. +
  24243. + if (hc->ep_is_in) {
  24244. + /* Always program an integral # of max packets for IN transfers. */
  24245. + hc->xfer_len = num_packets * hc->max_packet;
  24246. + }
  24247. +
  24248. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24249. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24250. + /*
  24251. + * Make sure that the multi_count field matches the
  24252. + * actual transfer length.
  24253. + */
  24254. + hc->multi_count = num_packets;
  24255. + }
  24256. +
  24257. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  24258. + set_pid_isoc(hc);
  24259. +
  24260. + hctsiz.b.xfersize = hc->xfer_len;
  24261. + }
  24262. +
  24263. + hc->start_pkt_count = num_packets;
  24264. + hctsiz.b.pktcnt = num_packets;
  24265. + hctsiz.b.pid = hc->data_pid_start;
  24266. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24267. +
  24268. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24269. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  24270. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  24271. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  24272. +
  24273. + if (core_if->dma_enable) {
  24274. + dwc_dma_t dma_addr;
  24275. + if (hc->align_buff) {
  24276. + dma_addr = hc->align_buff;
  24277. + } else {
  24278. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  24279. + }
  24280. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  24281. + }
  24282. +
  24283. + /* Start the split */
  24284. + if (hc->do_split) {
  24285. + hcsplt_data_t hcsplt;
  24286. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  24287. + hcsplt.b.spltena = 1;
  24288. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  24289. + }
  24290. +
  24291. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24292. + hcchar.b.multicnt = hc->multi_count;
  24293. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24294. +#ifdef DEBUG
  24295. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  24296. + if (hcchar.b.chdis) {
  24297. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  24298. + __func__, hc->hc_num, hcchar.d32);
  24299. + }
  24300. +#endif
  24301. +
  24302. + /* Set host channel enable after all other setup is complete. */
  24303. + hcchar.b.chen = 1;
  24304. + hcchar.b.chdis = 0;
  24305. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24306. +
  24307. + hc->xfer_started = 1;
  24308. + hc->requests++;
  24309. +
  24310. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  24311. + /* Load OUT packet into the appropriate Tx FIFO. */
  24312. + dwc_otg_hc_write_packet(core_if, hc);
  24313. + }
  24314. +#ifdef DEBUG
  24315. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  24316. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  24317. + hc->hc_num, core_if);//GRAYG
  24318. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  24319. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  24320. +
  24321. + /* Start a timer for this transfer. */
  24322. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  24323. + }
  24324. +#endif
  24325. +}
  24326. +
  24327. +/**
  24328. + * This function does the setup for a data transfer for a host channel
  24329. + * and starts the transfer in Descriptor DMA mode.
  24330. + *
  24331. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  24332. + * Sets PID and NTD values. For periodic transfers
  24333. + * initializes SCHED_INFO field with micro-frame bitmap.
  24334. + *
  24335. + * Initializes HCDMA register with descriptor list address and CTD value
  24336. + * then starts the transfer via enabling the channel.
  24337. + *
  24338. + * @param core_if Programming view of DWC_otg controller.
  24339. + * @param hc Information needed to initialize the host channel.
  24340. + */
  24341. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24342. +{
  24343. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24344. + hcchar_data_t hcchar;
  24345. + hctsiz_data_t hctsiz;
  24346. + hcdma_data_t hcdma;
  24347. +
  24348. + hctsiz.d32 = 0;
  24349. +
  24350. + if (hc->do_ping)
  24351. + hctsiz.b_ddma.dopng = 1;
  24352. +
  24353. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  24354. + set_pid_isoc(hc);
  24355. +
  24356. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  24357. + hctsiz.b_ddma.pid = hc->data_pid_start;
  24358. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  24359. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  24360. +
  24361. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24362. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  24363. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  24364. +
  24365. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24366. +
  24367. + hcdma.d32 = 0;
  24368. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  24369. +
  24370. + /* Always start from first descriptor. */
  24371. + hcdma.b.ctd = 0;
  24372. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  24373. +
  24374. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24375. + hcchar.b.multicnt = hc->multi_count;
  24376. +
  24377. +#ifdef DEBUG
  24378. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  24379. + if (hcchar.b.chdis) {
  24380. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  24381. + __func__, hc->hc_num, hcchar.d32);
  24382. + }
  24383. +#endif
  24384. +
  24385. + /* Set host channel enable after all other setup is complete. */
  24386. + hcchar.b.chen = 1;
  24387. + hcchar.b.chdis = 0;
  24388. +
  24389. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24390. +
  24391. + hc->xfer_started = 1;
  24392. + hc->requests++;
  24393. +
  24394. +#ifdef DEBUG
  24395. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  24396. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  24397. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  24398. + hc->hc_num, core_if);//GRAYG
  24399. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  24400. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  24401. + /* Start a timer for this transfer. */
  24402. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  24403. + }
  24404. +#endif
  24405. +
  24406. +}
  24407. +
  24408. +/**
  24409. + * This function continues a data transfer that was started by previous call
  24410. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  24411. + * sufficient space in the request queue and Tx Data FIFO. This function
  24412. + * should only be called in Slave mode. In DMA mode, the controller acts
  24413. + * autonomously to complete transfers programmed to a host channel.
  24414. + *
  24415. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  24416. + * if there is any data remaining to be queued. For an IN transfer, another
  24417. + * data packet is always requested. For the SETUP phase of a control transfer,
  24418. + * this function does nothing.
  24419. + *
  24420. + * @return 1 if a new request is queued, 0 if no more requests are required
  24421. + * for this transfer.
  24422. + */
  24423. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24424. +{
  24425. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24426. +
  24427. + if (hc->do_split) {
  24428. + /* SPLITs always queue just once per channel */
  24429. + return 0;
  24430. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  24431. + /* SETUPs are queued only once since they can't be NAKed. */
  24432. + return 0;
  24433. + } else if (hc->ep_is_in) {
  24434. + /*
  24435. + * Always queue another request for other IN transfers. If
  24436. + * back-to-back INs are issued and NAKs are received for both,
  24437. + * the driver may still be processing the first NAK when the
  24438. + * second NAK is received. When the interrupt handler clears
  24439. + * the NAK interrupt for the first NAK, the second NAK will
  24440. + * not be seen. So we can't depend on the NAK interrupt
  24441. + * handler to requeue a NAKed request. Instead, IN requests
  24442. + * are issued each time this function is called. When the
  24443. + * transfer completes, the extra requests for the channel will
  24444. + * be flushed.
  24445. + */
  24446. + hcchar_data_t hcchar;
  24447. + dwc_otg_hc_regs_t *hc_regs =
  24448. + core_if->host_if->hc_regs[hc->hc_num];
  24449. +
  24450. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24451. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24452. + hcchar.b.chen = 1;
  24453. + hcchar.b.chdis = 0;
  24454. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  24455. + hcchar.d32);
  24456. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24457. + hc->requests++;
  24458. + return 1;
  24459. + } else {
  24460. + /* OUT transfers. */
  24461. + if (hc->xfer_count < hc->xfer_len) {
  24462. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24463. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24464. + hcchar_data_t hcchar;
  24465. + dwc_otg_hc_regs_t *hc_regs;
  24466. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24467. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24468. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24469. + }
  24470. +
  24471. + /* Load OUT packet into the appropriate Tx FIFO. */
  24472. + dwc_otg_hc_write_packet(core_if, hc);
  24473. + hc->requests++;
  24474. + return 1;
  24475. + } else {
  24476. + return 0;
  24477. + }
  24478. + }
  24479. +}
  24480. +
  24481. +/**
  24482. + * Starts a PING transfer. This function should only be called in Slave mode.
  24483. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  24484. + */
  24485. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24486. +{
  24487. + hcchar_data_t hcchar;
  24488. + hctsiz_data_t hctsiz;
  24489. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24490. +
  24491. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24492. +
  24493. + hctsiz.d32 = 0;
  24494. + hctsiz.b.dopng = 1;
  24495. + hctsiz.b.pktcnt = 1;
  24496. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24497. +
  24498. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24499. + hcchar.b.chen = 1;
  24500. + hcchar.b.chdis = 0;
  24501. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24502. +}
  24503. +
  24504. +/*
  24505. + * This function writes a packet into the Tx FIFO associated with the Host
  24506. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  24507. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  24508. + * periodic Tx FIFO is written. This function should only be called in Slave
  24509. + * mode.
  24510. + *
  24511. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  24512. + * then number of bytes written to the Tx FIFO.
  24513. + */
  24514. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24515. +{
  24516. + uint32_t i;
  24517. + uint32_t remaining_count;
  24518. + uint32_t byte_count;
  24519. + uint32_t dword_count;
  24520. +
  24521. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  24522. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  24523. +
  24524. + remaining_count = hc->xfer_len - hc->xfer_count;
  24525. + if (remaining_count > hc->max_packet) {
  24526. + byte_count = hc->max_packet;
  24527. + } else {
  24528. + byte_count = remaining_count;
  24529. + }
  24530. +
  24531. + dword_count = (byte_count + 3) / 4;
  24532. +
  24533. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  24534. + /* xfer_buff is DWORD aligned. */
  24535. + for (i = 0; i < dword_count; i++, data_buff++) {
  24536. + DWC_WRITE_REG32(data_fifo, *data_buff);
  24537. + }
  24538. + } else {
  24539. + /* xfer_buff is not DWORD aligned. */
  24540. + for (i = 0; i < dword_count; i++, data_buff++) {
  24541. + uint32_t data;
  24542. + data =
  24543. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  24544. + 16 | data_buff[3] << 24);
  24545. + DWC_WRITE_REG32(data_fifo, data);
  24546. + }
  24547. + }
  24548. +
  24549. + hc->xfer_count += byte_count;
  24550. + hc->xfer_buff += byte_count;
  24551. +}
  24552. +
  24553. +/**
  24554. + * Gets the current USB frame number. This is the frame number from the last
  24555. + * SOF packet.
  24556. + */
  24557. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  24558. +{
  24559. + dsts_data_t dsts;
  24560. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  24561. +
  24562. + /* read current frame/microframe number from DSTS register */
  24563. + return dsts.b.soffn;
  24564. +}
  24565. +
  24566. +/**
  24567. + * Calculates and gets the frame Interval value of HFIR register according PHY
  24568. + * type and speed.The application can modify a value of HFIR register only after
  24569. + * the Port Enable bit of the Host Port Control and Status register
  24570. + * (HPRT.PrtEnaPort) has been set.
  24571. +*/
  24572. +
  24573. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  24574. +{
  24575. + gusbcfg_data_t usbcfg;
  24576. + hwcfg2_data_t hwcfg2;
  24577. + hprt0_data_t hprt0;
  24578. + int clock = 60; // default value
  24579. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  24580. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  24581. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  24582. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  24583. + clock = 60;
  24584. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  24585. + clock = 48;
  24586. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24587. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  24588. + clock = 30;
  24589. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24590. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  24591. + clock = 60;
  24592. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24593. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  24594. + clock = 48;
  24595. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  24596. + clock = 48;
  24597. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  24598. + clock = 48;
  24599. + if (hprt0.b.prtspd == 0)
  24600. + /* High speed case */
  24601. + return 125 * clock - 1;
  24602. + else
  24603. + /* FS/LS case */
  24604. + return 1000 * clock - 1;
  24605. +}
  24606. +
  24607. +/**
  24608. + * This function reads a setup packet from the Rx FIFO into the destination
  24609. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  24610. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  24611. + *
  24612. + * @param core_if Programming view of DWC_otg controller.
  24613. + * @param dest Destination buffer for packet data.
  24614. + */
  24615. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  24616. +{
  24617. + device_grxsts_data_t status;
  24618. + /* Get the 8 bytes of a setup transaction data */
  24619. +
  24620. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  24621. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  24622. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  24623. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  24624. + status.d32 =
  24625. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  24626. + DWC_DEBUGPL(DBG_ANY,
  24627. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  24628. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  24629. + status.b.fn, status.b.fn);
  24630. + }
  24631. +}
  24632. +
  24633. +/**
  24634. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  24635. + * IN for transmitting packets. It is normally called when the
  24636. + * "Enumeration Done" interrupt occurs.
  24637. + *
  24638. + * @param core_if Programming view of DWC_otg controller.
  24639. + * @param ep The EP0 data.
  24640. + */
  24641. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24642. +{
  24643. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24644. + dsts_data_t dsts;
  24645. + depctl_data_t diepctl;
  24646. + depctl_data_t doepctl;
  24647. + dctl_data_t dctl = {.d32 = 0 };
  24648. +
  24649. + ep->stp_rollover = 0;
  24650. + /* Read the Device Status and Endpoint 0 Control registers */
  24651. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  24652. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  24653. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  24654. +
  24655. + /* Set the MPS of the IN EP based on the enumeration speed */
  24656. + switch (dsts.b.enumspd) {
  24657. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  24658. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  24659. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  24660. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  24661. + break;
  24662. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  24663. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  24664. + break;
  24665. + }
  24666. +
  24667. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  24668. +
  24669. + /* Enable OUT EP for receive */
  24670. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  24671. + doepctl.b.epena = 1;
  24672. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  24673. + }
  24674. +#ifdef VERBOSE
  24675. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  24676. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  24677. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  24678. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  24679. +#endif
  24680. + dctl.b.cgnpinnak = 1;
  24681. +
  24682. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  24683. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  24684. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  24685. +
  24686. +}
  24687. +
  24688. +/**
  24689. + * This function activates an EP. The Device EP control register for
  24690. + * the EP is configured as defined in the ep structure. Note: This
  24691. + * function is not used for EP0.
  24692. + *
  24693. + * @param core_if Programming view of DWC_otg controller.
  24694. + * @param ep The EP to activate.
  24695. + */
  24696. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24697. +{
  24698. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24699. + depctl_data_t depctl;
  24700. + volatile uint32_t *addr;
  24701. + daint_data_t daintmsk = {.d32 = 0 };
  24702. + dcfg_data_t dcfg;
  24703. + uint8_t i;
  24704. +
  24705. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  24706. + (ep->is_in ? "IN" : "OUT"));
  24707. +
  24708. +#ifdef DWC_UTE_PER_IO
  24709. + ep->xiso_frame_num = 0xFFFFFFFF;
  24710. + ep->xiso_active_xfers = 0;
  24711. + ep->xiso_queued_xfers = 0;
  24712. +#endif
  24713. + /* Read DEPCTLn register */
  24714. + if (ep->is_in == 1) {
  24715. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  24716. + daintmsk.ep.in = 1 << ep->num;
  24717. + } else {
  24718. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  24719. + daintmsk.ep.out = 1 << ep->num;
  24720. + }
  24721. +
  24722. + /* If the EP is already active don't change the EP Control
  24723. + * register. */
  24724. + depctl.d32 = DWC_READ_REG32(addr);
  24725. + if (!depctl.b.usbactep) {
  24726. + depctl.b.mps = ep->maxpacket;
  24727. + depctl.b.eptype = ep->type;
  24728. + depctl.b.txfnum = ep->tx_fifo_num;
  24729. +
  24730. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24731. + depctl.b.setd0pid = 1; // ???
  24732. + } else {
  24733. + depctl.b.setd0pid = 1;
  24734. + }
  24735. + depctl.b.usbactep = 1;
  24736. +
  24737. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  24738. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  24739. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  24740. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  24741. + break;
  24742. + }
  24743. + core_if->nextep_seq[i] = ep->num;
  24744. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  24745. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  24746. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  24747. + dcfg.b.epmscnt++;
  24748. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  24749. +
  24750. + DWC_DEBUGPL(DBG_PCDV,
  24751. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  24752. + __func__, core_if->first_in_nextep_seq);
  24753. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  24754. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  24755. + core_if->nextep_seq[i]);
  24756. + }
  24757. +
  24758. + }
  24759. +
  24760. +
  24761. + DWC_WRITE_REG32(addr, depctl.d32);
  24762. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  24763. + }
  24764. +
  24765. + /* Enable the Interrupt for this EP */
  24766. + if (core_if->multiproc_int_enable) {
  24767. + if (ep->is_in == 1) {
  24768. + diepmsk_data_t diepmsk = {.d32 = 0 };
  24769. + diepmsk.b.xfercompl = 1;
  24770. + diepmsk.b.timeout = 1;
  24771. + diepmsk.b.epdisabled = 1;
  24772. + diepmsk.b.ahberr = 1;
  24773. + diepmsk.b.intknepmis = 1;
  24774. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  24775. + diepmsk.b.intknepmis = 0;
  24776. + diepmsk.b.txfifoundrn = 1; //?????
  24777. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24778. + diepmsk.b.nak = 1;
  24779. + }
  24780. +
  24781. +
  24782. +
  24783. +/*
  24784. + if (core_if->dma_desc_enable) {
  24785. + diepmsk.b.bna = 1;
  24786. + }
  24787. +*/
  24788. +/*
  24789. + if (core_if->dma_enable) {
  24790. + doepmsk.b.nak = 1;
  24791. + }
  24792. +*/
  24793. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  24794. + diepeachintmsk[ep->num], diepmsk.d32);
  24795. +
  24796. + } else {
  24797. + doepmsk_data_t doepmsk = {.d32 = 0 };
  24798. + doepmsk.b.xfercompl = 1;
  24799. + doepmsk.b.ahberr = 1;
  24800. + doepmsk.b.epdisabled = 1;
  24801. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  24802. + doepmsk.b.outtknepdis = 1;
  24803. +
  24804. +/*
  24805. +
  24806. + if (core_if->dma_desc_enable) {
  24807. + doepmsk.b.bna = 1;
  24808. + }
  24809. +*/
  24810. +/*
  24811. + doepmsk.b.babble = 1;
  24812. + doepmsk.b.nyet = 1;
  24813. + doepmsk.b.nak = 1;
  24814. +*/
  24815. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  24816. + doepeachintmsk[ep->num], doepmsk.d32);
  24817. + }
  24818. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  24819. + 0, daintmsk.d32);
  24820. + } else {
  24821. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24822. + if (ep->is_in) {
  24823. + diepmsk_data_t diepmsk = {.d32 = 0 };
  24824. + diepmsk.b.nak = 1;
  24825. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  24826. + } else {
  24827. + doepmsk_data_t doepmsk = {.d32 = 0 };
  24828. + doepmsk.b.outtknepdis = 1;
  24829. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  24830. + }
  24831. + }
  24832. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  24833. + 0, daintmsk.d32);
  24834. + }
  24835. +
  24836. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  24837. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  24838. +
  24839. + ep->stall_clear_flag = 0;
  24840. +
  24841. + return;
  24842. +}
  24843. +
  24844. +/**
  24845. + * This function deactivates an EP. This is done by clearing the USB Active
  24846. + * EP bit in the Device EP control register. Note: This function is not used
  24847. + * for EP0. EP0 cannot be deactivated.
  24848. + *
  24849. + * @param core_if Programming view of DWC_otg controller.
  24850. + * @param ep The EP to deactivate.
  24851. + */
  24852. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24853. +{
  24854. + depctl_data_t depctl = {.d32 = 0 };
  24855. + volatile uint32_t *addr;
  24856. + daint_data_t daintmsk = {.d32 = 0 };
  24857. + dcfg_data_t dcfg;
  24858. + uint8_t i = 0;
  24859. +
  24860. +#ifdef DWC_UTE_PER_IO
  24861. + ep->xiso_frame_num = 0xFFFFFFFF;
  24862. + ep->xiso_active_xfers = 0;
  24863. + ep->xiso_queued_xfers = 0;
  24864. +#endif
  24865. +
  24866. + /* Read DEPCTLn register */
  24867. + if (ep->is_in == 1) {
  24868. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  24869. + daintmsk.ep.in = 1 << ep->num;
  24870. + } else {
  24871. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  24872. + daintmsk.ep.out = 1 << ep->num;
  24873. + }
  24874. +
  24875. + depctl.d32 = DWC_READ_REG32(addr);
  24876. +
  24877. + depctl.b.usbactep = 0;
  24878. +
  24879. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  24880. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  24881. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  24882. + if (core_if->nextep_seq[i] == ep->num)
  24883. + break;
  24884. + }
  24885. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  24886. + if (core_if->first_in_nextep_seq == ep->num)
  24887. + core_if->first_in_nextep_seq = i;
  24888. + core_if->nextep_seq[ep->num] = 0xff;
  24889. + depctl.b.nextep = 0;
  24890. + dcfg.d32 =
  24891. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  24892. + dcfg.b.epmscnt--;
  24893. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  24894. + dcfg.d32);
  24895. +
  24896. + DWC_DEBUGPL(DBG_PCDV,
  24897. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  24898. + __func__, core_if->first_in_nextep_seq);
  24899. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  24900. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  24901. + }
  24902. + }
  24903. +
  24904. + if (ep->is_in == 1)
  24905. + depctl.b.txfnum = 0;
  24906. +
  24907. + if (core_if->dma_desc_enable)
  24908. + depctl.b.epdis = 1;
  24909. +
  24910. + DWC_WRITE_REG32(addr, depctl.d32);
  24911. + depctl.d32 = DWC_READ_REG32(addr);
  24912. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  24913. + && depctl.b.epena) {
  24914. + depctl_data_t depctl = {.d32 = 0};
  24915. + if (ep->is_in) {
  24916. + diepint_data_t diepint = {.d32 = 0};
  24917. +
  24918. + depctl.b.snak = 1;
  24919. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24920. + diepctl, depctl.d32);
  24921. + do {
  24922. + dwc_udelay(10);
  24923. + diepint.d32 =
  24924. + DWC_READ_REG32(&core_if->
  24925. + dev_if->in_ep_regs[ep->num]->
  24926. + diepint);
  24927. + } while (!diepint.b.inepnakeff);
  24928. + diepint.b.inepnakeff = 1;
  24929. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24930. + diepint, diepint.d32);
  24931. + depctl.d32 = 0;
  24932. + depctl.b.epdis = 1;
  24933. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24934. + diepctl, depctl.d32);
  24935. + do {
  24936. + dwc_udelay(10);
  24937. + diepint.d32 =
  24938. + DWC_READ_REG32(&core_if->
  24939. + dev_if->in_ep_regs[ep->num]->
  24940. + diepint);
  24941. + } while (!diepint.b.epdisabled);
  24942. + diepint.b.epdisabled = 1;
  24943. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24944. + diepint, diepint.d32);
  24945. + } else {
  24946. + dctl_data_t dctl = {.d32 = 0};
  24947. + gintmsk_data_t gintsts = {.d32 = 0};
  24948. + doepint_data_t doepint = {.d32 = 0};
  24949. + dctl.b.sgoutnak = 1;
  24950. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  24951. + dctl, 0, dctl.d32);
  24952. + do {
  24953. + dwc_udelay(10);
  24954. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  24955. + } while (!gintsts.b.goutnakeff);
  24956. + gintsts.d32 = 0;
  24957. + gintsts.b.goutnakeff = 1;
  24958. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  24959. +
  24960. + depctl.d32 = 0;
  24961. + depctl.b.epdis = 1;
  24962. + depctl.b.snak = 1;
  24963. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  24964. + do
  24965. + {
  24966. + dwc_udelay(10);
  24967. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  24968. + out_ep_regs[ep->num]->doepint);
  24969. + } while (!doepint.b.epdisabled);
  24970. +
  24971. + doepint.b.epdisabled = 1;
  24972. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  24973. +
  24974. + dctl.d32 = 0;
  24975. + dctl.b.cgoutnak = 1;
  24976. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  24977. + }
  24978. + }
  24979. +
  24980. + /* Disable the Interrupt for this EP */
  24981. + if (core_if->multiproc_int_enable) {
  24982. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  24983. + daintmsk.d32, 0);
  24984. +
  24985. + if (ep->is_in == 1) {
  24986. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  24987. + diepeachintmsk[ep->num], 0);
  24988. + } else {
  24989. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  24990. + doepeachintmsk[ep->num], 0);
  24991. + }
  24992. + } else {
  24993. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  24994. + daintmsk.d32, 0);
  24995. + }
  24996. +
  24997. +}
  24998. +
  24999. +/**
  25000. + * This function initializes dma descriptor chain.
  25001. + *
  25002. + * @param core_if Programming view of DWC_otg controller.
  25003. + * @param ep The EP to start the transfer on.
  25004. + */
  25005. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25006. +{
  25007. + dwc_otg_dev_dma_desc_t *dma_desc;
  25008. + uint32_t offset;
  25009. + uint32_t xfer_est;
  25010. + int i;
  25011. + unsigned maxxfer_local, total_len;
  25012. +
  25013. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  25014. + (ep->maxpacket%4)) {
  25015. + maxxfer_local = ep->maxpacket;
  25016. + total_len = ep->xfer_len;
  25017. + } else {
  25018. + maxxfer_local = ep->maxxfer;
  25019. + total_len = ep->total_len;
  25020. + }
  25021. +
  25022. + ep->desc_cnt = (total_len / maxxfer_local) +
  25023. + ((total_len % maxxfer_local) ? 1 : 0);
  25024. +
  25025. + if (!ep->desc_cnt)
  25026. + ep->desc_cnt = 1;
  25027. +
  25028. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  25029. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  25030. +
  25031. + dma_desc = ep->desc_addr;
  25032. + if (maxxfer_local == ep->maxpacket) {
  25033. + if ((total_len % maxxfer_local) &&
  25034. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  25035. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  25036. + (total_len % maxxfer_local);
  25037. + } else
  25038. + xfer_est = ep->desc_cnt * maxxfer_local;
  25039. + } else
  25040. + xfer_est = total_len;
  25041. + offset = 0;
  25042. + for (i = 0; i < ep->desc_cnt; ++i) {
  25043. + /** DMA Descriptor Setup */
  25044. + if (xfer_est > maxxfer_local) {
  25045. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25046. + dma_desc->status.b.l = 0;
  25047. + dma_desc->status.b.ioc = 0;
  25048. + dma_desc->status.b.sp = 0;
  25049. + dma_desc->status.b.bytes = maxxfer_local;
  25050. + dma_desc->buf = ep->dma_addr + offset;
  25051. + dma_desc->status.b.sts = 0;
  25052. + dma_desc->status.b.bs = BS_HOST_READY;
  25053. +
  25054. + xfer_est -= maxxfer_local;
  25055. + offset += maxxfer_local;
  25056. + } else {
  25057. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25058. + dma_desc->status.b.l = 1;
  25059. + dma_desc->status.b.ioc = 1;
  25060. + if (ep->is_in) {
  25061. + dma_desc->status.b.sp =
  25062. + (xfer_est %
  25063. + ep->maxpacket) ? 1 : ((ep->
  25064. + sent_zlp) ? 1 : 0);
  25065. + dma_desc->status.b.bytes = xfer_est;
  25066. + } else {
  25067. + if (maxxfer_local == ep->maxpacket)
  25068. + dma_desc->status.b.bytes = xfer_est;
  25069. + else
  25070. + dma_desc->status.b.bytes =
  25071. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  25072. + }
  25073. +
  25074. + dma_desc->buf = ep->dma_addr + offset;
  25075. + dma_desc->status.b.sts = 0;
  25076. + dma_desc->status.b.bs = BS_HOST_READY;
  25077. + }
  25078. + dma_desc++;
  25079. + }
  25080. +}
  25081. +/**
  25082. + * This function is called when to write ISOC data into appropriate dedicated
  25083. + * periodic FIFO.
  25084. + */
  25085. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  25086. +{
  25087. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  25088. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  25089. + dtxfsts_data_t txstatus = {.d32 = 0 };
  25090. + uint32_t len = 0;
  25091. + int epnum = dwc_ep->num;
  25092. + int dwords;
  25093. +
  25094. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  25095. +
  25096. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  25097. +
  25098. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  25099. +
  25100. + if (len > dwc_ep->maxpacket) {
  25101. + len = dwc_ep->maxpacket;
  25102. + }
  25103. +
  25104. + dwords = (len + 3) / 4;
  25105. +
  25106. + /* While there is space in the queue and space in the FIFO and
  25107. + * More data to tranfer, Write packets to the Tx FIFO */
  25108. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  25109. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  25110. +
  25111. + while (txstatus.b.txfspcavail > dwords &&
  25112. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  25113. + /* Write the FIFO */
  25114. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  25115. +
  25116. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  25117. + if (len > dwc_ep->maxpacket) {
  25118. + len = dwc_ep->maxpacket;
  25119. + }
  25120. +
  25121. + dwords = (len + 3) / 4;
  25122. + txstatus.d32 =
  25123. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  25124. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  25125. + txstatus.d32);
  25126. + }
  25127. +
  25128. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  25129. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  25130. +
  25131. + return 1;
  25132. +}
  25133. +/**
  25134. + * This function does the setup for a data transfer for an EP and
  25135. + * starts the transfer. For an IN transfer, the packets will be
  25136. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  25137. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  25138. + *
  25139. + * @param core_if Programming view of DWC_otg controller.
  25140. + * @param ep The EP to start the transfer on.
  25141. + */
  25142. +
  25143. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25144. +{
  25145. + depctl_data_t depctl;
  25146. + deptsiz_data_t deptsiz;
  25147. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25148. +
  25149. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  25150. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  25151. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  25152. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  25153. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  25154. + ep->total_len);
  25155. + /* IN endpoint */
  25156. + if (ep->is_in == 1) {
  25157. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25158. + core_if->dev_if->in_ep_regs[ep->num];
  25159. +
  25160. + gnptxsts_data_t gtxstatus;
  25161. +
  25162. + gtxstatus.d32 =
  25163. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25164. +
  25165. + if (core_if->en_multiple_tx_fifo == 0
  25166. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  25167. +#ifdef DEBUG
  25168. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  25169. +#endif
  25170. + return;
  25171. + }
  25172. +
  25173. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  25174. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  25175. +
  25176. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  25177. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  25178. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  25179. + else
  25180. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  25181. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  25182. +
  25183. +
  25184. + /* Zero Length Packet? */
  25185. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  25186. + deptsiz.b.xfersize = 0;
  25187. + deptsiz.b.pktcnt = 1;
  25188. + } else {
  25189. + /* Program the transfer size and packet count
  25190. + * as follows: xfersize = N * maxpacket +
  25191. + * short_packet pktcnt = N + (short_packet
  25192. + * exist ? 1 : 0)
  25193. + */
  25194. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  25195. + deptsiz.b.pktcnt =
  25196. + (ep->xfer_len - ep->xfer_count - 1 +
  25197. + ep->maxpacket) / ep->maxpacket;
  25198. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  25199. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  25200. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  25201. + }
  25202. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  25203. + deptsiz.b.mc = deptsiz.b.pktcnt;
  25204. + }
  25205. +
  25206. + /* Write the DMA register */
  25207. + if (core_if->dma_enable) {
  25208. + if (core_if->dma_desc_enable == 0) {
  25209. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  25210. + deptsiz.b.mc = 1;
  25211. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25212. + deptsiz.d32);
  25213. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25214. + (uint32_t) ep->dma_addr);
  25215. + } else {
  25216. +#ifdef DWC_UTE_CFI
  25217. + /* The descriptor chain should be already initialized by now */
  25218. + if (ep->buff_mode != BM_STANDARD) {
  25219. + DWC_WRITE_REG32(&in_regs->diepdma,
  25220. + ep->descs_dma_addr);
  25221. + } else {
  25222. +#endif
  25223. + init_dma_desc_chain(core_if, ep);
  25224. + /** DIEPDMAn Register write */
  25225. + DWC_WRITE_REG32(&in_regs->diepdma,
  25226. + ep->dma_desc_addr);
  25227. +#ifdef DWC_UTE_CFI
  25228. + }
  25229. +#endif
  25230. + }
  25231. + } else {
  25232. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25233. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  25234. + /**
  25235. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  25236. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  25237. + * the data will be written into the fifo by the ISR.
  25238. + */
  25239. + if (core_if->en_multiple_tx_fifo == 0) {
  25240. + intr_mask.b.nptxfempty = 1;
  25241. + DWC_MODIFY_REG32
  25242. + (&core_if->core_global_regs->gintmsk,
  25243. + intr_mask.d32, intr_mask.d32);
  25244. + } else {
  25245. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25246. + if (ep->xfer_len > 0) {
  25247. + uint32_t fifoemptymsk = 0;
  25248. + fifoemptymsk = 1 << ep->num;
  25249. + DWC_MODIFY_REG32
  25250. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25251. + 0, fifoemptymsk);
  25252. +
  25253. + }
  25254. + }
  25255. + } else {
  25256. + write_isoc_tx_fifo(core_if, ep);
  25257. + }
  25258. + }
  25259. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25260. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25261. +
  25262. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  25263. + dsts_data_t dsts = {.d32 = 0};
  25264. + if (ep->bInterval == 1) {
  25265. + dsts.d32 =
  25266. + DWC_READ_REG32(&core_if->dev_if->
  25267. + dev_global_regs->dsts);
  25268. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  25269. + if (ep->frame_num > 0x3FFF) {
  25270. + ep->frm_overrun = 1;
  25271. + ep->frame_num &= 0x3FFF;
  25272. + } else
  25273. + ep->frm_overrun = 0;
  25274. + if (ep->frame_num & 0x1) {
  25275. + depctl.b.setd1pid = 1;
  25276. + } else {
  25277. + depctl.b.setd0pid = 1;
  25278. + }
  25279. + }
  25280. + }
  25281. + /* EP enable, IN data in FIFO */
  25282. + depctl.b.cnak = 1;
  25283. + depctl.b.epena = 1;
  25284. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25285. +
  25286. + } else {
  25287. + /* OUT endpoint */
  25288. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25289. + core_if->dev_if->out_ep_regs[ep->num];
  25290. +
  25291. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  25292. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  25293. +
  25294. + if (!core_if->dma_desc_enable) {
  25295. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  25296. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  25297. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  25298. + else
  25299. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  25300. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  25301. + }
  25302. +
  25303. + /* Program the transfer size and packet count as follows:
  25304. + *
  25305. + * pktcnt = N
  25306. + * xfersize = N * maxpacket
  25307. + */
  25308. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  25309. + /* Zero Length Packet */
  25310. + deptsiz.b.xfersize = ep->maxpacket;
  25311. + deptsiz.b.pktcnt = 1;
  25312. + } else {
  25313. + deptsiz.b.pktcnt =
  25314. + (ep->xfer_len - ep->xfer_count +
  25315. + (ep->maxpacket - 1)) / ep->maxpacket;
  25316. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  25317. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  25318. + }
  25319. + if (!core_if->dma_desc_enable) {
  25320. + ep->xfer_len =
  25321. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  25322. + }
  25323. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  25324. + }
  25325. +
  25326. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  25327. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25328. +
  25329. + if (core_if->dma_enable) {
  25330. + if (!core_if->dma_desc_enable) {
  25331. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25332. + deptsiz.d32);
  25333. +
  25334. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25335. + (uint32_t) ep->dma_addr);
  25336. + } else {
  25337. +#ifdef DWC_UTE_CFI
  25338. + /* The descriptor chain should be already initialized by now */
  25339. + if (ep->buff_mode != BM_STANDARD) {
  25340. + DWC_WRITE_REG32(&out_regs->doepdma,
  25341. + ep->descs_dma_addr);
  25342. + } else {
  25343. +#endif
  25344. + /** This is used for interrupt out transfers*/
  25345. + if (!ep->xfer_len)
  25346. + ep->xfer_len = ep->total_len;
  25347. + init_dma_desc_chain(core_if, ep);
  25348. +
  25349. + if (core_if->core_params->dev_out_nak) {
  25350. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  25351. + deptsiz.b.pktcnt = (ep->total_len +
  25352. + (ep->maxpacket - 1)) / ep->maxpacket;
  25353. + deptsiz.b.xfersize = ep->total_len;
  25354. + /* Remember initial value of doeptsiz */
  25355. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  25356. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25357. + deptsiz.d32);
  25358. + }
  25359. + }
  25360. + /** DOEPDMAn Register write */
  25361. + DWC_WRITE_REG32(&out_regs->doepdma,
  25362. + ep->dma_desc_addr);
  25363. +#ifdef DWC_UTE_CFI
  25364. + }
  25365. +#endif
  25366. + }
  25367. + } else {
  25368. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25369. + }
  25370. +
  25371. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  25372. + dsts_data_t dsts = {.d32 = 0};
  25373. + if (ep->bInterval == 1) {
  25374. + dsts.d32 =
  25375. + DWC_READ_REG32(&core_if->dev_if->
  25376. + dev_global_regs->dsts);
  25377. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  25378. + if (ep->frame_num > 0x3FFF) {
  25379. + ep->frm_overrun = 1;
  25380. + ep->frame_num &= 0x3FFF;
  25381. + } else
  25382. + ep->frm_overrun = 0;
  25383. +
  25384. + if (ep->frame_num & 0x1) {
  25385. + depctl.b.setd1pid = 1;
  25386. + } else {
  25387. + depctl.b.setd0pid = 1;
  25388. + }
  25389. + }
  25390. + }
  25391. +
  25392. + /* EP enable */
  25393. + depctl.b.cnak = 1;
  25394. + depctl.b.epena = 1;
  25395. +
  25396. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25397. +
  25398. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  25399. + DWC_READ_REG32(&out_regs->doepctl),
  25400. + DWC_READ_REG32(&out_regs->doeptsiz));
  25401. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  25402. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  25403. + daintmsk),
  25404. + DWC_READ_REG32(&core_if->core_global_regs->
  25405. + gintmsk));
  25406. +
  25407. + /* Timer is scheduling only for out bulk transfers for
  25408. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  25409. + * about received data payload in case of timeout
  25410. + */
  25411. + if (core_if->core_params->dev_out_nak) {
  25412. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  25413. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  25414. + core_if->ep_xfer_info[ep->num].ep = ep;
  25415. + core_if->ep_xfer_info[ep->num].state = 1;
  25416. +
  25417. + /* Start a timer for this transfer. */
  25418. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  25419. + }
  25420. + }
  25421. + }
  25422. +}
  25423. +
  25424. +/**
  25425. + * This function setup a zero length transfer in Buffer DMA and
  25426. + * Slave modes for usb requests with zero field set
  25427. + *
  25428. + * @param core_if Programming view of DWC_otg controller.
  25429. + * @param ep The EP to start the transfer on.
  25430. + *
  25431. + */
  25432. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25433. +{
  25434. +
  25435. + depctl_data_t depctl;
  25436. + deptsiz_data_t deptsiz;
  25437. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25438. +
  25439. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  25440. + DWC_PRINTF("zero length transfer is called\n");
  25441. +
  25442. + /* IN endpoint */
  25443. + if (ep->is_in == 1) {
  25444. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25445. + core_if->dev_if->in_ep_regs[ep->num];
  25446. +
  25447. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  25448. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  25449. +
  25450. + deptsiz.b.xfersize = 0;
  25451. + deptsiz.b.pktcnt = 1;
  25452. +
  25453. + /* Write the DMA register */
  25454. + if (core_if->dma_enable) {
  25455. + if (core_if->dma_desc_enable == 0) {
  25456. + deptsiz.b.mc = 1;
  25457. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25458. + deptsiz.d32);
  25459. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25460. + (uint32_t) ep->dma_addr);
  25461. + }
  25462. + } else {
  25463. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25464. + /**
  25465. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  25466. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  25467. + * the data will be written into the fifo by the ISR.
  25468. + */
  25469. + if (core_if->en_multiple_tx_fifo == 0) {
  25470. + intr_mask.b.nptxfempty = 1;
  25471. + DWC_MODIFY_REG32(&core_if->
  25472. + core_global_regs->gintmsk,
  25473. + intr_mask.d32, intr_mask.d32);
  25474. + } else {
  25475. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25476. + if (ep->xfer_len > 0) {
  25477. + uint32_t fifoemptymsk = 0;
  25478. + fifoemptymsk = 1 << ep->num;
  25479. + DWC_MODIFY_REG32(&core_if->
  25480. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25481. + 0, fifoemptymsk);
  25482. + }
  25483. + }
  25484. + }
  25485. +
  25486. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25487. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25488. + /* EP enable, IN data in FIFO */
  25489. + depctl.b.cnak = 1;
  25490. + depctl.b.epena = 1;
  25491. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25492. +
  25493. + } else {
  25494. + /* OUT endpoint */
  25495. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25496. + core_if->dev_if->out_ep_regs[ep->num];
  25497. +
  25498. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  25499. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  25500. +
  25501. + /* Zero Length Packet */
  25502. + deptsiz.b.xfersize = ep->maxpacket;
  25503. + deptsiz.b.pktcnt = 1;
  25504. +
  25505. + if (core_if->dma_enable) {
  25506. + if (!core_if->dma_desc_enable) {
  25507. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25508. + deptsiz.d32);
  25509. +
  25510. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25511. + (uint32_t) ep->dma_addr);
  25512. + }
  25513. + } else {
  25514. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25515. + }
  25516. +
  25517. + /* EP enable */
  25518. + depctl.b.cnak = 1;
  25519. + depctl.b.epena = 1;
  25520. +
  25521. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25522. +
  25523. + }
  25524. +}
  25525. +
  25526. +/**
  25527. + * This function does the setup for a data transfer for EP0 and starts
  25528. + * the transfer. For an IN transfer, the packets will be loaded into
  25529. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  25530. + * unloaded from the Rx FIFO in the ISR.
  25531. + *
  25532. + * @param core_if Programming view of DWC_otg controller.
  25533. + * @param ep The EP0 data.
  25534. + */
  25535. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25536. +{
  25537. + depctl_data_t depctl;
  25538. + deptsiz0_data_t deptsiz;
  25539. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25540. + dwc_otg_dev_dma_desc_t *dma_desc;
  25541. +
  25542. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  25543. + "xfer_buff=%p start_xfer_buff=%p \n",
  25544. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  25545. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  25546. +
  25547. + ep->total_len = ep->xfer_len;
  25548. +
  25549. + /* IN endpoint */
  25550. + if (ep->is_in == 1) {
  25551. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25552. + core_if->dev_if->in_ep_regs[0];
  25553. +
  25554. + gnptxsts_data_t gtxstatus;
  25555. +
  25556. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  25557. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25558. + if (depctl.b.epena)
  25559. + return;
  25560. + }
  25561. +
  25562. + gtxstatus.d32 =
  25563. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25564. +
  25565. + /* If dedicated FIFO every time flush fifo before enable ep*/
  25566. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  25567. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  25568. +
  25569. + if (core_if->en_multiple_tx_fifo == 0
  25570. + && gtxstatus.b.nptxqspcavail == 0
  25571. + && !core_if->dma_enable) {
  25572. +#ifdef DEBUG
  25573. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25574. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  25575. + DWC_READ_REG32(&in_regs->diepctl));
  25576. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  25577. + deptsiz.d32,
  25578. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25579. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  25580. + gtxstatus.d32);
  25581. +#endif
  25582. + return;
  25583. + }
  25584. +
  25585. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25586. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25587. +
  25588. + /* Zero Length Packet? */
  25589. + if (ep->xfer_len == 0) {
  25590. + deptsiz.b.xfersize = 0;
  25591. + deptsiz.b.pktcnt = 1;
  25592. + } else {
  25593. + /* Program the transfer size and packet count
  25594. + * as follows: xfersize = N * maxpacket +
  25595. + * short_packet pktcnt = N + (short_packet
  25596. + * exist ? 1 : 0)
  25597. + */
  25598. + if (ep->xfer_len > ep->maxpacket) {
  25599. + ep->xfer_len = ep->maxpacket;
  25600. + deptsiz.b.xfersize = ep->maxpacket;
  25601. + } else {
  25602. + deptsiz.b.xfersize = ep->xfer_len;
  25603. + }
  25604. + deptsiz.b.pktcnt = 1;
  25605. +
  25606. + }
  25607. + DWC_DEBUGPL(DBG_PCDV,
  25608. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25609. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25610. + deptsiz.d32);
  25611. +
  25612. + /* Write the DMA register */
  25613. + if (core_if->dma_enable) {
  25614. + if (core_if->dma_desc_enable == 0) {
  25615. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25616. + deptsiz.d32);
  25617. +
  25618. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25619. + (uint32_t) ep->dma_addr);
  25620. + } else {
  25621. + dma_desc = core_if->dev_if->in_desc_addr;
  25622. +
  25623. + /** DMA Descriptor Setup */
  25624. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25625. + dma_desc->status.b.l = 1;
  25626. + dma_desc->status.b.ioc = 1;
  25627. + dma_desc->status.b.sp =
  25628. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  25629. + dma_desc->status.b.bytes = ep->xfer_len;
  25630. + dma_desc->buf = ep->dma_addr;
  25631. + dma_desc->status.b.sts = 0;
  25632. + dma_desc->status.b.bs = BS_HOST_READY;
  25633. +
  25634. + /** DIEPDMA0 Register write */
  25635. + DWC_WRITE_REG32(&in_regs->diepdma,
  25636. + core_if->
  25637. + dev_if->dma_in_desc_addr);
  25638. + }
  25639. + } else {
  25640. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25641. + }
  25642. +
  25643. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25644. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25645. + /* EP enable, IN data in FIFO */
  25646. + depctl.b.cnak = 1;
  25647. + depctl.b.epena = 1;
  25648. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25649. +
  25650. + /**
  25651. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  25652. + * data will be written into the fifo by the ISR.
  25653. + */
  25654. + if (!core_if->dma_enable) {
  25655. + if (core_if->en_multiple_tx_fifo == 0) {
  25656. + intr_mask.b.nptxfempty = 1;
  25657. + DWC_MODIFY_REG32(&core_if->
  25658. + core_global_regs->gintmsk,
  25659. + intr_mask.d32, intr_mask.d32);
  25660. + } else {
  25661. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25662. + if (ep->xfer_len > 0) {
  25663. + uint32_t fifoemptymsk = 0;
  25664. + fifoemptymsk |= 1 << ep->num;
  25665. + DWC_MODIFY_REG32(&core_if->
  25666. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25667. + 0, fifoemptymsk);
  25668. + }
  25669. + }
  25670. + }
  25671. + } else {
  25672. + /* OUT endpoint */
  25673. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25674. + core_if->dev_if->out_ep_regs[0];
  25675. +
  25676. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  25677. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  25678. +
  25679. + /* Program the transfer size and packet count as follows:
  25680. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  25681. + * pktcnt = N */
  25682. + /* Zero Length Packet */
  25683. + deptsiz.b.xfersize = ep->maxpacket;
  25684. + deptsiz.b.pktcnt = 1;
  25685. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  25686. + deptsiz.b.supcnt = 3;
  25687. +
  25688. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  25689. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25690. +
  25691. + if (core_if->dma_enable) {
  25692. + if (!core_if->dma_desc_enable) {
  25693. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25694. + deptsiz.d32);
  25695. +
  25696. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25697. + (uint32_t) ep->dma_addr);
  25698. + } else {
  25699. + dma_desc = core_if->dev_if->out_desc_addr;
  25700. +
  25701. + /** DMA Descriptor Setup */
  25702. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25703. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  25704. + dma_desc->status.b.mtrf = 0;
  25705. + dma_desc->status.b.sr = 0;
  25706. + }
  25707. + dma_desc->status.b.l = 1;
  25708. + dma_desc->status.b.ioc = 1;
  25709. + dma_desc->status.b.bytes = ep->maxpacket;
  25710. + dma_desc->buf = ep->dma_addr;
  25711. + dma_desc->status.b.sts = 0;
  25712. + dma_desc->status.b.bs = BS_HOST_READY;
  25713. +
  25714. + /** DOEPDMA0 Register write */
  25715. + DWC_WRITE_REG32(&out_regs->doepdma,
  25716. + core_if->dev_if->
  25717. + dma_out_desc_addr);
  25718. + }
  25719. + } else {
  25720. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25721. + }
  25722. +
  25723. + /* EP enable */
  25724. + depctl.b.cnak = 1;
  25725. + depctl.b.epena = 1;
  25726. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  25727. + }
  25728. +}
  25729. +
  25730. +/**
  25731. + * This function continues control IN transfers started by
  25732. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  25733. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  25734. + * bit for the packet count.
  25735. + *
  25736. + * @param core_if Programming view of DWC_otg controller.
  25737. + * @param ep The EP0 data.
  25738. + */
  25739. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25740. +{
  25741. + depctl_data_t depctl;
  25742. + deptsiz0_data_t deptsiz;
  25743. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25744. + dwc_otg_dev_dma_desc_t *dma_desc;
  25745. +
  25746. + if (ep->is_in == 1) {
  25747. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25748. + core_if->dev_if->in_ep_regs[0];
  25749. + gnptxsts_data_t tx_status = {.d32 = 0 };
  25750. +
  25751. + tx_status.d32 =
  25752. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25753. + /** @todo Should there be check for room in the Tx
  25754. + * Status Queue. If not remove the code above this comment. */
  25755. +
  25756. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25757. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25758. +
  25759. + /* Program the transfer size and packet count
  25760. + * as follows: xfersize = N * maxpacket +
  25761. + * short_packet pktcnt = N + (short_packet
  25762. + * exist ? 1 : 0)
  25763. + */
  25764. +
  25765. + if (core_if->dma_desc_enable == 0) {
  25766. + deptsiz.b.xfersize =
  25767. + (ep->total_len - ep->xfer_count) >
  25768. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  25769. + ep->xfer_count);
  25770. + deptsiz.b.pktcnt = 1;
  25771. + if (core_if->dma_enable == 0) {
  25772. + ep->xfer_len += deptsiz.b.xfersize;
  25773. + } else {
  25774. + ep->xfer_len = deptsiz.b.xfersize;
  25775. + }
  25776. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25777. + } else {
  25778. + ep->xfer_len =
  25779. + (ep->total_len - ep->xfer_count) >
  25780. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  25781. + ep->xfer_count);
  25782. +
  25783. + dma_desc = core_if->dev_if->in_desc_addr;
  25784. +
  25785. + /** DMA Descriptor Setup */
  25786. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25787. + dma_desc->status.b.l = 1;
  25788. + dma_desc->status.b.ioc = 1;
  25789. + dma_desc->status.b.sp =
  25790. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  25791. + dma_desc->status.b.bytes = ep->xfer_len;
  25792. + dma_desc->buf = ep->dma_addr;
  25793. + dma_desc->status.b.sts = 0;
  25794. + dma_desc->status.b.bs = BS_HOST_READY;
  25795. +
  25796. + /** DIEPDMA0 Register write */
  25797. + DWC_WRITE_REG32(&in_regs->diepdma,
  25798. + core_if->dev_if->dma_in_desc_addr);
  25799. + }
  25800. +
  25801. + DWC_DEBUGPL(DBG_PCDV,
  25802. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25803. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25804. + deptsiz.d32);
  25805. +
  25806. + /* Write the DMA register */
  25807. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  25808. + if (core_if->dma_desc_enable == 0)
  25809. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25810. + (uint32_t) ep->dma_addr);
  25811. + }
  25812. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25813. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25814. + /* EP enable, IN data in FIFO */
  25815. + depctl.b.cnak = 1;
  25816. + depctl.b.epena = 1;
  25817. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25818. +
  25819. + /**
  25820. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  25821. + * data will be written into the fifo by the ISR.
  25822. + */
  25823. + if (!core_if->dma_enable) {
  25824. + if (core_if->en_multiple_tx_fifo == 0) {
  25825. + /* First clear it from GINTSTS */
  25826. + intr_mask.b.nptxfempty = 1;
  25827. + DWC_MODIFY_REG32(&core_if->
  25828. + core_global_regs->gintmsk,
  25829. + intr_mask.d32, intr_mask.d32);
  25830. +
  25831. + } else {
  25832. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25833. + if (ep->xfer_len > 0) {
  25834. + uint32_t fifoemptymsk = 0;
  25835. + fifoemptymsk |= 1 << ep->num;
  25836. + DWC_MODIFY_REG32(&core_if->
  25837. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25838. + 0, fifoemptymsk);
  25839. + }
  25840. + }
  25841. + }
  25842. + } else {
  25843. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25844. + core_if->dev_if->out_ep_regs[0];
  25845. +
  25846. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  25847. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  25848. +
  25849. + /* Program the transfer size and packet count
  25850. + * as follows: xfersize = N * maxpacket +
  25851. + * short_packet pktcnt = N + (short_packet
  25852. + * exist ? 1 : 0)
  25853. + */
  25854. + deptsiz.b.xfersize = ep->maxpacket;
  25855. + deptsiz.b.pktcnt = 1;
  25856. +
  25857. + if (core_if->dma_desc_enable == 0) {
  25858. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25859. + } else {
  25860. + dma_desc = core_if->dev_if->out_desc_addr;
  25861. +
  25862. + /** DMA Descriptor Setup */
  25863. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25864. + dma_desc->status.b.l = 1;
  25865. + dma_desc->status.b.ioc = 1;
  25866. + dma_desc->status.b.bytes = ep->maxpacket;
  25867. + dma_desc->buf = ep->dma_addr;
  25868. + dma_desc->status.b.sts = 0;
  25869. + dma_desc->status.b.bs = BS_HOST_READY;
  25870. +
  25871. + /** DOEPDMA0 Register write */
  25872. + DWC_WRITE_REG32(&out_regs->doepdma,
  25873. + core_if->dev_if->dma_out_desc_addr);
  25874. + }
  25875. +
  25876. + DWC_DEBUGPL(DBG_PCDV,
  25877. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25878. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25879. + deptsiz.d32);
  25880. +
  25881. + /* Write the DMA register */
  25882. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  25883. + if (core_if->dma_desc_enable == 0)
  25884. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25885. + (uint32_t) ep->dma_addr);
  25886. +
  25887. + }
  25888. +
  25889. + /* EP enable, IN data in FIFO */
  25890. + depctl.b.cnak = 1;
  25891. + depctl.b.epena = 1;
  25892. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25893. +
  25894. + }
  25895. +}
  25896. +
  25897. +#ifdef DEBUG
  25898. +void dump_msg(const u8 * buf, unsigned int length)
  25899. +{
  25900. + unsigned int start, num, i;
  25901. + char line[52], *p;
  25902. +
  25903. + if (length >= 512)
  25904. + return;
  25905. + start = 0;
  25906. + while (length > 0) {
  25907. + num = length < 16u ? length : 16u;
  25908. + p = line;
  25909. + for (i = 0; i < num; ++i) {
  25910. + if (i == 8)
  25911. + *p++ = ' ';
  25912. + DWC_SPRINTF(p, " %02x", buf[i]);
  25913. + p += 3;
  25914. + }
  25915. + *p = 0;
  25916. + DWC_PRINTF("%6x: %s\n", start, line);
  25917. + buf += num;
  25918. + start += num;
  25919. + length -= num;
  25920. + }
  25921. +}
  25922. +#else
  25923. +static inline void dump_msg(const u8 * buf, unsigned int length)
  25924. +{
  25925. +}
  25926. +#endif
  25927. +
  25928. +/**
  25929. + * This function writes a packet into the Tx FIFO associated with the
  25930. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  25931. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  25932. + * with all packets for the next micro-frame.
  25933. + *
  25934. + * @param core_if Programming view of DWC_otg controller.
  25935. + * @param ep The EP to write packet for.
  25936. + * @param dma Indicates if DMA is being used.
  25937. + */
  25938. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  25939. + int dma)
  25940. +{
  25941. + /**
  25942. + * The buffer is padded to DWORD on a per packet basis in
  25943. + * slave/dma mode if the MPS is not DWORD aligned. The last
  25944. + * packet, if short, is also padded to a multiple of DWORD.
  25945. + *
  25946. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  25947. + * multiple of DWORD in length
  25948. + *
  25949. + * ep->xfer_len can be any number of bytes
  25950. + *
  25951. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  25952. + * packet
  25953. + *
  25954. + * FIFO access is DWORD */
  25955. +
  25956. + uint32_t i;
  25957. + uint32_t byte_count;
  25958. + uint32_t dword_count;
  25959. + uint32_t *fifo;
  25960. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  25961. +
  25962. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  25963. + ep);
  25964. + if (ep->xfer_count >= ep->xfer_len) {
  25965. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  25966. + return;
  25967. + }
  25968. +
  25969. + /* Find the byte length of the packet either short packet or MPS */
  25970. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  25971. + byte_count = ep->xfer_len - ep->xfer_count;
  25972. + } else {
  25973. + byte_count = ep->maxpacket;
  25974. + }
  25975. +
  25976. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  25977. + * is not a multiple of DWORD */
  25978. + dword_count = (byte_count + 3) / 4;
  25979. +
  25980. +#ifdef VERBOSE
  25981. + dump_msg(ep->xfer_buff, byte_count);
  25982. +#endif
  25983. +
  25984. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  25985. + * intialized? What should this be? */
  25986. +
  25987. + fifo = core_if->data_fifo[ep->num];
  25988. +
  25989. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  25990. + fifo, data_buff, *data_buff, byte_count);
  25991. +
  25992. + if (!dma) {
  25993. + for (i = 0; i < dword_count; i++, data_buff++) {
  25994. + DWC_WRITE_REG32(fifo, *data_buff);
  25995. + }
  25996. + }
  25997. +
  25998. + ep->xfer_count += byte_count;
  25999. + ep->xfer_buff += byte_count;
  26000. + ep->dma_addr += byte_count;
  26001. +}
  26002. +
  26003. +/**
  26004. + * Set the EP STALL.
  26005. + *
  26006. + * @param core_if Programming view of DWC_otg controller.
  26007. + * @param ep The EP to set the stall on.
  26008. + */
  26009. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  26010. +{
  26011. + depctl_data_t depctl;
  26012. + volatile uint32_t *depctl_addr;
  26013. +
  26014. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  26015. + (ep->is_in ? "IN" : "OUT"));
  26016. +
  26017. + if (ep->is_in == 1) {
  26018. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  26019. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  26020. +
  26021. + /* set the disable and stall bits */
  26022. + if (depctl.b.epena) {
  26023. + depctl.b.epdis = 1;
  26024. + }
  26025. + depctl.b.stall = 1;
  26026. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  26027. + } else {
  26028. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  26029. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  26030. +
  26031. + /* set the stall bit */
  26032. + depctl.b.stall = 1;
  26033. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  26034. + }
  26035. +
  26036. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  26037. +
  26038. + return;
  26039. +}
  26040. +
  26041. +/**
  26042. + * Clear the EP STALL.
  26043. + *
  26044. + * @param core_if Programming view of DWC_otg controller.
  26045. + * @param ep The EP to clear stall from.
  26046. + */
  26047. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  26048. +{
  26049. + depctl_data_t depctl;
  26050. + volatile uint32_t *depctl_addr;
  26051. +
  26052. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  26053. + (ep->is_in ? "IN" : "OUT"));
  26054. +
  26055. + if (ep->is_in == 1) {
  26056. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  26057. + } else {
  26058. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  26059. + }
  26060. +
  26061. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  26062. +
  26063. + /* clear the stall bits */
  26064. + depctl.b.stall = 0;
  26065. +
  26066. + /*
  26067. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  26068. + * of whether an endpoint has the Halt feature set, a
  26069. + * ClearFeature(ENDPOINT_HALT) request always results in the
  26070. + * data toggle being reinitialized to DATA0.
  26071. + */
  26072. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  26073. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  26074. + depctl.b.setd0pid = 1; /* DATA0 */
  26075. + }
  26076. +
  26077. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  26078. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  26079. + return;
  26080. +}
  26081. +
  26082. +/**
  26083. + * This function reads a packet from the Rx FIFO into the destination
  26084. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  26085. + *
  26086. + * @param core_if Programming view of DWC_otg controller.
  26087. + * @param dest Destination buffer for the packet.
  26088. + * @param bytes Number of bytes to copy to the destination.
  26089. + */
  26090. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  26091. + uint8_t * dest, uint16_t bytes)
  26092. +{
  26093. + int i;
  26094. + int word_count = (bytes + 3) / 4;
  26095. +
  26096. + volatile uint32_t *fifo = core_if->data_fifo[0];
  26097. + uint32_t *data_buff = (uint32_t *) dest;
  26098. +
  26099. + /**
  26100. + * @todo Account for the case where _dest is not dword aligned. This
  26101. + * requires reading data from the FIFO into a uint32_t temp buffer,
  26102. + * then moving it into the data buffer.
  26103. + */
  26104. +
  26105. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  26106. + core_if, dest, bytes);
  26107. +
  26108. + for (i = 0; i < word_count; i++, data_buff++) {
  26109. + *data_buff = DWC_READ_REG32(fifo);
  26110. + }
  26111. +
  26112. + return;
  26113. +}
  26114. +
  26115. +/**
  26116. + * This functions reads the device registers and prints them
  26117. + *
  26118. + * @param core_if Programming view of DWC_otg controller.
  26119. + */
  26120. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  26121. +{
  26122. + int i;
  26123. + volatile uint32_t *addr;
  26124. +
  26125. + DWC_PRINTF("Device Global Registers\n");
  26126. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  26127. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  26128. + (unsigned long)addr, DWC_READ_REG32(addr));
  26129. + addr = &core_if->dev_if->dev_global_regs->dctl;
  26130. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  26131. + (unsigned long)addr, DWC_READ_REG32(addr));
  26132. + addr = &core_if->dev_if->dev_global_regs->dsts;
  26133. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  26134. + (unsigned long)addr, DWC_READ_REG32(addr));
  26135. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  26136. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26137. + DWC_READ_REG32(addr));
  26138. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  26139. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26140. + DWC_READ_REG32(addr));
  26141. + addr = &core_if->dev_if->dev_global_regs->daint;
  26142. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26143. + DWC_READ_REG32(addr));
  26144. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  26145. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26146. + DWC_READ_REG32(addr));
  26147. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  26148. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26149. + DWC_READ_REG32(addr));
  26150. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  26151. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  26152. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  26153. + (unsigned long)addr, DWC_READ_REG32(addr));
  26154. + }
  26155. +
  26156. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  26157. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26158. + DWC_READ_REG32(addr));
  26159. +
  26160. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  26161. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  26162. + (unsigned long)addr, DWC_READ_REG32(addr));
  26163. +
  26164. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  26165. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  26166. + (unsigned long)addr, DWC_READ_REG32(addr));
  26167. +
  26168. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  26169. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  26170. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  26171. + (unsigned long)addr, DWC_READ_REG32(addr));
  26172. + }
  26173. +
  26174. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  26175. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26176. + DWC_READ_REG32(addr));
  26177. +
  26178. + if (core_if->hwcfg2.b.multi_proc_int) {
  26179. +
  26180. + addr = &core_if->dev_if->dev_global_regs->deachint;
  26181. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  26182. + (unsigned long)addr, DWC_READ_REG32(addr));
  26183. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  26184. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  26185. + (unsigned long)addr, DWC_READ_REG32(addr));
  26186. +
  26187. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  26188. + addr =
  26189. + &core_if->dev_if->
  26190. + dev_global_regs->diepeachintmsk[i];
  26191. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  26192. + i, (unsigned long)addr,
  26193. + DWC_READ_REG32(addr));
  26194. + }
  26195. +
  26196. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  26197. + addr =
  26198. + &core_if->dev_if->
  26199. + dev_global_regs->doepeachintmsk[i];
  26200. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  26201. + i, (unsigned long)addr,
  26202. + DWC_READ_REG32(addr));
  26203. + }
  26204. + }
  26205. +
  26206. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  26207. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  26208. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  26209. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  26210. + (unsigned long)addr, DWC_READ_REG32(addr));
  26211. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  26212. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  26213. + (unsigned long)addr, DWC_READ_REG32(addr));
  26214. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  26215. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  26216. + (unsigned long)addr, DWC_READ_REG32(addr));
  26217. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  26218. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  26219. + (unsigned long)addr, DWC_READ_REG32(addr));
  26220. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  26221. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  26222. + (unsigned long)addr, DWC_READ_REG32(addr));
  26223. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  26224. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  26225. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  26226. + }
  26227. +
  26228. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  26229. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  26230. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  26231. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  26232. + (unsigned long)addr, DWC_READ_REG32(addr));
  26233. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  26234. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  26235. + (unsigned long)addr, DWC_READ_REG32(addr));
  26236. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  26237. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  26238. + (unsigned long)addr, DWC_READ_REG32(addr));
  26239. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  26240. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  26241. + (unsigned long)addr, DWC_READ_REG32(addr));
  26242. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  26243. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  26244. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  26245. + (unsigned long)addr, DWC_READ_REG32(addr));
  26246. + }
  26247. +
  26248. + }
  26249. +}
  26250. +
  26251. +/**
  26252. + * This functions reads the SPRAM and prints its content
  26253. + *
  26254. + * @param core_if Programming view of DWC_otg controller.
  26255. + */
  26256. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  26257. +{
  26258. + volatile uint8_t *addr, *start_addr, *end_addr;
  26259. +
  26260. + DWC_PRINTF("SPRAM Data:\n");
  26261. + start_addr = (void *)core_if->core_global_regs;
  26262. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  26263. + start_addr += 0x00028000;
  26264. + end_addr = (void *)core_if->core_global_regs;
  26265. + end_addr += 0x000280e0;
  26266. +
  26267. + for (addr = start_addr; addr < end_addr; addr += 16) {
  26268. + DWC_PRINTF
  26269. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  26270. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  26271. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  26272. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  26273. + );
  26274. + }
  26275. +
  26276. + return;
  26277. +}
  26278. +
  26279. +/**
  26280. + * This function reads the host registers and prints them
  26281. + *
  26282. + * @param core_if Programming view of DWC_otg controller.
  26283. + */
  26284. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  26285. +{
  26286. + int i;
  26287. + volatile uint32_t *addr;
  26288. +
  26289. + DWC_PRINTF("Host Global Registers\n");
  26290. + addr = &core_if->host_if->host_global_regs->hcfg;
  26291. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  26292. + (unsigned long)addr, DWC_READ_REG32(addr));
  26293. + addr = &core_if->host_if->host_global_regs->hfir;
  26294. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  26295. + (unsigned long)addr, DWC_READ_REG32(addr));
  26296. + addr = &core_if->host_if->host_global_regs->hfnum;
  26297. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26298. + DWC_READ_REG32(addr));
  26299. + addr = &core_if->host_if->host_global_regs->hptxsts;
  26300. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26301. + DWC_READ_REG32(addr));
  26302. + addr = &core_if->host_if->host_global_regs->haint;
  26303. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26304. + DWC_READ_REG32(addr));
  26305. + addr = &core_if->host_if->host_global_regs->haintmsk;
  26306. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26307. + DWC_READ_REG32(addr));
  26308. + if (core_if->dma_desc_enable) {
  26309. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  26310. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  26311. + (unsigned long)addr, DWC_READ_REG32(addr));
  26312. + }
  26313. +
  26314. + addr = core_if->host_if->hprt0;
  26315. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26316. + DWC_READ_REG32(addr));
  26317. +
  26318. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  26319. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  26320. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  26321. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  26322. + (unsigned long)addr, DWC_READ_REG32(addr));
  26323. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  26324. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  26325. + (unsigned long)addr, DWC_READ_REG32(addr));
  26326. + addr = &core_if->host_if->hc_regs[i]->hcint;
  26327. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  26328. + (unsigned long)addr, DWC_READ_REG32(addr));
  26329. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  26330. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  26331. + (unsigned long)addr, DWC_READ_REG32(addr));
  26332. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  26333. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  26334. + (unsigned long)addr, DWC_READ_REG32(addr));
  26335. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  26336. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  26337. + (unsigned long)addr, DWC_READ_REG32(addr));
  26338. + if (core_if->dma_desc_enable) {
  26339. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  26340. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  26341. + (unsigned long)addr, DWC_READ_REG32(addr));
  26342. + }
  26343. +
  26344. + }
  26345. + return;
  26346. +}
  26347. +
  26348. +/**
  26349. + * This function reads the core global registers and prints them
  26350. + *
  26351. + * @param core_if Programming view of DWC_otg controller.
  26352. + */
  26353. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  26354. +{
  26355. + int i, ep_num;
  26356. + volatile uint32_t *addr;
  26357. + char *txfsiz;
  26358. +
  26359. + DWC_PRINTF("Core Global Registers\n");
  26360. + addr = &core_if->core_global_regs->gotgctl;
  26361. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26362. + DWC_READ_REG32(addr));
  26363. + addr = &core_if->core_global_regs->gotgint;
  26364. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26365. + DWC_READ_REG32(addr));
  26366. + addr = &core_if->core_global_regs->gahbcfg;
  26367. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26368. + DWC_READ_REG32(addr));
  26369. + addr = &core_if->core_global_regs->gusbcfg;
  26370. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26371. + DWC_READ_REG32(addr));
  26372. + addr = &core_if->core_global_regs->grstctl;
  26373. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26374. + DWC_READ_REG32(addr));
  26375. + addr = &core_if->core_global_regs->gintsts;
  26376. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26377. + DWC_READ_REG32(addr));
  26378. + addr = &core_if->core_global_regs->gintmsk;
  26379. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26380. + DWC_READ_REG32(addr));
  26381. + addr = &core_if->core_global_regs->grxstsr;
  26382. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26383. + DWC_READ_REG32(addr));
  26384. + addr = &core_if->core_global_regs->grxfsiz;
  26385. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26386. + DWC_READ_REG32(addr));
  26387. + addr = &core_if->core_global_regs->gnptxfsiz;
  26388. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26389. + DWC_READ_REG32(addr));
  26390. + addr = &core_if->core_global_regs->gnptxsts;
  26391. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26392. + DWC_READ_REG32(addr));
  26393. + addr = &core_if->core_global_regs->gi2cctl;
  26394. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26395. + DWC_READ_REG32(addr));
  26396. + addr = &core_if->core_global_regs->gpvndctl;
  26397. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26398. + DWC_READ_REG32(addr));
  26399. + addr = &core_if->core_global_regs->ggpio;
  26400. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26401. + DWC_READ_REG32(addr));
  26402. + addr = &core_if->core_global_regs->guid;
  26403. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  26404. + (unsigned long)addr, DWC_READ_REG32(addr));
  26405. + addr = &core_if->core_global_regs->gsnpsid;
  26406. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26407. + DWC_READ_REG32(addr));
  26408. + addr = &core_if->core_global_regs->ghwcfg1;
  26409. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26410. + DWC_READ_REG32(addr));
  26411. + addr = &core_if->core_global_regs->ghwcfg2;
  26412. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26413. + DWC_READ_REG32(addr));
  26414. + addr = &core_if->core_global_regs->ghwcfg3;
  26415. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26416. + DWC_READ_REG32(addr));
  26417. + addr = &core_if->core_global_regs->ghwcfg4;
  26418. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26419. + DWC_READ_REG32(addr));
  26420. + addr = &core_if->core_global_regs->glpmcfg;
  26421. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26422. + DWC_READ_REG32(addr));
  26423. + addr = &core_if->core_global_regs->gpwrdn;
  26424. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26425. + DWC_READ_REG32(addr));
  26426. + addr = &core_if->core_global_regs->gdfifocfg;
  26427. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26428. + DWC_READ_REG32(addr));
  26429. + addr = &core_if->core_global_regs->adpctl;
  26430. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26431. + dwc_otg_adp_read_reg(core_if));
  26432. + addr = &core_if->core_global_regs->hptxfsiz;
  26433. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26434. + DWC_READ_REG32(addr));
  26435. +
  26436. + if (core_if->en_multiple_tx_fifo == 0) {
  26437. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  26438. + txfsiz = "DPTXFSIZ";
  26439. + } else {
  26440. + ep_num = core_if->hwcfg4.b.num_in_eps;
  26441. + txfsiz = "DIENPTXF";
  26442. + }
  26443. + for (i = 0; i < ep_num; i++) {
  26444. + addr = &core_if->core_global_regs->dtxfsiz[i];
  26445. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  26446. + (unsigned long)addr, DWC_READ_REG32(addr));
  26447. + }
  26448. + addr = core_if->pcgcctl;
  26449. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26450. + DWC_READ_REG32(addr));
  26451. +}
  26452. +
  26453. +/**
  26454. + * Flush a Tx FIFO.
  26455. + *
  26456. + * @param core_if Programming view of DWC_otg controller.
  26457. + * @param num Tx FIFO to flush.
  26458. + */
  26459. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  26460. +{
  26461. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26462. + volatile grstctl_t greset = {.d32 = 0 };
  26463. + int count = 0;
  26464. +
  26465. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  26466. +
  26467. + greset.b.txfflsh = 1;
  26468. + greset.b.txfnum = num;
  26469. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26470. +
  26471. + do {
  26472. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26473. + if (++count > 10000) {
  26474. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  26475. + __func__, greset.d32,
  26476. + DWC_READ_REG32(&global_regs->gnptxsts));
  26477. + break;
  26478. + }
  26479. + dwc_udelay(1);
  26480. + } while (greset.b.txfflsh == 1);
  26481. +
  26482. + /* Wait for 3 PHY Clocks */
  26483. + dwc_udelay(1);
  26484. +}
  26485. +
  26486. +/**
  26487. + * Flush Rx FIFO.
  26488. + *
  26489. + * @param core_if Programming view of DWC_otg controller.
  26490. + */
  26491. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  26492. +{
  26493. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26494. + volatile grstctl_t greset = {.d32 = 0 };
  26495. + int count = 0;
  26496. +
  26497. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  26498. + /*
  26499. + *
  26500. + */
  26501. + greset.b.rxfflsh = 1;
  26502. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26503. +
  26504. + do {
  26505. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26506. + if (++count > 10000) {
  26507. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  26508. + greset.d32);
  26509. + break;
  26510. + }
  26511. + dwc_udelay(1);
  26512. + } while (greset.b.rxfflsh == 1);
  26513. +
  26514. + /* Wait for 3 PHY Clocks */
  26515. + dwc_udelay(1);
  26516. +}
  26517. +
  26518. +/**
  26519. + * Do core a soft reset of the core. Be careful with this because it
  26520. + * resets all the internal state machines of the core.
  26521. + */
  26522. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  26523. +{
  26524. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26525. + volatile grstctl_t greset = {.d32 = 0 };
  26526. + int count = 0;
  26527. +
  26528. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  26529. + /* Wait for AHB master IDLE state. */
  26530. + do {
  26531. + dwc_udelay(10);
  26532. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26533. + if (++count > 100000) {
  26534. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  26535. + greset.d32);
  26536. + return;
  26537. + }
  26538. + }
  26539. + while (greset.b.ahbidle == 0);
  26540. +
  26541. + /* Core Soft Reset */
  26542. + count = 0;
  26543. + greset.b.csftrst = 1;
  26544. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26545. + do {
  26546. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26547. + if (++count > 10000) {
  26548. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  26549. + __func__, greset.d32);
  26550. + break;
  26551. + }
  26552. + dwc_udelay(1);
  26553. + }
  26554. + while (greset.b.csftrst == 1);
  26555. +
  26556. + /* Wait for 3 PHY Clocks */
  26557. + dwc_mdelay(100);
  26558. +}
  26559. +
  26560. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  26561. +{
  26562. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  26563. +}
  26564. +
  26565. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  26566. +{
  26567. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  26568. +}
  26569. +
  26570. +/**
  26571. + * Register HCD callbacks. The callbacks are used to start and stop
  26572. + * the HCD for interrupt processing.
  26573. + *
  26574. + * @param core_if Programming view of DWC_otg controller.
  26575. + * @param cb the HCD callback structure.
  26576. + * @param p pointer to be passed to callback function (usb_hcd*).
  26577. + */
  26578. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  26579. + dwc_otg_cil_callbacks_t * cb, void *p)
  26580. +{
  26581. + core_if->hcd_cb = cb;
  26582. + cb->p = p;
  26583. +}
  26584. +
  26585. +/**
  26586. + * Register PCD callbacks. The callbacks are used to start and stop
  26587. + * the PCD for interrupt processing.
  26588. + *
  26589. + * @param core_if Programming view of DWC_otg controller.
  26590. + * @param cb the PCD callback structure.
  26591. + * @param p pointer to be passed to callback function (pcd*).
  26592. + */
  26593. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  26594. + dwc_otg_cil_callbacks_t * cb, void *p)
  26595. +{
  26596. + core_if->pcd_cb = cb;
  26597. + cb->p = p;
  26598. +}
  26599. +
  26600. +#ifdef DWC_EN_ISOC
  26601. +
  26602. +/**
  26603. + * This function writes isoc data per 1 (micro)frame into tx fifo
  26604. + *
  26605. + * @param core_if Programming view of DWC_otg controller.
  26606. + * @param ep The EP to start the transfer on.
  26607. + *
  26608. + */
  26609. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  26610. +{
  26611. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  26612. + dtxfsts_data_t txstatus = {.d32 = 0 };
  26613. + uint32_t len = 0;
  26614. + uint32_t dwords;
  26615. +
  26616. + ep->xfer_len = ep->data_per_frame;
  26617. + ep->xfer_count = 0;
  26618. +
  26619. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  26620. +
  26621. + len = ep->xfer_len - ep->xfer_count;
  26622. +
  26623. + if (len > ep->maxpacket) {
  26624. + len = ep->maxpacket;
  26625. + }
  26626. +
  26627. + dwords = (len + 3) / 4;
  26628. +
  26629. + /* While there is space in the queue and space in the FIFO and
  26630. + * More data to tranfer, Write packets to the Tx FIFO */
  26631. + txstatus.d32 =
  26632. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  26633. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  26634. +
  26635. + while (txstatus.b.txfspcavail > dwords &&
  26636. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  26637. + /* Write the FIFO */
  26638. + dwc_otg_ep_write_packet(core_if, ep, 0);
  26639. +
  26640. + len = ep->xfer_len - ep->xfer_count;
  26641. + if (len > ep->maxpacket) {
  26642. + len = ep->maxpacket;
  26643. + }
  26644. +
  26645. + dwords = (len + 3) / 4;
  26646. + txstatus.d32 =
  26647. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  26648. + dtxfsts);
  26649. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  26650. + txstatus.d32);
  26651. + }
  26652. +}
  26653. +
  26654. +/**
  26655. + * This function initializes a descriptor chain for Isochronous transfer
  26656. + *
  26657. + * @param core_if Programming view of DWC_otg controller.
  26658. + * @param ep The EP to start the transfer on.
  26659. + *
  26660. + */
  26661. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  26662. + dwc_ep_t * ep)
  26663. +{
  26664. + deptsiz_data_t deptsiz = {.d32 = 0 };
  26665. + depctl_data_t depctl = {.d32 = 0 };
  26666. + dsts_data_t dsts = {.d32 = 0 };
  26667. + volatile uint32_t *addr;
  26668. +
  26669. + if (ep->is_in) {
  26670. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  26671. + } else {
  26672. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  26673. + }
  26674. +
  26675. + ep->xfer_len = ep->data_per_frame;
  26676. + ep->xfer_count = 0;
  26677. + ep->xfer_buff = ep->cur_pkt_addr;
  26678. + ep->dma_addr = ep->cur_pkt_dma_addr;
  26679. +
  26680. + if (ep->is_in) {
  26681. + /* Program the transfer size and packet count
  26682. + * as follows: xfersize = N * maxpacket +
  26683. + * short_packet pktcnt = N + (short_packet
  26684. + * exist ? 1 : 0)
  26685. + */
  26686. + deptsiz.b.xfersize = ep->xfer_len;
  26687. + deptsiz.b.pktcnt =
  26688. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  26689. + deptsiz.b.mc = deptsiz.b.pktcnt;
  26690. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  26691. + deptsiz.d32);
  26692. +
  26693. + /* Write the DMA register */
  26694. + if (core_if->dma_enable) {
  26695. + DWC_WRITE_REG32(&
  26696. + (core_if->dev_if->in_ep_regs[ep->num]->
  26697. + diepdma), (uint32_t) ep->dma_addr);
  26698. + }
  26699. + } else {
  26700. + deptsiz.b.pktcnt =
  26701. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  26702. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  26703. +
  26704. + DWC_WRITE_REG32(&core_if->dev_if->
  26705. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  26706. +
  26707. + if (core_if->dma_enable) {
  26708. + DWC_WRITE_REG32(&
  26709. + (core_if->dev_if->
  26710. + out_ep_regs[ep->num]->doepdma),
  26711. + (uint32_t) ep->dma_addr);
  26712. + }
  26713. + }
  26714. +
  26715. + /** Enable endpoint, clear nak */
  26716. +
  26717. + depctl.d32 = 0;
  26718. + if (ep->bInterval == 1) {
  26719. + dsts.d32 =
  26720. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  26721. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  26722. +
  26723. + if (ep->next_frame & 0x1) {
  26724. + depctl.b.setd1pid = 1;
  26725. + } else {
  26726. + depctl.b.setd0pid = 1;
  26727. + }
  26728. + } else {
  26729. + ep->next_frame += ep->bInterval;
  26730. +
  26731. + if (ep->next_frame & 0x1) {
  26732. + depctl.b.setd1pid = 1;
  26733. + } else {
  26734. + depctl.b.setd0pid = 1;
  26735. + }
  26736. + }
  26737. + depctl.b.epena = 1;
  26738. + depctl.b.cnak = 1;
  26739. +
  26740. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  26741. + depctl.d32 = DWC_READ_REG32(addr);
  26742. +
  26743. + if (ep->is_in && core_if->dma_enable == 0) {
  26744. + write_isoc_frame_data(core_if, ep);
  26745. + }
  26746. +
  26747. +}
  26748. +#endif /* DWC_EN_ISOC */
  26749. +
  26750. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  26751. +{
  26752. + int i;
  26753. + for (i = 0; i < size; i++) {
  26754. + p[i] = -1;
  26755. + }
  26756. +}
  26757. +
  26758. +static int dwc_otg_param_initialized(int32_t val)
  26759. +{
  26760. + return val != -1;
  26761. +}
  26762. +
  26763. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  26764. +{
  26765. + int i;
  26766. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  26767. + if (!core_if->core_params) {
  26768. + return -DWC_E_NO_MEMORY;
  26769. + }
  26770. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  26771. + sizeof(*core_if->core_params) /
  26772. + sizeof(int32_t));
  26773. + DWC_PRINTF("Setting default values for core params\n");
  26774. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  26775. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  26776. + dwc_otg_set_param_dma_desc_enable(core_if,
  26777. + dwc_param_dma_desc_enable_default);
  26778. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  26779. + dwc_otg_set_param_dma_burst_size(core_if,
  26780. + dwc_param_dma_burst_size_default);
  26781. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  26782. + dwc_param_host_support_fs_ls_low_power_default);
  26783. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  26784. + dwc_param_enable_dynamic_fifo_default);
  26785. + dwc_otg_set_param_data_fifo_size(core_if,
  26786. + dwc_param_data_fifo_size_default);
  26787. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  26788. + dwc_param_dev_rx_fifo_size_default);
  26789. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  26790. + dwc_param_dev_nperio_tx_fifo_size_default);
  26791. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  26792. + dwc_param_host_rx_fifo_size_default);
  26793. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  26794. + dwc_param_host_nperio_tx_fifo_size_default);
  26795. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  26796. + dwc_param_host_perio_tx_fifo_size_default);
  26797. + dwc_otg_set_param_max_transfer_size(core_if,
  26798. + dwc_param_max_transfer_size_default);
  26799. + dwc_otg_set_param_max_packet_count(core_if,
  26800. + dwc_param_max_packet_count_default);
  26801. + dwc_otg_set_param_host_channels(core_if,
  26802. + dwc_param_host_channels_default);
  26803. + dwc_otg_set_param_dev_endpoints(core_if,
  26804. + dwc_param_dev_endpoints_default);
  26805. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  26806. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  26807. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  26808. + dwc_param_host_ls_low_power_phy_clk_default);
  26809. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  26810. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  26811. + dwc_param_phy_ulpi_ext_vbus_default);
  26812. + dwc_otg_set_param_phy_utmi_width(core_if,
  26813. + dwc_param_phy_utmi_width_default);
  26814. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  26815. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  26816. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  26817. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  26818. + dwc_param_en_multiple_tx_fifo_default);
  26819. + for (i = 0; i < 15; i++) {
  26820. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  26821. + dwc_param_dev_perio_tx_fifo_size_default,
  26822. + i);
  26823. + }
  26824. +
  26825. + for (i = 0; i < 15; i++) {
  26826. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  26827. + dwc_param_dev_tx_fifo_size_default,
  26828. + i);
  26829. + }
  26830. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  26831. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  26832. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  26833. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  26834. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  26835. + dwc_otg_set_param_tx_thr_length(core_if,
  26836. + dwc_param_tx_thr_length_default);
  26837. + dwc_otg_set_param_rx_thr_length(core_if,
  26838. + dwc_param_rx_thr_length_default);
  26839. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  26840. + dwc_param_ahb_thr_ratio_default);
  26841. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  26842. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  26843. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  26844. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  26845. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  26846. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  26847. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  26848. + DWC_PRINTF("Finished setting default values for core params\n");
  26849. +
  26850. + return 0;
  26851. +}
  26852. +
  26853. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  26854. +{
  26855. + return core_if->dma_enable;
  26856. +}
  26857. +
  26858. +/* Checks if the parameter is outside of its valid range of values */
  26859. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  26860. + (((_param_) < (_low_)) || \
  26861. + ((_param_) > (_high_)))
  26862. +
  26863. +/* Parameter access functions */
  26864. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  26865. +{
  26866. + int valid;
  26867. + int retval = 0;
  26868. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  26869. + DWC_WARN("Wrong value for otg_cap parameter\n");
  26870. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  26871. + retval = -DWC_E_INVALID;
  26872. + goto out;
  26873. + }
  26874. +
  26875. + valid = 1;
  26876. + switch (val) {
  26877. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  26878. + if (core_if->hwcfg2.b.op_mode !=
  26879. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26880. + valid = 0;
  26881. + break;
  26882. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  26883. + if ((core_if->hwcfg2.b.op_mode !=
  26884. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26885. + && (core_if->hwcfg2.b.op_mode !=
  26886. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  26887. + && (core_if->hwcfg2.b.op_mode !=
  26888. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  26889. + && (core_if->hwcfg2.b.op_mode !=
  26890. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  26891. + valid = 0;
  26892. + }
  26893. + break;
  26894. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  26895. + /* always valid */
  26896. + break;
  26897. + }
  26898. + if (!valid) {
  26899. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  26900. + DWC_ERROR
  26901. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  26902. + val);
  26903. + }
  26904. + val =
  26905. + (((core_if->hwcfg2.b.op_mode ==
  26906. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26907. + || (core_if->hwcfg2.b.op_mode ==
  26908. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  26909. + || (core_if->hwcfg2.b.op_mode ==
  26910. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  26911. + || (core_if->hwcfg2.b.op_mode ==
  26912. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  26913. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  26914. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  26915. + retval = -DWC_E_INVALID;
  26916. + }
  26917. +
  26918. + core_if->core_params->otg_cap = val;
  26919. +out:
  26920. + return retval;
  26921. +}
  26922. +
  26923. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  26924. +{
  26925. + return core_if->core_params->otg_cap;
  26926. +}
  26927. +
  26928. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  26929. +{
  26930. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26931. + DWC_WARN("Wrong value for opt parameter\n");
  26932. + return -DWC_E_INVALID;
  26933. + }
  26934. + core_if->core_params->opt = val;
  26935. + return 0;
  26936. +}
  26937. +
  26938. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  26939. +{
  26940. + return core_if->core_params->opt;
  26941. +}
  26942. +
  26943. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  26944. +{
  26945. + int retval = 0;
  26946. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26947. + DWC_WARN("Wrong value for dma enable\n");
  26948. + return -DWC_E_INVALID;
  26949. + }
  26950. +
  26951. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  26952. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  26953. + DWC_ERROR
  26954. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  26955. + val);
  26956. + }
  26957. + val = 0;
  26958. + retval = -DWC_E_INVALID;
  26959. + }
  26960. +
  26961. + core_if->core_params->dma_enable = val;
  26962. + if (val == 0) {
  26963. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  26964. + }
  26965. + return retval;
  26966. +}
  26967. +
  26968. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  26969. +{
  26970. + return core_if->core_params->dma_enable;
  26971. +}
  26972. +
  26973. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  26974. +{
  26975. + int retval = 0;
  26976. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26977. + DWC_WARN("Wrong value for dma_enable\n");
  26978. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  26979. + return -DWC_E_INVALID;
  26980. + }
  26981. +
  26982. + if ((val == 1)
  26983. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  26984. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  26985. + if (dwc_otg_param_initialized
  26986. + (core_if->core_params->dma_desc_enable)) {
  26987. + DWC_ERROR
  26988. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  26989. + val);
  26990. + }
  26991. + val = 0;
  26992. + retval = -DWC_E_INVALID;
  26993. + }
  26994. + core_if->core_params->dma_desc_enable = val;
  26995. + return retval;
  26996. +}
  26997. +
  26998. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  26999. +{
  27000. + return core_if->core_params->dma_desc_enable;
  27001. +}
  27002. +
  27003. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  27004. + int32_t val)
  27005. +{
  27006. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27007. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  27008. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  27009. + return -DWC_E_INVALID;
  27010. + }
  27011. + core_if->core_params->host_support_fs_ls_low_power = val;
  27012. + return 0;
  27013. +}
  27014. +
  27015. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  27016. + core_if)
  27017. +{
  27018. + return core_if->core_params->host_support_fs_ls_low_power;
  27019. +}
  27020. +
  27021. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  27022. + int32_t val)
  27023. +{
  27024. + int retval = 0;
  27025. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27026. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  27027. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  27028. + return -DWC_E_INVALID;
  27029. + }
  27030. +
  27031. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  27032. + if (dwc_otg_param_initialized
  27033. + (core_if->core_params->enable_dynamic_fifo)) {
  27034. + DWC_ERROR
  27035. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  27036. + val);
  27037. + }
  27038. + val = 0;
  27039. + retval = -DWC_E_INVALID;
  27040. + }
  27041. + core_if->core_params->enable_dynamic_fifo = val;
  27042. + return retval;
  27043. +}
  27044. +
  27045. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  27046. +{
  27047. + return core_if->core_params->enable_dynamic_fifo;
  27048. +}
  27049. +
  27050. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  27051. +{
  27052. + int retval = 0;
  27053. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  27054. + DWC_WARN("Wrong value for data_fifo_size\n");
  27055. + DWC_WARN("data_fifo_size must be 32-32768\n");
  27056. + return -DWC_E_INVALID;
  27057. + }
  27058. +
  27059. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  27060. + if (dwc_otg_param_initialized
  27061. + (core_if->core_params->data_fifo_size)) {
  27062. + DWC_ERROR
  27063. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  27064. + val);
  27065. + }
  27066. + val = core_if->hwcfg3.b.dfifo_depth;
  27067. + retval = -DWC_E_INVALID;
  27068. + }
  27069. +
  27070. + core_if->core_params->data_fifo_size = val;
  27071. + return retval;
  27072. +}
  27073. +
  27074. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  27075. +{
  27076. + return core_if->core_params->data_fifo_size;
  27077. +}
  27078. +
  27079. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  27080. +{
  27081. + int retval = 0;
  27082. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27083. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  27084. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  27085. + return -DWC_E_INVALID;
  27086. + }
  27087. +
  27088. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  27089. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  27090. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  27091. + }
  27092. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  27093. + retval = -DWC_E_INVALID;
  27094. + }
  27095. +
  27096. + core_if->core_params->dev_rx_fifo_size = val;
  27097. + return retval;
  27098. +}
  27099. +
  27100. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  27101. +{
  27102. + return core_if->core_params->dev_rx_fifo_size;
  27103. +}
  27104. +
  27105. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27106. + int32_t val)
  27107. +{
  27108. + int retval = 0;
  27109. +
  27110. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27111. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  27112. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  27113. + return -DWC_E_INVALID;
  27114. + }
  27115. +
  27116. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  27117. + if (dwc_otg_param_initialized
  27118. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  27119. + DWC_ERROR
  27120. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  27121. + val);
  27122. + }
  27123. + val =
  27124. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  27125. + 16);
  27126. + retval = -DWC_E_INVALID;
  27127. + }
  27128. +
  27129. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  27130. + return retval;
  27131. +}
  27132. +
  27133. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27134. +{
  27135. + return core_if->core_params->dev_nperio_tx_fifo_size;
  27136. +}
  27137. +
  27138. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  27139. + int32_t val)
  27140. +{
  27141. + int retval = 0;
  27142. +
  27143. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27144. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  27145. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  27146. + return -DWC_E_INVALID;
  27147. + }
  27148. +
  27149. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  27150. + if (dwc_otg_param_initialized
  27151. + (core_if->core_params->host_rx_fifo_size)) {
  27152. + DWC_ERROR
  27153. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  27154. + val);
  27155. + }
  27156. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  27157. + retval = -DWC_E_INVALID;
  27158. + }
  27159. +
  27160. + core_if->core_params->host_rx_fifo_size = val;
  27161. + return retval;
  27162. +
  27163. +}
  27164. +
  27165. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  27166. +{
  27167. + return core_if->core_params->host_rx_fifo_size;
  27168. +}
  27169. +
  27170. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27171. + int32_t val)
  27172. +{
  27173. + int retval = 0;
  27174. +
  27175. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27176. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  27177. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  27178. + return -DWC_E_INVALID;
  27179. + }
  27180. +
  27181. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  27182. + if (dwc_otg_param_initialized
  27183. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  27184. + DWC_ERROR
  27185. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  27186. + val);
  27187. + }
  27188. + val =
  27189. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  27190. + 16);
  27191. + retval = -DWC_E_INVALID;
  27192. + }
  27193. +
  27194. + core_if->core_params->host_nperio_tx_fifo_size = val;
  27195. + return retval;
  27196. +}
  27197. +
  27198. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27199. +{
  27200. + return core_if->core_params->host_nperio_tx_fifo_size;
  27201. +}
  27202. +
  27203. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27204. + int32_t val)
  27205. +{
  27206. + int retval = 0;
  27207. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27208. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  27209. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  27210. + return -DWC_E_INVALID;
  27211. + }
  27212. +
  27213. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  27214. + if (dwc_otg_param_initialized
  27215. + (core_if->core_params->host_perio_tx_fifo_size)) {
  27216. + DWC_ERROR
  27217. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  27218. + val);
  27219. + }
  27220. + val = (core_if->hptxfsiz.d32) >> 16;
  27221. + retval = -DWC_E_INVALID;
  27222. + }
  27223. +
  27224. + core_if->core_params->host_perio_tx_fifo_size = val;
  27225. + return retval;
  27226. +}
  27227. +
  27228. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27229. +{
  27230. + return core_if->core_params->host_perio_tx_fifo_size;
  27231. +}
  27232. +
  27233. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  27234. + int32_t val)
  27235. +{
  27236. + int retval = 0;
  27237. +
  27238. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  27239. + DWC_WARN("Wrong value for max_transfer_size\n");
  27240. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  27241. + return -DWC_E_INVALID;
  27242. + }
  27243. +
  27244. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  27245. + if (dwc_otg_param_initialized
  27246. + (core_if->core_params->max_transfer_size)) {
  27247. + DWC_ERROR
  27248. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  27249. + val);
  27250. + }
  27251. + val =
  27252. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  27253. + 1);
  27254. + retval = -DWC_E_INVALID;
  27255. + }
  27256. +
  27257. + core_if->core_params->max_transfer_size = val;
  27258. + return retval;
  27259. +}
  27260. +
  27261. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  27262. +{
  27263. + return core_if->core_params->max_transfer_size;
  27264. +}
  27265. +
  27266. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  27267. +{
  27268. + int retval = 0;
  27269. +
  27270. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  27271. + DWC_WARN("Wrong value for max_packet_count\n");
  27272. + DWC_WARN("max_packet_count must be 15-511\n");
  27273. + return -DWC_E_INVALID;
  27274. + }
  27275. +
  27276. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  27277. + if (dwc_otg_param_initialized
  27278. + (core_if->core_params->max_packet_count)) {
  27279. + DWC_ERROR
  27280. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  27281. + val);
  27282. + }
  27283. + val =
  27284. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  27285. + retval = -DWC_E_INVALID;
  27286. + }
  27287. +
  27288. + core_if->core_params->max_packet_count = val;
  27289. + return retval;
  27290. +}
  27291. +
  27292. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  27293. +{
  27294. + return core_if->core_params->max_packet_count;
  27295. +}
  27296. +
  27297. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  27298. +{
  27299. + int retval = 0;
  27300. +
  27301. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  27302. + DWC_WARN("Wrong value for host_channels\n");
  27303. + DWC_WARN("host_channels must be 1-16\n");
  27304. + return -DWC_E_INVALID;
  27305. + }
  27306. +
  27307. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  27308. + if (dwc_otg_param_initialized
  27309. + (core_if->core_params->host_channels)) {
  27310. + DWC_ERROR
  27311. + ("%d invalid for host_channels. Check HW configurations.\n",
  27312. + val);
  27313. + }
  27314. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  27315. + retval = -DWC_E_INVALID;
  27316. + }
  27317. +
  27318. + core_if->core_params->host_channels = val;
  27319. + return retval;
  27320. +}
  27321. +
  27322. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  27323. +{
  27324. + return core_if->core_params->host_channels;
  27325. +}
  27326. +
  27327. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  27328. +{
  27329. + int retval = 0;
  27330. +
  27331. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  27332. + DWC_WARN("Wrong value for dev_endpoints\n");
  27333. + DWC_WARN("dev_endpoints must be 1-15\n");
  27334. + return -DWC_E_INVALID;
  27335. + }
  27336. +
  27337. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  27338. + if (dwc_otg_param_initialized
  27339. + (core_if->core_params->dev_endpoints)) {
  27340. + DWC_ERROR
  27341. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  27342. + val);
  27343. + }
  27344. + val = core_if->hwcfg2.b.num_dev_ep;
  27345. + retval = -DWC_E_INVALID;
  27346. + }
  27347. +
  27348. + core_if->core_params->dev_endpoints = val;
  27349. + return retval;
  27350. +}
  27351. +
  27352. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  27353. +{
  27354. + return core_if->core_params->dev_endpoints;
  27355. +}
  27356. +
  27357. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  27358. +{
  27359. + int retval = 0;
  27360. + int valid = 0;
  27361. +
  27362. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  27363. + DWC_WARN("Wrong value for phy_type\n");
  27364. + DWC_WARN("phy_type must be 0,1 or 2\n");
  27365. + return -DWC_E_INVALID;
  27366. + }
  27367. +#ifndef NO_FS_PHY_HW_CHECKS
  27368. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  27369. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  27370. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  27371. + valid = 1;
  27372. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  27373. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  27374. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  27375. + valid = 1;
  27376. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  27377. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  27378. + valid = 1;
  27379. + }
  27380. + if (!valid) {
  27381. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  27382. + DWC_ERROR
  27383. + ("%d invalid for phy_type. Check HW configurations.\n",
  27384. + val);
  27385. + }
  27386. + if (core_if->hwcfg2.b.hs_phy_type) {
  27387. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  27388. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  27389. + val = DWC_PHY_TYPE_PARAM_UTMI;
  27390. + } else {
  27391. + val = DWC_PHY_TYPE_PARAM_ULPI;
  27392. + }
  27393. + }
  27394. + retval = -DWC_E_INVALID;
  27395. + }
  27396. +#endif
  27397. + core_if->core_params->phy_type = val;
  27398. + return retval;
  27399. +}
  27400. +
  27401. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  27402. +{
  27403. + return core_if->core_params->phy_type;
  27404. +}
  27405. +
  27406. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  27407. +{
  27408. + int retval = 0;
  27409. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27410. + DWC_WARN("Wrong value for speed parameter\n");
  27411. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  27412. + return -DWC_E_INVALID;
  27413. + }
  27414. + if ((val == 0)
  27415. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  27416. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  27417. + DWC_ERROR
  27418. + ("%d invalid for speed paremter. Check HW configuration.\n",
  27419. + val);
  27420. + }
  27421. + val =
  27422. + (dwc_otg_get_param_phy_type(core_if) ==
  27423. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  27424. + retval = -DWC_E_INVALID;
  27425. + }
  27426. + core_if->core_params->speed = val;
  27427. + return retval;
  27428. +}
  27429. +
  27430. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  27431. +{
  27432. + return core_if->core_params->speed;
  27433. +}
  27434. +
  27435. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  27436. + int32_t val)
  27437. +{
  27438. + int retval = 0;
  27439. +
  27440. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27441. + DWC_WARN
  27442. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  27443. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  27444. + return -DWC_E_INVALID;
  27445. + }
  27446. +
  27447. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  27448. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  27449. + if (dwc_otg_param_initialized
  27450. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  27451. + DWC_ERROR
  27452. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  27453. + val);
  27454. + }
  27455. + val =
  27456. + (dwc_otg_get_param_phy_type(core_if) ==
  27457. + DWC_PHY_TYPE_PARAM_FS) ?
  27458. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  27459. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  27460. + retval = -DWC_E_INVALID;
  27461. + }
  27462. +
  27463. + core_if->core_params->host_ls_low_power_phy_clk = val;
  27464. + return retval;
  27465. +}
  27466. +
  27467. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  27468. +{
  27469. + return core_if->core_params->host_ls_low_power_phy_clk;
  27470. +}
  27471. +
  27472. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  27473. +{
  27474. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27475. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  27476. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  27477. + return -DWC_E_INVALID;
  27478. + }
  27479. +
  27480. + core_if->core_params->phy_ulpi_ddr = val;
  27481. + return 0;
  27482. +}
  27483. +
  27484. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  27485. +{
  27486. + return core_if->core_params->phy_ulpi_ddr;
  27487. +}
  27488. +
  27489. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  27490. + int32_t val)
  27491. +{
  27492. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27493. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  27494. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  27495. + return -DWC_E_INVALID;
  27496. + }
  27497. +
  27498. + core_if->core_params->phy_ulpi_ext_vbus = val;
  27499. + return 0;
  27500. +}
  27501. +
  27502. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  27503. +{
  27504. + return core_if->core_params->phy_ulpi_ext_vbus;
  27505. +}
  27506. +
  27507. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  27508. +{
  27509. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  27510. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  27511. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  27512. + return -DWC_E_INVALID;
  27513. + }
  27514. +
  27515. + core_if->core_params->phy_utmi_width = val;
  27516. + return 0;
  27517. +}
  27518. +
  27519. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  27520. +{
  27521. + return core_if->core_params->phy_utmi_width;
  27522. +}
  27523. +
  27524. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  27525. +{
  27526. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27527. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  27528. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  27529. + return -DWC_E_INVALID;
  27530. + }
  27531. +
  27532. + core_if->core_params->ulpi_fs_ls = val;
  27533. + return 0;
  27534. +}
  27535. +
  27536. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  27537. +{
  27538. + return core_if->core_params->ulpi_fs_ls;
  27539. +}
  27540. +
  27541. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  27542. +{
  27543. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27544. + DWC_WARN("Wrong valaue for ts_dline\n");
  27545. + DWC_WARN("ts_dline must be 0 or 1\n");
  27546. + return -DWC_E_INVALID;
  27547. + }
  27548. +
  27549. + core_if->core_params->ts_dline = val;
  27550. + return 0;
  27551. +}
  27552. +
  27553. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  27554. +{
  27555. + return core_if->core_params->ts_dline;
  27556. +}
  27557. +
  27558. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27559. +{
  27560. + int retval = 0;
  27561. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27562. + DWC_WARN("Wrong valaue for i2c_enable\n");
  27563. + DWC_WARN("i2c_enable must be 0 or 1\n");
  27564. + return -DWC_E_INVALID;
  27565. + }
  27566. +#ifndef NO_FS_PHY_HW_CHECK
  27567. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  27568. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  27569. + DWC_ERROR
  27570. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  27571. + val);
  27572. + }
  27573. + val = 0;
  27574. + retval = -DWC_E_INVALID;
  27575. + }
  27576. +#endif
  27577. +
  27578. + core_if->core_params->i2c_enable = val;
  27579. + return retval;
  27580. +}
  27581. +
  27582. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  27583. +{
  27584. + return core_if->core_params->i2c_enable;
  27585. +}
  27586. +
  27587. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27588. + int32_t val, int fifo_num)
  27589. +{
  27590. + int retval = 0;
  27591. +
  27592. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  27593. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  27594. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  27595. + return -DWC_E_INVALID;
  27596. + }
  27597. +
  27598. + if (val >
  27599. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  27600. + if (dwc_otg_param_initialized
  27601. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  27602. + DWC_ERROR
  27603. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  27604. + val, fifo_num);
  27605. + }
  27606. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  27607. + retval = -DWC_E_INVALID;
  27608. + }
  27609. +
  27610. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  27611. + return retval;
  27612. +}
  27613. +
  27614. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27615. + int fifo_num)
  27616. +{
  27617. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  27618. +}
  27619. +
  27620. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  27621. + int32_t val)
  27622. +{
  27623. + int retval = 0;
  27624. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27625. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  27626. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  27627. + return -DWC_E_INVALID;
  27628. + }
  27629. +
  27630. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  27631. + if (dwc_otg_param_initialized
  27632. + (core_if->core_params->en_multiple_tx_fifo)) {
  27633. + DWC_ERROR
  27634. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  27635. + val);
  27636. + }
  27637. + val = 0;
  27638. + retval = -DWC_E_INVALID;
  27639. + }
  27640. +
  27641. + core_if->core_params->en_multiple_tx_fifo = val;
  27642. + return retval;
  27643. +}
  27644. +
  27645. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  27646. +{
  27647. + return core_if->core_params->en_multiple_tx_fifo;
  27648. +}
  27649. +
  27650. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  27651. + int fifo_num)
  27652. +{
  27653. + int retval = 0;
  27654. +
  27655. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  27656. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  27657. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  27658. + return -DWC_E_INVALID;
  27659. + }
  27660. +
  27661. + if (val >
  27662. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  27663. + if (dwc_otg_param_initialized
  27664. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  27665. + DWC_ERROR
  27666. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  27667. + val, fifo_num);
  27668. + }
  27669. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  27670. + retval = -DWC_E_INVALID;
  27671. + }
  27672. +
  27673. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  27674. + return retval;
  27675. +}
  27676. +
  27677. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27678. + int fifo_num)
  27679. +{
  27680. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  27681. +}
  27682. +
  27683. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  27684. +{
  27685. + int retval = 0;
  27686. +
  27687. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  27688. + DWC_WARN("Wrong value for thr_ctl\n");
  27689. + DWC_WARN("thr_ctl must be 0-7\n");
  27690. + return -DWC_E_INVALID;
  27691. + }
  27692. +
  27693. + if ((val != 0) &&
  27694. + (!dwc_otg_get_param_dma_enable(core_if) ||
  27695. + !core_if->hwcfg4.b.ded_fifo_en)) {
  27696. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  27697. + DWC_ERROR
  27698. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  27699. + val);
  27700. + }
  27701. + val = 0;
  27702. + retval = -DWC_E_INVALID;
  27703. + }
  27704. +
  27705. + core_if->core_params->thr_ctl = val;
  27706. + return retval;
  27707. +}
  27708. +
  27709. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  27710. +{
  27711. + return core_if->core_params->thr_ctl;
  27712. +}
  27713. +
  27714. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27715. +{
  27716. + int retval = 0;
  27717. +
  27718. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27719. + DWC_WARN("Wrong value for lpm_enable\n");
  27720. + DWC_WARN("lpm_enable must be 0 or 1\n");
  27721. + return -DWC_E_INVALID;
  27722. + }
  27723. +
  27724. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  27725. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  27726. + DWC_ERROR
  27727. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  27728. + val);
  27729. + }
  27730. + val = 0;
  27731. + retval = -DWC_E_INVALID;
  27732. + }
  27733. +
  27734. + core_if->core_params->lpm_enable = val;
  27735. + return retval;
  27736. +}
  27737. +
  27738. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  27739. +{
  27740. + return core_if->core_params->lpm_enable;
  27741. +}
  27742. +
  27743. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  27744. +{
  27745. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  27746. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  27747. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  27748. + return -DWC_E_INVALID;
  27749. + }
  27750. +
  27751. + core_if->core_params->tx_thr_length = val;
  27752. + return 0;
  27753. +}
  27754. +
  27755. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  27756. +{
  27757. + return core_if->core_params->tx_thr_length;
  27758. +}
  27759. +
  27760. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  27761. +{
  27762. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  27763. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  27764. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  27765. + return -DWC_E_INVALID;
  27766. + }
  27767. +
  27768. + core_if->core_params->rx_thr_length = val;
  27769. + return 0;
  27770. +}
  27771. +
  27772. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  27773. +{
  27774. + return core_if->core_params->rx_thr_length;
  27775. +}
  27776. +
  27777. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  27778. +{
  27779. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  27780. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  27781. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  27782. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  27783. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  27784. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  27785. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  27786. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  27787. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  27788. + return -DWC_E_INVALID;
  27789. + }
  27790. + core_if->core_params->dma_burst_size = val;
  27791. + return 0;
  27792. +}
  27793. +
  27794. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  27795. +{
  27796. + return core_if->core_params->dma_burst_size;
  27797. +}
  27798. +
  27799. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27800. +{
  27801. + int retval = 0;
  27802. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27803. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  27804. + return -DWC_E_INVALID;
  27805. + }
  27806. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  27807. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  27808. + DWC_ERROR
  27809. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  27810. + val);
  27811. + }
  27812. + retval = -DWC_E_INVALID;
  27813. + val = 0;
  27814. + }
  27815. + core_if->core_params->pti_enable = val;
  27816. + return retval;
  27817. +}
  27818. +
  27819. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  27820. +{
  27821. + return core_if->core_params->pti_enable;
  27822. +}
  27823. +
  27824. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27825. +{
  27826. + int retval = 0;
  27827. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27828. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  27829. + return -DWC_E_INVALID;
  27830. + }
  27831. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  27832. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  27833. + DWC_ERROR
  27834. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  27835. + val);
  27836. + }
  27837. + retval = -DWC_E_INVALID;
  27838. + val = 0;
  27839. + }
  27840. + core_if->core_params->mpi_enable = val;
  27841. + return retval;
  27842. +}
  27843. +
  27844. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  27845. +{
  27846. + return core_if->core_params->mpi_enable;
  27847. +}
  27848. +
  27849. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27850. +{
  27851. + int retval = 0;
  27852. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27853. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  27854. + return -DWC_E_INVALID;
  27855. + }
  27856. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  27857. + if (dwc_otg_param_initialized
  27858. + (core_if->core_params->adp_supp_enable)) {
  27859. + DWC_ERROR
  27860. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  27861. + val);
  27862. + }
  27863. + retval = -DWC_E_INVALID;
  27864. + val = 0;
  27865. + }
  27866. + core_if->core_params->adp_supp_enable = val;
  27867. + /*Set OTG version 2.0 in case of enabling ADP*/
  27868. + if (val)
  27869. + dwc_otg_set_param_otg_ver(core_if, 1);
  27870. +
  27871. + return retval;
  27872. +}
  27873. +
  27874. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  27875. +{
  27876. + return core_if->core_params->adp_supp_enable;
  27877. +}
  27878. +
  27879. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  27880. +{
  27881. + int retval = 0;
  27882. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27883. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  27884. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  27885. + return -DWC_E_INVALID;
  27886. + }
  27887. +
  27888. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  27889. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  27890. + DWC_ERROR
  27891. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  27892. + val);
  27893. + }
  27894. + retval = -DWC_E_INVALID;
  27895. + val = 0;
  27896. + }
  27897. + core_if->core_params->ic_usb_cap = val;
  27898. + return retval;
  27899. +}
  27900. +
  27901. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  27902. +{
  27903. + return core_if->core_params->ic_usb_cap;
  27904. +}
  27905. +
  27906. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  27907. +{
  27908. + int retval = 0;
  27909. + int valid = 1;
  27910. +
  27911. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  27912. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  27913. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  27914. + return -DWC_E_INVALID;
  27915. + }
  27916. +
  27917. + if (val
  27918. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  27919. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  27920. + valid = 0;
  27921. + } else if (val
  27922. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  27923. + 4)) {
  27924. + valid = 0;
  27925. + }
  27926. + if (valid == 0) {
  27927. + if (dwc_otg_param_initialized
  27928. + (core_if->core_params->ahb_thr_ratio)) {
  27929. + DWC_ERROR
  27930. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  27931. + val);
  27932. + }
  27933. + retval = -DWC_E_INVALID;
  27934. + val = 0;
  27935. + }
  27936. +
  27937. + core_if->core_params->ahb_thr_ratio = val;
  27938. + return retval;
  27939. +}
  27940. +
  27941. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  27942. +{
  27943. + return core_if->core_params->ahb_thr_ratio;
  27944. +}
  27945. +
  27946. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  27947. +{
  27948. + int retval = 0;
  27949. + int valid = 1;
  27950. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  27951. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  27952. +
  27953. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  27954. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  27955. + DWC_WARN("power_down must be 0 - 2\n");
  27956. + return -DWC_E_INVALID;
  27957. + }
  27958. +
  27959. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  27960. + valid = 0;
  27961. + }
  27962. + if ((val == 3)
  27963. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  27964. + || (hwcfg4.b.xhiber == 0))) {
  27965. + valid = 0;
  27966. + }
  27967. + if (valid == 0) {
  27968. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  27969. + DWC_ERROR
  27970. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  27971. + val);
  27972. + }
  27973. + retval = -DWC_E_INVALID;
  27974. + val = 0;
  27975. + }
  27976. + core_if->core_params->power_down = val;
  27977. + return retval;
  27978. +}
  27979. +
  27980. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  27981. +{
  27982. + return core_if->core_params->power_down;
  27983. +}
  27984. +
  27985. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  27986. +{
  27987. + int retval = 0;
  27988. + int valid = 1;
  27989. +
  27990. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27991. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  27992. + DWC_WARN("reload_ctl must be 0 or 1\n");
  27993. + return -DWC_E_INVALID;
  27994. + }
  27995. +
  27996. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  27997. + valid = 0;
  27998. + }
  27999. + if (valid == 0) {
  28000. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  28001. + DWC_ERROR("%d invalid for parameter reload_ctl."
  28002. + "Check HW configuration.\n", val);
  28003. + }
  28004. + retval = -DWC_E_INVALID;
  28005. + val = 0;
  28006. + }
  28007. + core_if->core_params->reload_ctl = val;
  28008. + return retval;
  28009. +}
  28010. +
  28011. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  28012. +{
  28013. + return core_if->core_params->reload_ctl;
  28014. +}
  28015. +
  28016. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  28017. +{
  28018. + int retval = 0;
  28019. + int valid = 1;
  28020. +
  28021. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  28022. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  28023. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  28024. + return -DWC_E_INVALID;
  28025. + }
  28026. +
  28027. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  28028. + !(core_if->core_params->dma_desc_enable))) {
  28029. + valid = 0;
  28030. + }
  28031. + if (valid == 0) {
  28032. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  28033. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  28034. + "Check HW configuration.\n", val);
  28035. + }
  28036. + retval = -DWC_E_INVALID;
  28037. + val = 0;
  28038. + }
  28039. + core_if->core_params->dev_out_nak = val;
  28040. + return retval;
  28041. +}
  28042. +
  28043. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  28044. +{
  28045. + return core_if->core_params->dev_out_nak;
  28046. +}
  28047. +
  28048. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  28049. +{
  28050. + int retval = 0;
  28051. + int valid = 1;
  28052. +
  28053. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  28054. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  28055. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  28056. + return -DWC_E_INVALID;
  28057. + }
  28058. +
  28059. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  28060. + !(core_if->core_params->dma_desc_enable))) {
  28061. + valid = 0;
  28062. + }
  28063. + if (valid == 0) {
  28064. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  28065. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  28066. + "Check HW configuration.\n", val);
  28067. + }
  28068. + retval = -DWC_E_INVALID;
  28069. + val = 0;
  28070. + }
  28071. + core_if->core_params->cont_on_bna = val;
  28072. + return retval;
  28073. +}
  28074. +
  28075. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  28076. +{
  28077. + return core_if->core_params->cont_on_bna;
  28078. +}
  28079. +
  28080. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  28081. +{
  28082. + int retval = 0;
  28083. + int valid = 1;
  28084. +
  28085. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  28086. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  28087. + DWC_WARN("ahb_single must be 0 or 1\n");
  28088. + return -DWC_E_INVALID;
  28089. + }
  28090. +
  28091. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  28092. + valid = 0;
  28093. + }
  28094. + if (valid == 0) {
  28095. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  28096. + DWC_ERROR("%d invalid for parameter ahb_single."
  28097. + "Check HW configuration.\n", val);
  28098. + }
  28099. + retval = -DWC_E_INVALID;
  28100. + val = 0;
  28101. + }
  28102. + core_if->core_params->ahb_single = val;
  28103. + return retval;
  28104. +}
  28105. +
  28106. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  28107. +{
  28108. + return core_if->core_params->ahb_single;
  28109. +}
  28110. +
  28111. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  28112. +{
  28113. + int retval = 0;
  28114. +
  28115. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  28116. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  28117. + DWC_WARN
  28118. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  28119. + return -DWC_E_INVALID;
  28120. + }
  28121. +
  28122. + core_if->core_params->otg_ver = val;
  28123. + return retval;
  28124. +}
  28125. +
  28126. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  28127. +{
  28128. + return core_if->core_params->otg_ver;
  28129. +}
  28130. +
  28131. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  28132. +{
  28133. + gotgctl_data_t otgctl;
  28134. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28135. + return otgctl.b.hstnegscs;
  28136. +}
  28137. +
  28138. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  28139. +{
  28140. + gotgctl_data_t otgctl;
  28141. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28142. + return otgctl.b.sesreqscs;
  28143. +}
  28144. +
  28145. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  28146. +{
  28147. + if(core_if->otg_ver == 0) {
  28148. + gotgctl_data_t otgctl;
  28149. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28150. + otgctl.b.hnpreq = val;
  28151. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  28152. + } else {
  28153. + core_if->otg_sts = val;
  28154. + }
  28155. +}
  28156. +
  28157. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  28158. +{
  28159. + return core_if->snpsid;
  28160. +}
  28161. +
  28162. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  28163. +{
  28164. + gintsts_data_t gintsts;
  28165. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  28166. + return gintsts.b.curmode;
  28167. +}
  28168. +
  28169. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  28170. +{
  28171. + gusbcfg_data_t usbcfg;
  28172. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28173. + return usbcfg.b.hnpcap;
  28174. +}
  28175. +
  28176. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  28177. +{
  28178. + gusbcfg_data_t usbcfg;
  28179. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28180. + usbcfg.b.hnpcap = val;
  28181. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  28182. +}
  28183. +
  28184. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  28185. +{
  28186. + gusbcfg_data_t usbcfg;
  28187. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28188. + return usbcfg.b.srpcap;
  28189. +}
  28190. +
  28191. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  28192. +{
  28193. + gusbcfg_data_t usbcfg;
  28194. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28195. + usbcfg.b.srpcap = val;
  28196. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  28197. +}
  28198. +
  28199. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  28200. +{
  28201. + dcfg_data_t dcfg;
  28202. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  28203. +
  28204. + dcfg.d32 = -1; //GRAYG
  28205. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  28206. + if (NULL == core_if)
  28207. + DWC_ERROR("reg request with NULL core_if\n");
  28208. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  28209. + core_if, core_if->dev_if);
  28210. + if (NULL == core_if->dev_if)
  28211. + DWC_ERROR("reg request with NULL dev_if\n");
  28212. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  28213. + "dev_global_regs(%p)\n", __func__,
  28214. + core_if, core_if->dev_if,
  28215. + core_if->dev_if->dev_global_regs);
  28216. + if (NULL == core_if->dev_if->dev_global_regs)
  28217. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  28218. + else {
  28219. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  28220. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  28221. + core_if, core_if->dev_if,
  28222. + core_if->dev_if->dev_global_regs,
  28223. + &core_if->dev_if->dev_global_regs->dcfg);
  28224. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  28225. + }
  28226. + return dcfg.b.devspd;
  28227. +}
  28228. +
  28229. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  28230. +{
  28231. + dcfg_data_t dcfg;
  28232. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  28233. + dcfg.b.devspd = val;
  28234. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  28235. +}
  28236. +
  28237. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  28238. +{
  28239. + hprt0_data_t hprt0;
  28240. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28241. + return hprt0.b.prtconnsts;
  28242. +}
  28243. +
  28244. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  28245. +{
  28246. + dsts_data_t dsts;
  28247. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  28248. + return dsts.b.enumspd;
  28249. +}
  28250. +
  28251. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  28252. +{
  28253. + hprt0_data_t hprt0;
  28254. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28255. + return hprt0.b.prtpwr;
  28256. +
  28257. +}
  28258. +
  28259. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  28260. +{
  28261. + return core_if->hibernation_suspend;
  28262. +}
  28263. +
  28264. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  28265. +{
  28266. + hprt0_data_t hprt0;
  28267. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28268. + hprt0.b.prtpwr = val;
  28269. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28270. +}
  28271. +
  28272. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  28273. +{
  28274. + hprt0_data_t hprt0;
  28275. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28276. + return hprt0.b.prtsusp;
  28277. +
  28278. +}
  28279. +
  28280. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  28281. +{
  28282. + hprt0_data_t hprt0;
  28283. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28284. + hprt0.b.prtsusp = val;
  28285. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28286. +}
  28287. +
  28288. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  28289. +{
  28290. + hfir_data_t hfir;
  28291. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  28292. + return hfir.b.frint;
  28293. +
  28294. +}
  28295. +
  28296. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  28297. +{
  28298. + hfir_data_t hfir;
  28299. + uint32_t fram_int;
  28300. + fram_int = calc_frame_interval(core_if);
  28301. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  28302. + if (!core_if->core_params->reload_ctl) {
  28303. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  28304. + "not set to 1.\nShould load driver with reload_ctl=1"
  28305. + " module parameter\n");
  28306. + return;
  28307. + }
  28308. + switch (fram_int) {
  28309. + case 3750:
  28310. + if ((val < 3350) || (val > 4150)) {
  28311. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  28312. + "clock freq should be from 3350 to 4150\n");
  28313. + return;
  28314. + }
  28315. + break;
  28316. + case 30000:
  28317. + if ((val < 26820) || (val > 33180)) {
  28318. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  28319. + "clock freq should be from 26820 to 33180\n");
  28320. + return;
  28321. + }
  28322. + break;
  28323. + case 6000:
  28324. + if ((val < 5360) || (val > 6640)) {
  28325. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  28326. + "clock freq should be from 5360 to 6640\n");
  28327. + return;
  28328. + }
  28329. + break;
  28330. + case 48000:
  28331. + if ((val < 42912) || (val > 53088)) {
  28332. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  28333. + "clock freq should be from 42912 to 53088\n");
  28334. + return;
  28335. + }
  28336. + break;
  28337. + case 7500:
  28338. + if ((val < 6700) || (val > 8300)) {
  28339. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  28340. + "clock freq should be from 6700 to 8300\n");
  28341. + return;
  28342. + }
  28343. + break;
  28344. + case 60000:
  28345. + if ((val < 53640) || (val > 65536)) {
  28346. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  28347. + "clock freq should be from 53640 to 65536\n");
  28348. + return;
  28349. + }
  28350. + break;
  28351. + default:
  28352. + DWC_WARN("Unknown frame interval\n");
  28353. + return;
  28354. + break;
  28355. +
  28356. + }
  28357. + hfir.b.frint = val;
  28358. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  28359. +}
  28360. +
  28361. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  28362. +{
  28363. + hcfg_data_t hcfg;
  28364. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  28365. + return hcfg.b.modechtimen;
  28366. +
  28367. +}
  28368. +
  28369. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  28370. +{
  28371. + hcfg_data_t hcfg;
  28372. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  28373. + hcfg.b.modechtimen = val;
  28374. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  28375. +}
  28376. +
  28377. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  28378. +{
  28379. + hprt0_data_t hprt0;
  28380. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28381. + hprt0.b.prtres = val;
  28382. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28383. +}
  28384. +
  28385. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  28386. +{
  28387. + dctl_data_t dctl;
  28388. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  28389. + return dctl.b.rmtwkupsig;
  28390. +}
  28391. +
  28392. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  28393. +{
  28394. + glpmcfg_data_t lpmcfg;
  28395. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28396. +
  28397. + DWC_ASSERT(!
  28398. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  28399. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  28400. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  28401. +
  28402. + return lpmcfg.b.prt_sleep_sts;
  28403. +}
  28404. +
  28405. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  28406. +{
  28407. + glpmcfg_data_t lpmcfg;
  28408. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28409. + return lpmcfg.b.rem_wkup_en;
  28410. +}
  28411. +
  28412. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  28413. +{
  28414. + glpmcfg_data_t lpmcfg;
  28415. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28416. + return lpmcfg.b.appl_resp;
  28417. +}
  28418. +
  28419. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  28420. +{
  28421. + glpmcfg_data_t lpmcfg;
  28422. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28423. + lpmcfg.b.appl_resp = val;
  28424. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28425. +}
  28426. +
  28427. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  28428. +{
  28429. + glpmcfg_data_t lpmcfg;
  28430. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28431. + return lpmcfg.b.hsic_connect;
  28432. +}
  28433. +
  28434. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  28435. +{
  28436. + glpmcfg_data_t lpmcfg;
  28437. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28438. + lpmcfg.b.hsic_connect = val;
  28439. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28440. +}
  28441. +
  28442. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  28443. +{
  28444. + glpmcfg_data_t lpmcfg;
  28445. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28446. + return lpmcfg.b.inv_sel_hsic;
  28447. +
  28448. +}
  28449. +
  28450. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  28451. +{
  28452. + glpmcfg_data_t lpmcfg;
  28453. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28454. + lpmcfg.b.inv_sel_hsic = val;
  28455. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28456. +}
  28457. +
  28458. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  28459. +{
  28460. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28461. +}
  28462. +
  28463. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  28464. +{
  28465. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  28466. +}
  28467. +
  28468. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  28469. +{
  28470. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28471. +}
  28472. +
  28473. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  28474. +{
  28475. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  28476. +}
  28477. +
  28478. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  28479. +{
  28480. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  28481. +}
  28482. +
  28483. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  28484. +{
  28485. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  28486. +}
  28487. +
  28488. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  28489. +{
  28490. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  28491. +}
  28492. +
  28493. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  28494. +{
  28495. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  28496. +}
  28497. +
  28498. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  28499. +{
  28500. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  28501. +}
  28502. +
  28503. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  28504. +{
  28505. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  28506. +}
  28507. +
  28508. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  28509. +{
  28510. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  28511. +}
  28512. +
  28513. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  28514. +{
  28515. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  28516. +}
  28517. +
  28518. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  28519. +{
  28520. + return DWC_READ_REG32(core_if->host_if->hprt0);
  28521. +
  28522. +}
  28523. +
  28524. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  28525. +{
  28526. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  28527. +}
  28528. +
  28529. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  28530. +{
  28531. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  28532. +}
  28533. +
  28534. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  28535. +{
  28536. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  28537. +}
  28538. +
  28539. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  28540. +{
  28541. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  28542. +}
  28543. +
  28544. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  28545. +{
  28546. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  28547. +}
  28548. +
  28549. +/**
  28550. + * Start the SRP timer to detect when the SRP does not complete within
  28551. + * 6 seconds.
  28552. + *
  28553. + * @param core_if the pointer to core_if strucure.
  28554. + */
  28555. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  28556. +{
  28557. + core_if->srp_timer_started = 1;
  28558. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  28559. +}
  28560. +
  28561. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  28562. +{
  28563. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  28564. + gotgctl_data_t mem;
  28565. + gotgctl_data_t val;
  28566. +
  28567. + val.d32 = DWC_READ_REG32(addr);
  28568. + if (val.b.sesreq) {
  28569. + DWC_ERROR("Session Request Already active!\n");
  28570. + return;
  28571. + }
  28572. +
  28573. + DWC_INFO("Session Request Initated\n"); //NOTICE
  28574. + mem.d32 = DWC_READ_REG32(addr);
  28575. + mem.b.sesreq = 1;
  28576. + DWC_WRITE_REG32(addr, mem.d32);
  28577. +
  28578. + /* Start the SRP timer */
  28579. + dwc_otg_pcd_start_srp_timer(core_if);
  28580. + return;
  28581. +}
  28582. --- /dev/null
  28583. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  28584. @@ -0,0 +1,1464 @@
  28585. +/* ==========================================================================
  28586. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  28587. + * $Revision: #123 $
  28588. + * $Date: 2012/08/10 $
  28589. + * $Change: 2047372 $
  28590. + *
  28591. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  28592. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  28593. + * otherwise expressly agreed to in writing between Synopsys and you.
  28594. + *
  28595. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  28596. + * any End User Software License Agreement or Agreement for Licensed Product
  28597. + * with Synopsys or any supplement thereto. You are permitted to use and
  28598. + * redistribute this Software in source and binary forms, with or without
  28599. + * modification, provided that redistributions of source code must retain this
  28600. + * notice. You may not view, use, disclose, copy or distribute this file or
  28601. + * any information contained herein except pursuant to this license grant from
  28602. + * Synopsys. If you do not agree with this notice, including the disclaimer
  28603. + * below, then you are not authorized to use the Software.
  28604. + *
  28605. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  28606. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28607. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28608. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  28609. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28610. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28611. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28612. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28613. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28614. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  28615. + * DAMAGE.
  28616. + * ========================================================================== */
  28617. +
  28618. +#if !defined(__DWC_CIL_H__)
  28619. +#define __DWC_CIL_H__
  28620. +
  28621. +#include "dwc_list.h"
  28622. +#include "dwc_otg_dbg.h"
  28623. +#include "dwc_otg_regs.h"
  28624. +
  28625. +#include "dwc_otg_core_if.h"
  28626. +#include "dwc_otg_adp.h"
  28627. +
  28628. +/**
  28629. + * @file
  28630. + * This file contains the interface to the Core Interface Layer.
  28631. + */
  28632. +
  28633. +#ifdef DWC_UTE_CFI
  28634. +
  28635. +#define MAX_DMA_DESCS_PER_EP 256
  28636. +
  28637. +/**
  28638. + * Enumeration for the data buffer mode
  28639. + */
  28640. +typedef enum _data_buffer_mode {
  28641. + BM_STANDARD = 0, /* data buffer is in normal mode */
  28642. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  28643. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  28644. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  28645. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  28646. +} data_buffer_mode_e;
  28647. +#endif //DWC_UTE_CFI
  28648. +
  28649. +/** Macros defined for DWC OTG HW Release version */
  28650. +
  28651. +#define OTG_CORE_REV_2_60a 0x4F54260A
  28652. +#define OTG_CORE_REV_2_71a 0x4F54271A
  28653. +#define OTG_CORE_REV_2_72a 0x4F54272A
  28654. +#define OTG_CORE_REV_2_80a 0x4F54280A
  28655. +#define OTG_CORE_REV_2_81a 0x4F54281A
  28656. +#define OTG_CORE_REV_2_90a 0x4F54290A
  28657. +#define OTG_CORE_REV_2_91a 0x4F54291A
  28658. +#define OTG_CORE_REV_2_92a 0x4F54292A
  28659. +#define OTG_CORE_REV_2_93a 0x4F54293A
  28660. +#define OTG_CORE_REV_2_94a 0x4F54294A
  28661. +#define OTG_CORE_REV_3_00a 0x4F54300A
  28662. +
  28663. +/**
  28664. + * Information for each ISOC packet.
  28665. + */
  28666. +typedef struct iso_pkt_info {
  28667. + uint32_t offset;
  28668. + uint32_t length;
  28669. + int32_t status;
  28670. +} iso_pkt_info_t;
  28671. +
  28672. +/**
  28673. + * The <code>dwc_ep</code> structure represents the state of a single
  28674. + * endpoint when acting in device mode. It contains the data items
  28675. + * needed for an endpoint to be activated and transfer packets.
  28676. + */
  28677. +typedef struct dwc_ep {
  28678. + /** EP number used for register address lookup */
  28679. + uint8_t num;
  28680. + /** EP direction 0 = OUT */
  28681. + unsigned is_in:1;
  28682. + /** EP active. */
  28683. + unsigned active:1;
  28684. +
  28685. + /**
  28686. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  28687. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  28688. + unsigned tx_fifo_num:4;
  28689. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  28690. + unsigned type:2;
  28691. +#define DWC_OTG_EP_TYPE_CONTROL 0
  28692. +#define DWC_OTG_EP_TYPE_ISOC 1
  28693. +#define DWC_OTG_EP_TYPE_BULK 2
  28694. +#define DWC_OTG_EP_TYPE_INTR 3
  28695. +
  28696. + /** DATA start PID for INTR and BULK EP */
  28697. + unsigned data_pid_start:1;
  28698. + /** Frame (even/odd) for ISOC EP */
  28699. + unsigned even_odd_frame:1;
  28700. + /** Max Packet bytes */
  28701. + unsigned maxpacket:11;
  28702. +
  28703. + /** Max Transfer size */
  28704. + uint32_t maxxfer;
  28705. +
  28706. + /** @name Transfer state */
  28707. + /** @{ */
  28708. +
  28709. + /**
  28710. + * Pointer to the beginning of the transfer buffer -- do not modify
  28711. + * during transfer.
  28712. + */
  28713. +
  28714. + dwc_dma_t dma_addr;
  28715. +
  28716. + dwc_dma_t dma_desc_addr;
  28717. + dwc_otg_dev_dma_desc_t *desc_addr;
  28718. +
  28719. + uint8_t *start_xfer_buff;
  28720. + /** pointer to the transfer buffer */
  28721. + uint8_t *xfer_buff;
  28722. + /** Number of bytes to transfer */
  28723. + unsigned xfer_len:19;
  28724. + /** Number of bytes transferred. */
  28725. + unsigned xfer_count:19;
  28726. + /** Sent ZLP */
  28727. + unsigned sent_zlp:1;
  28728. + /** Total len for control transfer */
  28729. + unsigned total_len:19;
  28730. +
  28731. + /** stall clear flag */
  28732. + unsigned stall_clear_flag:1;
  28733. +
  28734. + /** SETUP pkt cnt rollover flag for EP0 out*/
  28735. + unsigned stp_rollover;
  28736. +
  28737. +#ifdef DWC_UTE_CFI
  28738. + /* The buffer mode */
  28739. + data_buffer_mode_e buff_mode;
  28740. +
  28741. + /* The chain of DMA descriptors.
  28742. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  28743. + */
  28744. + dwc_otg_dma_desc_t *descs;
  28745. +
  28746. + /* The DMA address of the descriptors chain start */
  28747. + dma_addr_t descs_dma_addr;
  28748. + /** This variable stores the length of the last enqueued request */
  28749. + uint32_t cfi_req_len;
  28750. +#endif //DWC_UTE_CFI
  28751. +
  28752. +/** Max DMA Descriptor count for any EP */
  28753. +#define MAX_DMA_DESC_CNT 256
  28754. + /** Allocated DMA Desc count */
  28755. + uint32_t desc_cnt;
  28756. +
  28757. + /** bInterval */
  28758. + uint32_t bInterval;
  28759. + /** Next frame num to setup next ISOC transfer */
  28760. + uint32_t frame_num;
  28761. + /** Indicates SOF number overrun in DSTS */
  28762. + uint8_t frm_overrun;
  28763. +
  28764. +#ifdef DWC_UTE_PER_IO
  28765. + /** Next frame num for which will be setup DMA Desc */
  28766. + uint32_t xiso_frame_num;
  28767. + /** bInterval */
  28768. + uint32_t xiso_bInterval;
  28769. + /** Count of currently active transfers - shall be either 0 or 1 */
  28770. + int xiso_active_xfers;
  28771. + int xiso_queued_xfers;
  28772. +#endif
  28773. +#ifdef DWC_EN_ISOC
  28774. + /**
  28775. + * Variables specific for ISOC EPs
  28776. + *
  28777. + */
  28778. + /** DMA addresses of ISOC buffers */
  28779. + dwc_dma_t dma_addr0;
  28780. + dwc_dma_t dma_addr1;
  28781. +
  28782. + dwc_dma_t iso_dma_desc_addr;
  28783. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  28784. +
  28785. + /** pointer to the transfer buffers */
  28786. + uint8_t *xfer_buff0;
  28787. + uint8_t *xfer_buff1;
  28788. +
  28789. + /** number of ISOC Buffer is processing */
  28790. + uint32_t proc_buf_num;
  28791. + /** Interval of ISOC Buffer processing */
  28792. + uint32_t buf_proc_intrvl;
  28793. + /** Data size for regular frame */
  28794. + uint32_t data_per_frame;
  28795. +
  28796. + /* todo - pattern data support is to be implemented in the future */
  28797. + /** Data size for pattern frame */
  28798. + uint32_t data_pattern_frame;
  28799. + /** Frame number of pattern data */
  28800. + uint32_t sync_frame;
  28801. +
  28802. + /** bInterval */
  28803. + uint32_t bInterval;
  28804. + /** ISO Packet number per frame */
  28805. + uint32_t pkt_per_frm;
  28806. + /** Next frame num for which will be setup DMA Desc */
  28807. + uint32_t next_frame;
  28808. + /** Number of packets per buffer processing */
  28809. + uint32_t pkt_cnt;
  28810. + /** Info for all isoc packets */
  28811. + iso_pkt_info_t *pkt_info;
  28812. + /** current pkt number */
  28813. + uint32_t cur_pkt;
  28814. + /** current pkt number */
  28815. + uint8_t *cur_pkt_addr;
  28816. + /** current pkt number */
  28817. + uint32_t cur_pkt_dma_addr;
  28818. +#endif /* DWC_EN_ISOC */
  28819. +
  28820. +/** @} */
  28821. +} dwc_ep_t;
  28822. +
  28823. +/*
  28824. + * Reasons for halting a host channel.
  28825. + */
  28826. +typedef enum dwc_otg_halt_status {
  28827. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  28828. + DWC_OTG_HC_XFER_COMPLETE,
  28829. + DWC_OTG_HC_XFER_URB_COMPLETE,
  28830. + DWC_OTG_HC_XFER_ACK,
  28831. + DWC_OTG_HC_XFER_NAK,
  28832. + DWC_OTG_HC_XFER_NYET,
  28833. + DWC_OTG_HC_XFER_STALL,
  28834. + DWC_OTG_HC_XFER_XACT_ERR,
  28835. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  28836. + DWC_OTG_HC_XFER_BABBLE_ERR,
  28837. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  28838. + DWC_OTG_HC_XFER_AHB_ERR,
  28839. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  28840. + DWC_OTG_HC_XFER_URB_DEQUEUE
  28841. +} dwc_otg_halt_status_e;
  28842. +
  28843. +/**
  28844. + * Host channel descriptor. This structure represents the state of a single
  28845. + * host channel when acting in host mode. It contains the data items needed to
  28846. + * transfer packets to an endpoint via a host channel.
  28847. + */
  28848. +typedef struct dwc_hc {
  28849. + /** Host channel number used for register address lookup */
  28850. + uint8_t hc_num;
  28851. +
  28852. + /** Device to access */
  28853. + unsigned dev_addr:7;
  28854. +
  28855. + /** EP to access */
  28856. + unsigned ep_num:4;
  28857. +
  28858. + /** EP direction. 0: OUT, 1: IN */
  28859. + unsigned ep_is_in:1;
  28860. +
  28861. + /**
  28862. + * EP speed.
  28863. + * One of the following values:
  28864. + * - DWC_OTG_EP_SPEED_LOW
  28865. + * - DWC_OTG_EP_SPEED_FULL
  28866. + * - DWC_OTG_EP_SPEED_HIGH
  28867. + */
  28868. + unsigned speed:2;
  28869. +#define DWC_OTG_EP_SPEED_LOW 0
  28870. +#define DWC_OTG_EP_SPEED_FULL 1
  28871. +#define DWC_OTG_EP_SPEED_HIGH 2
  28872. +
  28873. + /**
  28874. + * Endpoint type.
  28875. + * One of the following values:
  28876. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  28877. + * - DWC_OTG_EP_TYPE_ISOC: 1
  28878. + * - DWC_OTG_EP_TYPE_BULK: 2
  28879. + * - DWC_OTG_EP_TYPE_INTR: 3
  28880. + */
  28881. + unsigned ep_type:2;
  28882. +
  28883. + /** Max packet size in bytes */
  28884. + unsigned max_packet:11;
  28885. +
  28886. + /**
  28887. + * PID for initial transaction.
  28888. + * 0: DATA0,<br>
  28889. + * 1: DATA2,<br>
  28890. + * 2: DATA1,<br>
  28891. + * 3: MDATA (non-Control EP),
  28892. + * SETUP (Control EP)
  28893. + */
  28894. + unsigned data_pid_start:2;
  28895. +#define DWC_OTG_HC_PID_DATA0 0
  28896. +#define DWC_OTG_HC_PID_DATA2 1
  28897. +#define DWC_OTG_HC_PID_DATA1 2
  28898. +#define DWC_OTG_HC_PID_MDATA 3
  28899. +#define DWC_OTG_HC_PID_SETUP 3
  28900. +
  28901. + /** Number of periodic transactions per (micro)frame */
  28902. + unsigned multi_count:2;
  28903. +
  28904. + /** @name Transfer State */
  28905. + /** @{ */
  28906. +
  28907. + /** Pointer to the current transfer buffer position. */
  28908. + uint8_t *xfer_buff;
  28909. + /**
  28910. + * In Buffer DMA mode this buffer will be used
  28911. + * if xfer_buff is not DWORD aligned.
  28912. + */
  28913. + dwc_dma_t align_buff;
  28914. + /** Total number of bytes to transfer. */
  28915. + uint32_t xfer_len;
  28916. + /** Number of bytes transferred so far. */
  28917. + uint32_t xfer_count;
  28918. + /** Packet count at start of transfer.*/
  28919. + uint16_t start_pkt_count;
  28920. +
  28921. + /**
  28922. + * Flag to indicate whether the transfer has been started. Set to 1 if
  28923. + * it has been started, 0 otherwise.
  28924. + */
  28925. + uint8_t xfer_started;
  28926. +
  28927. + /**
  28928. + * Set to 1 to indicate that a PING request should be issued on this
  28929. + * channel. If 0, process normally.
  28930. + */
  28931. + uint8_t do_ping;
  28932. +
  28933. + /**
  28934. + * Set to 1 to indicate that the error count for this transaction is
  28935. + * non-zero. Set to 0 if the error count is 0.
  28936. + */
  28937. + uint8_t error_state;
  28938. +
  28939. + /**
  28940. + * Set to 1 to indicate that this channel should be halted the next
  28941. + * time a request is queued for the channel. This is necessary in
  28942. + * slave mode if no request queue space is available when an attempt
  28943. + * is made to halt the channel.
  28944. + */
  28945. + uint8_t halt_on_queue;
  28946. +
  28947. + /**
  28948. + * Set to 1 if the host channel has been halted, but the core is not
  28949. + * finished flushing queued requests. Otherwise 0.
  28950. + */
  28951. + uint8_t halt_pending;
  28952. +
  28953. + /**
  28954. + * Reason for halting the host channel.
  28955. + */
  28956. + dwc_otg_halt_status_e halt_status;
  28957. +
  28958. + /*
  28959. + * Split settings for the host channel
  28960. + */
  28961. + uint8_t do_split; /**< Enable split for the channel */
  28962. + uint8_t complete_split; /**< Enable complete split */
  28963. + uint8_t hub_addr; /**< Address of high speed hub */
  28964. +
  28965. + uint8_t port_addr; /**< Port of the low/full speed device */
  28966. + /** Split transaction position
  28967. + * One of the following values:
  28968. + * - DWC_HCSPLIT_XACTPOS_MID
  28969. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  28970. + * - DWC_HCSPLIT_XACTPOS_END
  28971. + * - DWC_HCSPLIT_XACTPOS_ALL */
  28972. + uint8_t xact_pos;
  28973. +
  28974. + /** Set when the host channel does a short read. */
  28975. + uint8_t short_read;
  28976. +
  28977. + /**
  28978. + * Number of requests issued for this channel since it was assigned to
  28979. + * the current transfer (not counting PINGs).
  28980. + */
  28981. + uint8_t requests;
  28982. +
  28983. + /**
  28984. + * Queue Head for the transfer being processed by this channel.
  28985. + */
  28986. + struct dwc_otg_qh *qh;
  28987. +
  28988. + /** @} */
  28989. +
  28990. + /** Entry in list of host channels. */
  28991. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  28992. +
  28993. + /** @name Descriptor DMA support */
  28994. + /** @{ */
  28995. +
  28996. + /** Number of Transfer Descriptors */
  28997. + uint16_t ntd;
  28998. +
  28999. + /** Descriptor List DMA address */
  29000. + dwc_dma_t desc_list_addr;
  29001. +
  29002. + /** Scheduling micro-frame bitmap. */
  29003. + uint8_t schinfo;
  29004. +
  29005. + /** @} */
  29006. +} dwc_hc_t;
  29007. +
  29008. +/**
  29009. + * The following parameters may be specified when starting the module. These
  29010. + * parameters define how the DWC_otg controller should be configured.
  29011. + */
  29012. +typedef struct dwc_otg_core_params {
  29013. + int32_t opt;
  29014. +
  29015. + /**
  29016. + * Specifies the OTG capabilities. The driver will automatically
  29017. + * detect the value for this parameter if none is specified.
  29018. + * 0 - HNP and SRP capable (default)
  29019. + * 1 - SRP Only capable
  29020. + * 2 - No HNP/SRP capable
  29021. + */
  29022. + int32_t otg_cap;
  29023. +
  29024. + /**
  29025. + * Specifies whether to use slave or DMA mode for accessing the data
  29026. + * FIFOs. The driver will automatically detect the value for this
  29027. + * parameter if none is specified.
  29028. + * 0 - Slave
  29029. + * 1 - DMA (default, if available)
  29030. + */
  29031. + int32_t dma_enable;
  29032. +
  29033. + /**
  29034. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  29035. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  29036. + * will automatically detect the value for this if none is specified.
  29037. + * 0 - address DMA
  29038. + * 1 - DMA Descriptor(default, if available)
  29039. + */
  29040. + int32_t dma_desc_enable;
  29041. + /** The DMA Burst size (applicable only for External DMA
  29042. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  29043. + */
  29044. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  29045. +
  29046. + /**
  29047. + * Specifies the maximum speed of operation in host and device mode.
  29048. + * The actual speed depends on the speed of the attached device and
  29049. + * the value of phy_type. The actual speed depends on the speed of the
  29050. + * attached device.
  29051. + * 0 - High Speed (default)
  29052. + * 1 - Full Speed
  29053. + */
  29054. + int32_t speed;
  29055. + /** Specifies whether low power mode is supported when attached
  29056. + * to a Full Speed or Low Speed device in host mode.
  29057. + * 0 - Don't support low power mode (default)
  29058. + * 1 - Support low power mode
  29059. + */
  29060. + int32_t host_support_fs_ls_low_power;
  29061. +
  29062. + /** Specifies the PHY clock rate in low power mode when connected to a
  29063. + * Low Speed device in host mode. This parameter is applicable only if
  29064. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  29065. + * then defaults to 6 MHZ otherwise 48 MHZ.
  29066. + *
  29067. + * 0 - 48 MHz
  29068. + * 1 - 6 MHz
  29069. + */
  29070. + int32_t host_ls_low_power_phy_clk;
  29071. +
  29072. + /**
  29073. + * 0 - Use cC FIFO size parameters
  29074. + * 1 - Allow dynamic FIFO sizing (default)
  29075. + */
  29076. + int32_t enable_dynamic_fifo;
  29077. +
  29078. + /** Total number of 4-byte words in the data FIFO memory. This
  29079. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  29080. + * Tx FIFOs.
  29081. + * 32 to 32768 (default 8192)
  29082. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  29083. + */
  29084. + int32_t data_fifo_size;
  29085. +
  29086. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  29087. + * FIFO sizing is enabled.
  29088. + * 16 to 32768 (default 1064)
  29089. + */
  29090. + int32_t dev_rx_fifo_size;
  29091. +
  29092. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  29093. + * when dynamic FIFO sizing is enabled.
  29094. + * 16 to 32768 (default 1024)
  29095. + */
  29096. + int32_t dev_nperio_tx_fifo_size;
  29097. +
  29098. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  29099. + * mode when dynamic FIFO sizing is enabled.
  29100. + * 4 to 768 (default 256)
  29101. + */
  29102. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  29103. +
  29104. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  29105. + * FIFO sizing is enabled.
  29106. + * 16 to 32768 (default 1024)
  29107. + */
  29108. + int32_t host_rx_fifo_size;
  29109. +
  29110. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  29111. + * when Dynamic FIFO sizing is enabled in the core.
  29112. + * 16 to 32768 (default 1024)
  29113. + */
  29114. + int32_t host_nperio_tx_fifo_size;
  29115. +
  29116. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  29117. + * FIFO sizing is enabled.
  29118. + * 16 to 32768 (default 1024)
  29119. + */
  29120. + int32_t host_perio_tx_fifo_size;
  29121. +
  29122. + /** The maximum transfer size supported in bytes.
  29123. + * 2047 to 65,535 (default 65,535)
  29124. + */
  29125. + int32_t max_transfer_size;
  29126. +
  29127. + /** The maximum number of packets in a transfer.
  29128. + * 15 to 511 (default 511)
  29129. + */
  29130. + int32_t max_packet_count;
  29131. +
  29132. + /** The number of host channel registers to use.
  29133. + * 1 to 16 (default 12)
  29134. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  29135. + */
  29136. + int32_t host_channels;
  29137. +
  29138. + /** The number of endpoints in addition to EP0 available for device
  29139. + * mode operations.
  29140. + * 1 to 15 (default 6 IN and OUT)
  29141. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  29142. + * endpoints in addition to EP0.
  29143. + */
  29144. + int32_t dev_endpoints;
  29145. +
  29146. + /**
  29147. + * Specifies the type of PHY interface to use. By default, the driver
  29148. + * will automatically detect the phy_type.
  29149. + *
  29150. + * 0 - Full Speed PHY
  29151. + * 1 - UTMI+ (default)
  29152. + * 2 - ULPI
  29153. + */
  29154. + int32_t phy_type;
  29155. +
  29156. + /**
  29157. + * Specifies the UTMI+ Data Width. This parameter is
  29158. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  29159. + * PHY_TYPE, this parameter indicates the data width between
  29160. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  29161. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  29162. + * to "8 and 16 bits", meaning that the core has been
  29163. + * configured to work at either data path width.
  29164. + *
  29165. + * 8 or 16 bits (default 16)
  29166. + */
  29167. + int32_t phy_utmi_width;
  29168. +
  29169. + /**
  29170. + * Specifies whether the ULPI operates at double or single
  29171. + * data rate. This parameter is only applicable if PHY_TYPE is
  29172. + * ULPI.
  29173. + *
  29174. + * 0 - single data rate ULPI interface with 8 bit wide data
  29175. + * bus (default)
  29176. + * 1 - double data rate ULPI interface with 4 bit wide data
  29177. + * bus
  29178. + */
  29179. + int32_t phy_ulpi_ddr;
  29180. +
  29181. + /**
  29182. + * Specifies whether to use the internal or external supply to
  29183. + * drive the vbus with a ULPI phy.
  29184. + */
  29185. + int32_t phy_ulpi_ext_vbus;
  29186. +
  29187. + /**
  29188. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  29189. + * parameter is only applicable if PHY_TYPE is FS.
  29190. + * 0 - No (default)
  29191. + * 1 - Yes
  29192. + */
  29193. + int32_t i2c_enable;
  29194. +
  29195. + int32_t ulpi_fs_ls;
  29196. +
  29197. + int32_t ts_dline;
  29198. +
  29199. + /**
  29200. + * Specifies whether dedicated transmit FIFOs are
  29201. + * enabled for non periodic IN endpoints in device mode
  29202. + * 0 - No
  29203. + * 1 - Yes
  29204. + */
  29205. + int32_t en_multiple_tx_fifo;
  29206. +
  29207. + /** Number of 4-byte words in each of the Tx FIFOs in device
  29208. + * mode when dynamic FIFO sizing is enabled.
  29209. + * 4 to 768 (default 256)
  29210. + */
  29211. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  29212. +
  29213. + /** Thresholding enable flag-
  29214. + * bit 0 - enable non-ISO Tx thresholding
  29215. + * bit 1 - enable ISO Tx thresholding
  29216. + * bit 2 - enable Rx thresholding
  29217. + */
  29218. + uint32_t thr_ctl;
  29219. +
  29220. + /** Thresholding length for Tx
  29221. + * FIFOs in 32 bit DWORDs
  29222. + */
  29223. + uint32_t tx_thr_length;
  29224. +
  29225. + /** Thresholding length for Rx
  29226. + * FIFOs in 32 bit DWORDs
  29227. + */
  29228. + uint32_t rx_thr_length;
  29229. +
  29230. + /**
  29231. + * Specifies whether LPM (Link Power Management) support is enabled
  29232. + */
  29233. + int32_t lpm_enable;
  29234. +
  29235. + /** Per Transfer Interrupt
  29236. + * mode enable flag
  29237. + * 1 - Enabled
  29238. + * 0 - Disabled
  29239. + */
  29240. + int32_t pti_enable;
  29241. +
  29242. + /** Multi Processor Interrupt
  29243. + * mode enable flag
  29244. + * 1 - Enabled
  29245. + * 0 - Disabled
  29246. + */
  29247. + int32_t mpi_enable;
  29248. +
  29249. + /** IS_USB Capability
  29250. + * 1 - Enabled
  29251. + * 0 - Disabled
  29252. + */
  29253. + int32_t ic_usb_cap;
  29254. +
  29255. + /** AHB Threshold Ratio
  29256. + * 2'b00 AHB Threshold = MAC Threshold
  29257. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  29258. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  29259. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  29260. + */
  29261. + int32_t ahb_thr_ratio;
  29262. +
  29263. + /** ADP Support
  29264. + * 1 - Enabled
  29265. + * 0 - Disabled
  29266. + */
  29267. + int32_t adp_supp_enable;
  29268. +
  29269. + /** HFIR Reload Control
  29270. + * 0 - The HFIR cannot be reloaded dynamically.
  29271. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  29272. + */
  29273. + int32_t reload_ctl;
  29274. +
  29275. + /** DCFG: Enable device Out NAK
  29276. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  29277. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  29278. + */
  29279. + int32_t dev_out_nak;
  29280. +
  29281. + /** DCFG: Enable Continue on BNA
  29282. + * After receiving BNA interrupt the core disables the endpoint,when the
  29283. + * endpoint is re-enabled by the application the core starts processing
  29284. + * 0 - from the DOEPDMA descriptor
  29285. + * 1 - from the descriptor which received the BNA.
  29286. + */
  29287. + int32_t cont_on_bna;
  29288. +
  29289. + /** GAHBCFG: AHB Single Support
  29290. + * This bit when programmed supports SINGLE transfers for remainder
  29291. + * data in a transfer for DMA mode of operation.
  29292. + * 0 - in this case the remainder data will be sent using INCR burst size.
  29293. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  29294. + */
  29295. + int32_t ahb_single;
  29296. +
  29297. + /** Core Power down mode
  29298. + * 0 - No Power Down is enabled
  29299. + * 1 - Reserved
  29300. + * 2 - Complete Power Down (Hibernation)
  29301. + */
  29302. + int32_t power_down;
  29303. +
  29304. + /** OTG revision supported
  29305. + * 0 - OTG 1.3 revision
  29306. + * 1 - OTG 2.0 revision
  29307. + */
  29308. + int32_t otg_ver;
  29309. +
  29310. +} dwc_otg_core_params_t;
  29311. +
  29312. +#ifdef DEBUG
  29313. +struct dwc_otg_core_if;
  29314. +typedef struct hc_xfer_info {
  29315. + struct dwc_otg_core_if *core_if;
  29316. + dwc_hc_t *hc;
  29317. +} hc_xfer_info_t;
  29318. +#endif
  29319. +
  29320. +typedef struct ep_xfer_info {
  29321. + struct dwc_otg_core_if *core_if;
  29322. + dwc_ep_t *ep;
  29323. + uint8_t state;
  29324. +} ep_xfer_info_t;
  29325. +/*
  29326. + * Device States
  29327. + */
  29328. +typedef enum dwc_otg_lx_state {
  29329. + /** On state */
  29330. + DWC_OTG_L0,
  29331. + /** LPM sleep state*/
  29332. + DWC_OTG_L1,
  29333. + /** USB suspend state*/
  29334. + DWC_OTG_L2,
  29335. + /** Off state*/
  29336. + DWC_OTG_L3
  29337. +} dwc_otg_lx_state_e;
  29338. +
  29339. +struct dwc_otg_global_regs_backup {
  29340. + uint32_t gotgctl_local;
  29341. + uint32_t gintmsk_local;
  29342. + uint32_t gahbcfg_local;
  29343. + uint32_t gusbcfg_local;
  29344. + uint32_t grxfsiz_local;
  29345. + uint32_t gnptxfsiz_local;
  29346. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29347. + uint32_t glpmcfg_local;
  29348. +#endif
  29349. + uint32_t gi2cctl_local;
  29350. + uint32_t hptxfsiz_local;
  29351. + uint32_t pcgcctl_local;
  29352. + uint32_t gdfifocfg_local;
  29353. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  29354. + uint32_t gpwrdn_local;
  29355. + uint32_t xhib_pcgcctl;
  29356. + uint32_t xhib_gpwrdn;
  29357. +};
  29358. +
  29359. +struct dwc_otg_host_regs_backup {
  29360. + uint32_t hcfg_local;
  29361. + uint32_t haintmsk_local;
  29362. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  29363. + uint32_t hprt0_local;
  29364. + uint32_t hfir_local;
  29365. +};
  29366. +
  29367. +struct dwc_otg_dev_regs_backup {
  29368. + uint32_t dcfg;
  29369. + uint32_t dctl;
  29370. + uint32_t daintmsk;
  29371. + uint32_t diepmsk;
  29372. + uint32_t doepmsk;
  29373. + uint32_t diepctl[MAX_EPS_CHANNELS];
  29374. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  29375. + uint32_t diepdma[MAX_EPS_CHANNELS];
  29376. +};
  29377. +/**
  29378. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  29379. + * the DWC_otg controller acting in either host or device mode. It
  29380. + * represents the programming view of the controller as a whole.
  29381. + */
  29382. +struct dwc_otg_core_if {
  29383. + /** Parameters that define how the core should be configured.*/
  29384. + dwc_otg_core_params_t *core_params;
  29385. +
  29386. + /** Core Global registers starting at offset 000h. */
  29387. + dwc_otg_core_global_regs_t *core_global_regs;
  29388. +
  29389. + /** Device-specific information */
  29390. + dwc_otg_dev_if_t *dev_if;
  29391. + /** Host-specific information */
  29392. + dwc_otg_host_if_t *host_if;
  29393. +
  29394. + /** Value from SNPSID register */
  29395. + uint32_t snpsid;
  29396. +
  29397. + /*
  29398. + * Set to 1 if the core PHY interface bits in USBCFG have been
  29399. + * initialized.
  29400. + */
  29401. + uint8_t phy_init_done;
  29402. +
  29403. + /*
  29404. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  29405. + */
  29406. + uint8_t srp_success;
  29407. + uint8_t srp_timer_started;
  29408. + /** Timer for SRP. If it expires before SRP is successful
  29409. + * clear the SRP. */
  29410. + dwc_timer_t *srp_timer;
  29411. +
  29412. +#ifdef DWC_DEV_SRPCAP
  29413. + /* This timer is needed to power on the hibernated host core if SRP is not
  29414. + * initiated on connected SRP capable device for limited period of time
  29415. + */
  29416. + uint8_t pwron_timer_started;
  29417. + dwc_timer_t *pwron_timer;
  29418. +#endif
  29419. + /* Common configuration information */
  29420. + /** Power and Clock Gating Control Register */
  29421. + volatile uint32_t *pcgcctl;
  29422. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  29423. +
  29424. + /** Push/pop addresses for endpoints or host channels.*/
  29425. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  29426. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  29427. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  29428. +
  29429. + /** Total RAM for FIFOs (Bytes) */
  29430. + uint16_t total_fifo_size;
  29431. + /** Size of Rx FIFO (Bytes) */
  29432. + uint16_t rx_fifo_size;
  29433. + /** Size of Non-periodic Tx FIFO (Bytes) */
  29434. + uint16_t nperio_tx_fifo_size;
  29435. +
  29436. + /** 1 if DMA is enabled, 0 otherwise. */
  29437. + uint8_t dma_enable;
  29438. +
  29439. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  29440. + uint8_t dma_desc_enable;
  29441. +
  29442. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  29443. + uint8_t pti_enh_enable;
  29444. +
  29445. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  29446. + uint8_t multiproc_int_enable;
  29447. +
  29448. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  29449. + uint8_t en_multiple_tx_fifo;
  29450. +
  29451. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  29452. + * process of being queued */
  29453. + uint8_t queuing_high_bandwidth;
  29454. +
  29455. + /** Hardware Configuration -- stored here for convenience.*/
  29456. + hwcfg1_data_t hwcfg1;
  29457. + hwcfg2_data_t hwcfg2;
  29458. + hwcfg3_data_t hwcfg3;
  29459. + hwcfg4_data_t hwcfg4;
  29460. + fifosize_data_t hptxfsiz;
  29461. +
  29462. + /** Host and Device Configuration -- stored here for convenience.*/
  29463. + hcfg_data_t hcfg;
  29464. + dcfg_data_t dcfg;
  29465. +
  29466. + /** The operational State, during transations
  29467. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  29468. + * match the core but allows the software to determine
  29469. + * transitions.
  29470. + */
  29471. + uint8_t op_state;
  29472. +
  29473. + /**
  29474. + * Set to 1 if the HCD needs to be restarted on a session request
  29475. + * interrupt. This is required if no connector ID status change has
  29476. + * occurred since the HCD was last disconnected.
  29477. + */
  29478. + uint8_t restart_hcd_on_session_req;
  29479. +
  29480. + /** HCD callbacks */
  29481. + /** A-Device is a_host */
  29482. +#define A_HOST (1)
  29483. + /** A-Device is a_suspend */
  29484. +#define A_SUSPEND (2)
  29485. + /** A-Device is a_peripherial */
  29486. +#define A_PERIPHERAL (3)
  29487. + /** B-Device is operating as a Peripheral. */
  29488. +#define B_PERIPHERAL (4)
  29489. + /** B-Device is operating as a Host. */
  29490. +#define B_HOST (5)
  29491. +
  29492. + /** HCD callbacks */
  29493. + struct dwc_otg_cil_callbacks *hcd_cb;
  29494. + /** PCD callbacks */
  29495. + struct dwc_otg_cil_callbacks *pcd_cb;
  29496. +
  29497. + /** Device mode Periodic Tx FIFO Mask */
  29498. + uint32_t p_tx_msk;
  29499. + /** Device mode Periodic Tx FIFO Mask */
  29500. + uint32_t tx_msk;
  29501. +
  29502. + /** Workqueue object used for handling several interrupts */
  29503. + dwc_workq_t *wq_otg;
  29504. +
  29505. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  29506. + dwc_timer_t *wkp_timer;
  29507. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  29508. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  29509. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  29510. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  29511. +#ifdef DEBUG
  29512. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  29513. +
  29514. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  29515. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  29516. +
  29517. + uint32_t hfnum_7_samples;
  29518. + uint64_t hfnum_7_frrem_accum;
  29519. + uint32_t hfnum_0_samples;
  29520. + uint64_t hfnum_0_frrem_accum;
  29521. + uint32_t hfnum_other_samples;
  29522. + uint64_t hfnum_other_frrem_accum;
  29523. +#endif
  29524. +
  29525. +#ifdef DWC_UTE_CFI
  29526. + uint16_t pwron_rxfsiz;
  29527. + uint16_t pwron_gnptxfsiz;
  29528. + uint16_t pwron_txfsiz[15];
  29529. +
  29530. + uint16_t init_rxfsiz;
  29531. + uint16_t init_gnptxfsiz;
  29532. + uint16_t init_txfsiz[15];
  29533. +#endif
  29534. +
  29535. + /** Lx state of device */
  29536. + dwc_otg_lx_state_e lx_state;
  29537. +
  29538. + /** Saved Core Global registers */
  29539. + struct dwc_otg_global_regs_backup *gr_backup;
  29540. + /** Saved Host registers */
  29541. + struct dwc_otg_host_regs_backup *hr_backup;
  29542. + /** Saved Device registers */
  29543. + struct dwc_otg_dev_regs_backup *dr_backup;
  29544. +
  29545. + /** Power Down Enable */
  29546. + uint32_t power_down;
  29547. +
  29548. + /** ADP support Enable */
  29549. + uint32_t adp_enable;
  29550. +
  29551. + /** ADP structure object */
  29552. + dwc_otg_adp_t adp;
  29553. +
  29554. + /** hibernation/suspend flag */
  29555. + int hibernation_suspend;
  29556. +
  29557. + /** Device mode extended hibernation flag */
  29558. + int xhib;
  29559. +
  29560. + /** OTG revision supported */
  29561. + uint32_t otg_ver;
  29562. +
  29563. + /** OTG status flag used for HNP polling */
  29564. + uint8_t otg_sts;
  29565. +
  29566. + /** Pointer to either hcd->lock or pcd->lock */
  29567. + dwc_spinlock_t *lock;
  29568. +
  29569. + /** Start predict NextEP based on Learning Queue if equal 1,
  29570. + * also used as counter of disabled NP IN EP's */
  29571. + uint8_t start_predict;
  29572. +
  29573. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  29574. + * active, 0xff otherwise */
  29575. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  29576. +
  29577. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  29578. + uint8_t first_in_nextep_seq;
  29579. +
  29580. + /** Frame number while entering to ISR - needed for ISOCs **/
  29581. + uint32_t frame_num;
  29582. +
  29583. +};
  29584. +
  29585. +#ifdef DEBUG
  29586. +/*
  29587. + * This function is called when transfer is timed out.
  29588. + */
  29589. +extern void hc_xfer_timeout(void *ptr);
  29590. +#endif
  29591. +
  29592. +/*
  29593. + * This function is called when transfer is timed out on endpoint.
  29594. + */
  29595. +extern void ep_xfer_timeout(void *ptr);
  29596. +
  29597. +/*
  29598. + * The following functions are functions for works
  29599. + * using during handling some interrupts
  29600. + */
  29601. +extern void w_conn_id_status_change(void *p);
  29602. +
  29603. +extern void w_wakeup_detected(void *p);
  29604. +
  29605. +/** Saves global register values into system memory. */
  29606. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  29607. +/** Saves device register values into system memory. */
  29608. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  29609. +/** Saves host register values into system memory. */
  29610. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  29611. +/** Restore global register values. */
  29612. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  29613. +/** Restore host register values. */
  29614. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  29615. +/** Restore device register values. */
  29616. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  29617. + int rem_wakeup);
  29618. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  29619. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  29620. + int is_host);
  29621. +
  29622. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  29623. + int restore_mode, int reset);
  29624. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  29625. + int rem_wakeup, int reset);
  29626. +
  29627. +/*
  29628. + * The following functions support initialization of the CIL driver component
  29629. + * and the DWC_otg controller.
  29630. + */
  29631. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  29632. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  29633. +
  29634. +/** @name Device CIL Functions
  29635. + * The following functions support managing the DWC_otg controller in device
  29636. + * mode.
  29637. + */
  29638. +/**@{*/
  29639. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  29640. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  29641. + uint32_t * _dest);
  29642. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  29643. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29644. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29645. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29646. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  29647. + dwc_ep_t * _ep);
  29648. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  29649. + dwc_ep_t * _ep);
  29650. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  29651. + dwc_ep_t * _ep);
  29652. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  29653. + dwc_ep_t * _ep);
  29654. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  29655. + dwc_ep_t * _ep, int _dma);
  29656. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29657. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  29658. + dwc_ep_t * _ep);
  29659. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  29660. +
  29661. +#ifdef DWC_EN_ISOC
  29662. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  29663. + dwc_ep_t * ep);
  29664. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  29665. + dwc_ep_t * ep);
  29666. +#endif /* DWC_EN_ISOC */
  29667. +/**@}*/
  29668. +
  29669. +/** @name Host CIL Functions
  29670. + * The following functions support managing the DWC_otg controller in host
  29671. + * mode.
  29672. + */
  29673. +/**@{*/
  29674. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29675. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  29676. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  29677. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29678. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  29679. + dwc_hc_t * _hc);
  29680. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  29681. + dwc_hc_t * _hc);
  29682. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29683. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  29684. + dwc_hc_t * _hc);
  29685. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  29686. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  29687. +
  29688. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  29689. + dwc_hc_t * hc);
  29690. +
  29691. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  29692. +
  29693. +/* Macro used to clear one channel interrupt */
  29694. +#define clear_hc_int(_hc_regs_, _intr_) \
  29695. +do { \
  29696. + hcint_data_t hcint_clear = {.d32 = 0}; \
  29697. + hcint_clear.b._intr_ = 1; \
  29698. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  29699. +} while (0)
  29700. +
  29701. +/*
  29702. + * Macro used to disable one channel interrupt. Channel interrupts are
  29703. + * disabled when the channel is halted or released by the interrupt handler.
  29704. + * There is no need to handle further interrupts of that type until the
  29705. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  29706. + * because the channel structures are cleaned up when the channel is released.
  29707. + */
  29708. +#define disable_hc_int(_hc_regs_, _intr_) \
  29709. +do { \
  29710. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  29711. + hcintmsk.b._intr_ = 1; \
  29712. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  29713. +} while (0)
  29714. +
  29715. +/**
  29716. + * This function Reads HPRT0 in preparation to modify. It keeps the
  29717. + * WC bits 0 so that if they are read as 1, they won't clear when you
  29718. + * write it back
  29719. + */
  29720. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  29721. +{
  29722. + hprt0_data_t hprt0;
  29723. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  29724. + hprt0.b.prtena = 0;
  29725. + hprt0.b.prtconndet = 0;
  29726. + hprt0.b.prtenchng = 0;
  29727. + hprt0.b.prtovrcurrchng = 0;
  29728. + return hprt0.d32;
  29729. +}
  29730. +
  29731. +/**@}*/
  29732. +
  29733. +/** @name Common CIL Functions
  29734. + * The following functions support managing the DWC_otg controller in either
  29735. + * device or host mode.
  29736. + */
  29737. +/**@{*/
  29738. +
  29739. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  29740. + uint8_t * dest, uint16_t bytes);
  29741. +
  29742. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  29743. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  29744. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  29745. +
  29746. +/**
  29747. + * This function returns the Core Interrupt register.
  29748. + */
  29749. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  29750. +{
  29751. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  29752. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  29753. +}
  29754. +
  29755. +/**
  29756. + * This function returns the OTG Interrupt register.
  29757. + */
  29758. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  29759. +{
  29760. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  29761. +}
  29762. +
  29763. +/**
  29764. + * This function reads the Device All Endpoints Interrupt register and
  29765. + * returns the IN endpoint interrupt bits.
  29766. + */
  29767. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  29768. + core_if)
  29769. +{
  29770. +
  29771. + uint32_t v;
  29772. +
  29773. + if (core_if->multiproc_int_enable) {
  29774. + v = DWC_READ_REG32(&core_if->dev_if->
  29775. + dev_global_regs->deachint) &
  29776. + DWC_READ_REG32(&core_if->
  29777. + dev_if->dev_global_regs->deachintmsk);
  29778. + } else {
  29779. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  29780. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  29781. + }
  29782. + return (v & 0xffff);
  29783. +}
  29784. +
  29785. +/**
  29786. + * This function reads the Device All Endpoints Interrupt register and
  29787. + * returns the OUT endpoint interrupt bits.
  29788. + */
  29789. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  29790. + core_if)
  29791. +{
  29792. + uint32_t v;
  29793. +
  29794. + if (core_if->multiproc_int_enable) {
  29795. + v = DWC_READ_REG32(&core_if->dev_if->
  29796. + dev_global_regs->deachint) &
  29797. + DWC_READ_REG32(&core_if->
  29798. + dev_if->dev_global_regs->deachintmsk);
  29799. + } else {
  29800. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  29801. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  29802. + }
  29803. +
  29804. + return ((v & 0xffff0000) >> 16);
  29805. +}
  29806. +
  29807. +/**
  29808. + * This function returns the Device IN EP Interrupt register
  29809. + */
  29810. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  29811. + dwc_ep_t * ep)
  29812. +{
  29813. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  29814. + uint32_t v, msk, emp;
  29815. +
  29816. + if (core_if->multiproc_int_enable) {
  29817. + msk =
  29818. + DWC_READ_REG32(&dev_if->
  29819. + dev_global_regs->diepeachintmsk[ep->num]);
  29820. + emp =
  29821. + DWC_READ_REG32(&dev_if->
  29822. + dev_global_regs->dtknqr4_fifoemptymsk);
  29823. + msk |= ((emp >> ep->num) & 0x1) << 7;
  29824. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  29825. + } else {
  29826. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  29827. + emp =
  29828. + DWC_READ_REG32(&dev_if->
  29829. + dev_global_regs->dtknqr4_fifoemptymsk);
  29830. + msk |= ((emp >> ep->num) & 0x1) << 7;
  29831. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  29832. + }
  29833. +
  29834. + return v;
  29835. +}
  29836. +
  29837. +/**
  29838. + * This function returns the Device OUT EP Interrupt register
  29839. + */
  29840. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  29841. + _core_if, dwc_ep_t * _ep)
  29842. +{
  29843. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  29844. + uint32_t v;
  29845. + doepmsk_data_t msk = {.d32 = 0 };
  29846. +
  29847. + if (_core_if->multiproc_int_enable) {
  29848. + msk.d32 =
  29849. + DWC_READ_REG32(&dev_if->
  29850. + dev_global_regs->doepeachintmsk[_ep->num]);
  29851. + if (_core_if->pti_enh_enable) {
  29852. + msk.b.pktdrpsts = 1;
  29853. + }
  29854. + v = DWC_READ_REG32(&dev_if->
  29855. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  29856. + } else {
  29857. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  29858. + if (_core_if->pti_enh_enable) {
  29859. + msk.b.pktdrpsts = 1;
  29860. + }
  29861. + v = DWC_READ_REG32(&dev_if->
  29862. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  29863. + }
  29864. + return v;
  29865. +}
  29866. +
  29867. +/**
  29868. + * This function returns the Host All Channel Interrupt register
  29869. + */
  29870. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  29871. + _core_if)
  29872. +{
  29873. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  29874. +}
  29875. +
  29876. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  29877. + _core_if, dwc_hc_t * _hc)
  29878. +{
  29879. + return (DWC_READ_REG32
  29880. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  29881. +}
  29882. +
  29883. +/**
  29884. + * This function returns the mode of the operation, host or device.
  29885. + *
  29886. + * @return 0 - Device Mode, 1 - Host Mode
  29887. + */
  29888. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  29889. +{
  29890. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  29891. +}
  29892. +
  29893. +/**@}*/
  29894. +
  29895. +/**
  29896. + * DWC_otg CIL callback structure. This structure allows the HCD and
  29897. + * PCD to register functions used for starting and stopping the PCD
  29898. + * and HCD for role change on for a DRD.
  29899. + */
  29900. +typedef struct dwc_otg_cil_callbacks {
  29901. + /** Start function for role change */
  29902. + int (*start) (void *_p);
  29903. + /** Stop Function for role change */
  29904. + int (*stop) (void *_p);
  29905. + /** Disconnect Function for role change */
  29906. + int (*disconnect) (void *_p);
  29907. + /** Resume/Remote wakeup Function */
  29908. + int (*resume_wakeup) (void *_p);
  29909. + /** Suspend function */
  29910. + int (*suspend) (void *_p);
  29911. + /** Session Start (SRP) */
  29912. + int (*session_start) (void *_p);
  29913. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29914. + /** Sleep (switch to L0 state) */
  29915. + int (*sleep) (void *_p);
  29916. +#endif
  29917. + /** Pointer passed to start() and stop() */
  29918. + void *p;
  29919. +} dwc_otg_cil_callbacks_t;
  29920. +
  29921. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  29922. + dwc_otg_cil_callbacks_t * _cb,
  29923. + void *_p);
  29924. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  29925. + dwc_otg_cil_callbacks_t * _cb,
  29926. + void *_p);
  29927. +
  29928. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  29929. +
  29930. +//////////////////////////////////////////////////////////////////////
  29931. +/** Start the HCD. Helper function for using the HCD callbacks.
  29932. + *
  29933. + * @param core_if Programming view of DWC_otg controller.
  29934. + */
  29935. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  29936. +{
  29937. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  29938. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  29939. + }
  29940. +}
  29941. +
  29942. +/** Stop the HCD. Helper function for using the HCD callbacks.
  29943. + *
  29944. + * @param core_if Programming view of DWC_otg controller.
  29945. + */
  29946. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  29947. +{
  29948. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  29949. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  29950. + }
  29951. +}
  29952. +
  29953. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  29954. + *
  29955. + * @param core_if Programming view of DWC_otg controller.
  29956. + */
  29957. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  29958. +{
  29959. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  29960. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  29961. + }
  29962. +}
  29963. +
  29964. +/** Inform the HCD the a New Session has begun. Helper function for
  29965. + * using the HCD callbacks.
  29966. + *
  29967. + * @param core_if Programming view of DWC_otg controller.
  29968. + */
  29969. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  29970. +{
  29971. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  29972. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  29973. + }
  29974. +}
  29975. +
  29976. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29977. +/**
  29978. + * Inform the HCD about LPM sleep.
  29979. + * Helper function for using the HCD callbacks.
  29980. + *
  29981. + * @param core_if Programming view of DWC_otg controller.
  29982. + */
  29983. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  29984. +{
  29985. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  29986. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  29987. + }
  29988. +}
  29989. +#endif
  29990. +
  29991. +/** Resume the HCD. Helper function for using the HCD callbacks.
  29992. + *
  29993. + * @param core_if Programming view of DWC_otg controller.
  29994. + */
  29995. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  29996. +{
  29997. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  29998. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  29999. + }
  30000. +}
  30001. +
  30002. +/** Start the PCD. Helper function for using the PCD callbacks.
  30003. + *
  30004. + * @param core_if Programming view of DWC_otg controller.
  30005. + */
  30006. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  30007. +{
  30008. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  30009. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  30010. + }
  30011. +}
  30012. +
  30013. +/** Stop the PCD. Helper function for using the PCD callbacks.
  30014. + *
  30015. + * @param core_if Programming view of DWC_otg controller.
  30016. + */
  30017. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  30018. +{
  30019. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  30020. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  30021. + }
  30022. +}
  30023. +
  30024. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  30025. + *
  30026. + * @param core_if Programming view of DWC_otg controller.
  30027. + */
  30028. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  30029. +{
  30030. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  30031. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  30032. + }
  30033. +}
  30034. +
  30035. +/** Resume the PCD. Helper function for using the PCD callbacks.
  30036. + *
  30037. + * @param core_if Programming view of DWC_otg controller.
  30038. + */
  30039. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  30040. +{
  30041. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  30042. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  30043. + }
  30044. +}
  30045. +
  30046. +//////////////////////////////////////////////////////////////////////
  30047. +
  30048. +#endif
  30049. --- /dev/null
  30050. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  30051. @@ -0,0 +1,1596 @@
  30052. +/* ==========================================================================
  30053. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  30054. + * $Revision: #32 $
  30055. + * $Date: 2012/08/10 $
  30056. + * $Change: 2047372 $
  30057. + *
  30058. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  30059. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  30060. + * otherwise expressly agreed to in writing between Synopsys and you.
  30061. + *
  30062. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  30063. + * any End User Software License Agreement or Agreement for Licensed Product
  30064. + * with Synopsys or any supplement thereto. You are permitted to use and
  30065. + * redistribute this Software in source and binary forms, with or without
  30066. + * modification, provided that redistributions of source code must retain this
  30067. + * notice. You may not view, use, disclose, copy or distribute this file or
  30068. + * any information contained herein except pursuant to this license grant from
  30069. + * Synopsys. If you do not agree with this notice, including the disclaimer
  30070. + * below, then you are not authorized to use the Software.
  30071. + *
  30072. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  30073. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30074. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30075. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  30076. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30077. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30078. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30079. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30080. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30081. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  30082. + * DAMAGE.
  30083. + * ========================================================================== */
  30084. +
  30085. +/** @file
  30086. + *
  30087. + * The Core Interface Layer provides basic services for accessing and
  30088. + * managing the DWC_otg hardware. These services are used by both the
  30089. + * Host Controller Driver and the Peripheral Controller Driver.
  30090. + *
  30091. + * This file contains the Common Interrupt handlers.
  30092. + */
  30093. +#include "dwc_os.h"
  30094. +#include "dwc_otg_regs.h"
  30095. +#include "dwc_otg_cil.h"
  30096. +#include "dwc_otg_driver.h"
  30097. +#include "dwc_otg_pcd.h"
  30098. +#include "dwc_otg_hcd.h"
  30099. +
  30100. +#ifdef DEBUG
  30101. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  30102. +{
  30103. + return (core_if->op_state == A_HOST ? "a_host" :
  30104. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  30105. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  30106. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  30107. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  30108. +}
  30109. +#endif
  30110. +
  30111. +/** This function will log a debug message
  30112. + *
  30113. + * @param core_if Programming view of DWC_otg controller.
  30114. + */
  30115. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  30116. +{
  30117. + gintsts_data_t gintsts;
  30118. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  30119. + dwc_otg_mode(core_if) ? "Host" : "Device");
  30120. +
  30121. + /* Clear interrupt */
  30122. + gintsts.d32 = 0;
  30123. + gintsts.b.modemismatch = 1;
  30124. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30125. + return 1;
  30126. +}
  30127. +
  30128. +/**
  30129. + * This function handles the OTG Interrupts. It reads the OTG
  30130. + * Interrupt Register (GOTGINT) to determine what interrupt has
  30131. + * occurred.
  30132. + *
  30133. + * @param core_if Programming view of DWC_otg controller.
  30134. + */
  30135. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  30136. +{
  30137. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  30138. + gotgint_data_t gotgint;
  30139. + gotgctl_data_t gotgctl;
  30140. + gintmsk_data_t gintmsk;
  30141. + gpwrdn_data_t gpwrdn;
  30142. +
  30143. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  30144. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30145. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  30146. + op_state_str(core_if));
  30147. +
  30148. + if (gotgint.b.sesenddet) {
  30149. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30150. + "Session End Detected++ (%s)\n",
  30151. + op_state_str(core_if));
  30152. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30153. +
  30154. + if (core_if->op_state == B_HOST) {
  30155. + cil_pcd_start(core_if);
  30156. + core_if->op_state = B_PERIPHERAL;
  30157. + } else {
  30158. + /* If not B_HOST and Device HNP still set. HNP
  30159. + * Did not succeed!*/
  30160. + if (gotgctl.b.devhnpen) {
  30161. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  30162. + __DWC_ERROR("Device Not Connected/Responding!\n");
  30163. + }
  30164. +
  30165. + /* If Session End Detected the B-Cable has
  30166. + * been disconnected. */
  30167. + /* Reset PCD and Gadget driver to a
  30168. + * clean state. */
  30169. + core_if->lx_state = DWC_OTG_L0;
  30170. + DWC_SPINUNLOCK(core_if->lock);
  30171. + cil_pcd_stop(core_if);
  30172. + DWC_SPINLOCK(core_if->lock);
  30173. +
  30174. + if (core_if->adp_enable) {
  30175. + if (core_if->power_down == 2) {
  30176. + gpwrdn.d32 = 0;
  30177. + gpwrdn.b.pwrdnswtch = 1;
  30178. + DWC_MODIFY_REG32(&core_if->
  30179. + core_global_regs->
  30180. + gpwrdn, gpwrdn.d32, 0);
  30181. + }
  30182. +
  30183. + gpwrdn.d32 = 0;
  30184. + gpwrdn.b.pmuintsel = 1;
  30185. + gpwrdn.b.pmuactv = 1;
  30186. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30187. + gpwrdn, 0, gpwrdn.d32);
  30188. +
  30189. + dwc_otg_adp_sense_start(core_if);
  30190. + }
  30191. + }
  30192. +
  30193. + gotgctl.d32 = 0;
  30194. + gotgctl.b.devhnpen = 1;
  30195. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  30196. + }
  30197. + if (gotgint.b.sesreqsucstschng) {
  30198. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30199. + "Session Reqeust Success Status Change++\n");
  30200. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30201. + if (gotgctl.b.sesreqscs) {
  30202. +
  30203. + if ((core_if->core_params->phy_type ==
  30204. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  30205. + core_if->srp_success = 1;
  30206. + } else {
  30207. + DWC_SPINUNLOCK(core_if->lock);
  30208. + cil_pcd_resume(core_if);
  30209. + DWC_SPINLOCK(core_if->lock);
  30210. + /* Clear Session Request */
  30211. + gotgctl.d32 = 0;
  30212. + gotgctl.b.sesreq = 1;
  30213. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  30214. + gotgctl.d32, 0);
  30215. + }
  30216. + }
  30217. + }
  30218. + if (gotgint.b.hstnegsucstschng) {
  30219. + /* Print statements during the HNP interrupt handling
  30220. + * can cause it to fail.*/
  30221. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30222. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  30223. + * this does not help*/
  30224. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  30225. + dwc_udelay(100);
  30226. + if (gotgctl.b.hstnegscs) {
  30227. + if (dwc_otg_is_host_mode(core_if)) {
  30228. + core_if->op_state = B_HOST;
  30229. + /*
  30230. + * Need to disable SOF interrupt immediately.
  30231. + * When switching from device to host, the PCD
  30232. + * interrupt handler won't handle the
  30233. + * interrupt if host mode is already set. The
  30234. + * HCD interrupt handler won't get called if
  30235. + * the HCD state is HALT. This means that the
  30236. + * interrupt does not get handled and Linux
  30237. + * complains loudly.
  30238. + */
  30239. + gintmsk.d32 = 0;
  30240. + gintmsk.b.sofintr = 1;
  30241. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  30242. + gintmsk.d32, 0);
  30243. + /* Call callback function with spin lock released */
  30244. + DWC_SPINUNLOCK(core_if->lock);
  30245. + cil_pcd_stop(core_if);
  30246. + /*
  30247. + * Initialize the Core for Host mode.
  30248. + */
  30249. + cil_hcd_start(core_if);
  30250. + DWC_SPINLOCK(core_if->lock);
  30251. + core_if->op_state = B_HOST;
  30252. + }
  30253. + } else {
  30254. + gotgctl.d32 = 0;
  30255. + gotgctl.b.hnpreq = 1;
  30256. + gotgctl.b.devhnpen = 1;
  30257. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  30258. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  30259. + __DWC_ERROR("Device Not Connected/Responding\n");
  30260. + }
  30261. + }
  30262. + if (gotgint.b.hstnegdet) {
  30263. + /* The disconnect interrupt is set at the same time as
  30264. + * Host Negotiation Detected. During the mode
  30265. + * switch all interrupts are cleared so the disconnect
  30266. + * interrupt handler will not get executed.
  30267. + */
  30268. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30269. + "Host Negotiation Detected++ (%s)\n",
  30270. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30271. + "Device"));
  30272. + if (dwc_otg_is_device_mode(core_if)) {
  30273. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  30274. + core_if->op_state);
  30275. + DWC_SPINUNLOCK(core_if->lock);
  30276. + cil_hcd_disconnect(core_if);
  30277. + cil_pcd_start(core_if);
  30278. + DWC_SPINLOCK(core_if->lock);
  30279. + core_if->op_state = A_PERIPHERAL;
  30280. + } else {
  30281. + /*
  30282. + * Need to disable SOF interrupt immediately. When
  30283. + * switching from device to host, the PCD interrupt
  30284. + * handler won't handle the interrupt if host mode is
  30285. + * already set. The HCD interrupt handler won't get
  30286. + * called if the HCD state is HALT. This means that
  30287. + * the interrupt does not get handled and Linux
  30288. + * complains loudly.
  30289. + */
  30290. + gintmsk.d32 = 0;
  30291. + gintmsk.b.sofintr = 1;
  30292. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  30293. + DWC_SPINUNLOCK(core_if->lock);
  30294. + cil_pcd_stop(core_if);
  30295. + cil_hcd_start(core_if);
  30296. + DWC_SPINLOCK(core_if->lock);
  30297. + core_if->op_state = A_HOST;
  30298. + }
  30299. + }
  30300. + if (gotgint.b.adevtoutchng) {
  30301. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30302. + "A-Device Timeout Change++\n");
  30303. + }
  30304. + if (gotgint.b.debdone) {
  30305. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  30306. + }
  30307. +
  30308. + /* Clear GOTGINT */
  30309. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  30310. +
  30311. + return 1;
  30312. +}
  30313. +
  30314. +void w_conn_id_status_change(void *p)
  30315. +{
  30316. + dwc_otg_core_if_t *core_if = p;
  30317. + uint32_t count = 0;
  30318. + gotgctl_data_t gotgctl = {.d32 = 0 };
  30319. +
  30320. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  30321. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  30322. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  30323. +
  30324. + /* B-Device connector (Device Mode) */
  30325. + if (gotgctl.b.conidsts) {
  30326. + /* Wait for switch to device mode. */
  30327. + while (!dwc_otg_is_device_mode(core_if)) {
  30328. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  30329. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30330. + "Peripheral"));
  30331. + dwc_mdelay(100);
  30332. + if (++count > 10000)
  30333. + break;
  30334. + }
  30335. + DWC_ASSERT(++count < 10000,
  30336. + "Connection id status change timed out");
  30337. + core_if->op_state = B_PERIPHERAL;
  30338. + dwc_otg_core_init(core_if);
  30339. + dwc_otg_enable_global_interrupts(core_if);
  30340. + cil_pcd_start(core_if);
  30341. + } else {
  30342. + /* A-Device connector (Host Mode) */
  30343. + while (!dwc_otg_is_host_mode(core_if)) {
  30344. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  30345. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30346. + "Peripheral"));
  30347. + dwc_mdelay(100);
  30348. + if (++count > 10000)
  30349. + break;
  30350. + }
  30351. + DWC_ASSERT(++count < 10000,
  30352. + "Connection id status change timed out");
  30353. + core_if->op_state = A_HOST;
  30354. + /*
  30355. + * Initialize the Core for Host mode.
  30356. + */
  30357. + dwc_otg_core_init(core_if);
  30358. + dwc_otg_enable_global_interrupts(core_if);
  30359. + cil_hcd_start(core_if);
  30360. + }
  30361. +}
  30362. +
  30363. +/**
  30364. + * This function handles the Connector ID Status Change Interrupt. It
  30365. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  30366. + * is a Device to Host Mode transition or a Host Mode to Device
  30367. + * Transition.
  30368. + *
  30369. + * This only occurs when the cable is connected/removed from the PHY
  30370. + * connector.
  30371. + *
  30372. + * @param core_if Programming view of DWC_otg controller.
  30373. + */
  30374. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  30375. +{
  30376. +
  30377. + /*
  30378. + * Need to disable SOF interrupt immediately. If switching from device
  30379. + * to host, the PCD interrupt handler won't handle the interrupt if
  30380. + * host mode is already set. The HCD interrupt handler won't get
  30381. + * called if the HCD state is HALT. This means that the interrupt does
  30382. + * not get handled and Linux complains loudly.
  30383. + */
  30384. + gintmsk_data_t gintmsk = {.d32 = 0 };
  30385. + gintsts_data_t gintsts = {.d32 = 0 };
  30386. +
  30387. + gintmsk.b.sofintr = 1;
  30388. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  30389. +
  30390. + DWC_DEBUGPL(DBG_CIL,
  30391. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  30392. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  30393. +
  30394. + DWC_SPINUNLOCK(core_if->lock);
  30395. +
  30396. + /*
  30397. + * Need to schedule a work, as there are possible DELAY function calls
  30398. + * Release lock before scheduling workq as it holds spinlock during scheduling
  30399. + */
  30400. +
  30401. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  30402. + core_if, "connection id status change");
  30403. + DWC_SPINLOCK(core_if->lock);
  30404. +
  30405. + /* Set flag and clear interrupt */
  30406. + gintsts.b.conidstschng = 1;
  30407. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30408. +
  30409. + return 1;
  30410. +}
  30411. +
  30412. +/**
  30413. + * This interrupt indicates that a device is initiating the Session
  30414. + * Request Protocol to request the host to turn on bus power so a new
  30415. + * session can begin. The handler responds by turning on bus power. If
  30416. + * the DWC_otg controller is in low power mode, the handler brings the
  30417. + * controller out of low power mode before turning on bus power.
  30418. + *
  30419. + * @param core_if Programming view of DWC_otg controller.
  30420. + */
  30421. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  30422. +{
  30423. + gintsts_data_t gintsts;
  30424. +
  30425. +#ifndef DWC_HOST_ONLY
  30426. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  30427. +
  30428. + if (dwc_otg_is_device_mode(core_if)) {
  30429. + DWC_PRINTF("SRP: Device mode\n");
  30430. + } else {
  30431. + hprt0_data_t hprt0;
  30432. + DWC_PRINTF("SRP: Host mode\n");
  30433. +
  30434. + /* Turn on the port power bit. */
  30435. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  30436. + hprt0.b.prtpwr = 1;
  30437. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  30438. +
  30439. + /* Start the Connection timer. So a message can be displayed
  30440. + * if connect does not occur within 10 seconds. */
  30441. + cil_hcd_session_start(core_if);
  30442. + }
  30443. +#endif
  30444. +
  30445. + /* Clear interrupt */
  30446. + gintsts.d32 = 0;
  30447. + gintsts.b.sessreqintr = 1;
  30448. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30449. +
  30450. + return 1;
  30451. +}
  30452. +
  30453. +void w_wakeup_detected(void *p)
  30454. +{
  30455. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  30456. + /*
  30457. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  30458. + * so that OPT tests pass with all PHYs).
  30459. + */
  30460. + hprt0_data_t hprt0 = {.d32 = 0 };
  30461. +#if 0
  30462. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30463. + /* Restart the Phy Clock */
  30464. + pcgcctl.b.stoppclk = 1;
  30465. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  30466. + dwc_udelay(10);
  30467. +#endif //0
  30468. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  30469. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  30470. +// dwc_mdelay(70);
  30471. + hprt0.b.prtres = 0; /* Resume */
  30472. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  30473. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  30474. + DWC_READ_REG32(core_if->host_if->hprt0));
  30475. +
  30476. + cil_hcd_resume(core_if);
  30477. +
  30478. + /** Change to L0 state*/
  30479. + core_if->lx_state = DWC_OTG_L0;
  30480. +}
  30481. +
  30482. +/**
  30483. + * This interrupt indicates that the DWC_otg controller has detected a
  30484. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  30485. + * low power mode, the handler must brings the controller out of low
  30486. + * power mode. The controller automatically begins resume
  30487. + * signaling. The handler schedules a time to stop resume signaling.
  30488. + */
  30489. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  30490. +{
  30491. + gintsts_data_t gintsts;
  30492. +
  30493. + DWC_DEBUGPL(DBG_ANY,
  30494. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  30495. +
  30496. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  30497. +
  30498. + if (dwc_otg_is_device_mode(core_if)) {
  30499. + dctl_data_t dctl = {.d32 = 0 };
  30500. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  30501. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  30502. + dsts));
  30503. + if (core_if->lx_state == DWC_OTG_L2) {
  30504. +#ifdef PARTIAL_POWER_DOWN
  30505. + if (core_if->hwcfg4.b.power_optimiz) {
  30506. + pcgcctl_data_t power = {.d32 = 0 };
  30507. +
  30508. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  30509. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  30510. + power.d32);
  30511. +
  30512. + power.b.stoppclk = 0;
  30513. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30514. +
  30515. + power.b.pwrclmp = 0;
  30516. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30517. +
  30518. + power.b.rstpdwnmodule = 0;
  30519. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30520. + }
  30521. +#endif
  30522. + /* Clear the Remote Wakeup Signaling */
  30523. + dctl.b.rmtwkupsig = 1;
  30524. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  30525. + dctl, dctl.d32, 0);
  30526. +
  30527. + DWC_SPINUNLOCK(core_if->lock);
  30528. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  30529. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  30530. + }
  30531. + DWC_SPINLOCK(core_if->lock);
  30532. + } else {
  30533. + glpmcfg_data_t lpmcfg;
  30534. + lpmcfg.d32 =
  30535. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  30536. + lpmcfg.b.hird_thres &= (~(1 << 4));
  30537. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  30538. + lpmcfg.d32);
  30539. + }
  30540. + /** Change to L0 state*/
  30541. + core_if->lx_state = DWC_OTG_L0;
  30542. + } else {
  30543. + if (core_if->lx_state != DWC_OTG_L1) {
  30544. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30545. +
  30546. + /* Restart the Phy Clock */
  30547. + pcgcctl.b.stoppclk = 1;
  30548. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  30549. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  30550. + } else {
  30551. + /** Change to L0 state*/
  30552. + core_if->lx_state = DWC_OTG_L0;
  30553. + }
  30554. + }
  30555. +
  30556. + /* Clear interrupt */
  30557. + gintsts.d32 = 0;
  30558. + gintsts.b.wkupintr = 1;
  30559. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30560. +
  30561. + return 1;
  30562. +}
  30563. +
  30564. +/**
  30565. + * This interrupt indicates that the Wakeup Logic has detected a
  30566. + * Device disconnect.
  30567. + */
  30568. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  30569. +{
  30570. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  30571. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  30572. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30573. +
  30574. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30575. +
  30576. + if (!core_if->hibernation_suspend) {
  30577. + DWC_PRINTF("Already exited from Hibernation\n");
  30578. + return 1;
  30579. + }
  30580. +
  30581. + /* Switch on the voltage to the core */
  30582. + gpwrdn.b.pwrdnswtch = 1;
  30583. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30584. + dwc_udelay(10);
  30585. +
  30586. + /* Reset the core */
  30587. + gpwrdn.d32 = 0;
  30588. + gpwrdn.b.pwrdnrstn = 1;
  30589. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30590. + dwc_udelay(10);
  30591. +
  30592. + /* Disable power clamps*/
  30593. + gpwrdn.d32 = 0;
  30594. + gpwrdn.b.pwrdnclmp = 1;
  30595. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30596. +
  30597. + /* Remove reset the core signal */
  30598. + gpwrdn.d32 = 0;
  30599. + gpwrdn.b.pwrdnrstn = 1;
  30600. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30601. + dwc_udelay(10);
  30602. +
  30603. + /* Disable PMU interrupt */
  30604. + gpwrdn.d32 = 0;
  30605. + gpwrdn.b.pmuintsel = 1;
  30606. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30607. +
  30608. + core_if->hibernation_suspend = 0;
  30609. +
  30610. + /* Disable PMU */
  30611. + gpwrdn.d32 = 0;
  30612. + gpwrdn.b.pmuactv = 1;
  30613. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30614. + dwc_udelay(10);
  30615. +
  30616. + if (gpwrdn_temp.b.idsts) {
  30617. + core_if->op_state = B_PERIPHERAL;
  30618. + dwc_otg_core_init(core_if);
  30619. + dwc_otg_enable_global_interrupts(core_if);
  30620. + cil_pcd_start(core_if);
  30621. + } else {
  30622. + core_if->op_state = A_HOST;
  30623. + dwc_otg_core_init(core_if);
  30624. + dwc_otg_enable_global_interrupts(core_if);
  30625. + cil_hcd_start(core_if);
  30626. + }
  30627. +
  30628. + return 1;
  30629. +}
  30630. +
  30631. +/**
  30632. + * This interrupt indicates that the Wakeup Logic has detected a
  30633. + * remote wakeup sequence.
  30634. + */
  30635. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  30636. +{
  30637. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30638. + DWC_DEBUGPL(DBG_ANY,
  30639. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  30640. +
  30641. + if (!core_if->hibernation_suspend) {
  30642. + DWC_PRINTF("Already exited from Hibernation\n");
  30643. + return 1;
  30644. + }
  30645. +
  30646. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30647. + if (gpwrdn.b.idsts) { // Device Mode
  30648. + if ((core_if->power_down == 2)
  30649. + && (core_if->hibernation_suspend == 1)) {
  30650. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  30651. + }
  30652. + } else {
  30653. + if ((core_if->power_down == 2)
  30654. + && (core_if->hibernation_suspend == 1)) {
  30655. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  30656. + }
  30657. + }
  30658. + return 1;
  30659. +}
  30660. +
  30661. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  30662. +{
  30663. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30664. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  30665. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  30666. +
  30667. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30668. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30669. + if (core_if->power_down == 2) {
  30670. + if (!core_if->hibernation_suspend) {
  30671. + DWC_PRINTF("Already exited from Hibernation\n");
  30672. + return 1;
  30673. + }
  30674. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  30675. + /* Switch on the voltage to the core */
  30676. + gpwrdn.b.pwrdnswtch = 1;
  30677. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30678. + dwc_udelay(10);
  30679. +
  30680. + /* Reset the core */
  30681. + gpwrdn.d32 = 0;
  30682. + gpwrdn.b.pwrdnrstn = 1;
  30683. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30684. + dwc_udelay(10);
  30685. +
  30686. + /* Disable power clamps */
  30687. + gpwrdn.d32 = 0;
  30688. + gpwrdn.b.pwrdnclmp = 1;
  30689. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30690. +
  30691. + /* Remove reset the core signal */
  30692. + gpwrdn.d32 = 0;
  30693. + gpwrdn.b.pwrdnrstn = 1;
  30694. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30695. + dwc_udelay(10);
  30696. +
  30697. + /* Disable PMU interrupt */
  30698. + gpwrdn.d32 = 0;
  30699. + gpwrdn.b.pmuintsel = 1;
  30700. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30701. +
  30702. + /*Indicates that we are exiting from hibernation */
  30703. + core_if->hibernation_suspend = 0;
  30704. +
  30705. + /* Disable PMU */
  30706. + gpwrdn.d32 = 0;
  30707. + gpwrdn.b.pmuactv = 1;
  30708. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30709. + dwc_udelay(10);
  30710. +
  30711. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  30712. + if (gpwrdn.b.dis_vbus == 1) {
  30713. + gpwrdn.d32 = 0;
  30714. + gpwrdn.b.dis_vbus = 1;
  30715. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30716. + }
  30717. +
  30718. + if (gpwrdn_temp.b.idsts) {
  30719. + core_if->op_state = B_PERIPHERAL;
  30720. + dwc_otg_core_init(core_if);
  30721. + dwc_otg_enable_global_interrupts(core_if);
  30722. + cil_pcd_start(core_if);
  30723. + } else {
  30724. + core_if->op_state = A_HOST;
  30725. + dwc_otg_core_init(core_if);
  30726. + dwc_otg_enable_global_interrupts(core_if);
  30727. + cil_hcd_start(core_if);
  30728. + }
  30729. + }
  30730. +
  30731. + if (core_if->adp_enable) {
  30732. + uint8_t is_host = 0;
  30733. + DWC_SPINUNLOCK(core_if->lock);
  30734. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  30735. +#ifndef DWC_HOST_ONLY
  30736. + if (gpwrdn_temp.b.idsts)
  30737. + core_if->lock = otg_dev->pcd->lock;
  30738. +#endif
  30739. +#ifndef DWC_DEVICE_ONLY
  30740. + if (!gpwrdn_temp.b.idsts) {
  30741. + core_if->lock = otg_dev->hcd->lock;
  30742. + is_host = 1;
  30743. + }
  30744. +#endif
  30745. + DWC_PRINTF("RESTART ADP\n");
  30746. + if (core_if->adp.probe_enabled)
  30747. + dwc_otg_adp_probe_stop(core_if);
  30748. + if (core_if->adp.sense_enabled)
  30749. + dwc_otg_adp_sense_stop(core_if);
  30750. + if (core_if->adp.sense_timer_started)
  30751. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  30752. + if (core_if->adp.vbuson_timer_started)
  30753. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  30754. + core_if->adp.probe_timer_values[0] = -1;
  30755. + core_if->adp.probe_timer_values[1] = -1;
  30756. + core_if->adp.sense_timer_started = 0;
  30757. + core_if->adp.vbuson_timer_started = 0;
  30758. + core_if->adp.probe_counter = 0;
  30759. + core_if->adp.gpwrdn = 0;
  30760. +
  30761. + /* Disable PMU and restart ADP */
  30762. + gpwrdn_temp.d32 = 0;
  30763. + gpwrdn_temp.b.pmuactv = 1;
  30764. + gpwrdn_temp.b.pmuintsel = 1;
  30765. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30766. + DWC_PRINTF("Check point 1\n");
  30767. + dwc_mdelay(110);
  30768. + dwc_otg_adp_start(core_if, is_host);
  30769. + DWC_SPINLOCK(core_if->lock);
  30770. + }
  30771. +
  30772. +
  30773. + return 1;
  30774. +}
  30775. +
  30776. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  30777. +{
  30778. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30779. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  30780. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30781. +
  30782. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30783. + if (core_if->power_down == 2) {
  30784. + if (!core_if->hibernation_suspend) {
  30785. + DWC_PRINTF("Already exited from Hibernation\n");
  30786. + return 1;
  30787. + }
  30788. +
  30789. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  30790. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  30791. + gpwrdn.b.bsessvld == 0) {
  30792. + /* Save gpwrdn register for further usage if stschng interrupt */
  30793. + core_if->gr_backup->gpwrdn_local =
  30794. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30795. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  30796. + return 1;
  30797. + }
  30798. +
  30799. + /* Switch on the voltage to the core */
  30800. + gpwrdn.d32 = 0;
  30801. + gpwrdn.b.pwrdnswtch = 1;
  30802. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30803. + dwc_udelay(10);
  30804. +
  30805. + /* Reset the core */
  30806. + gpwrdn.d32 = 0;
  30807. + gpwrdn.b.pwrdnrstn = 1;
  30808. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30809. + dwc_udelay(10);
  30810. +
  30811. + /* Disable power clamps */
  30812. + gpwrdn.d32 = 0;
  30813. + gpwrdn.b.pwrdnclmp = 1;
  30814. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30815. +
  30816. + /* Remove reset the core signal */
  30817. + gpwrdn.d32 = 0;
  30818. + gpwrdn.b.pwrdnrstn = 1;
  30819. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30820. + dwc_udelay(10);
  30821. +
  30822. + /* Disable PMU interrupt */
  30823. + gpwrdn.d32 = 0;
  30824. + gpwrdn.b.pmuintsel = 1;
  30825. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30826. + dwc_udelay(10);
  30827. +
  30828. + /*Indicates that we are exiting from hibernation */
  30829. + core_if->hibernation_suspend = 0;
  30830. +
  30831. + /* Disable PMU */
  30832. + gpwrdn.d32 = 0;
  30833. + gpwrdn.b.pmuactv = 1;
  30834. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30835. + dwc_udelay(10);
  30836. +
  30837. + core_if->op_state = B_PERIPHERAL;
  30838. + dwc_otg_core_init(core_if);
  30839. + dwc_otg_enable_global_interrupts(core_if);
  30840. + cil_pcd_start(core_if);
  30841. +
  30842. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  30843. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  30844. + /*
  30845. + * Initiate SRP after initial ADP probe.
  30846. + */
  30847. + dwc_otg_initiate_srp(core_if);
  30848. + }
  30849. + }
  30850. +
  30851. + return 1;
  30852. +}
  30853. +/**
  30854. + * This interrupt indicates that the Wakeup Logic has detected a
  30855. + * status change either on IDDIG or BSessVld.
  30856. + */
  30857. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  30858. +{
  30859. + int retval;
  30860. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30861. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  30862. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  30863. +
  30864. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30865. +
  30866. + if (core_if->power_down == 2) {
  30867. + if (core_if->hibernation_suspend <= 0) {
  30868. + DWC_PRINTF("Already exited from Hibernation\n");
  30869. + return 1;
  30870. + } else
  30871. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  30872. +
  30873. + } else {
  30874. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  30875. + }
  30876. +
  30877. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30878. +
  30879. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  30880. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  30881. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  30882. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  30883. + }
  30884. +
  30885. + return retval;
  30886. +}
  30887. +
  30888. +/**
  30889. + * This interrupt indicates that the Wakeup Logic has detected a
  30890. + * SRP.
  30891. + */
  30892. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  30893. +{
  30894. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30895. +
  30896. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30897. +
  30898. + if (!core_if->hibernation_suspend) {
  30899. + DWC_PRINTF("Already exited from Hibernation\n");
  30900. + return 1;
  30901. + }
  30902. +#ifdef DWC_DEV_SRPCAP
  30903. + if (core_if->pwron_timer_started) {
  30904. + core_if->pwron_timer_started = 0;
  30905. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  30906. + }
  30907. +#endif
  30908. +
  30909. + /* Switch on the voltage to the core */
  30910. + gpwrdn.b.pwrdnswtch = 1;
  30911. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30912. + dwc_udelay(10);
  30913. +
  30914. + /* Reset the core */
  30915. + gpwrdn.d32 = 0;
  30916. + gpwrdn.b.pwrdnrstn = 1;
  30917. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30918. + dwc_udelay(10);
  30919. +
  30920. + /* Disable power clamps */
  30921. + gpwrdn.d32 = 0;
  30922. + gpwrdn.b.pwrdnclmp = 1;
  30923. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30924. +
  30925. + /* Remove reset the core signal */
  30926. + gpwrdn.d32 = 0;
  30927. + gpwrdn.b.pwrdnrstn = 1;
  30928. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30929. + dwc_udelay(10);
  30930. +
  30931. + /* Disable PMU interrupt */
  30932. + gpwrdn.d32 = 0;
  30933. + gpwrdn.b.pmuintsel = 1;
  30934. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30935. +
  30936. + /* Indicates that we are exiting from hibernation */
  30937. + core_if->hibernation_suspend = 0;
  30938. +
  30939. + /* Disable PMU */
  30940. + gpwrdn.d32 = 0;
  30941. + gpwrdn.b.pmuactv = 1;
  30942. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30943. + dwc_udelay(10);
  30944. +
  30945. + /* Programm Disable VBUS to 0 */
  30946. + gpwrdn.d32 = 0;
  30947. + gpwrdn.b.dis_vbus = 1;
  30948. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30949. +
  30950. + /*Initialize the core as Host */
  30951. + core_if->op_state = A_HOST;
  30952. + dwc_otg_core_init(core_if);
  30953. + dwc_otg_enable_global_interrupts(core_if);
  30954. + cil_hcd_start(core_if);
  30955. +
  30956. + return 1;
  30957. +}
  30958. +
  30959. +/** This interrupt indicates that restore command after Hibernation
  30960. + * was completed by the core. */
  30961. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  30962. +{
  30963. + pcgcctl_data_t pcgcctl;
  30964. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  30965. +
  30966. + //TODO De-assert restore signal. 8.a
  30967. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  30968. + if (pcgcctl.b.restoremode == 1) {
  30969. + gintmsk_data_t gintmsk = {.d32 = 0 };
  30970. + /*
  30971. + * If restore mode is Remote Wakeup,
  30972. + * unmask Remote Wakeup interrupt.
  30973. + */
  30974. + gintmsk.b.wkupintr = 1;
  30975. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  30976. + 0, gintmsk.d32);
  30977. + }
  30978. +
  30979. + return 1;
  30980. +}
  30981. +
  30982. +/**
  30983. + * This interrupt indicates that a device has been disconnected from
  30984. + * the root port.
  30985. + */
  30986. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  30987. +{
  30988. + gintsts_data_t gintsts;
  30989. +
  30990. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  30991. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  30992. + op_state_str(core_if));
  30993. +
  30994. +/** @todo Consolidate this if statement. */
  30995. +#ifndef DWC_HOST_ONLY
  30996. + if (core_if->op_state == B_HOST) {
  30997. + /* If in device mode Disconnect and stop the HCD, then
  30998. + * start the PCD. */
  30999. + DWC_SPINUNLOCK(core_if->lock);
  31000. + cil_hcd_disconnect(core_if);
  31001. + cil_pcd_start(core_if);
  31002. + DWC_SPINLOCK(core_if->lock);
  31003. + core_if->op_state = B_PERIPHERAL;
  31004. + } else if (dwc_otg_is_device_mode(core_if)) {
  31005. + gotgctl_data_t gotgctl = {.d32 = 0 };
  31006. + gotgctl.d32 =
  31007. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  31008. + if (gotgctl.b.hstsethnpen == 1) {
  31009. + /* Do nothing, if HNP in process the OTG
  31010. + * interrupt "Host Negotiation Detected"
  31011. + * interrupt will do the mode switch.
  31012. + */
  31013. + } else if (gotgctl.b.devhnpen == 0) {
  31014. + /* If in device mode Disconnect and stop the HCD, then
  31015. + * start the PCD. */
  31016. + DWC_SPINUNLOCK(core_if->lock);
  31017. + cil_hcd_disconnect(core_if);
  31018. + cil_pcd_start(core_if);
  31019. + DWC_SPINLOCK(core_if->lock);
  31020. + core_if->op_state = B_PERIPHERAL;
  31021. + } else {
  31022. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  31023. + }
  31024. + } else {
  31025. + if (core_if->op_state == A_HOST) {
  31026. + /* A-Cable still connected but device disconnected. */
  31027. + DWC_SPINUNLOCK(core_if->lock);
  31028. + cil_hcd_disconnect(core_if);
  31029. + DWC_SPINLOCK(core_if->lock);
  31030. + if (core_if->adp_enable) {
  31031. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  31032. + cil_hcd_stop(core_if);
  31033. + /* Enable Power Down Logic */
  31034. + gpwrdn.b.pmuintsel = 1;
  31035. + gpwrdn.b.pmuactv = 1;
  31036. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31037. + gpwrdn, 0, gpwrdn.d32);
  31038. + dwc_otg_adp_probe_start(core_if);
  31039. +
  31040. + /* Power off the core */
  31041. + if (core_if->power_down == 2) {
  31042. + gpwrdn.d32 = 0;
  31043. + gpwrdn.b.pwrdnswtch = 1;
  31044. + DWC_MODIFY_REG32
  31045. + (&core_if->core_global_regs->gpwrdn,
  31046. + gpwrdn.d32, 0);
  31047. + }
  31048. + }
  31049. + }
  31050. + }
  31051. +#endif
  31052. + /* Change to L3(OFF) state */
  31053. + core_if->lx_state = DWC_OTG_L3;
  31054. +
  31055. + gintsts.d32 = 0;
  31056. + gintsts.b.disconnect = 1;
  31057. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  31058. + return 1;
  31059. +}
  31060. +
  31061. +/**
  31062. + * This interrupt indicates that SUSPEND state has been detected on
  31063. + * the USB.
  31064. + *
  31065. + * For HNP the USB Suspend interrupt signals the change from
  31066. + * "a_peripheral" to "a_host".
  31067. + *
  31068. + * When power management is enabled the core will be put in low power
  31069. + * mode.
  31070. + */
  31071. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  31072. +{
  31073. + dsts_data_t dsts;
  31074. + gintsts_data_t gintsts;
  31075. + dcfg_data_t dcfg;
  31076. +
  31077. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  31078. +
  31079. + if (dwc_otg_is_device_mode(core_if)) {
  31080. + /* Check the Device status register to determine if the Suspend
  31081. + * state is active. */
  31082. + dsts.d32 =
  31083. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  31084. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  31085. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  31086. + "HWCFG4.power Optimize=%d\n",
  31087. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  31088. +
  31089. +#ifdef PARTIAL_POWER_DOWN
  31090. +/** @todo Add a module parameter for power management. */
  31091. +
  31092. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  31093. + pcgcctl_data_t power = {.d32 = 0 };
  31094. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  31095. +
  31096. + power.b.pwrclmp = 1;
  31097. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  31098. +
  31099. + power.b.rstpdwnmodule = 1;
  31100. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  31101. +
  31102. + power.b.stoppclk = 1;
  31103. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  31104. +
  31105. + } else {
  31106. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  31107. + }
  31108. +#endif
  31109. + /* PCD callback for suspend. Release the lock inside of callback function */
  31110. + cil_pcd_suspend(core_if);
  31111. + if (core_if->power_down == 2)
  31112. + {
  31113. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  31114. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  31115. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  31116. +
  31117. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  31118. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31119. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31120. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  31121. +
  31122. + /* Change to L2(suspend) state */
  31123. + core_if->lx_state = DWC_OTG_L2;
  31124. +
  31125. + /* Clear interrupt in gintsts */
  31126. + gintsts.d32 = 0;
  31127. + gintsts.b.usbsuspend = 1;
  31128. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31129. + gintsts, gintsts.d32);
  31130. + DWC_PRINTF("Start of hibernation completed\n");
  31131. + dwc_otg_save_global_regs(core_if);
  31132. + dwc_otg_save_dev_regs(core_if);
  31133. +
  31134. + gusbcfg.d32 =
  31135. + DWC_READ_REG32(&core_if->core_global_regs->
  31136. + gusbcfg);
  31137. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  31138. + /* ULPI interface */
  31139. + /* Suspend the Phy Clock */
  31140. + pcgcctl.d32 = 0;
  31141. + pcgcctl.b.stoppclk = 1;
  31142. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  31143. + pcgcctl.d32);
  31144. + dwc_udelay(10);
  31145. + gpwrdn.b.pmuactv = 1;
  31146. + DWC_MODIFY_REG32(&core_if->
  31147. + core_global_regs->
  31148. + gpwrdn, 0, gpwrdn.d32);
  31149. + } else {
  31150. + /* UTMI+ Interface */
  31151. + gpwrdn.b.pmuactv = 1;
  31152. + DWC_MODIFY_REG32(&core_if->
  31153. + core_global_regs->
  31154. + gpwrdn, 0, gpwrdn.d32);
  31155. + dwc_udelay(10);
  31156. + pcgcctl.b.stoppclk = 1;
  31157. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  31158. + pcgcctl.d32);
  31159. + dwc_udelay(10);
  31160. + }
  31161. +
  31162. + /* Set flag to indicate that we are in hibernation */
  31163. + core_if->hibernation_suspend = 1;
  31164. + /* Enable interrupts from wake up logic */
  31165. + gpwrdn.d32 = 0;
  31166. + gpwrdn.b.pmuintsel = 1;
  31167. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31168. + gpwrdn, 0, gpwrdn.d32);
  31169. + dwc_udelay(10);
  31170. +
  31171. + /* Unmask device mode interrupts in GPWRDN */
  31172. + gpwrdn.d32 = 0;
  31173. + gpwrdn.b.rst_det_msk = 1;
  31174. + gpwrdn.b.lnstchng_msk = 1;
  31175. + gpwrdn.b.sts_chngint_msk = 1;
  31176. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31177. + gpwrdn, 0, gpwrdn.d32);
  31178. + dwc_udelay(10);
  31179. +
  31180. + /* Enable Power Down Clamp */
  31181. + gpwrdn.d32 = 0;
  31182. + gpwrdn.b.pwrdnclmp = 1;
  31183. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31184. + gpwrdn, 0, gpwrdn.d32);
  31185. + dwc_udelay(10);
  31186. +
  31187. + /* Switch off VDD */
  31188. + gpwrdn.d32 = 0;
  31189. + gpwrdn.b.pwrdnswtch = 1;
  31190. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31191. + gpwrdn, 0, gpwrdn.d32);
  31192. +
  31193. + /* Save gpwrdn register for further usage if stschng interrupt */
  31194. + core_if->gr_backup->gpwrdn_local =
  31195. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31196. + DWC_PRINTF("Hibernation completed\n");
  31197. +
  31198. + return 1;
  31199. + }
  31200. + } else if (core_if->power_down == 3) {
  31201. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31202. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  31203. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  31204. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  31205. +
  31206. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  31207. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  31208. + core_if->xhib = 1;
  31209. +
  31210. + /* Clear interrupt in gintsts */
  31211. + gintsts.d32 = 0;
  31212. + gintsts.b.usbsuspend = 1;
  31213. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31214. + gintsts, gintsts.d32);
  31215. +
  31216. + dwc_otg_save_global_regs(core_if);
  31217. + dwc_otg_save_dev_regs(core_if);
  31218. +
  31219. + /* Wait for 10 PHY clocks */
  31220. + dwc_udelay(10);
  31221. +
  31222. + /* Program GPIO register while entering to xHib */
  31223. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  31224. +
  31225. + pcgcctl.b.enbl_extnd_hiber = 1;
  31226. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31227. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31228. +
  31229. + pcgcctl.d32 = 0;
  31230. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  31231. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31232. +
  31233. + pcgcctl.d32 = 0;
  31234. + pcgcctl.b.extnd_hiber_switch = 1;
  31235. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31236. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  31237. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31238. +
  31239. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  31240. +
  31241. + return 1;
  31242. + }
  31243. + }
  31244. + } else {
  31245. + if (core_if->op_state == A_PERIPHERAL) {
  31246. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  31247. + /* Clear the a_peripheral flag, back to a_host. */
  31248. + DWC_SPINUNLOCK(core_if->lock);
  31249. + cil_pcd_stop(core_if);
  31250. + cil_hcd_start(core_if);
  31251. + DWC_SPINLOCK(core_if->lock);
  31252. + core_if->op_state = A_HOST;
  31253. + }
  31254. + }
  31255. +
  31256. + /* Change to L2(suspend) state */
  31257. + core_if->lx_state = DWC_OTG_L2;
  31258. +
  31259. + /* Clear interrupt */
  31260. + gintsts.d32 = 0;
  31261. + gintsts.b.usbsuspend = 1;
  31262. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  31263. +
  31264. + return 1;
  31265. +}
  31266. +
  31267. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  31268. +{
  31269. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31270. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31271. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  31272. +
  31273. + dwc_udelay(10);
  31274. +
  31275. + /* Program GPIO register while entering to xHib */
  31276. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  31277. +
  31278. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  31279. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  31280. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31281. + dwc_udelay(10);
  31282. +
  31283. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  31284. + gpwrdn.b.restore = 1;
  31285. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  31286. + dwc_udelay(10);
  31287. +
  31288. + restore_lpm_i2c_regs(core_if);
  31289. +
  31290. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31291. + pcgcctl.b.max_xcvrselect = 1;
  31292. + pcgcctl.b.ess_reg_restored = 0;
  31293. + pcgcctl.b.extnd_hiber_switch = 0;
  31294. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  31295. + pcgcctl.b.enbl_extnd_hiber = 1;
  31296. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31297. +
  31298. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  31299. + gahbcfg.b.glblintrmsk = 1;
  31300. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  31301. +
  31302. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  31303. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  31304. +
  31305. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  31306. + core_if->gr_backup->gusbcfg_local);
  31307. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  31308. + core_if->dr_backup->dcfg);
  31309. +
  31310. + pcgcctl.d32 = 0;
  31311. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31312. + pcgcctl.b.max_xcvrselect = 1;
  31313. + pcgcctl.d32 |= 0x608;
  31314. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31315. + dwc_udelay(10);
  31316. +
  31317. + pcgcctl.d32 = 0;
  31318. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31319. + pcgcctl.b.max_xcvrselect = 1;
  31320. + pcgcctl.b.ess_reg_restored = 1;
  31321. + pcgcctl.b.enbl_extnd_hiber = 1;
  31322. + pcgcctl.b.rstpdwnmodule = 1;
  31323. + pcgcctl.b.restoremode = 1;
  31324. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31325. +
  31326. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  31327. +
  31328. + return 1;
  31329. +}
  31330. +
  31331. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31332. +/**
  31333. + * This function hadles LPM transaction received interrupt.
  31334. + */
  31335. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  31336. +{
  31337. + glpmcfg_data_t lpmcfg;
  31338. + gintsts_data_t gintsts;
  31339. +
  31340. + if (!core_if->core_params->lpm_enable) {
  31341. + DWC_PRINTF("Unexpected LPM interrupt\n");
  31342. + }
  31343. +
  31344. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  31345. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  31346. +
  31347. + if (dwc_otg_is_host_mode(core_if)) {
  31348. + cil_hcd_sleep(core_if);
  31349. + } else {
  31350. + lpmcfg.b.hird_thres |= (1 << 4);
  31351. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  31352. + lpmcfg.d32);
  31353. + }
  31354. +
  31355. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  31356. + dwc_udelay(10);
  31357. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  31358. + if (lpmcfg.b.prt_sleep_sts) {
  31359. + /* Save the current state */
  31360. + core_if->lx_state = DWC_OTG_L1;
  31361. + }
  31362. +
  31363. + /* Clear interrupt */
  31364. + gintsts.d32 = 0;
  31365. + gintsts.b.lpmtranrcvd = 1;
  31366. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  31367. + return 1;
  31368. +}
  31369. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  31370. +
  31371. +/**
  31372. + * This function returns the Core Interrupt register.
  31373. + */
  31374. +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if, gintmsk_data_t *reenable_gintmsk, dwc_otg_hcd_t *hcd)
  31375. +{
  31376. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  31377. + gintsts_data_t gintsts;
  31378. + gintmsk_data_t gintmsk;
  31379. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  31380. + gintmsk_common.b.wkupintr = 1;
  31381. + gintmsk_common.b.sessreqintr = 1;
  31382. + gintmsk_common.b.conidstschng = 1;
  31383. + gintmsk_common.b.otgintr = 1;
  31384. + gintmsk_common.b.modemismatch = 1;
  31385. + gintmsk_common.b.disconnect = 1;
  31386. + gintmsk_common.b.usbsuspend = 1;
  31387. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31388. + gintmsk_common.b.lpmtranrcvd = 1;
  31389. +#endif
  31390. + gintmsk_common.b.restoredone = 1;
  31391. + if(dwc_otg_is_device_mode(core_if))
  31392. + {
  31393. + /** @todo: The port interrupt occurs while in device
  31394. + * mode. Added code to CIL to clear the interrupt for now!
  31395. + */
  31396. + gintmsk_common.b.portintr = 1;
  31397. + }
  31398. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  31399. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  31400. + if(fiq_enable) {
  31401. + local_fiq_disable();
  31402. + /* Pull in the interrupts that the FIQ has masked */
  31403. + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
  31404. + gintmsk.d32 |= gintmsk_common.d32;
  31405. + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
  31406. + reenable_gintmsk->d32 = gintmsk.d32;
  31407. + local_fiq_enable();
  31408. + }
  31409. +
  31410. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  31411. +
  31412. +#ifdef DEBUG
  31413. + /* if any common interrupts set */
  31414. + if (gintsts.d32 & gintmsk_common.d32) {
  31415. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  31416. + gintsts.d32, gintmsk.d32);
  31417. + }
  31418. +#endif
  31419. + if (!fiq_enable){
  31420. + if (gahbcfg.b.glblintrmsk)
  31421. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  31422. + else
  31423. + return 0;
  31424. + } else {
  31425. + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
  31426. + * Can't trust the global interrupt mask bit in this case.
  31427. + */
  31428. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  31429. + }
  31430. +
  31431. +}
  31432. +
  31433. +/* MACRO for clearing interupt bits in GPWRDN register */
  31434. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  31435. +do { \
  31436. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  31437. + gpwrdn.b.__intr = 1; \
  31438. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  31439. + 0, gpwrdn.d32); \
  31440. +} while (0)
  31441. +
  31442. +/**
  31443. + * Common interrupt handler.
  31444. + *
  31445. + * The common interrupts are those that occur in both Host and Device mode.
  31446. + * This handler handles the following interrupts:
  31447. + * - Mode Mismatch Interrupt
  31448. + * - Disconnect Interrupt
  31449. + * - OTG Interrupt
  31450. + * - Connector ID Status Change Interrupt
  31451. + * - Session Request Interrupt.
  31452. + * - Resume / Remote Wakeup Detected Interrupt.
  31453. + * - LPM Transaction Received Interrupt
  31454. + * - ADP Transaction Received Interrupt
  31455. + *
  31456. + */
  31457. +int32_t dwc_otg_handle_common_intr(void *dev)
  31458. +{
  31459. + int retval = 0;
  31460. + gintsts_data_t gintsts;
  31461. + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
  31462. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31463. + dwc_otg_device_t *otg_dev = dev;
  31464. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  31465. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31466. + if (dwc_otg_is_device_mode(core_if))
  31467. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  31468. +
  31469. + if (core_if->lock)
  31470. + DWC_SPINLOCK(core_if->lock);
  31471. +
  31472. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  31473. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  31474. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  31475. + core_if->xhib = 2;
  31476. + if (core_if->lock)
  31477. + DWC_SPINUNLOCK(core_if->lock);
  31478. +
  31479. + return retval;
  31480. + }
  31481. +
  31482. + if (core_if->hibernation_suspend <= 0) {
  31483. + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
  31484. + * of this handler - god only knows why it's done like this
  31485. + */
  31486. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
  31487. +
  31488. + if (gintsts.b.modemismatch) {
  31489. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  31490. + }
  31491. + if (gintsts.b.otgintr) {
  31492. + retval |= dwc_otg_handle_otg_intr(core_if);
  31493. + }
  31494. + if (gintsts.b.conidstschng) {
  31495. + retval |=
  31496. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  31497. + }
  31498. + if (gintsts.b.disconnect) {
  31499. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  31500. + }
  31501. + if (gintsts.b.sessreqintr) {
  31502. + retval |= dwc_otg_handle_session_req_intr(core_if);
  31503. + }
  31504. + if (gintsts.b.wkupintr) {
  31505. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  31506. + }
  31507. + if (gintsts.b.usbsuspend) {
  31508. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  31509. + }
  31510. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31511. + if (gintsts.b.lpmtranrcvd) {
  31512. + retval |= dwc_otg_handle_lpm_intr(core_if);
  31513. + }
  31514. +#endif
  31515. + if (gintsts.b.restoredone) {
  31516. + gintsts.d32 = 0;
  31517. + if (core_if->power_down == 2)
  31518. + core_if->hibernation_suspend = -1;
  31519. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  31520. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31521. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31522. + dctl_data_t dctl = {.d32 = 0 };
  31523. +
  31524. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31525. + gintsts, 0xFFFFFFFF);
  31526. +
  31527. + DWC_DEBUGPL(DBG_ANY,
  31528. + "RESTORE DONE generated\n");
  31529. +
  31530. + gpwrdn.b.restore = 1;
  31531. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  31532. + dwc_udelay(10);
  31533. +
  31534. + pcgcctl.b.rstpdwnmodule = 1;
  31535. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  31536. +
  31537. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  31538. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  31539. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  31540. + dwc_udelay(50);
  31541. +
  31542. + dctl.b.pwronprgdone = 1;
  31543. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  31544. + dwc_udelay(10);
  31545. +
  31546. + dwc_otg_restore_global_regs(core_if);
  31547. + dwc_otg_restore_dev_regs(core_if, 0);
  31548. +
  31549. + dctl.d32 = 0;
  31550. + dctl.b.pwronprgdone = 1;
  31551. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  31552. + dwc_udelay(10);
  31553. +
  31554. + pcgcctl.d32 = 0;
  31555. + pcgcctl.b.enbl_extnd_hiber = 1;
  31556. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  31557. +
  31558. + /* The core will be in ON STATE */
  31559. + core_if->lx_state = DWC_OTG_L0;
  31560. + core_if->xhib = 0;
  31561. +
  31562. + DWC_SPINUNLOCK(core_if->lock);
  31563. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  31564. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  31565. + }
  31566. + DWC_SPINLOCK(core_if->lock);
  31567. +
  31568. + }
  31569. +
  31570. + gintsts.b.restoredone = 1;
  31571. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  31572. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  31573. + retval |= 1;
  31574. + }
  31575. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  31576. + /* The port interrupt occurs while in device mode with HPRT0
  31577. + * Port Enable/Disable.
  31578. + */
  31579. + gintsts.d32 = 0;
  31580. + gintsts.b.portintr = 1;
  31581. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  31582. + retval |= 1;
  31583. + gintmsk_reenable.b.portintr = 1;
  31584. +
  31585. + }
  31586. + /* Did we actually handle anything? if so, unmask the interrupt */
  31587. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
  31588. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
  31589. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
  31590. + if (retval && fiq_enable) {
  31591. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
  31592. + }
  31593. +
  31594. + } else {
  31595. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  31596. +
  31597. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  31598. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  31599. + if (gpwrdn.b.linestate == 0) {
  31600. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  31601. + } else {
  31602. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  31603. + }
  31604. +
  31605. + retval |= 1;
  31606. + }
  31607. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  31608. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  31609. + /* remote wakeup from hibernation */
  31610. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  31611. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  31612. + } else {
  31613. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  31614. + }
  31615. + retval |= 1;
  31616. + }
  31617. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  31618. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  31619. + if (gpwrdn.b.linestate == 0) {
  31620. + DWC_PRINTF("Reset detected\n");
  31621. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  31622. + }
  31623. + }
  31624. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  31625. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  31626. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  31627. + retval |= 1;
  31628. + }
  31629. + }
  31630. + /* Handle ADP interrupt here */
  31631. + if (gpwrdn.b.adp_int) {
  31632. + DWC_PRINTF("ADP interrupt\n");
  31633. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  31634. + dwc_otg_adp_handle_intr(core_if);
  31635. + retval |= 1;
  31636. + }
  31637. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  31638. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  31639. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  31640. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  31641. +
  31642. + retval |= 1;
  31643. + }
  31644. + if (core_if->lock)
  31645. + DWC_SPINUNLOCK(core_if->lock);
  31646. + return retval;
  31647. +}
  31648. --- /dev/null
  31649. +++ b/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  31650. @@ -0,0 +1,705 @@
  31651. +/* ==========================================================================
  31652. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  31653. + * $Revision: #13 $
  31654. + * $Date: 2012/08/10 $
  31655. + * $Change: 2047372 $
  31656. + *
  31657. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  31658. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  31659. + * otherwise expressly agreed to in writing between Synopsys and you.
  31660. + *
  31661. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  31662. + * any End User Software License Agreement or Agreement for Licensed Product
  31663. + * with Synopsys or any supplement thereto. You are permitted to use and
  31664. + * redistribute this Software in source and binary forms, with or without
  31665. + * modification, provided that redistributions of source code must retain this
  31666. + * notice. You may not view, use, disclose, copy or distribute this file or
  31667. + * any information contained herein except pursuant to this license grant from
  31668. + * Synopsys. If you do not agree with this notice, including the disclaimer
  31669. + * below, then you are not authorized to use the Software.
  31670. + *
  31671. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  31672. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31673. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31674. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  31675. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31676. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31677. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31678. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31679. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31680. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31681. + * DAMAGE.
  31682. + * ========================================================================== */
  31683. +#if !defined(__DWC_CORE_IF_H__)
  31684. +#define __DWC_CORE_IF_H__
  31685. +
  31686. +#include "dwc_os.h"
  31687. +
  31688. +/** @file
  31689. + * This file defines DWC_OTG Core API
  31690. + */
  31691. +
  31692. +struct dwc_otg_core_if;
  31693. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  31694. +
  31695. +/** Maximum number of Periodic FIFOs */
  31696. +#define MAX_PERIO_FIFOS 15
  31697. +/** Maximum number of Periodic FIFOs */
  31698. +#define MAX_TX_FIFOS 15
  31699. +
  31700. +/** Maximum number of Endpoints/HostChannels */
  31701. +#define MAX_EPS_CHANNELS 16
  31702. +
  31703. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  31704. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  31705. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  31706. +
  31707. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  31708. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  31709. +
  31710. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  31711. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  31712. +
  31713. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  31714. +
  31715. +/** This function should be called on every hardware interrupt. */
  31716. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  31717. +
  31718. +/** @name OTG Core Parameters */
  31719. +/** @{ */
  31720. +
  31721. +/**
  31722. + * Specifies the OTG capabilities. The driver will automatically
  31723. + * detect the value for this parameter if none is specified.
  31724. + * 0 - HNP and SRP capable (default)
  31725. + * 1 - SRP Only capable
  31726. + * 2 - No HNP/SRP capable
  31727. + */
  31728. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  31729. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  31730. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  31731. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  31732. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  31733. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  31734. +
  31735. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  31736. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  31737. +#define dwc_param_opt_default 1
  31738. +
  31739. +/**
  31740. + * Specifies whether to use slave or DMA mode for accessing the data
  31741. + * FIFOs. The driver will automatically detect the value for this
  31742. + * parameter if none is specified.
  31743. + * 0 - Slave
  31744. + * 1 - DMA (default, if available)
  31745. + */
  31746. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  31747. + int32_t val);
  31748. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  31749. +#define dwc_param_dma_enable_default 1
  31750. +
  31751. +/**
  31752. + * When DMA mode is enabled specifies whether to use
  31753. + * address DMA or DMA Descritor mode for accessing the data
  31754. + * FIFOs in device mode. The driver will automatically detect
  31755. + * the value for this parameter if none is specified.
  31756. + * 0 - address DMA
  31757. + * 1 - DMA Descriptor(default, if available)
  31758. + */
  31759. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  31760. + int32_t val);
  31761. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  31762. +//#define dwc_param_dma_desc_enable_default 1
  31763. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  31764. +
  31765. +/** The DMA Burst size (applicable only for External DMA
  31766. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  31767. + */
  31768. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  31769. + int32_t val);
  31770. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  31771. +#define dwc_param_dma_burst_size_default 32
  31772. +
  31773. +/**
  31774. + * Specifies the maximum speed of operation in host and device mode.
  31775. + * The actual speed depends on the speed of the attached device and
  31776. + * the value of phy_type. The actual speed depends on the speed of the
  31777. + * attached device.
  31778. + * 0 - High Speed (default)
  31779. + * 1 - Full Speed
  31780. + */
  31781. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  31782. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  31783. +#define dwc_param_speed_default 0
  31784. +#define DWC_SPEED_PARAM_HIGH 0
  31785. +#define DWC_SPEED_PARAM_FULL 1
  31786. +
  31787. +/** Specifies whether low power mode is supported when attached
  31788. + * to a Full Speed or Low Speed device in host mode.
  31789. + * 0 - Don't support low power mode (default)
  31790. + * 1 - Support low power mode
  31791. + */
  31792. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  31793. + core_if, int32_t val);
  31794. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  31795. + * core_if);
  31796. +#define dwc_param_host_support_fs_ls_low_power_default 0
  31797. +
  31798. +/** Specifies the PHY clock rate in low power mode when connected to a
  31799. + * Low Speed device in host mode. This parameter is applicable only if
  31800. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  31801. + * then defaults to 6 MHZ otherwise 48 MHZ.
  31802. + *
  31803. + * 0 - 48 MHz
  31804. + * 1 - 6 MHz
  31805. + */
  31806. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  31807. + core_if, int32_t val);
  31808. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  31809. + core_if);
  31810. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  31811. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  31812. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  31813. +
  31814. +/**
  31815. + * 0 - Use cC FIFO size parameters
  31816. + * 1 - Allow dynamic FIFO sizing (default)
  31817. + */
  31818. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  31819. + int32_t val);
  31820. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  31821. + core_if);
  31822. +#define dwc_param_enable_dynamic_fifo_default 1
  31823. +
  31824. +/** Total number of 4-byte words in the data FIFO memory. This
  31825. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  31826. + * Tx FIFOs.
  31827. + * 32 to 32768 (default 8192)
  31828. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  31829. + */
  31830. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  31831. + int32_t val);
  31832. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  31833. +//#define dwc_param_data_fifo_size_default 8192
  31834. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  31835. +
  31836. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  31837. + * FIFO sizing is enabled.
  31838. + * 16 to 32768 (default 1064)
  31839. + */
  31840. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  31841. + int32_t val);
  31842. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  31843. +#define dwc_param_dev_rx_fifo_size_default 1064
  31844. +
  31845. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  31846. + * when dynamic FIFO sizing is enabled.
  31847. + * 16 to 32768 (default 1024)
  31848. + */
  31849. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31850. + core_if, int32_t val);
  31851. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31852. + core_if);
  31853. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  31854. +
  31855. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  31856. + * mode when dynamic FIFO sizing is enabled.
  31857. + * 4 to 768 (default 256)
  31858. + */
  31859. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31860. + int32_t val, int fifo_num);
  31861. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  31862. + core_if, int fifo_num);
  31863. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  31864. +
  31865. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  31866. + * FIFO sizing is enabled.
  31867. + * 16 to 32768 (default 1024)
  31868. + */
  31869. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  31870. + int32_t val);
  31871. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  31872. +//#define dwc_param_host_rx_fifo_size_default 1024
  31873. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  31874. +
  31875. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  31876. + * when Dynamic FIFO sizing is enabled in the core.
  31877. + * 16 to 32768 (default 1024)
  31878. + */
  31879. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31880. + core_if, int32_t val);
  31881. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31882. + core_if);
  31883. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  31884. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  31885. +
  31886. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  31887. + * FIFO sizing is enabled.
  31888. + * 16 to 32768 (default 1024)
  31889. + */
  31890. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  31891. + core_if, int32_t val);
  31892. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  31893. + core_if);
  31894. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  31895. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  31896. +
  31897. +/** The maximum transfer size supported in bytes.
  31898. + * 2047 to 65,535 (default 65,535)
  31899. + */
  31900. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  31901. + int32_t val);
  31902. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  31903. +#define dwc_param_max_transfer_size_default 65535
  31904. +
  31905. +/** The maximum number of packets in a transfer.
  31906. + * 15 to 511 (default 511)
  31907. + */
  31908. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  31909. + int32_t val);
  31910. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  31911. +#define dwc_param_max_packet_count_default 511
  31912. +
  31913. +/** The number of host channel registers to use.
  31914. + * 1 to 16 (default 12)
  31915. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  31916. + */
  31917. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  31918. + int32_t val);
  31919. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  31920. +//#define dwc_param_host_channels_default 12
  31921. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  31922. +
  31923. +/** The number of endpoints in addition to EP0 available for device
  31924. + * mode operations.
  31925. + * 1 to 15 (default 6 IN and OUT)
  31926. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  31927. + * endpoints in addition to EP0.
  31928. + */
  31929. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  31930. + int32_t val);
  31931. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  31932. +#define dwc_param_dev_endpoints_default 6
  31933. +
  31934. +/**
  31935. + * Specifies the type of PHY interface to use. By default, the driver
  31936. + * will automatically detect the phy_type.
  31937. + *
  31938. + * 0 - Full Speed PHY
  31939. + * 1 - UTMI+ (default)
  31940. + * 2 - ULPI
  31941. + */
  31942. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  31943. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  31944. +#define DWC_PHY_TYPE_PARAM_FS 0
  31945. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  31946. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  31947. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  31948. +
  31949. +/**
  31950. + * Specifies the UTMI+ Data Width. This parameter is
  31951. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  31952. + * PHY_TYPE, this parameter indicates the data width between
  31953. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  31954. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  31955. + * to "8 and 16 bits", meaning that the core has been
  31956. + * configured to work at either data path width.
  31957. + *
  31958. + * 8 or 16 bits (default 16)
  31959. + */
  31960. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  31961. + int32_t val);
  31962. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  31963. +//#define dwc_param_phy_utmi_width_default 16
  31964. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  31965. +
  31966. +/**
  31967. + * Specifies whether the ULPI operates at double or single
  31968. + * data rate. This parameter is only applicable if PHY_TYPE is
  31969. + * ULPI.
  31970. + *
  31971. + * 0 - single data rate ULPI interface with 8 bit wide data
  31972. + * bus (default)
  31973. + * 1 - double data rate ULPI interface with 4 bit wide data
  31974. + * bus
  31975. + */
  31976. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  31977. + int32_t val);
  31978. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  31979. +#define dwc_param_phy_ulpi_ddr_default 0
  31980. +
  31981. +/**
  31982. + * Specifies whether to use the internal or external supply to
  31983. + * drive the vbus with a ULPI phy.
  31984. + */
  31985. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  31986. + int32_t val);
  31987. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  31988. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  31989. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  31990. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  31991. +
  31992. +/**
  31993. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  31994. + * parameter is only applicable if PHY_TYPE is FS.
  31995. + * 0 - No (default)
  31996. + * 1 - Yes
  31997. + */
  31998. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  31999. + int32_t val);
  32000. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  32001. +#define dwc_param_i2c_enable_default 0
  32002. +
  32003. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  32004. + int32_t val);
  32005. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  32006. +#define dwc_param_ulpi_fs_ls_default 0
  32007. +
  32008. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  32009. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  32010. +#define dwc_param_ts_dline_default 0
  32011. +
  32012. +/**
  32013. + * Specifies whether dedicated transmit FIFOs are
  32014. + * enabled for non periodic IN endpoints in device mode
  32015. + * 0 - No
  32016. + * 1 - Yes
  32017. + */
  32018. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  32019. + int32_t val);
  32020. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  32021. + core_if);
  32022. +#define dwc_param_en_multiple_tx_fifo_default 1
  32023. +
  32024. +/** Number of 4-byte words in each of the Tx FIFOs in device
  32025. + * mode when dynamic FIFO sizing is enabled.
  32026. + * 4 to 768 (default 256)
  32027. + */
  32028. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  32029. + int fifo_num, int32_t val);
  32030. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  32031. + int fifo_num);
  32032. +#define dwc_param_dev_tx_fifo_size_default 768
  32033. +
  32034. +/** Thresholding enable flag-
  32035. + * bit 0 - enable non-ISO Tx thresholding
  32036. + * bit 1 - enable ISO Tx thresholding
  32037. + * bit 2 - enable Rx thresholding
  32038. + */
  32039. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  32040. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  32041. +#define dwc_param_thr_ctl_default 0
  32042. +
  32043. +/** Thresholding length for Tx
  32044. + * FIFOs in 32 bit DWORDs
  32045. + */
  32046. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  32047. + int32_t val);
  32048. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  32049. +#define dwc_param_tx_thr_length_default 64
  32050. +
  32051. +/** Thresholding length for Rx
  32052. + * FIFOs in 32 bit DWORDs
  32053. + */
  32054. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  32055. + int32_t val);
  32056. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  32057. +#define dwc_param_rx_thr_length_default 64
  32058. +
  32059. +/**
  32060. + * Specifies whether LPM (Link Power Management) support is enabled
  32061. + */
  32062. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  32063. + int32_t val);
  32064. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  32065. +#define dwc_param_lpm_enable_default 1
  32066. +
  32067. +/**
  32068. + * Specifies whether PTI enhancement is enabled
  32069. + */
  32070. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  32071. + int32_t val);
  32072. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  32073. +#define dwc_param_pti_enable_default 0
  32074. +
  32075. +/**
  32076. + * Specifies whether MPI enhancement is enabled
  32077. + */
  32078. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  32079. + int32_t val);
  32080. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  32081. +#define dwc_param_mpi_enable_default 0
  32082. +
  32083. +/**
  32084. + * Specifies whether ADP capability is enabled
  32085. + */
  32086. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  32087. + int32_t val);
  32088. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  32089. +#define dwc_param_adp_enable_default 0
  32090. +
  32091. +/**
  32092. + * Specifies whether IC_USB capability is enabled
  32093. + */
  32094. +
  32095. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  32096. + int32_t val);
  32097. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  32098. +#define dwc_param_ic_usb_cap_default 0
  32099. +
  32100. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  32101. + int32_t val);
  32102. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  32103. +#define dwc_param_ahb_thr_ratio_default 0
  32104. +
  32105. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  32106. + int32_t val);
  32107. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  32108. +#define dwc_param_power_down_default 0
  32109. +
  32110. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  32111. + int32_t val);
  32112. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  32113. +#define dwc_param_reload_ctl_default 0
  32114. +
  32115. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  32116. + int32_t val);
  32117. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  32118. +#define dwc_param_dev_out_nak_default 0
  32119. +
  32120. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  32121. + int32_t val);
  32122. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  32123. +#define dwc_param_cont_on_bna_default 0
  32124. +
  32125. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  32126. + int32_t val);
  32127. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  32128. +#define dwc_param_ahb_single_default 0
  32129. +
  32130. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  32131. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  32132. +#define dwc_param_otg_ver_default 0
  32133. +
  32134. +/** @} */
  32135. +
  32136. +/** @name Access to registers and bit-fields */
  32137. +
  32138. +/**
  32139. + * Dump core registers and SPRAM
  32140. + */
  32141. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  32142. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  32143. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  32144. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  32145. +
  32146. +/**
  32147. + * Get host negotiation status.
  32148. + */
  32149. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  32150. +
  32151. +/**
  32152. + * Get srp status
  32153. + */
  32154. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  32155. +
  32156. +/**
  32157. + * Set hnpreq bit in the GOTGCTL register.
  32158. + */
  32159. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  32160. +
  32161. +/**
  32162. + * Get Content of SNPSID register.
  32163. + */
  32164. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  32165. +
  32166. +/**
  32167. + * Get current mode.
  32168. + * Returns 0 if in device mode, and 1 if in host mode.
  32169. + */
  32170. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  32171. +
  32172. +/**
  32173. + * Get value of hnpcapable field in the GUSBCFG register
  32174. + */
  32175. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  32176. +/**
  32177. + * Set value of hnpcapable field in the GUSBCFG register
  32178. + */
  32179. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  32180. +
  32181. +/**
  32182. + * Get value of srpcapable field in the GUSBCFG register
  32183. + */
  32184. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  32185. +/**
  32186. + * Set value of srpcapable field in the GUSBCFG register
  32187. + */
  32188. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  32189. +
  32190. +/**
  32191. + * Get value of devspeed field in the DCFG register
  32192. + */
  32193. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  32194. +/**
  32195. + * Set value of devspeed field in the DCFG register
  32196. + */
  32197. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  32198. +
  32199. +/**
  32200. + * Get the value of busconnected field from the HPRT0 register
  32201. + */
  32202. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  32203. +
  32204. +/**
  32205. + * Gets the device enumeration Speed.
  32206. + */
  32207. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  32208. +
  32209. +/**
  32210. + * Get value of prtpwr field from the HPRT0 register
  32211. + */
  32212. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  32213. +
  32214. +/**
  32215. + * Get value of flag indicating core state - hibernated or not
  32216. + */
  32217. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  32218. +
  32219. +/**
  32220. + * Set value of prtpwr field from the HPRT0 register
  32221. + */
  32222. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  32223. +
  32224. +/**
  32225. + * Get value of prtsusp field from the HPRT0 regsiter
  32226. + */
  32227. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  32228. +/**
  32229. + * Set value of prtpwr field from the HPRT0 register
  32230. + */
  32231. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  32232. +
  32233. +/**
  32234. + * Get value of ModeChTimEn field from the HCFG regsiter
  32235. + */
  32236. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  32237. +/**
  32238. + * Set value of ModeChTimEn field from the HCFG regsiter
  32239. + */
  32240. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  32241. +
  32242. +/**
  32243. + * Get value of Fram Interval field from the HFIR regsiter
  32244. + */
  32245. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  32246. +/**
  32247. + * Set value of Frame Interval field from the HFIR regsiter
  32248. + */
  32249. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  32250. +
  32251. +/**
  32252. + * Set value of prtres field from the HPRT0 register
  32253. + *FIXME Remove?
  32254. + */
  32255. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  32256. +
  32257. +/**
  32258. + * Get value of rmtwkupsig bit in DCTL register
  32259. + */
  32260. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  32261. +
  32262. +/**
  32263. + * Get value of prt_sleep_sts field from the GLPMCFG register
  32264. + */
  32265. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  32266. +
  32267. +/**
  32268. + * Get value of rem_wkup_en field from the GLPMCFG register
  32269. + */
  32270. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  32271. +
  32272. +/**
  32273. + * Get value of appl_resp field from the GLPMCFG register
  32274. + */
  32275. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  32276. +/**
  32277. + * Set value of appl_resp field from the GLPMCFG register
  32278. + */
  32279. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  32280. +
  32281. +/**
  32282. + * Get value of hsic_connect field from the GLPMCFG register
  32283. + */
  32284. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  32285. +/**
  32286. + * Set value of hsic_connect field from the GLPMCFG register
  32287. + */
  32288. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  32289. +
  32290. +/**
  32291. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  32292. + */
  32293. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  32294. +/**
  32295. + * Set value of inv_sel_hsic field from the GLPMFG register.
  32296. + */
  32297. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  32298. +
  32299. +/*
  32300. + * Some functions for accessing registers
  32301. + */
  32302. +
  32303. +/**
  32304. + * GOTGCTL register
  32305. + */
  32306. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  32307. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  32308. +
  32309. +/**
  32310. + * GUSBCFG register
  32311. + */
  32312. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  32313. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  32314. +
  32315. +/**
  32316. + * GRXFSIZ register
  32317. + */
  32318. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  32319. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  32320. +
  32321. +/**
  32322. + * GNPTXFSIZ register
  32323. + */
  32324. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  32325. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  32326. +
  32327. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  32328. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  32329. +
  32330. +/**
  32331. + * GGPIO register
  32332. + */
  32333. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  32334. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  32335. +
  32336. +/**
  32337. + * GUID register
  32338. + */
  32339. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  32340. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  32341. +
  32342. +/**
  32343. + * HPRT0 register
  32344. + */
  32345. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  32346. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  32347. +
  32348. +/**
  32349. + * GHPTXFSIZE
  32350. + */
  32351. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  32352. +
  32353. +/** @} */
  32354. +
  32355. +#endif /* __DWC_CORE_IF_H__ */
  32356. --- /dev/null
  32357. +++ b/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  32358. @@ -0,0 +1,117 @@
  32359. +/* ==========================================================================
  32360. + *
  32361. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  32362. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  32363. + * otherwise expressly agreed to in writing between Synopsys and you.
  32364. + *
  32365. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  32366. + * any End User Software License Agreement or Agreement for Licensed Product
  32367. + * with Synopsys or any supplement thereto. You are permitted to use and
  32368. + * redistribute this Software in source and binary forms, with or without
  32369. + * modification, provided that redistributions of source code must retain this
  32370. + * notice. You may not view, use, disclose, copy or distribute this file or
  32371. + * any information contained herein except pursuant to this license grant from
  32372. + * Synopsys. If you do not agree with this notice, including the disclaimer
  32373. + * below, then you are not authorized to use the Software.
  32374. + *
  32375. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  32376. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32377. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32378. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  32379. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32380. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32381. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32382. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32383. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32384. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32385. + * DAMAGE.
  32386. + * ========================================================================== */
  32387. +
  32388. +#ifndef __DWC_OTG_DBG_H__
  32389. +#define __DWC_OTG_DBG_H__
  32390. +
  32391. +/** @file
  32392. + * This file defines debug levels.
  32393. + * Debugging support vanishes in non-debug builds.
  32394. + */
  32395. +
  32396. +/**
  32397. + * The Debug Level bit-mask variable.
  32398. + */
  32399. +extern uint32_t g_dbg_lvl;
  32400. +/**
  32401. + * Set the Debug Level variable.
  32402. + */
  32403. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  32404. +{
  32405. + uint32_t old = g_dbg_lvl;
  32406. + g_dbg_lvl = new;
  32407. + return old;
  32408. +}
  32409. +
  32410. +#define DBG_USER (0x1)
  32411. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  32412. +#define DBG_CIL (0x2)
  32413. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  32414. + * messages */
  32415. +#define DBG_CILV (0x20)
  32416. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  32417. + * messages */
  32418. +#define DBG_PCD (0x4)
  32419. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  32420. + * messages */
  32421. +#define DBG_PCDV (0x40)
  32422. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  32423. +#define DBG_HCD (0x8)
  32424. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  32425. + * messages */
  32426. +#define DBG_HCDV (0x80)
  32427. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  32428. + * mode. */
  32429. +#define DBG_HCD_URB (0x800)
  32430. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  32431. + * messages. */
  32432. +#define DBG_HCDI (0x1000)
  32433. +
  32434. +/** When debug level has any bit set, display debug messages */
  32435. +#define DBG_ANY (0xFF)
  32436. +
  32437. +/** All debug messages off */
  32438. +#define DBG_OFF 0
  32439. +
  32440. +/** Prefix string for DWC_DEBUG print macros. */
  32441. +#define USB_DWC "DWC_otg: "
  32442. +
  32443. +/**
  32444. + * Print a debug message when the Global debug level variable contains
  32445. + * the bit defined in <code>lvl</code>.
  32446. + *
  32447. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  32448. + * @param[in] x - like printf
  32449. + *
  32450. + * Example:<p>
  32451. + * <code>
  32452. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  32453. + * </code>
  32454. + * <br>
  32455. + * results in:<br>
  32456. + * <code>
  32457. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  32458. + * </code>
  32459. + */
  32460. +#ifdef DEBUG
  32461. +
  32462. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  32463. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  32464. +
  32465. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  32466. +
  32467. +#else
  32468. +
  32469. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  32470. +# define DWC_DEBUGP(x...)
  32471. +
  32472. +# define CHK_DEBUG_LEVEL(level) (0)
  32473. +
  32474. +#endif /*DEBUG*/
  32475. +#endif
  32476. --- /dev/null
  32477. +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  32478. @@ -0,0 +1,1760 @@
  32479. +/* ==========================================================================
  32480. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  32481. + * $Revision: #92 $
  32482. + * $Date: 2012/08/10 $
  32483. + * $Change: 2047372 $
  32484. + *
  32485. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  32486. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  32487. + * otherwise expressly agreed to in writing between Synopsys and you.
  32488. + *
  32489. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  32490. + * any End User Software License Agreement or Agreement for Licensed Product
  32491. + * with Synopsys or any supplement thereto. You are permitted to use and
  32492. + * redistribute this Software in source and binary forms, with or without
  32493. + * modification, provided that redistributions of source code must retain this
  32494. + * notice. You may not view, use, disclose, copy or distribute this file or
  32495. + * any information contained herein except pursuant to this license grant from
  32496. + * Synopsys. If you do not agree with this notice, including the disclaimer
  32497. + * below, then you are not authorized to use the Software.
  32498. + *
  32499. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  32500. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32501. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32502. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  32503. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32504. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32505. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32506. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32507. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32508. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32509. + * DAMAGE.
  32510. + * ========================================================================== */
  32511. +
  32512. +/** @file
  32513. + * The dwc_otg_driver module provides the initialization and cleanup entry
  32514. + * points for the DWC_otg driver. This module will be dynamically installed
  32515. + * after Linux is booted using the insmod command. When the module is
  32516. + * installed, the dwc_otg_driver_init function is called. When the module is
  32517. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  32518. + *
  32519. + * This module also defines a data structure for the dwc_otg_driver, which is
  32520. + * used in conjunction with the standard ARM lm_device structure. These
  32521. + * structures allow the OTG driver to comply with the standard Linux driver
  32522. + * model in which devices and drivers are registered with a bus driver. This
  32523. + * has the benefit that Linux can expose attributes of the driver and device
  32524. + * in its special sysfs file system. Users can then read or write files in
  32525. + * this file system to perform diagnostics on the driver components or the
  32526. + * device.
  32527. + */
  32528. +
  32529. +#include "dwc_otg_os_dep.h"
  32530. +#include "dwc_os.h"
  32531. +#include "dwc_otg_dbg.h"
  32532. +#include "dwc_otg_driver.h"
  32533. +#include "dwc_otg_attr.h"
  32534. +#include "dwc_otg_core_if.h"
  32535. +#include "dwc_otg_pcd_if.h"
  32536. +#include "dwc_otg_hcd_if.h"
  32537. +#include "dwc_otg_fiq_fsm.h"
  32538. +
  32539. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  32540. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  32541. +
  32542. +bool microframe_schedule=true;
  32543. +
  32544. +static const char dwc_driver_name[] = "dwc_otg";
  32545. +
  32546. +
  32547. +extern int pcd_init(
  32548. +#ifdef LM_INTERFACE
  32549. + struct lm_device *_dev
  32550. +#elif defined(PCI_INTERFACE)
  32551. + struct pci_dev *_dev
  32552. +#elif defined(PLATFORM_INTERFACE)
  32553. + struct platform_device *dev
  32554. +#endif
  32555. + );
  32556. +extern int hcd_init(
  32557. +#ifdef LM_INTERFACE
  32558. + struct lm_device *_dev
  32559. +#elif defined(PCI_INTERFACE)
  32560. + struct pci_dev *_dev
  32561. +#elif defined(PLATFORM_INTERFACE)
  32562. + struct platform_device *dev
  32563. +#endif
  32564. + );
  32565. +
  32566. +extern int pcd_remove(
  32567. +#ifdef LM_INTERFACE
  32568. + struct lm_device *_dev
  32569. +#elif defined(PCI_INTERFACE)
  32570. + struct pci_dev *_dev
  32571. +#elif defined(PLATFORM_INTERFACE)
  32572. + struct platform_device *_dev
  32573. +#endif
  32574. + );
  32575. +
  32576. +extern void hcd_remove(
  32577. +#ifdef LM_INTERFACE
  32578. + struct lm_device *_dev
  32579. +#elif defined(PCI_INTERFACE)
  32580. + struct pci_dev *_dev
  32581. +#elif defined(PLATFORM_INTERFACE)
  32582. + struct platform_device *_dev
  32583. +#endif
  32584. + );
  32585. +
  32586. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  32587. +
  32588. +/*-------------------------------------------------------------------------*/
  32589. +/* Encapsulate the module parameter settings */
  32590. +
  32591. +struct dwc_otg_driver_module_params {
  32592. + int32_t opt;
  32593. + int32_t otg_cap;
  32594. + int32_t dma_enable;
  32595. + int32_t dma_desc_enable;
  32596. + int32_t dma_burst_size;
  32597. + int32_t speed;
  32598. + int32_t host_support_fs_ls_low_power;
  32599. + int32_t host_ls_low_power_phy_clk;
  32600. + int32_t enable_dynamic_fifo;
  32601. + int32_t data_fifo_size;
  32602. + int32_t dev_rx_fifo_size;
  32603. + int32_t dev_nperio_tx_fifo_size;
  32604. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  32605. + int32_t host_rx_fifo_size;
  32606. + int32_t host_nperio_tx_fifo_size;
  32607. + int32_t host_perio_tx_fifo_size;
  32608. + int32_t max_transfer_size;
  32609. + int32_t max_packet_count;
  32610. + int32_t host_channels;
  32611. + int32_t dev_endpoints;
  32612. + int32_t phy_type;
  32613. + int32_t phy_utmi_width;
  32614. + int32_t phy_ulpi_ddr;
  32615. + int32_t phy_ulpi_ext_vbus;
  32616. + int32_t i2c_enable;
  32617. + int32_t ulpi_fs_ls;
  32618. + int32_t ts_dline;
  32619. + int32_t en_multiple_tx_fifo;
  32620. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  32621. + uint32_t thr_ctl;
  32622. + uint32_t tx_thr_length;
  32623. + uint32_t rx_thr_length;
  32624. + int32_t pti_enable;
  32625. + int32_t mpi_enable;
  32626. + int32_t lpm_enable;
  32627. + int32_t ic_usb_cap;
  32628. + int32_t ahb_thr_ratio;
  32629. + int32_t power_down;
  32630. + int32_t reload_ctl;
  32631. + int32_t dev_out_nak;
  32632. + int32_t cont_on_bna;
  32633. + int32_t ahb_single;
  32634. + int32_t otg_ver;
  32635. + int32_t adp_enable;
  32636. +};
  32637. +
  32638. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  32639. + .opt = -1,
  32640. + .otg_cap = -1,
  32641. + .dma_enable = -1,
  32642. + .dma_desc_enable = -1,
  32643. + .dma_burst_size = -1,
  32644. + .speed = -1,
  32645. + .host_support_fs_ls_low_power = -1,
  32646. + .host_ls_low_power_phy_clk = -1,
  32647. + .enable_dynamic_fifo = -1,
  32648. + .data_fifo_size = -1,
  32649. + .dev_rx_fifo_size = -1,
  32650. + .dev_nperio_tx_fifo_size = -1,
  32651. + .dev_perio_tx_fifo_size = {
  32652. + /* dev_perio_tx_fifo_size_1 */
  32653. + -1,
  32654. + -1,
  32655. + -1,
  32656. + -1,
  32657. + -1,
  32658. + -1,
  32659. + -1,
  32660. + -1,
  32661. + -1,
  32662. + -1,
  32663. + -1,
  32664. + -1,
  32665. + -1,
  32666. + -1,
  32667. + -1
  32668. + /* 15 */
  32669. + },
  32670. + .host_rx_fifo_size = -1,
  32671. + .host_nperio_tx_fifo_size = -1,
  32672. + .host_perio_tx_fifo_size = -1,
  32673. + .max_transfer_size = -1,
  32674. + .max_packet_count = -1,
  32675. + .host_channels = -1,
  32676. + .dev_endpoints = -1,
  32677. + .phy_type = -1,
  32678. + .phy_utmi_width = -1,
  32679. + .phy_ulpi_ddr = -1,
  32680. + .phy_ulpi_ext_vbus = -1,
  32681. + .i2c_enable = -1,
  32682. + .ulpi_fs_ls = -1,
  32683. + .ts_dline = -1,
  32684. + .en_multiple_tx_fifo = -1,
  32685. + .dev_tx_fifo_size = {
  32686. + /* dev_tx_fifo_size */
  32687. + -1,
  32688. + -1,
  32689. + -1,
  32690. + -1,
  32691. + -1,
  32692. + -1,
  32693. + -1,
  32694. + -1,
  32695. + -1,
  32696. + -1,
  32697. + -1,
  32698. + -1,
  32699. + -1,
  32700. + -1,
  32701. + -1
  32702. + /* 15 */
  32703. + },
  32704. + .thr_ctl = -1,
  32705. + .tx_thr_length = -1,
  32706. + .rx_thr_length = -1,
  32707. + .pti_enable = -1,
  32708. + .mpi_enable = -1,
  32709. + .lpm_enable = 0,
  32710. + .ic_usb_cap = -1,
  32711. + .ahb_thr_ratio = -1,
  32712. + .power_down = -1,
  32713. + .reload_ctl = -1,
  32714. + .dev_out_nak = -1,
  32715. + .cont_on_bna = -1,
  32716. + .ahb_single = -1,
  32717. + .otg_ver = -1,
  32718. + .adp_enable = -1,
  32719. +};
  32720. +
  32721. +//Global variable to switch the fiq fix on or off
  32722. +bool fiq_enable = 1;
  32723. +// Global variable to enable the split transaction fix
  32724. +bool fiq_fsm_enable = true;
  32725. +//Bulk split-transaction NAK holdoff in microframes
  32726. +uint16_t nak_holdoff = 8;
  32727. +
  32728. +unsigned short fiq_fsm_mask = 0x0F;
  32729. +
  32730. +unsigned short int_ep_interval_min = 0;
  32731. +/**
  32732. + * This function shows the Driver Version.
  32733. + */
  32734. +static ssize_t version_show(struct device_driver *dev, char *buf)
  32735. +{
  32736. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  32737. + DWC_DRIVER_VERSION);
  32738. +}
  32739. +
  32740. +static DRIVER_ATTR_RO(version);
  32741. +
  32742. +/**
  32743. + * Global Debug Level Mask.
  32744. + */
  32745. +uint32_t g_dbg_lvl = 0; /* OFF */
  32746. +
  32747. +/**
  32748. + * This function shows the driver Debug Level.
  32749. + */
  32750. +static ssize_t debuglevel_show(struct device_driver *drv, char *buf)
  32751. +{
  32752. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  32753. +}
  32754. +
  32755. +/**
  32756. + * This function stores the driver Debug Level.
  32757. + */
  32758. +static ssize_t debuglevel_store(struct device_driver *drv, const char *buf,
  32759. + size_t count)
  32760. +{
  32761. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  32762. + return count;
  32763. +}
  32764. +
  32765. +static DRIVER_ATTR_RW(debuglevel);
  32766. +
  32767. +/**
  32768. + * This function is called during module intialization
  32769. + * to pass module parameters to the DWC_OTG CORE.
  32770. + */
  32771. +static int set_parameters(dwc_otg_core_if_t * core_if)
  32772. +{
  32773. + int retval = 0;
  32774. + int i;
  32775. +
  32776. + if (dwc_otg_module_params.otg_cap != -1) {
  32777. + retval +=
  32778. + dwc_otg_set_param_otg_cap(core_if,
  32779. + dwc_otg_module_params.otg_cap);
  32780. + }
  32781. + if (dwc_otg_module_params.dma_enable != -1) {
  32782. + retval +=
  32783. + dwc_otg_set_param_dma_enable(core_if,
  32784. + dwc_otg_module_params.
  32785. + dma_enable);
  32786. + }
  32787. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  32788. + retval +=
  32789. + dwc_otg_set_param_dma_desc_enable(core_if,
  32790. + dwc_otg_module_params.
  32791. + dma_desc_enable);
  32792. + }
  32793. + if (dwc_otg_module_params.opt != -1) {
  32794. + retval +=
  32795. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  32796. + }
  32797. + if (dwc_otg_module_params.dma_burst_size != -1) {
  32798. + retval +=
  32799. + dwc_otg_set_param_dma_burst_size(core_if,
  32800. + dwc_otg_module_params.
  32801. + dma_burst_size);
  32802. + }
  32803. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  32804. + retval +=
  32805. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  32806. + dwc_otg_module_params.
  32807. + host_support_fs_ls_low_power);
  32808. + }
  32809. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  32810. + retval +=
  32811. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  32812. + dwc_otg_module_params.
  32813. + enable_dynamic_fifo);
  32814. + }
  32815. + if (dwc_otg_module_params.data_fifo_size != -1) {
  32816. + retval +=
  32817. + dwc_otg_set_param_data_fifo_size(core_if,
  32818. + dwc_otg_module_params.
  32819. + data_fifo_size);
  32820. + }
  32821. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  32822. + retval +=
  32823. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  32824. + dwc_otg_module_params.
  32825. + dev_rx_fifo_size);
  32826. + }
  32827. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  32828. + retval +=
  32829. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  32830. + dwc_otg_module_params.
  32831. + dev_nperio_tx_fifo_size);
  32832. + }
  32833. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  32834. + retval +=
  32835. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  32836. + dwc_otg_module_params.host_rx_fifo_size);
  32837. + }
  32838. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  32839. + retval +=
  32840. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  32841. + dwc_otg_module_params.
  32842. + host_nperio_tx_fifo_size);
  32843. + }
  32844. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  32845. + retval +=
  32846. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  32847. + dwc_otg_module_params.
  32848. + host_perio_tx_fifo_size);
  32849. + }
  32850. + if (dwc_otg_module_params.max_transfer_size != -1) {
  32851. + retval +=
  32852. + dwc_otg_set_param_max_transfer_size(core_if,
  32853. + dwc_otg_module_params.
  32854. + max_transfer_size);
  32855. + }
  32856. + if (dwc_otg_module_params.max_packet_count != -1) {
  32857. + retval +=
  32858. + dwc_otg_set_param_max_packet_count(core_if,
  32859. + dwc_otg_module_params.
  32860. + max_packet_count);
  32861. + }
  32862. + if (dwc_otg_module_params.host_channels != -1) {
  32863. + retval +=
  32864. + dwc_otg_set_param_host_channels(core_if,
  32865. + dwc_otg_module_params.
  32866. + host_channels);
  32867. + }
  32868. + if (dwc_otg_module_params.dev_endpoints != -1) {
  32869. + retval +=
  32870. + dwc_otg_set_param_dev_endpoints(core_if,
  32871. + dwc_otg_module_params.
  32872. + dev_endpoints);
  32873. + }
  32874. + if (dwc_otg_module_params.phy_type != -1) {
  32875. + retval +=
  32876. + dwc_otg_set_param_phy_type(core_if,
  32877. + dwc_otg_module_params.phy_type);
  32878. + }
  32879. + if (dwc_otg_module_params.speed != -1) {
  32880. + retval +=
  32881. + dwc_otg_set_param_speed(core_if,
  32882. + dwc_otg_module_params.speed);
  32883. + }
  32884. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  32885. + retval +=
  32886. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  32887. + dwc_otg_module_params.
  32888. + host_ls_low_power_phy_clk);
  32889. + }
  32890. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  32891. + retval +=
  32892. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  32893. + dwc_otg_module_params.
  32894. + phy_ulpi_ddr);
  32895. + }
  32896. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  32897. + retval +=
  32898. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  32899. + dwc_otg_module_params.
  32900. + phy_ulpi_ext_vbus);
  32901. + }
  32902. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  32903. + retval +=
  32904. + dwc_otg_set_param_phy_utmi_width(core_if,
  32905. + dwc_otg_module_params.
  32906. + phy_utmi_width);
  32907. + }
  32908. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  32909. + retval +=
  32910. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  32911. + dwc_otg_module_params.ulpi_fs_ls);
  32912. + }
  32913. + if (dwc_otg_module_params.ts_dline != -1) {
  32914. + retval +=
  32915. + dwc_otg_set_param_ts_dline(core_if,
  32916. + dwc_otg_module_params.ts_dline);
  32917. + }
  32918. + if (dwc_otg_module_params.i2c_enable != -1) {
  32919. + retval +=
  32920. + dwc_otg_set_param_i2c_enable(core_if,
  32921. + dwc_otg_module_params.
  32922. + i2c_enable);
  32923. + }
  32924. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  32925. + retval +=
  32926. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  32927. + dwc_otg_module_params.
  32928. + en_multiple_tx_fifo);
  32929. + }
  32930. + for (i = 0; i < 15; i++) {
  32931. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  32932. + retval +=
  32933. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  32934. + dwc_otg_module_params.
  32935. + dev_perio_tx_fifo_size
  32936. + [i], i);
  32937. + }
  32938. + }
  32939. +
  32940. + for (i = 0; i < 15; i++) {
  32941. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  32942. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  32943. + dwc_otg_module_params.
  32944. + dev_tx_fifo_size
  32945. + [i], i);
  32946. + }
  32947. + }
  32948. + if (dwc_otg_module_params.thr_ctl != -1) {
  32949. + retval +=
  32950. + dwc_otg_set_param_thr_ctl(core_if,
  32951. + dwc_otg_module_params.thr_ctl);
  32952. + }
  32953. + if (dwc_otg_module_params.mpi_enable != -1) {
  32954. + retval +=
  32955. + dwc_otg_set_param_mpi_enable(core_if,
  32956. + dwc_otg_module_params.
  32957. + mpi_enable);
  32958. + }
  32959. + if (dwc_otg_module_params.pti_enable != -1) {
  32960. + retval +=
  32961. + dwc_otg_set_param_pti_enable(core_if,
  32962. + dwc_otg_module_params.
  32963. + pti_enable);
  32964. + }
  32965. + if (dwc_otg_module_params.lpm_enable != -1) {
  32966. + retval +=
  32967. + dwc_otg_set_param_lpm_enable(core_if,
  32968. + dwc_otg_module_params.
  32969. + lpm_enable);
  32970. + }
  32971. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  32972. + retval +=
  32973. + dwc_otg_set_param_ic_usb_cap(core_if,
  32974. + dwc_otg_module_params.
  32975. + ic_usb_cap);
  32976. + }
  32977. + if (dwc_otg_module_params.tx_thr_length != -1) {
  32978. + retval +=
  32979. + dwc_otg_set_param_tx_thr_length(core_if,
  32980. + dwc_otg_module_params.tx_thr_length);
  32981. + }
  32982. + if (dwc_otg_module_params.rx_thr_length != -1) {
  32983. + retval +=
  32984. + dwc_otg_set_param_rx_thr_length(core_if,
  32985. + dwc_otg_module_params.
  32986. + rx_thr_length);
  32987. + }
  32988. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  32989. + retval +=
  32990. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  32991. + dwc_otg_module_params.ahb_thr_ratio);
  32992. + }
  32993. + if (dwc_otg_module_params.power_down != -1) {
  32994. + retval +=
  32995. + dwc_otg_set_param_power_down(core_if,
  32996. + dwc_otg_module_params.power_down);
  32997. + }
  32998. + if (dwc_otg_module_params.reload_ctl != -1) {
  32999. + retval +=
  33000. + dwc_otg_set_param_reload_ctl(core_if,
  33001. + dwc_otg_module_params.reload_ctl);
  33002. + }
  33003. +
  33004. + if (dwc_otg_module_params.dev_out_nak != -1) {
  33005. + retval +=
  33006. + dwc_otg_set_param_dev_out_nak(core_if,
  33007. + dwc_otg_module_params.dev_out_nak);
  33008. + }
  33009. +
  33010. + if (dwc_otg_module_params.cont_on_bna != -1) {
  33011. + retval +=
  33012. + dwc_otg_set_param_cont_on_bna(core_if,
  33013. + dwc_otg_module_params.cont_on_bna);
  33014. + }
  33015. +
  33016. + if (dwc_otg_module_params.ahb_single != -1) {
  33017. + retval +=
  33018. + dwc_otg_set_param_ahb_single(core_if,
  33019. + dwc_otg_module_params.ahb_single);
  33020. + }
  33021. +
  33022. + if (dwc_otg_module_params.otg_ver != -1) {
  33023. + retval +=
  33024. + dwc_otg_set_param_otg_ver(core_if,
  33025. + dwc_otg_module_params.otg_ver);
  33026. + }
  33027. + if (dwc_otg_module_params.adp_enable != -1) {
  33028. + retval +=
  33029. + dwc_otg_set_param_adp_enable(core_if,
  33030. + dwc_otg_module_params.
  33031. + adp_enable);
  33032. + }
  33033. + return retval;
  33034. +}
  33035. +
  33036. +/**
  33037. + * This function is the top level interrupt handler for the Common
  33038. + * (Device and host modes) interrupts.
  33039. + */
  33040. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  33041. +{
  33042. + int32_t retval = IRQ_NONE;
  33043. +
  33044. + retval = dwc_otg_handle_common_intr(dev);
  33045. + if (retval != 0) {
  33046. + S3C2410X_CLEAR_EINTPEND();
  33047. + }
  33048. + return IRQ_RETVAL(retval);
  33049. +}
  33050. +
  33051. +/**
  33052. + * This function is called when a lm_device is unregistered with the
  33053. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  33054. + * executed. The device may or may not be electrically present. If it is
  33055. + * present, the driver stops device processing. Any resources used on behalf
  33056. + * of this device are freed.
  33057. + *
  33058. + * @param _dev
  33059. + */
  33060. +#ifdef LM_INTERFACE
  33061. +#define REM_RETVAL(n)
  33062. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  33063. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  33064. +#elif defined(PCI_INTERFACE)
  33065. +#define REM_RETVAL(n)
  33066. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  33067. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  33068. +#elif defined(PLATFORM_INTERFACE)
  33069. +#define REM_RETVAL(n) n
  33070. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  33071. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  33072. +#endif
  33073. +
  33074. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  33075. +
  33076. + if (!otg_dev) {
  33077. + /* Memory allocation for the dwc_otg_device failed. */
  33078. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  33079. + return REM_RETVAL(-ENOMEM);
  33080. + }
  33081. +#ifndef DWC_DEVICE_ONLY
  33082. + if (otg_dev->hcd) {
  33083. + hcd_remove(_dev);
  33084. + } else {
  33085. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  33086. + return REM_RETVAL(-EINVAL);
  33087. + }
  33088. +#endif
  33089. +
  33090. +#ifndef DWC_HOST_ONLY
  33091. + if (otg_dev->pcd) {
  33092. + pcd_remove(_dev);
  33093. + } else {
  33094. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  33095. + return REM_RETVAL(-EINVAL);
  33096. + }
  33097. +#endif
  33098. + /*
  33099. + * Free the IRQ
  33100. + */
  33101. + if (otg_dev->common_irq_installed) {
  33102. +#ifdef PLATFORM_INTERFACE
  33103. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  33104. +#else
  33105. + free_irq(_dev->irq, otg_dev);
  33106. +#endif
  33107. + } else {
  33108. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  33109. + return REM_RETVAL(-ENXIO);
  33110. + }
  33111. +
  33112. + if (otg_dev->core_if) {
  33113. + dwc_otg_cil_remove(otg_dev->core_if);
  33114. + } else {
  33115. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  33116. + return REM_RETVAL(-ENXIO);
  33117. + }
  33118. +
  33119. + /*
  33120. + * Remove the device attributes
  33121. + */
  33122. + dwc_otg_attr_remove(_dev);
  33123. +
  33124. + /*
  33125. + * Return the memory.
  33126. + */
  33127. + if (otg_dev->os_dep.base) {
  33128. + iounmap(otg_dev->os_dep.base);
  33129. + }
  33130. + DWC_FREE(otg_dev);
  33131. +
  33132. + /*
  33133. + * Clear the drvdata pointer.
  33134. + */
  33135. +#ifdef LM_INTERFACE
  33136. + lm_set_drvdata(_dev, 0);
  33137. +#elif defined(PCI_INTERFACE)
  33138. + release_mem_region(otg_dev->os_dep.rsrc_start,
  33139. + otg_dev->os_dep.rsrc_len);
  33140. + pci_set_drvdata(_dev, 0);
  33141. +#elif defined(PLATFORM_INTERFACE)
  33142. + platform_set_drvdata(_dev, 0);
  33143. +#endif
  33144. + return REM_RETVAL(0);
  33145. +}
  33146. +
  33147. +/**
  33148. + * This function is called when an lm_device is bound to a
  33149. + * dwc_otg_driver. It creates the driver components required to
  33150. + * control the device (CIL, HCD, and PCD) and it initializes the
  33151. + * device. The driver components are stored in a dwc_otg_device
  33152. + * structure. A reference to the dwc_otg_device is saved in the
  33153. + * lm_device. This allows the driver to access the dwc_otg_device
  33154. + * structure on subsequent calls to driver methods for this device.
  33155. + *
  33156. + * @param _dev Bus device
  33157. + */
  33158. +static int dwc_otg_driver_probe(
  33159. +#ifdef LM_INTERFACE
  33160. + struct lm_device *_dev
  33161. +#elif defined(PCI_INTERFACE)
  33162. + struct pci_dev *_dev,
  33163. + const struct pci_device_id *id
  33164. +#elif defined(PLATFORM_INTERFACE)
  33165. + struct platform_device *_dev
  33166. +#endif
  33167. + )
  33168. +{
  33169. + int retval = 0;
  33170. + dwc_otg_device_t *dwc_otg_device;
  33171. + int devirq;
  33172. +
  33173. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  33174. +#ifdef LM_INTERFACE
  33175. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  33176. +#elif defined(PCI_INTERFACE)
  33177. + if (!id) {
  33178. + DWC_ERROR("Invalid pci_device_id %p", id);
  33179. + return -EINVAL;
  33180. + }
  33181. +
  33182. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  33183. + DWC_ERROR("Invalid pci_device %p", _dev);
  33184. + return -ENODEV;
  33185. + }
  33186. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  33187. + /* other stuff needed as well? */
  33188. +
  33189. +#elif defined(PLATFORM_INTERFACE)
  33190. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  33191. + (unsigned)_dev->resource->start,
  33192. + (unsigned)(_dev->resource->end - _dev->resource->start));
  33193. +#endif
  33194. +
  33195. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  33196. +
  33197. + if (!dwc_otg_device) {
  33198. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  33199. + return -ENOMEM;
  33200. + }
  33201. +
  33202. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  33203. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  33204. + dwc_otg_device->os_dep.platformdev = _dev;
  33205. +
  33206. + /*
  33207. + * Map the DWC_otg Core memory into virtual address space.
  33208. + */
  33209. +#ifdef LM_INTERFACE
  33210. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  33211. +
  33212. + if (!dwc_otg_device->os_dep.base) {
  33213. + dev_err(&_dev->dev, "ioremap() failed\n");
  33214. + DWC_FREE(dwc_otg_device);
  33215. + return -ENOMEM;
  33216. + }
  33217. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  33218. + (unsigned)dwc_otg_device->os_dep.base);
  33219. +#elif defined(PCI_INTERFACE)
  33220. + _dev->current_state = PCI_D0;
  33221. + _dev->dev.power.power_state = PMSG_ON;
  33222. +
  33223. + if (!_dev->irq) {
  33224. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  33225. + pci_name(_dev));
  33226. + iounmap(dwc_otg_device->os_dep.base);
  33227. + DWC_FREE(dwc_otg_device);
  33228. + return -ENODEV;
  33229. + }
  33230. +
  33231. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  33232. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  33233. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  33234. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  33235. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  33236. + if (!request_mem_region
  33237. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  33238. + "dwc_otg")) {
  33239. + dev_dbg(&_dev->dev, "error requesting memory\n");
  33240. + iounmap(dwc_otg_device->os_dep.base);
  33241. + DWC_FREE(dwc_otg_device);
  33242. + return -EFAULT;
  33243. + }
  33244. +
  33245. + dwc_otg_device->os_dep.base =
  33246. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  33247. + dwc_otg_device->os_dep.rsrc_len);
  33248. + if (dwc_otg_device->os_dep.base == NULL) {
  33249. + dev_dbg(&_dev->dev, "error mapping memory\n");
  33250. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  33251. + dwc_otg_device->os_dep.rsrc_len);
  33252. + iounmap(dwc_otg_device->os_dep.base);
  33253. + DWC_FREE(dwc_otg_device);
  33254. + return -EFAULT;
  33255. + }
  33256. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  33257. + dwc_otg_device->os_dep.base);
  33258. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  33259. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  33260. + dwc_otg_device->os_dep.base);
  33261. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  33262. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  33263. + dwc_otg_device->os_dep.base);
  33264. +
  33265. + pci_set_master(_dev);
  33266. + pci_set_drvdata(_dev, dwc_otg_device);
  33267. +#elif defined(PLATFORM_INTERFACE)
  33268. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  33269. + _dev->resource->start,
  33270. + _dev->resource->end - _dev->resource->start + 1);
  33271. +#if 1
  33272. + if (!request_mem_region(_dev->resource[0].start,
  33273. + _dev->resource[0].end - _dev->resource[0].start + 1,
  33274. + "dwc_otg")) {
  33275. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  33276. + retval = -EFAULT;
  33277. + goto fail;
  33278. + }
  33279. +
  33280. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  33281. + _dev->resource[0].end -
  33282. + _dev->resource[0].start+1);
  33283. + if (fiq_enable)
  33284. + {
  33285. + if (!request_mem_region(_dev->resource[1].start,
  33286. + _dev->resource[1].end - _dev->resource[1].start + 1,
  33287. + "dwc_otg")) {
  33288. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  33289. + retval = -EFAULT;
  33290. + goto fail;
  33291. + }
  33292. +
  33293. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  33294. + _dev->resource[1].end -
  33295. + _dev->resource[1].start + 1);
  33296. + }
  33297. +
  33298. +#else
  33299. + {
  33300. + struct map_desc desc = {
  33301. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  33302. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  33303. + .length = SZ_128K,
  33304. + .type = MT_DEVICE
  33305. + };
  33306. + iotable_init(&desc, 1);
  33307. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  33308. + }
  33309. +#endif
  33310. + if (!dwc_otg_device->os_dep.base) {
  33311. + dev_err(&_dev->dev, "ioremap() failed\n");
  33312. + retval = -ENOMEM;
  33313. + goto fail;
  33314. + }
  33315. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  33316. + (unsigned)dwc_otg_device->os_dep.base);
  33317. +#endif
  33318. +
  33319. + /*
  33320. + * Initialize driver data to point to the global DWC_otg
  33321. + * Device structure.
  33322. + */
  33323. +#ifdef LM_INTERFACE
  33324. + lm_set_drvdata(_dev, dwc_otg_device);
  33325. +#elif defined(PLATFORM_INTERFACE)
  33326. + platform_set_drvdata(_dev, dwc_otg_device);
  33327. +#endif
  33328. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  33329. +
  33330. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  33331. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  33332. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  33333. +
  33334. + if (!dwc_otg_device->core_if) {
  33335. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  33336. + retval = -ENOMEM;
  33337. + goto fail;
  33338. + }
  33339. +
  33340. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  33341. + /*
  33342. + * Attempt to ensure this device is really a DWC_otg Controller.
  33343. + * Read and verify the SNPSID register contents. The value should be
  33344. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  33345. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  33346. + */
  33347. +
  33348. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  33349. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  33350. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  33351. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  33352. + retval = -EINVAL;
  33353. + goto fail;
  33354. + }
  33355. +
  33356. + /*
  33357. + * Validate parameter values.
  33358. + */
  33359. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  33360. + if (set_parameters(dwc_otg_device->core_if)) {
  33361. + retval = -EINVAL;
  33362. + goto fail;
  33363. + }
  33364. +
  33365. + /*
  33366. + * Create Device Attributes in sysfs
  33367. + */
  33368. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  33369. + dwc_otg_attr_create(_dev);
  33370. +
  33371. + /*
  33372. + * Disable the global interrupt until all the interrupt
  33373. + * handlers are installed.
  33374. + */
  33375. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  33376. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  33377. +
  33378. + /*
  33379. + * Install the interrupt handler for the common interrupts before
  33380. + * enabling common interrupts in core_init below.
  33381. + */
  33382. +
  33383. +#if defined(PLATFORM_INTERFACE)
  33384. + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
  33385. +#else
  33386. + devirq = _dev->irq;
  33387. +#endif
  33388. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  33389. + devirq);
  33390. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  33391. + retval = request_irq(devirq, dwc_otg_common_irq,
  33392. + IRQF_SHARED,
  33393. + "dwc_otg", dwc_otg_device);
  33394. + if (retval) {
  33395. + DWC_ERROR("request of irq%d failed\n", devirq);
  33396. + retval = -EBUSY;
  33397. + goto fail;
  33398. + } else {
  33399. + dwc_otg_device->common_irq_installed = 1;
  33400. + }
  33401. +
  33402. +#ifndef IRQF_TRIGGER_LOW
  33403. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  33404. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  33405. + set_irq_type(devirq,
  33406. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  33407. + IRQT_LOW
  33408. +#else
  33409. + IRQ_TYPE_LEVEL_LOW
  33410. +#endif
  33411. + );
  33412. +#endif
  33413. +#endif /*IRQF_TRIGGER_LOW*/
  33414. +
  33415. + /*
  33416. + * Initialize the DWC_otg core.
  33417. + */
  33418. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  33419. + dwc_otg_core_init(dwc_otg_device->core_if);
  33420. +
  33421. +#ifndef DWC_HOST_ONLY
  33422. + /*
  33423. + * Initialize the PCD
  33424. + */
  33425. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  33426. + retval = pcd_init(_dev);
  33427. + if (retval != 0) {
  33428. + DWC_ERROR("pcd_init failed\n");
  33429. + dwc_otg_device->pcd = NULL;
  33430. + goto fail;
  33431. + }
  33432. +#endif
  33433. +#ifndef DWC_DEVICE_ONLY
  33434. + /*
  33435. + * Initialize the HCD
  33436. + */
  33437. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  33438. + retval = hcd_init(_dev);
  33439. + if (retval != 0) {
  33440. + DWC_ERROR("hcd_init failed\n");
  33441. + dwc_otg_device->hcd = NULL;
  33442. + goto fail;
  33443. + }
  33444. +#endif
  33445. + /* Recover from drvdata having been overwritten by hcd_init() */
  33446. +#ifdef LM_INTERFACE
  33447. + lm_set_drvdata(_dev, dwc_otg_device);
  33448. +#elif defined(PLATFORM_INTERFACE)
  33449. + platform_set_drvdata(_dev, dwc_otg_device);
  33450. +#elif defined(PCI_INTERFACE)
  33451. + pci_set_drvdata(_dev, dwc_otg_device);
  33452. + dwc_otg_device->os_dep.pcidev = _dev;
  33453. +#endif
  33454. +
  33455. + /*
  33456. + * Enable the global interrupt after all the interrupt
  33457. + * handlers are installed if there is no ADP support else
  33458. + * perform initial actions required for Internal ADP logic.
  33459. + */
  33460. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  33461. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  33462. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  33463. + dev_dbg(&_dev->dev, "Done\n");
  33464. + } else
  33465. + dwc_otg_adp_start(dwc_otg_device->core_if,
  33466. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  33467. +
  33468. + return 0;
  33469. +
  33470. +fail:
  33471. + dwc_otg_driver_remove(_dev);
  33472. + return retval;
  33473. +}
  33474. +
  33475. +/**
  33476. + * This structure defines the methods to be called by a bus driver
  33477. + * during the lifecycle of a device on that bus. Both drivers and
  33478. + * devices are registered with a bus driver. The bus driver matches
  33479. + * devices to drivers based on information in the device and driver
  33480. + * structures.
  33481. + *
  33482. + * The probe function is called when the bus driver matches a device
  33483. + * to this driver. The remove function is called when a device is
  33484. + * unregistered with the bus driver.
  33485. + */
  33486. +#ifdef LM_INTERFACE
  33487. +static struct lm_driver dwc_otg_driver = {
  33488. + .drv = {.name = (char *)dwc_driver_name,},
  33489. + .probe = dwc_otg_driver_probe,
  33490. + .remove = dwc_otg_driver_remove,
  33491. + // 'suspend' and 'resume' absent
  33492. +};
  33493. +#elif defined(PCI_INTERFACE)
  33494. +static const struct pci_device_id pci_ids[] = { {
  33495. + PCI_DEVICE(0x16c3, 0xabcd),
  33496. + .driver_data =
  33497. + (unsigned long)0xdeadbeef,
  33498. + }, { /* end: all zeroes */ }
  33499. +};
  33500. +
  33501. +MODULE_DEVICE_TABLE(pci, pci_ids);
  33502. +
  33503. +/* pci driver glue; this is a "new style" PCI driver module */
  33504. +static struct pci_driver dwc_otg_driver = {
  33505. + .name = "dwc_otg",
  33506. + .id_table = pci_ids,
  33507. +
  33508. + .probe = dwc_otg_driver_probe,
  33509. + .remove = dwc_otg_driver_remove,
  33510. +
  33511. + .driver = {
  33512. + .name = (char *)dwc_driver_name,
  33513. + },
  33514. +};
  33515. +#elif defined(PLATFORM_INTERFACE)
  33516. +static struct platform_device_id platform_ids[] = {
  33517. + {
  33518. + .name = "bcm2708_usb",
  33519. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  33520. + },
  33521. + { /* end: all zeroes */ }
  33522. +};
  33523. +MODULE_DEVICE_TABLE(platform, platform_ids);
  33524. +
  33525. +static const struct of_device_id dwc_otg_of_match_table[] = {
  33526. + { .compatible = "brcm,bcm2708-usb", },
  33527. + {},
  33528. +};
  33529. +MODULE_DEVICE_TABLE(of, dwc_otg_of_match_table);
  33530. +
  33531. +static struct platform_driver dwc_otg_driver = {
  33532. + .driver = {
  33533. + .name = (char *)dwc_driver_name,
  33534. + .of_match_table = dwc_otg_of_match_table,
  33535. + },
  33536. + .id_table = platform_ids,
  33537. +
  33538. + .probe = dwc_otg_driver_probe,
  33539. + .remove = dwc_otg_driver_remove,
  33540. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  33541. +};
  33542. +#endif
  33543. +
  33544. +/**
  33545. + * This function is called when the dwc_otg_driver is installed with the
  33546. + * insmod command. It registers the dwc_otg_driver structure with the
  33547. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  33548. + * to be called. In addition, the bus driver will automatically expose
  33549. + * attributes defined for the device and driver in the special sysfs file
  33550. + * system.
  33551. + *
  33552. + * @return
  33553. + */
  33554. +static int __init dwc_otg_driver_init(void)
  33555. +{
  33556. + int retval = 0;
  33557. + int error;
  33558. + struct device_driver *drv;
  33559. +
  33560. + if(fiq_fsm_enable && !fiq_enable) {
  33561. + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
  33562. + fiq_enable = 1;
  33563. + }
  33564. +
  33565. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  33566. + DWC_DRIVER_VERSION,
  33567. +#ifdef LM_INTERFACE
  33568. + "logicmodule");
  33569. + retval = lm_driver_register(&dwc_otg_driver);
  33570. + drv = &dwc_otg_driver.drv;
  33571. +#elif defined(PCI_INTERFACE)
  33572. + "pci");
  33573. + retval = pci_register_driver(&dwc_otg_driver);
  33574. + drv = &dwc_otg_driver.driver;
  33575. +#elif defined(PLATFORM_INTERFACE)
  33576. + "platform");
  33577. + retval = platform_driver_register(&dwc_otg_driver);
  33578. + drv = &dwc_otg_driver.driver;
  33579. +#endif
  33580. + if (retval < 0) {
  33581. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  33582. + return retval;
  33583. + }
  33584. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
  33585. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
  33586. + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
  33587. +
  33588. + error = driver_create_file(drv, &driver_attr_version);
  33589. +#ifdef DEBUG
  33590. + error = driver_create_file(drv, &driver_attr_debuglevel);
  33591. +#endif
  33592. + return retval;
  33593. +}
  33594. +
  33595. +module_init(dwc_otg_driver_init);
  33596. +
  33597. +/**
  33598. + * This function is called when the driver is removed from the kernel
  33599. + * with the rmmod command. The driver unregisters itself with its bus
  33600. + * driver.
  33601. + *
  33602. + */
  33603. +static void __exit dwc_otg_driver_cleanup(void)
  33604. +{
  33605. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  33606. +
  33607. +#ifdef LM_INTERFACE
  33608. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  33609. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  33610. + lm_driver_unregister(&dwc_otg_driver);
  33611. +#elif defined(PCI_INTERFACE)
  33612. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  33613. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  33614. + pci_unregister_driver(&dwc_otg_driver);
  33615. +#elif defined(PLATFORM_INTERFACE)
  33616. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  33617. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  33618. + platform_driver_unregister(&dwc_otg_driver);
  33619. +#endif
  33620. +
  33621. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  33622. +}
  33623. +
  33624. +module_exit(dwc_otg_driver_cleanup);
  33625. +
  33626. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  33627. +MODULE_AUTHOR("Synopsys Inc.");
  33628. +MODULE_LICENSE("GPL");
  33629. +
  33630. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  33631. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  33632. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  33633. +MODULE_PARM_DESC(opt, "OPT Mode");
  33634. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  33635. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  33636. +
  33637. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  33638. + 0444);
  33639. +MODULE_PARM_DESC(dma_desc_enable,
  33640. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  33641. +
  33642. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  33643. + 0444);
  33644. +MODULE_PARM_DESC(dma_burst_size,
  33645. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  33646. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  33647. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  33648. +module_param_named(host_support_fs_ls_low_power,
  33649. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  33650. + 0444);
  33651. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  33652. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  33653. +module_param_named(host_ls_low_power_phy_clk,
  33654. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  33655. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  33656. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  33657. +module_param_named(enable_dynamic_fifo,
  33658. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  33659. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  33660. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  33661. + 0444);
  33662. +MODULE_PARM_DESC(data_fifo_size,
  33663. + "Total number of words in the data FIFO memory 32-32768");
  33664. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  33665. + int, 0444);
  33666. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  33667. +module_param_named(dev_nperio_tx_fifo_size,
  33668. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  33669. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  33670. + "Number of words in the non-periodic Tx FIFO 16-32768");
  33671. +module_param_named(dev_perio_tx_fifo_size_1,
  33672. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  33673. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  33674. + "Number of words in the periodic Tx FIFO 4-768");
  33675. +module_param_named(dev_perio_tx_fifo_size_2,
  33676. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  33677. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  33678. + "Number of words in the periodic Tx FIFO 4-768");
  33679. +module_param_named(dev_perio_tx_fifo_size_3,
  33680. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  33681. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  33682. + "Number of words in the periodic Tx FIFO 4-768");
  33683. +module_param_named(dev_perio_tx_fifo_size_4,
  33684. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  33685. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  33686. + "Number of words in the periodic Tx FIFO 4-768");
  33687. +module_param_named(dev_perio_tx_fifo_size_5,
  33688. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  33689. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  33690. + "Number of words in the periodic Tx FIFO 4-768");
  33691. +module_param_named(dev_perio_tx_fifo_size_6,
  33692. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  33693. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  33694. + "Number of words in the periodic Tx FIFO 4-768");
  33695. +module_param_named(dev_perio_tx_fifo_size_7,
  33696. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  33697. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  33698. + "Number of words in the periodic Tx FIFO 4-768");
  33699. +module_param_named(dev_perio_tx_fifo_size_8,
  33700. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  33701. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  33702. + "Number of words in the periodic Tx FIFO 4-768");
  33703. +module_param_named(dev_perio_tx_fifo_size_9,
  33704. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  33705. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  33706. + "Number of words in the periodic Tx FIFO 4-768");
  33707. +module_param_named(dev_perio_tx_fifo_size_10,
  33708. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  33709. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  33710. + "Number of words in the periodic Tx FIFO 4-768");
  33711. +module_param_named(dev_perio_tx_fifo_size_11,
  33712. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  33713. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  33714. + "Number of words in the periodic Tx FIFO 4-768");
  33715. +module_param_named(dev_perio_tx_fifo_size_12,
  33716. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  33717. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  33718. + "Number of words in the periodic Tx FIFO 4-768");
  33719. +module_param_named(dev_perio_tx_fifo_size_13,
  33720. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  33721. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  33722. + "Number of words in the periodic Tx FIFO 4-768");
  33723. +module_param_named(dev_perio_tx_fifo_size_14,
  33724. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  33725. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  33726. + "Number of words in the periodic Tx FIFO 4-768");
  33727. +module_param_named(dev_perio_tx_fifo_size_15,
  33728. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  33729. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  33730. + "Number of words in the periodic Tx FIFO 4-768");
  33731. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  33732. + int, 0444);
  33733. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  33734. +module_param_named(host_nperio_tx_fifo_size,
  33735. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  33736. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  33737. + "Number of words in the non-periodic Tx FIFO 16-32768");
  33738. +module_param_named(host_perio_tx_fifo_size,
  33739. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  33740. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  33741. + "Number of words in the host periodic Tx FIFO 16-32768");
  33742. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  33743. + int, 0444);
  33744. +/** @todo Set the max to 512K, modify checks */
  33745. +MODULE_PARM_DESC(max_transfer_size,
  33746. + "The maximum transfer size supported in bytes 2047-65535");
  33747. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  33748. + int, 0444);
  33749. +MODULE_PARM_DESC(max_packet_count,
  33750. + "The maximum number of packets in a transfer 15-511");
  33751. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  33752. + 0444);
  33753. +MODULE_PARM_DESC(host_channels,
  33754. + "The number of host channel registers to use 1-16");
  33755. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  33756. + 0444);
  33757. +MODULE_PARM_DESC(dev_endpoints,
  33758. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  33759. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  33760. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  33761. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  33762. + 0444);
  33763. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  33764. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  33765. +MODULE_PARM_DESC(phy_ulpi_ddr,
  33766. + "ULPI at double or single data rate 0=Single 1=Double");
  33767. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  33768. + int, 0444);
  33769. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  33770. + "ULPI PHY using internal or external vbus 0=Internal");
  33771. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  33772. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  33773. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  33774. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  33775. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  33776. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  33777. +module_param_named(debug, g_dbg_lvl, int, 0444);
  33778. +MODULE_PARM_DESC(debug, "");
  33779. +
  33780. +module_param_named(en_multiple_tx_fifo,
  33781. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  33782. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  33783. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  33784. +module_param_named(dev_tx_fifo_size_1,
  33785. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  33786. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  33787. +module_param_named(dev_tx_fifo_size_2,
  33788. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  33789. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  33790. +module_param_named(dev_tx_fifo_size_3,
  33791. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  33792. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  33793. +module_param_named(dev_tx_fifo_size_4,
  33794. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  33795. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  33796. +module_param_named(dev_tx_fifo_size_5,
  33797. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  33798. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  33799. +module_param_named(dev_tx_fifo_size_6,
  33800. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  33801. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  33802. +module_param_named(dev_tx_fifo_size_7,
  33803. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  33804. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  33805. +module_param_named(dev_tx_fifo_size_8,
  33806. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  33807. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  33808. +module_param_named(dev_tx_fifo_size_9,
  33809. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  33810. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  33811. +module_param_named(dev_tx_fifo_size_10,
  33812. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  33813. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  33814. +module_param_named(dev_tx_fifo_size_11,
  33815. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  33816. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  33817. +module_param_named(dev_tx_fifo_size_12,
  33818. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  33819. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  33820. +module_param_named(dev_tx_fifo_size_13,
  33821. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  33822. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  33823. +module_param_named(dev_tx_fifo_size_14,
  33824. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  33825. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  33826. +module_param_named(dev_tx_fifo_size_15,
  33827. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  33828. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  33829. +
  33830. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  33831. +MODULE_PARM_DESC(thr_ctl,
  33832. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  33833. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  33834. + 0444);
  33835. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  33836. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  33837. + 0444);
  33838. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  33839. +
  33840. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  33841. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  33842. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  33843. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  33844. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  33845. +MODULE_PARM_DESC(ic_usb_cap,
  33846. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  33847. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  33848. + 0444);
  33849. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  33850. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  33851. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  33852. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  33853. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  33854. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  33855. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  33856. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  33857. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  33858. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  33859. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  33860. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  33861. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  33862. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  33863. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  33864. +module_param(microframe_schedule, bool, 0444);
  33865. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  33866. +
  33867. +module_param(fiq_enable, bool, 0444);
  33868. +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
  33869. +module_param(nak_holdoff, ushort, 0644);
  33870. +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
  33871. +module_param(fiq_fsm_enable, bool, 0444);
  33872. +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
  33873. +module_param(fiq_fsm_mask, ushort, 0444);
  33874. +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
  33875. + "Bit 0 : Non-periodic split transactions\n"
  33876. + "Bit 1 : Periodic split transactions\n"
  33877. + "Bit 2 : High-speed multi-transfer isochronous\n"
  33878. + "All other bits should be set 0.");
  33879. +module_param(int_ep_interval_min, ushort, 0644);
  33880. +MODULE_PARM_DESC(int_ep_interval_min, "Clamp high-speed Interrupt endpoints to a minimum polling interval.\n"
  33881. + "0..1 = Use endpoint default\n"
  33882. + "2..n = Minimum interval n microframes. Use powers of 2.\n");
  33883. +
  33884. +/** @page "Module Parameters"
  33885. + *
  33886. + * The following parameters may be specified when starting the module.
  33887. + * These parameters define how the DWC_otg controller should be
  33888. + * configured. Parameter values are passed to the CIL initialization
  33889. + * function dwc_otg_cil_init
  33890. + *
  33891. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  33892. + *
  33893. +
  33894. + <table>
  33895. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  33896. +
  33897. + <tr>
  33898. + <td>otg_cap</td>
  33899. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  33900. + value for this parameter if none is specified.
  33901. + - 0: HNP and SRP capable (default, if available)
  33902. + - 1: SRP Only capable
  33903. + - 2: No HNP/SRP capable
  33904. + </td></tr>
  33905. +
  33906. + <tr>
  33907. + <td>dma_enable</td>
  33908. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  33909. + The driver will automatically detect the value for this parameter if none is
  33910. + specified.
  33911. + - 0: Slave
  33912. + - 1: DMA (default, if available)
  33913. + </td></tr>
  33914. +
  33915. + <tr>
  33916. + <td>dma_burst_size</td>
  33917. + <td>The DMA Burst size (applicable only for External DMA Mode).
  33918. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  33919. + </td></tr>
  33920. +
  33921. + <tr>
  33922. + <td>speed</td>
  33923. + <td>Specifies the maximum speed of operation in host and device mode. The
  33924. + actual speed depends on the speed of the attached device and the value of
  33925. + phy_type.
  33926. + - 0: High Speed (default)
  33927. + - 1: Full Speed
  33928. + </td></tr>
  33929. +
  33930. + <tr>
  33931. + <td>host_support_fs_ls_low_power</td>
  33932. + <td>Specifies whether low power mode is supported when attached to a Full
  33933. + Speed or Low Speed device in host mode.
  33934. + - 0: Don't support low power mode (default)
  33935. + - 1: Support low power mode
  33936. + </td></tr>
  33937. +
  33938. + <tr>
  33939. + <td>host_ls_low_power_phy_clk</td>
  33940. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  33941. + Speed device in host mode. This parameter is applicable only if
  33942. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  33943. + - 0: 48 MHz (default)
  33944. + - 1: 6 MHz
  33945. + </td></tr>
  33946. +
  33947. + <tr>
  33948. + <td>enable_dynamic_fifo</td>
  33949. + <td> Specifies whether FIFOs may be resized by the driver software.
  33950. + - 0: Use cC FIFO size parameters
  33951. + - 1: Allow dynamic FIFO sizing (default)
  33952. + </td></tr>
  33953. +
  33954. + <tr>
  33955. + <td>data_fifo_size</td>
  33956. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  33957. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  33958. + - Values: 32 to 32768 (default 8192)
  33959. +
  33960. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  33961. + </td></tr>
  33962. +
  33963. + <tr>
  33964. + <td>dev_rx_fifo_size</td>
  33965. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  33966. + FIFO sizing is enabled.
  33967. + - Values: 16 to 32768 (default 1064)
  33968. + </td></tr>
  33969. +
  33970. + <tr>
  33971. + <td>dev_nperio_tx_fifo_size</td>
  33972. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  33973. + dynamic FIFO sizing is enabled.
  33974. + - Values: 16 to 32768 (default 1024)
  33975. + </td></tr>
  33976. +
  33977. + <tr>
  33978. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  33979. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  33980. + when dynamic FIFO sizing is enabled.
  33981. + - Values: 4 to 768 (default 256)
  33982. + </td></tr>
  33983. +
  33984. + <tr>
  33985. + <td>host_rx_fifo_size</td>
  33986. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  33987. + sizing is enabled.
  33988. + - Values: 16 to 32768 (default 1024)
  33989. + </td></tr>
  33990. +
  33991. + <tr>
  33992. + <td>host_nperio_tx_fifo_size</td>
  33993. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  33994. + dynamic FIFO sizing is enabled in the core.
  33995. + - Values: 16 to 32768 (default 1024)
  33996. + </td></tr>
  33997. +
  33998. + <tr>
  33999. + <td>host_perio_tx_fifo_size</td>
  34000. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  34001. + sizing is enabled.
  34002. + - Values: 16 to 32768 (default 1024)
  34003. + </td></tr>
  34004. +
  34005. + <tr>
  34006. + <td>max_transfer_size</td>
  34007. + <td>The maximum transfer size supported in bytes.
  34008. + - Values: 2047 to 65,535 (default 65,535)
  34009. + </td></tr>
  34010. +
  34011. + <tr>
  34012. + <td>max_packet_count</td>
  34013. + <td>The maximum number of packets in a transfer.
  34014. + - Values: 15 to 511 (default 511)
  34015. + </td></tr>
  34016. +
  34017. + <tr>
  34018. + <td>host_channels</td>
  34019. + <td>The number of host channel registers to use.
  34020. + - Values: 1 to 16 (default 12)
  34021. +
  34022. + Note: The FPGA configuration supports a maximum of 12 host channels.
  34023. + </td></tr>
  34024. +
  34025. + <tr>
  34026. + <td>dev_endpoints</td>
  34027. + <td>The number of endpoints in addition to EP0 available for device mode
  34028. + operations.
  34029. + - Values: 1 to 15 (default 6 IN and OUT)
  34030. +
  34031. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  34032. + addition to EP0.
  34033. + </td></tr>
  34034. +
  34035. + <tr>
  34036. + <td>phy_type</td>
  34037. + <td>Specifies the type of PHY interface to use. By default, the driver will
  34038. + automatically detect the phy_type.
  34039. + - 0: Full Speed
  34040. + - 1: UTMI+ (default, if available)
  34041. + - 2: ULPI
  34042. + </td></tr>
  34043. +
  34044. + <tr>
  34045. + <td>phy_utmi_width</td>
  34046. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  34047. + phy_type of UTMI+. Also, this parameter is applicable only if the
  34048. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  34049. + core has been configured to work at either data path width.
  34050. + - Values: 8 or 16 bits (default 16)
  34051. + </td></tr>
  34052. +
  34053. + <tr>
  34054. + <td>phy_ulpi_ddr</td>
  34055. + <td>Specifies whether the ULPI operates at double or single data rate. This
  34056. + parameter is only applicable if phy_type is ULPI.
  34057. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  34058. + - 1: double data rate ULPI interface with 4 bit wide data bus
  34059. + </td></tr>
  34060. +
  34061. + <tr>
  34062. + <td>i2c_enable</td>
  34063. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  34064. + parameter is only applicable if PHY_TYPE is FS.
  34065. + - 0: Disabled (default)
  34066. + - 1: Enabled
  34067. + </td></tr>
  34068. +
  34069. + <tr>
  34070. + <td>ulpi_fs_ls</td>
  34071. + <td>Specifies whether to use ULPI FS/LS mode only.
  34072. + - 0: Disabled (default)
  34073. + - 1: Enabled
  34074. + </td></tr>
  34075. +
  34076. + <tr>
  34077. + <td>ts_dline</td>
  34078. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  34079. + - 0: Disabled (default)
  34080. + - 1: Enabled
  34081. + </td></tr>
  34082. +
  34083. + <tr>
  34084. + <td>en_multiple_tx_fifo</td>
  34085. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  34086. + The driver will automatically detect the value for this parameter if none is
  34087. + specified.
  34088. + - 0: Disabled
  34089. + - 1: Enabled (default, if available)
  34090. + </td></tr>
  34091. +
  34092. + <tr>
  34093. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  34094. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  34095. + when dynamic FIFO sizing is enabled.
  34096. + - Values: 4 to 768 (default 256)
  34097. + </td></tr>
  34098. +
  34099. + <tr>
  34100. + <td>tx_thr_length</td>
  34101. + <td>Transmit Threshold length in 32 bit double words
  34102. + - Values: 8 to 128 (default 64)
  34103. + </td></tr>
  34104. +
  34105. + <tr>
  34106. + <td>rx_thr_length</td>
  34107. + <td>Receive Threshold length in 32 bit double words
  34108. + - Values: 8 to 128 (default 64)
  34109. + </td></tr>
  34110. +
  34111. +<tr>
  34112. + <td>thr_ctl</td>
  34113. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  34114. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  34115. + Rx transfers accordingly.
  34116. + The driver will automatically detect the value for this parameter if none is
  34117. + specified.
  34118. + - Values: 0 to 7 (default 0)
  34119. + Bit values indicate:
  34120. + - 0: Thresholding disabled
  34121. + - 1: Thresholding enabled
  34122. + </td></tr>
  34123. +
  34124. +<tr>
  34125. + <td>dma_desc_enable</td>
  34126. + <td>Specifies whether to enable Descriptor DMA mode.
  34127. + The driver will automatically detect the value for this parameter if none is
  34128. + specified.
  34129. + - 0: Descriptor DMA disabled
  34130. + - 1: Descriptor DMA (default, if available)
  34131. + </td></tr>
  34132. +
  34133. +<tr>
  34134. + <td>mpi_enable</td>
  34135. + <td>Specifies whether to enable MPI enhancement mode.
  34136. + The driver will automatically detect the value for this parameter if none is
  34137. + specified.
  34138. + - 0: MPI disabled (default)
  34139. + - 1: MPI enable
  34140. + </td></tr>
  34141. +
  34142. +<tr>
  34143. + <td>pti_enable</td>
  34144. + <td>Specifies whether to enable PTI enhancement support.
  34145. + The driver will automatically detect the value for this parameter if none is
  34146. + specified.
  34147. + - 0: PTI disabled (default)
  34148. + - 1: PTI enable
  34149. + </td></tr>
  34150. +
  34151. +<tr>
  34152. + <td>lpm_enable</td>
  34153. + <td>Specifies whether to enable LPM support.
  34154. + The driver will automatically detect the value for this parameter if none is
  34155. + specified.
  34156. + - 0: LPM disabled
  34157. + - 1: LPM enable (default, if available)
  34158. + </td></tr>
  34159. +
  34160. +<tr>
  34161. + <td>ic_usb_cap</td>
  34162. + <td>Specifies whether to enable IC_USB capability.
  34163. + The driver will automatically detect the value for this parameter if none is
  34164. + specified.
  34165. + - 0: IC_USB disabled (default, if available)
  34166. + - 1: IC_USB enable
  34167. + </td></tr>
  34168. +
  34169. +<tr>
  34170. + <td>ahb_thr_ratio</td>
  34171. + <td>Specifies AHB Threshold ratio.
  34172. + - Values: 0 to 3 (default 0)
  34173. + </td></tr>
  34174. +
  34175. +<tr>
  34176. + <td>power_down</td>
  34177. + <td>Specifies Power Down(Hibernation) Mode.
  34178. + The driver will automatically detect the value for this parameter if none is
  34179. + specified.
  34180. + - 0: Power Down disabled (default)
  34181. + - 2: Power Down enabled
  34182. + </td></tr>
  34183. +
  34184. + <tr>
  34185. + <td>reload_ctl</td>
  34186. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  34187. + run time. The driver will automatically detect the value for this parameter if
  34188. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  34189. + the core might misbehave.
  34190. + - 0: Reload Control disabled (default)
  34191. + - 1: Reload Control enabled
  34192. + </td></tr>
  34193. +
  34194. + <tr>
  34195. + <td>dev_out_nak</td>
  34196. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  34197. + The driver will automatically detect the value for this parameter if
  34198. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  34199. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  34200. + - 1: The core sets NAK after Bulk OUT transfer complete
  34201. + </td></tr>
  34202. +
  34203. + <tr>
  34204. + <td>cont_on_bna</td>
  34205. + <td>Specifies whether Enable Continue on BNA enabled or no.
  34206. + After receiving BNA interrupt the core disables the endpoint,when the
  34207. + endpoint is re-enabled by the application the
  34208. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  34209. + - 1: Core starts processing from the descriptor which received the BNA.
  34210. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  34211. + </td></tr>
  34212. +
  34213. + <tr>
  34214. + <td>ahb_single</td>
  34215. + <td>This bit when programmed supports SINGLE transfers for remainder data
  34216. + in a transfer for DMA mode of operation.
  34217. + - 0: The remainder data will be sent using INCR burst size (default)
  34218. + - 1: The remainder data will be sent using SINGLE burst size.
  34219. + </td></tr>
  34220. +
  34221. +<tr>
  34222. + <td>adp_enable</td>
  34223. + <td>Specifies whether ADP feature is enabled.
  34224. + The driver will automatically detect the value for this parameter if none is
  34225. + specified.
  34226. + - 0: ADP feature disabled (default)
  34227. + - 1: ADP feature enabled
  34228. + </td></tr>
  34229. +
  34230. + <tr>
  34231. + <td>otg_ver</td>
  34232. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  34233. + USB OTG device.
  34234. + - 0: OTG 2.0 support disabled (default)
  34235. + - 1: OTG 2.0 support enabled
  34236. + </td></tr>
  34237. +
  34238. +*/
  34239. --- /dev/null
  34240. +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  34241. @@ -0,0 +1,86 @@
  34242. +/* ==========================================================================
  34243. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  34244. + * $Revision: #19 $
  34245. + * $Date: 2010/11/15 $
  34246. + * $Change: 1627671 $
  34247. + *
  34248. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  34249. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  34250. + * otherwise expressly agreed to in writing between Synopsys and you.
  34251. + *
  34252. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  34253. + * any End User Software License Agreement or Agreement for Licensed Product
  34254. + * with Synopsys or any supplement thereto. You are permitted to use and
  34255. + * redistribute this Software in source and binary forms, with or without
  34256. + * modification, provided that redistributions of source code must retain this
  34257. + * notice. You may not view, use, disclose, copy or distribute this file or
  34258. + * any information contained herein except pursuant to this license grant from
  34259. + * Synopsys. If you do not agree with this notice, including the disclaimer
  34260. + * below, then you are not authorized to use the Software.
  34261. + *
  34262. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  34263. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34264. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34265. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  34266. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  34267. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34268. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34269. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34270. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34271. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  34272. + * DAMAGE.
  34273. + * ========================================================================== */
  34274. +
  34275. +#ifndef __DWC_OTG_DRIVER_H__
  34276. +#define __DWC_OTG_DRIVER_H__
  34277. +
  34278. +/** @file
  34279. + * This file contains the interface to the Linux driver.
  34280. + */
  34281. +#include "dwc_otg_os_dep.h"
  34282. +#include "dwc_otg_core_if.h"
  34283. +
  34284. +/* Type declarations */
  34285. +struct dwc_otg_pcd;
  34286. +struct dwc_otg_hcd;
  34287. +
  34288. +/**
  34289. + * This structure is a wrapper that encapsulates the driver components used to
  34290. + * manage a single DWC_otg controller.
  34291. + */
  34292. +typedef struct dwc_otg_device {
  34293. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  34294. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  34295. + * require this. */
  34296. + struct os_dependent os_dep;
  34297. +
  34298. + /** Pointer to the core interface structure. */
  34299. + dwc_otg_core_if_t *core_if;
  34300. +
  34301. + /** Pointer to the PCD structure. */
  34302. + struct dwc_otg_pcd *pcd;
  34303. +
  34304. + /** Pointer to the HCD structure. */
  34305. + struct dwc_otg_hcd *hcd;
  34306. +
  34307. + /** Flag to indicate whether the common IRQ handler is installed. */
  34308. + uint8_t common_irq_installed;
  34309. +
  34310. +} dwc_otg_device_t;
  34311. +
  34312. +/*We must clear S3C24XX_EINTPEND external interrupt register
  34313. + * because after clearing in this register trigerred IRQ from
  34314. + * H/W core in kernel interrupt can be occured again before OTG
  34315. + * handlers clear all IRQ sources of Core registers because of
  34316. + * timing latencies and Low Level IRQ Type.
  34317. + */
  34318. +#ifdef CONFIG_MACH_IPMATE
  34319. +#define S3C2410X_CLEAR_EINTPEND() \
  34320. +do { \
  34321. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  34322. +} while (0)
  34323. +#else
  34324. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  34325. +#endif
  34326. +
  34327. +#endif
  34328. --- /dev/null
  34329. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  34330. @@ -0,0 +1,1389 @@
  34331. +/*
  34332. + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
  34333. + *
  34334. + * Copyright (c) 2013 Raspberry Pi Foundation
  34335. + *
  34336. + * Author: Jonathan Bell <[email protected]>
  34337. + * All rights reserved.
  34338. + *
  34339. + * Redistribution and use in source and binary forms, with or without
  34340. + * modification, are permitted provided that the following conditions are met:
  34341. + * * Redistributions of source code must retain the above copyright
  34342. + * notice, this list of conditions and the following disclaimer.
  34343. + * * Redistributions in binary form must reproduce the above copyright
  34344. + * notice, this list of conditions and the following disclaimer in the
  34345. + * documentation and/or other materials provided with the distribution.
  34346. + * * Neither the name of Raspberry Pi nor the
  34347. + * names of its contributors may be used to endorse or promote products
  34348. + * derived from this software without specific prior written permission.
  34349. + *
  34350. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  34351. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  34352. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  34353. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  34354. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  34355. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34356. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  34357. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34358. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34359. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34360. + *
  34361. + * This FIQ implements functionality that performs split transactions on
  34362. + * the dwc_otg hardware without any outside intervention. A split transaction
  34363. + * is "queued" by nominating a specific host channel to perform the entirety
  34364. + * of a split transaction. This FIQ will then perform the microframe-precise
  34365. + * scheduling required in each phase of the transaction until completion.
  34366. + *
  34367. + * The FIQ functionality is glued into the Synopsys driver via the entry point
  34368. + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
  34369. + * for a FSM-enabled channel.
  34370. + *
  34371. + * NB: Large parts of this implementation have architecture-specific code.
  34372. + * For porting this functionality to other ARM machines, the minimum is required:
  34373. + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
  34374. + * to the FIQ
  34375. + * - A method of forcing a software generated interrupt from FIQ mode that then
  34376. + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
  34377. + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
  34378. + * processor core - there is no locking between the FIQ and IRQ (aside from
  34379. + * local_fiq_disable)
  34380. + *
  34381. + */
  34382. +
  34383. +#include "dwc_otg_fiq_fsm.h"
  34384. +
  34385. +
  34386. +char buffer[1000*16];
  34387. +int wptr;
  34388. +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
  34389. +{
  34390. + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
  34391. + va_list args;
  34392. + char text[17];
  34393. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
  34394. +
  34395. + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
  34396. + {
  34397. + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
  34398. + va_start(args, fmt);
  34399. + vsnprintf(text+8, 9, fmt, args);
  34400. + va_end(args);
  34401. +
  34402. + memcpy(buffer + wptr, text, 16);
  34403. + wptr = (wptr + 16) % sizeof(buffer);
  34404. + }
  34405. +}
  34406. +
  34407. +/**
  34408. + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
  34409. + * Must be called with local interrupts and FIQ disabled.
  34410. + */
  34411. +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
  34412. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
  34413. +{
  34414. + unsigned long tmp;
  34415. + uint32_t newval;
  34416. + fiq_lock_t lockval;
  34417. + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
  34418. + * will be sufficient. If we are on a different CPU, then the lock protects us. */
  34419. + prefetchw(&lock->slock);
  34420. + asm volatile (
  34421. + "1: ldrex %0, [%3]\n"
  34422. + " add %1, %0, %4\n"
  34423. + " strex %2, %1, [%3]\n"
  34424. + " teq %2, #0\n"
  34425. + " bne 1b"
  34426. + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
  34427. + : "r" (&lock->slock), "I" (1 << 16)
  34428. + : "cc");
  34429. +
  34430. + while (lockval.tickets.next != lockval.tickets.owner) {
  34431. + wfe();
  34432. + lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
  34433. + }
  34434. + smp_mb();
  34435. +}
  34436. +#else
  34437. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock) { }
  34438. +#endif
  34439. +
  34440. +/**
  34441. + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
  34442. + */
  34443. +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
  34444. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
  34445. +{
  34446. + smp_mb();
  34447. + lock->tickets.owner++;
  34448. + dsb_sev();
  34449. +}
  34450. +#else
  34451. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock) { }
  34452. +#endif
  34453. +
  34454. +/**
  34455. + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
  34456. + * @channel: channel to re-enable
  34457. + */
  34458. +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
  34459. +{
  34460. + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
  34461. +
  34462. + hcchar.b.chen = 0;
  34463. + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
  34464. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34465. + /* Hardware bug workaround: update the ssplit index */
  34466. + if (st->channel[n].hcsplt_copy.b.spltena)
  34467. + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  34468. +
  34469. + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  34470. + }
  34471. +
  34472. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  34473. + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  34474. + hcchar.b.chen = 1;
  34475. +
  34476. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  34477. + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
  34478. +}
  34479. +
  34480. +/**
  34481. + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
  34482. + * @st: Pointer to the channel's state
  34483. + * @n : channel number
  34484. + *
  34485. + * Change host channel registers to perform a complete-split transaction. Being mindful of the
  34486. + * endpoint direction, set control regs up correctly.
  34487. + */
  34488. +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
  34489. +{
  34490. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
  34491. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34492. +
  34493. + hcsplt.b.compsplt = 1;
  34494. + if (st->channel[n].hcchar_copy.b.epdir == 1) {
  34495. + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
  34496. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  34497. + } else {
  34498. + // If OUT, the CSPLIT result contains handshake only.
  34499. + hctsiz.b.xfersize = 0;
  34500. + }
  34501. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  34502. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34503. + mb();
  34504. +}
  34505. +
  34506. +/**
  34507. + * fiq_fsm_restart_np_pending() - Restart a single non-periodic contended transfer
  34508. + * @st: Pointer to the channel's state
  34509. + * @num_channels: Total number of host channels
  34510. + * @orig_channel: Channel index of completed transfer
  34511. + *
  34512. + * In the case where an IN and OUT transfer are simultaneously scheduled to the
  34513. + * same device/EP, inadequate hub implementations will misbehave. Once the first
  34514. + * transfer is complete, a pending non-periodic split can then be issued.
  34515. + */
  34516. +static void notrace fiq_fsm_restart_np_pending(struct fiq_state *st, int num_channels, int orig_channel)
  34517. +{
  34518. + int i;
  34519. + int dev_addr = st->channel[orig_channel].hcchar_copy.b.devaddr;
  34520. + int ep_num = st->channel[orig_channel].hcchar_copy.b.epnum;
  34521. + for (i = 0; i < num_channels; i++) {
  34522. + if (st->channel[i].fsm == FIQ_NP_SSPLIT_PENDING &&
  34523. + st->channel[i].hcchar_copy.b.devaddr == dev_addr &&
  34524. + st->channel[i].hcchar_copy.b.epnum == ep_num) {
  34525. + st->channel[i].fsm = FIQ_NP_SSPLIT_STARTED;
  34526. + fiq_fsm_restart_channel(st, i, 0);
  34527. + break;
  34528. + }
  34529. + }
  34530. +}
  34531. +
  34532. +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
  34533. +{
  34534. + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
  34535. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34536. +
  34537. + if (st->channel[n].hcchar_copy.b.epdir == 0) {
  34538. + return st->channel[n].hctsiz_copy.b.xfersize;
  34539. + } else {
  34540. + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
  34541. + }
  34542. +
  34543. +}
  34544. +
  34545. +
  34546. +/**
  34547. + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
  34548. + *
  34549. + * Of use only for IN periodic transfers.
  34550. + */
  34551. +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
  34552. +{
  34553. + hcdma_data_t hcdma;
  34554. + int i = st->channel[n].dma_info.index;
  34555. + int len;
  34556. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  34557. +
  34558. + len = fiq_get_xfer_len(st, n);
  34559. + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
  34560. + st->channel[n].dma_info.slot_len[i] = len;
  34561. + i++;
  34562. + if (i > 6)
  34563. + BUG();
  34564. +
  34565. + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
  34566. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34567. + st->channel[n].dma_info.index = i;
  34568. + return 0;
  34569. +}
  34570. +
  34571. +/**
  34572. + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
  34573. + */
  34574. +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
  34575. +{
  34576. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34577. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  34578. + hctsiz.b.pktcnt = 1;
  34579. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34580. +}
  34581. +
  34582. +/**
  34583. + * fiq_iso_out_advance() - update DMA address and split position bits
  34584. + * for isochronous OUT transactions.
  34585. + *
  34586. + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
  34587. + * Split-BEGIN states are not handled - this is done when the transaction was queued.
  34588. + *
  34589. + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
  34590. + */
  34591. +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
  34592. +{
  34593. + hcsplt_data_t hcsplt;
  34594. + hctsiz_data_t hctsiz;
  34595. + hcdma_data_t hcdma;
  34596. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  34597. + int last = 0;
  34598. + int i = st->channel[n].dma_info.index;
  34599. +
  34600. + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
  34601. + i++;
  34602. + if (i == 4)
  34603. + last = 1;
  34604. + if (st->channel[n].dma_info.slot_len[i+1] == 255)
  34605. + last = 1;
  34606. +
  34607. + /* New DMA address - address of bounce buffer referred to in index */
  34608. + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
  34609. + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n));
  34610. + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
  34611. + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
  34612. + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
  34613. + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
  34614. + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
  34615. + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
  34616. + /* Set up new packet length */
  34617. + hctsiz.b.pktcnt = 1;
  34618. + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
  34619. + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
  34620. +
  34621. + st->channel[n].dma_info.index++;
  34622. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  34623. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34624. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34625. + return last;
  34626. +}
  34627. +
  34628. +/**
  34629. + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
  34630. + *
  34631. + * Despite the limitations of the DWC core, we can force a microframe pipeline of
  34632. + * isochronous OUT start-split transactions while waiting for a corresponding other-type
  34633. + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
  34634. + * is very unlikely that filling the start-split FIFO will cause data loss.
  34635. + * This allows much better interleaving of transactions in an order-independent way-
  34636. + * there is no requirement to prioritise isochronous, just a state-space search has
  34637. + * to be performed on each periodic start-split complete interrupt.
  34638. + */
  34639. +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
  34640. +{
  34641. + int hub_addr = st->channel[n].hub_addr;
  34642. + int port_addr = st->channel[n].port_addr;
  34643. + int i, poked = 0;
  34644. + for (i = 0; i < num_channels; i++) {
  34645. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  34646. + continue;
  34647. + if (st->channel[i].hub_addr == hub_addr &&
  34648. + st->channel[i].port_addr == port_addr) {
  34649. + switch (st->channel[i].fsm) {
  34650. + case FIQ_PER_ISO_OUT_PENDING:
  34651. + if (st->channel[i].nrpackets == 1) {
  34652. + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
  34653. + } else {
  34654. + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34655. + }
  34656. + fiq_fsm_restart_channel(st, i, 0);
  34657. + poked = 1;
  34658. + break;
  34659. +
  34660. + default:
  34661. + break;
  34662. + }
  34663. + }
  34664. + if (poked)
  34665. + break;
  34666. + }
  34667. + return poked;
  34668. +}
  34669. +
  34670. +/**
  34671. + * fiq_fsm_tt_in_use() - search for host channels using this TT
  34672. + * @n: Channel to use as reference
  34673. + *
  34674. + */
  34675. +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
  34676. +{
  34677. + int hub_addr = st->channel[n].hub_addr;
  34678. + int port_addr = st->channel[n].port_addr;
  34679. + int i, in_use = 0;
  34680. + for (i = 0; i < num_channels; i++) {
  34681. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  34682. + continue;
  34683. + switch (st->channel[i].fsm) {
  34684. + /* TT is reserved for channels that are in the middle of a periodic
  34685. + * split transaction.
  34686. + */
  34687. + case FIQ_PER_SSPLIT_STARTED:
  34688. + case FIQ_PER_CSPLIT_WAIT:
  34689. + case FIQ_PER_CSPLIT_NYET1:
  34690. + //case FIQ_PER_CSPLIT_POLL:
  34691. + case FIQ_PER_ISO_OUT_ACTIVE:
  34692. + case FIQ_PER_ISO_OUT_LAST:
  34693. + if (st->channel[i].hub_addr == hub_addr &&
  34694. + st->channel[i].port_addr == port_addr) {
  34695. + in_use = 1;
  34696. + }
  34697. + break;
  34698. + default:
  34699. + break;
  34700. + }
  34701. + if (in_use)
  34702. + break;
  34703. + }
  34704. + return in_use;
  34705. +}
  34706. +
  34707. +/**
  34708. + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
  34709. + * to be issued for this IN transaction.
  34710. + *
  34711. + * We cannot tell the inbound PID of a data packet due to hardware limitations.
  34712. + * we need to make an educated guess as to whether we need to queue another CSPLIT
  34713. + * or not. A no-brainer is when we have received enough data to fill the endpoint
  34714. + * size, but for endpoints that give variable-length data then we have to resort
  34715. + * to heuristics.
  34716. + *
  34717. + * We also return whether this is the last CSPLIT to be queued, again based on
  34718. + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
  34719. + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
  34720. + */
  34721. +
  34722. +/*
  34723. + * We need some way of guaranteeing if a returned periodic packet of size X
  34724. + * has a DATA0 PID.
  34725. + * The heuristic value of 144 bytes assumes that the received data has maximal
  34726. + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
  34727. + * permissible limit. If the transfer length results in a final packet size
  34728. + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
  34729. + * Also used to ensure that an endpoint will nominally only return a single
  34730. + * complete-split worth of data.
  34731. + */
  34732. +#define DATA0_PID_HEURISTIC 144
  34733. +
  34734. +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
  34735. +{
  34736. +
  34737. + int i;
  34738. + int total_len = 0;
  34739. + int more_needed = 1;
  34740. + struct fiq_channel_state *st = &state->channel[n];
  34741. +
  34742. + for (i = 0; i < st->dma_info.index; i++) {
  34743. + total_len += st->dma_info.slot_len[i];
  34744. + }
  34745. +
  34746. + *probably_last = 0;
  34747. +
  34748. + if (st->hcchar_copy.b.eptype == 0x3) {
  34749. + /*
  34750. + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
  34751. + * then this is definitely the last CSPLIT.
  34752. + */
  34753. + *probably_last = 1;
  34754. + } else {
  34755. + /* Isoc IN. This is a bit risky if we are the first transaction:
  34756. + * we may have been held off slightly. */
  34757. + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
  34758. + more_needed = 0;
  34759. + }
  34760. + /* If in the next uframe we will receive enough data to fill the endpoint,
  34761. + * then only issue 1 more csplit.
  34762. + */
  34763. + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
  34764. + *probably_last = 1;
  34765. + }
  34766. +
  34767. + if (total_len >= st->hctsiz_copy.b.xfersize ||
  34768. + i == 6 || total_len == 0)
  34769. + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
  34770. + * a single endpoint. Accepting more would completely break our scheduling mechanism though
  34771. + * - in these extreme cases we will pass through a truncated packet.
  34772. + */
  34773. + more_needed = 0;
  34774. +
  34775. + return more_needed;
  34776. +}
  34777. +
  34778. +/**
  34779. + * fiq_fsm_too_late() - Test transaction for lateness
  34780. + *
  34781. + * If a SSPLIT for a large IN transaction is issued too late in a frame,
  34782. + * the hub will disable the port to the device and respond with ERR handshakes.
  34783. + * The hub status endpoint will not reflect this change.
  34784. + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
  34785. + */
  34786. +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
  34787. +{
  34788. + int uframe;
  34789. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34790. + uframe = hfnum.b.frnum & 0x7;
  34791. + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
  34792. + return 1;
  34793. + } else {
  34794. + return 0;
  34795. + }
  34796. +}
  34797. +
  34798. +
  34799. +/**
  34800. + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
  34801. + *
  34802. + * Search pending transactions in the start-split pending state and queue them.
  34803. + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
  34804. + * Note: we specifically don't do isochronous OUT transactions first because better
  34805. + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
  34806. + */
  34807. +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
  34808. +{
  34809. + int n;
  34810. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34811. + if ((hfnum.b.frnum & 0x7) == 5)
  34812. + return;
  34813. + for (n = 0; n < num_channels; n++) {
  34814. + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
  34815. + /* Check to see if any other transactions are using this TT */
  34816. + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
  34817. + if (!fiq_fsm_too_late(st, n)) {
  34818. + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  34819. + fiq_print(FIQDBG_INT, st, "NEXTPER ");
  34820. + fiq_fsm_restart_channel(st, n, 0);
  34821. + } else {
  34822. + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34823. + }
  34824. + break;
  34825. + }
  34826. + }
  34827. + }
  34828. + for (n = 0; n < num_channels; n++) {
  34829. + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
  34830. + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
  34831. + fiq_print(FIQDBG_INT, st, "NEXTISO ");
  34832. + if (st->channel[n].nrpackets == 1)
  34833. + st->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  34834. + else
  34835. + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34836. + fiq_fsm_restart_channel(st, n, 0);
  34837. + break;
  34838. + }
  34839. + }
  34840. + }
  34841. +}
  34842. +
  34843. +/**
  34844. + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
  34845. + * @state: Pointer to fiq_state
  34846. + * @n: Channel transaction is active on
  34847. + * @hcint: Copy of host channel interrupt register
  34848. + *
  34849. + * Returns 0 if there are no more transactions for this HC to do, 1
  34850. + * otherwise.
  34851. + */
  34852. +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
  34853. +{
  34854. + struct fiq_channel_state *st = &state->channel[n];
  34855. + int xfer_len = 0, nrpackets = 0;
  34856. + hcdma_data_t hcdma;
  34857. + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
  34858. +
  34859. + xfer_len = fiq_get_xfer_len(state, n);
  34860. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
  34861. +
  34862. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
  34863. +
  34864. + st->hs_isoc_info.index++;
  34865. + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
  34866. + return 0;
  34867. + }
  34868. +
  34869. + /* grab the next DMA address offset from the array */
  34870. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
  34871. + FIQ_WRITE(state->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34872. +
  34873. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  34874. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  34875. + * this is always set to the maximum size of the endpoint. */
  34876. + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
  34877. + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
  34878. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  34879. + if (nrpackets == 0)
  34880. + nrpackets = 1;
  34881. + st->hcchar_copy.b.multicnt = nrpackets;
  34882. + st->hctsiz_copy.b.pktcnt = nrpackets;
  34883. +
  34884. + /* Initial PID also needs to be set */
  34885. + if (st->hcchar_copy.b.epdir == 0) {
  34886. + st->hctsiz_copy.b.xfersize = xfer_len;
  34887. + switch (st->hcchar_copy.b.multicnt) {
  34888. + case 1:
  34889. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  34890. + break;
  34891. + case 2:
  34892. + case 3:
  34893. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  34894. + break;
  34895. + }
  34896. +
  34897. + } else {
  34898. + switch (st->hcchar_copy.b.multicnt) {
  34899. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  34900. + case 1:
  34901. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  34902. + break;
  34903. + case 2:
  34904. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  34905. + break;
  34906. + case 3:
  34907. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  34908. + break;
  34909. + }
  34910. + }
  34911. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
  34912. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
  34913. + /* Channel is enabled on hcint handler exit */
  34914. + fiq_print(FIQDBG_INT, state, "HSISOOUT");
  34915. + return 1;
  34916. +}
  34917. +
  34918. +
  34919. +/**
  34920. + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
  34921. + * @state: Pointer to the state struct passed from banked FIQ mode registers.
  34922. + * @num_channels: set according to the DWC hardware configuration
  34923. + *
  34924. + * The SOF handler in FSM mode has two functions
  34925. + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
  34926. + * nothing to do
  34927. + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
  34928. + * of holdoff.
  34929. + *
  34930. + * The second part is architecture-specific to mach-bcm2835 -
  34931. + * a sane interrupt controller would have a mask register for ARM interrupt sources
  34932. + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
  34933. + * number (USB) can be enabled. This means that certain parts of the USB specification
  34934. + * that require "wait a little while, then issue another packet" cannot be fulfilled with
  34935. + * the timing granularity required to achieve optimal throughout. The workaround is to use
  34936. + * the SOF "timer" (125uS) to perform this task.
  34937. + */
  34938. +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
  34939. +{
  34940. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
  34941. + int n;
  34942. + int kick_irq = 0;
  34943. +
  34944. + if ((hfnum.b.frnum & 0x7) == 1) {
  34945. + /* We cannot issue csplits for transactions in the last frame past (n+1).1
  34946. + * Check to see if there are any transactions that are stale.
  34947. + * Boot them out.
  34948. + */
  34949. + for (n = 0; n < num_channels; n++) {
  34950. + switch (state->channel[n].fsm) {
  34951. + case FIQ_PER_CSPLIT_WAIT:
  34952. + case FIQ_PER_CSPLIT_NYET1:
  34953. + case FIQ_PER_CSPLIT_POLL:
  34954. + case FIQ_PER_CSPLIT_LAST:
  34955. + /* Check if we are no longer in the same full-speed frame. */
  34956. + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
  34957. + (hfnum.b.frnum & ~0x7))
  34958. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34959. + break;
  34960. + default:
  34961. + break;
  34962. + }
  34963. + }
  34964. + }
  34965. +
  34966. + for (n = 0; n < num_channels; n++) {
  34967. + switch (state->channel[n].fsm) {
  34968. +
  34969. + case FIQ_NP_SSPLIT_RETRY:
  34970. + case FIQ_NP_IN_CSPLIT_RETRY:
  34971. + case FIQ_NP_OUT_CSPLIT_RETRY:
  34972. + fiq_fsm_restart_channel(state, n, 0);
  34973. + break;
  34974. +
  34975. + case FIQ_HS_ISOC_SLEEPING:
  34976. + /* Is it time to wake this channel yet? */
  34977. + if (--state->channel[n].uframe_sleeps == 0) {
  34978. + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
  34979. + fiq_fsm_restart_channel(state, n, 0);
  34980. + }
  34981. + break;
  34982. +
  34983. + case FIQ_PER_SSPLIT_QUEUED:
  34984. + if ((hfnum.b.frnum & 0x7) == 5)
  34985. + break;
  34986. + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
  34987. + if (!fiq_fsm_too_late(state, n)) {
  34988. + fiq_print(FIQDBG_INT, state, "SOF GO %01d", n);
  34989. + fiq_fsm_restart_channel(state, n, 0);
  34990. + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  34991. + } else {
  34992. + /* Transaction cannot be started without risking a device babble error */
  34993. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34994. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  34995. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  34996. + kick_irq |= 1;
  34997. + }
  34998. + }
  34999. + break;
  35000. +
  35001. + case FIQ_PER_ISO_OUT_PENDING:
  35002. + /* Ordinarily, this should be poked after the SSPLIT
  35003. + * complete interrupt for a competing transfer on the same
  35004. + * TT. Doesn't happen for aborted transactions though.
  35005. + */
  35006. + if ((hfnum.b.frnum & 0x7) >= 5)
  35007. + break;
  35008. + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
  35009. + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
  35010. + * that caused this.
  35011. + */
  35012. + fiq_fsm_restart_channel(state, n, 0);
  35013. + fiq_print(FIQDBG_INT, state, "SOF ISOC");
  35014. + if (state->channel[n].nrpackets == 1) {
  35015. + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  35016. + } else {
  35017. + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  35018. + }
  35019. + }
  35020. + break;
  35021. +
  35022. + case FIQ_PER_CSPLIT_WAIT:
  35023. + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
  35024. + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
  35025. + * will utterly bugger this up though.
  35026. + */
  35027. + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
  35028. + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
  35029. + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
  35030. + fiq_fsm_restart_channel(state, n, 0);
  35031. + fiq_fsm_start_next_periodic(state, num_channels);
  35032. +
  35033. + }
  35034. + break;
  35035. +
  35036. + case FIQ_PER_SPLIT_TIMEOUT:
  35037. + case FIQ_DEQUEUE_ISSUED:
  35038. + /* Ugly: we have to force a HCD interrupt.
  35039. + * Poke the mask for the channel in question.
  35040. + * We will take a fake SOF because of this, but
  35041. + * that's OK.
  35042. + */
  35043. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  35044. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  35045. + kick_irq |= 1;
  35046. + break;
  35047. +
  35048. + default:
  35049. + break;
  35050. + }
  35051. + }
  35052. +
  35053. + if (state->kick_np_queues ||
  35054. + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
  35055. + kick_irq |= 1;
  35056. +
  35057. + return !kick_irq;
  35058. +}
  35059. +
  35060. +
  35061. +/**
  35062. + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
  35063. + * @state: Pointer to the FIQ state struct
  35064. + * @num_channels: Number of channels as per hardware config
  35065. + * @n: channel for which HAINT(i) was raised
  35066. + *
  35067. + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
  35068. + */
  35069. +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
  35070. +{
  35071. + hcint_data_t hcint;
  35072. + hcintmsk_data_t hcintmsk;
  35073. + hcint_data_t hcint_probe;
  35074. + hcchar_data_t hcchar;
  35075. + int handled = 0;
  35076. + int restart = 0;
  35077. + int last_csplit = 0;
  35078. + int start_next_periodic = 0;
  35079. + struct fiq_channel_state *st = &state->channel[n];
  35080. + hfnum_data_t hfnum;
  35081. +
  35082. + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
  35083. + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
  35084. + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
  35085. +
  35086. + if (st->fsm != FIQ_PASSTHROUGH) {
  35087. + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
  35088. + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
  35089. + }
  35090. +
  35091. + switch (st->fsm) {
  35092. +
  35093. + case FIQ_PASSTHROUGH:
  35094. + case FIQ_DEQUEUE_ISSUED:
  35095. + /* doesn't belong to us, kick it upstairs */
  35096. + break;
  35097. +
  35098. + case FIQ_PASSTHROUGH_ERRORSTATE:
  35099. + /* We are here to emulate the error recovery mechanism of the dwc HCD.
  35100. + * Several interrupts are unmasked if a previous transaction failed - it's
  35101. + * death for the FIQ to attempt to handle them as the channel isn't halted.
  35102. + * Emulate what the HCD does in this situation: mask and continue.
  35103. + * The FSM has no other state setup so this has to be handled out-of-band.
  35104. + */
  35105. + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
  35106. + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
  35107. + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
  35108. + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
  35109. + * interrupt, after the device has disappeared.
  35110. + */
  35111. + if (!hcint.b.xacterr)
  35112. + st->nr_errors = 0;
  35113. + hcintmsk.b.nak = 0;
  35114. + hcintmsk.b.ack = 0;
  35115. + hcintmsk.b.datatglerr = 0;
  35116. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
  35117. + return 1;
  35118. + }
  35119. + if (hcint_probe.b.chhltd) {
  35120. + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
  35121. + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
  35122. + return 0;
  35123. + }
  35124. + break;
  35125. +
  35126. + /* Non-periodic state groups */
  35127. + case FIQ_NP_SSPLIT_STARTED:
  35128. + case FIQ_NP_SSPLIT_RETRY:
  35129. + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
  35130. + if (hcint.b.ack) {
  35131. + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
  35132. + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
  35133. + */
  35134. + if(st->hcchar_copy.b.epdir == 1)
  35135. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  35136. + else
  35137. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35138. + st->nr_errors = 0;
  35139. + handled = 1;
  35140. + fiq_fsm_setup_csplit(state, n);
  35141. + } else if (hcint.b.nak) {
  35142. + // No buffer space in TT. Retry on a uframe boundary.
  35143. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  35144. + handled = 1;
  35145. + } else if (hcint.b.xacterr) {
  35146. + // The only other one we care about is xacterr. This implies HS bus error - retry.
  35147. + st->nr_errors++;
  35148. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  35149. + if (st->nr_errors >= 3) {
  35150. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35151. + } else {
  35152. + handled = 1;
  35153. + restart = 1;
  35154. + }
  35155. + } else {
  35156. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35157. + handled = 0;
  35158. + restart = 0;
  35159. + }
  35160. + break;
  35161. +
  35162. + case FIQ_NP_IN_CSPLIT_RETRY:
  35163. + /* Received a CSPLIT done interrupt.
  35164. + * Expected Data/NAK/STALL/NYET for IN.
  35165. + */
  35166. + if (hcint.b.xfercomp) {
  35167. + /* For IN, data is present. */
  35168. + st->fsm = FIQ_NP_SPLIT_DONE;
  35169. + } else if (hcint.b.nak) {
  35170. + /* no endpoint data. Punt it upstairs */
  35171. + st->fsm = FIQ_NP_SPLIT_DONE;
  35172. + } else if (hcint.b.nyet) {
  35173. + /* CSPLIT NYET - retry on a uframe boundary. */
  35174. + handled = 1;
  35175. + st->nr_errors = 0;
  35176. + } else if (hcint.b.datatglerr) {
  35177. + /* data toggle errors do not set the xfercomp bit. */
  35178. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35179. + } else if (hcint.b.xacterr) {
  35180. + /* HS error. Retry immediate */
  35181. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  35182. + st->nr_errors++;
  35183. + if (st->nr_errors >= 3) {
  35184. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35185. + } else {
  35186. + handled = 1;
  35187. + restart = 1;
  35188. + }
  35189. + } else if (hcint.b.stall || hcint.b.bblerr) {
  35190. + /* A STALL implies either a LS bus error or a genuine STALL. */
  35191. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35192. + } else {
  35193. + /* Hardware bug. It's possible in some cases to
  35194. + * get a channel halt with nothing else set when
  35195. + * the response was a NYET. Treat as local 3-strikes retry.
  35196. + */
  35197. + hcint_data_t hcint_test = hcint;
  35198. + hcint_test.b.chhltd = 0;
  35199. + if (!hcint_test.d32) {
  35200. + st->nr_errors++;
  35201. + if (st->nr_errors >= 3) {
  35202. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35203. + } else {
  35204. + handled = 1;
  35205. + }
  35206. + } else {
  35207. + /* Bail out if something unexpected happened */
  35208. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35209. + }
  35210. + }
  35211. + if (st->fsm != FIQ_NP_IN_CSPLIT_RETRY) {
  35212. + fiq_fsm_restart_np_pending(state, num_channels, n);
  35213. + }
  35214. + break;
  35215. +
  35216. + case FIQ_NP_OUT_CSPLIT_RETRY:
  35217. + /* Received a CSPLIT done interrupt.
  35218. + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
  35219. + if (hcint.b.xfercomp) {
  35220. + st->fsm = FIQ_NP_SPLIT_DONE;
  35221. + } else if (hcint.b.nak) {
  35222. + // The HCD will implement the holdoff on frame boundaries.
  35223. + st->fsm = FIQ_NP_SPLIT_DONE;
  35224. + } else if (hcint.b.nyet) {
  35225. + // Hub still processing.
  35226. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35227. + handled = 1;
  35228. + st->nr_errors = 0;
  35229. + //restart = 1;
  35230. + } else if (hcint.b.xacterr) {
  35231. + /* HS error. retry immediate */
  35232. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35233. + st->nr_errors++;
  35234. + if (st->nr_errors >= 3) {
  35235. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35236. + } else {
  35237. + handled = 1;
  35238. + restart = 1;
  35239. + }
  35240. + } else if (hcint.b.stall) {
  35241. + /* LS bus error or genuine stall */
  35242. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35243. + } else {
  35244. + /*
  35245. + * Hardware bug. It's possible in some cases to get a
  35246. + * channel halt with nothing else set when the response was a NYET.
  35247. + * Treat as local 3-strikes retry.
  35248. + */
  35249. + hcint_data_t hcint_test = hcint;
  35250. + hcint_test.b.chhltd = 0;
  35251. + if (!hcint_test.d32) {
  35252. + st->nr_errors++;
  35253. + if (st->nr_errors >= 3) {
  35254. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35255. + } else {
  35256. + handled = 1;
  35257. + }
  35258. + } else {
  35259. + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
  35260. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35261. + }
  35262. + }
  35263. + if (st->fsm != FIQ_NP_OUT_CSPLIT_RETRY) {
  35264. + fiq_fsm_restart_np_pending(state, num_channels, n);
  35265. + }
  35266. + break;
  35267. +
  35268. + /* Periodic split states (except isoc out) */
  35269. + case FIQ_PER_SSPLIT_STARTED:
  35270. + /* Expect an ACK or failure for SSPLIT */
  35271. + if (hcint.b.ack) {
  35272. + /*
  35273. + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
  35274. + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
  35275. + * point for microframe n-3, the packet will not appear on the bus until microframe n.
  35276. + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
  35277. + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
  35278. + * coincident with SOF for n+1.
  35279. + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
  35280. + * These appear to be caused by timing/clock crossing bugs within the core itself.
  35281. + * State machine workaround.
  35282. + */
  35283. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35284. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35285. + fiq_fsm_setup_csplit(state, n);
  35286. + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
  35287. + * time. If not, then we're in the next SOF.
  35288. + */
  35289. + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
  35290. + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
  35291. + st->expected_uframe = hfnum.b.frnum;
  35292. + st->fsm = FIQ_PER_CSPLIT_WAIT;
  35293. + } else {
  35294. + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
  35295. + /* For isochronous IN endpoints,
  35296. + * we need to hold off if we are expecting a lot of data */
  35297. + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
  35298. + start_next_periodic = 1;
  35299. + }
  35300. + /* Danger will robinson: we are in a broken state. If our first interrupt after
  35301. + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
  35302. + * lag. Unmask the NYET interrupt.
  35303. + */
  35304. + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  35305. + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
  35306. + restart = 1;
  35307. + }
  35308. + handled = 1;
  35309. + } else if (hcint.b.xacterr) {
  35310. + /* 3-strikes retry is enabled, we have hit our max nr_errors */
  35311. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35312. + start_next_periodic = 1;
  35313. + } else {
  35314. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35315. + start_next_periodic = 1;
  35316. + }
  35317. + /* We can now queue the next isochronous OUT transaction, if one is pending. */
  35318. + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
  35319. + fiq_print(FIQDBG_INT, state, "NEXTISO ");
  35320. + }
  35321. + break;
  35322. +
  35323. + case FIQ_PER_CSPLIT_NYET1:
  35324. + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
  35325. + * we are too late and the TT has dropped its CSPLIT fifo.
  35326. + */
  35327. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35328. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35329. + start_next_periodic = 1;
  35330. + if (hcint.b.nak) {
  35331. + st->fsm = FIQ_PER_SPLIT_DONE;
  35332. + } else if (hcint.b.xfercomp) {
  35333. + fiq_increment_dma_buf(state, num_channels, n);
  35334. + st->fsm = FIQ_PER_CSPLIT_POLL;
  35335. + st->nr_errors = 0;
  35336. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35337. + handled = 1;
  35338. + restart = 1;
  35339. + if (!last_csplit)
  35340. + start_next_periodic = 0;
  35341. + } else {
  35342. + st->fsm = FIQ_PER_SPLIT_DONE;
  35343. + }
  35344. + } else if (hcint.b.nyet) {
  35345. + /* Doh. Data lost. */
  35346. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35347. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35348. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35349. + } else {
  35350. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35351. + }
  35352. + break;
  35353. +
  35354. + case FIQ_PER_CSPLIT_BROKEN_NYET1:
  35355. + /*
  35356. + * we got here because our host channel is in the delayed-interrupt
  35357. + * state and we cannot take a NYET interrupt any later than when it
  35358. + * occurred. Disable then re-enable the channel if this happens to force
  35359. + * CSPLITs to occur at the right time.
  35360. + */
  35361. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35362. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35363. + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
  35364. + if (hcint.b.nak) {
  35365. + st->fsm = FIQ_PER_SPLIT_DONE;
  35366. + start_next_periodic = 1;
  35367. + } else if (hcint.b.xfercomp) {
  35368. + fiq_increment_dma_buf(state, num_channels, n);
  35369. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35370. + st->fsm = FIQ_PER_CSPLIT_POLL;
  35371. + handled = 1;
  35372. + restart = 1;
  35373. + start_next_periodic = 1;
  35374. + /* Reload HCTSIZ for the next transfer */
  35375. + fiq_fsm_reload_hctsiz(state, n);
  35376. + if (!last_csplit)
  35377. + start_next_periodic = 0;
  35378. + } else {
  35379. + st->fsm = FIQ_PER_SPLIT_DONE;
  35380. + }
  35381. + } else if (hcint.b.nyet) {
  35382. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35383. + start_next_periodic = 1;
  35384. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35385. + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
  35386. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35387. + } else {
  35388. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35389. + }
  35390. + break;
  35391. +
  35392. + case FIQ_PER_CSPLIT_POLL:
  35393. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35394. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35395. + start_next_periodic = 1;
  35396. + if (hcint.b.nak) {
  35397. + st->fsm = FIQ_PER_SPLIT_DONE;
  35398. + } else if (hcint.b.xfercomp) {
  35399. + fiq_increment_dma_buf(state, num_channels, n);
  35400. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35401. + handled = 1;
  35402. + restart = 1;
  35403. + /* Reload HCTSIZ for the next transfer */
  35404. + fiq_fsm_reload_hctsiz(state, n);
  35405. + if (!last_csplit)
  35406. + start_next_periodic = 0;
  35407. + } else {
  35408. + st->fsm = FIQ_PER_SPLIT_DONE;
  35409. + }
  35410. + } else if (hcint.b.nyet) {
  35411. + /* Are we a NYET after the first data packet? */
  35412. + if (st->nrpackets == 0) {
  35413. + st->fsm = FIQ_PER_CSPLIT_NYET1;
  35414. + handled = 1;
  35415. + restart = 1;
  35416. + } else {
  35417. + /* We got a NYET when polling CSPLITs. Can happen
  35418. + * if our heuristic fails, or if someone disables us
  35419. + * for any significant length of time.
  35420. + */
  35421. + if (st->nr_errors >= 3) {
  35422. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35423. + } else {
  35424. + st->fsm = FIQ_PER_SPLIT_DONE;
  35425. + }
  35426. + }
  35427. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35428. + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
  35429. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35430. + } else {
  35431. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35432. + }
  35433. + break;
  35434. +
  35435. + case FIQ_HS_ISOC_TURBO:
  35436. + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
  35437. + /* more transactions to come */
  35438. + handled = 1;
  35439. + fiq_print(FIQDBG_INT, state, "HSISO M ");
  35440. + /* For strided transfers, put ourselves to sleep */
  35441. + if (st->hs_isoc_info.stride > 1) {
  35442. + st->uframe_sleeps = st->hs_isoc_info.stride - 1;
  35443. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  35444. + } else {
  35445. + restart = 1;
  35446. + }
  35447. + } else {
  35448. + st->fsm = FIQ_HS_ISOC_DONE;
  35449. + fiq_print(FIQDBG_INT, state, "HSISO F ");
  35450. + }
  35451. + break;
  35452. +
  35453. + case FIQ_HS_ISOC_ABORTED:
  35454. + /* This abort is called by the driver rewriting the state mid-transaction
  35455. + * which allows the dequeue mechanism to work more effectively.
  35456. + */
  35457. + break;
  35458. +
  35459. + case FIQ_PER_ISO_OUT_ACTIVE:
  35460. + if (hcint.b.ack) {
  35461. + if(fiq_iso_out_advance(state, num_channels, n)) {
  35462. + /* last OUT transfer */
  35463. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  35464. + /*
  35465. + * Assuming the periodic FIFO in the dwc core
  35466. + * actually does its job properly, we can queue
  35467. + * the next ssplit now and in theory, the wire
  35468. + * transactions will be in-order.
  35469. + */
  35470. + // No it doesn't. It appears to process requests in host channel order.
  35471. + //start_next_periodic = 1;
  35472. + }
  35473. + handled = 1;
  35474. + restart = 1;
  35475. + } else {
  35476. + /*
  35477. + * Isochronous transactions carry on regardless. Log the error
  35478. + * and continue.
  35479. + */
  35480. + //explode += 1;
  35481. + st->nr_errors++;
  35482. + if(fiq_iso_out_advance(state, num_channels, n)) {
  35483. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  35484. + //start_next_periodic = 1;
  35485. + }
  35486. + handled = 1;
  35487. + restart = 1;
  35488. + }
  35489. + break;
  35490. +
  35491. + case FIQ_PER_ISO_OUT_LAST:
  35492. + if (hcint.b.ack) {
  35493. + /* All done here */
  35494. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  35495. + } else {
  35496. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  35497. + st->nr_errors++;
  35498. + }
  35499. + start_next_periodic = 1;
  35500. + break;
  35501. +
  35502. + case FIQ_PER_SPLIT_TIMEOUT:
  35503. + /* SOF kicked us because we overran. */
  35504. + start_next_periodic = 1;
  35505. + break;
  35506. +
  35507. + default:
  35508. + break;
  35509. + }
  35510. +
  35511. + if (handled) {
  35512. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
  35513. + } else {
  35514. + /* Copy the regs into the state so the IRQ knows what to do */
  35515. + st->hcint_copy.d32 = hcint.d32;
  35516. + }
  35517. +
  35518. + if (restart) {
  35519. + /* Restart always implies handled. */
  35520. + if (restart == 2) {
  35521. + /* For complete-split INs, the show must go on.
  35522. + * Force a channel restart */
  35523. + fiq_fsm_restart_channel(state, n, 1);
  35524. + } else {
  35525. + fiq_fsm_restart_channel(state, n, 0);
  35526. + }
  35527. + }
  35528. + if (start_next_periodic) {
  35529. + fiq_fsm_start_next_periodic(state, num_channels);
  35530. + }
  35531. + if (st->fsm != FIQ_PASSTHROUGH)
  35532. + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
  35533. +
  35534. + return handled;
  35535. +}
  35536. +
  35537. +
  35538. +/**
  35539. + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
  35540. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  35541. + * @num_channels: set according to the DWC hardware configuration
  35542. + * @dma: pointer to DMA bounce buffers for split transaction slots
  35543. + *
  35544. + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
  35545. + * inside an EHCI or similar host controller regarding split transactions. The DWC core
  35546. + * interrupts each and every time a split transaction packet is received or sent successfully.
  35547. + * This results in either an interrupt storm when everything is working "properly", or
  35548. + * the interrupt latency of the system in general breaks time-sensitive periodic split
  35549. + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
  35550. + * solves these problems.
  35551. + *
  35552. + * Return: void
  35553. + */
  35554. +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
  35555. +{
  35556. + gintsts_data_t gintsts, gintsts_handled;
  35557. + gintmsk_data_t gintmsk;
  35558. + //hfnum_data_t hfnum;
  35559. + haint_data_t haint, haint_handled;
  35560. + haintmsk_data_t haintmsk;
  35561. + int kick_irq = 0;
  35562. +
  35563. + gintsts_handled.d32 = 0;
  35564. + haint_handled.d32 = 0;
  35565. +
  35566. + fiq_fsm_spin_lock(&state->lock);
  35567. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  35568. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  35569. + gintsts.d32 &= gintmsk.d32;
  35570. +
  35571. + if (gintsts.b.sofintr) {
  35572. + /* For FSM mode, SOF is required to keep the state machine advance for
  35573. + * certain stages of the periodic pipeline. It's death to mask this
  35574. + * interrupt in that case.
  35575. + */
  35576. +
  35577. + if (!fiq_fsm_do_sof(state, num_channels)) {
  35578. + /* Kick IRQ once. Queue advancement means that all pending transactions
  35579. + * will get serviced when the IRQ finally executes.
  35580. + */
  35581. + if (state->gintmsk_saved.b.sofintr == 1)
  35582. + kick_irq |= 1;
  35583. + state->gintmsk_saved.b.sofintr = 0;
  35584. + }
  35585. + gintsts_handled.b.sofintr = 1;
  35586. + }
  35587. +
  35588. + if (gintsts.b.hcintr) {
  35589. + int i;
  35590. + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
  35591. + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
  35592. + haint.d32 &= haintmsk.d32;
  35593. + haint_handled.d32 = 0;
  35594. + for (i=0; i<num_channels; i++) {
  35595. + if (haint.b2.chint & (1 << i)) {
  35596. + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
  35597. + /* HCINT was not handled in FIQ
  35598. + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
  35599. + * Mask HAINT(i) but keep top-level hcint unmasked.
  35600. + */
  35601. + state->haintmsk_saved.b2.chint &= ~(1 << i);
  35602. + } else {
  35603. + /* do_hcintr cleaned up after itself, but clear haint */
  35604. + haint_handled.b2.chint |= (1 << i);
  35605. + }
  35606. + }
  35607. + }
  35608. +
  35609. + if (haint_handled.b2.chint) {
  35610. + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
  35611. + }
  35612. +
  35613. + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
  35614. + /*
  35615. + * This is necessary to avoid multiple retriggers of the MPHI in the case
  35616. + * where interrupts are held off and HCINTs start to pile up.
  35617. + * Only wake up the IRQ if a new interrupt came in, was not handled and was
  35618. + * masked.
  35619. + */
  35620. + haintmsk.d32 &= state->haintmsk_saved.d32;
  35621. + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
  35622. + kick_irq |= 1;
  35623. + }
  35624. + /* Top-Level interrupt - always handled because it's level-sensitive */
  35625. + gintsts_handled.b.hcintr = 1;
  35626. + }
  35627. +
  35628. +
  35629. + /* Clear the bits in the saved register that were not handled but were triggered. */
  35630. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  35631. +
  35632. + /* FIQ didn't handle something - mask has changed - write new mask */
  35633. + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
  35634. + gintmsk.d32 &= state->gintmsk_saved.d32;
  35635. + gintmsk.b.sofintr = 1;
  35636. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  35637. +// fiq_print(FIQDBG_INT, state, "KICKGINT");
  35638. +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
  35639. +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
  35640. + kick_irq |= 1;
  35641. + }
  35642. +
  35643. + if (gintsts_handled.d32) {
  35644. + /* Only applies to edge-sensitive bits in GINTSTS */
  35645. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  35646. + }
  35647. +
  35648. + /* We got an interrupt, didn't handle it. */
  35649. + if (kick_irq) {
  35650. + state->mphi_int_count++;
  35651. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  35652. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  35653. +
  35654. + }
  35655. + state->fiq_done++;
  35656. + mb();
  35657. + fiq_fsm_spin_unlock(&state->lock);
  35658. +}
  35659. +
  35660. +
  35661. +/**
  35662. + * dwc_otg_fiq_nop() - FIQ "lite"
  35663. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  35664. + *
  35665. + * The "nop" handler does not intervene on any interrupts other than SOF.
  35666. + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
  35667. + * with non-periodic/periodic queues) needs to be kicked.
  35668. + *
  35669. + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
  35670. + *
  35671. + * Return: void
  35672. + */
  35673. +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
  35674. +{
  35675. + gintsts_data_t gintsts, gintsts_handled;
  35676. + gintmsk_data_t gintmsk;
  35677. + hfnum_data_t hfnum;
  35678. +
  35679. + fiq_fsm_spin_lock(&state->lock);
  35680. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35681. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  35682. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  35683. + gintsts.d32 &= gintmsk.d32;
  35684. + gintsts_handled.d32 = 0;
  35685. +
  35686. + if (gintsts.b.sofintr) {
  35687. + if (!state->kick_np_queues &&
  35688. + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
  35689. + /* SOF handled, no work to do, just ACK interrupt */
  35690. + gintsts_handled.b.sofintr = 1;
  35691. + } else {
  35692. + /* Kick IRQ */
  35693. + state->gintmsk_saved.b.sofintr = 0;
  35694. + }
  35695. + }
  35696. +
  35697. + /* Reset handled interrupts */
  35698. + if(gintsts_handled.d32) {
  35699. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  35700. + }
  35701. +
  35702. + /* Clear the bits in the saved register that were not handled but were triggered. */
  35703. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  35704. +
  35705. + /* We got an interrupt, didn't handle it and want to mask it */
  35706. + if (~(state->gintmsk_saved.d32)) {
  35707. + state->mphi_int_count++;
  35708. + gintmsk.d32 &= state->gintmsk_saved.d32;
  35709. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  35710. + /* Force a clear before another dummy send */
  35711. + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
  35712. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  35713. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  35714. +
  35715. + }
  35716. + state->fiq_done++;
  35717. + mb();
  35718. + fiq_fsm_spin_unlock(&state->lock);
  35719. +}
  35720. --- /dev/null
  35721. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  35722. @@ -0,0 +1,372 @@
  35723. +/*
  35724. + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
  35725. + *
  35726. + * Copyright (c) 2013 Raspberry Pi Foundation
  35727. + *
  35728. + * Author: Jonathan Bell <[email protected]>
  35729. + * All rights reserved.
  35730. + *
  35731. + * Redistribution and use in source and binary forms, with or without
  35732. + * modification, are permitted provided that the following conditions are met:
  35733. + * * Redistributions of source code must retain the above copyright
  35734. + * notice, this list of conditions and the following disclaimer.
  35735. + * * Redistributions in binary form must reproduce the above copyright
  35736. + * notice, this list of conditions and the following disclaimer in the
  35737. + * documentation and/or other materials provided with the distribution.
  35738. + * * Neither the name of Raspberry Pi nor the
  35739. + * names of its contributors may be used to endorse or promote products
  35740. + * derived from this software without specific prior written permission.
  35741. + *
  35742. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  35743. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  35744. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35745. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  35746. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35747. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35748. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35749. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35750. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35751. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35752. + *
  35753. + * This FIQ implements functionality that performs split transactions on
  35754. + * the dwc_otg hardware without any outside intervention. A split transaction
  35755. + * is "queued" by nominating a specific host channel to perform the entirety
  35756. + * of a split transaction. This FIQ will then perform the microframe-precise
  35757. + * scheduling required in each phase of the transaction until completion.
  35758. + *
  35759. + * The FIQ functionality has been surgically implanted into the Synopsys
  35760. + * vendor-provided driver.
  35761. + *
  35762. + */
  35763. +
  35764. +#ifndef DWC_OTG_FIQ_FSM_H_
  35765. +#define DWC_OTG_FIQ_FSM_H_
  35766. +
  35767. +#include "dwc_otg_regs.h"
  35768. +#include "dwc_otg_cil.h"
  35769. +#include "dwc_otg_hcd.h"
  35770. +#include <linux/kernel.h>
  35771. +#include <linux/irqflags.h>
  35772. +#include <linux/string.h>
  35773. +#include <asm/barrier.h>
  35774. +
  35775. +#if 0
  35776. +#define FLAME_ON(x) \
  35777. +do { \
  35778. + int gpioreg; \
  35779. + \
  35780. + gpioreg = readl(__io_address(0x20200000+0x8)); \
  35781. + gpioreg &= ~(7 << (x-20)*3); \
  35782. + gpioreg |= 0x1 << (x-20)*3; \
  35783. + writel(gpioreg, __io_address(0x20200000+0x8)); \
  35784. + \
  35785. + writel(1<<x, __io_address(0x20200000+(0x1C))); \
  35786. +} while (0)
  35787. +
  35788. +#define FLAME_OFF(x) \
  35789. +do { \
  35790. + writel(1<<x, __io_address(0x20200000+(0x28))); \
  35791. +} while (0)
  35792. +#else
  35793. +#define FLAME_ON(x) do { } while (0)
  35794. +#define FLAME_OFF(X) do { } while (0)
  35795. +#endif
  35796. +
  35797. +/* This is a quick-and-dirty arch-specific register read/write. We know that
  35798. + * writes to a peripheral on BCM2835 will always arrive in-order, also that
  35799. + * reads and writes are executed in-order therefore the need for memory barriers
  35800. + * is obviated if we're only talking to USB.
  35801. + */
  35802. +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
  35803. +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
  35804. +
  35805. +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
  35806. +#define GINTSTS 0x014
  35807. +#define GINTMSK 0x018
  35808. +/* Debug register. Poll the top of the received packets FIFO. */
  35809. +#define GRXSTSR 0x01C
  35810. +#define HFNUM 0x408
  35811. +#define HAINT 0x414
  35812. +#define HAINTMSK 0x418
  35813. +#define HPRT0 0x440
  35814. +
  35815. +/* HC_regs start from an offset of 0x500 */
  35816. +#define HC_START 0x500
  35817. +#define HC_OFFSET 0x020
  35818. +
  35819. +#define HC_DMA 0x514
  35820. +
  35821. +#define HCCHAR 0x00
  35822. +#define HCSPLT 0x04
  35823. +#define HCINT 0x08
  35824. +#define HCINTMSK 0x0C
  35825. +#define HCTSIZ 0x10
  35826. +
  35827. +#define ISOC_XACTPOS_ALL 0b11
  35828. +#define ISOC_XACTPOS_BEGIN 0b10
  35829. +#define ISOC_XACTPOS_MID 0b00
  35830. +#define ISOC_XACTPOS_END 0b01
  35831. +
  35832. +#define DWC_PID_DATA2 0b01
  35833. +#define DWC_PID_MDATA 0b11
  35834. +#define DWC_PID_DATA1 0b10
  35835. +#define DWC_PID_DATA0 0b00
  35836. +
  35837. +typedef struct {
  35838. + volatile void* base;
  35839. + volatile void* ctrl;
  35840. + volatile void* outdda;
  35841. + volatile void* outddb;
  35842. + volatile void* intstat;
  35843. +} mphi_regs_t;
  35844. +
  35845. +enum fiq_debug_level {
  35846. + FIQDBG_SCHED = (1 << 0),
  35847. + FIQDBG_INT = (1 << 1),
  35848. + FIQDBG_ERR = (1 << 2),
  35849. + FIQDBG_PORTHUB = (1 << 3),
  35850. +};
  35851. +
  35852. +typedef struct {
  35853. + union {
  35854. + uint32_t slock;
  35855. + struct _tickets {
  35856. + uint16_t owner;
  35857. + uint16_t next;
  35858. + } tickets;
  35859. + };
  35860. +} fiq_lock_t;
  35861. +
  35862. +struct fiq_state;
  35863. +
  35864. +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
  35865. +#if 0
  35866. +#define fiq_print _fiq_print
  35867. +#else
  35868. +#define fiq_print(x, y, ...)
  35869. +#endif
  35870. +
  35871. +extern bool fiq_enable, fiq_fsm_enable;
  35872. +extern ushort nak_holdoff;
  35873. +
  35874. +/**
  35875. + * enum fiq_fsm_state - The FIQ FSM states.
  35876. + *
  35877. + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
  35878. + * USB2.0 specification for host responses to various transaction states.
  35879. + * There are modifications to this host state machine because of a variety of
  35880. + * quirks and limitations in the dwc_otg hardware.
  35881. + *
  35882. + * The fsm state is also used to communicate back to the driver on completion of
  35883. + * a split transaction. The end states are used in conjunction with the interrupts
  35884. + * raised by the final transaction.
  35885. + */
  35886. +enum fiq_fsm_state {
  35887. + /* FIQ isn't enabled for this host channel */
  35888. + FIQ_PASSTHROUGH = 0,
  35889. + /* For the first interrupt received for this channel,
  35890. + * the FIQ has to ack any interrupts indicating success. */
  35891. + FIQ_PASSTHROUGH_ERRORSTATE = 31,
  35892. + /* Nonperiodic state groups */
  35893. + FIQ_NP_SSPLIT_STARTED = 1,
  35894. + FIQ_NP_SSPLIT_RETRY = 2,
  35895. + /* TT contention - working around hub bugs */
  35896. + FIQ_NP_SSPLIT_PENDING = 33,
  35897. + FIQ_NP_OUT_CSPLIT_RETRY = 3,
  35898. + FIQ_NP_IN_CSPLIT_RETRY = 4,
  35899. + FIQ_NP_SPLIT_DONE = 5,
  35900. + FIQ_NP_SPLIT_LS_ABORTED = 6,
  35901. + /* This differentiates a HS transaction error from a LS one
  35902. + * (handling the hub state is different) */
  35903. + FIQ_NP_SPLIT_HS_ABORTED = 7,
  35904. +
  35905. + /* Periodic state groups */
  35906. + /* Periodic transactions are either started directly by the IRQ handler
  35907. + * or deferred if the TT is already in use.
  35908. + */
  35909. + FIQ_PER_SSPLIT_QUEUED = 8,
  35910. + FIQ_PER_SSPLIT_STARTED = 9,
  35911. + FIQ_PER_SSPLIT_LAST = 10,
  35912. +
  35913. +
  35914. + FIQ_PER_ISO_OUT_PENDING = 11,
  35915. + FIQ_PER_ISO_OUT_ACTIVE = 12,
  35916. + FIQ_PER_ISO_OUT_LAST = 13,
  35917. + FIQ_PER_ISO_OUT_DONE = 27,
  35918. +
  35919. + FIQ_PER_CSPLIT_WAIT = 14,
  35920. + FIQ_PER_CSPLIT_NYET1 = 15,
  35921. + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
  35922. + FIQ_PER_CSPLIT_NYET_FAFF = 29,
  35923. + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
  35924. + FIQ_PER_CSPLIT_POLL = 16,
  35925. + /* The last CSPLIT for a transaction has been issued, differentiates
  35926. + * for the state machine to queue the next packet.
  35927. + */
  35928. + FIQ_PER_CSPLIT_LAST = 17,
  35929. +
  35930. + FIQ_PER_SPLIT_DONE = 18,
  35931. + FIQ_PER_SPLIT_LS_ABORTED = 19,
  35932. + FIQ_PER_SPLIT_HS_ABORTED = 20,
  35933. + FIQ_PER_SPLIT_NYET_ABORTED = 21,
  35934. + /* Frame rollover has occurred without the transaction finishing. */
  35935. + FIQ_PER_SPLIT_TIMEOUT = 22,
  35936. +
  35937. + /* FIQ-accelerated HS Isochronous state groups */
  35938. + FIQ_HS_ISOC_TURBO = 23,
  35939. + /* For interval > 1, SOF wakes up the isochronous FSM */
  35940. + FIQ_HS_ISOC_SLEEPING = 24,
  35941. + FIQ_HS_ISOC_DONE = 25,
  35942. + FIQ_HS_ISOC_ABORTED = 26,
  35943. + FIQ_DEQUEUE_ISSUED = 30,
  35944. + FIQ_TEST = 32,
  35945. +};
  35946. +
  35947. +struct fiq_stack {
  35948. + int magic1;
  35949. + uint8_t stack[2048];
  35950. + int magic2;
  35951. +};
  35952. +
  35953. +
  35954. +/**
  35955. + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
  35956. + * @index: Number of slots reported used for IN transactions / number of slots
  35957. + * transmitted for an OUT transaction
  35958. + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
  35959. + *
  35960. + * Split transaction transfers can have variable length depending on other bus
  35961. + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
  35962. + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
  35963. + * can happen per-frame.
  35964. + */
  35965. +struct fiq_dma_info {
  35966. + u8 index;
  35967. + u8 slot_len[6];
  35968. +};
  35969. +
  35970. +struct __attribute__((packed)) fiq_split_dma_slot {
  35971. + u8 buf[188];
  35972. +};
  35973. +
  35974. +struct fiq_dma_channel {
  35975. + struct __attribute__((packed)) fiq_split_dma_slot index[6];
  35976. +};
  35977. +
  35978. +struct fiq_dma_blob {
  35979. + struct __attribute__((packed)) fiq_dma_channel channel[0];
  35980. +};
  35981. +
  35982. +/**
  35983. + * struct fiq_hs_isoc_info - USB2.0 isochronous data
  35984. + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
  35985. + * @nrframes: Total length of iso_frame_desc array
  35986. + * @index: Current index (FIQ-maintained)
  35987. + * @stride: Interval in uframes between HS isoc transactions
  35988. + */
  35989. +struct fiq_hs_isoc_info {
  35990. + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
  35991. + unsigned int nrframes;
  35992. + unsigned int index;
  35993. + unsigned int stride;
  35994. +};
  35995. +
  35996. +/**
  35997. + * struct fiq_channel_state - FIQ state machine storage
  35998. + * @fsm: Current state of the channel as understood by the FIQ
  35999. + * @nr_errors: Number of transaction errors on this split-transaction
  36000. + * @hub_addr: SSPLIT/CSPLIT destination hub
  36001. + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
  36002. + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
  36003. + * split-IN, number of CSPLIT data packets that were received.
  36004. + * @hcchar_copy:
  36005. + * @hcsplt_copy:
  36006. + * @hcintmsk_copy:
  36007. + * @hctsiz_copy: Copies of the host channel registers.
  36008. + * For use as scratch, or for returning state.
  36009. + *
  36010. + * The fiq_channel_state is state storage between interrupts for a host channel. The
  36011. + * FSM state is stored here. Members of this structure must only be set up by the
  36012. + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
  36013. + * has updated the state to either a COMPLETE state group or ABORT state group.
  36014. + */
  36015. +
  36016. +struct fiq_channel_state {
  36017. + enum fiq_fsm_state fsm;
  36018. + unsigned int nr_errors;
  36019. + unsigned int hub_addr;
  36020. + unsigned int port_addr;
  36021. + /* Hardware bug workaround: sometimes channel halt interrupts are
  36022. + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
  36023. + unsigned int expected_uframe;
  36024. + /* number of uframes remaining (for interval > 1 HS isoc transfers) before next transfer */
  36025. + unsigned int uframe_sleeps;
  36026. + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
  36027. + unsigned int nrpackets;
  36028. + struct fiq_dma_info dma_info;
  36029. + struct fiq_hs_isoc_info hs_isoc_info;
  36030. + /* Copies of HC registers - in/out communication from/to IRQ handler
  36031. + * and for ease of channel setup. A bit of mungeing is performed - for
  36032. + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
  36033. + */
  36034. + hcchar_data_t hcchar_copy;
  36035. + hcsplt_data_t hcsplt_copy;
  36036. + hcint_data_t hcint_copy;
  36037. + hcintmsk_data_t hcintmsk_copy;
  36038. + hctsiz_data_t hctsiz_copy;
  36039. + hcdma_data_t hcdma_copy;
  36040. +};
  36041. +
  36042. +/**
  36043. + * struct fiq_state - top-level FIQ state machine storage
  36044. + * @mphi_regs: virtual address of the MPHI peripheral register file
  36045. + * @dwc_regs_base: virtual address of the base of the DWC core register file
  36046. + * @dma_base: physical address for the base of the DMA bounce buffers
  36047. + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
  36048. + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
  36049. + * Used for determining which interrupts fired to set off the IRQ handler.
  36050. + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
  36051. + * @np_count: Non-periodic transactions in the active queue
  36052. + * @np_sent: Count of non-periodic transactions that have completed
  36053. + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
  36054. + * this is the next frame on which a SOF interrupt is required. Used to hold off
  36055. + * passing SOF through to the driver until necessary.
  36056. + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
  36057. + * channels configured into the core logic.
  36058. + *
  36059. + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
  36060. + * It contains top-level state information.
  36061. + */
  36062. +struct fiq_state {
  36063. + fiq_lock_t lock;
  36064. + mphi_regs_t mphi_regs;
  36065. + void *dwc_regs_base;
  36066. + dma_addr_t dma_base;
  36067. + struct fiq_dma_blob *fiq_dmab;
  36068. + void *dummy_send;
  36069. + gintmsk_data_t gintmsk_saved;
  36070. + haintmsk_data_t haintmsk_saved;
  36071. + int mphi_int_count;
  36072. + unsigned int fiq_done;
  36073. + unsigned int kick_np_queues;
  36074. + unsigned int next_sched_frame;
  36075. +#ifdef FIQ_DEBUG
  36076. + char * buffer;
  36077. + unsigned int bufsiz;
  36078. +#endif
  36079. + struct fiq_channel_state channel[0];
  36080. +};
  36081. +
  36082. +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
  36083. +
  36084. +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
  36085. +
  36086. +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
  36087. +
  36088. +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
  36089. +
  36090. +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
  36091. +
  36092. +extern void dwc_otg_fiq_nop(struct fiq_state *state);
  36093. +
  36094. +#endif /* DWC_OTG_FIQ_FSM_H_ */
  36095. --- /dev/null
  36096. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  36097. @@ -0,0 +1,80 @@
  36098. +/*
  36099. + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
  36100. + *
  36101. + * Copyright (c) 2013 Raspberry Pi Foundation
  36102. + *
  36103. + * Author: Jonathan Bell <[email protected]>
  36104. + * All rights reserved.
  36105. + *
  36106. + * Redistribution and use in source and binary forms, with or without
  36107. + * modification, are permitted provided that the following conditions are met:
  36108. + * * Redistributions of source code must retain the above copyright
  36109. + * notice, this list of conditions and the following disclaimer.
  36110. + * * Redistributions in binary form must reproduce the above copyright
  36111. + * notice, this list of conditions and the following disclaimer in the
  36112. + * documentation and/or other materials provided with the distribution.
  36113. + * * Neither the name of Raspberry Pi nor the
  36114. + * names of its contributors may be used to endorse or promote products
  36115. + * derived from this software without specific prior written permission.
  36116. + *
  36117. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  36118. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  36119. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  36120. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  36121. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36122. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36123. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  36124. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36125. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36126. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36127. + */
  36128. +
  36129. +
  36130. +#include <asm/assembler.h>
  36131. +#include <linux/linkage.h>
  36132. +
  36133. +
  36134. +.text
  36135. +
  36136. +.global _dwc_otg_fiq_stub_end;
  36137. +
  36138. +/**
  36139. + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
  36140. + * a C-style function call with arguments from the FIQ banked registers.
  36141. + * r0 = &hcd->fiq_state
  36142. + * r1 = &hcd->num_channels
  36143. + * r2 = &hcd->dma_buffers
  36144. + * Tramples: r0, r1, r2, r4, fp, ip
  36145. + */
  36146. +
  36147. +ENTRY(_dwc_otg_fiq_stub)
  36148. + /* Stash unbanked regs - SP will have been set up for us */
  36149. + mov ip, sp;
  36150. + stmdb sp!, {r0-r12, lr};
  36151. +#ifdef FIQ_DEBUG
  36152. + // Cycle profiling - read cycle counter at start
  36153. + mrc p15, 0, r5, c15, c12, 1;
  36154. +#endif
  36155. + /* r11 = fp, don't trample it */
  36156. + mov r4, fp;
  36157. + /* set EABI frame size */
  36158. + sub fp, ip, #512;
  36159. +
  36160. + /* for fiq NOP mode - just need state */
  36161. + mov r0, r8;
  36162. + /* r9 = num_channels */
  36163. + mov r1, r9;
  36164. + /* r10 = struct *dma_bufs */
  36165. +// mov r2, r10;
  36166. +
  36167. + /* r4 = &fiq_c_function */
  36168. + blx r4;
  36169. +#ifdef FIQ_DEBUG
  36170. + mrc p15, 0, r4, c15, c12, 1;
  36171. + subs r5, r5, r4;
  36172. + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
  36173. +#endif
  36174. + ldmia sp!, {r0-r12, lr};
  36175. + subs pc, lr, #4;
  36176. +_dwc_otg_fiq_stub_end:
  36177. +END(_dwc_otg_fiq_stub)
  36178. --- /dev/null
  36179. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  36180. @@ -0,0 +1,4283 @@
  36181. +
  36182. +/* ==========================================================================
  36183. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  36184. + * $Revision: #104 $
  36185. + * $Date: 2011/10/24 $
  36186. + * $Change: 1871159 $
  36187. + *
  36188. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  36189. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  36190. + * otherwise expressly agreed to in writing between Synopsys and you.
  36191. + *
  36192. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  36193. + * any End User Software License Agreement or Agreement for Licensed Product
  36194. + * with Synopsys or any supplement thereto. You are permitted to use and
  36195. + * redistribute this Software in source and binary forms, with or without
  36196. + * modification, provided that redistributions of source code must retain this
  36197. + * notice. You may not view, use, disclose, copy or distribute this file or
  36198. + * any information contained herein except pursuant to this license grant from
  36199. + * Synopsys. If you do not agree with this notice, including the disclaimer
  36200. + * below, then you are not authorized to use the Software.
  36201. + *
  36202. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  36203. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36204. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36205. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  36206. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36207. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36208. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36209. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36210. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36211. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  36212. + * DAMAGE.
  36213. + * ========================================================================== */
  36214. +#ifndef DWC_DEVICE_ONLY
  36215. +
  36216. +/** @file
  36217. + * This file implements HCD Core. All code in this file is portable and doesn't
  36218. + * use any OS specific functions.
  36219. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  36220. + * header file.
  36221. + */
  36222. +
  36223. +#include <linux/usb.h>
  36224. +#include <linux/usb/hcd.h>
  36225. +
  36226. +#include "dwc_otg_hcd.h"
  36227. +#include "dwc_otg_regs.h"
  36228. +#include "dwc_otg_fiq_fsm.h"
  36229. +
  36230. +extern bool microframe_schedule;
  36231. +extern uint16_t fiq_fsm_mask, nak_holdoff;
  36232. +
  36233. +//#define DEBUG_HOST_CHANNELS
  36234. +#ifdef DEBUG_HOST_CHANNELS
  36235. +static int last_sel_trans_num_per_scheduled = 0;
  36236. +static int last_sel_trans_num_nonper_scheduled = 0;
  36237. +static int last_sel_trans_num_avail_hc_at_start = 0;
  36238. +static int last_sel_trans_num_avail_hc_at_end = 0;
  36239. +#endif /* DEBUG_HOST_CHANNELS */
  36240. +
  36241. +
  36242. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  36243. +{
  36244. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  36245. +}
  36246. +
  36247. +/**
  36248. + * Connection timeout function. An OTG host is required to display a
  36249. + * message if the device does not connect within 10 seconds.
  36250. + */
  36251. +void dwc_otg_hcd_connect_timeout(void *ptr)
  36252. +{
  36253. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  36254. + DWC_PRINTF("Connect Timeout\n");
  36255. + __DWC_ERROR("Device Not Connected/Responding\n");
  36256. +}
  36257. +
  36258. +#if defined(DEBUG)
  36259. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  36260. +{
  36261. + if (qh->channel != NULL) {
  36262. + dwc_hc_t *hc = qh->channel;
  36263. + dwc_list_link_t *item;
  36264. + dwc_otg_qh_t *qh_item;
  36265. + int num_channels = hcd->core_if->core_params->host_channels;
  36266. + int i;
  36267. +
  36268. + dwc_otg_hc_regs_t *hc_regs;
  36269. + hcchar_data_t hcchar;
  36270. + hcsplt_data_t hcsplt;
  36271. + hctsiz_data_t hctsiz;
  36272. + uint32_t hcdma;
  36273. +
  36274. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  36275. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  36276. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  36277. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  36278. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  36279. +
  36280. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  36281. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  36282. + hcsplt.d32);
  36283. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  36284. + hcdma);
  36285. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  36286. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  36287. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  36288. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  36289. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  36290. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  36291. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  36292. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  36293. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  36294. + DWC_PRINTF(" qh: %p\n", hc->qh);
  36295. + DWC_PRINTF(" NP inactive sched:\n");
  36296. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  36297. + qh_item =
  36298. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36299. + DWC_PRINTF(" %p\n", qh_item);
  36300. + }
  36301. + DWC_PRINTF(" NP active sched:\n");
  36302. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  36303. + qh_item =
  36304. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36305. + DWC_PRINTF(" %p\n", qh_item);
  36306. + }
  36307. + DWC_PRINTF(" Channels: \n");
  36308. + for (i = 0; i < num_channels; i++) {
  36309. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  36310. + DWC_PRINTF(" %2d: %p\n", i, hc);
  36311. + }
  36312. + }
  36313. +}
  36314. +#else
  36315. +#define dump_channel_info(hcd, qh)
  36316. +#endif /* DEBUG */
  36317. +
  36318. +/**
  36319. + * Work queue function for starting the HCD when A-Cable is connected.
  36320. + * The hcd_start() must be called in a process context.
  36321. + */
  36322. +static void hcd_start_func(void *_vp)
  36323. +{
  36324. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  36325. +
  36326. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  36327. + if (hcd) {
  36328. + hcd->fops->start(hcd);
  36329. + }
  36330. +}
  36331. +
  36332. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  36333. +{
  36334. +#ifdef DEBUG
  36335. + int i;
  36336. + int num_channels = hcd->core_if->core_params->host_channels;
  36337. + for (i = 0; i < num_channels; i++) {
  36338. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  36339. + }
  36340. +#endif
  36341. +}
  36342. +
  36343. +static void del_timers(dwc_otg_hcd_t * hcd)
  36344. +{
  36345. + del_xfer_timers(hcd);
  36346. + DWC_TIMER_CANCEL(hcd->conn_timer);
  36347. +}
  36348. +
  36349. +/**
  36350. + * Processes all the URBs in a single list of QHs. Completes them with
  36351. + * -ESHUTDOWN and frees the QTD.
  36352. + */
  36353. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  36354. +{
  36355. + dwc_list_link_t *qh_item, *qh_tmp;
  36356. + dwc_otg_qh_t *qh;
  36357. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  36358. +
  36359. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  36360. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  36361. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  36362. + &qh->qtd_list, qtd_list_entry) {
  36363. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  36364. + if (qtd->urb != NULL) {
  36365. + hcd->fops->complete(hcd, qtd->urb->priv,
  36366. + qtd->urb, -DWC_E_SHUTDOWN);
  36367. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  36368. + }
  36369. +
  36370. + }
  36371. + if(qh->channel) {
  36372. + /* Using hcchar.chen == 1 is not a reliable test.
  36373. + * It is possible that the channel has already halted
  36374. + * but not yet been through the IRQ handler.
  36375. + */
  36376. + if (fiq_fsm_enable && (hcd->fiq_state->channel[qh->channel->hc_num].fsm != FIQ_PASSTHROUGH)) {
  36377. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  36378. + qh->channel->halt_pending = 1;
  36379. + } else {
  36380. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  36381. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  36382. + }
  36383. + qh->channel = NULL;
  36384. + }
  36385. + dwc_otg_hcd_qh_remove(hcd, qh);
  36386. + }
  36387. +}
  36388. +
  36389. +/**
  36390. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  36391. + * and periodic schedules. The QTD associated with each URB is removed from
  36392. + * the schedule and freed. This function may be called when a disconnect is
  36393. + * detected or when the HCD is being stopped.
  36394. + */
  36395. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  36396. +{
  36397. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  36398. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  36399. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  36400. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  36401. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  36402. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  36403. +}
  36404. +
  36405. +/**
  36406. + * Start the connection timer. An OTG host is required to display a
  36407. + * message if the device does not connect within 10 seconds. The
  36408. + * timer is deleted if a port connect interrupt occurs before the
  36409. + * timer expires.
  36410. + */
  36411. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  36412. +{
  36413. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  36414. +}
  36415. +
  36416. +/**
  36417. + * HCD Callback function for disconnect of the HCD.
  36418. + *
  36419. + * @param p void pointer to the <code>struct usb_hcd</code>
  36420. + */
  36421. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  36422. +{
  36423. + dwc_otg_hcd_t *dwc_otg_hcd;
  36424. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  36425. + dwc_otg_hcd = p;
  36426. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  36427. + return 1;
  36428. +}
  36429. +
  36430. +/**
  36431. + * HCD Callback function for starting the HCD when A-Cable is
  36432. + * connected.
  36433. + *
  36434. + * @param p void pointer to the <code>struct usb_hcd</code>
  36435. + */
  36436. +static int32_t dwc_otg_hcd_start_cb(void *p)
  36437. +{
  36438. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36439. + dwc_otg_core_if_t *core_if;
  36440. + hprt0_data_t hprt0;
  36441. +
  36442. + core_if = dwc_otg_hcd->core_if;
  36443. +
  36444. + if (core_if->op_state == B_HOST) {
  36445. + /*
  36446. + * Reset the port. During a HNP mode switch the reset
  36447. + * needs to occur within 1ms and have a duration of at
  36448. + * least 50ms.
  36449. + */
  36450. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  36451. + hprt0.b.prtrst = 1;
  36452. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36453. + }
  36454. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  36455. + hcd_start_func, dwc_otg_hcd, 50,
  36456. + "start hcd");
  36457. +
  36458. + return 1;
  36459. +}
  36460. +
  36461. +/**
  36462. + * HCD Callback function for disconnect of the HCD.
  36463. + *
  36464. + * @param p void pointer to the <code>struct usb_hcd</code>
  36465. + */
  36466. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  36467. +{
  36468. + gintsts_data_t intr;
  36469. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36470. +
  36471. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  36472. + /*
  36473. + * Set status flags for the hub driver.
  36474. + */
  36475. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  36476. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  36477. + if(fiq_enable) {
  36478. + local_fiq_disable();
  36479. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  36480. + }
  36481. + /*
  36482. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  36483. + * interrupt mask and status bits and disabling subsequent host
  36484. + * channel interrupts.
  36485. + */
  36486. + intr.d32 = 0;
  36487. + intr.b.nptxfempty = 1;
  36488. + intr.b.ptxfempty = 1;
  36489. + intr.b.hcintr = 1;
  36490. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  36491. + intr.d32, 0);
  36492. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  36493. + intr.d32, 0);
  36494. +
  36495. + del_timers(dwc_otg_hcd);
  36496. +
  36497. + /*
  36498. + * Turn off the vbus power only if the core has transitioned to device
  36499. + * mode. If still in host mode, need to keep power on to detect a
  36500. + * reconnection.
  36501. + */
  36502. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  36503. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  36504. + hprt0_data_t hprt0 = {.d32 = 0 };
  36505. + DWC_PRINTF("Disconnect: PortPower off\n");
  36506. + hprt0.b.prtpwr = 0;
  36507. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  36508. + hprt0.d32);
  36509. + }
  36510. +
  36511. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  36512. + }
  36513. +
  36514. + /* Respond with an error status to all URBs in the schedule. */
  36515. + kill_all_urbs(dwc_otg_hcd);
  36516. +
  36517. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  36518. + /* Clean up any host channels that were in use. */
  36519. + int num_channels;
  36520. + int i;
  36521. + dwc_hc_t *channel;
  36522. + dwc_otg_hc_regs_t *hc_regs;
  36523. + hcchar_data_t hcchar;
  36524. +
  36525. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  36526. +
  36527. + if (!dwc_otg_hcd->core_if->dma_enable) {
  36528. + /* Flush out any channel requests in slave mode. */
  36529. + for (i = 0; i < num_channels; i++) {
  36530. + channel = dwc_otg_hcd->hc_ptr_array[i];
  36531. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  36532. + (channel, hc_list_entry)) {
  36533. + hc_regs =
  36534. + dwc_otg_hcd->core_if->
  36535. + host_if->hc_regs[i];
  36536. + hcchar.d32 =
  36537. + DWC_READ_REG32(&hc_regs->hcchar);
  36538. + if (hcchar.b.chen) {
  36539. + hcchar.b.chen = 0;
  36540. + hcchar.b.chdis = 1;
  36541. + hcchar.b.epdir = 0;
  36542. + DWC_WRITE_REG32
  36543. + (&hc_regs->hcchar,
  36544. + hcchar.d32);
  36545. + }
  36546. + }
  36547. + }
  36548. + }
  36549. +
  36550. + if(fiq_fsm_enable) {
  36551. + for(i=0; i < 128; i++) {
  36552. + dwc_otg_hcd->hub_port[i] = 0;
  36553. + }
  36554. + }
  36555. + }
  36556. +
  36557. + if(fiq_enable) {
  36558. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  36559. + local_fiq_enable();
  36560. + }
  36561. +
  36562. + if (dwc_otg_hcd->fops->disconnect) {
  36563. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  36564. + }
  36565. +
  36566. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  36567. + return 1;
  36568. +}
  36569. +
  36570. +/**
  36571. + * HCD Callback function for stopping the HCD.
  36572. + *
  36573. + * @param p void pointer to the <code>struct usb_hcd</code>
  36574. + */
  36575. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  36576. +{
  36577. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36578. +
  36579. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  36580. + dwc_otg_hcd_stop(dwc_otg_hcd);
  36581. + return 1;
  36582. +}
  36583. +
  36584. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36585. +/**
  36586. + * HCD Callback function for sleep of HCD.
  36587. + *
  36588. + * @param p void pointer to the <code>struct usb_hcd</code>
  36589. + */
  36590. +static int dwc_otg_hcd_sleep_cb(void *p)
  36591. +{
  36592. + dwc_otg_hcd_t *hcd = p;
  36593. +
  36594. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  36595. +
  36596. + return 0;
  36597. +}
  36598. +#endif
  36599. +
  36600. +
  36601. +/**
  36602. + * HCD Callback function for Remote Wakeup.
  36603. + *
  36604. + * @param p void pointer to the <code>struct usb_hcd</code>
  36605. + */
  36606. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  36607. +{
  36608. + dwc_otg_hcd_t *hcd = p;
  36609. +
  36610. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  36611. + hcd->flags.b.port_suspend_change = 1;
  36612. + }
  36613. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36614. + else {
  36615. + hcd->flags.b.port_l1_change = 1;
  36616. + }
  36617. +#endif
  36618. + return 0;
  36619. +}
  36620. +
  36621. +/**
  36622. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  36623. + * stopped.
  36624. + */
  36625. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  36626. +{
  36627. + hprt0_data_t hprt0 = {.d32 = 0 };
  36628. +
  36629. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  36630. +
  36631. + /*
  36632. + * The root hub should be disconnected before this function is called.
  36633. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  36634. + * and the QH lists (via ..._hcd_endpoint_disable).
  36635. + */
  36636. +
  36637. + /* Turn off all host-specific interrupts. */
  36638. + dwc_otg_disable_host_interrupts(hcd->core_if);
  36639. +
  36640. + /* Turn off the vbus power */
  36641. + DWC_PRINTF("PortPower off\n");
  36642. + hprt0.b.prtpwr = 0;
  36643. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  36644. + dwc_mdelay(1);
  36645. +}
  36646. +
  36647. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  36648. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  36649. + int atomic_alloc)
  36650. +{
  36651. + int retval = 0;
  36652. + uint8_t needs_scheduling = 0;
  36653. + dwc_otg_transaction_type_e tr_type;
  36654. + dwc_otg_qtd_t *qtd;
  36655. + gintmsk_data_t intr_mask = {.d32 = 0 };
  36656. + hprt0_data_t hprt0 = { .d32 = 0 };
  36657. +
  36658. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36659. + if (NULL == hcd->core_if) {
  36660. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  36661. + /* No longer connected. */
  36662. + return -DWC_E_INVALID;
  36663. + }
  36664. +#endif
  36665. + if (!hcd->flags.b.port_connect_status) {
  36666. + /* No longer connected. */
  36667. + DWC_ERROR("Not connected\n");
  36668. + return -DWC_E_NO_DEVICE;
  36669. + }
  36670. +
  36671. + /* Some core configurations cannot support LS traffic on a FS root port */
  36672. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  36673. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  36674. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  36675. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  36676. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  36677. + return -DWC_E_NO_DEVICE;
  36678. + }
  36679. + }
  36680. +
  36681. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  36682. + if (qtd == NULL) {
  36683. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  36684. + return -DWC_E_NO_MEMORY;
  36685. + }
  36686. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36687. + if (qtd->urb == NULL) {
  36688. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  36689. + return -DWC_E_NO_MEMORY;
  36690. + }
  36691. + if (qtd->urb->priv == NULL) {
  36692. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  36693. + return -DWC_E_NO_MEMORY;
  36694. + }
  36695. +#endif
  36696. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  36697. + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
  36698. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  36699. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  36700. + needs_scheduling = 0;
  36701. +
  36702. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  36703. + // creates a new queue in ep_handle if it doesn't exist already
  36704. + if (retval < 0) {
  36705. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  36706. + "Error status %d\n", retval);
  36707. + dwc_otg_hcd_qtd_free(qtd);
  36708. + return retval;
  36709. + }
  36710. +
  36711. + if(needs_scheduling) {
  36712. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  36713. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  36714. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  36715. + }
  36716. + }
  36717. + return retval;
  36718. +}
  36719. +
  36720. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  36721. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  36722. +{
  36723. + dwc_otg_qh_t *qh;
  36724. + dwc_otg_qtd_t *urb_qtd;
  36725. + BUG_ON(!hcd);
  36726. + BUG_ON(!dwc_otg_urb);
  36727. +
  36728. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36729. +
  36730. + if (hcd == NULL) {
  36731. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  36732. + return -DWC_E_INVALID;
  36733. + }
  36734. + if (dwc_otg_urb == NULL) {
  36735. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  36736. + return -DWC_E_INVALID;
  36737. + }
  36738. + if (dwc_otg_urb->qtd == NULL) {
  36739. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  36740. + return -DWC_E_INVALID;
  36741. + }
  36742. + urb_qtd = dwc_otg_urb->qtd;
  36743. + BUG_ON(!urb_qtd);
  36744. + if (urb_qtd->qh == NULL) {
  36745. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  36746. + return -DWC_E_INVALID;
  36747. + }
  36748. +#else
  36749. + urb_qtd = dwc_otg_urb->qtd;
  36750. + BUG_ON(!urb_qtd);
  36751. +#endif
  36752. + qh = urb_qtd->qh;
  36753. + BUG_ON(!qh);
  36754. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  36755. + if (urb_qtd->in_process) {
  36756. + dump_channel_info(hcd, qh);
  36757. + }
  36758. + }
  36759. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36760. + if (hcd->core_if == NULL) {
  36761. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  36762. + return -DWC_E_INVALID;
  36763. + }
  36764. +#endif
  36765. + if (urb_qtd->in_process && qh->channel) {
  36766. + /* The QTD is in process (it has been assigned to a channel). */
  36767. + if (hcd->flags.b.port_connect_status) {
  36768. + int n = qh->channel->hc_num;
  36769. + /*
  36770. + * If still connected (i.e. in host mode), halt the
  36771. + * channel so it can be used for other transfers. If
  36772. + * no longer connected, the host registers can't be
  36773. + * written to halt the channel since the core is in
  36774. + * device mode.
  36775. + */
  36776. + /* In FIQ FSM mode, we need to shut down carefully.
  36777. + * The FIQ may attempt to restart a disabled channel */
  36778. + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
  36779. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  36780. + qh->channel->halt_pending = 1;
  36781. + //hcd->fiq_state->channel[n].fsm = FIQ_DEQUEUE_ISSUED;
  36782. + } else {
  36783. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  36784. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  36785. + }
  36786. + }
  36787. + }
  36788. +
  36789. + /*
  36790. + * Free the QTD and clean up the associated QH. Leave the QH in the
  36791. + * schedule if it has any remaining QTDs.
  36792. + */
  36793. +
  36794. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  36795. + "delete %sQueue handler\n",
  36796. + hcd->core_if->dma_desc_enable?"DMA ":"");
  36797. + if (!hcd->core_if->dma_desc_enable) {
  36798. + uint8_t b = urb_qtd->in_process;
  36799. + if (nak_holdoff && qh->do_split && dwc_qh_is_non_per(qh))
  36800. + qh->nak_frame = 0xFFFF;
  36801. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  36802. + if (b) {
  36803. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  36804. + qh->channel = NULL;
  36805. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  36806. + dwc_otg_hcd_qh_remove(hcd, qh);
  36807. + }
  36808. + } else {
  36809. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  36810. + }
  36811. + return 0;
  36812. +}
  36813. +
  36814. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  36815. + int retry)
  36816. +{
  36817. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  36818. + int retval = 0;
  36819. + dwc_irqflags_t flags;
  36820. +
  36821. + if (retry < 0) {
  36822. + retval = -DWC_E_INVALID;
  36823. + goto done;
  36824. + }
  36825. +
  36826. + if (!qh) {
  36827. + retval = -DWC_E_INVALID;
  36828. + goto done;
  36829. + }
  36830. +
  36831. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36832. +
  36833. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  36834. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36835. + retry--;
  36836. + dwc_msleep(5);
  36837. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36838. + }
  36839. +
  36840. + dwc_otg_hcd_qh_remove(hcd, qh);
  36841. +
  36842. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36843. + /*
  36844. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  36845. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  36846. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  36847. + * and dwc_otg_hcd_frame_list_alloc().
  36848. + */
  36849. + dwc_otg_hcd_qh_free(hcd, qh);
  36850. +
  36851. +done:
  36852. + return retval;
  36853. +}
  36854. +
  36855. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  36856. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  36857. +{
  36858. + int retval = 0;
  36859. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  36860. + if (!qh)
  36861. + return -DWC_E_INVALID;
  36862. +
  36863. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  36864. + return retval;
  36865. +}
  36866. +#endif
  36867. +
  36868. +/**
  36869. + * HCD Callback structure for handling mode switching.
  36870. + */
  36871. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  36872. + .start = dwc_otg_hcd_start_cb,
  36873. + .stop = dwc_otg_hcd_stop_cb,
  36874. + .disconnect = dwc_otg_hcd_disconnect_cb,
  36875. + .session_start = dwc_otg_hcd_session_start_cb,
  36876. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  36877. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36878. + .sleep = dwc_otg_hcd_sleep_cb,
  36879. +#endif
  36880. + .p = 0,
  36881. +};
  36882. +
  36883. +/**
  36884. + * Reset tasklet function
  36885. + */
  36886. +static void reset_tasklet_func(void *data)
  36887. +{
  36888. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  36889. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  36890. + hprt0_data_t hprt0;
  36891. +
  36892. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  36893. +
  36894. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  36895. + hprt0.b.prtrst = 1;
  36896. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36897. + dwc_mdelay(60);
  36898. +
  36899. + hprt0.b.prtrst = 0;
  36900. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36901. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  36902. +}
  36903. +
  36904. +static void completion_tasklet_func(void *ptr)
  36905. +{
  36906. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  36907. + struct urb *urb;
  36908. + urb_tq_entry_t *item;
  36909. + dwc_irqflags_t flags;
  36910. +
  36911. + /* This could just be spin_lock_irq */
  36912. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36913. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  36914. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  36915. + urb = item->urb;
  36916. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  36917. + urb_tq_entries);
  36918. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36919. + DWC_FREE(item);
  36920. +
  36921. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  36922. +
  36923. +
  36924. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36925. + }
  36926. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36927. + return;
  36928. +}
  36929. +
  36930. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  36931. +{
  36932. + dwc_list_link_t *item;
  36933. + dwc_otg_qh_t *qh;
  36934. + dwc_irqflags_t flags;
  36935. +
  36936. + if (!qh_list->next) {
  36937. + /* The list hasn't been initialized yet. */
  36938. + return;
  36939. + }
  36940. + /*
  36941. + * Hold spinlock here. Not needed in that case if bellow
  36942. + * function is being called from ISR
  36943. + */
  36944. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36945. + /* Ensure there are no QTDs or URBs left. */
  36946. + kill_urbs_in_qh_list(hcd, qh_list);
  36947. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36948. +
  36949. + DWC_LIST_FOREACH(item, qh_list) {
  36950. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36951. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  36952. + }
  36953. +}
  36954. +
  36955. +/**
  36956. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  36957. + * Device during SRP time by host power up.
  36958. + */
  36959. +void dwc_otg_hcd_power_up(void *ptr)
  36960. +{
  36961. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  36962. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  36963. +
  36964. + DWC_PRINTF("%s called\n", __FUNCTION__);
  36965. +
  36966. + if (!core_if->hibernation_suspend) {
  36967. + DWC_PRINTF("Already exited from Hibernation\n");
  36968. + return;
  36969. + }
  36970. +
  36971. + /* Switch on the voltage to the core */
  36972. + gpwrdn.b.pwrdnswtch = 1;
  36973. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36974. + dwc_udelay(10);
  36975. +
  36976. + /* Reset the core */
  36977. + gpwrdn.d32 = 0;
  36978. + gpwrdn.b.pwrdnrstn = 1;
  36979. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36980. + dwc_udelay(10);
  36981. +
  36982. + /* Disable power clamps */
  36983. + gpwrdn.d32 = 0;
  36984. + gpwrdn.b.pwrdnclmp = 1;
  36985. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36986. +
  36987. + /* Remove reset the core signal */
  36988. + gpwrdn.d32 = 0;
  36989. + gpwrdn.b.pwrdnrstn = 1;
  36990. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  36991. + dwc_udelay(10);
  36992. +
  36993. + /* Disable PMU interrupt */
  36994. + gpwrdn.d32 = 0;
  36995. + gpwrdn.b.pmuintsel = 1;
  36996. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36997. +
  36998. + core_if->hibernation_suspend = 0;
  36999. +
  37000. + /* Disable PMU */
  37001. + gpwrdn.d32 = 0;
  37002. + gpwrdn.b.pmuactv = 1;
  37003. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  37004. + dwc_udelay(10);
  37005. +
  37006. + /* Enable VBUS */
  37007. + gpwrdn.d32 = 0;
  37008. + gpwrdn.b.dis_vbus = 1;
  37009. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  37010. +
  37011. + core_if->op_state = A_HOST;
  37012. + dwc_otg_core_init(core_if);
  37013. + dwc_otg_enable_global_interrupts(core_if);
  37014. + cil_hcd_start(core_if);
  37015. +}
  37016. +
  37017. +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
  37018. +{
  37019. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  37020. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  37021. + int i;
  37022. +
  37023. + st->fsm = FIQ_PASSTHROUGH;
  37024. + st->hcchar_copy.d32 = 0;
  37025. + st->hcsplt_copy.d32 = 0;
  37026. + st->hcint_copy.d32 = 0;
  37027. + st->hcintmsk_copy.d32 = 0;
  37028. + st->hctsiz_copy.d32 = 0;
  37029. + st->hcdma_copy.d32 = 0;
  37030. + st->nr_errors = 0;
  37031. + st->hub_addr = 0;
  37032. + st->port_addr = 0;
  37033. + st->expected_uframe = 0;
  37034. + st->nrpackets = 0;
  37035. + st->dma_info.index = 0;
  37036. + for (i = 0; i < 6; i++)
  37037. + st->dma_info.slot_len[i] = 255;
  37038. + st->hs_isoc_info.index = 0;
  37039. + st->hs_isoc_info.iso_desc = NULL;
  37040. + st->hs_isoc_info.nrframes = 0;
  37041. +
  37042. + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
  37043. +}
  37044. +
  37045. +/**
  37046. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  37047. + * in the struct usb_hcd field.
  37048. + */
  37049. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  37050. +{
  37051. + struct device *dev = dwc_otg_hcd_to_dev(dwc_otg_hcd);
  37052. + int i;
  37053. +
  37054. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  37055. +
  37056. + del_timers(dwc_otg_hcd);
  37057. +
  37058. + /* Free memory for QH/QTD lists */
  37059. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  37060. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  37061. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  37062. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  37063. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  37064. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  37065. +
  37066. + /* Free memory for the host channels. */
  37067. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  37068. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  37069. +
  37070. +#ifdef DEBUG
  37071. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  37072. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  37073. + }
  37074. +#endif
  37075. + if (hc != NULL) {
  37076. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  37077. + i, hc);
  37078. + DWC_FREE(hc);
  37079. + }
  37080. + }
  37081. +
  37082. + if (dwc_otg_hcd->core_if->dma_enable) {
  37083. + if (dwc_otg_hcd->status_buf_dma) {
  37084. + DWC_DMA_FREE(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
  37085. + dwc_otg_hcd->status_buf,
  37086. + dwc_otg_hcd->status_buf_dma);
  37087. + }
  37088. + } else if (dwc_otg_hcd->status_buf != NULL) {
  37089. + DWC_FREE(dwc_otg_hcd->status_buf);
  37090. + }
  37091. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  37092. + /* Set core_if's lock pointer to NULL */
  37093. + dwc_otg_hcd->core_if->lock = NULL;
  37094. +
  37095. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  37096. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  37097. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  37098. + DWC_FREE(dwc_otg_hcd->fiq_state);
  37099. +
  37100. +#ifdef DWC_DEV_SRPCAP
  37101. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  37102. + dwc_otg_hcd->core_if->pwron_timer) {
  37103. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  37104. + }
  37105. +#endif
  37106. + DWC_FREE(dwc_otg_hcd);
  37107. +}
  37108. +
  37109. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  37110. +{
  37111. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  37112. + int retval = 0;
  37113. + int num_channels;
  37114. + int i;
  37115. + dwc_hc_t *channel;
  37116. +
  37117. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  37118. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
  37119. +#else
  37120. + hcd->lock = DWC_SPINLOCK_ALLOC();
  37121. +#endif
  37122. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  37123. + hcd, core_if);
  37124. + if (!hcd->lock) {
  37125. + DWC_ERROR("Could not allocate lock for pcd");
  37126. + DWC_FREE(hcd);
  37127. + retval = -DWC_E_NO_MEMORY;
  37128. + goto out;
  37129. + }
  37130. + hcd->core_if = core_if;
  37131. +
  37132. + /* Register the HCD CIL Callbacks */
  37133. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  37134. + &hcd_cil_callbacks, hcd);
  37135. +
  37136. + /* Initialize the non-periodic schedule. */
  37137. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  37138. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  37139. +
  37140. + /* Initialize the periodic schedule. */
  37141. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  37142. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  37143. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  37144. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  37145. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  37146. + /*
  37147. + * Create a host channel descriptor for each host channel implemented
  37148. + * in the controller. Initialize the channel descriptor array.
  37149. + */
  37150. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  37151. + num_channels = hcd->core_if->core_params->host_channels;
  37152. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  37153. + for (i = 0; i < num_channels; i++) {
  37154. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  37155. + if (channel == NULL) {
  37156. + retval = -DWC_E_NO_MEMORY;
  37157. + DWC_ERROR("%s: host channel allocation failed\n",
  37158. + __func__);
  37159. + dwc_otg_hcd_free(hcd);
  37160. + goto out;
  37161. + }
  37162. + channel->hc_num = i;
  37163. + hcd->hc_ptr_array[i] = channel;
  37164. +#ifdef DEBUG
  37165. + hcd->core_if->hc_xfer_timer[i] =
  37166. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  37167. + &hcd->core_if->hc_xfer_info[i]);
  37168. +#endif
  37169. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  37170. + channel);
  37171. + }
  37172. +
  37173. + if (fiq_enable) {
  37174. + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
  37175. + if (!hcd->fiq_state) {
  37176. + retval = -DWC_E_NO_MEMORY;
  37177. + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
  37178. + dwc_otg_hcd_free(hcd);
  37179. + goto out;
  37180. + }
  37181. + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
  37182. +
  37183. + for (i = 0; i < num_channels; i++) {
  37184. + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
  37185. + }
  37186. + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
  37187. +
  37188. + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
  37189. + if (!hcd->fiq_stack) {
  37190. + retval = -DWC_E_NO_MEMORY;
  37191. + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
  37192. + dwc_otg_hcd_free(hcd);
  37193. + goto out;
  37194. + }
  37195. + hcd->fiq_stack->magic1 = 0xDEADBEEF;
  37196. + hcd->fiq_stack->magic2 = 0xD00DFEED;
  37197. + hcd->fiq_state->gintmsk_saved.d32 = ~0;
  37198. + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  37199. +
  37200. + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
  37201. + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
  37202. + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
  37203. + * moderately readable array casts.
  37204. + */
  37205. + hcd->fiq_dmab = DWC_DMA_ALLOC(dev, (sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
  37206. + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
  37207. + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
  37208. + sizeof(struct fiq_dma_channel) * num_channels);
  37209. +
  37210. + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
  37211. +
  37212. + /* pointer for debug in fiq_print */
  37213. + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
  37214. + if (fiq_fsm_enable) {
  37215. + int i;
  37216. + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
  37217. + dwc_otg_cleanup_fiq_channel(hcd, i);
  37218. + }
  37219. + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
  37220. + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
  37221. + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
  37222. + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
  37223. + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
  37224. + }
  37225. + }
  37226. +
  37227. + /* Initialize the Connection timeout timer. */
  37228. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  37229. + dwc_otg_hcd_connect_timeout, 0);
  37230. +
  37231. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  37232. + if (microframe_schedule)
  37233. + init_hcd_usecs(hcd);
  37234. +
  37235. + /* Initialize reset tasklet. */
  37236. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  37237. +
  37238. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  37239. + completion_tasklet_func, hcd);
  37240. +#ifdef DWC_DEV_SRPCAP
  37241. + if (hcd->core_if->power_down == 2) {
  37242. + /* Initialize Power on timer for Host power up in case hibernation */
  37243. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  37244. + dwc_otg_hcd_power_up, core_if);
  37245. + }
  37246. +#endif
  37247. +
  37248. + /*
  37249. + * Allocate space for storing data on status transactions. Normally no
  37250. + * data is sent, but this space acts as a bit bucket. This must be
  37251. + * done after usb_add_hcd since that function allocates the DMA buffer
  37252. + * pool.
  37253. + */
  37254. + if (hcd->core_if->dma_enable) {
  37255. + hcd->status_buf =
  37256. + DWC_DMA_ALLOC(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
  37257. + &hcd->status_buf_dma);
  37258. + } else {
  37259. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  37260. + }
  37261. + if (!hcd->status_buf) {
  37262. + retval = -DWC_E_NO_MEMORY;
  37263. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  37264. + dwc_otg_hcd_free(hcd);
  37265. + goto out;
  37266. + }
  37267. +
  37268. + hcd->otg_port = 1;
  37269. + hcd->frame_list = NULL;
  37270. + hcd->frame_list_dma = 0;
  37271. + hcd->periodic_qh_count = 0;
  37272. +
  37273. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  37274. +#ifdef FIQ_DEBUG
  37275. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  37276. +#endif
  37277. +
  37278. +out:
  37279. + return retval;
  37280. +}
  37281. +
  37282. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  37283. +{
  37284. + /* Turn off all host-specific interrupts. */
  37285. + dwc_otg_disable_host_interrupts(hcd->core_if);
  37286. +
  37287. + dwc_otg_hcd_free(hcd);
  37288. +}
  37289. +
  37290. +/**
  37291. + * Initializes dynamic portions of the DWC_otg HCD state.
  37292. + */
  37293. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  37294. +{
  37295. + int num_channels;
  37296. + int i;
  37297. + dwc_hc_t *channel;
  37298. + dwc_hc_t *channel_tmp;
  37299. +
  37300. + hcd->flags.d32 = 0;
  37301. +
  37302. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  37303. + if (!microframe_schedule) {
  37304. + hcd->non_periodic_channels = 0;
  37305. + hcd->periodic_channels = 0;
  37306. + } else {
  37307. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  37308. + }
  37309. + /*
  37310. + * Put all channels in the free channel list and clean up channel
  37311. + * states.
  37312. + */
  37313. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  37314. + &hcd->free_hc_list, hc_list_entry) {
  37315. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  37316. + }
  37317. +
  37318. + num_channels = hcd->core_if->core_params->host_channels;
  37319. + for (i = 0; i < num_channels; i++) {
  37320. + channel = hcd->hc_ptr_array[i];
  37321. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  37322. + hc_list_entry);
  37323. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  37324. + }
  37325. +
  37326. + /* Initialize the DWC core for host mode operation. */
  37327. + dwc_otg_core_host_init(hcd->core_if);
  37328. +
  37329. + /* Set core_if's lock pointer to the hcd->lock */
  37330. + hcd->core_if->lock = hcd->lock;
  37331. +}
  37332. +
  37333. +/**
  37334. + * Assigns transactions from a QTD to a free host channel and initializes the
  37335. + * host channel to perform the transactions. The host channel is removed from
  37336. + * the free list.
  37337. + *
  37338. + * @param hcd The HCD state structure.
  37339. + * @param qh Transactions from the first QTD for this QH are selected and
  37340. + * assigned to a free host channel.
  37341. + */
  37342. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  37343. +{
  37344. + dwc_hc_t *hc;
  37345. + dwc_otg_qtd_t *qtd;
  37346. + dwc_otg_hcd_urb_t *urb;
  37347. + void* ptr = NULL;
  37348. + uint32_t intr_enable;
  37349. + unsigned long flags;
  37350. + gintmsk_data_t gintmsk = { .d32 = 0, };
  37351. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  37352. +
  37353. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37354. +
  37355. + urb = qtd->urb;
  37356. +
  37357. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  37358. +
  37359. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  37360. + urb->actual_length = urb->length;
  37361. +
  37362. +
  37363. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  37364. +
  37365. + /* Remove the host channel from the free list. */
  37366. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  37367. +
  37368. + qh->channel = hc;
  37369. +
  37370. + qtd->in_process = 1;
  37371. +
  37372. + /*
  37373. + * Use usb_pipedevice to determine device address. This address is
  37374. + * 0 before the SET_ADDRESS command and the correct address afterward.
  37375. + */
  37376. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  37377. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  37378. + hc->speed = qh->dev_speed;
  37379. + hc->max_packet = dwc_max_packet(qh->maxp);
  37380. +
  37381. + hc->xfer_started = 0;
  37382. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  37383. + hc->error_state = (qtd->error_count > 0);
  37384. + hc->halt_on_queue = 0;
  37385. + hc->halt_pending = 0;
  37386. + hc->requests = 0;
  37387. +
  37388. + /*
  37389. + * The following values may be modified in the transfer type section
  37390. + * below. The xfer_len value may be reduced when the transfer is
  37391. + * started to accommodate the max widths of the XferSize and PktCnt
  37392. + * fields in the HCTSIZn register.
  37393. + */
  37394. +
  37395. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  37396. + if (hc->ep_is_in) {
  37397. + hc->do_ping = 0;
  37398. + } else {
  37399. + hc->do_ping = qh->ping_state;
  37400. + }
  37401. +
  37402. + hc->data_pid_start = qh->data_toggle;
  37403. + hc->multi_count = 1;
  37404. +
  37405. + if (hcd->core_if->dma_enable) {
  37406. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  37407. +
  37408. + /* For non-dword aligned case */
  37409. + if (((unsigned long)hc->xfer_buff & 0x3)
  37410. + && !hcd->core_if->dma_desc_enable) {
  37411. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  37412. + }
  37413. + } else {
  37414. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  37415. + }
  37416. + hc->xfer_len = urb->length - urb->actual_length;
  37417. + hc->xfer_count = 0;
  37418. +
  37419. + /*
  37420. + * Set the split attributes
  37421. + */
  37422. + hc->do_split = 0;
  37423. + if (qh->do_split) {
  37424. + uint32_t hub_addr, port_addr;
  37425. + hc->do_split = 1;
  37426. + hc->start_pkt_count = 1;
  37427. + hc->xact_pos = qtd->isoc_split_pos;
  37428. + /* We don't need to do complete splits anymore */
  37429. +// if(fiq_fsm_enable)
  37430. + if (0)
  37431. + hc->complete_split = qtd->complete_split = 0;
  37432. + else
  37433. + hc->complete_split = qtd->complete_split;
  37434. +
  37435. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  37436. + hc->hub_addr = (uint8_t) hub_addr;
  37437. + hc->port_addr = (uint8_t) port_addr;
  37438. + }
  37439. +
  37440. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  37441. + case UE_CONTROL:
  37442. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  37443. + switch (qtd->control_phase) {
  37444. + case DWC_OTG_CONTROL_SETUP:
  37445. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  37446. + hc->do_ping = 0;
  37447. + hc->ep_is_in = 0;
  37448. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  37449. + if (hcd->core_if->dma_enable) {
  37450. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  37451. + } else {
  37452. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  37453. + }
  37454. + hc->xfer_len = 8;
  37455. + ptr = NULL;
  37456. + break;
  37457. + case DWC_OTG_CONTROL_DATA:
  37458. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  37459. + hc->data_pid_start = qtd->data_toggle;
  37460. + break;
  37461. + case DWC_OTG_CONTROL_STATUS:
  37462. + /*
  37463. + * Direction is opposite of data direction or IN if no
  37464. + * data.
  37465. + */
  37466. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  37467. + if (urb->length == 0) {
  37468. + hc->ep_is_in = 1;
  37469. + } else {
  37470. + hc->ep_is_in =
  37471. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  37472. + }
  37473. + if (hc->ep_is_in) {
  37474. + hc->do_ping = 0;
  37475. + }
  37476. +
  37477. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  37478. +
  37479. + hc->xfer_len = 0;
  37480. + if (hcd->core_if->dma_enable) {
  37481. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  37482. + } else {
  37483. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  37484. + }
  37485. + ptr = NULL;
  37486. + break;
  37487. + }
  37488. + break;
  37489. + case UE_BULK:
  37490. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  37491. + break;
  37492. + case UE_INTERRUPT:
  37493. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  37494. + break;
  37495. + case UE_ISOCHRONOUS:
  37496. + {
  37497. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  37498. +
  37499. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  37500. +
  37501. + if (hcd->core_if->dma_desc_enable)
  37502. + break;
  37503. +
  37504. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  37505. +
  37506. + frame_desc->status = 0;
  37507. +
  37508. + if (hcd->core_if->dma_enable) {
  37509. + hc->xfer_buff = (uint8_t *) urb->dma;
  37510. + } else {
  37511. + hc->xfer_buff = (uint8_t *) urb->buf;
  37512. + }
  37513. + hc->xfer_buff +=
  37514. + frame_desc->offset + qtd->isoc_split_offset;
  37515. + hc->xfer_len =
  37516. + frame_desc->length - qtd->isoc_split_offset;
  37517. +
  37518. + /* For non-dword aligned buffers */
  37519. + if (((unsigned long)hc->xfer_buff & 0x3)
  37520. + && hcd->core_if->dma_enable) {
  37521. + ptr =
  37522. + (uint8_t *) urb->buf + frame_desc->offset +
  37523. + qtd->isoc_split_offset;
  37524. + } else
  37525. + ptr = NULL;
  37526. +
  37527. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  37528. + if (hc->xfer_len <= 188) {
  37529. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  37530. + } else {
  37531. + hc->xact_pos =
  37532. + DWC_HCSPLIT_XACTPOS_BEGIN;
  37533. + }
  37534. + }
  37535. + }
  37536. + break;
  37537. + }
  37538. + /* non DWORD-aligned buffer case */
  37539. + if (ptr) {
  37540. + uint32_t buf_size;
  37541. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  37542. + buf_size = hcd->core_if->core_params->max_transfer_size;
  37543. + } else {
  37544. + buf_size = 4096;
  37545. + }
  37546. + if (!qh->dw_align_buf) {
  37547. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(dev, buf_size,
  37548. + &qh->dw_align_buf_dma);
  37549. + if (!qh->dw_align_buf) {
  37550. + DWC_ERROR
  37551. + ("%s: Failed to allocate memory to handle "
  37552. + "non-dword aligned buffer case\n",
  37553. + __func__);
  37554. + return;
  37555. + }
  37556. + }
  37557. + if (!hc->ep_is_in) {
  37558. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  37559. + }
  37560. + hc->align_buff = qh->dw_align_buf_dma;
  37561. + } else {
  37562. + hc->align_buff = 0;
  37563. + }
  37564. +
  37565. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  37566. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  37567. + /*
  37568. + * This value may be modified when the transfer is started to
  37569. + * reflect the actual transfer length.
  37570. + */
  37571. + hc->multi_count = dwc_hb_mult(qh->maxp);
  37572. + }
  37573. +
  37574. + if (hcd->core_if->dma_desc_enable)
  37575. + hc->desc_list_addr = qh->desc_list_dma;
  37576. +
  37577. + dwc_otg_hc_init(hcd->core_if, hc);
  37578. +
  37579. + local_irq_save(flags);
  37580. +
  37581. + if (fiq_enable) {
  37582. + local_fiq_disable();
  37583. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37584. + }
  37585. +
  37586. + /* Enable the top level host channel interrupt. */
  37587. + intr_enable = (1 << hc->hc_num);
  37588. + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
  37589. +
  37590. + /* Make sure host channel interrupts are enabled. */
  37591. + gintmsk.b.hcintr = 1;
  37592. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  37593. +
  37594. + if (fiq_enable) {
  37595. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37596. + local_fiq_enable();
  37597. + }
  37598. +
  37599. + local_irq_restore(flags);
  37600. + hc->qh = qh;
  37601. +}
  37602. +
  37603. +
  37604. +/**
  37605. + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
  37606. + * @hcd: Pointer to the dwc_otg_hcd struct
  37607. + * @qh: pointer to the endpoint's queue head
  37608. + *
  37609. + * Transaction start/end control flow is grafted onto the existing dwc_otg
  37610. + * mechanisms, to avoid spaghettifying the functions more than they already are.
  37611. + * This function's eligibility check is altered by debug parameter.
  37612. + *
  37613. + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
  37614. + */
  37615. +
  37616. +int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37617. +{
  37618. + if (qh->do_split) {
  37619. + switch (qh->ep_type) {
  37620. + case UE_CONTROL:
  37621. + case UE_BULK:
  37622. + if (fiq_fsm_mask & (1 << 0))
  37623. + return 1;
  37624. + break;
  37625. + case UE_INTERRUPT:
  37626. + case UE_ISOCHRONOUS:
  37627. + if (fiq_fsm_mask & (1 << 1))
  37628. + return 1;
  37629. + break;
  37630. + default:
  37631. + break;
  37632. + }
  37633. + } else if (qh->ep_type == UE_ISOCHRONOUS) {
  37634. + if (fiq_fsm_mask & (1 << 2)) {
  37635. + /* ISOCH support. We test for compatibility:
  37636. + * - DWORD aligned buffers
  37637. + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
  37638. + * If yes, then the fsm enqueue function will handle the state machine setup.
  37639. + */
  37640. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37641. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  37642. + dwc_dma_t ptr;
  37643. + int i;
  37644. +
  37645. + if (urb->packet_count < 2)
  37646. + return 0;
  37647. + for (i = 0; i < urb->packet_count; i++) {
  37648. + ptr = urb->dma + urb->iso_descs[i].offset;
  37649. + if (ptr & 0x3)
  37650. + return 0;
  37651. + }
  37652. + return 1;
  37653. + }
  37654. + }
  37655. + return 0;
  37656. +}
  37657. +
  37658. +/**
  37659. + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
  37660. + * @hcd: Pointer to the dwc_otg_hcd struct
  37661. + * @qh: Pointer to the endpoint's queue head
  37662. + *
  37663. + * Periodic split transactions are transmitted modulo 188 bytes.
  37664. + * This necessitates slicing data up into buckets for isochronous out
  37665. + * and fixing up the DMA address for all IN transfers.
  37666. + *
  37667. + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
  37668. + * HC buffer has been used.
  37669. + */
  37670. +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
  37671. + {
  37672. + int frame_length, i = 0;
  37673. + uint8_t *ptr = NULL;
  37674. + dwc_hc_t *hc = qh->channel;
  37675. + struct fiq_dma_blob *blob;
  37676. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  37677. +
  37678. + for (i = 0; i < 6; i++) {
  37679. + st->dma_info.slot_len[i] = 255;
  37680. + }
  37681. + st->dma_info.index = 0;
  37682. + i = 0;
  37683. + if (hc->ep_is_in) {
  37684. + /*
  37685. + * Set dma_regs to bounce buffer. FIQ will update the
  37686. + * state depending on transaction progress.
  37687. + */
  37688. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  37689. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  37690. + /* Calculate the max number of CSPLITS such that the FIQ can time out
  37691. + * a transaction if it fails.
  37692. + */
  37693. + frame_length = st->hcchar_copy.b.mps;
  37694. + do {
  37695. + i++;
  37696. + frame_length -= 188;
  37697. + } while (frame_length >= 0);
  37698. + st->nrpackets = i;
  37699. + return 1;
  37700. + } else {
  37701. + if (qh->ep_type == UE_ISOCHRONOUS) {
  37702. +
  37703. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37704. +
  37705. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  37706. + frame_length = frame_desc->length;
  37707. +
  37708. + /* Virtual address for bounce buffers */
  37709. + blob = hcd->fiq_dmab;
  37710. +
  37711. + ptr = qtd->urb->buf + frame_desc->offset;
  37712. + if (frame_length == 0) {
  37713. + /*
  37714. + * for isochronous transactions, we must still transmit a packet
  37715. + * even if the length is zero.
  37716. + */
  37717. + st->dma_info.slot_len[0] = 0;
  37718. + st->nrpackets = 1;
  37719. + } else {
  37720. + do {
  37721. + if (frame_length <= 188) {
  37722. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
  37723. + st->dma_info.slot_len[i] = frame_length;
  37724. + ptr += frame_length;
  37725. + } else {
  37726. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
  37727. + st->dma_info.slot_len[i] = 188;
  37728. + ptr += 188;
  37729. + }
  37730. + i++;
  37731. + frame_length -= 188;
  37732. + } while (frame_length > 0);
  37733. + st->nrpackets = i;
  37734. + }
  37735. + ptr = qtd->urb->buf + frame_desc->offset;
  37736. + /* Point the HC at the DMA address of the bounce buffers */
  37737. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  37738. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  37739. +
  37740. + /* fixup xfersize to the actual packet size */
  37741. + st->hctsiz_copy.b.pid = 0;
  37742. + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
  37743. + return 1;
  37744. + } else {
  37745. + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
  37746. + return 0;
  37747. + }
  37748. + }
  37749. +}
  37750. +
  37751. +/**
  37752. + * fiq_fsm_np_tt_contended() - Avoid performing contended non-periodic transfers
  37753. + * @hcd: Pointer to the dwc_otg_hcd struct
  37754. + * @qh: Pointer to the endpoint's queue head
  37755. + *
  37756. + * Certain hub chips don't differentiate between IN and OUT non-periodic pipes
  37757. + * with the same endpoint number. If transfers get completed out of order
  37758. + * (disregarding the direction token) then the hub can lock up
  37759. + * or return erroneous responses.
  37760. + *
  37761. + * Returns 1 if initiating the transfer would cause contention, 0 otherwise.
  37762. + */
  37763. +int fiq_fsm_np_tt_contended(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37764. +{
  37765. + int i;
  37766. + struct fiq_channel_state *st;
  37767. + int dev_addr = qh->channel->dev_addr;
  37768. + int ep_num = qh->channel->ep_num;
  37769. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  37770. + if (i == qh->channel->hc_num)
  37771. + continue;
  37772. + st = &hcd->fiq_state->channel[i];
  37773. + switch (st->fsm) {
  37774. + case FIQ_NP_SSPLIT_STARTED:
  37775. + case FIQ_NP_SSPLIT_RETRY:
  37776. + case FIQ_NP_SSPLIT_PENDING:
  37777. + case FIQ_NP_OUT_CSPLIT_RETRY:
  37778. + case FIQ_NP_IN_CSPLIT_RETRY:
  37779. + if (st->hcchar_copy.b.devaddr == dev_addr &&
  37780. + st->hcchar_copy.b.epnum == ep_num)
  37781. + return 1;
  37782. + break;
  37783. + default:
  37784. + break;
  37785. + }
  37786. + }
  37787. + return 0;
  37788. +}
  37789. +
  37790. +/*
  37791. + * Pushing a periodic request into the queue near the EOF1 point
  37792. + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
  37793. + * Usually, the request goes out on the bus causing a transfer but
  37794. + * the core does not transfer the data to memory.
  37795. + * This guard interval (in number of 60MHz clocks) is required which
  37796. + * must cater for CPU latency between reading the value and enabling
  37797. + * the channel.
  37798. + */
  37799. +#define PERIODIC_FRREM_BACKOFF 1000
  37800. +
  37801. +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37802. +{
  37803. + dwc_hc_t *hc = qh->channel;
  37804. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  37805. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37806. + int frame;
  37807. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  37808. + int xfer_len, nrpackets;
  37809. + hcdma_data_t hcdma;
  37810. + hfnum_data_t hfnum;
  37811. +
  37812. + if (st->fsm != FIQ_PASSTHROUGH)
  37813. + return 0;
  37814. +
  37815. + st->nr_errors = 0;
  37816. +
  37817. + st->hcchar_copy.d32 = 0;
  37818. + st->hcchar_copy.b.mps = hc->max_packet;
  37819. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  37820. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  37821. + st->hcchar_copy.b.epnum = hc->ep_num;
  37822. + st->hcchar_copy.b.eptype = hc->ep_type;
  37823. +
  37824. + st->hcintmsk_copy.b.chhltd = 1;
  37825. +
  37826. + frame = dwc_otg_hcd_get_frame_number(hcd);
  37827. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  37828. +
  37829. + st->hcchar_copy.b.lspddev = 0;
  37830. + /* Enable the channel later as a final register write. */
  37831. +
  37832. + st->hcsplt_copy.d32 = 0;
  37833. +
  37834. + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
  37835. + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
  37836. + /* grab the next DMA address offset from the array */
  37837. + st->hcdma_copy.d32 = qtd->urb->dma;
  37838. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
  37839. +
  37840. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  37841. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  37842. + * this is always set to the maximum size of the endpoint. */
  37843. + xfer_len = st->hs_isoc_info.iso_desc[0].length;
  37844. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  37845. + if (nrpackets == 0)
  37846. + nrpackets = 1;
  37847. + st->hcchar_copy.b.multicnt = nrpackets;
  37848. + st->hctsiz_copy.b.pktcnt = nrpackets;
  37849. +
  37850. + /* Initial PID also needs to be set */
  37851. + if (st->hcchar_copy.b.epdir == 0) {
  37852. + st->hctsiz_copy.b.xfersize = xfer_len;
  37853. + switch (st->hcchar_copy.b.multicnt) {
  37854. + case 1:
  37855. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  37856. + break;
  37857. + case 2:
  37858. + case 3:
  37859. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  37860. + break;
  37861. + }
  37862. +
  37863. + } else {
  37864. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  37865. + switch (st->hcchar_copy.b.multicnt) {
  37866. + case 1:
  37867. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  37868. + break;
  37869. + case 2:
  37870. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  37871. + break;
  37872. + case 3:
  37873. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  37874. + break;
  37875. + }
  37876. + }
  37877. +
  37878. + st->hs_isoc_info.stride = qh->interval;
  37879. + st->uframe_sleeps = 0;
  37880. +
  37881. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
  37882. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
  37883. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  37884. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  37885. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  37886. + local_fiq_disable();
  37887. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37888. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  37889. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  37890. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  37891. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37892. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  37893. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  37894. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  37895. + * split transaction is queued very close to EOF. SOF interrupt handler
  37896. + * will wake this channel at the next interrupt.
  37897. + */
  37898. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  37899. + st->uframe_sleeps = 1;
  37900. + } else {
  37901. + st->fsm = FIQ_HS_ISOC_TURBO;
  37902. + st->hcchar_copy.b.chen = 1;
  37903. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37904. + }
  37905. + mb();
  37906. + st->hcchar_copy.b.chen = 0;
  37907. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37908. + local_fiq_enable();
  37909. + return 0;
  37910. +}
  37911. +
  37912. +
  37913. +/**
  37914. + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
  37915. + * @hcd: Pointer to the dwc_otg_hcd struct
  37916. + * @qh: Pointer to the endpoint's queue head
  37917. + *
  37918. + * This overrides the dwc_otg driver's normal method of queueing a transaction.
  37919. + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
  37920. + * for the nominated host channel.
  37921. + *
  37922. + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
  37923. + * start is possible. If not, then the FIQ is left to start the transfer.
  37924. + */
  37925. +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37926. +{
  37927. + int start_immediate = 1, i;
  37928. + hfnum_data_t hfnum;
  37929. + dwc_hc_t *hc = qh->channel;
  37930. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  37931. + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
  37932. + int hub_addr, port_addr, frame, uframe;
  37933. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  37934. +
  37935. + /*
  37936. + * Non-periodic channel assignments stay in the non_periodic_active queue.
  37937. + * Therefore we get repeatedly called until the FIQ's done processing this channel.
  37938. + */
  37939. + if (qh->channel->xfer_started == 1)
  37940. + return 0;
  37941. +
  37942. + if (st->fsm != FIQ_PASSTHROUGH) {
  37943. + pr_warn_ratelimited("%s:%d: Queue called for an active channel\n", __func__, __LINE__);
  37944. + return 0;
  37945. + }
  37946. +
  37947. + qh->channel->xfer_started = 1;
  37948. +
  37949. + st->nr_errors = 0;
  37950. +
  37951. + st->hcchar_copy.d32 = 0;
  37952. + st->hcchar_copy.b.mps = hc->max_packet;
  37953. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  37954. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  37955. + st->hcchar_copy.b.epnum = hc->ep_num;
  37956. + st->hcchar_copy.b.eptype = hc->ep_type;
  37957. + if (hc->ep_type & 0x1) {
  37958. + if (hc->ep_is_in)
  37959. + st->hcchar_copy.b.multicnt = 3;
  37960. + else
  37961. + /* Docs say set this to 1, but driver sets to 0! */
  37962. + st->hcchar_copy.b.multicnt = 0;
  37963. + } else {
  37964. + st->hcchar_copy.b.multicnt = 1;
  37965. + st->hcchar_copy.b.oddfrm = 0;
  37966. + }
  37967. + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
  37968. + /* Enable the channel later as a final register write. */
  37969. +
  37970. + st->hcsplt_copy.d32 = 0;
  37971. + if(qh->do_split) {
  37972. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  37973. + st->hcsplt_copy.b.compsplt = 0;
  37974. + st->hcsplt_copy.b.spltena = 1;
  37975. + // XACTPOS is for isoc-out only but needs initialising anyway.
  37976. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
  37977. + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
  37978. + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
  37979. + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
  37980. + * will update as necessary.
  37981. + */
  37982. + if (hc->xfer_len > 188) {
  37983. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
  37984. + }
  37985. + }
  37986. + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
  37987. + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
  37988. + st->hub_addr = hub_addr;
  37989. + st->port_addr = port_addr;
  37990. + }
  37991. +
  37992. + st->hctsiz_copy.d32 = 0;
  37993. + st->hctsiz_copy.b.dopng = 0;
  37994. + st->hctsiz_copy.b.pid = hc->data_pid_start;
  37995. +
  37996. + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  37997. + hc->xfer_len = hc->max_packet;
  37998. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  37999. + hc->xfer_len = 188;
  38000. + }
  38001. + st->hctsiz_copy.b.xfersize = hc->xfer_len;
  38002. +
  38003. + st->hctsiz_copy.b.pktcnt = 1;
  38004. +
  38005. + if (hc->ep_type & 0x1) {
  38006. + /*
  38007. + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
  38008. + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
  38009. + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
  38010. + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
  38011. + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
  38012. + * must not touch internal driver state.
  38013. + */
  38014. + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
  38015. + if (hc->align_buff) {
  38016. + st->hcdma_copy.d32 = hc->align_buff;
  38017. + } else {
  38018. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  38019. + }
  38020. + }
  38021. + } else {
  38022. + if (hc->align_buff) {
  38023. + st->hcdma_copy.d32 = hc->align_buff;
  38024. + } else {
  38025. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  38026. + }
  38027. + }
  38028. + /* The FIQ depends upon no other interrupts being enabled except channel halt.
  38029. + * Fixup channel interrupt mask. */
  38030. + st->hcintmsk_copy.d32 = 0;
  38031. + st->hcintmsk_copy.b.chhltd = 1;
  38032. + st->hcintmsk_copy.b.ahberr = 1;
  38033. +
  38034. + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
  38035. + * as Control puts the transfer into the non-periodic request queue and the
  38036. + * non-periodic handler in the hub. Makes things lots easier.
  38037. + */
  38038. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
  38039. + st->hcchar_copy.b.multicnt = 0;
  38040. + st->hcchar_copy.b.oddfrm = 0;
  38041. + st->hcchar_copy.b.eptype = UE_CONTROL;
  38042. + if (hc->align_buff) {
  38043. + st->hcdma_copy.d32 = hc->align_buff;
  38044. + } else {
  38045. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  38046. + }
  38047. + }
  38048. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  38049. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  38050. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  38051. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  38052. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  38053. +
  38054. + local_fiq_disable();
  38055. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  38056. +
  38057. + if (hc->ep_type & 0x1) {
  38058. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  38059. + frame = (hfnum.b.frnum & ~0x7) >> 3;
  38060. + uframe = hfnum.b.frnum & 0x7;
  38061. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  38062. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  38063. + * split transaction is queued very close to EOF.
  38064. + */
  38065. + start_immediate = 0;
  38066. + } else if (uframe == 5) {
  38067. + start_immediate = 0;
  38068. + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
  38069. + start_immediate = 0;
  38070. + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
  38071. + start_immediate = 0;
  38072. + } else {
  38073. + /* Search through all host channels to determine if a transaction
  38074. + * is currently in progress */
  38075. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  38076. + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
  38077. + continue;
  38078. + switch (hcd->fiq_state->channel[i].fsm) {
  38079. + /* TT is reserved for channels that are in the middle of a periodic
  38080. + * split transaction.
  38081. + */
  38082. + case FIQ_PER_SSPLIT_STARTED:
  38083. + case FIQ_PER_CSPLIT_WAIT:
  38084. + case FIQ_PER_CSPLIT_NYET1:
  38085. + case FIQ_PER_CSPLIT_POLL:
  38086. + case FIQ_PER_ISO_OUT_ACTIVE:
  38087. + case FIQ_PER_ISO_OUT_LAST:
  38088. + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
  38089. + hcd->fiq_state->channel[i].port_addr == port_addr) {
  38090. + start_immediate = 0;
  38091. + }
  38092. + break;
  38093. + default:
  38094. + break;
  38095. + }
  38096. + if (!start_immediate)
  38097. + break;
  38098. + }
  38099. + }
  38100. + }
  38101. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
  38102. + start_immediate = 1;
  38103. +
  38104. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
  38105. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
  38106. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
  38107. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  38108. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  38109. + switch (hc->ep_type) {
  38110. + case UE_CONTROL:
  38111. + case UE_BULK:
  38112. + if (fiq_fsm_np_tt_contended(hcd, qh)) {
  38113. + st->fsm = FIQ_NP_SSPLIT_PENDING;
  38114. + start_immediate = 0;
  38115. + } else {
  38116. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  38117. + }
  38118. + break;
  38119. + case UE_ISOCHRONOUS:
  38120. + if (hc->ep_is_in) {
  38121. + if (start_immediate) {
  38122. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  38123. + } else {
  38124. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  38125. + }
  38126. + } else {
  38127. + if (start_immediate) {
  38128. + /* Single-isoc OUT packets don't require FIQ involvement */
  38129. + if (st->nrpackets == 1) {
  38130. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  38131. + } else {
  38132. + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
  38133. + }
  38134. + } else {
  38135. + st->fsm = FIQ_PER_ISO_OUT_PENDING;
  38136. + }
  38137. + }
  38138. + break;
  38139. + case UE_INTERRUPT:
  38140. + if (fiq_fsm_mask & 0x8) {
  38141. + if (fiq_fsm_np_tt_contended(hcd, qh)) {
  38142. + st->fsm = FIQ_NP_SSPLIT_PENDING;
  38143. + start_immediate = 0;
  38144. + } else {
  38145. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  38146. + }
  38147. + } else if (start_immediate) {
  38148. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  38149. + } else {
  38150. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  38151. + }
  38152. + default:
  38153. + break;
  38154. + }
  38155. + if (start_immediate) {
  38156. + /* Set the oddfrm bit as close as possible to actual queueing */
  38157. + frame = dwc_otg_hcd_get_frame_number(hcd);
  38158. + st->expected_uframe = (frame + 1) & 0x3FFF;
  38159. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  38160. + st->hcchar_copy.b.chen = 1;
  38161. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  38162. + }
  38163. + mb();
  38164. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  38165. + local_fiq_enable();
  38166. + return 0;
  38167. +}
  38168. +
  38169. +
  38170. +/**
  38171. + * This function selects transactions from the HCD transfer schedule and
  38172. + * assigns them to available host channels. It is called from HCD interrupt
  38173. + * handler functions.
  38174. + *
  38175. + * @param hcd The HCD state structure.
  38176. + *
  38177. + * @return The types of new transactions that were assigned to host channels.
  38178. + */
  38179. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  38180. +{
  38181. + dwc_list_link_t *qh_ptr;
  38182. + dwc_otg_qh_t *qh;
  38183. + int num_channels;
  38184. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  38185. +
  38186. +#ifdef DEBUG_HOST_CHANNELS
  38187. + last_sel_trans_num_per_scheduled = 0;
  38188. + last_sel_trans_num_nonper_scheduled = 0;
  38189. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  38190. +#endif /* DEBUG_HOST_CHANNELS */
  38191. +
  38192. + /* Process entries in the periodic ready list. */
  38193. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  38194. +
  38195. + while (qh_ptr != &hcd->periodic_sched_ready &&
  38196. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  38197. +
  38198. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38199. +
  38200. + if (microframe_schedule) {
  38201. + // Make sure we leave one channel for non periodic transactions.
  38202. + if (hcd->available_host_channels <= 1) {
  38203. + break;
  38204. + }
  38205. + hcd->available_host_channels--;
  38206. +#ifdef DEBUG_HOST_CHANNELS
  38207. + last_sel_trans_num_per_scheduled++;
  38208. +#endif /* DEBUG_HOST_CHANNELS */
  38209. + }
  38210. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38211. + assign_and_init_hc(hcd, qh);
  38212. +
  38213. + /*
  38214. + * Move the QH from the periodic ready schedule to the
  38215. + * periodic assigned schedule.
  38216. + */
  38217. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38218. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  38219. + &qh->qh_list_entry);
  38220. + }
  38221. +
  38222. + /*
  38223. + * Process entries in the inactive portion of the non-periodic
  38224. + * schedule. Some free host channels may not be used if they are
  38225. + * reserved for periodic transfers.
  38226. + */
  38227. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  38228. + num_channels = hcd->core_if->core_params->host_channels;
  38229. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  38230. + (microframe_schedule || hcd->non_periodic_channels <
  38231. + num_channels - hcd->periodic_channels) &&
  38232. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  38233. +
  38234. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38235. + /*
  38236. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  38237. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  38238. + * cheeky devices that just hold off using NAKs
  38239. + */
  38240. + if (fiq_enable && nak_holdoff && qh->do_split) {
  38241. + if (qh->nak_frame != 0xffff) {
  38242. + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
  38243. + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
  38244. + if (dwc_frame_num_le(frame, next_frame)) {
  38245. + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
  38246. + hcd->fiq_state->next_sched_frame = next_frame;
  38247. + }
  38248. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38249. + continue;
  38250. + } else {
  38251. + qh->nak_frame = 0xFFFF;
  38252. + }
  38253. + }
  38254. + }
  38255. +
  38256. + if (microframe_schedule) {
  38257. + if (hcd->available_host_channels < 1) {
  38258. + break;
  38259. + }
  38260. + hcd->available_host_channels--;
  38261. +#ifdef DEBUG_HOST_CHANNELS
  38262. + last_sel_trans_num_nonper_scheduled++;
  38263. +#endif /* DEBUG_HOST_CHANNELS */
  38264. + }
  38265. +
  38266. + assign_and_init_hc(hcd, qh);
  38267. +
  38268. + /*
  38269. + * Move the QH from the non-periodic inactive schedule to the
  38270. + * non-periodic active schedule.
  38271. + */
  38272. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38273. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  38274. + &qh->qh_list_entry);
  38275. +
  38276. + if (!microframe_schedule)
  38277. + hcd->non_periodic_channels++;
  38278. + }
  38279. + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
  38280. + * stop the FIQ from kicking us. We could potentially still have elements here if we
  38281. + * ran out of host channels.
  38282. + */
  38283. + if (fiq_enable) {
  38284. + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
  38285. + hcd->fiq_state->kick_np_queues = 0;
  38286. + } else {
  38287. + /* For each entry remaining in the NP inactive queue,
  38288. + * if this a NAK'd retransmit then don't set the kick flag.
  38289. + */
  38290. + if(nak_holdoff) {
  38291. + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
  38292. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38293. + if (qh->nak_frame == 0xFFFF) {
  38294. + hcd->fiq_state->kick_np_queues = 1;
  38295. + }
  38296. + }
  38297. + }
  38298. + }
  38299. + }
  38300. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  38301. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  38302. +
  38303. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  38304. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  38305. +
  38306. +
  38307. +#ifdef DEBUG_HOST_CHANNELS
  38308. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  38309. +#endif /* DEBUG_HOST_CHANNELS */
  38310. + return ret_val;
  38311. +}
  38312. +
  38313. +/**
  38314. + * Attempts to queue a single transaction request for a host channel
  38315. + * associated with either a periodic or non-periodic transfer. This function
  38316. + * assumes that there is space available in the appropriate request queue. For
  38317. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  38318. + * is available in the appropriate Tx FIFO.
  38319. + *
  38320. + * @param hcd The HCD state structure.
  38321. + * @param hc Host channel descriptor associated with either a periodic or
  38322. + * non-periodic transfer.
  38323. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  38324. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  38325. + * transfers.
  38326. + *
  38327. + * @return 1 if a request is queued and more requests may be needed to
  38328. + * complete the transfer, 0 if no more requests are required for this
  38329. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  38330. + */
  38331. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  38332. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  38333. +{
  38334. + int retval;
  38335. +
  38336. + if (hcd->core_if->dma_enable) {
  38337. + if (hcd->core_if->dma_desc_enable) {
  38338. + if (!hc->xfer_started
  38339. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  38340. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  38341. + hc->qh->ping_state = 0;
  38342. + }
  38343. + } else if (!hc->xfer_started) {
  38344. + if (fiq_fsm_enable && hc->error_state) {
  38345. + hcd->fiq_state->channel[hc->hc_num].nr_errors =
  38346. + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
  38347. + hcd->fiq_state->channel[hc->hc_num].fsm =
  38348. + FIQ_PASSTHROUGH_ERRORSTATE;
  38349. + }
  38350. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38351. + hc->qh->ping_state = 0;
  38352. + }
  38353. + retval = 0;
  38354. + } else if (hc->halt_pending) {
  38355. + /* Don't queue a request if the channel has been halted. */
  38356. + retval = 0;
  38357. + } else if (hc->halt_on_queue) {
  38358. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  38359. + retval = 0;
  38360. + } else if (hc->do_ping) {
  38361. + if (!hc->xfer_started) {
  38362. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38363. + }
  38364. + retval = 0;
  38365. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  38366. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  38367. + if (!hc->xfer_started) {
  38368. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38369. + retval = 1;
  38370. + } else {
  38371. + retval =
  38372. + dwc_otg_hc_continue_transfer(hcd->core_if,
  38373. + hc);
  38374. + }
  38375. + } else {
  38376. + retval = -1;
  38377. + }
  38378. + } else {
  38379. + if (!hc->xfer_started) {
  38380. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38381. + retval = 1;
  38382. + } else {
  38383. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  38384. + }
  38385. + }
  38386. +
  38387. + return retval;
  38388. +}
  38389. +
  38390. +/**
  38391. + * Processes periodic channels for the next frame and queues transactions for
  38392. + * these channels to the DWC_otg controller. After queueing transactions, the
  38393. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  38394. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  38395. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  38396. + */
  38397. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  38398. +{
  38399. + hptxsts_data_t tx_status;
  38400. + dwc_list_link_t *qh_ptr;
  38401. + dwc_otg_qh_t *qh;
  38402. + int status = 0;
  38403. + int no_queue_space = 0;
  38404. + int no_fifo_space = 0;
  38405. +
  38406. + dwc_otg_host_global_regs_t *host_regs;
  38407. + host_regs = hcd->core_if->host_if->host_global_regs;
  38408. +
  38409. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  38410. +#ifdef DEBUG
  38411. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38412. + DWC_DEBUGPL(DBG_HCDV,
  38413. + " P Tx Req Queue Space Avail (before queue): %d\n",
  38414. + tx_status.b.ptxqspcavail);
  38415. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  38416. + tx_status.b.ptxfspcavail);
  38417. +#endif
  38418. +
  38419. + qh_ptr = hcd->periodic_sched_assigned.next;
  38420. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  38421. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38422. + if (tx_status.b.ptxqspcavail == 0) {
  38423. + no_queue_space = 1;
  38424. + break;
  38425. + }
  38426. +
  38427. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38428. +
  38429. + // Do not send a split start transaction any later than frame .6
  38430. + // Note, we have to schedule a periodic in .5 to make it go in .6
  38431. + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  38432. + {
  38433. + qh_ptr = qh_ptr->next;
  38434. + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  38435. + continue;
  38436. + }
  38437. +
  38438. + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
  38439. + if (qh->do_split)
  38440. + fiq_fsm_queue_split_transaction(hcd, qh);
  38441. + else
  38442. + fiq_fsm_queue_isoc_transaction(hcd, qh);
  38443. + } else {
  38444. +
  38445. + /*
  38446. + * Set a flag if we're queueing high-bandwidth in slave mode.
  38447. + * The flag prevents any halts to get into the request queue in
  38448. + * the middle of multiple high-bandwidth packets getting queued.
  38449. + */
  38450. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  38451. + hcd->core_if->queuing_high_bandwidth = 1;
  38452. + }
  38453. + status = queue_transaction(hcd, qh->channel,
  38454. + tx_status.b.ptxfspcavail);
  38455. + if (status < 0) {
  38456. + no_fifo_space = 1;
  38457. + break;
  38458. + }
  38459. + }
  38460. +
  38461. + /*
  38462. + * In Slave mode, stay on the current transfer until there is
  38463. + * nothing more to do or the high-bandwidth request count is
  38464. + * reached. In DMA mode, only need to queue one request. The
  38465. + * controller automatically handles multiple packets for
  38466. + * high-bandwidth transfers.
  38467. + */
  38468. + if (hcd->core_if->dma_enable || status == 0 ||
  38469. + qh->channel->requests == qh->channel->multi_count) {
  38470. + qh_ptr = qh_ptr->next;
  38471. + /*
  38472. + * Move the QH from the periodic assigned schedule to
  38473. + * the periodic queued schedule.
  38474. + */
  38475. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  38476. + &qh->qh_list_entry);
  38477. +
  38478. + /* done queuing high bandwidth */
  38479. + hcd->core_if->queuing_high_bandwidth = 0;
  38480. + }
  38481. + }
  38482. +
  38483. + if (!hcd->core_if->dma_enable) {
  38484. + dwc_otg_core_global_regs_t *global_regs;
  38485. + gintmsk_data_t intr_mask = {.d32 = 0 };
  38486. +
  38487. + global_regs = hcd->core_if->core_global_regs;
  38488. + intr_mask.b.ptxfempty = 1;
  38489. +#ifdef DEBUG
  38490. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38491. + DWC_DEBUGPL(DBG_HCDV,
  38492. + " P Tx Req Queue Space Avail (after queue): %d\n",
  38493. + tx_status.b.ptxqspcavail);
  38494. + DWC_DEBUGPL(DBG_HCDV,
  38495. + " P Tx FIFO Space Avail (after queue): %d\n",
  38496. + tx_status.b.ptxfspcavail);
  38497. +#endif
  38498. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  38499. + no_queue_space || no_fifo_space) {
  38500. + /*
  38501. + * May need to queue more transactions as the request
  38502. + * queue or Tx FIFO empties. Enable the periodic Tx
  38503. + * FIFO empty interrupt. (Always use the half-empty
  38504. + * level to ensure that new requests are loaded as
  38505. + * soon as possible.)
  38506. + */
  38507. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  38508. + intr_mask.d32);
  38509. + } else {
  38510. + /*
  38511. + * Disable the Tx FIFO empty interrupt since there are
  38512. + * no more transactions that need to be queued right
  38513. + * now. This function is called from interrupt
  38514. + * handlers to queue more transactions as transfer
  38515. + * states change.
  38516. + */
  38517. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  38518. + 0);
  38519. + }
  38520. + }
  38521. +}
  38522. +
  38523. +/**
  38524. + * Processes active non-periodic channels and queues transactions for these
  38525. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  38526. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  38527. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  38528. + * FIFO Empty interrupt is disabled.
  38529. + */
  38530. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  38531. +{
  38532. + gnptxsts_data_t tx_status;
  38533. + dwc_list_link_t *orig_qh_ptr;
  38534. + dwc_otg_qh_t *qh;
  38535. + int status;
  38536. + int no_queue_space = 0;
  38537. + int no_fifo_space = 0;
  38538. + int more_to_do = 0;
  38539. +
  38540. + dwc_otg_core_global_regs_t *global_regs =
  38541. + hcd->core_if->core_global_regs;
  38542. +
  38543. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  38544. +#ifdef DEBUG
  38545. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38546. + DWC_DEBUGPL(DBG_HCDV,
  38547. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  38548. + tx_status.b.nptxqspcavail);
  38549. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  38550. + tx_status.b.nptxfspcavail);
  38551. +#endif
  38552. + /*
  38553. + * Keep track of the starting point. Skip over the start-of-list
  38554. + * entry.
  38555. + */
  38556. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  38557. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  38558. + }
  38559. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  38560. +
  38561. + /*
  38562. + * Process once through the active list or until no more space is
  38563. + * available in the request queue or the Tx FIFO.
  38564. + */
  38565. + do {
  38566. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38567. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  38568. + no_queue_space = 1;
  38569. + break;
  38570. + }
  38571. +
  38572. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  38573. + qh_list_entry);
  38574. +
  38575. + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
  38576. + fiq_fsm_queue_split_transaction(hcd, qh);
  38577. + } else {
  38578. + status = queue_transaction(hcd, qh->channel,
  38579. + tx_status.b.nptxfspcavail);
  38580. +
  38581. + if (status > 0) {
  38582. + more_to_do = 1;
  38583. + } else if (status < 0) {
  38584. + no_fifo_space = 1;
  38585. + break;
  38586. + }
  38587. + }
  38588. + /* Advance to next QH, skipping start-of-list entry. */
  38589. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  38590. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  38591. + hcd->non_periodic_qh_ptr =
  38592. + hcd->non_periodic_qh_ptr->next;
  38593. + }
  38594. +
  38595. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  38596. +
  38597. + if (!hcd->core_if->dma_enable) {
  38598. + gintmsk_data_t intr_mask = {.d32 = 0 };
  38599. + intr_mask.b.nptxfempty = 1;
  38600. +
  38601. +#ifdef DEBUG
  38602. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38603. + DWC_DEBUGPL(DBG_HCDV,
  38604. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  38605. + tx_status.b.nptxqspcavail);
  38606. + DWC_DEBUGPL(DBG_HCDV,
  38607. + " NP Tx FIFO Space Avail (after queue): %d\n",
  38608. + tx_status.b.nptxfspcavail);
  38609. +#endif
  38610. + if (more_to_do || no_queue_space || no_fifo_space) {
  38611. + /*
  38612. + * May need to queue more transactions as the request
  38613. + * queue or Tx FIFO empties. Enable the non-periodic
  38614. + * Tx FIFO empty interrupt. (Always use the half-empty
  38615. + * level to ensure that new requests are loaded as
  38616. + * soon as possible.)
  38617. + */
  38618. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  38619. + intr_mask.d32);
  38620. + } else {
  38621. + /*
  38622. + * Disable the Tx FIFO empty interrupt since there are
  38623. + * no more transactions that need to be queued right
  38624. + * now. This function is called from interrupt
  38625. + * handlers to queue more transactions as transfer
  38626. + * states change.
  38627. + */
  38628. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  38629. + 0);
  38630. + }
  38631. + }
  38632. +}
  38633. +
  38634. +/**
  38635. + * This function processes the currently active host channels and queues
  38636. + * transactions for these channels to the DWC_otg controller. It is called
  38637. + * from HCD interrupt handler functions.
  38638. + *
  38639. + * @param hcd The HCD state structure.
  38640. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  38641. + * periodic, or both).
  38642. + */
  38643. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  38644. + dwc_otg_transaction_type_e tr_type)
  38645. +{
  38646. +#ifdef DEBUG_SOF
  38647. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  38648. +#endif
  38649. + /* Process host channels associated with periodic transfers. */
  38650. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  38651. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  38652. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  38653. +
  38654. + process_periodic_channels(hcd);
  38655. + }
  38656. +
  38657. + /* Process host channels associated with non-periodic transfers. */
  38658. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  38659. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  38660. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  38661. + process_non_periodic_channels(hcd);
  38662. + } else {
  38663. + /*
  38664. + * Ensure NP Tx FIFO empty interrupt is disabled when
  38665. + * there are no non-periodic transfers to process.
  38666. + */
  38667. + gintmsk_data_t gintmsk = {.d32 = 0 };
  38668. + gintmsk.b.nptxfempty = 1;
  38669. +
  38670. + if (fiq_enable) {
  38671. + local_fiq_disable();
  38672. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  38673. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  38674. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  38675. + local_fiq_enable();
  38676. + } else {
  38677. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  38678. + }
  38679. + }
  38680. + }
  38681. +}
  38682. +
  38683. +#ifdef DWC_HS_ELECT_TST
  38684. +/*
  38685. + * Quick and dirty hack to implement the HS Electrical Test
  38686. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  38687. + *
  38688. + * This code was copied from our userspace app "hset". It sends a
  38689. + * Get Device Descriptor control sequence in two parts, first the
  38690. + * Setup packet by itself, followed some time later by the In and
  38691. + * Ack packets. Rather than trying to figure out how to add this
  38692. + * functionality to the normal driver code, we just hijack the
  38693. + * hardware, using these two function to drive the hardware
  38694. + * directly.
  38695. + */
  38696. +
  38697. +static dwc_otg_core_global_regs_t *global_regs;
  38698. +static dwc_otg_host_global_regs_t *hc_global_regs;
  38699. +static dwc_otg_hc_regs_t *hc_regs;
  38700. +static uint32_t *data_fifo;
  38701. +
  38702. +static void do_setup(void)
  38703. +{
  38704. + gintsts_data_t gintsts;
  38705. + hctsiz_data_t hctsiz;
  38706. + hcchar_data_t hcchar;
  38707. + haint_data_t haint;
  38708. + hcint_data_t hcint;
  38709. +
  38710. + /* Enable HAINTs */
  38711. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  38712. +
  38713. + /* Enable HCINTs */
  38714. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  38715. +
  38716. + /* Read GINTSTS */
  38717. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38718. +
  38719. + /* Read HAINT */
  38720. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38721. +
  38722. + /* Read HCINT */
  38723. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38724. +
  38725. + /* Read HCCHAR */
  38726. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38727. +
  38728. + /* Clear HCINT */
  38729. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38730. +
  38731. + /* Clear HAINT */
  38732. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38733. +
  38734. + /* Clear GINTSTS */
  38735. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38736. +
  38737. + /* Read GINTSTS */
  38738. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38739. +
  38740. + /*
  38741. + * Send Setup packet (Get Device Descriptor)
  38742. + */
  38743. +
  38744. + /* Make sure channel is disabled */
  38745. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38746. + if (hcchar.b.chen) {
  38747. + hcchar.b.chdis = 1;
  38748. +// hcchar.b.chen = 1;
  38749. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38750. + //sleep(1);
  38751. + dwc_mdelay(1000);
  38752. +
  38753. + /* Read GINTSTS */
  38754. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38755. +
  38756. + /* Read HAINT */
  38757. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38758. +
  38759. + /* Read HCINT */
  38760. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38761. +
  38762. + /* Read HCCHAR */
  38763. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38764. +
  38765. + /* Clear HCINT */
  38766. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38767. +
  38768. + /* Clear HAINT */
  38769. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38770. +
  38771. + /* Clear GINTSTS */
  38772. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38773. +
  38774. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38775. + }
  38776. +
  38777. + /* Set HCTSIZ */
  38778. + hctsiz.d32 = 0;
  38779. + hctsiz.b.xfersize = 8;
  38780. + hctsiz.b.pktcnt = 1;
  38781. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  38782. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38783. +
  38784. + /* Set HCCHAR */
  38785. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38786. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38787. + hcchar.b.epdir = 0;
  38788. + hcchar.b.epnum = 0;
  38789. + hcchar.b.mps = 8;
  38790. + hcchar.b.chen = 1;
  38791. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38792. +
  38793. + /* Fill FIFO with Setup data for Get Device Descriptor */
  38794. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  38795. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  38796. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  38797. +
  38798. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38799. +
  38800. + /* Wait for host channel interrupt */
  38801. + do {
  38802. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38803. + } while (gintsts.b.hcintr == 0);
  38804. +
  38805. + /* Disable HCINTs */
  38806. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  38807. +
  38808. + /* Disable HAINTs */
  38809. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  38810. +
  38811. + /* Read HAINT */
  38812. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38813. +
  38814. + /* Read HCINT */
  38815. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38816. +
  38817. + /* Read HCCHAR */
  38818. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38819. +
  38820. + /* Clear HCINT */
  38821. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38822. +
  38823. + /* Clear HAINT */
  38824. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38825. +
  38826. + /* Clear GINTSTS */
  38827. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38828. +
  38829. + /* Read GINTSTS */
  38830. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38831. +}
  38832. +
  38833. +static void do_in_ack(void)
  38834. +{
  38835. + gintsts_data_t gintsts;
  38836. + hctsiz_data_t hctsiz;
  38837. + hcchar_data_t hcchar;
  38838. + haint_data_t haint;
  38839. + hcint_data_t hcint;
  38840. + host_grxsts_data_t grxsts;
  38841. +
  38842. + /* Enable HAINTs */
  38843. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  38844. +
  38845. + /* Enable HCINTs */
  38846. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  38847. +
  38848. + /* Read GINTSTS */
  38849. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38850. +
  38851. + /* Read HAINT */
  38852. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38853. +
  38854. + /* Read HCINT */
  38855. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38856. +
  38857. + /* Read HCCHAR */
  38858. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38859. +
  38860. + /* Clear HCINT */
  38861. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38862. +
  38863. + /* Clear HAINT */
  38864. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38865. +
  38866. + /* Clear GINTSTS */
  38867. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38868. +
  38869. + /* Read GINTSTS */
  38870. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38871. +
  38872. + /*
  38873. + * Receive Control In packet
  38874. + */
  38875. +
  38876. + /* Make sure channel is disabled */
  38877. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38878. + if (hcchar.b.chen) {
  38879. + hcchar.b.chdis = 1;
  38880. + hcchar.b.chen = 1;
  38881. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38882. + //sleep(1);
  38883. + dwc_mdelay(1000);
  38884. +
  38885. + /* Read GINTSTS */
  38886. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38887. +
  38888. + /* Read HAINT */
  38889. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38890. +
  38891. + /* Read HCINT */
  38892. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38893. +
  38894. + /* Read HCCHAR */
  38895. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38896. +
  38897. + /* Clear HCINT */
  38898. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38899. +
  38900. + /* Clear HAINT */
  38901. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38902. +
  38903. + /* Clear GINTSTS */
  38904. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38905. +
  38906. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38907. + }
  38908. +
  38909. + /* Set HCTSIZ */
  38910. + hctsiz.d32 = 0;
  38911. + hctsiz.b.xfersize = 8;
  38912. + hctsiz.b.pktcnt = 1;
  38913. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  38914. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38915. +
  38916. + /* Set HCCHAR */
  38917. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38918. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38919. + hcchar.b.epdir = 1;
  38920. + hcchar.b.epnum = 0;
  38921. + hcchar.b.mps = 8;
  38922. + hcchar.b.chen = 1;
  38923. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38924. +
  38925. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38926. +
  38927. + /* Wait for receive status queue interrupt */
  38928. + do {
  38929. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38930. + } while (gintsts.b.rxstsqlvl == 0);
  38931. +
  38932. + /* Read RXSTS */
  38933. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  38934. +
  38935. + /* Clear RXSTSQLVL in GINTSTS */
  38936. + gintsts.d32 = 0;
  38937. + gintsts.b.rxstsqlvl = 1;
  38938. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38939. +
  38940. + switch (grxsts.b.pktsts) {
  38941. + case DWC_GRXSTS_PKTSTS_IN:
  38942. + /* Read the data into the host buffer */
  38943. + if (grxsts.b.bcnt > 0) {
  38944. + int i;
  38945. + int word_count = (grxsts.b.bcnt + 3) / 4;
  38946. +
  38947. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  38948. +
  38949. + for (i = 0; i < word_count; i++) {
  38950. + (void)DWC_READ_REG32(data_fifo++);
  38951. + }
  38952. + }
  38953. + break;
  38954. +
  38955. + default:
  38956. + break;
  38957. + }
  38958. +
  38959. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38960. +
  38961. + /* Wait for receive status queue interrupt */
  38962. + do {
  38963. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38964. + } while (gintsts.b.rxstsqlvl == 0);
  38965. +
  38966. + /* Read RXSTS */
  38967. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  38968. +
  38969. + /* Clear RXSTSQLVL in GINTSTS */
  38970. + gintsts.d32 = 0;
  38971. + gintsts.b.rxstsqlvl = 1;
  38972. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38973. +
  38974. + switch (grxsts.b.pktsts) {
  38975. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  38976. + break;
  38977. +
  38978. + default:
  38979. + break;
  38980. + }
  38981. +
  38982. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38983. +
  38984. + /* Wait for host channel interrupt */
  38985. + do {
  38986. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38987. + } while (gintsts.b.hcintr == 0);
  38988. +
  38989. + /* Read HAINT */
  38990. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38991. +
  38992. + /* Read HCINT */
  38993. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38994. +
  38995. + /* Read HCCHAR */
  38996. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38997. +
  38998. + /* Clear HCINT */
  38999. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  39000. +
  39001. + /* Clear HAINT */
  39002. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  39003. +
  39004. + /* Clear GINTSTS */
  39005. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  39006. +
  39007. + /* Read GINTSTS */
  39008. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  39009. +
  39010. +// usleep(100000);
  39011. +// mdelay(100);
  39012. + dwc_mdelay(1);
  39013. +
  39014. + /*
  39015. + * Send handshake packet
  39016. + */
  39017. +
  39018. + /* Read HAINT */
  39019. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  39020. +
  39021. + /* Read HCINT */
  39022. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  39023. +
  39024. + /* Read HCCHAR */
  39025. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  39026. +
  39027. + /* Clear HCINT */
  39028. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  39029. +
  39030. + /* Clear HAINT */
  39031. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  39032. +
  39033. + /* Clear GINTSTS */
  39034. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  39035. +
  39036. + /* Read GINTSTS */
  39037. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  39038. +
  39039. + /* Make sure channel is disabled */
  39040. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  39041. + if (hcchar.b.chen) {
  39042. + hcchar.b.chdis = 1;
  39043. + hcchar.b.chen = 1;
  39044. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  39045. + //sleep(1);
  39046. + dwc_mdelay(1000);
  39047. +
  39048. + /* Read GINTSTS */
  39049. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  39050. +
  39051. + /* Read HAINT */
  39052. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  39053. +
  39054. + /* Read HCINT */
  39055. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  39056. +
  39057. + /* Read HCCHAR */
  39058. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  39059. +
  39060. + /* Clear HCINT */
  39061. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  39062. +
  39063. + /* Clear HAINT */
  39064. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  39065. +
  39066. + /* Clear GINTSTS */
  39067. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  39068. +
  39069. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  39070. + }
  39071. +
  39072. + /* Set HCTSIZ */
  39073. + hctsiz.d32 = 0;
  39074. + hctsiz.b.xfersize = 0;
  39075. + hctsiz.b.pktcnt = 1;
  39076. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  39077. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  39078. +
  39079. + /* Set HCCHAR */
  39080. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  39081. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  39082. + hcchar.b.epdir = 0;
  39083. + hcchar.b.epnum = 0;
  39084. + hcchar.b.mps = 8;
  39085. + hcchar.b.chen = 1;
  39086. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  39087. +
  39088. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  39089. +
  39090. + /* Wait for host channel interrupt */
  39091. + do {
  39092. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  39093. + } while (gintsts.b.hcintr == 0);
  39094. +
  39095. + /* Disable HCINTs */
  39096. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  39097. +
  39098. + /* Disable HAINTs */
  39099. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  39100. +
  39101. + /* Read HAINT */
  39102. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  39103. +
  39104. + /* Read HCINT */
  39105. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  39106. +
  39107. + /* Read HCCHAR */
  39108. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  39109. +
  39110. + /* Clear HCINT */
  39111. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  39112. +
  39113. + /* Clear HAINT */
  39114. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  39115. +
  39116. + /* Clear GINTSTS */
  39117. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  39118. +
  39119. + /* Read GINTSTS */
  39120. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  39121. +}
  39122. +#endif
  39123. +
  39124. +/** Handles hub class-specific requests. */
  39125. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  39126. + uint16_t typeReq,
  39127. + uint16_t wValue,
  39128. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  39129. +{
  39130. + int retval = 0;
  39131. +
  39132. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  39133. + usb_hub_descriptor_t *hub_desc;
  39134. + hprt0_data_t hprt0 = {.d32 = 0 };
  39135. +
  39136. + uint32_t port_status;
  39137. +
  39138. + switch (typeReq) {
  39139. + case UCR_CLEAR_HUB_FEATURE:
  39140. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39141. + "ClearHubFeature 0x%x\n", wValue);
  39142. + switch (wValue) {
  39143. + case UHF_C_HUB_LOCAL_POWER:
  39144. + case UHF_C_HUB_OVER_CURRENT:
  39145. + /* Nothing required here */
  39146. + break;
  39147. + default:
  39148. + retval = -DWC_E_INVALID;
  39149. + DWC_ERROR("DWC OTG HCD - "
  39150. + "ClearHubFeature request %xh unknown\n",
  39151. + wValue);
  39152. + }
  39153. + break;
  39154. + case UCR_CLEAR_PORT_FEATURE:
  39155. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39156. + if (wValue != UHF_PORT_L1)
  39157. +#endif
  39158. + if (!wIndex || wIndex > 1)
  39159. + goto error;
  39160. +
  39161. + switch (wValue) {
  39162. + case UHF_PORT_ENABLE:
  39163. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  39164. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  39165. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39166. + hprt0.b.prtena = 1;
  39167. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39168. + break;
  39169. + case UHF_PORT_SUSPEND:
  39170. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39171. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  39172. +
  39173. + if (core_if->power_down == 2) {
  39174. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  39175. + } else {
  39176. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  39177. + dwc_mdelay(5);
  39178. +
  39179. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39180. + hprt0.b.prtres = 1;
  39181. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39182. + hprt0.b.prtsusp = 0;
  39183. + /* Clear Resume bit */
  39184. + dwc_mdelay(100);
  39185. + hprt0.b.prtres = 0;
  39186. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39187. + }
  39188. + break;
  39189. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39190. + case UHF_PORT_L1:
  39191. + {
  39192. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39193. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  39194. +
  39195. + lpmcfg.d32 =
  39196. + DWC_READ_REG32(&core_if->
  39197. + core_global_regs->glpmcfg);
  39198. + lpmcfg.b.en_utmi_sleep = 0;
  39199. + lpmcfg.b.hird_thres &= (~(1 << 4));
  39200. + lpmcfg.b.prt_sleep_sts = 1;
  39201. + DWC_WRITE_REG32(&core_if->
  39202. + core_global_regs->glpmcfg,
  39203. + lpmcfg.d32);
  39204. +
  39205. + /* Clear Enbl_L1Gating bit. */
  39206. + pcgcctl.b.enbl_sleep_gating = 1;
  39207. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  39208. + 0);
  39209. +
  39210. + dwc_mdelay(5);
  39211. +
  39212. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39213. + hprt0.b.prtres = 1;
  39214. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39215. + hprt0.d32);
  39216. + /* This bit will be cleared in wakeup interrupt handle */
  39217. + break;
  39218. + }
  39219. +#endif
  39220. + case UHF_PORT_POWER:
  39221. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39222. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  39223. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39224. + hprt0.b.prtpwr = 0;
  39225. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39226. + break;
  39227. + case UHF_PORT_INDICATOR:
  39228. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39229. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  39230. + /* Port inidicator not supported */
  39231. + break;
  39232. + case UHF_C_PORT_CONNECTION:
  39233. + /* Clears drivers internal connect status change
  39234. + * flag */
  39235. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39236. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  39237. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  39238. + break;
  39239. + case UHF_C_PORT_RESET:
  39240. + /* Clears the driver's internal Port Reset Change
  39241. + * flag */
  39242. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39243. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  39244. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  39245. + break;
  39246. + case UHF_C_PORT_ENABLE:
  39247. + /* Clears the driver's internal Port
  39248. + * Enable/Disable Change flag */
  39249. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39250. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  39251. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  39252. + break;
  39253. + case UHF_C_PORT_SUSPEND:
  39254. + /* Clears the driver's internal Port Suspend
  39255. + * Change flag, which is set when resume signaling on
  39256. + * the host port is complete */
  39257. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39258. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  39259. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  39260. + break;
  39261. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39262. + case UHF_C_PORT_L1:
  39263. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  39264. + break;
  39265. +#endif
  39266. + case UHF_C_PORT_OVER_CURRENT:
  39267. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39268. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  39269. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  39270. + break;
  39271. + default:
  39272. + retval = -DWC_E_INVALID;
  39273. + DWC_ERROR("DWC OTG HCD - "
  39274. + "ClearPortFeature request %xh "
  39275. + "unknown or unsupported\n", wValue);
  39276. + }
  39277. + break;
  39278. + case UCR_GET_HUB_DESCRIPTOR:
  39279. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39280. + "GetHubDescriptor\n");
  39281. + hub_desc = (usb_hub_descriptor_t *) buf;
  39282. + hub_desc->bDescLength = 9;
  39283. + hub_desc->bDescriptorType = 0x29;
  39284. + hub_desc->bNbrPorts = 1;
  39285. + USETW(hub_desc->wHubCharacteristics, 0x08);
  39286. + hub_desc->bPwrOn2PwrGood = 1;
  39287. + hub_desc->bHubContrCurrent = 0;
  39288. + hub_desc->DeviceRemovable[0] = 0;
  39289. + hub_desc->DeviceRemovable[1] = 0xff;
  39290. + break;
  39291. + case UCR_GET_HUB_STATUS:
  39292. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39293. + "GetHubStatus\n");
  39294. + DWC_MEMSET(buf, 0, 4);
  39295. + break;
  39296. + case UCR_GET_PORT_STATUS:
  39297. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39298. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  39299. + wIndex, dwc_otg_hcd->flags.d32);
  39300. + if (!wIndex || wIndex > 1)
  39301. + goto error;
  39302. +
  39303. + port_status = 0;
  39304. +
  39305. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  39306. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  39307. +
  39308. + if (dwc_otg_hcd->flags.b.port_enable_change)
  39309. + port_status |= (1 << UHF_C_PORT_ENABLE);
  39310. +
  39311. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  39312. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  39313. +
  39314. + if (dwc_otg_hcd->flags.b.port_l1_change)
  39315. + port_status |= (1 << UHF_C_PORT_L1);
  39316. +
  39317. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  39318. + port_status |= (1 << UHF_C_PORT_RESET);
  39319. + }
  39320. +
  39321. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  39322. + DWC_WARN("Overcurrent change detected\n");
  39323. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  39324. + }
  39325. +
  39326. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  39327. + /*
  39328. + * The port is disconnected, which means the core is
  39329. + * either in device mode or it soon will be. Just
  39330. + * return 0's for the remainder of the port status
  39331. + * since the port register can't be read if the core
  39332. + * is in device mode.
  39333. + */
  39334. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  39335. + break;
  39336. + }
  39337. +
  39338. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  39339. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  39340. +
  39341. + if (hprt0.b.prtconnsts)
  39342. + port_status |= (1 << UHF_PORT_CONNECTION);
  39343. +
  39344. + if (hprt0.b.prtena)
  39345. + port_status |= (1 << UHF_PORT_ENABLE);
  39346. +
  39347. + if (hprt0.b.prtsusp)
  39348. + port_status |= (1 << UHF_PORT_SUSPEND);
  39349. +
  39350. + if (hprt0.b.prtovrcurract)
  39351. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  39352. +
  39353. + if (hprt0.b.prtrst)
  39354. + port_status |= (1 << UHF_PORT_RESET);
  39355. +
  39356. + if (hprt0.b.prtpwr)
  39357. + port_status |= (1 << UHF_PORT_POWER);
  39358. +
  39359. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  39360. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  39361. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  39362. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  39363. +
  39364. + if (hprt0.b.prttstctl)
  39365. + port_status |= (1 << UHF_PORT_TEST);
  39366. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  39367. + port_status |= (1 << UHF_PORT_L1);
  39368. + }
  39369. + /*
  39370. + For Synopsys HW emulation of Power down wkup_control asserts the
  39371. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  39372. + We intentionally tell the software that port is in L2Suspend state.
  39373. + Only for STE.
  39374. + */
  39375. + if ((core_if->power_down == 2)
  39376. + && (core_if->hibernation_suspend == 1)) {
  39377. + port_status |= (1 << UHF_PORT_SUSPEND);
  39378. + }
  39379. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  39380. +
  39381. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  39382. +
  39383. + break;
  39384. + case UCR_SET_HUB_FEATURE:
  39385. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39386. + "SetHubFeature\n");
  39387. + /* No HUB features supported */
  39388. + break;
  39389. + case UCR_SET_PORT_FEATURE:
  39390. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  39391. + goto error;
  39392. +
  39393. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  39394. + /*
  39395. + * The port is disconnected, which means the core is
  39396. + * either in device mode or it soon will be. Just
  39397. + * return without doing anything since the port
  39398. + * register can't be written if the core is in device
  39399. + * mode.
  39400. + */
  39401. + break;
  39402. + }
  39403. +
  39404. + switch (wValue) {
  39405. + case UHF_PORT_SUSPEND:
  39406. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39407. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  39408. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  39409. + goto error;
  39410. + }
  39411. + if (core_if->power_down == 2) {
  39412. + int timeout = 300;
  39413. + dwc_irqflags_t flags;
  39414. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39415. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  39416. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  39417. +#ifdef DWC_DEV_SRPCAP
  39418. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  39419. +#endif
  39420. + DWC_PRINTF("Preparing for complete power-off\n");
  39421. +
  39422. + /* Save registers before hibernation */
  39423. + dwc_otg_save_global_regs(core_if);
  39424. + dwc_otg_save_host_regs(core_if);
  39425. +
  39426. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39427. + hprt0.b.prtsusp = 1;
  39428. + hprt0.b.prtena = 0;
  39429. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39430. + /* Spin hprt0.b.prtsusp to became 1 */
  39431. + do {
  39432. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39433. + if (hprt0.b.prtsusp) {
  39434. + break;
  39435. + }
  39436. + dwc_mdelay(1);
  39437. + } while (--timeout);
  39438. + if (!timeout) {
  39439. + DWC_WARN("Suspend wasn't genereted\n");
  39440. + }
  39441. + dwc_udelay(10);
  39442. +
  39443. + /*
  39444. + * We need to disable interrupts to prevent servicing of any IRQ
  39445. + * during going to hibernation
  39446. + */
  39447. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  39448. + core_if->lx_state = DWC_OTG_L2;
  39449. +#ifdef DWC_DEV_SRPCAP
  39450. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39451. + hprt0.b.prtpwr = 0;
  39452. + hprt0.b.prtena = 0;
  39453. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39454. + hprt0.d32);
  39455. +#endif
  39456. + gusbcfg.d32 =
  39457. + DWC_READ_REG32(&core_if->core_global_regs->
  39458. + gusbcfg);
  39459. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  39460. + /* ULPI interface */
  39461. + /* Suspend the Phy Clock */
  39462. + pcgcctl.d32 = 0;
  39463. + pcgcctl.b.stoppclk = 1;
  39464. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  39465. + pcgcctl.d32);
  39466. + dwc_udelay(10);
  39467. + gpwrdn.b.pmuactv = 1;
  39468. + DWC_MODIFY_REG32(&core_if->
  39469. + core_global_regs->
  39470. + gpwrdn, 0, gpwrdn.d32);
  39471. + } else {
  39472. + /* UTMI+ Interface */
  39473. + gpwrdn.b.pmuactv = 1;
  39474. + DWC_MODIFY_REG32(&core_if->
  39475. + core_global_regs->
  39476. + gpwrdn, 0, gpwrdn.d32);
  39477. + dwc_udelay(10);
  39478. + pcgcctl.b.stoppclk = 1;
  39479. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  39480. + dwc_udelay(10);
  39481. + }
  39482. +#ifdef DWC_DEV_SRPCAP
  39483. + gpwrdn.d32 = 0;
  39484. + gpwrdn.b.dis_vbus = 1;
  39485. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39486. + gpwrdn, 0, gpwrdn.d32);
  39487. +#endif
  39488. + gpwrdn.d32 = 0;
  39489. + gpwrdn.b.pmuintsel = 1;
  39490. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39491. + gpwrdn, 0, gpwrdn.d32);
  39492. + dwc_udelay(10);
  39493. +
  39494. + gpwrdn.d32 = 0;
  39495. +#ifdef DWC_DEV_SRPCAP
  39496. + gpwrdn.b.srp_det_msk = 1;
  39497. +#endif
  39498. + gpwrdn.b.disconn_det_msk = 1;
  39499. + gpwrdn.b.lnstchng_msk = 1;
  39500. + gpwrdn.b.sts_chngint_msk = 1;
  39501. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39502. + gpwrdn, 0, gpwrdn.d32);
  39503. + dwc_udelay(10);
  39504. +
  39505. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  39506. + gpwrdn.d32 = 0;
  39507. + gpwrdn.b.pwrdnclmp = 1;
  39508. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39509. + gpwrdn, 0, gpwrdn.d32);
  39510. + dwc_udelay(10);
  39511. +
  39512. + /* Switch off VDD */
  39513. + gpwrdn.d32 = 0;
  39514. + gpwrdn.b.pwrdnswtch = 1;
  39515. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39516. + gpwrdn, 0, gpwrdn.d32);
  39517. +
  39518. +#ifdef DWC_DEV_SRPCAP
  39519. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  39520. + {
  39521. + core_if->pwron_timer_started = 1;
  39522. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  39523. + }
  39524. +#endif
  39525. + /* Save gpwrdn register for further usage if stschng interrupt */
  39526. + core_if->gr_backup->gpwrdn_local =
  39527. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  39528. +
  39529. + /* Set flag to indicate that we are in hibernation */
  39530. + core_if->hibernation_suspend = 1;
  39531. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  39532. +
  39533. + DWC_PRINTF("Host hibernation completed\n");
  39534. + // Exit from case statement
  39535. + break;
  39536. +
  39537. + }
  39538. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  39539. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  39540. + gotgctl_data_t gotgctl = {.d32 = 0 };
  39541. + gotgctl.b.hstsethnpen = 1;
  39542. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39543. + gotgctl, 0, gotgctl.d32);
  39544. + core_if->op_state = A_SUSPEND;
  39545. + }
  39546. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39547. + hprt0.b.prtsusp = 1;
  39548. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39549. + {
  39550. + dwc_irqflags_t flags;
  39551. + /* Update lx_state */
  39552. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  39553. + core_if->lx_state = DWC_OTG_L2;
  39554. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  39555. + }
  39556. + /* Suspend the Phy Clock */
  39557. + {
  39558. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39559. + pcgcctl.b.stoppclk = 1;
  39560. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  39561. + pcgcctl.d32);
  39562. + dwc_udelay(10);
  39563. + }
  39564. +
  39565. + /* For HNP the bus must be suspended for at least 200ms. */
  39566. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  39567. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39568. + pcgcctl.b.stoppclk = 1;
  39569. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  39570. + dwc_mdelay(200);
  39571. + }
  39572. +
  39573. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  39574. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  39575. + if (core_if->adp_enable) {
  39576. + gotgctl_data_t gotgctl = {.d32 = 0 };
  39577. + gpwrdn_data_t gpwrdn;
  39578. +
  39579. + while (gotgctl.b.asesvld == 1) {
  39580. + gotgctl.d32 =
  39581. + DWC_READ_REG32(&core_if->
  39582. + core_global_regs->
  39583. + gotgctl);
  39584. + dwc_mdelay(100);
  39585. + }
  39586. +
  39587. + /* Enable Power Down Logic */
  39588. + gpwrdn.d32 = 0;
  39589. + gpwrdn.b.pmuactv = 1;
  39590. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39591. + gpwrdn, 0, gpwrdn.d32);
  39592. +
  39593. + /* Unmask SRP detected interrupt from Power Down Logic */
  39594. + gpwrdn.d32 = 0;
  39595. + gpwrdn.b.srp_det_msk = 1;
  39596. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39597. + gpwrdn, 0, gpwrdn.d32);
  39598. +
  39599. + dwc_otg_adp_probe_start(core_if);
  39600. + }
  39601. +#endif
  39602. + break;
  39603. + case UHF_PORT_POWER:
  39604. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39605. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  39606. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39607. + hprt0.b.prtpwr = 1;
  39608. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39609. + break;
  39610. + case UHF_PORT_RESET:
  39611. + if ((core_if->power_down == 2)
  39612. + && (core_if->hibernation_suspend == 1)) {
  39613. + /* If we are going to exit from Hibernated
  39614. + * state via USB RESET.
  39615. + */
  39616. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  39617. + } else {
  39618. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39619. +
  39620. + DWC_DEBUGPL(DBG_HCD,
  39621. + "DWC OTG HCD HUB CONTROL - "
  39622. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  39623. + {
  39624. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39625. + pcgcctl.b.enbl_sleep_gating = 1;
  39626. + pcgcctl.b.stoppclk = 1;
  39627. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  39628. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  39629. + }
  39630. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39631. + {
  39632. + glpmcfg_data_t lpmcfg;
  39633. + lpmcfg.d32 =
  39634. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39635. + if (lpmcfg.b.prt_sleep_sts) {
  39636. + lpmcfg.b.en_utmi_sleep = 0;
  39637. + lpmcfg.b.hird_thres &= (~(1 << 4));
  39638. + DWC_WRITE_REG32
  39639. + (&core_if->core_global_regs->glpmcfg,
  39640. + lpmcfg.d32);
  39641. + dwc_mdelay(1);
  39642. + }
  39643. + }
  39644. +#endif
  39645. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39646. + /* Clear suspend bit if resetting from suspended state. */
  39647. + hprt0.b.prtsusp = 0;
  39648. + /* When B-Host the Port reset bit is set in
  39649. + * the Start HCD Callback function, so that
  39650. + * the reset is started within 1ms of the HNP
  39651. + * success interrupt. */
  39652. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  39653. + hprt0.b.prtpwr = 1;
  39654. + hprt0.b.prtrst = 1;
  39655. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  39656. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39657. + hprt0.d32);
  39658. + }
  39659. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  39660. + dwc_mdelay(60);
  39661. + hprt0.b.prtrst = 0;
  39662. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39663. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  39664. + }
  39665. + break;
  39666. +#ifdef DWC_HS_ELECT_TST
  39667. + case UHF_PORT_TEST:
  39668. + {
  39669. + uint32_t t;
  39670. + gintmsk_data_t gintmsk;
  39671. +
  39672. + t = (wIndex >> 8); /* MSB wIndex USB */
  39673. + DWC_DEBUGPL(DBG_HCD,
  39674. + "DWC OTG HCD HUB CONTROL - "
  39675. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  39676. + t);
  39677. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  39678. + if (t < 6) {
  39679. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39680. + hprt0.b.prttstctl = t;
  39681. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39682. + hprt0.d32);
  39683. + } else {
  39684. + /* Setup global vars with reg addresses (quick and
  39685. + * dirty hack, should be cleaned up)
  39686. + */
  39687. + global_regs = core_if->core_global_regs;
  39688. + hc_global_regs =
  39689. + core_if->host_if->host_global_regs;
  39690. + hc_regs =
  39691. + (dwc_otg_hc_regs_t *) ((char *)
  39692. + global_regs +
  39693. + 0x500);
  39694. + data_fifo =
  39695. + (uint32_t *) ((char *)global_regs +
  39696. + 0x1000);
  39697. +
  39698. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  39699. + /* Save current interrupt mask */
  39700. + gintmsk.d32 =
  39701. + DWC_READ_REG32
  39702. + (&global_regs->gintmsk);
  39703. +
  39704. + /* Disable all interrupts while we muck with
  39705. + * the hardware directly
  39706. + */
  39707. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39708. +
  39709. + /* 15 second delay per the test spec */
  39710. + dwc_mdelay(15000);
  39711. +
  39712. + /* Drive suspend on the root port */
  39713. + hprt0.d32 =
  39714. + dwc_otg_read_hprt0(core_if);
  39715. + hprt0.b.prtsusp = 1;
  39716. + hprt0.b.prtres = 0;
  39717. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39718. +
  39719. + /* 15 second delay per the test spec */
  39720. + dwc_mdelay(15000);
  39721. +
  39722. + /* Drive resume on the root port */
  39723. + hprt0.d32 =
  39724. + dwc_otg_read_hprt0(core_if);
  39725. + hprt0.b.prtsusp = 0;
  39726. + hprt0.b.prtres = 1;
  39727. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39728. + dwc_mdelay(100);
  39729. +
  39730. + /* Clear the resume bit */
  39731. + hprt0.b.prtres = 0;
  39732. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39733. +
  39734. + /* Restore interrupts */
  39735. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39736. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  39737. + /* Save current interrupt mask */
  39738. + gintmsk.d32 =
  39739. + DWC_READ_REG32
  39740. + (&global_regs->gintmsk);
  39741. +
  39742. + /* Disable all interrupts while we muck with
  39743. + * the hardware directly
  39744. + */
  39745. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39746. +
  39747. + /* 15 second delay per the test spec */
  39748. + dwc_mdelay(15000);
  39749. +
  39750. + /* Send the Setup packet */
  39751. + do_setup();
  39752. +
  39753. + /* 15 second delay so nothing else happens for awhile */
  39754. + dwc_mdelay(15000);
  39755. +
  39756. + /* Restore interrupts */
  39757. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39758. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  39759. + /* Save current interrupt mask */
  39760. + gintmsk.d32 =
  39761. + DWC_READ_REG32
  39762. + (&global_regs->gintmsk);
  39763. +
  39764. + /* Disable all interrupts while we muck with
  39765. + * the hardware directly
  39766. + */
  39767. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39768. +
  39769. + /* Send the Setup packet */
  39770. + do_setup();
  39771. +
  39772. + /* 15 second delay so nothing else happens for awhile */
  39773. + dwc_mdelay(15000);
  39774. +
  39775. + /* Send the In and Ack packets */
  39776. + do_in_ack();
  39777. +
  39778. + /* 15 second delay so nothing else happens for awhile */
  39779. + dwc_mdelay(15000);
  39780. +
  39781. + /* Restore interrupts */
  39782. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39783. + }
  39784. + }
  39785. + break;
  39786. + }
  39787. +#endif /* DWC_HS_ELECT_TST */
  39788. +
  39789. + case UHF_PORT_INDICATOR:
  39790. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39791. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  39792. + /* Not supported */
  39793. + break;
  39794. + default:
  39795. + retval = -DWC_E_INVALID;
  39796. + DWC_ERROR("DWC OTG HCD - "
  39797. + "SetPortFeature request %xh "
  39798. + "unknown or unsupported\n", wValue);
  39799. + break;
  39800. + }
  39801. + break;
  39802. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39803. + case UCR_SET_AND_TEST_PORT_FEATURE:
  39804. + if (wValue != UHF_PORT_L1) {
  39805. + goto error;
  39806. + }
  39807. + {
  39808. + int portnum, hird, devaddr, remwake;
  39809. + glpmcfg_data_t lpmcfg;
  39810. + uint32_t time_usecs;
  39811. + gintsts_data_t gintsts;
  39812. + gintmsk_data_t gintmsk;
  39813. +
  39814. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  39815. + goto error;
  39816. + }
  39817. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  39818. + goto error;
  39819. + }
  39820. + /* Check if the port currently is in SLEEP state */
  39821. + lpmcfg.d32 =
  39822. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39823. + if (lpmcfg.b.prt_sleep_sts) {
  39824. + DWC_INFO("Port is already in sleep mode\n");
  39825. + buf[0] = 0; /* Return success */
  39826. + break;
  39827. + }
  39828. +
  39829. + portnum = wIndex & 0xf;
  39830. + hird = (wIndex >> 4) & 0xf;
  39831. + devaddr = (wIndex >> 8) & 0x7f;
  39832. + remwake = (wIndex >> 15);
  39833. +
  39834. + if (portnum != 1) {
  39835. + retval = -DWC_E_INVALID;
  39836. + DWC_WARN
  39837. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  39838. + portnum);
  39839. + break;
  39840. + }
  39841. +
  39842. + DWC_PRINTF
  39843. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  39844. + portnum, hird, devaddr, remwake);
  39845. + /* Disable LPM interrupt */
  39846. + gintmsk.d32 = 0;
  39847. + gintmsk.b.lpmtranrcvd = 1;
  39848. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  39849. + gintmsk.d32, 0);
  39850. +
  39851. + if (dwc_otg_hcd_send_lpm
  39852. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  39853. + retval = -DWC_E_INVALID;
  39854. + break;
  39855. + }
  39856. +
  39857. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  39858. + /* We will consider timeout if time_usecs microseconds pass,
  39859. + * and we don't receive LPM transaction status.
  39860. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  39861. + * core will set lpmtranrcvd bit.
  39862. + */
  39863. + do {
  39864. + gintsts.d32 =
  39865. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  39866. + if (gintsts.b.lpmtranrcvd) {
  39867. + break;
  39868. + }
  39869. + dwc_udelay(1);
  39870. + } while (--time_usecs);
  39871. + /* lpm_int bit will be cleared in LPM interrupt handler */
  39872. +
  39873. + /* Now fill status
  39874. + * 0x00 - Success
  39875. + * 0x10 - NYET
  39876. + * 0x11 - Timeout
  39877. + */
  39878. + if (!gintsts.b.lpmtranrcvd) {
  39879. + buf[0] = 0x3; /* Completion code is Timeout */
  39880. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  39881. + } else {
  39882. + lpmcfg.d32 =
  39883. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39884. + if (lpmcfg.b.lpm_resp == 0x3) {
  39885. + /* ACK responce from the device */
  39886. + buf[0] = 0x00; /* Success */
  39887. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  39888. + /* NYET responce from the device */
  39889. + buf[0] = 0x2;
  39890. + } else {
  39891. + /* Otherwise responce with Timeout */
  39892. + buf[0] = 0x3;
  39893. + }
  39894. + }
  39895. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  39896. + lpmcfg.b.lpm_resp);
  39897. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  39898. + gintmsk.d32);
  39899. +
  39900. + break;
  39901. + }
  39902. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  39903. + default:
  39904. +error:
  39905. + retval = -DWC_E_INVALID;
  39906. + DWC_WARN("DWC OTG HCD - "
  39907. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  39908. + typeReq, wIndex, wValue);
  39909. + break;
  39910. + }
  39911. +
  39912. + return retval;
  39913. +}
  39914. +
  39915. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39916. +/** Returns index of host channel to perform LPM transaction. */
  39917. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  39918. +{
  39919. + dwc_otg_core_if_t *core_if = hcd->core_if;
  39920. + dwc_hc_t *hc;
  39921. + hcchar_data_t hcchar;
  39922. + gintmsk_data_t gintmsk = {.d32 = 0 };
  39923. +
  39924. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  39925. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  39926. + return -1;
  39927. + }
  39928. +
  39929. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  39930. +
  39931. + /* Mask host channel interrupts. */
  39932. + gintmsk.b.hcintr = 1;
  39933. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  39934. +
  39935. + /* Fill fields that core needs for LPM transaction */
  39936. + hcchar.b.devaddr = devaddr;
  39937. + hcchar.b.epnum = 0;
  39938. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  39939. + hcchar.b.mps = 64;
  39940. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  39941. + hcchar.b.epdir = 0; /* OUT */
  39942. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  39943. + hcchar.d32);
  39944. +
  39945. + /* Remove the host channel from the free list. */
  39946. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  39947. +
  39948. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  39949. +
  39950. + return hc->hc_num;
  39951. +}
  39952. +
  39953. +/** Release hc after performing LPM transaction */
  39954. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  39955. +{
  39956. + dwc_hc_t *hc;
  39957. + glpmcfg_data_t lpmcfg;
  39958. + uint8_t hc_num;
  39959. +
  39960. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  39961. + hc_num = lpmcfg.b.lpm_chan_index;
  39962. +
  39963. + hc = hcd->hc_ptr_array[hc_num];
  39964. +
  39965. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  39966. + /* Return host channel to free list */
  39967. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  39968. +}
  39969. +
  39970. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  39971. + uint8_t bRemoteWake)
  39972. +{
  39973. + glpmcfg_data_t lpmcfg;
  39974. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39975. + int channel;
  39976. +
  39977. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  39978. + if (channel < 0) {
  39979. + return channel;
  39980. + }
  39981. +
  39982. + pcgcctl.b.enbl_sleep_gating = 1;
  39983. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  39984. +
  39985. + /* Read LPM config register */
  39986. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  39987. +
  39988. + /* Program LPM transaction fields */
  39989. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  39990. + lpmcfg.b.hird = hird;
  39991. + lpmcfg.b.hird_thres = 0x1c;
  39992. + lpmcfg.b.lpm_chan_index = channel;
  39993. + lpmcfg.b.en_utmi_sleep = 1;
  39994. + /* Program LPM config register */
  39995. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  39996. +
  39997. + /* Send LPM transaction */
  39998. + lpmcfg.b.send_lpm = 1;
  39999. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  40000. +
  40001. + return 0;
  40002. +}
  40003. +
  40004. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  40005. +
  40006. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  40007. +{
  40008. + int retval;
  40009. +
  40010. + if (port != 1) {
  40011. + return -DWC_E_INVALID;
  40012. + }
  40013. +
  40014. + retval = (hcd->flags.b.port_connect_status_change ||
  40015. + hcd->flags.b.port_reset_change ||
  40016. + hcd->flags.b.port_enable_change ||
  40017. + hcd->flags.b.port_suspend_change ||
  40018. + hcd->flags.b.port_over_current_change);
  40019. +#ifdef DEBUG
  40020. + if (retval) {
  40021. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  40022. + " Root port status changed\n");
  40023. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  40024. + hcd->flags.b.port_connect_status_change);
  40025. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  40026. + hcd->flags.b.port_reset_change);
  40027. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  40028. + hcd->flags.b.port_enable_change);
  40029. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  40030. + hcd->flags.b.port_suspend_change);
  40031. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  40032. + hcd->flags.b.port_over_current_change);
  40033. + }
  40034. +#endif
  40035. + return retval;
  40036. +}
  40037. +
  40038. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  40039. +{
  40040. + hfnum_data_t hfnum;
  40041. + hfnum.d32 =
  40042. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  40043. + hfnum);
  40044. +
  40045. +#ifdef DEBUG_SOF
  40046. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  40047. + hfnum.b.frnum);
  40048. +#endif
  40049. + return hfnum.b.frnum;
  40050. +}
  40051. +
  40052. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  40053. + struct dwc_otg_hcd_function_ops *fops)
  40054. +{
  40055. + int retval = 0;
  40056. +
  40057. + hcd->fops = fops;
  40058. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  40059. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  40060. + dwc_otg_hcd_reinit(hcd);
  40061. + } else {
  40062. + retval = -DWC_E_NO_DEVICE;
  40063. + }
  40064. +
  40065. + return retval;
  40066. +}
  40067. +
  40068. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  40069. +{
  40070. + return hcd->priv;
  40071. +}
  40072. +
  40073. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  40074. +{
  40075. + hcd->priv = priv_data;
  40076. +}
  40077. +
  40078. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  40079. +{
  40080. + return hcd->otg_port;
  40081. +}
  40082. +
  40083. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  40084. +{
  40085. + uint32_t is_b_host;
  40086. + if (hcd->core_if->op_state == B_HOST) {
  40087. + is_b_host = 1;
  40088. + } else {
  40089. + is_b_host = 0;
  40090. + }
  40091. +
  40092. + return is_b_host;
  40093. +}
  40094. +
  40095. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  40096. + int iso_desc_count, int atomic_alloc)
  40097. +{
  40098. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  40099. + uint32_t size;
  40100. +
  40101. + size =
  40102. + sizeof(*dwc_otg_urb) +
  40103. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  40104. + if (atomic_alloc)
  40105. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  40106. + else
  40107. + dwc_otg_urb = DWC_ALLOC(size);
  40108. +
  40109. + if (dwc_otg_urb)
  40110. + dwc_otg_urb->packet_count = iso_desc_count;
  40111. + else {
  40112. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  40113. + "%salloc of %db failed\n",
  40114. + atomic_alloc?"atomic ":"", size);
  40115. + }
  40116. + return dwc_otg_urb;
  40117. +}
  40118. +
  40119. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  40120. + uint8_t dev_addr, uint8_t ep_num,
  40121. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  40122. +{
  40123. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  40124. + ep_type, ep_dir, mps);
  40125. +#if 0
  40126. + DWC_PRINTF
  40127. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  40128. + dev_addr, ep_num, ep_dir, ep_type, mps);
  40129. +#endif
  40130. +}
  40131. +
  40132. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  40133. + void *urb_handle, void *buf, dwc_dma_t dma,
  40134. + uint32_t buflen, void *setup_packet,
  40135. + dwc_dma_t setup_dma, uint32_t flags,
  40136. + uint16_t interval)
  40137. +{
  40138. + dwc_otg_urb->priv = urb_handle;
  40139. + dwc_otg_urb->buf = buf;
  40140. + dwc_otg_urb->dma = dma;
  40141. + dwc_otg_urb->length = buflen;
  40142. + dwc_otg_urb->setup_packet = setup_packet;
  40143. + dwc_otg_urb->setup_dma = setup_dma;
  40144. + dwc_otg_urb->flags = flags;
  40145. + dwc_otg_urb->interval = interval;
  40146. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  40147. +}
  40148. +
  40149. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  40150. +{
  40151. + return dwc_otg_urb->status;
  40152. +}
  40153. +
  40154. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  40155. +{
  40156. + return dwc_otg_urb->actual_length;
  40157. +}
  40158. +
  40159. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  40160. +{
  40161. + return dwc_otg_urb->error_count;
  40162. +}
  40163. +
  40164. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  40165. + int desc_num, uint32_t offset,
  40166. + uint32_t length)
  40167. +{
  40168. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  40169. + dwc_otg_urb->iso_descs[desc_num].length = length;
  40170. +}
  40171. +
  40172. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  40173. + int desc_num)
  40174. +{
  40175. + return dwc_otg_urb->iso_descs[desc_num].status;
  40176. +}
  40177. +
  40178. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  40179. + dwc_otg_urb, int desc_num)
  40180. +{
  40181. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  40182. +}
  40183. +
  40184. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  40185. +{
  40186. + int allocated = 0;
  40187. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40188. +
  40189. + if (qh) {
  40190. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  40191. + allocated = 1;
  40192. + }
  40193. + }
  40194. + return allocated;
  40195. +}
  40196. +
  40197. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  40198. +{
  40199. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40200. + int freed = 0;
  40201. + DWC_ASSERT(qh, "qh is not allocated\n");
  40202. +
  40203. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  40204. + freed = 1;
  40205. + }
  40206. +
  40207. + return freed;
  40208. +}
  40209. +
  40210. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  40211. +{
  40212. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40213. + DWC_ASSERT(qh, "qh is not allocated\n");
  40214. + return qh->usecs;
  40215. +}
  40216. +
  40217. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  40218. +{
  40219. +#ifdef DEBUG
  40220. + int num_channels;
  40221. + int i;
  40222. + gnptxsts_data_t np_tx_status;
  40223. + hptxsts_data_t p_tx_status;
  40224. +
  40225. + num_channels = hcd->core_if->core_params->host_channels;
  40226. + DWC_PRINTF("\n");
  40227. + DWC_PRINTF
  40228. + ("************************************************************\n");
  40229. + DWC_PRINTF("HCD State:\n");
  40230. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  40231. + for (i = 0; i < num_channels; i++) {
  40232. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  40233. + DWC_PRINTF(" Channel %d:\n", i);
  40234. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  40235. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  40236. + DWC_PRINTF(" speed: %d\n", hc->speed);
  40237. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  40238. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  40239. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  40240. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  40241. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  40242. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  40243. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  40244. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  40245. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  40246. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  40247. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  40248. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  40249. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  40250. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  40251. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  40252. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  40253. + DWC_PRINTF(" requests: %d\n", hc->requests);
  40254. + DWC_PRINTF(" qh: %p\n", hc->qh);
  40255. + if (hc->xfer_started) {
  40256. + hfnum_data_t hfnum;
  40257. + hcchar_data_t hcchar;
  40258. + hctsiz_data_t hctsiz;
  40259. + hcint_data_t hcint;
  40260. + hcintmsk_data_t hcintmsk;
  40261. + hfnum.d32 =
  40262. + DWC_READ_REG32(&hcd->core_if->
  40263. + host_if->host_global_regs->hfnum);
  40264. + hcchar.d32 =
  40265. + DWC_READ_REG32(&hcd->core_if->host_if->
  40266. + hc_regs[i]->hcchar);
  40267. + hctsiz.d32 =
  40268. + DWC_READ_REG32(&hcd->core_if->host_if->
  40269. + hc_regs[i]->hctsiz);
  40270. + hcint.d32 =
  40271. + DWC_READ_REG32(&hcd->core_if->host_if->
  40272. + hc_regs[i]->hcint);
  40273. + hcintmsk.d32 =
  40274. + DWC_READ_REG32(&hcd->core_if->host_if->
  40275. + hc_regs[i]->hcintmsk);
  40276. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  40277. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  40278. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  40279. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  40280. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  40281. + }
  40282. + if (hc->xfer_started && hc->qh) {
  40283. + dwc_otg_qtd_t *qtd;
  40284. + dwc_otg_hcd_urb_t *urb;
  40285. +
  40286. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  40287. + if (!qtd->in_process)
  40288. + break;
  40289. +
  40290. + urb = qtd->urb;
  40291. + DWC_PRINTF(" URB Info:\n");
  40292. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  40293. + if (urb) {
  40294. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  40295. + dwc_otg_hcd_get_dev_addr(&urb->
  40296. + pipe_info),
  40297. + dwc_otg_hcd_get_ep_num(&urb->
  40298. + pipe_info),
  40299. + dwc_otg_hcd_is_pipe_in(&urb->
  40300. + pipe_info) ?
  40301. + "IN" : "OUT");
  40302. + DWC_PRINTF(" Max packet size: %d\n",
  40303. + dwc_otg_hcd_get_mps(&urb->
  40304. + pipe_info));
  40305. + DWC_PRINTF(" transfer_buffer: %p\n",
  40306. + urb->buf);
  40307. + DWC_PRINTF(" transfer_dma: %p\n",
  40308. + (void *)urb->dma);
  40309. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  40310. + urb->length);
  40311. + DWC_PRINTF(" actual_length: %d\n",
  40312. + urb->actual_length);
  40313. + }
  40314. + }
  40315. + }
  40316. + }
  40317. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  40318. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  40319. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  40320. + np_tx_status.d32 =
  40321. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  40322. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  40323. + np_tx_status.b.nptxqspcavail);
  40324. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  40325. + np_tx_status.b.nptxfspcavail);
  40326. + p_tx_status.d32 =
  40327. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  40328. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  40329. + p_tx_status.b.ptxqspcavail);
  40330. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  40331. + dwc_otg_hcd_dump_frrem(hcd);
  40332. + dwc_otg_dump_global_registers(hcd->core_if);
  40333. + dwc_otg_dump_host_registers(hcd->core_if);
  40334. + DWC_PRINTF
  40335. + ("************************************************************\n");
  40336. + DWC_PRINTF("\n");
  40337. +#endif
  40338. +}
  40339. +
  40340. +#ifdef DEBUG
  40341. +void dwc_print_setup_data(uint8_t * setup)
  40342. +{
  40343. + int i;
  40344. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  40345. + DWC_PRINTF("Setup Data = MSB ");
  40346. + for (i = 7; i >= 0; i--)
  40347. + DWC_PRINTF("%02x ", setup[i]);
  40348. + DWC_PRINTF("\n");
  40349. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  40350. + (setup[0] & 0x80) ? "Device-to-Host" :
  40351. + "Host-to-Device");
  40352. + DWC_PRINTF(" bmRequestType Type = ");
  40353. + switch ((setup[0] & 0x60) >> 5) {
  40354. + case 0:
  40355. + DWC_PRINTF("Standard\n");
  40356. + break;
  40357. + case 1:
  40358. + DWC_PRINTF("Class\n");
  40359. + break;
  40360. + case 2:
  40361. + DWC_PRINTF("Vendor\n");
  40362. + break;
  40363. + case 3:
  40364. + DWC_PRINTF("Reserved\n");
  40365. + break;
  40366. + }
  40367. + DWC_PRINTF(" bmRequestType Recipient = ");
  40368. + switch (setup[0] & 0x1f) {
  40369. + case 0:
  40370. + DWC_PRINTF("Device\n");
  40371. + break;
  40372. + case 1:
  40373. + DWC_PRINTF("Interface\n");
  40374. + break;
  40375. + case 2:
  40376. + DWC_PRINTF("Endpoint\n");
  40377. + break;
  40378. + case 3:
  40379. + DWC_PRINTF("Other\n");
  40380. + break;
  40381. + default:
  40382. + DWC_PRINTF("Reserved\n");
  40383. + break;
  40384. + }
  40385. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  40386. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  40387. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  40388. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  40389. + }
  40390. +}
  40391. +#endif
  40392. +
  40393. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  40394. +{
  40395. +#if 0
  40396. + DWC_PRINTF("Frame remaining at SOF:\n");
  40397. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40398. + hcd->frrem_samples, hcd->frrem_accum,
  40399. + (hcd->frrem_samples > 0) ?
  40400. + hcd->frrem_accum / hcd->frrem_samples : 0);
  40401. +
  40402. + DWC_PRINTF("\n");
  40403. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  40404. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40405. + hcd->core_if->hfnum_7_samples,
  40406. + hcd->core_if->hfnum_7_frrem_accum,
  40407. + (hcd->core_if->hfnum_7_samples >
  40408. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  40409. + hcd->core_if->hfnum_7_samples : 0);
  40410. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  40411. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40412. + hcd->core_if->hfnum_0_samples,
  40413. + hcd->core_if->hfnum_0_frrem_accum,
  40414. + (hcd->core_if->hfnum_0_samples >
  40415. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  40416. + hcd->core_if->hfnum_0_samples : 0);
  40417. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  40418. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40419. + hcd->core_if->hfnum_other_samples,
  40420. + hcd->core_if->hfnum_other_frrem_accum,
  40421. + (hcd->core_if->hfnum_other_samples >
  40422. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  40423. + hcd->core_if->hfnum_other_samples : 0);
  40424. +
  40425. + DWC_PRINTF("\n");
  40426. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  40427. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40428. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  40429. + (hcd->hfnum_7_samples_a > 0) ?
  40430. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  40431. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  40432. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40433. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  40434. + (hcd->hfnum_0_samples_a > 0) ?
  40435. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  40436. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  40437. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40438. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  40439. + (hcd->hfnum_other_samples_a > 0) ?
  40440. + hcd->hfnum_other_frrem_accum_a /
  40441. + hcd->hfnum_other_samples_a : 0);
  40442. +
  40443. + DWC_PRINTF("\n");
  40444. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  40445. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40446. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  40447. + (hcd->hfnum_7_samples_b > 0) ?
  40448. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  40449. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  40450. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40451. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  40452. + (hcd->hfnum_0_samples_b > 0) ?
  40453. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  40454. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  40455. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40456. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  40457. + (hcd->hfnum_other_samples_b > 0) ?
  40458. + hcd->hfnum_other_frrem_accum_b /
  40459. + hcd->hfnum_other_samples_b : 0);
  40460. +#endif
  40461. +}
  40462. +
  40463. +#endif /* DWC_DEVICE_ONLY */
  40464. --- /dev/null
  40465. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  40466. @@ -0,0 +1,870 @@
  40467. +/* ==========================================================================
  40468. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  40469. + * $Revision: #58 $
  40470. + * $Date: 2011/09/15 $
  40471. + * $Change: 1846647 $
  40472. + *
  40473. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  40474. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  40475. + * otherwise expressly agreed to in writing between Synopsys and you.
  40476. + *
  40477. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  40478. + * any End User Software License Agreement or Agreement for Licensed Product
  40479. + * with Synopsys or any supplement thereto. You are permitted to use and
  40480. + * redistribute this Software in source and binary forms, with or without
  40481. + * modification, provided that redistributions of source code must retain this
  40482. + * notice. You may not view, use, disclose, copy or distribute this file or
  40483. + * any information contained herein except pursuant to this license grant from
  40484. + * Synopsys. If you do not agree with this notice, including the disclaimer
  40485. + * below, then you are not authorized to use the Software.
  40486. + *
  40487. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  40488. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40489. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40490. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  40491. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  40492. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  40493. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40494. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40495. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40496. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40497. + * DAMAGE.
  40498. + * ========================================================================== */
  40499. +#ifndef DWC_DEVICE_ONLY
  40500. +#ifndef __DWC_HCD_H__
  40501. +#define __DWC_HCD_H__
  40502. +
  40503. +#include "dwc_otg_os_dep.h"
  40504. +#include "usb.h"
  40505. +#include "dwc_otg_hcd_if.h"
  40506. +#include "dwc_otg_core_if.h"
  40507. +#include "dwc_list.h"
  40508. +#include "dwc_otg_cil.h"
  40509. +#include "dwc_otg_fiq_fsm.h"
  40510. +#include "dwc_otg_driver.h"
  40511. +
  40512. +
  40513. +/**
  40514. + * @file
  40515. + *
  40516. + * This file contains the structures, constants, and interfaces for
  40517. + * the Host Contoller Driver (HCD).
  40518. + *
  40519. + * The Host Controller Driver (HCD) is responsible for translating requests
  40520. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  40521. + * It isolates the USBD from the specifics of the controller by providing an
  40522. + * API to the USBD.
  40523. + */
  40524. +
  40525. +struct dwc_otg_hcd_pipe_info {
  40526. + uint8_t dev_addr;
  40527. + uint8_t ep_num;
  40528. + uint8_t pipe_type;
  40529. + uint8_t pipe_dir;
  40530. + uint16_t mps;
  40531. +};
  40532. +
  40533. +struct dwc_otg_hcd_iso_packet_desc {
  40534. + uint32_t offset;
  40535. + uint32_t length;
  40536. + uint32_t actual_length;
  40537. + uint32_t status;
  40538. +};
  40539. +
  40540. +struct dwc_otg_qtd;
  40541. +
  40542. +struct dwc_otg_hcd_urb {
  40543. + void *priv;
  40544. + struct dwc_otg_qtd *qtd;
  40545. + void *buf;
  40546. + dwc_dma_t dma;
  40547. + void *setup_packet;
  40548. + dwc_dma_t setup_dma;
  40549. + uint32_t length;
  40550. + uint32_t actual_length;
  40551. + uint32_t status;
  40552. + uint32_t error_count;
  40553. + uint32_t packet_count;
  40554. + uint32_t flags;
  40555. + uint16_t interval;
  40556. + struct dwc_otg_hcd_pipe_info pipe_info;
  40557. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  40558. +};
  40559. +
  40560. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  40561. +{
  40562. + return pipe->ep_num;
  40563. +}
  40564. +
  40565. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  40566. + *pipe)
  40567. +{
  40568. + return pipe->pipe_type;
  40569. +}
  40570. +
  40571. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  40572. +{
  40573. + return pipe->mps;
  40574. +}
  40575. +
  40576. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  40577. + *pipe)
  40578. +{
  40579. + return pipe->dev_addr;
  40580. +}
  40581. +
  40582. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  40583. + *pipe)
  40584. +{
  40585. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  40586. +}
  40587. +
  40588. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  40589. + *pipe)
  40590. +{
  40591. + return (pipe->pipe_type == UE_INTERRUPT);
  40592. +}
  40593. +
  40594. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  40595. + *pipe)
  40596. +{
  40597. + return (pipe->pipe_type == UE_BULK);
  40598. +}
  40599. +
  40600. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  40601. + *pipe)
  40602. +{
  40603. + return (pipe->pipe_type == UE_CONTROL);
  40604. +}
  40605. +
  40606. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  40607. +{
  40608. + return (pipe->pipe_dir == UE_DIR_IN);
  40609. +}
  40610. +
  40611. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  40612. + *pipe)
  40613. +{
  40614. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  40615. +}
  40616. +
  40617. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  40618. + uint8_t devaddr, uint8_t ep_num,
  40619. + uint8_t pipe_type, uint8_t pipe_dir,
  40620. + uint16_t mps)
  40621. +{
  40622. + pipe->dev_addr = devaddr;
  40623. + pipe->ep_num = ep_num;
  40624. + pipe->pipe_type = pipe_type;
  40625. + pipe->pipe_dir = pipe_dir;
  40626. + pipe->mps = mps;
  40627. +}
  40628. +
  40629. +/**
  40630. + * Phases for control transfers.
  40631. + */
  40632. +typedef enum dwc_otg_control_phase {
  40633. + DWC_OTG_CONTROL_SETUP,
  40634. + DWC_OTG_CONTROL_DATA,
  40635. + DWC_OTG_CONTROL_STATUS
  40636. +} dwc_otg_control_phase_e;
  40637. +
  40638. +/** Transaction types. */
  40639. +typedef enum dwc_otg_transaction_type {
  40640. + DWC_OTG_TRANSACTION_NONE = 0,
  40641. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  40642. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  40643. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  40644. +} dwc_otg_transaction_type_e;
  40645. +
  40646. +struct dwc_otg_qh;
  40647. +
  40648. +/**
  40649. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  40650. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  40651. + * (of one of these types) submitted to the HCD. The transfer associated with
  40652. + * a QTD may require one or multiple transactions.
  40653. + *
  40654. + * A QTD is linked to a Queue Head, which is entered in either the
  40655. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  40656. + * execution, some or all of its transactions may be executed. After
  40657. + * execution, the state of the QTD is updated. The QTD may be retired if all
  40658. + * its transactions are complete or if an error occurred. Otherwise, it
  40659. + * remains in the schedule so more transactions can be executed later.
  40660. + */
  40661. +typedef struct dwc_otg_qtd {
  40662. + /**
  40663. + * Determines the PID of the next data packet for the data phase of
  40664. + * control transfers. Ignored for other transfer types.<br>
  40665. + * One of the following values:
  40666. + * - DWC_OTG_HC_PID_DATA0
  40667. + * - DWC_OTG_HC_PID_DATA1
  40668. + */
  40669. + uint8_t data_toggle;
  40670. +
  40671. + /** Current phase for control transfers (Setup, Data, or Status). */
  40672. + dwc_otg_control_phase_e control_phase;
  40673. +
  40674. + /** Keep track of the current split type
  40675. + * for FS/LS endpoints on a HS Hub */
  40676. + uint8_t complete_split;
  40677. +
  40678. + /** How many bytes transferred during SSPLIT OUT */
  40679. + uint32_t ssplit_out_xfer_count;
  40680. +
  40681. + /**
  40682. + * Holds the number of bus errors that have occurred for a transaction
  40683. + * within this transfer.
  40684. + */
  40685. + uint8_t error_count;
  40686. +
  40687. + /**
  40688. + * Index of the next frame descriptor for an isochronous transfer. A
  40689. + * frame descriptor describes the buffer position and length of the
  40690. + * data to be transferred in the next scheduled (micro)frame of an
  40691. + * isochronous transfer. It also holds status for that transaction.
  40692. + * The frame index starts at 0.
  40693. + */
  40694. + uint16_t isoc_frame_index;
  40695. +
  40696. + /** Position of the ISOC split on full/low speed */
  40697. + uint8_t isoc_split_pos;
  40698. +
  40699. + /** Position of the ISOC split in the buffer for the current frame */
  40700. + uint16_t isoc_split_offset;
  40701. +
  40702. + /** URB for this transfer */
  40703. + struct dwc_otg_hcd_urb *urb;
  40704. +
  40705. + struct dwc_otg_qh *qh;
  40706. +
  40707. + /** This list of QTDs */
  40708. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  40709. +
  40710. + /** Indicates if this QTD is currently processed by HW. */
  40711. + uint8_t in_process;
  40712. +
  40713. + /** Number of DMA descriptors for this QTD */
  40714. + uint8_t n_desc;
  40715. +
  40716. + /**
  40717. + * Last activated frame(packet) index.
  40718. + * Used in Descriptor DMA mode only.
  40719. + */
  40720. + uint16_t isoc_frame_index_last;
  40721. +
  40722. +} dwc_otg_qtd_t;
  40723. +
  40724. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  40725. +
  40726. +/**
  40727. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  40728. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  40729. + * be entered in either the non-periodic or periodic schedule.
  40730. + */
  40731. +typedef struct dwc_otg_qh {
  40732. + /**
  40733. + * Endpoint type.
  40734. + * One of the following values:
  40735. + * - UE_CONTROL
  40736. + * - UE_BULK
  40737. + * - UE_INTERRUPT
  40738. + * - UE_ISOCHRONOUS
  40739. + */
  40740. + uint8_t ep_type;
  40741. + uint8_t ep_is_in;
  40742. +
  40743. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  40744. + uint16_t maxp;
  40745. +
  40746. + /**
  40747. + * Device speed.
  40748. + * One of the following values:
  40749. + * - DWC_OTG_EP_SPEED_LOW
  40750. + * - DWC_OTG_EP_SPEED_FULL
  40751. + * - DWC_OTG_EP_SPEED_HIGH
  40752. + */
  40753. + uint8_t dev_speed;
  40754. +
  40755. + /**
  40756. + * Determines the PID of the next data packet for non-control
  40757. + * transfers. Ignored for control transfers.<br>
  40758. + * One of the following values:
  40759. + * - DWC_OTG_HC_PID_DATA0
  40760. + * - DWC_OTG_HC_PID_DATA1
  40761. + */
  40762. + uint8_t data_toggle;
  40763. +
  40764. + /** Ping state if 1. */
  40765. + uint8_t ping_state;
  40766. +
  40767. + /**
  40768. + * List of QTDs for this QH.
  40769. + */
  40770. + struct dwc_otg_qtd_list qtd_list;
  40771. +
  40772. + /** Host channel currently processing transfers for this QH. */
  40773. + struct dwc_hc *channel;
  40774. +
  40775. + /** Full/low speed endpoint on high-speed hub requires split. */
  40776. + uint8_t do_split;
  40777. +
  40778. + /** @name Periodic schedule information */
  40779. + /** @{ */
  40780. +
  40781. + /** Bandwidth in microseconds per (micro)frame. */
  40782. + uint16_t usecs;
  40783. +
  40784. + /** Interval between transfers in (micro)frames. */
  40785. + uint16_t interval;
  40786. +
  40787. + /**
  40788. + * (micro)frame to initialize a periodic transfer. The transfer
  40789. + * executes in the following (micro)frame.
  40790. + */
  40791. + uint16_t sched_frame;
  40792. +
  40793. + /*
  40794. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  40795. + */
  40796. + uint16_t nak_frame;
  40797. +
  40798. + /** (micro)frame at which last start split was initialized. */
  40799. + uint16_t start_split_frame;
  40800. +
  40801. + /** @} */
  40802. +
  40803. + /**
  40804. + * Used instead of original buffer if
  40805. + * it(physical address) is not dword-aligned.
  40806. + */
  40807. + uint8_t *dw_align_buf;
  40808. + dwc_dma_t dw_align_buf_dma;
  40809. +
  40810. + /** Entry for QH in either the periodic or non-periodic schedule. */
  40811. + dwc_list_link_t qh_list_entry;
  40812. +
  40813. + /** @name Descriptor DMA support */
  40814. + /** @{ */
  40815. +
  40816. + /** Descriptor List. */
  40817. + dwc_otg_host_dma_desc_t *desc_list;
  40818. +
  40819. + /** Descriptor List physical address. */
  40820. + dwc_dma_t desc_list_dma;
  40821. +
  40822. + /**
  40823. + * Xfer Bytes array.
  40824. + * Each element corresponds to a descriptor and indicates
  40825. + * original XferSize size value for the descriptor.
  40826. + */
  40827. + uint32_t *n_bytes;
  40828. +
  40829. + /** Actual number of transfer descriptors in a list. */
  40830. + uint16_t ntd;
  40831. +
  40832. + /** First activated isochronous transfer descriptor index. */
  40833. + uint8_t td_first;
  40834. + /** Last activated isochronous transfer descriptor index. */
  40835. + uint8_t td_last;
  40836. +
  40837. + /** @} */
  40838. +
  40839. +
  40840. + uint16_t speed;
  40841. + uint16_t frame_usecs[8];
  40842. +
  40843. + uint32_t skip_count;
  40844. +} dwc_otg_qh_t;
  40845. +
  40846. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  40847. +
  40848. +typedef struct urb_tq_entry {
  40849. + struct urb *urb;
  40850. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  40851. +} urb_tq_entry_t;
  40852. +
  40853. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  40854. +
  40855. +/**
  40856. + * This structure holds the state of the HCD, including the non-periodic and
  40857. + * periodic schedules.
  40858. + */
  40859. +struct dwc_otg_hcd {
  40860. + /** The DWC otg device pointer */
  40861. + struct dwc_otg_device *otg_dev;
  40862. + /** DWC OTG Core Interface Layer */
  40863. + dwc_otg_core_if_t *core_if;
  40864. +
  40865. + /** Function HCD driver callbacks */
  40866. + struct dwc_otg_hcd_function_ops *fops;
  40867. +
  40868. + /** Internal DWC HCD Flags */
  40869. + volatile union dwc_otg_hcd_internal_flags {
  40870. + uint32_t d32;
  40871. + struct {
  40872. + unsigned port_connect_status_change:1;
  40873. + unsigned port_connect_status:1;
  40874. + unsigned port_reset_change:1;
  40875. + unsigned port_enable_change:1;
  40876. + unsigned port_suspend_change:1;
  40877. + unsigned port_over_current_change:1;
  40878. + unsigned port_l1_change:1;
  40879. + unsigned port_speed:2;
  40880. + unsigned reserved:24;
  40881. + } b;
  40882. + } flags;
  40883. +
  40884. + /**
  40885. + * Inactive items in the non-periodic schedule. This is a list of
  40886. + * Queue Heads. Transfers associated with these Queue Heads are not
  40887. + * currently assigned to a host channel.
  40888. + */
  40889. + dwc_list_link_t non_periodic_sched_inactive;
  40890. +
  40891. + /**
  40892. + * Active items in the non-periodic schedule. This is a list of
  40893. + * Queue Heads. Transfers associated with these Queue Heads are
  40894. + * currently assigned to a host channel.
  40895. + */
  40896. + dwc_list_link_t non_periodic_sched_active;
  40897. +
  40898. + /**
  40899. + * Pointer to the next Queue Head to process in the active
  40900. + * non-periodic schedule.
  40901. + */
  40902. + dwc_list_link_t *non_periodic_qh_ptr;
  40903. +
  40904. + /**
  40905. + * Inactive items in the periodic schedule. This is a list of QHs for
  40906. + * periodic transfers that are _not_ scheduled for the next frame.
  40907. + * Each QH in the list has an interval counter that determines when it
  40908. + * needs to be scheduled for execution. This scheduling mechanism
  40909. + * allows only a simple calculation for periodic bandwidth used (i.e.
  40910. + * must assume that all periodic transfers may need to execute in the
  40911. + * same frame). However, it greatly simplifies scheduling and should
  40912. + * be sufficient for the vast majority of OTG hosts, which need to
  40913. + * connect to a small number of peripherals at one time.
  40914. + *
  40915. + * Items move from this list to periodic_sched_ready when the QH
  40916. + * interval counter is 0 at SOF.
  40917. + */
  40918. + dwc_list_link_t periodic_sched_inactive;
  40919. +
  40920. + /**
  40921. + * List of periodic QHs that are ready for execution in the next
  40922. + * frame, but have not yet been assigned to host channels.
  40923. + *
  40924. + * Items move from this list to periodic_sched_assigned as host
  40925. + * channels become available during the current frame.
  40926. + */
  40927. + dwc_list_link_t periodic_sched_ready;
  40928. +
  40929. + /**
  40930. + * List of periodic QHs to be executed in the next frame that are
  40931. + * assigned to host channels.
  40932. + *
  40933. + * Items move from this list to periodic_sched_queued as the
  40934. + * transactions for the QH are queued to the DWC_otg controller.
  40935. + */
  40936. + dwc_list_link_t periodic_sched_assigned;
  40937. +
  40938. + /**
  40939. + * List of periodic QHs that have been queued for execution.
  40940. + *
  40941. + * Items move from this list to either periodic_sched_inactive or
  40942. + * periodic_sched_ready when the channel associated with the transfer
  40943. + * is released. If the interval for the QH is 1, the item moves to
  40944. + * periodic_sched_ready because it must be rescheduled for the next
  40945. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  40946. + */
  40947. + dwc_list_link_t periodic_sched_queued;
  40948. +
  40949. + /**
  40950. + * Total bandwidth claimed so far for periodic transfers. This value
  40951. + * is in microseconds per (micro)frame. The assumption is that all
  40952. + * periodic transfers may occur in the same (micro)frame.
  40953. + */
  40954. + uint16_t periodic_usecs;
  40955. +
  40956. + /**
  40957. + * Total bandwidth claimed so far for all periodic transfers
  40958. + * in a frame.
  40959. + * This will include a mixture of HS and FS transfers.
  40960. + * Units are microseconds per (micro)frame.
  40961. + * We have a budget per frame and have to schedule
  40962. + * transactions accordingly.
  40963. + * Watch out for the fact that things are actually scheduled for the
  40964. + * "next frame".
  40965. + */
  40966. + uint16_t frame_usecs[8];
  40967. +
  40968. +
  40969. + /**
  40970. + * Frame number read from the core at SOF. The value ranges from 0 to
  40971. + * DWC_HFNUM_MAX_FRNUM.
  40972. + */
  40973. + uint16_t frame_number;
  40974. +
  40975. + /**
  40976. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  40977. + */
  40978. + uint16_t periodic_qh_count;
  40979. +
  40980. + /**
  40981. + * Free host channels in the controller. This is a list of
  40982. + * dwc_hc_t items.
  40983. + */
  40984. + struct hc_list free_hc_list;
  40985. + /**
  40986. + * Number of host channels assigned to periodic transfers. Currently
  40987. + * assuming that there is a dedicated host channel for each periodic
  40988. + * transaction and at least one host channel available for
  40989. + * non-periodic transactions.
  40990. + */
  40991. + int periodic_channels; /* microframe_schedule==0 */
  40992. +
  40993. + /**
  40994. + * Number of host channels assigned to non-periodic transfers.
  40995. + */
  40996. + int non_periodic_channels; /* microframe_schedule==0 */
  40997. +
  40998. + /**
  40999. + * Number of host channels assigned to non-periodic transfers.
  41000. + */
  41001. + int available_host_channels;
  41002. +
  41003. + /**
  41004. + * Array of pointers to the host channel descriptors. Allows accessing
  41005. + * a host channel descriptor given the host channel number. This is
  41006. + * useful in interrupt handlers.
  41007. + */
  41008. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  41009. +
  41010. + /**
  41011. + * Buffer to use for any data received during the status phase of a
  41012. + * control transfer. Normally no data is transferred during the status
  41013. + * phase. This buffer is used as a bit bucket.
  41014. + */
  41015. + uint8_t *status_buf;
  41016. +
  41017. + /**
  41018. + * DMA address for status_buf.
  41019. + */
  41020. + dma_addr_t status_buf_dma;
  41021. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  41022. +
  41023. + /**
  41024. + * Connection timer. An OTG host must display a message if the device
  41025. + * does not connect. Started when the VBus power is turned on via
  41026. + * sysfs attribute "buspower".
  41027. + */
  41028. + dwc_timer_t *conn_timer;
  41029. +
  41030. + /* Tasket to do a reset */
  41031. + dwc_tasklet_t *reset_tasklet;
  41032. +
  41033. + dwc_tasklet_t *completion_tasklet;
  41034. + struct urb_list completed_urb_list;
  41035. +
  41036. + /* */
  41037. + dwc_spinlock_t *lock;
  41038. + /**
  41039. + * Private data that could be used by OS wrapper.
  41040. + */
  41041. + void *priv;
  41042. +
  41043. + uint8_t otg_port;
  41044. +
  41045. + /** Frame List */
  41046. + uint32_t *frame_list;
  41047. +
  41048. + /** Hub - Port assignment */
  41049. + int hub_port[128];
  41050. +#ifdef FIQ_DEBUG
  41051. + int hub_port_alloc[2048];
  41052. +#endif
  41053. +
  41054. + /** Frame List DMA address */
  41055. + dma_addr_t frame_list_dma;
  41056. +
  41057. + struct fiq_stack *fiq_stack;
  41058. + struct fiq_state *fiq_state;
  41059. +
  41060. + /** Virtual address for split transaction DMA bounce buffers */
  41061. + struct fiq_dma_blob *fiq_dmab;
  41062. +
  41063. +#ifdef DEBUG
  41064. + uint32_t frrem_samples;
  41065. + uint64_t frrem_accum;
  41066. +
  41067. + uint32_t hfnum_7_samples_a;
  41068. + uint64_t hfnum_7_frrem_accum_a;
  41069. + uint32_t hfnum_0_samples_a;
  41070. + uint64_t hfnum_0_frrem_accum_a;
  41071. + uint32_t hfnum_other_samples_a;
  41072. + uint64_t hfnum_other_frrem_accum_a;
  41073. +
  41074. + uint32_t hfnum_7_samples_b;
  41075. + uint64_t hfnum_7_frrem_accum_b;
  41076. + uint32_t hfnum_0_samples_b;
  41077. + uint64_t hfnum_0_frrem_accum_b;
  41078. + uint32_t hfnum_other_samples_b;
  41079. + uint64_t hfnum_other_frrem_accum_b;
  41080. +#endif
  41081. +};
  41082. +
  41083. +static inline struct device *dwc_otg_hcd_to_dev(struct dwc_otg_hcd *hcd)
  41084. +{
  41085. + return &hcd->otg_dev->os_dep.platformdev->dev;
  41086. +}
  41087. +
  41088. +/** @name Transaction Execution Functions */
  41089. +/** @{ */
  41090. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  41091. + * hcd);
  41092. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  41093. + dwc_otg_transaction_type_e tr_type);
  41094. +
  41095. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  41096. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  41097. +
  41098. +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  41099. +extern int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  41100. +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
  41101. +
  41102. +/** @} */
  41103. +
  41104. +/** @name Interrupt Handler Functions */
  41105. +/** @{ */
  41106. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  41107. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  41108. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  41109. + dwc_otg_hcd);
  41110. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  41111. + dwc_otg_hcd);
  41112. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  41113. + dwc_otg_hcd);
  41114. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  41115. + dwc_otg_hcd);
  41116. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  41117. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  41118. + dwc_otg_hcd);
  41119. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  41120. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  41121. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  41122. + uint32_t num);
  41123. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  41124. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  41125. + dwc_otg_hcd);
  41126. +/** @} */
  41127. +
  41128. +/** @name Schedule Queue Functions */
  41129. +/** @{ */
  41130. +
  41131. +/* Implemented in dwc_otg_hcd_queue.c */
  41132. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  41133. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  41134. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41135. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41136. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41137. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41138. + int sched_csplit);
  41139. +
  41140. +/** Remove and free a QH */
  41141. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  41142. + dwc_otg_qh_t * qh)
  41143. +{
  41144. + dwc_irqflags_t flags;
  41145. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  41146. + dwc_otg_hcd_qh_remove(hcd, qh);
  41147. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  41148. + dwc_otg_hcd_qh_free(hcd, qh);
  41149. +}
  41150. +
  41151. +/** Allocates memory for a QH structure.
  41152. + * @return Returns the memory allocate or NULL on error. */
  41153. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  41154. +{
  41155. + if (atomic_alloc)
  41156. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  41157. + else
  41158. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  41159. +}
  41160. +
  41161. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  41162. + int atomic_alloc);
  41163. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  41164. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  41165. + dwc_otg_qh_t ** qh, int atomic_alloc);
  41166. +
  41167. +/** Allocates memory for a QTD structure.
  41168. + * @return Returns the memory allocate or NULL on error. */
  41169. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  41170. +{
  41171. + if (atomic_alloc)
  41172. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  41173. + else
  41174. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  41175. +}
  41176. +
  41177. +/** Frees the memory for a QTD structure. QTD should already be removed from
  41178. + * list.
  41179. + * @param qtd QTD to free.*/
  41180. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  41181. +{
  41182. + DWC_FREE(qtd);
  41183. +}
  41184. +
  41185. +/** Removes a QTD from list.
  41186. + * @param hcd HCD instance.
  41187. + * @param qtd QTD to remove from list.
  41188. + * @param qh QTD belongs to.
  41189. + */
  41190. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  41191. + dwc_otg_qtd_t * qtd,
  41192. + dwc_otg_qh_t * qh)
  41193. +{
  41194. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  41195. +}
  41196. +
  41197. +/** Remove and free a QTD
  41198. + * Need to disable IRQ and hold hcd lock while calling this function out of
  41199. + * interrupt servicing chain */
  41200. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  41201. + dwc_otg_qtd_t * qtd,
  41202. + dwc_otg_qh_t * qh)
  41203. +{
  41204. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  41205. + dwc_otg_hcd_qtd_free(qtd);
  41206. +}
  41207. +
  41208. +/** @} */
  41209. +
  41210. +/** @name Descriptor DMA Supporting Functions */
  41211. +/** @{ */
  41212. +
  41213. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41214. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  41215. + dwc_hc_t * hc,
  41216. + dwc_otg_hc_regs_t * hc_regs,
  41217. + dwc_otg_halt_status_e halt_status);
  41218. +
  41219. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41220. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41221. +
  41222. +/** @} */
  41223. +
  41224. +/** @name Internal Functions */
  41225. +/** @{ */
  41226. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  41227. +/** @} */
  41228. +
  41229. +#ifdef CONFIG_USB_DWC_OTG_LPM
  41230. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  41231. + uint8_t devaddr);
  41232. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  41233. +#endif
  41234. +
  41235. +/** Gets the QH that contains the list_head */
  41236. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  41237. +
  41238. +/** Gets the QTD that contains the list_head */
  41239. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  41240. +
  41241. +/** Check if QH is non-periodic */
  41242. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  41243. + (_qh_ptr_->ep_type == UE_CONTROL))
  41244. +
  41245. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  41246. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  41247. +
  41248. +/** Packet size for any kind of endpoint descriptor */
  41249. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  41250. +
  41251. +/**
  41252. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  41253. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  41254. + * frame number when the max frame number is reached.
  41255. + */
  41256. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  41257. +{
  41258. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  41259. + (DWC_HFNUM_MAX_FRNUM >> 1);
  41260. +}
  41261. +
  41262. +/**
  41263. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  41264. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  41265. + * number when the max frame number is reached.
  41266. + */
  41267. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  41268. +{
  41269. + return (frame1 != frame2) &&
  41270. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  41271. + (DWC_HFNUM_MAX_FRNUM >> 1));
  41272. +}
  41273. +
  41274. +/**
  41275. + * Increments _frame by the amount specified by _inc. The addition is done
  41276. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  41277. + */
  41278. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  41279. +{
  41280. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  41281. +}
  41282. +
  41283. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  41284. +{
  41285. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  41286. +}
  41287. +
  41288. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  41289. +{
  41290. + return frame & 0x7;
  41291. +}
  41292. +
  41293. +extern void init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  41294. +
  41295. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  41296. + dwc_otg_hc_regs_t * hc_regs,
  41297. + dwc_otg_qtd_t * qtd);
  41298. +
  41299. +#ifdef DEBUG
  41300. +/**
  41301. + * Macro to sample the remaining PHY clocks left in the current frame. This
  41302. + * may be used during debugging to determine the average time it takes to
  41303. + * execute sections of code. There are two possible sample points, "a" and
  41304. + * "b", so the _letter argument must be one of these values.
  41305. + *
  41306. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  41307. + * example, "cat /sys/devices/lm0/hcd_frrem".
  41308. + */
  41309. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  41310. +{ \
  41311. + hfnum_data_t hfnum; \
  41312. + dwc_otg_qtd_t *qtd; \
  41313. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  41314. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  41315. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  41316. + switch (hfnum.b.frnum & 0x7) { \
  41317. + case 7: \
  41318. + _hcd->hfnum_7_samples_##_letter++; \
  41319. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  41320. + break; \
  41321. + case 0: \
  41322. + _hcd->hfnum_0_samples_##_letter++; \
  41323. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  41324. + break; \
  41325. + default: \
  41326. + _hcd->hfnum_other_samples_##_letter++; \
  41327. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  41328. + break; \
  41329. + } \
  41330. + } \
  41331. +}
  41332. +#else
  41333. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  41334. +#endif
  41335. +#endif
  41336. +#endif /* DWC_DEVICE_ONLY */
  41337. --- /dev/null
  41338. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  41339. @@ -0,0 +1,1134 @@
  41340. +/*==========================================================================
  41341. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  41342. + * $Revision: #10 $
  41343. + * $Date: 2011/10/20 $
  41344. + * $Change: 1869464 $
  41345. + *
  41346. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  41347. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  41348. + * otherwise expressly agreed to in writing between Synopsys and you.
  41349. + *
  41350. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  41351. + * any End User Software License Agreement or Agreement for Licensed Product
  41352. + * with Synopsys or any supplement thereto. You are permitted to use and
  41353. + * redistribute this Software in source and binary forms, with or without
  41354. + * modification, provided that redistributions of source code must retain this
  41355. + * notice. You may not view, use, disclose, copy or distribute this file or
  41356. + * any information contained herein except pursuant to this license grant from
  41357. + * Synopsys. If you do not agree with this notice, including the disclaimer
  41358. + * below, then you are not authorized to use the Software.
  41359. + *
  41360. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  41361. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41362. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41363. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  41364. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  41365. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  41366. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41367. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  41368. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41369. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  41370. + * DAMAGE.
  41371. + * ========================================================================== */
  41372. +#ifndef DWC_DEVICE_ONLY
  41373. +
  41374. +/** @file
  41375. + * This file contains Descriptor DMA support implementation for host mode.
  41376. + */
  41377. +
  41378. +#include "dwc_otg_hcd.h"
  41379. +#include "dwc_otg_regs.h"
  41380. +
  41381. +extern bool microframe_schedule;
  41382. +
  41383. +static inline uint8_t frame_list_idx(uint16_t frame)
  41384. +{
  41385. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  41386. +}
  41387. +
  41388. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  41389. +{
  41390. + return (idx + inc) &
  41391. + (((speed ==
  41392. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  41393. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  41394. +}
  41395. +
  41396. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  41397. +{
  41398. + return (idx - inc) &
  41399. + (((speed ==
  41400. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  41401. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  41402. +}
  41403. +
  41404. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  41405. +{
  41406. + return (((qh->ep_type == UE_ISOCHRONOUS)
  41407. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  41408. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  41409. +}
  41410. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  41411. +{
  41412. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  41413. + ? ((qh->interval + 8 - 1) / 8)
  41414. + : qh->interval);
  41415. +}
  41416. +
  41417. +static int desc_list_alloc(struct device *dev, dwc_otg_qh_t * qh)
  41418. +{
  41419. + int retval = 0;
  41420. +
  41421. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  41422. + DWC_DMA_ALLOC(dev, sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  41423. + &qh->desc_list_dma);
  41424. +
  41425. + if (!qh->desc_list) {
  41426. + retval = -DWC_E_NO_MEMORY;
  41427. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  41428. +
  41429. + }
  41430. +
  41431. + dwc_memset(qh->desc_list, 0x00,
  41432. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  41433. +
  41434. + qh->n_bytes =
  41435. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  41436. +
  41437. + if (!qh->n_bytes) {
  41438. + retval = -DWC_E_NO_MEMORY;
  41439. + DWC_ERROR
  41440. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  41441. + __func__);
  41442. +
  41443. + }
  41444. + return retval;
  41445. +
  41446. +}
  41447. +
  41448. +static void desc_list_free(struct device *dev, dwc_otg_qh_t * qh)
  41449. +{
  41450. + if (qh->desc_list) {
  41451. + DWC_DMA_FREE(dev, max_desc_num(qh), qh->desc_list,
  41452. + qh->desc_list_dma);
  41453. + qh->desc_list = NULL;
  41454. + }
  41455. +
  41456. + if (qh->n_bytes) {
  41457. + DWC_FREE(qh->n_bytes);
  41458. + qh->n_bytes = NULL;
  41459. + }
  41460. +}
  41461. +
  41462. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  41463. +{
  41464. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41465. + int retval = 0;
  41466. +
  41467. + if (hcd->frame_list)
  41468. + return 0;
  41469. +
  41470. + hcd->frame_list = DWC_DMA_ALLOC(dev, 4 * MAX_FRLIST_EN_NUM,
  41471. + &hcd->frame_list_dma);
  41472. + if (!hcd->frame_list) {
  41473. + retval = -DWC_E_NO_MEMORY;
  41474. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  41475. + }
  41476. +
  41477. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  41478. +
  41479. + return retval;
  41480. +}
  41481. +
  41482. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  41483. +{
  41484. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41485. +
  41486. + if (!hcd->frame_list)
  41487. + return;
  41488. +
  41489. + DWC_DMA_FREE(dev, 4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  41490. + hcd->frame_list = NULL;
  41491. +}
  41492. +
  41493. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  41494. +{
  41495. +
  41496. + hcfg_data_t hcfg;
  41497. +
  41498. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  41499. +
  41500. + if (hcfg.b.perschedena) {
  41501. + /* already enabled */
  41502. + return;
  41503. + }
  41504. +
  41505. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  41506. + hcd->frame_list_dma);
  41507. +
  41508. + switch (fr_list_en) {
  41509. + case 64:
  41510. + hcfg.b.frlisten = 3;
  41511. + break;
  41512. + case 32:
  41513. + hcfg.b.frlisten = 2;
  41514. + break;
  41515. + case 16:
  41516. + hcfg.b.frlisten = 1;
  41517. + break;
  41518. + case 8:
  41519. + hcfg.b.frlisten = 0;
  41520. + break;
  41521. + default:
  41522. + break;
  41523. + }
  41524. +
  41525. + hcfg.b.perschedena = 1;
  41526. +
  41527. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  41528. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  41529. +
  41530. +}
  41531. +
  41532. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  41533. +{
  41534. + hcfg_data_t hcfg;
  41535. +
  41536. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  41537. +
  41538. + if (!hcfg.b.perschedena) {
  41539. + /* already disabled */
  41540. + return;
  41541. + }
  41542. + hcfg.b.perschedena = 0;
  41543. +
  41544. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  41545. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  41546. +}
  41547. +
  41548. +/*
  41549. + * Activates/Deactivates FrameList entries for the channel
  41550. + * based on endpoint servicing period.
  41551. + */
  41552. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  41553. +{
  41554. + uint16_t i, j, inc;
  41555. + dwc_hc_t *hc = NULL;
  41556. +
  41557. + if (!qh->channel) {
  41558. + DWC_ERROR("qh->channel = %p", qh->channel);
  41559. + return;
  41560. + }
  41561. +
  41562. + if (!hcd) {
  41563. + DWC_ERROR("------hcd = %p", hcd);
  41564. + return;
  41565. + }
  41566. +
  41567. + if (!hcd->frame_list) {
  41568. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  41569. + return;
  41570. + }
  41571. +
  41572. + hc = qh->channel;
  41573. + inc = frame_incr_val(qh);
  41574. + if (qh->ep_type == UE_ISOCHRONOUS)
  41575. + i = frame_list_idx(qh->sched_frame);
  41576. + else
  41577. + i = 0;
  41578. +
  41579. + j = i;
  41580. + do {
  41581. + if (enable)
  41582. + hcd->frame_list[j] |= (1 << hc->hc_num);
  41583. + else
  41584. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  41585. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  41586. + }
  41587. + while (j != i);
  41588. + if (!enable)
  41589. + return;
  41590. + hc->schinfo = 0;
  41591. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  41592. + j = 1;
  41593. + /* TODO - check this */
  41594. + inc = (8 + qh->interval - 1) / qh->interval;
  41595. + for (i = 0; i < inc; i++) {
  41596. + hc->schinfo |= j;
  41597. + j = j << qh->interval;
  41598. + }
  41599. + } else {
  41600. + hc->schinfo = 0xff;
  41601. + }
  41602. +}
  41603. +
  41604. +#if 1
  41605. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  41606. +{
  41607. + int i = 0;
  41608. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  41609. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  41610. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  41611. + if (!(i % 8) && i)
  41612. + DWC_PRINTF("\n");
  41613. + }
  41614. + DWC_PRINTF("\n----\n");
  41615. +
  41616. +}
  41617. +#endif
  41618. +
  41619. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41620. +{
  41621. + dwc_hc_t *hc = qh->channel;
  41622. + if (dwc_qh_is_non_per(qh)) {
  41623. + if (!microframe_schedule)
  41624. + hcd->non_periodic_channels--;
  41625. + else
  41626. + hcd->available_host_channels++;
  41627. + } else
  41628. + update_frame_list(hcd, qh, 0);
  41629. +
  41630. + /*
  41631. + * The condition is added to prevent double cleanup try in case of device
  41632. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  41633. + */
  41634. + if (hc->qh) {
  41635. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  41636. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  41637. + hc->qh = NULL;
  41638. + }
  41639. +
  41640. + qh->channel = NULL;
  41641. + qh->ntd = 0;
  41642. +
  41643. + if (qh->desc_list) {
  41644. + dwc_memset(qh->desc_list, 0x00,
  41645. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  41646. + }
  41647. +}
  41648. +
  41649. +/**
  41650. + * Initializes a QH structure's Descriptor DMA related members.
  41651. + * Allocates memory for descriptor list.
  41652. + * On first periodic QH, allocates memory for FrameList
  41653. + * and enables periodic scheduling.
  41654. + *
  41655. + * @param hcd The HCD state structure for the DWC OTG controller.
  41656. + * @param qh The QH to init.
  41657. + *
  41658. + * @return 0 if successful, negative error code otherwise.
  41659. + */
  41660. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41661. +{
  41662. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41663. + int retval = 0;
  41664. +
  41665. + if (qh->do_split) {
  41666. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  41667. + return -1;
  41668. + }
  41669. +
  41670. + retval = desc_list_alloc(dev, qh);
  41671. +
  41672. + if ((retval == 0)
  41673. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  41674. + if (!hcd->frame_list) {
  41675. + retval = frame_list_alloc(hcd);
  41676. + /* Enable periodic schedule on first periodic QH */
  41677. + if (retval == 0)
  41678. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  41679. + }
  41680. + }
  41681. +
  41682. + qh->ntd = 0;
  41683. +
  41684. + return retval;
  41685. +}
  41686. +
  41687. +/**
  41688. + * Frees descriptor list memory associated with the QH.
  41689. + * If QH is periodic and the last, frees FrameList memory
  41690. + * and disables periodic scheduling.
  41691. + *
  41692. + * @param hcd The HCD state structure for the DWC OTG controller.
  41693. + * @param qh The QH to init.
  41694. + */
  41695. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41696. +{
  41697. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41698. +
  41699. + desc_list_free(dev, qh);
  41700. +
  41701. + /*
  41702. + * Channel still assigned due to some reasons.
  41703. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  41704. + * ChHalted interrupt to release the channel. Afterwards
  41705. + * when it comes here from endpoint disable routine
  41706. + * channel remains assigned.
  41707. + */
  41708. + if (qh->channel)
  41709. + release_channel_ddma(hcd, qh);
  41710. +
  41711. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  41712. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  41713. +
  41714. + per_sched_disable(hcd);
  41715. + frame_list_free(hcd);
  41716. + }
  41717. +}
  41718. +
  41719. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  41720. +{
  41721. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  41722. + /*
  41723. + * Descriptor set(8 descriptors) index
  41724. + * which is 8-aligned.
  41725. + */
  41726. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  41727. + } else {
  41728. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  41729. + }
  41730. +}
  41731. +
  41732. +/*
  41733. + * Determine starting frame for Isochronous transfer.
  41734. + * Few frames skipped to prevent race condition with HC.
  41735. + */
  41736. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41737. + uint8_t * skip_frames)
  41738. +{
  41739. + uint16_t frame = 0;
  41740. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  41741. +
  41742. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  41743. +
  41744. + /*
  41745. + * skip_frames is used to limit activated descriptors number
  41746. + * to avoid the situation when HC services the last activated
  41747. + * descriptor firstly.
  41748. + * Example for FS:
  41749. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  41750. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  41751. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  41752. + * list will be fully programmed with Active descriptors and it is possible
  41753. + * case(rare) that the latest descriptor(considering rollback) corresponding
  41754. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  41755. + * up to 11 uframes(16 in the code) may be skipped.
  41756. + */
  41757. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  41758. + /*
  41759. + * Consider uframe counter also, to start xfer asap.
  41760. + * If half of the frame elapsed skip 2 frames otherwise
  41761. + * just 1 frame.
  41762. + * Starting descriptor index must be 8-aligned, so
  41763. + * if the current frame is near to complete the next one
  41764. + * is skipped as well.
  41765. + */
  41766. +
  41767. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  41768. + *skip_frames = 2 * 8;
  41769. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  41770. + } else {
  41771. + *skip_frames = 1 * 8;
  41772. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  41773. + }
  41774. +
  41775. + frame = dwc_full_frame_num(frame);
  41776. + } else {
  41777. + /*
  41778. + * Two frames are skipped for FS - the current and the next.
  41779. + * But for descriptor programming, 1 frame(descriptor) is enough,
  41780. + * see example above.
  41781. + */
  41782. + *skip_frames = 1;
  41783. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  41784. + }
  41785. +
  41786. + return frame;
  41787. +}
  41788. +
  41789. +/*
  41790. + * Calculate initial descriptor index for isochronous transfer
  41791. + * based on scheduled frame.
  41792. + */
  41793. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41794. +{
  41795. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  41796. + uint8_t skip_frames = 0;
  41797. + /*
  41798. + * With current ISOC processing algorithm the channel is being
  41799. + * released when no more QTDs in the list(qh->ntd == 0).
  41800. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  41801. + *
  41802. + * So qh->channel != NULL branch is not used and just not removed from the
  41803. + * source file. It is required for another possible approach which is,
  41804. + * do not disable and release the channel when ISOC session completed,
  41805. + * just move QH to inactive schedule until new QTD arrives.
  41806. + * On new QTD, the QH moved back to 'ready' schedule,
  41807. + * starting frame and therefore starting desc_index are recalculated.
  41808. + * In this case channel is released only on ep_disable.
  41809. + */
  41810. +
  41811. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  41812. + if (qh->channel) {
  41813. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  41814. + /*
  41815. + * Calculate initial descriptor index based on FrameList current bitmap
  41816. + * and servicing period.
  41817. + */
  41818. + fr_idx_tmp = frame_list_idx(frame);
  41819. + fr_idx =
  41820. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  41821. + fr_idx_tmp)
  41822. + % frame_incr_val(qh);
  41823. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  41824. + } else {
  41825. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  41826. + fr_idx = frame_list_idx(qh->sched_frame);
  41827. + }
  41828. +
  41829. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  41830. +
  41831. + return skip_frames;
  41832. +}
  41833. +
  41834. +#define ISOC_URB_GIVEBACK_ASAP
  41835. +
  41836. +#define MAX_ISOC_XFER_SIZE_FS 1023
  41837. +#define MAX_ISOC_XFER_SIZE_HS 3072
  41838. +#define DESCNUM_THRESHOLD 4
  41839. +
  41840. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41841. + uint8_t skip_frames)
  41842. +{
  41843. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  41844. + dwc_otg_qtd_t *qtd;
  41845. + dwc_otg_host_dma_desc_t *dma_desc;
  41846. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  41847. +
  41848. + idx = qh->td_last;
  41849. + inc = qh->interval;
  41850. + n_desc = 0;
  41851. +
  41852. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  41853. + if (skip_frames && !qh->channel)
  41854. + ntd_max = ntd_max - skip_frames / qh->interval;
  41855. +
  41856. + max_xfer_size =
  41857. + (qh->dev_speed ==
  41858. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  41859. + MAX_ISOC_XFER_SIZE_FS;
  41860. +
  41861. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  41862. + while ((qh->ntd < ntd_max)
  41863. + && (qtd->isoc_frame_index_last <
  41864. + qtd->urb->packet_count)) {
  41865. +
  41866. + dma_desc = &qh->desc_list[idx];
  41867. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  41868. +
  41869. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  41870. +
  41871. + if (frame_desc->length > max_xfer_size)
  41872. + qh->n_bytes[idx] = max_xfer_size;
  41873. + else
  41874. + qh->n_bytes[idx] = frame_desc->length;
  41875. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  41876. + dma_desc->status.b_isoc.a = 1;
  41877. + dma_desc->status.b_isoc.sts = 0;
  41878. +
  41879. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  41880. +
  41881. + qh->ntd++;
  41882. +
  41883. + qtd->isoc_frame_index_last++;
  41884. +
  41885. +#ifdef ISOC_URB_GIVEBACK_ASAP
  41886. + /*
  41887. + * Set IOC for each descriptor corresponding to the
  41888. + * last frame of the URB.
  41889. + */
  41890. + if (qtd->isoc_frame_index_last ==
  41891. + qtd->urb->packet_count)
  41892. + dma_desc->status.b_isoc.ioc = 1;
  41893. +
  41894. +#endif
  41895. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  41896. + n_desc++;
  41897. +
  41898. + }
  41899. + qtd->in_process = 1;
  41900. + }
  41901. +
  41902. + qh->td_last = idx;
  41903. +
  41904. +#ifdef ISOC_URB_GIVEBACK_ASAP
  41905. + /* Set IOC for the last descriptor if descriptor list is full */
  41906. + if (qh->ntd == ntd_max) {
  41907. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  41908. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  41909. + }
  41910. +#else
  41911. + /*
  41912. + * Set IOC bit only for one descriptor.
  41913. + * Always try to be ahead of HW processing,
  41914. + * i.e. on IOC generation driver activates next descriptors but
  41915. + * core continues to process descriptors followed the one with IOC set.
  41916. + */
  41917. +
  41918. + if (n_desc > DESCNUM_THRESHOLD) {
  41919. + /*
  41920. + * Move IOC "up". Required even if there is only one QTD
  41921. + * in the list, cause QTDs migth continue to be queued,
  41922. + * but during the activation it was only one queued.
  41923. + * Actually more than one QTD might be in the list if this function called
  41924. + * from XferCompletion - QTDs was queued during HW processing of the previous
  41925. + * descriptor chunk.
  41926. + */
  41927. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  41928. + } else {
  41929. + /*
  41930. + * Set the IOC for the latest descriptor
  41931. + * if either number of descriptor is not greather than threshold
  41932. + * or no more new descriptors activated.
  41933. + */
  41934. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  41935. + }
  41936. +
  41937. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  41938. +#endif
  41939. +}
  41940. +
  41941. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41942. +{
  41943. +
  41944. + dwc_hc_t *hc;
  41945. + dwc_otg_host_dma_desc_t *dma_desc;
  41946. + dwc_otg_qtd_t *qtd;
  41947. + int num_packets, len, n_desc = 0;
  41948. +
  41949. + hc = qh->channel;
  41950. +
  41951. + /*
  41952. + * Start with hc->xfer_buff initialized in
  41953. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  41954. + * this pointer re-assigned to the buffer of the currently processed QTD.
  41955. + * For non-SG request there is always one QTD active.
  41956. + */
  41957. +
  41958. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  41959. +
  41960. + if (n_desc) {
  41961. + /* SG request - more than 1 QTDs */
  41962. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  41963. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  41964. + }
  41965. +
  41966. + qtd->n_desc = 0;
  41967. +
  41968. + do {
  41969. + dma_desc = &qh->desc_list[n_desc];
  41970. + len = hc->xfer_len;
  41971. +
  41972. + if (len > MAX_DMA_DESC_SIZE)
  41973. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  41974. +
  41975. + if (hc->ep_is_in) {
  41976. + if (len > 0) {
  41977. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  41978. + } else {
  41979. + /* Need 1 packet for transfer length of 0. */
  41980. + num_packets = 1;
  41981. + }
  41982. + /* Always program an integral # of max packets for IN transfers. */
  41983. + len = num_packets * hc->max_packet;
  41984. + }
  41985. +
  41986. + dma_desc->status.b.n_bytes = len;
  41987. +
  41988. + qh->n_bytes[n_desc] = len;
  41989. +
  41990. + if ((qh->ep_type == UE_CONTROL)
  41991. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  41992. + dma_desc->status.b.sup = 1; /* Setup Packet */
  41993. +
  41994. + dma_desc->status.b.a = 1; /* Active descriptor */
  41995. + dma_desc->status.b.sts = 0;
  41996. +
  41997. + dma_desc->buf =
  41998. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  41999. +
  42000. + /*
  42001. + * Last descriptor(or single) of IN transfer
  42002. + * with actual size less than MaxPacket.
  42003. + */
  42004. + if (len > hc->xfer_len) {
  42005. + hc->xfer_len = 0;
  42006. + } else {
  42007. + hc->xfer_buff += len;
  42008. + hc->xfer_len -= len;
  42009. + }
  42010. +
  42011. + qtd->n_desc++;
  42012. + n_desc++;
  42013. + }
  42014. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  42015. +
  42016. +
  42017. + qtd->in_process = 1;
  42018. +
  42019. + if (qh->ep_type == UE_CONTROL)
  42020. + break;
  42021. +
  42022. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  42023. + break;
  42024. + }
  42025. +
  42026. + if (n_desc) {
  42027. + /* Request Transfer Complete interrupt for the last descriptor */
  42028. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  42029. + /* End of List indicator */
  42030. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  42031. +
  42032. + hc->ntd = n_desc;
  42033. + }
  42034. +}
  42035. +
  42036. +/**
  42037. + * For Control and Bulk endpoints initializes descriptor list
  42038. + * and starts the transfer.
  42039. + *
  42040. + * For Interrupt and Isochronous endpoints initializes descriptor list
  42041. + * then updates FrameList, marking appropriate entries as active.
  42042. + * In case of Isochronous, the starting descriptor index is calculated based
  42043. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  42044. + * Then starts the transfer via enabling the channel.
  42045. + * For Isochronous endpoint the channel is not halted on XferComplete
  42046. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  42047. + *
  42048. + * @param hcd The HCD state structure for the DWC OTG controller.
  42049. + * @param qh The QH to init.
  42050. + *
  42051. + * @return 0 if successful, negative error code otherwise.
  42052. + */
  42053. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  42054. +{
  42055. + /* Channel is already assigned */
  42056. + dwc_hc_t *hc = qh->channel;
  42057. + uint8_t skip_frames = 0;
  42058. +
  42059. + switch (hc->ep_type) {
  42060. + case DWC_OTG_EP_TYPE_CONTROL:
  42061. + case DWC_OTG_EP_TYPE_BULK:
  42062. + init_non_isoc_dma_desc(hcd, qh);
  42063. +
  42064. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  42065. + break;
  42066. + case DWC_OTG_EP_TYPE_INTR:
  42067. + init_non_isoc_dma_desc(hcd, qh);
  42068. +
  42069. + update_frame_list(hcd, qh, 1);
  42070. +
  42071. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  42072. + break;
  42073. + case DWC_OTG_EP_TYPE_ISOC:
  42074. +
  42075. + if (!qh->ntd)
  42076. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  42077. +
  42078. + init_isoc_dma_desc(hcd, qh, skip_frames);
  42079. +
  42080. + if (!hc->xfer_started) {
  42081. +
  42082. + update_frame_list(hcd, qh, 1);
  42083. +
  42084. + /*
  42085. + * Always set to max, instead of actual size.
  42086. + * Otherwise ntd will be changed with
  42087. + * channel being enabled. Not recommended.
  42088. + *
  42089. + */
  42090. + hc->ntd = max_desc_num(qh);
  42091. + /* Enable channel only once for ISOC */
  42092. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  42093. + }
  42094. +
  42095. + break;
  42096. + default:
  42097. +
  42098. + break;
  42099. + }
  42100. +}
  42101. +
  42102. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  42103. + dwc_hc_t * hc,
  42104. + dwc_otg_hc_regs_t * hc_regs,
  42105. + dwc_otg_halt_status_e halt_status)
  42106. +{
  42107. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  42108. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  42109. + dwc_otg_qh_t *qh;
  42110. + dwc_otg_host_dma_desc_t *dma_desc;
  42111. + uint16_t idx, remain;
  42112. + uint8_t urb_compl;
  42113. +
  42114. + qh = hc->qh;
  42115. + idx = qh->td_first;
  42116. +
  42117. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  42118. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  42119. + qtd->in_process = 0;
  42120. + return;
  42121. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  42122. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  42123. + /*
  42124. + * Channel is halted in these error cases.
  42125. + * Considered as serious issues.
  42126. + * Complete all URBs marking all frames as failed,
  42127. + * irrespective whether some of the descriptors(frames) succeeded or no.
  42128. + * Pass error code to completion routine as well, to
  42129. + * update urb->status, some of class drivers might use it to stop
  42130. + * queing transfer requests.
  42131. + */
  42132. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  42133. + ? (-DWC_E_IO)
  42134. + : (-DWC_E_OVERFLOW);
  42135. +
  42136. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  42137. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  42138. + frame_desc = &qtd->urb->iso_descs[idx];
  42139. + frame_desc->status = err;
  42140. + }
  42141. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  42142. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  42143. + }
  42144. + return;
  42145. + }
  42146. +
  42147. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  42148. +
  42149. + if (!qtd->in_process)
  42150. + break;
  42151. +
  42152. + urb_compl = 0;
  42153. +
  42154. + do {
  42155. +
  42156. + dma_desc = &qh->desc_list[idx];
  42157. +
  42158. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  42159. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  42160. +
  42161. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  42162. + /*
  42163. + * XactError or, unable to complete all the transactions
  42164. + * in the scheduled micro-frame/frame,
  42165. + * both indicated by DMA_DESC_STS_PKTERR.
  42166. + */
  42167. + qtd->urb->error_count++;
  42168. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  42169. + frame_desc->status = -DWC_E_PROTOCOL;
  42170. + } else {
  42171. + /* Success */
  42172. +
  42173. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  42174. + frame_desc->status = 0;
  42175. + }
  42176. +
  42177. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  42178. + /*
  42179. + * urb->status is not used for isoc transfers here.
  42180. + * The individual frame_desc status are used instead.
  42181. + */
  42182. +
  42183. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  42184. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  42185. +
  42186. + /*
  42187. + * This check is necessary because urb_dequeue can be called
  42188. + * from urb complete callback(sound driver example).
  42189. + * All pending URBs are dequeued there, so no need for
  42190. + * further processing.
  42191. + */
  42192. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  42193. + return;
  42194. + }
  42195. +
  42196. + urb_compl = 1;
  42197. +
  42198. + }
  42199. +
  42200. + qh->ntd--;
  42201. +
  42202. + /* Stop if IOC requested descriptor reached */
  42203. + if (dma_desc->status.b_isoc.ioc) {
  42204. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  42205. + goto stop_scan;
  42206. + }
  42207. +
  42208. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  42209. +
  42210. + if (urb_compl)
  42211. + break;
  42212. + }
  42213. + while (idx != qh->td_first);
  42214. + }
  42215. +stop_scan:
  42216. + qh->td_first = idx;
  42217. +}
  42218. +
  42219. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  42220. + dwc_hc_t * hc,
  42221. + dwc_otg_qtd_t * qtd,
  42222. + dwc_otg_host_dma_desc_t * dma_desc,
  42223. + dwc_otg_halt_status_e halt_status,
  42224. + uint32_t n_bytes, uint8_t * xfer_done)
  42225. +{
  42226. +
  42227. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  42228. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  42229. +
  42230. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  42231. + urb->status = -DWC_E_IO;
  42232. + return 1;
  42233. + }
  42234. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  42235. + switch (halt_status) {
  42236. + case DWC_OTG_HC_XFER_STALL:
  42237. + urb->status = -DWC_E_PIPE;
  42238. + break;
  42239. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  42240. + urb->status = -DWC_E_OVERFLOW;
  42241. + break;
  42242. + case DWC_OTG_HC_XFER_XACT_ERR:
  42243. + urb->status = -DWC_E_PROTOCOL;
  42244. + break;
  42245. + default:
  42246. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  42247. + halt_status);
  42248. + break;
  42249. + }
  42250. + return 1;
  42251. + }
  42252. +
  42253. + if (dma_desc->status.b.a == 1) {
  42254. + DWC_DEBUGPL(DBG_HCDV,
  42255. + "Active descriptor encountered on channel %d\n",
  42256. + hc->hc_num);
  42257. + return 0;
  42258. + }
  42259. +
  42260. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  42261. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  42262. + urb->actual_length += n_bytes - remain;
  42263. + if (remain || urb->actual_length == urb->length) {
  42264. + /*
  42265. + * For Control Data stage do not set urb->status=0 to prevent
  42266. + * URB callback. Set it when Status phase done. See below.
  42267. + */
  42268. + *xfer_done = 1;
  42269. + }
  42270. +
  42271. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  42272. + urb->status = 0;
  42273. + *xfer_done = 1;
  42274. + }
  42275. + /* No handling for SETUP stage */
  42276. + } else {
  42277. + /* BULK and INTR */
  42278. + urb->actual_length += n_bytes - remain;
  42279. + if (remain || urb->actual_length == urb->length) {
  42280. + urb->status = 0;
  42281. + *xfer_done = 1;
  42282. + }
  42283. + }
  42284. +
  42285. + return 0;
  42286. +}
  42287. +
  42288. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  42289. + dwc_hc_t * hc,
  42290. + dwc_otg_hc_regs_t * hc_regs,
  42291. + dwc_otg_halt_status_e halt_status)
  42292. +{
  42293. + dwc_otg_hcd_urb_t *urb = NULL;
  42294. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  42295. + dwc_otg_qh_t *qh;
  42296. + dwc_otg_host_dma_desc_t *dma_desc;
  42297. + uint32_t n_bytes, n_desc, i;
  42298. + uint8_t failed = 0, xfer_done;
  42299. +
  42300. + n_desc = 0;
  42301. +
  42302. + qh = hc->qh;
  42303. +
  42304. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  42305. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  42306. + qtd->in_process = 0;
  42307. + }
  42308. + return;
  42309. + }
  42310. +
  42311. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  42312. +
  42313. + urb = qtd->urb;
  42314. +
  42315. + n_bytes = 0;
  42316. + xfer_done = 0;
  42317. +
  42318. + for (i = 0; i < qtd->n_desc; i++) {
  42319. + dma_desc = &qh->desc_list[n_desc];
  42320. +
  42321. + n_bytes = qh->n_bytes[n_desc];
  42322. +
  42323. + failed =
  42324. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  42325. + dma_desc,
  42326. + halt_status, n_bytes,
  42327. + &xfer_done);
  42328. +
  42329. + if (failed
  42330. + || (xfer_done
  42331. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  42332. +
  42333. + hcd->fops->complete(hcd, urb->priv, urb,
  42334. + urb->status);
  42335. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  42336. +
  42337. + if (failed)
  42338. + goto stop_scan;
  42339. + } else if (qh->ep_type == UE_CONTROL) {
  42340. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  42341. + if (urb->length > 0) {
  42342. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  42343. + } else {
  42344. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  42345. + }
  42346. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  42347. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  42348. + if (xfer_done) {
  42349. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  42350. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  42351. + } else if (i + 1 == qtd->n_desc) {
  42352. + /*
  42353. + * Last descriptor for Control data stage which is
  42354. + * not completed yet.
  42355. + */
  42356. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  42357. + }
  42358. + }
  42359. + }
  42360. +
  42361. + n_desc++;
  42362. + }
  42363. +
  42364. + }
  42365. +
  42366. +stop_scan:
  42367. +
  42368. + if (qh->ep_type != UE_CONTROL) {
  42369. + /*
  42370. + * Resetting the data toggle for bulk
  42371. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  42372. + */
  42373. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  42374. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  42375. + else
  42376. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  42377. + }
  42378. +
  42379. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  42380. + hcint_data_t hcint;
  42381. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  42382. + if (hcint.b.nyet) {
  42383. + /*
  42384. + * Got a NYET on the last transaction of the transfer. It
  42385. + * means that the endpoint should be in the PING state at the
  42386. + * beginning of the next transfer.
  42387. + */
  42388. + qh->ping_state = 1;
  42389. + clear_hc_int(hc_regs, nyet);
  42390. + }
  42391. +
  42392. + }
  42393. +
  42394. +}
  42395. +
  42396. +/**
  42397. + * This function is called from interrupt handlers.
  42398. + * Scans the descriptor list, updates URB's status and
  42399. + * calls completion routine for the URB if it's done.
  42400. + * Releases the channel to be used by other transfers.
  42401. + * In case of Isochronous endpoint the channel is not halted until
  42402. + * the end of the session, i.e. QTD list is empty.
  42403. + * If periodic channel released the FrameList is updated accordingly.
  42404. + *
  42405. + * Calls transaction selection routines to activate pending transfers.
  42406. + *
  42407. + * @param hcd The HCD state structure for the DWC OTG controller.
  42408. + * @param hc Host channel, the transfer is completed on.
  42409. + * @param hc_regs Host channel registers.
  42410. + * @param halt_status Reason the channel is being halted,
  42411. + * or just XferComplete for isochronous transfer
  42412. + */
  42413. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  42414. + dwc_hc_t * hc,
  42415. + dwc_otg_hc_regs_t * hc_regs,
  42416. + dwc_otg_halt_status_e halt_status)
  42417. +{
  42418. + uint8_t continue_isoc_xfer = 0;
  42419. + dwc_otg_transaction_type_e tr_type;
  42420. + dwc_otg_qh_t *qh = hc->qh;
  42421. +
  42422. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  42423. +
  42424. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  42425. +
  42426. + /* Release the channel if halted or session completed */
  42427. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  42428. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  42429. +
  42430. + /* Halt the channel if session completed */
  42431. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  42432. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  42433. + }
  42434. +
  42435. + release_channel_ddma(hcd, qh);
  42436. + dwc_otg_hcd_qh_remove(hcd, qh);
  42437. + } else {
  42438. + /* Keep in assigned schedule to continue transfer */
  42439. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  42440. + &qh->qh_list_entry);
  42441. + continue_isoc_xfer = 1;
  42442. +
  42443. + }
  42444. + /** @todo Consider the case when period exceeds FrameList size.
  42445. + * Frame Rollover interrupt should be used.
  42446. + */
  42447. + } else {
  42448. + /* Scan descriptor list to complete the URB(s), then release the channel */
  42449. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  42450. +
  42451. + release_channel_ddma(hcd, qh);
  42452. + dwc_otg_hcd_qh_remove(hcd, qh);
  42453. +
  42454. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  42455. + /* Add back to inactive non-periodic schedule on normal completion */
  42456. + dwc_otg_hcd_qh_add(hcd, qh);
  42457. + }
  42458. +
  42459. + }
  42460. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  42461. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  42462. + if (continue_isoc_xfer) {
  42463. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  42464. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  42465. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  42466. + tr_type = DWC_OTG_TRANSACTION_ALL;
  42467. + }
  42468. + }
  42469. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  42470. + }
  42471. +}
  42472. +
  42473. +#endif /* DWC_DEVICE_ONLY */
  42474. --- /dev/null
  42475. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  42476. @@ -0,0 +1,417 @@
  42477. +/* ==========================================================================
  42478. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  42479. + * $Revision: #12 $
  42480. + * $Date: 2011/10/26 $
  42481. + * $Change: 1873028 $
  42482. + *
  42483. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  42484. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  42485. + * otherwise expressly agreed to in writing between Synopsys and you.
  42486. + *
  42487. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  42488. + * any End User Software License Agreement or Agreement for Licensed Product
  42489. + * with Synopsys or any supplement thereto. You are permitted to use and
  42490. + * redistribute this Software in source and binary forms, with or without
  42491. + * modification, provided that redistributions of source code must retain this
  42492. + * notice. You may not view, use, disclose, copy or distribute this file or
  42493. + * any information contained herein except pursuant to this license grant from
  42494. + * Synopsys. If you do not agree with this notice, including the disclaimer
  42495. + * below, then you are not authorized to use the Software.
  42496. + *
  42497. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  42498. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42499. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42500. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  42501. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  42502. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42503. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42504. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42505. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42506. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42507. + * DAMAGE.
  42508. + * ========================================================================== */
  42509. +#ifndef DWC_DEVICE_ONLY
  42510. +#ifndef __DWC_HCD_IF_H__
  42511. +#define __DWC_HCD_IF_H__
  42512. +
  42513. +#include "dwc_otg_core_if.h"
  42514. +
  42515. +/** @file
  42516. + * This file defines DWC_OTG HCD Core API.
  42517. + */
  42518. +
  42519. +struct dwc_otg_hcd;
  42520. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  42521. +
  42522. +struct dwc_otg_hcd_urb;
  42523. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  42524. +
  42525. +/** @name HCD Function Driver Callbacks */
  42526. +/** @{ */
  42527. +
  42528. +/** This function is called whenever core switches to host mode. */
  42529. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  42530. +
  42531. +/** This function is called when device has been disconnected */
  42532. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  42533. +
  42534. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  42535. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42536. + void *urb_handle,
  42537. + uint32_t * hub_addr,
  42538. + uint32_t * port_addr);
  42539. +/** Via this function HCD core gets device speed */
  42540. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42541. + void *urb_handle);
  42542. +
  42543. +/** This function is called when urb is completed */
  42544. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42545. + void *urb_handle,
  42546. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  42547. + int32_t status);
  42548. +
  42549. +/** Via this function HCD core gets b_hnp_enable parameter */
  42550. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  42551. +
  42552. +struct dwc_otg_hcd_function_ops {
  42553. + dwc_otg_hcd_start_cb_t start;
  42554. + dwc_otg_hcd_disconnect_cb_t disconnect;
  42555. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  42556. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  42557. + dwc_otg_hcd_complete_urb_cb_t complete;
  42558. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  42559. +};
  42560. +/** @} */
  42561. +
  42562. +/** @name HCD Core API */
  42563. +/** @{ */
  42564. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  42565. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  42566. +
  42567. +/** This function should be called to initiate HCD Core.
  42568. + *
  42569. + * @param hcd The HCD
  42570. + * @param core_if The DWC_OTG Core
  42571. + *
  42572. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  42573. + * Returns 0 on success
  42574. + */
  42575. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  42576. +
  42577. +/** Frees HCD
  42578. + *
  42579. + * @param hcd The HCD
  42580. + */
  42581. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  42582. +
  42583. +/** This function should be called on every hardware interrupt.
  42584. + *
  42585. + * @param dwc_otg_hcd The HCD
  42586. + *
  42587. + * Returns non zero if interrupt is handled
  42588. + * Return 0 if interrupt is not handled
  42589. + */
  42590. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  42591. +
  42592. +/** This function is used to handle the fast interrupt
  42593. + *
  42594. + */
  42595. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  42596. +
  42597. +/**
  42598. + * Returns private data set by
  42599. + * dwc_otg_hcd_set_priv_data function.
  42600. + *
  42601. + * @param hcd The HCD
  42602. + */
  42603. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  42604. +
  42605. +/**
  42606. + * Set private data.
  42607. + *
  42608. + * @param hcd The HCD
  42609. + * @param priv_data pointer to be stored in private data
  42610. + */
  42611. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  42612. +
  42613. +/**
  42614. + * This function initializes the HCD Core.
  42615. + *
  42616. + * @param hcd The HCD
  42617. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  42618. + *
  42619. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  42620. + * Returns 0 on success
  42621. + */
  42622. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  42623. + struct dwc_otg_hcd_function_ops *fops);
  42624. +
  42625. +/**
  42626. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  42627. + * stopped.
  42628. + *
  42629. + * @param hcd The HCD
  42630. + */
  42631. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  42632. +
  42633. +/**
  42634. + * Handles hub class-specific requests.
  42635. + *
  42636. + * @param dwc_otg_hcd The HCD
  42637. + * @param typeReq Request Type
  42638. + * @param wValue wValue from control request
  42639. + * @param wIndex wIndex from control request
  42640. + * @param buf data buffer
  42641. + * @param wLength data buffer length
  42642. + *
  42643. + * Returns -DWC_E_INVALID if invalid argument is passed
  42644. + * Returns 0 on success
  42645. + */
  42646. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  42647. + uint16_t typeReq, uint16_t wValue,
  42648. + uint16_t wIndex, uint8_t * buf,
  42649. + uint16_t wLength);
  42650. +
  42651. +/**
  42652. + * Returns otg port number.
  42653. + *
  42654. + * @param hcd The HCD
  42655. + */
  42656. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  42657. +
  42658. +/**
  42659. + * Returns OTG version - either 1.3 or 2.0.
  42660. + *
  42661. + * @param core_if The core_if structure pointer
  42662. + */
  42663. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  42664. +
  42665. +/**
  42666. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  42667. + *
  42668. + * @param hcd The HCD
  42669. + */
  42670. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  42671. +
  42672. +/**
  42673. + * Returns current frame number.
  42674. + *
  42675. + * @param hcd The HCD
  42676. + */
  42677. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  42678. +
  42679. +/**
  42680. + * Dumps hcd state.
  42681. + *
  42682. + * @param hcd The HCD
  42683. + */
  42684. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  42685. +
  42686. +/**
  42687. + * Dump the average frame remaining at SOF. This can be used to
  42688. + * determine average interrupt latency. Frame remaining is also shown for
  42689. + * start transfer and two additional sample points.
  42690. + * Currently this function is not implemented.
  42691. + *
  42692. + * @param hcd The HCD
  42693. + */
  42694. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  42695. +
  42696. +/**
  42697. + * Sends LPM transaction to the local device.
  42698. + *
  42699. + * @param hcd The HCD
  42700. + * @param devaddr Device Address
  42701. + * @param hird Host initiated resume duration
  42702. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  42703. + *
  42704. + * Returns negative value if sending LPM transaction was not succeeded.
  42705. + * Returns 0 on success.
  42706. + */
  42707. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  42708. + uint8_t hird, uint8_t bRemoteWake);
  42709. +
  42710. +/* URB interface */
  42711. +
  42712. +/**
  42713. + * Allocates memory for dwc_otg_hcd_urb structure.
  42714. + * Allocated memory should be freed by call of DWC_FREE.
  42715. + *
  42716. + * @param hcd The HCD
  42717. + * @param iso_desc_count Count of ISOC descriptors
  42718. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  42719. + */
  42720. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  42721. + int iso_desc_count,
  42722. + int atomic_alloc);
  42723. +
  42724. +/**
  42725. + * Set pipe information in URB.
  42726. + *
  42727. + * @param hcd_urb DWC_OTG URB
  42728. + * @param devaddr Device Address
  42729. + * @param ep_num Endpoint Number
  42730. + * @param ep_type Endpoint Type
  42731. + * @param ep_dir Endpoint Direction
  42732. + * @param mps Max Packet Size
  42733. + */
  42734. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  42735. + uint8_t devaddr, uint8_t ep_num,
  42736. + uint8_t ep_type, uint8_t ep_dir,
  42737. + uint16_t mps);
  42738. +
  42739. +/* Transfer flags */
  42740. +#define URB_GIVEBACK_ASAP 0x1
  42741. +#define URB_SEND_ZERO_PACKET 0x2
  42742. +
  42743. +/**
  42744. + * Sets dwc_otg_hcd_urb parameters.
  42745. + *
  42746. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  42747. + * @param urb_handle Unique handle for request, this will be passed back
  42748. + * to function driver in completion callback.
  42749. + * @param buf The buffer for the data
  42750. + * @param dma The DMA buffer for the data
  42751. + * @param buflen Transfer length
  42752. + * @param sp Buffer for setup data
  42753. + * @param sp_dma DMA address of setup data buffer
  42754. + * @param flags Transfer flags
  42755. + * @param interval Polling interval for interrupt or isochronous transfers.
  42756. + */
  42757. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  42758. + void *urb_handle, void *buf,
  42759. + dwc_dma_t dma, uint32_t buflen, void *sp,
  42760. + dwc_dma_t sp_dma, uint32_t flags,
  42761. + uint16_t interval);
  42762. +
  42763. +/** Gets status from dwc_otg_hcd_urb
  42764. + *
  42765. + * @param dwc_otg_urb DWC_OTG URB
  42766. + */
  42767. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  42768. +
  42769. +/** Gets actual length from dwc_otg_hcd_urb
  42770. + *
  42771. + * @param dwc_otg_urb DWC_OTG URB
  42772. + */
  42773. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  42774. + dwc_otg_urb);
  42775. +
  42776. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  42777. + *
  42778. + * @param dwc_otg_urb DWC_OTG URB
  42779. + */
  42780. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  42781. + dwc_otg_urb);
  42782. +
  42783. +/** Set ISOC descriptor offset and length
  42784. + *
  42785. + * @param dwc_otg_urb DWC_OTG URB
  42786. + * @param desc_num ISOC descriptor number
  42787. + * @param offset Offset from beginig of buffer.
  42788. + * @param length Transaction length
  42789. + */
  42790. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  42791. + int desc_num, uint32_t offset,
  42792. + uint32_t length);
  42793. +
  42794. +/** Get status of ISOC descriptor, specified by desc_num
  42795. + *
  42796. + * @param dwc_otg_urb DWC_OTG URB
  42797. + * @param desc_num ISOC descriptor number
  42798. + */
  42799. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  42800. + dwc_otg_urb, int desc_num);
  42801. +
  42802. +/** Get actual length of ISOC descriptor, specified by desc_num
  42803. + *
  42804. + * @param dwc_otg_urb DWC_OTG URB
  42805. + * @param desc_num ISOC descriptor number
  42806. + */
  42807. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  42808. + dwc_otg_urb,
  42809. + int desc_num);
  42810. +
  42811. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  42812. + *
  42813. + * @param dwc_otg_hcd The HCD
  42814. + * @param dwc_otg_urb DWC_OTG URB
  42815. + * @param ep_handle Out parameter for returning endpoint handle
  42816. + * @param atomic_alloc Flag to do atomic allocation if needed
  42817. + *
  42818. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  42819. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  42820. + * Returns 0 on success.
  42821. + */
  42822. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  42823. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  42824. + void **ep_handle, int atomic_alloc);
  42825. +
  42826. +/** De-queue the specified URB
  42827. + *
  42828. + * @param dwc_otg_hcd The HCD
  42829. + * @param dwc_otg_urb DWC_OTG URB
  42830. + */
  42831. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  42832. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  42833. +
  42834. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  42835. + * Any URBs for the endpoint must already be dequeued.
  42836. + *
  42837. + * @param hcd The HCD
  42838. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  42839. + * @param retry Number of retries if there are queued transfers.
  42840. + *
  42841. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  42842. + * Returns 0 on success
  42843. + */
  42844. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  42845. + int retry);
  42846. +
  42847. +/* Resets the data toggle in qh structure. This function can be called from
  42848. + * usb_clear_halt routine.
  42849. + *
  42850. + * @param hcd The HCD
  42851. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  42852. + *
  42853. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  42854. + * Returns 0 on success
  42855. + */
  42856. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  42857. +
  42858. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  42859. + *
  42860. + * @param hcd The HCD
  42861. + * @param port Port number
  42862. + */
  42863. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  42864. +
  42865. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  42866. + * Only for ISOC and INTERRUPT endpoints.
  42867. + *
  42868. + * @param hcd The HCD
  42869. + * @param ep_handle Endpoint handle
  42870. + */
  42871. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  42872. + void *ep_handle);
  42873. +
  42874. +/** Call this function to check if bandwidth was freed for specified endpoint.
  42875. + *
  42876. + * @param hcd The HCD
  42877. + * @param ep_handle Endpoint handle
  42878. + */
  42879. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  42880. +
  42881. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  42882. + * Only for ISOC and INTERRUPT endpoints.
  42883. + *
  42884. + * @param hcd The HCD
  42885. + * @param ep_handle Endpoint handle
  42886. + */
  42887. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  42888. + void *ep_handle);
  42889. +
  42890. +/** @} */
  42891. +
  42892. +#endif /* __DWC_HCD_IF_H__ */
  42893. +#endif /* DWC_DEVICE_ONLY */
  42894. --- /dev/null
  42895. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  42896. @@ -0,0 +1,2752 @@
  42897. +/* ==========================================================================
  42898. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  42899. + * $Revision: #89 $
  42900. + * $Date: 2011/10/20 $
  42901. + * $Change: 1869487 $
  42902. + *
  42903. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  42904. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  42905. + * otherwise expressly agreed to in writing between Synopsys and you.
  42906. + *
  42907. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  42908. + * any End User Software License Agreement or Agreement for Licensed Product
  42909. + * with Synopsys or any supplement thereto. You are permitted to use and
  42910. + * redistribute this Software in source and binary forms, with or without
  42911. + * modification, provided that redistributions of source code must retain this
  42912. + * notice. You may not view, use, disclose, copy or distribute this file or
  42913. + * any information contained herein except pursuant to this license grant from
  42914. + * Synopsys. If you do not agree with this notice, including the disclaimer
  42915. + * below, then you are not authorized to use the Software.
  42916. + *
  42917. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  42918. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42919. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42920. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  42921. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  42922. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42923. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42924. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42925. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42926. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42927. + * DAMAGE.
  42928. + * ========================================================================== */
  42929. +#ifndef DWC_DEVICE_ONLY
  42930. +
  42931. +#include "dwc_otg_hcd.h"
  42932. +#include "dwc_otg_regs.h"
  42933. +
  42934. +#include <linux/jiffies.h>
  42935. +#include <asm/fiq.h>
  42936. +
  42937. +
  42938. +extern bool microframe_schedule;
  42939. +
  42940. +/** @file
  42941. + * This file contains the implementation of the HCD Interrupt handlers.
  42942. + */
  42943. +
  42944. +int fiq_done, int_done;
  42945. +
  42946. +#ifdef FIQ_DEBUG
  42947. +char buffer[1000*16];
  42948. +int wptr;
  42949. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  42950. +{
  42951. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  42952. + va_list args;
  42953. + char text[17];
  42954. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  42955. +
  42956. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  42957. + {
  42958. + local_fiq_disable();
  42959. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  42960. + va_start(args, fmt);
  42961. + vsnprintf(text+8, 9, fmt, args);
  42962. + va_end(args);
  42963. +
  42964. + memcpy(buffer + wptr, text, 16);
  42965. + wptr = (wptr + 16) % sizeof(buffer);
  42966. + local_fiq_enable();
  42967. + }
  42968. +}
  42969. +#endif
  42970. +
  42971. +/** This function handles interrupts for the HCD. */
  42972. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  42973. +{
  42974. + int retval = 0;
  42975. + static int last_time;
  42976. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  42977. + gintsts_data_t gintsts;
  42978. + gintmsk_data_t gintmsk;
  42979. + hfnum_data_t hfnum;
  42980. + haintmsk_data_t haintmsk;
  42981. +
  42982. +#ifdef DEBUG
  42983. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  42984. +
  42985. +#endif
  42986. +
  42987. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  42988. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  42989. +
  42990. + /* Exit from ISR if core is hibernated */
  42991. + if (core_if->hibernation_suspend == 1) {
  42992. + goto exit_handler_routine;
  42993. + }
  42994. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  42995. + /* Check if HOST Mode */
  42996. + if (dwc_otg_is_host_mode(core_if)) {
  42997. + if (fiq_enable) {
  42998. + local_fiq_disable();
  42999. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  43000. + /* Pull in from the FIQ's disabled mask */
  43001. + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
  43002. + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
  43003. + }
  43004. +
  43005. + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
  43006. + gintsts.b.hcintr = 1;
  43007. + }
  43008. +
  43009. + /* Danger will robinson: fake a SOF if necessary */
  43010. + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
  43011. + gintsts.b.sofintr = 1;
  43012. + }
  43013. + gintsts.d32 &= gintmsk.d32;
  43014. +
  43015. + if (fiq_enable) {
  43016. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  43017. + local_fiq_enable();
  43018. + }
  43019. +
  43020. + if (!gintsts.d32) {
  43021. + goto exit_handler_routine;
  43022. + }
  43023. +
  43024. +#ifdef DEBUG
  43025. + // We should be OK doing this because the common interrupts should already have been serviced
  43026. + /* Don't print debug message in the interrupt handler on SOF */
  43027. +#ifndef DEBUG_SOF
  43028. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  43029. +#endif
  43030. + DWC_DEBUGPL(DBG_HCDI, "\n");
  43031. +#endif
  43032. +
  43033. +#ifdef DEBUG
  43034. +#ifndef DEBUG_SOF
  43035. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  43036. +#endif
  43037. + DWC_DEBUGPL(DBG_HCDI,
  43038. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  43039. + gintsts.d32, core_if);
  43040. +#endif
  43041. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  43042. + if (gintsts.b.sofintr) {
  43043. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  43044. + }
  43045. +
  43046. + if (gintsts.b.rxstsqlvl) {
  43047. + retval |=
  43048. + dwc_otg_hcd_handle_rx_status_q_level_intr
  43049. + (dwc_otg_hcd);
  43050. + }
  43051. + if (gintsts.b.nptxfempty) {
  43052. + retval |=
  43053. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  43054. + (dwc_otg_hcd);
  43055. + }
  43056. + if (gintsts.b.i2cintr) {
  43057. + /** @todo Implement i2cintr handler. */
  43058. + }
  43059. + if (gintsts.b.portintr) {
  43060. +
  43061. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  43062. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  43063. + if (fiq_enable) {
  43064. + local_fiq_disable();
  43065. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  43066. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  43067. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  43068. + local_fiq_enable();
  43069. + } else {
  43070. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  43071. + }
  43072. + }
  43073. + if (gintsts.b.hcintr) {
  43074. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  43075. + }
  43076. + if (gintsts.b.ptxfempty) {
  43077. + retval |=
  43078. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  43079. + (dwc_otg_hcd);
  43080. + }
  43081. +#ifdef DEBUG
  43082. +#ifndef DEBUG_SOF
  43083. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  43084. +#endif
  43085. + {
  43086. + DWC_DEBUGPL(DBG_HCDI,
  43087. + "DWC OTG HCD Finished Servicing Interrupts\n");
  43088. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  43089. + DWC_READ_REG32(&global_regs->gintsts));
  43090. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  43091. + DWC_READ_REG32(&global_regs->gintmsk));
  43092. + }
  43093. +#endif
  43094. +
  43095. +#ifdef DEBUG
  43096. +#ifndef DEBUG_SOF
  43097. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  43098. +#endif
  43099. + DWC_DEBUGPL(DBG_HCDI, "\n");
  43100. +#endif
  43101. +
  43102. + }
  43103. +
  43104. +exit_handler_routine:
  43105. + if (fiq_enable) {
  43106. + gintmsk_data_t gintmsk_new;
  43107. + haintmsk_data_t haintmsk_new;
  43108. + local_fiq_disable();
  43109. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  43110. + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
  43111. + if(fiq_fsm_enable)
  43112. + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
  43113. + else
  43114. + haintmsk_new.d32 = 0x0000FFFF;
  43115. +
  43116. + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
  43117. + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
  43118. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
  43119. + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
  43120. + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
  43121. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
  43122. + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
  43123. + ;
  43124. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
  43125. + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
  43126. + }
  43127. + int_done++;
  43128. + }
  43129. + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  43130. + /* Re-enable interrupts that the FIQ masked (first time round) */
  43131. + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
  43132. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  43133. + local_fiq_enable();
  43134. +
  43135. + if ((jiffies / HZ) > last_time) {
  43136. + //dwc_otg_qh_t *qh;
  43137. + //dwc_list_link_t *cur;
  43138. + /* Once a second output the fiq and irq numbers, useful for debug */
  43139. + last_time = jiffies / HZ;
  43140. + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
  43141. + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
  43142. + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
  43143. + //printk(KERN_WARNING "Periodic queues:\n");
  43144. + }
  43145. + }
  43146. +
  43147. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  43148. + return retval;
  43149. +}
  43150. +
  43151. +#ifdef DWC_TRACK_MISSED_SOFS
  43152. +
  43153. +#warning Compiling code to track missed SOFs
  43154. +#define FRAME_NUM_ARRAY_SIZE 1000
  43155. +/**
  43156. + * This function is for debug only.
  43157. + */
  43158. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  43159. +{
  43160. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  43161. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  43162. + static int frame_num_idx = 0;
  43163. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  43164. + static int dumped_frame_num_array = 0;
  43165. +
  43166. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  43167. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  43168. + curr_frame_number) {
  43169. + frame_num_array[frame_num_idx] = curr_frame_number;
  43170. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  43171. + }
  43172. + } else if (!dumped_frame_num_array) {
  43173. + int i;
  43174. + DWC_PRINTF("Frame Last Frame\n");
  43175. + DWC_PRINTF("----- ----------\n");
  43176. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  43177. + DWC_PRINTF("0x%04x 0x%04x\n",
  43178. + frame_num_array[i], last_frame_num_array[i]);
  43179. + }
  43180. + dumped_frame_num_array = 1;
  43181. + }
  43182. + last_frame_num = curr_frame_number;
  43183. +}
  43184. +#endif
  43185. +
  43186. +/**
  43187. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  43188. + * transactions may be queued to the DWC_otg controller for the current
  43189. + * (micro)frame. Periodic transactions may be queued to the controller for the
  43190. + * next (micro)frame.
  43191. + */
  43192. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  43193. +{
  43194. + hfnum_data_t hfnum;
  43195. + gintsts_data_t gintsts = { .d32 = 0 };
  43196. + dwc_list_link_t *qh_entry;
  43197. + dwc_otg_qh_t *qh;
  43198. + dwc_otg_transaction_type_e tr_type;
  43199. + int did_something = 0;
  43200. + int32_t next_sched_frame = -1;
  43201. +
  43202. + hfnum.d32 =
  43203. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  43204. +
  43205. +#ifdef DEBUG_SOF
  43206. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  43207. +#endif
  43208. + hcd->frame_number = hfnum.b.frnum;
  43209. +
  43210. +#ifdef DEBUG
  43211. + hcd->frrem_accum += hfnum.b.frrem;
  43212. + hcd->frrem_samples++;
  43213. +#endif
  43214. +
  43215. +#ifdef DWC_TRACK_MISSED_SOFS
  43216. + track_missed_sofs(hcd->frame_number);
  43217. +#endif
  43218. + /* Determine whether any periodic QHs should be executed. */
  43219. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  43220. + while (qh_entry != &hcd->periodic_sched_inactive) {
  43221. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  43222. + qh_entry = qh_entry->next;
  43223. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  43224. +
  43225. + /*
  43226. + * Move QH to the ready list to be executed next
  43227. + * (micro)frame.
  43228. + */
  43229. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  43230. + &qh->qh_list_entry);
  43231. +
  43232. + did_something = 1;
  43233. + }
  43234. + else
  43235. + {
  43236. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  43237. + {
  43238. + next_sched_frame = qh->sched_frame;
  43239. + }
  43240. + }
  43241. + }
  43242. + if (fiq_enable)
  43243. + hcd->fiq_state->next_sched_frame = next_sched_frame;
  43244. +
  43245. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  43246. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  43247. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  43248. + did_something = 1;
  43249. + }
  43250. +
  43251. + /* Clear interrupt - but do not trample on the FIQ sof */
  43252. + if (!fiq_fsm_enable) {
  43253. + gintsts.b.sofintr = 1;
  43254. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  43255. + }
  43256. + return 1;
  43257. +}
  43258. +
  43259. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  43260. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  43261. + * memory if the DWC_otg controller is operating in Slave mode. */
  43262. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43263. +{
  43264. + host_grxsts_data_t grxsts;
  43265. + dwc_hc_t *hc = NULL;
  43266. +
  43267. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  43268. +
  43269. + grxsts.d32 =
  43270. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  43271. +
  43272. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  43273. + if (!hc) {
  43274. + DWC_ERROR("Unable to get corresponding channel\n");
  43275. + return 0;
  43276. + }
  43277. +
  43278. + /* Packet Status */
  43279. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  43280. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  43281. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  43282. + hc->data_pid_start);
  43283. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  43284. +
  43285. + switch (grxsts.b.pktsts) {
  43286. + case DWC_GRXSTS_PKTSTS_IN:
  43287. + /* Read the data into the host buffer. */
  43288. + if (grxsts.b.bcnt > 0) {
  43289. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  43290. + hc->xfer_buff, grxsts.b.bcnt);
  43291. +
  43292. + /* Update the HC fields for the next packet received. */
  43293. + hc->xfer_count += grxsts.b.bcnt;
  43294. + hc->xfer_buff += grxsts.b.bcnt;
  43295. + }
  43296. +
  43297. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  43298. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  43299. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  43300. + /* Handled in interrupt, just ignore data */
  43301. + break;
  43302. + default:
  43303. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  43304. + grxsts.b.pktsts);
  43305. + break;
  43306. + }
  43307. +
  43308. + return 1;
  43309. +}
  43310. +
  43311. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  43312. + * data packets may be written to the FIFO for OUT transfers. More requests
  43313. + * may be written to the non-periodic request queue for IN transfers. This
  43314. + * interrupt is enabled only in Slave mode. */
  43315. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43316. +{
  43317. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  43318. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  43319. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  43320. + return 1;
  43321. +}
  43322. +
  43323. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  43324. + * packets may be written to the FIFO for OUT transfers. More requests may be
  43325. + * written to the periodic request queue for IN transfers. This interrupt is
  43326. + * enabled only in Slave mode. */
  43327. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43328. +{
  43329. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  43330. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  43331. + DWC_OTG_TRANSACTION_PERIODIC);
  43332. + return 1;
  43333. +}
  43334. +
  43335. +/** There are multiple conditions that can cause a port interrupt. This function
  43336. + * determines which interrupt conditions have occurred and handles them
  43337. + * appropriately. */
  43338. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43339. +{
  43340. + int retval = 0;
  43341. + hprt0_data_t hprt0;
  43342. + hprt0_data_t hprt0_modify;
  43343. +
  43344. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43345. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43346. +
  43347. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  43348. + * GINTSTS */
  43349. +
  43350. + hprt0_modify.b.prtena = 0;
  43351. + hprt0_modify.b.prtconndet = 0;
  43352. + hprt0_modify.b.prtenchng = 0;
  43353. + hprt0_modify.b.prtovrcurrchng = 0;
  43354. +
  43355. + /* Port Connect Detected
  43356. + * Set flag and clear if detected */
  43357. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  43358. + // Dont modify port status if we are in hibernation state
  43359. + hprt0_modify.b.prtconndet = 1;
  43360. + hprt0_modify.b.prtenchng = 1;
  43361. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  43362. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43363. + return retval;
  43364. + }
  43365. +
  43366. + if (hprt0.b.prtconndet) {
  43367. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  43368. + if (dwc_otg_hcd->core_if->adp_enable &&
  43369. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  43370. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  43371. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  43372. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  43373. + /* TODO - check if this is required, as
  43374. + * host initialization was already performed
  43375. + * after initial ADP probing
  43376. + */
  43377. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  43378. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  43379. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  43380. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  43381. + } else {
  43382. +
  43383. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  43384. + "Port Connect Detected--\n", hprt0.d32);
  43385. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  43386. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  43387. + hprt0_modify.b.prtconndet = 1;
  43388. +
  43389. + /* B-Device has connected, Delete the connection timer. */
  43390. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  43391. + }
  43392. + /* The Hub driver asserts a reset when it sees port connect
  43393. + * status change flag */
  43394. + retval |= 1;
  43395. + }
  43396. +
  43397. + /* Port Enable Changed
  43398. + * Clear if detected - Set internal flag if disabled */
  43399. + if (hprt0.b.prtenchng) {
  43400. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  43401. + "Port Enable Changed--\n", hprt0.d32);
  43402. + hprt0_modify.b.prtenchng = 1;
  43403. + if (hprt0.b.prtena == 1) {
  43404. + hfir_data_t hfir;
  43405. + int do_reset = 0;
  43406. + dwc_otg_core_params_t *params =
  43407. + dwc_otg_hcd->core_if->core_params;
  43408. + dwc_otg_core_global_regs_t *global_regs =
  43409. + dwc_otg_hcd->core_if->core_global_regs;
  43410. + dwc_otg_host_if_t *host_if =
  43411. + dwc_otg_hcd->core_if->host_if;
  43412. +
  43413. + dwc_otg_hcd->flags.b.port_speed = hprt0.b.prtspd;
  43414. + if (microframe_schedule)
  43415. + init_hcd_usecs(dwc_otg_hcd);
  43416. +
  43417. + /* Every time when port enables calculate
  43418. + * HFIR.FrInterval
  43419. + */
  43420. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  43421. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  43422. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  43423. +
  43424. + /* Check if we need to adjust the PHY clock speed for
  43425. + * low power and adjust it */
  43426. + if (params->host_support_fs_ls_low_power) {
  43427. + gusbcfg_data_t usbcfg;
  43428. +
  43429. + usbcfg.d32 =
  43430. + DWC_READ_REG32(&global_regs->gusbcfg);
  43431. +
  43432. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  43433. + || hprt0.b.prtspd ==
  43434. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  43435. + /*
  43436. + * Low power
  43437. + */
  43438. + hcfg_data_t hcfg;
  43439. + if (usbcfg.b.phylpwrclksel == 0) {
  43440. + /* Set PHY low power clock select for FS/LS devices */
  43441. + usbcfg.b.phylpwrclksel = 1;
  43442. + DWC_WRITE_REG32
  43443. + (&global_regs->gusbcfg,
  43444. + usbcfg.d32);
  43445. + do_reset = 1;
  43446. + }
  43447. +
  43448. + hcfg.d32 =
  43449. + DWC_READ_REG32
  43450. + (&host_if->host_global_regs->hcfg);
  43451. +
  43452. + if (hprt0.b.prtspd ==
  43453. + DWC_HPRT0_PRTSPD_LOW_SPEED
  43454. + && params->host_ls_low_power_phy_clk
  43455. + ==
  43456. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  43457. + {
  43458. + /* 6 MHZ */
  43459. + DWC_DEBUGPL(DBG_CIL,
  43460. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  43461. + if (hcfg.b.fslspclksel !=
  43462. + DWC_HCFG_6_MHZ) {
  43463. + hcfg.b.fslspclksel =
  43464. + DWC_HCFG_6_MHZ;
  43465. + DWC_WRITE_REG32
  43466. + (&host_if->host_global_regs->hcfg,
  43467. + hcfg.d32);
  43468. + do_reset = 1;
  43469. + }
  43470. + } else {
  43471. + /* 48 MHZ */
  43472. + DWC_DEBUGPL(DBG_CIL,
  43473. + "FS_PHY programming HCFG to 48 MHz ()\n");
  43474. + if (hcfg.b.fslspclksel !=
  43475. + DWC_HCFG_48_MHZ) {
  43476. + hcfg.b.fslspclksel =
  43477. + DWC_HCFG_48_MHZ;
  43478. + DWC_WRITE_REG32
  43479. + (&host_if->host_global_regs->hcfg,
  43480. + hcfg.d32);
  43481. + do_reset = 1;
  43482. + }
  43483. + }
  43484. + } else {
  43485. + /*
  43486. + * Not low power
  43487. + */
  43488. + if (usbcfg.b.phylpwrclksel == 1) {
  43489. + usbcfg.b.phylpwrclksel = 0;
  43490. + DWC_WRITE_REG32
  43491. + (&global_regs->gusbcfg,
  43492. + usbcfg.d32);
  43493. + do_reset = 1;
  43494. + }
  43495. + }
  43496. +
  43497. + if (do_reset) {
  43498. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  43499. + }
  43500. + }
  43501. +
  43502. + if (!do_reset) {
  43503. + /* Port has been enabled set the reset change flag */
  43504. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  43505. + }
  43506. + } else {
  43507. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  43508. + }
  43509. + retval |= 1;
  43510. + }
  43511. +
  43512. + /** Overcurrent Change Interrupt */
  43513. + if (hprt0.b.prtovrcurrchng) {
  43514. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  43515. + "Port Overcurrent Changed--\n", hprt0.d32);
  43516. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  43517. + hprt0_modify.b.prtovrcurrchng = 1;
  43518. + retval |= 1;
  43519. + }
  43520. +
  43521. + /* Clear Port Interrupts */
  43522. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  43523. +
  43524. + return retval;
  43525. +}
  43526. +
  43527. +/** This interrupt indicates that one or more host channels has a pending
  43528. + * interrupt. There are multiple conditions that can cause each host channel
  43529. + * interrupt. This function determines which conditions have occurred for each
  43530. + * host channel interrupt and handles them appropriately. */
  43531. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43532. +{
  43533. + int i;
  43534. + int retval = 0;
  43535. + haint_data_t haint = { .d32 = 0 } ;
  43536. +
  43537. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  43538. + * GINTSTS */
  43539. +
  43540. + if (!fiq_fsm_enable)
  43541. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  43542. +
  43543. + // Overwrite with saved interrupts from fiq handler
  43544. + if(fiq_fsm_enable)
  43545. + {
  43546. + /* check the mask? */
  43547. + local_fiq_disable();
  43548. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  43549. + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
  43550. + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  43551. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  43552. + local_fiq_enable();
  43553. + }
  43554. +
  43555. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  43556. + if (haint.b2.chint & (1 << i)) {
  43557. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  43558. + }
  43559. + }
  43560. +
  43561. + return retval;
  43562. +}
  43563. +
  43564. +/**
  43565. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  43566. + * holds the reason for the halt.
  43567. + *
  43568. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  43569. + * *short_read is set to 1 upon return if less than the requested
  43570. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  43571. + * return. short_read may also be NULL on entry, in which case it remains
  43572. + * unchanged.
  43573. + */
  43574. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  43575. + dwc_otg_hc_regs_t * hc_regs,
  43576. + dwc_otg_qtd_t * qtd,
  43577. + dwc_otg_halt_status_e halt_status,
  43578. + int *short_read)
  43579. +{
  43580. + hctsiz_data_t hctsiz;
  43581. + uint32_t length;
  43582. +
  43583. + if (short_read != NULL) {
  43584. + *short_read = 0;
  43585. + }
  43586. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43587. +
  43588. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  43589. + if (hc->ep_is_in) {
  43590. + length = hc->xfer_len - hctsiz.b.xfersize;
  43591. + if (short_read != NULL) {
  43592. + *short_read = (hctsiz.b.xfersize != 0);
  43593. + }
  43594. + } else if (hc->qh->do_split) {
  43595. + //length = split_out_xfersize[hc->hc_num];
  43596. + length = qtd->ssplit_out_xfer_count;
  43597. + } else {
  43598. + length = hc->xfer_len;
  43599. + }
  43600. + } else {
  43601. + /*
  43602. + * Must use the hctsiz.pktcnt field to determine how much data
  43603. + * has been transferred. This field reflects the number of
  43604. + * packets that have been transferred via the USB. This is
  43605. + * always an integral number of packets if the transfer was
  43606. + * halted before its normal completion. (Can't use the
  43607. + * hctsiz.xfersize field because that reflects the number of
  43608. + * bytes transferred via the AHB, not the USB).
  43609. + */
  43610. + length =
  43611. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  43612. + }
  43613. +
  43614. + return length;
  43615. +}
  43616. +
  43617. +/**
  43618. + * Updates the state of the URB after a Transfer Complete interrupt on the
  43619. + * host channel. Updates the actual_length field of the URB based on the
  43620. + * number of bytes transferred via the host channel. Sets the URB status
  43621. + * if the data transfer is finished.
  43622. + *
  43623. + * @return 1 if the data transfer specified by the URB is completely finished,
  43624. + * 0 otherwise.
  43625. + */
  43626. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  43627. + dwc_otg_hc_regs_t * hc_regs,
  43628. + dwc_otg_hcd_urb_t * urb,
  43629. + dwc_otg_qtd_t * qtd)
  43630. +{
  43631. + int xfer_done = 0;
  43632. + int short_read = 0;
  43633. +
  43634. + int xfer_length;
  43635. +
  43636. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  43637. + DWC_OTG_HC_XFER_COMPLETE,
  43638. + &short_read);
  43639. +
  43640. + if (urb->actual_length + xfer_length > urb->length) {
  43641. + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
  43642. + hc->dev_addr, __func__, __LINE__);
  43643. + xfer_length = urb->length - urb->actual_length;
  43644. + }
  43645. +
  43646. + /* non DWORD-aligned buffer case handling. */
  43647. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  43648. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  43649. + xfer_length);
  43650. + }
  43651. +
  43652. + urb->actual_length += xfer_length;
  43653. +
  43654. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  43655. + (urb->flags & URB_SEND_ZERO_PACKET)
  43656. + && (urb->actual_length == urb->length)
  43657. + && !(urb->length % hc->max_packet)) {
  43658. + xfer_done = 0;
  43659. + } else if (short_read || urb->actual_length >= urb->length) {
  43660. + xfer_done = 1;
  43661. + urb->status = 0;
  43662. + }
  43663. +
  43664. +#ifdef DEBUG
  43665. + {
  43666. + hctsiz_data_t hctsiz;
  43667. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43668. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  43669. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  43670. + hc->hc_num);
  43671. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  43672. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  43673. + hctsiz.b.xfersize);
  43674. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  43675. + urb->length);
  43676. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  43677. + urb->actual_length);
  43678. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  43679. + short_read, xfer_done);
  43680. + }
  43681. +#endif
  43682. +
  43683. + return xfer_done;
  43684. +}
  43685. +
  43686. +/*
  43687. + * Save the starting data toggle for the next transfer. The data toggle is
  43688. + * saved in the QH for non-control transfers and it's saved in the QTD for
  43689. + * control transfers.
  43690. + */
  43691. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  43692. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  43693. +{
  43694. + hctsiz_data_t hctsiz;
  43695. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43696. +
  43697. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  43698. + dwc_otg_qh_t *qh = hc->qh;
  43699. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  43700. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  43701. + } else {
  43702. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  43703. + }
  43704. + } else {
  43705. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  43706. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  43707. + } else {
  43708. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  43709. + }
  43710. + }
  43711. +}
  43712. +
  43713. +/**
  43714. + * Updates the state of an Isochronous URB when the transfer is stopped for
  43715. + * any reason. The fields of the current entry in the frame descriptor array
  43716. + * are set based on the transfer state and the input _halt_status. Completes
  43717. + * the Isochronous URB if all the URB frames have been completed.
  43718. + *
  43719. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  43720. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  43721. + */
  43722. +static dwc_otg_halt_status_e
  43723. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  43724. + dwc_hc_t * hc,
  43725. + dwc_otg_hc_regs_t * hc_regs,
  43726. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  43727. +{
  43728. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  43729. + dwc_otg_halt_status_e ret_val = halt_status;
  43730. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  43731. +
  43732. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  43733. + switch (halt_status) {
  43734. + case DWC_OTG_HC_XFER_COMPLETE:
  43735. + frame_desc->status = 0;
  43736. + frame_desc->actual_length =
  43737. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  43738. +
  43739. + /* non DWORD-aligned buffer case handling. */
  43740. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  43741. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  43742. + hc->qh->dw_align_buf, frame_desc->actual_length);
  43743. + }
  43744. +
  43745. + break;
  43746. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  43747. + urb->error_count++;
  43748. + if (hc->ep_is_in) {
  43749. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  43750. + } else {
  43751. + frame_desc->status = -DWC_E_COMMUNICATION;
  43752. + }
  43753. + frame_desc->actual_length = 0;
  43754. + break;
  43755. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  43756. + urb->error_count++;
  43757. + frame_desc->status = -DWC_E_OVERFLOW;
  43758. + /* Don't need to update actual_length in this case. */
  43759. + break;
  43760. + case DWC_OTG_HC_XFER_XACT_ERR:
  43761. + urb->error_count++;
  43762. + frame_desc->status = -DWC_E_PROTOCOL;
  43763. + frame_desc->actual_length =
  43764. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  43765. +
  43766. + /* non DWORD-aligned buffer case handling. */
  43767. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  43768. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  43769. + hc->qh->dw_align_buf, frame_desc->actual_length);
  43770. + }
  43771. + /* Skip whole frame */
  43772. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  43773. + hc->ep_is_in && hcd->core_if->dma_enable) {
  43774. + qtd->complete_split = 0;
  43775. + qtd->isoc_split_offset = 0;
  43776. + }
  43777. +
  43778. + break;
  43779. + default:
  43780. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  43781. + break;
  43782. + }
  43783. + if (++qtd->isoc_frame_index == urb->packet_count) {
  43784. + /*
  43785. + * urb->status is not used for isoc transfers.
  43786. + * The individual frame_desc statuses are used instead.
  43787. + */
  43788. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  43789. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  43790. + } else {
  43791. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  43792. + }
  43793. + return ret_val;
  43794. +}
  43795. +
  43796. +/**
  43797. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  43798. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  43799. + * still linked to the QH, the QH is added to the end of the inactive
  43800. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  43801. + * schedule if no more QTDs are linked to the QH.
  43802. + */
  43803. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  43804. +{
  43805. + int continue_split = 0;
  43806. + dwc_otg_qtd_t *qtd;
  43807. +
  43808. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  43809. +
  43810. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  43811. +
  43812. + if (qtd->complete_split) {
  43813. + continue_split = 1;
  43814. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  43815. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  43816. + continue_split = 1;
  43817. + }
  43818. +
  43819. + if (free_qtd) {
  43820. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  43821. + continue_split = 0;
  43822. + }
  43823. +
  43824. + qh->channel = NULL;
  43825. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  43826. +}
  43827. +
  43828. +/**
  43829. + * Releases a host channel for use by other transfers. Attempts to select and
  43830. + * queue more transactions since at least one host channel is available.
  43831. + *
  43832. + * @param hcd The HCD state structure.
  43833. + * @param hc The host channel to release.
  43834. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  43835. + * if the transfer is complete or an error has occurred.
  43836. + * @param halt_status Reason the channel is being released. This status
  43837. + * determines the actions taken by this function.
  43838. + */
  43839. +static void release_channel(dwc_otg_hcd_t * hcd,
  43840. + dwc_hc_t * hc,
  43841. + dwc_otg_qtd_t * qtd,
  43842. + dwc_otg_halt_status_e halt_status)
  43843. +{
  43844. + dwc_otg_transaction_type_e tr_type;
  43845. + int free_qtd;
  43846. +
  43847. + int hog_port = 0;
  43848. +
  43849. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  43850. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  43851. +
  43852. + if(fiq_fsm_enable && hc->do_split) {
  43853. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  43854. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  43855. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  43856. + hog_port = 0;
  43857. + }
  43858. + }
  43859. + }
  43860. +
  43861. + switch (halt_status) {
  43862. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  43863. + free_qtd = 1;
  43864. + break;
  43865. + case DWC_OTG_HC_XFER_AHB_ERR:
  43866. + case DWC_OTG_HC_XFER_STALL:
  43867. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  43868. + free_qtd = 1;
  43869. + break;
  43870. + case DWC_OTG_HC_XFER_XACT_ERR:
  43871. + if (qtd->error_count >= 3) {
  43872. + DWC_DEBUGPL(DBG_HCDV,
  43873. + " Complete URB with transaction error\n");
  43874. + free_qtd = 1;
  43875. + qtd->urb->status = -DWC_E_PROTOCOL;
  43876. + hcd->fops->complete(hcd, qtd->urb->priv,
  43877. + qtd->urb, -DWC_E_PROTOCOL);
  43878. + } else {
  43879. + free_qtd = 0;
  43880. + }
  43881. + break;
  43882. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  43883. + /*
  43884. + * The QTD has already been removed and the QH has been
  43885. + * deactivated. Don't want to do anything except release the
  43886. + * host channel and try to queue more transfers.
  43887. + */
  43888. + goto cleanup;
  43889. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  43890. + free_qtd = 0;
  43891. + break;
  43892. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  43893. + DWC_DEBUGPL(DBG_HCDV,
  43894. + " Complete URB with I/O error\n");
  43895. + free_qtd = 1;
  43896. + qtd->urb->status = -DWC_E_IO;
  43897. + hcd->fops->complete(hcd, qtd->urb->priv,
  43898. + qtd->urb, -DWC_E_IO);
  43899. + break;
  43900. + default:
  43901. + free_qtd = 0;
  43902. + break;
  43903. + }
  43904. +
  43905. + deactivate_qh(hcd, hc->qh, free_qtd);
  43906. +
  43907. +cleanup:
  43908. + /*
  43909. + * Release the host channel for use by other transfers. The cleanup
  43910. + * function clears the channel interrupt enables and conditions, so
  43911. + * there's no need to clear the Channel Halted interrupt separately.
  43912. + */
  43913. + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
  43914. + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
  43915. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  43916. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  43917. +
  43918. + if (!microframe_schedule) {
  43919. + switch (hc->ep_type) {
  43920. + case DWC_OTG_EP_TYPE_CONTROL:
  43921. + case DWC_OTG_EP_TYPE_BULK:
  43922. + hcd->non_periodic_channels--;
  43923. + break;
  43924. +
  43925. + default:
  43926. + /*
  43927. + * Don't release reservations for periodic channels here.
  43928. + * That's done when a periodic transfer is descheduled (i.e.
  43929. + * when the QH is removed from the periodic schedule).
  43930. + */
  43931. + break;
  43932. + }
  43933. + } else {
  43934. + hcd->available_host_channels++;
  43935. + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
  43936. + }
  43937. +
  43938. + /* Try to queue more transfers now that there's a free channel. */
  43939. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  43940. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  43941. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  43942. + }
  43943. +}
  43944. +
  43945. +/**
  43946. + * Halts a host channel. If the channel cannot be halted immediately because
  43947. + * the request queue is full, this function ensures that the FIFO empty
  43948. + * interrupt for the appropriate queue is enabled so that the halt request can
  43949. + * be queued when there is space in the request queue.
  43950. + *
  43951. + * This function may also be called in DMA mode. In that case, the channel is
  43952. + * simply released since the core always halts the channel automatically in
  43953. + * DMA mode.
  43954. + */
  43955. +static void halt_channel(dwc_otg_hcd_t * hcd,
  43956. + dwc_hc_t * hc,
  43957. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  43958. +{
  43959. + if (hcd->core_if->dma_enable) {
  43960. + release_channel(hcd, hc, qtd, halt_status);
  43961. + return;
  43962. + }
  43963. +
  43964. + /* Slave mode processing... */
  43965. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  43966. +
  43967. + if (hc->halt_on_queue) {
  43968. + gintmsk_data_t gintmsk = {.d32 = 0 };
  43969. + dwc_otg_core_global_regs_t *global_regs;
  43970. + global_regs = hcd->core_if->core_global_regs;
  43971. +
  43972. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  43973. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  43974. + /*
  43975. + * Make sure the Non-periodic Tx FIFO empty interrupt
  43976. + * is enabled so that the non-periodic schedule will
  43977. + * be processed.
  43978. + */
  43979. + gintmsk.b.nptxfempty = 1;
  43980. + if (fiq_enable) {
  43981. + local_fiq_disable();
  43982. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  43983. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43984. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  43985. + local_fiq_enable();
  43986. + } else {
  43987. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43988. + }
  43989. + } else {
  43990. + /*
  43991. + * Move the QH from the periodic queued schedule to
  43992. + * the periodic assigned schedule. This allows the
  43993. + * halt to be queued when the periodic schedule is
  43994. + * processed.
  43995. + */
  43996. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  43997. + &hc->qh->qh_list_entry);
  43998. +
  43999. + /*
  44000. + * Make sure the Periodic Tx FIFO Empty interrupt is
  44001. + * enabled so that the periodic schedule will be
  44002. + * processed.
  44003. + */
  44004. + gintmsk.b.ptxfempty = 1;
  44005. + if (fiq_enable) {
  44006. + local_fiq_disable();
  44007. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  44008. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  44009. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  44010. + local_fiq_enable();
  44011. + } else {
  44012. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  44013. + }
  44014. + }
  44015. + }
  44016. +}
  44017. +
  44018. +/**
  44019. + * Performs common cleanup for non-periodic transfers after a Transfer
  44020. + * Complete interrupt. This function should be called after any endpoint type
  44021. + * specific handling is finished to release the host channel.
  44022. + */
  44023. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  44024. + dwc_hc_t * hc,
  44025. + dwc_otg_hc_regs_t * hc_regs,
  44026. + dwc_otg_qtd_t * qtd,
  44027. + dwc_otg_halt_status_e halt_status)
  44028. +{
  44029. + hcint_data_t hcint;
  44030. +
  44031. + qtd->error_count = 0;
  44032. +
  44033. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  44034. + if (hcint.b.nyet) {
  44035. + /*
  44036. + * Got a NYET on the last transaction of the transfer. This
  44037. + * means that the endpoint should be in the PING state at the
  44038. + * beginning of the next transfer.
  44039. + */
  44040. + hc->qh->ping_state = 1;
  44041. + clear_hc_int(hc_regs, nyet);
  44042. + }
  44043. +
  44044. + /*
  44045. + * Always halt and release the host channel to make it available for
  44046. + * more transfers. There may still be more phases for a control
  44047. + * transfer or more data packets for a bulk transfer at this point,
  44048. + * but the host channel is still halted. A channel will be reassigned
  44049. + * to the transfer when the non-periodic schedule is processed after
  44050. + * the channel is released. This allows transactions to be queued
  44051. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  44052. + * Tx FIFO Empty interrupt if necessary.
  44053. + */
  44054. + if (hc->ep_is_in) {
  44055. + /*
  44056. + * IN transfers in Slave mode require an explicit disable to
  44057. + * halt the channel. (In DMA mode, this call simply releases
  44058. + * the channel.)
  44059. + */
  44060. + halt_channel(hcd, hc, qtd, halt_status);
  44061. + } else {
  44062. + /*
  44063. + * The channel is automatically disabled by the core for OUT
  44064. + * transfers in Slave mode.
  44065. + */
  44066. + release_channel(hcd, hc, qtd, halt_status);
  44067. + }
  44068. +}
  44069. +
  44070. +/**
  44071. + * Performs common cleanup for periodic transfers after a Transfer Complete
  44072. + * interrupt. This function should be called after any endpoint type specific
  44073. + * handling is finished to release the host channel.
  44074. + */
  44075. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  44076. + dwc_hc_t * hc,
  44077. + dwc_otg_hc_regs_t * hc_regs,
  44078. + dwc_otg_qtd_t * qtd,
  44079. + dwc_otg_halt_status_e halt_status)
  44080. +{
  44081. + hctsiz_data_t hctsiz;
  44082. + qtd->error_count = 0;
  44083. +
  44084. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44085. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  44086. + /* Core halts channel in these cases. */
  44087. + release_channel(hcd, hc, qtd, halt_status);
  44088. + } else {
  44089. + /* Flush any outstanding requests from the Tx queue. */
  44090. + halt_channel(hcd, hc, qtd, halt_status);
  44091. + }
  44092. +}
  44093. +
  44094. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  44095. + dwc_hc_t * hc,
  44096. + dwc_otg_hc_regs_t * hc_regs,
  44097. + dwc_otg_qtd_t * qtd)
  44098. +{
  44099. + uint32_t len;
  44100. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  44101. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  44102. +
  44103. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  44104. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  44105. +
  44106. + if (!len) {
  44107. + qtd->complete_split = 0;
  44108. + qtd->isoc_split_offset = 0;
  44109. + return 0;
  44110. + }
  44111. + frame_desc->actual_length += len;
  44112. +
  44113. + if (hc->align_buff && len)
  44114. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  44115. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  44116. + qtd->isoc_split_offset += len;
  44117. +
  44118. + if (frame_desc->length == frame_desc->actual_length) {
  44119. + frame_desc->status = 0;
  44120. + qtd->isoc_frame_index++;
  44121. + qtd->complete_split = 0;
  44122. + qtd->isoc_split_offset = 0;
  44123. + }
  44124. +
  44125. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  44126. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  44127. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  44128. + } else {
  44129. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  44130. + }
  44131. +
  44132. + return 1; /* Indicates that channel released */
  44133. +}
  44134. +
  44135. +/**
  44136. + * Handles a host channel Transfer Complete interrupt. This handler may be
  44137. + * called in either DMA mode or Slave mode.
  44138. + */
  44139. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  44140. + dwc_hc_t * hc,
  44141. + dwc_otg_hc_regs_t * hc_regs,
  44142. + dwc_otg_qtd_t * qtd)
  44143. +{
  44144. + int urb_xfer_done;
  44145. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44146. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44147. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  44148. +
  44149. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44150. + "Transfer Complete--\n", hc->hc_num);
  44151. +
  44152. + if (hcd->core_if->dma_desc_enable) {
  44153. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  44154. + if (pipe_type == UE_ISOCHRONOUS) {
  44155. + /* Do not disable the interrupt, just clear it */
  44156. + clear_hc_int(hc_regs, xfercomp);
  44157. + return 1;
  44158. + }
  44159. + goto handle_xfercomp_done;
  44160. + }
  44161. +
  44162. + /*
  44163. + * Handle xfer complete on CSPLIT.
  44164. + */
  44165. +
  44166. + if (hc->qh->do_split) {
  44167. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  44168. + && hcd->core_if->dma_enable) {
  44169. + if (qtd->complete_split
  44170. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  44171. + qtd))
  44172. + goto handle_xfercomp_done;
  44173. + } else {
  44174. + qtd->complete_split = 0;
  44175. + }
  44176. + }
  44177. +
  44178. + /* Update the QTD and URB states. */
  44179. + switch (pipe_type) {
  44180. + case UE_CONTROL:
  44181. + switch (qtd->control_phase) {
  44182. + case DWC_OTG_CONTROL_SETUP:
  44183. + if (urb->length > 0) {
  44184. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  44185. + } else {
  44186. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  44187. + }
  44188. + DWC_DEBUGPL(DBG_HCDV,
  44189. + " Control setup transaction done\n");
  44190. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44191. + break;
  44192. + case DWC_OTG_CONTROL_DATA:{
  44193. + urb_xfer_done =
  44194. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  44195. + qtd);
  44196. + if (urb_xfer_done) {
  44197. + qtd->control_phase =
  44198. + DWC_OTG_CONTROL_STATUS;
  44199. + DWC_DEBUGPL(DBG_HCDV,
  44200. + " Control data transfer done\n");
  44201. + } else {
  44202. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44203. + }
  44204. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44205. + break;
  44206. + }
  44207. + case DWC_OTG_CONTROL_STATUS:
  44208. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  44209. + if (urb->status == -DWC_E_IN_PROGRESS) {
  44210. + urb->status = 0;
  44211. + }
  44212. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44213. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44214. + break;
  44215. + }
  44216. +
  44217. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44218. + break;
  44219. + case UE_BULK:
  44220. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  44221. + urb_xfer_done =
  44222. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  44223. + if (urb_xfer_done) {
  44224. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44225. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44226. + } else {
  44227. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44228. + }
  44229. +
  44230. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44231. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44232. + break;
  44233. + case UE_INTERRUPT:
  44234. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  44235. + urb_xfer_done =
  44236. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  44237. +
  44238. + /*
  44239. + * Interrupt URB is done on the first transfer complete
  44240. + * interrupt.
  44241. + */
  44242. + if (urb_xfer_done) {
  44243. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44244. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44245. + } else {
  44246. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44247. + }
  44248. +
  44249. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44250. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44251. + break;
  44252. + case UE_ISOCHRONOUS:
  44253. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  44254. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  44255. + halt_status =
  44256. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44257. + DWC_OTG_HC_XFER_COMPLETE);
  44258. + }
  44259. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44260. + break;
  44261. + }
  44262. +
  44263. +handle_xfercomp_done:
  44264. + disable_hc_int(hc_regs, xfercompl);
  44265. +
  44266. + return 1;
  44267. +}
  44268. +
  44269. +/**
  44270. + * Handles a host channel STALL interrupt. This handler may be called in
  44271. + * either DMA mode or Slave mode.
  44272. + */
  44273. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  44274. + dwc_hc_t * hc,
  44275. + dwc_otg_hc_regs_t * hc_regs,
  44276. + dwc_otg_qtd_t * qtd)
  44277. +{
  44278. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44279. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  44280. +
  44281. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  44282. + "STALL Received--\n", hc->hc_num);
  44283. +
  44284. + if (hcd->core_if->dma_desc_enable) {
  44285. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  44286. + goto handle_stall_done;
  44287. + }
  44288. +
  44289. + if (pipe_type == UE_CONTROL) {
  44290. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  44291. + }
  44292. +
  44293. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  44294. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  44295. + /*
  44296. + * USB protocol requires resetting the data toggle for bulk
  44297. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  44298. + * setup command is issued to the endpoint. Anticipate the
  44299. + * CLEAR_FEATURE command since a STALL has occurred and reset
  44300. + * the data toggle now.
  44301. + */
  44302. + hc->qh->data_toggle = 0;
  44303. + }
  44304. +
  44305. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  44306. +
  44307. +handle_stall_done:
  44308. + disable_hc_int(hc_regs, stall);
  44309. +
  44310. + return 1;
  44311. +}
  44312. +
  44313. +/*
  44314. + * Updates the state of the URB when a transfer has been stopped due to an
  44315. + * abnormal condition before the transfer completes. Modifies the
  44316. + * actual_length field of the URB to reflect the number of bytes that have
  44317. + * actually been transferred via the host channel.
  44318. + */
  44319. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  44320. + dwc_otg_hc_regs_t * hc_regs,
  44321. + dwc_otg_hcd_urb_t * urb,
  44322. + dwc_otg_qtd_t * qtd,
  44323. + dwc_otg_halt_status_e halt_status)
  44324. +{
  44325. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  44326. + halt_status, NULL);
  44327. +
  44328. + if (urb->actual_length + bytes_transferred > urb->length) {
  44329. + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
  44330. + hc->dev_addr, __func__, __LINE__);
  44331. + bytes_transferred = urb->length - urb->actual_length;
  44332. + }
  44333. +
  44334. + /* non DWORD-aligned buffer case handling. */
  44335. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  44336. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  44337. + bytes_transferred);
  44338. + }
  44339. +
  44340. + urb->actual_length += bytes_transferred;
  44341. +
  44342. +#ifdef DEBUG
  44343. + {
  44344. + hctsiz_data_t hctsiz;
  44345. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44346. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  44347. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  44348. + hc->hc_num);
  44349. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  44350. + hc->start_pkt_count);
  44351. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  44352. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  44353. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  44354. + bytes_transferred);
  44355. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  44356. + urb->actual_length);
  44357. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  44358. + urb->length);
  44359. + }
  44360. +#endif
  44361. +}
  44362. +
  44363. +/**
  44364. + * Handles a host channel NAK interrupt. This handler may be called in either
  44365. + * DMA mode or Slave mode.
  44366. + */
  44367. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  44368. + dwc_hc_t * hc,
  44369. + dwc_otg_hc_regs_t * hc_regs,
  44370. + dwc_otg_qtd_t * qtd)
  44371. +{
  44372. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44373. + "NAK Received--\n", hc->hc_num);
  44374. +
  44375. + /*
  44376. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  44377. + * the beginning of the next frame
  44378. + */
  44379. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44380. + case UE_BULK:
  44381. + case UE_CONTROL:
  44382. + if (nak_holdoff && qtd->qh->do_split)
  44383. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  44384. + }
  44385. +
  44386. + /*
  44387. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  44388. + * interrupt. Re-start the SSPLIT transfer.
  44389. + */
  44390. + if (hc->do_split) {
  44391. + if (hc->complete_split) {
  44392. + qtd->error_count = 0;
  44393. + }
  44394. + qtd->complete_split = 0;
  44395. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44396. + goto handle_nak_done;
  44397. + }
  44398. +
  44399. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44400. + case UE_CONTROL:
  44401. + case UE_BULK:
  44402. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  44403. + /*
  44404. + * NAK interrupts are enabled on bulk/control IN
  44405. + * transfers in DMA mode for the sole purpose of
  44406. + * resetting the error count after a transaction error
  44407. + * occurs. The core will continue transferring data.
  44408. + * Disable other interrupts unmasked for the same
  44409. + * reason.
  44410. + */
  44411. + disable_hc_int(hc_regs, datatglerr);
  44412. + disable_hc_int(hc_regs, ack);
  44413. + qtd->error_count = 0;
  44414. + goto handle_nak_done;
  44415. + }
  44416. +
  44417. + /*
  44418. + * NAK interrupts normally occur during OUT transfers in DMA
  44419. + * or Slave mode. For IN transfers, more requests will be
  44420. + * queued as request queue space is available.
  44421. + */
  44422. + qtd->error_count = 0;
  44423. +
  44424. + if (!hc->qh->ping_state) {
  44425. + update_urb_state_xfer_intr(hc, hc_regs,
  44426. + qtd->urb, qtd,
  44427. + DWC_OTG_HC_XFER_NAK);
  44428. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44429. +
  44430. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  44431. + hc->qh->ping_state = 1;
  44432. + }
  44433. +
  44434. + /*
  44435. + * Halt the channel so the transfer can be re-started from
  44436. + * the appropriate point or the PING protocol will
  44437. + * start/continue.
  44438. + */
  44439. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44440. + break;
  44441. + case UE_INTERRUPT:
  44442. + qtd->error_count = 0;
  44443. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44444. + break;
  44445. + case UE_ISOCHRONOUS:
  44446. + /* Should never get called for isochronous transfers. */
  44447. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  44448. + break;
  44449. + }
  44450. +
  44451. +handle_nak_done:
  44452. + disable_hc_int(hc_regs, nak);
  44453. +
  44454. + return 1;
  44455. +}
  44456. +
  44457. +/**
  44458. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  44459. + * performing the PING protocol in Slave mode, when errors occur during
  44460. + * either Slave mode or DMA mode, and during Start Split transactions.
  44461. + */
  44462. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  44463. + dwc_hc_t * hc,
  44464. + dwc_otg_hc_regs_t * hc_regs,
  44465. + dwc_otg_qtd_t * qtd)
  44466. +{
  44467. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44468. + "ACK Received--\n", hc->hc_num);
  44469. +
  44470. + if (hc->do_split) {
  44471. + /*
  44472. + * Handle ACK on SSPLIT.
  44473. + * ACK should not occur in CSPLIT.
  44474. + */
  44475. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  44476. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  44477. + }
  44478. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  44479. + /* Don't need complete for isochronous out transfers. */
  44480. + qtd->complete_split = 1;
  44481. + }
  44482. +
  44483. + /* ISOC OUT */
  44484. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  44485. + switch (hc->xact_pos) {
  44486. + case DWC_HCSPLIT_XACTPOS_ALL:
  44487. + break;
  44488. + case DWC_HCSPLIT_XACTPOS_END:
  44489. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  44490. + qtd->isoc_split_offset = 0;
  44491. + break;
  44492. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  44493. + case DWC_HCSPLIT_XACTPOS_MID:
  44494. + /*
  44495. + * For BEGIN or MID, calculate the length for
  44496. + * the next microframe to determine the correct
  44497. + * SSPLIT token, either MID or END.
  44498. + */
  44499. + {
  44500. + struct dwc_otg_hcd_iso_packet_desc
  44501. + *frame_desc;
  44502. +
  44503. + frame_desc =
  44504. + &qtd->urb->
  44505. + iso_descs[qtd->isoc_frame_index];
  44506. + qtd->isoc_split_offset += 188;
  44507. +
  44508. + if ((frame_desc->length -
  44509. + qtd->isoc_split_offset) <= 188) {
  44510. + qtd->isoc_split_pos =
  44511. + DWC_HCSPLIT_XACTPOS_END;
  44512. + } else {
  44513. + qtd->isoc_split_pos =
  44514. + DWC_HCSPLIT_XACTPOS_MID;
  44515. + }
  44516. +
  44517. + }
  44518. + break;
  44519. + }
  44520. + } else {
  44521. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  44522. + }
  44523. + } else {
  44524. + /*
  44525. + * An unmasked ACK on a non-split DMA transaction is
  44526. + * for the sole purpose of resetting error counts. Disable other
  44527. + * interrupts unmasked for the same reason.
  44528. + */
  44529. + if(hcd->core_if->dma_enable) {
  44530. + disable_hc_int(hc_regs, datatglerr);
  44531. + disable_hc_int(hc_regs, nak);
  44532. + }
  44533. + qtd->error_count = 0;
  44534. +
  44535. + if (hc->qh->ping_state) {
  44536. + hc->qh->ping_state = 0;
  44537. + /*
  44538. + * Halt the channel so the transfer can be re-started
  44539. + * from the appropriate point. This only happens in
  44540. + * Slave mode. In DMA mode, the ping_state is cleared
  44541. + * when the transfer is started because the core
  44542. + * automatically executes the PING, then the transfer.
  44543. + */
  44544. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  44545. + }
  44546. + }
  44547. +
  44548. + /*
  44549. + * If the ACK occurred when _not_ in the PING state, let the channel
  44550. + * continue transferring data after clearing the error count.
  44551. + */
  44552. +
  44553. + disable_hc_int(hc_regs, ack);
  44554. +
  44555. + return 1;
  44556. +}
  44557. +
  44558. +/**
  44559. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  44560. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  44561. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  44562. + * handled in the xfercomp interrupt handler, not here. This handler may be
  44563. + * called in either DMA mode or Slave mode.
  44564. + */
  44565. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  44566. + dwc_hc_t * hc,
  44567. + dwc_otg_hc_regs_t * hc_regs,
  44568. + dwc_otg_qtd_t * qtd)
  44569. +{
  44570. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44571. + "NYET Received--\n", hc->hc_num);
  44572. +
  44573. + /*
  44574. + * NYET on CSPLIT
  44575. + * re-do the CSPLIT immediately on non-periodic
  44576. + */
  44577. + if (hc->do_split && hc->complete_split) {
  44578. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  44579. + && hcd->core_if->dma_enable) {
  44580. + qtd->complete_split = 0;
  44581. + qtd->isoc_split_offset = 0;
  44582. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  44583. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  44584. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  44585. + }
  44586. + else
  44587. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  44588. + goto handle_nyet_done;
  44589. + }
  44590. +
  44591. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  44592. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  44593. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  44594. +
  44595. + // With the FIQ running we only ever see the failed NYET
  44596. + if (dwc_full_frame_num(frnum) !=
  44597. + dwc_full_frame_num(hc->qh->sched_frame) ||
  44598. + fiq_fsm_enable) {
  44599. + /*
  44600. + * No longer in the same full speed frame.
  44601. + * Treat this as a transaction error.
  44602. + */
  44603. +#if 0
  44604. + /** @todo Fix system performance so this can
  44605. + * be treated as an error. Right now complete
  44606. + * splits cannot be scheduled precisely enough
  44607. + * due to other system activity, so this error
  44608. + * occurs regularly in Slave mode.
  44609. + */
  44610. + qtd->error_count++;
  44611. +#endif
  44612. + qtd->complete_split = 0;
  44613. + halt_channel(hcd, hc, qtd,
  44614. + DWC_OTG_HC_XFER_XACT_ERR);
  44615. + /** @todo add support for isoc release */
  44616. + goto handle_nyet_done;
  44617. + }
  44618. + }
  44619. +
  44620. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  44621. + goto handle_nyet_done;
  44622. + }
  44623. +
  44624. + hc->qh->ping_state = 1;
  44625. + qtd->error_count = 0;
  44626. +
  44627. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  44628. + DWC_OTG_HC_XFER_NYET);
  44629. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44630. +
  44631. + /*
  44632. + * Halt the channel and re-start the transfer so the PING
  44633. + * protocol will start.
  44634. + */
  44635. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  44636. +
  44637. +handle_nyet_done:
  44638. + disable_hc_int(hc_regs, nyet);
  44639. + return 1;
  44640. +}
  44641. +
  44642. +/**
  44643. + * Handles a host channel babble interrupt. This handler may be called in
  44644. + * either DMA mode or Slave mode.
  44645. + */
  44646. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  44647. + dwc_hc_t * hc,
  44648. + dwc_otg_hc_regs_t * hc_regs,
  44649. + dwc_otg_qtd_t * qtd)
  44650. +{
  44651. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44652. + "Babble Error--\n", hc->hc_num);
  44653. +
  44654. + if (hcd->core_if->dma_desc_enable) {
  44655. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44656. + DWC_OTG_HC_XFER_BABBLE_ERR);
  44657. + goto handle_babble_done;
  44658. + }
  44659. +
  44660. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  44661. + hcd->fops->complete(hcd, qtd->urb->priv,
  44662. + qtd->urb, -DWC_E_OVERFLOW);
  44663. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  44664. + } else {
  44665. + dwc_otg_halt_status_e halt_status;
  44666. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44667. + DWC_OTG_HC_XFER_BABBLE_ERR);
  44668. + halt_channel(hcd, hc, qtd, halt_status);
  44669. + }
  44670. +
  44671. +handle_babble_done:
  44672. + disable_hc_int(hc_regs, bblerr);
  44673. + return 1;
  44674. +}
  44675. +
  44676. +/**
  44677. + * Handles a host channel AHB error interrupt. This handler is only called in
  44678. + * DMA mode.
  44679. + */
  44680. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  44681. + dwc_hc_t * hc,
  44682. + dwc_otg_hc_regs_t * hc_regs,
  44683. + dwc_otg_qtd_t * qtd)
  44684. +{
  44685. + hcchar_data_t hcchar;
  44686. + hcsplt_data_t hcsplt;
  44687. + hctsiz_data_t hctsiz;
  44688. + uint32_t hcdma;
  44689. + char *pipetype, *speed;
  44690. +
  44691. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44692. +
  44693. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44694. + "AHB Error--\n", hc->hc_num);
  44695. +
  44696. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44697. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  44698. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44699. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  44700. +
  44701. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  44702. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  44703. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  44704. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  44705. + DWC_ERROR(" Device address: %d\n",
  44706. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  44707. + DWC_ERROR(" Endpoint: %d, %s\n",
  44708. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  44709. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  44710. +
  44711. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  44712. + case UE_CONTROL:
  44713. + pipetype = "CONTROL";
  44714. + break;
  44715. + case UE_BULK:
  44716. + pipetype = "BULK";
  44717. + break;
  44718. + case UE_INTERRUPT:
  44719. + pipetype = "INTERRUPT";
  44720. + break;
  44721. + case UE_ISOCHRONOUS:
  44722. + pipetype = "ISOCHRONOUS";
  44723. + break;
  44724. + default:
  44725. + pipetype = "UNKNOWN";
  44726. + break;
  44727. + }
  44728. +
  44729. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  44730. +
  44731. + switch (hc->speed) {
  44732. + case DWC_OTG_EP_SPEED_HIGH:
  44733. + speed = "HIGH";
  44734. + break;
  44735. + case DWC_OTG_EP_SPEED_FULL:
  44736. + speed = "FULL";
  44737. + break;
  44738. + case DWC_OTG_EP_SPEED_LOW:
  44739. + speed = "LOW";
  44740. + break;
  44741. + default:
  44742. + speed = "UNKNOWN";
  44743. + break;
  44744. + };
  44745. +
  44746. + DWC_ERROR(" Speed: %s\n", speed);
  44747. +
  44748. + DWC_ERROR(" Max packet size: %d\n",
  44749. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  44750. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  44751. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  44752. + urb->buf, (void *)urb->dma);
  44753. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  44754. + urb->setup_packet, (void *)urb->setup_dma);
  44755. + DWC_ERROR(" Interval: %d\n", urb->interval);
  44756. +
  44757. + /* Core haltes the channel for Descriptor DMA mode */
  44758. + if (hcd->core_if->dma_desc_enable) {
  44759. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44760. + DWC_OTG_HC_XFER_AHB_ERR);
  44761. + goto handle_ahberr_done;
  44762. + }
  44763. +
  44764. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  44765. +
  44766. + /*
  44767. + * Force a channel halt. Don't call halt_channel because that won't
  44768. + * write to the HCCHARn register in DMA mode to force the halt.
  44769. + */
  44770. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  44771. +handle_ahberr_done:
  44772. + disable_hc_int(hc_regs, ahberr);
  44773. + return 1;
  44774. +}
  44775. +
  44776. +/**
  44777. + * Handles a host channel transaction error interrupt. This handler may be
  44778. + * called in either DMA mode or Slave mode.
  44779. + */
  44780. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  44781. + dwc_hc_t * hc,
  44782. + dwc_otg_hc_regs_t * hc_regs,
  44783. + dwc_otg_qtd_t * qtd)
  44784. +{
  44785. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44786. + "Transaction Error--\n", hc->hc_num);
  44787. +
  44788. + if (hcd->core_if->dma_desc_enable) {
  44789. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44790. + DWC_OTG_HC_XFER_XACT_ERR);
  44791. + goto handle_xacterr_done;
  44792. + }
  44793. +
  44794. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44795. + case UE_CONTROL:
  44796. + case UE_BULK:
  44797. + qtd->error_count++;
  44798. + if (!hc->qh->ping_state) {
  44799. +
  44800. + update_urb_state_xfer_intr(hc, hc_regs,
  44801. + qtd->urb, qtd,
  44802. + DWC_OTG_HC_XFER_XACT_ERR);
  44803. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44804. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  44805. + hc->qh->ping_state = 1;
  44806. + }
  44807. + }
  44808. +
  44809. + /*
  44810. + * Halt the channel so the transfer can be re-started from
  44811. + * the appropriate point or the PING protocol will start.
  44812. + */
  44813. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44814. + break;
  44815. + case UE_INTERRUPT:
  44816. + qtd->error_count++;
  44817. + if (hc->do_split && hc->complete_split) {
  44818. + qtd->complete_split = 0;
  44819. + }
  44820. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44821. + break;
  44822. + case UE_ISOCHRONOUS:
  44823. + {
  44824. + dwc_otg_halt_status_e halt_status;
  44825. + halt_status =
  44826. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44827. + DWC_OTG_HC_XFER_XACT_ERR);
  44828. +
  44829. + halt_channel(hcd, hc, qtd, halt_status);
  44830. + }
  44831. + break;
  44832. + }
  44833. +handle_xacterr_done:
  44834. + disable_hc_int(hc_regs, xacterr);
  44835. +
  44836. + return 1;
  44837. +}
  44838. +
  44839. +/**
  44840. + * Handles a host channel frame overrun interrupt. This handler may be called
  44841. + * in either DMA mode or Slave mode.
  44842. + */
  44843. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  44844. + dwc_hc_t * hc,
  44845. + dwc_otg_hc_regs_t * hc_regs,
  44846. + dwc_otg_qtd_t * qtd)
  44847. +{
  44848. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44849. + "Frame Overrun--\n", hc->hc_num);
  44850. +
  44851. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44852. + case UE_CONTROL:
  44853. + case UE_BULK:
  44854. + break;
  44855. + case UE_INTERRUPT:
  44856. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  44857. + break;
  44858. + case UE_ISOCHRONOUS:
  44859. + {
  44860. + dwc_otg_halt_status_e halt_status;
  44861. + halt_status =
  44862. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44863. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  44864. +
  44865. + halt_channel(hcd, hc, qtd, halt_status);
  44866. + }
  44867. + break;
  44868. + }
  44869. +
  44870. + disable_hc_int(hc_regs, frmovrun);
  44871. +
  44872. + return 1;
  44873. +}
  44874. +
  44875. +/**
  44876. + * Handles a host channel data toggle error interrupt. This handler may be
  44877. + * called in either DMA mode or Slave mode.
  44878. + */
  44879. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  44880. + dwc_hc_t * hc,
  44881. + dwc_otg_hc_regs_t * hc_regs,
  44882. + dwc_otg_qtd_t * qtd)
  44883. +{
  44884. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44885. + "Data Toggle Error on %s transfer--\n",
  44886. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  44887. +
  44888. + /* Data toggles on split transactions cause the hc to halt.
  44889. + * restart transfer */
  44890. + if(hc->qh->do_split)
  44891. + {
  44892. + qtd->error_count++;
  44893. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44894. + update_urb_state_xfer_intr(hc, hc_regs,
  44895. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44896. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44897. + } else if (hc->ep_is_in) {
  44898. + /* An unmasked data toggle error on a non-split DMA transaction is
  44899. + * for the sole purpose of resetting error counts. Disable other
  44900. + * interrupts unmasked for the same reason.
  44901. + */
  44902. + if(hcd->core_if->dma_enable) {
  44903. + disable_hc_int(hc_regs, ack);
  44904. + disable_hc_int(hc_regs, nak);
  44905. + }
  44906. + qtd->error_count = 0;
  44907. + }
  44908. +
  44909. + disable_hc_int(hc_regs, datatglerr);
  44910. +
  44911. + return 1;
  44912. +}
  44913. +
  44914. +#ifdef DEBUG
  44915. +/**
  44916. + * This function is for debug only. It checks that a valid halt status is set
  44917. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  44918. + * taken and a warning is issued.
  44919. + * @return 1 if halt status is ok, 0 otherwise.
  44920. + */
  44921. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  44922. + dwc_hc_t * hc,
  44923. + dwc_otg_hc_regs_t * hc_regs,
  44924. + dwc_otg_qtd_t * qtd)
  44925. +{
  44926. + hcchar_data_t hcchar;
  44927. + hctsiz_data_t hctsiz;
  44928. + hcint_data_t hcint;
  44929. + hcintmsk_data_t hcintmsk;
  44930. + hcsplt_data_t hcsplt;
  44931. +
  44932. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  44933. + /*
  44934. + * This code is here only as a check. This condition should
  44935. + * never happen. Ignore the halt if it does occur.
  44936. + */
  44937. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44938. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44939. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  44940. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  44941. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  44942. + DWC_WARN
  44943. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  44944. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  44945. + "hcint 0x%08x, hcintmsk 0x%08x, "
  44946. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  44947. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  44948. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  44949. +
  44950. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  44951. + __func__, hc->hc_num);
  44952. + DWC_WARN("\n");
  44953. + clear_hc_int(hc_regs, chhltd);
  44954. + return 0;
  44955. + }
  44956. +
  44957. + /*
  44958. + * This code is here only as a check. hcchar.chdis should
  44959. + * never be set when the halt interrupt occurs. Halt the
  44960. + * channel again if it does occur.
  44961. + */
  44962. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44963. + if (hcchar.b.chdis) {
  44964. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  44965. + "hcchar 0x%08x, trying to halt again\n",
  44966. + __func__, hcchar.d32);
  44967. + clear_hc_int(hc_regs, chhltd);
  44968. + hc->halt_pending = 0;
  44969. + halt_channel(hcd, hc, qtd, hc->halt_status);
  44970. + return 0;
  44971. + }
  44972. +
  44973. + return 1;
  44974. +}
  44975. +#endif
  44976. +
  44977. +/**
  44978. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  44979. + * determines the reason the channel halted and proceeds accordingly.
  44980. + */
  44981. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  44982. + dwc_hc_t * hc,
  44983. + dwc_otg_hc_regs_t * hc_regs,
  44984. + dwc_otg_qtd_t * qtd)
  44985. +{
  44986. + int out_nak_enh = 0;
  44987. + hcint_data_t hcint;
  44988. + hcintmsk_data_t hcintmsk;
  44989. + /* For core with OUT NAK enhancement, the flow for high-
  44990. + * speed CONTROL/BULK OUT is handled a little differently.
  44991. + */
  44992. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  44993. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  44994. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  44995. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  44996. + out_nak_enh = 1;
  44997. + }
  44998. + }
  44999. +
  45000. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  45001. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  45002. + && !hcd->core_if->dma_desc_enable)) {
  45003. + /*
  45004. + * Just release the channel. A dequeue can happen on a
  45005. + * transfer timeout. In the case of an AHB Error, the channel
  45006. + * was forced to halt because there's no way to gracefully
  45007. + * recover.
  45008. + */
  45009. + if (hcd->core_if->dma_desc_enable)
  45010. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  45011. + hc->halt_status);
  45012. + else
  45013. + release_channel(hcd, hc, qtd, hc->halt_status);
  45014. + return;
  45015. + }
  45016. +
  45017. + /* Read the HCINTn register to determine the cause for the halt. */
  45018. +
  45019. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  45020. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  45021. +
  45022. + if (hcint.b.xfercomp) {
  45023. + /** @todo This is here because of a possible hardware bug. Spec
  45024. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  45025. + * interrupt w/ACK bit set should occur, but I only see the
  45026. + * XFERCOMP bit, even with it masked out. This is a workaround
  45027. + * for that behavior. Should fix this when hardware is fixed.
  45028. + */
  45029. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  45030. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  45031. + }
  45032. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  45033. + } else if (hcint.b.stall) {
  45034. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45035. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  45036. + if (out_nak_enh) {
  45037. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  45038. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  45039. + qtd->error_count = 0;
  45040. + } else {
  45041. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  45042. + }
  45043. + }
  45044. +
  45045. + /*
  45046. + * Must handle xacterr before nak or ack. Could get a xacterr
  45047. + * at the same time as either of these on a BULK/CONTROL OUT
  45048. + * that started with a PING. The xacterr takes precedence.
  45049. + */
  45050. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45051. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  45052. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45053. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  45054. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45055. + } else if (hcint.b.bblerr) {
  45056. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45057. + } else if (hcint.b.frmovrun) {
  45058. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  45059. + } else if (hcint.b.datatglerr) {
  45060. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  45061. + } else if (!out_nak_enh) {
  45062. + if (hcint.b.nyet) {
  45063. + /*
  45064. + * Must handle nyet before nak or ack. Could get a nyet at the
  45065. + * same time as either of those on a BULK/CONTROL OUT that
  45066. + * started with a PING. The nyet takes precedence.
  45067. + */
  45068. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  45069. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  45070. + /*
  45071. + * If nak is not masked, it's because a non-split IN transfer
  45072. + * is in an error state. In that case, the nak is handled by
  45073. + * the nak interrupt handler, not here. Handle nak here for
  45074. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  45075. + * rewinding the buffer pointer.
  45076. + */
  45077. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45078. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  45079. + /*
  45080. + * If ack is not masked, it's because a non-split IN transfer
  45081. + * is in an error state. In that case, the ack is handled by
  45082. + * the ack interrupt handler, not here. Handle ack here for
  45083. + * split transfers. Start splits halt on ACK.
  45084. + */
  45085. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  45086. + } else {
  45087. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  45088. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  45089. + /*
  45090. + * A periodic transfer halted with no other channel
  45091. + * interrupts set. Assume it was halted by the core
  45092. + * because it could not be completed in its scheduled
  45093. + * (micro)frame.
  45094. + */
  45095. +#ifdef DEBUG
  45096. + DWC_PRINTF
  45097. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  45098. + __func__, hc->hc_num);
  45099. +#endif
  45100. + halt_channel(hcd, hc, qtd,
  45101. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  45102. + } else {
  45103. + DWC_ERROR
  45104. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  45105. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  45106. + __func__, hc->hc_num, hcint.d32,
  45107. + DWC_READ_REG32(&hcd->
  45108. + core_if->core_global_regs->
  45109. + gintsts));
  45110. + /* Failthrough: use 3-strikes rule */
  45111. + qtd->error_count++;
  45112. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  45113. + update_urb_state_xfer_intr(hc, hc_regs,
  45114. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  45115. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  45116. + }
  45117. +
  45118. + }
  45119. + } else {
  45120. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  45121. + hcint.d32);
  45122. + /* Failthrough: use 3-strikes rule */
  45123. + qtd->error_count++;
  45124. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  45125. + update_urb_state_xfer_intr(hc, hc_regs,
  45126. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  45127. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  45128. + }
  45129. +}
  45130. +
  45131. +/**
  45132. + * Handles a host channel Channel Halted interrupt.
  45133. + *
  45134. + * In slave mode, this handler is called only when the driver specifically
  45135. + * requests a halt. This occurs during handling other host channel interrupts
  45136. + * (e.g. nak, xacterr, stall, nyet, etc.).
  45137. + *
  45138. + * In DMA mode, this is the interrupt that occurs when the core has finished
  45139. + * processing a transfer on a channel. Other host channel interrupts (except
  45140. + * ahberr) are disabled in DMA mode.
  45141. + */
  45142. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  45143. + dwc_hc_t * hc,
  45144. + dwc_otg_hc_regs_t * hc_regs,
  45145. + dwc_otg_qtd_t * qtd)
  45146. +{
  45147. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  45148. + "Channel Halted--\n", hc->hc_num);
  45149. +
  45150. + if (hcd->core_if->dma_enable) {
  45151. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
  45152. + } else {
  45153. +#ifdef DEBUG
  45154. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  45155. + return 1;
  45156. + }
  45157. +#endif
  45158. + release_channel(hcd, hc, qtd, hc->halt_status);
  45159. + }
  45160. +
  45161. + return 1;
  45162. +}
  45163. +
  45164. +
  45165. +/**
  45166. + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
  45167. + * FIQ transfer completion
  45168. + * @hcd: Pointer to dwc_otg_hcd struct
  45169. + * @num: Host channel number
  45170. + *
  45171. + * 1. Un-mangle the status as recorded in each iso_frame_desc status
  45172. + * 2. Copy it from the dwc_otg_urb into the real URB
  45173. + */
  45174. +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  45175. +{
  45176. + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
  45177. + int nr_frames = dwc_urb->packet_count;
  45178. + int i;
  45179. + hcint_data_t frame_hcint;
  45180. +
  45181. + for (i = 0; i < nr_frames; i++) {
  45182. + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
  45183. + if (frame_hcint.b.xfercomp) {
  45184. + dwc_urb->iso_descs[i].status = 0;
  45185. + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
  45186. + } else if (frame_hcint.b.frmovrun) {
  45187. + if (qh->ep_is_in)
  45188. + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
  45189. + else
  45190. + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
  45191. + dwc_urb->error_count++;
  45192. + dwc_urb->iso_descs[i].actual_length = 0;
  45193. + } else if (frame_hcint.b.xacterr) {
  45194. + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
  45195. + dwc_urb->error_count++;
  45196. + dwc_urb->iso_descs[i].actual_length = 0;
  45197. + } else if (frame_hcint.b.bblerr) {
  45198. + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
  45199. + dwc_urb->error_count++;
  45200. + dwc_urb->iso_descs[i].actual_length = 0;
  45201. + } else {
  45202. + /* Something went wrong */
  45203. + dwc_urb->iso_descs[i].status = -1;
  45204. + dwc_urb->iso_descs[i].actual_length = 0;
  45205. + dwc_urb->error_count++;
  45206. + }
  45207. + }
  45208. + qh->sched_frame = dwc_frame_num_inc(qh->sched_frame, qh->interval * (nr_frames - 1));
  45209. +
  45210. + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
  45211. + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
  45212. +}
  45213. +
  45214. +/**
  45215. + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
  45216. + * @hcd: Pointer to dwc_otg_hcd struct
  45217. + * @num: Host channel number
  45218. + *
  45219. + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
  45220. + * Returns total length of data or -1 if the buffers were not used.
  45221. + *
  45222. + */
  45223. +int dwc_otg_fiq_unsetup_per_dma(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  45224. +{
  45225. + dwc_hc_t *hc = qh->channel;
  45226. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  45227. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  45228. + uint8_t *ptr = NULL;
  45229. + int index = 0, len = 0;
  45230. + int i = 0;
  45231. + if (hc->ep_is_in) {
  45232. + /* Copy data out of the DMA bounce buffers to the URB's buffer.
  45233. + * The align_buf is ignored as this is ignored on FSM enqueue. */
  45234. + ptr = qtd->urb->buf;
  45235. + if (qh->ep_type == UE_ISOCHRONOUS) {
  45236. + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
  45237. + index = qtd->isoc_frame_index;
  45238. + ptr += qtd->urb->iso_descs[index].offset;
  45239. + } else {
  45240. + /* Need to increment by actual_length for interrupt IN */
  45241. + ptr += qtd->urb->actual_length;
  45242. + }
  45243. +
  45244. + for (i = 0; i < st->dma_info.index; i++) {
  45245. + len += st->dma_info.slot_len[i];
  45246. + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
  45247. + ptr += st->dma_info.slot_len[i];
  45248. + }
  45249. + return len;
  45250. + } else {
  45251. + /* OUT endpoints - nothing to do. */
  45252. + return -1;
  45253. + }
  45254. +
  45255. +}
  45256. +/**
  45257. + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
  45258. + * from a channel handled in the FIQ
  45259. + * @hcd: Pointer to dwc_otg_hcd struct
  45260. + * @num: Host channel number
  45261. + *
  45262. + * If a host channel interrupt was received by the IRQ and this was a channel
  45263. + * used by the FIQ, the execution flow for transfer completion is substantially
  45264. + * different from the normal (messy) path. This function and its friends handles
  45265. + * channel cleanup and transaction completion from a FIQ transaction.
  45266. + */
  45267. +void dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
  45268. +{
  45269. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  45270. + dwc_hc_t *hc = hcd->hc_ptr_array[num];
  45271. + dwc_otg_qtd_t *qtd;
  45272. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
  45273. + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
  45274. + hctsiz_data_t hctsiz = hcd->fiq_state->channel[num].hctsiz_copy;
  45275. + int hostchannels = 0;
  45276. + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
  45277. +
  45278. + hostchannels = hcd->available_host_channels;
  45279. + if (hc->halt_pending) {
  45280. + /* Dequeue: The FIQ was allowed to complete the transfer but state has been cleared. */
  45281. + if (hc->qh && st->fsm == FIQ_NP_SPLIT_DONE &&
  45282. + hcint.b.xfercomp && hc->qh->ep_type == UE_BULK) {
  45283. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  45284. + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  45285. + } else {
  45286. + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  45287. + }
  45288. + }
  45289. + release_channel(hcd, hc, NULL, hc->halt_status);
  45290. + return;
  45291. + }
  45292. +
  45293. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  45294. + switch (st->fsm) {
  45295. + case FIQ_TEST:
  45296. + break;
  45297. +
  45298. + case FIQ_DEQUEUE_ISSUED:
  45299. + /* Handled above, but keep for posterity */
  45300. + release_channel(hcd, hc, NULL, hc->halt_status);
  45301. + break;
  45302. +
  45303. + case FIQ_NP_SPLIT_DONE:
  45304. + /* Nonperiodic transaction complete. */
  45305. + if (!hc->ep_is_in) {
  45306. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  45307. + }
  45308. + if (hcint.b.xfercomp) {
  45309. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  45310. + } else if (hcint.b.nak) {
  45311. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45312. + } else {
  45313. + DWC_WARN("Unexpected IRQ state on FSM transaction:"
  45314. + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
  45315. + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
  45316. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45317. + }
  45318. + break;
  45319. +
  45320. + case FIQ_NP_SPLIT_HS_ABORTED:
  45321. + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
  45322. + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
  45323. + * because there's no guarantee which order a non-periodic split happened in.
  45324. + * We could end up clearing a perfectly good transaction out of the buffer.
  45325. + */
  45326. + if (hcint.b.xacterr) {
  45327. + qtd->error_count += st->nr_errors;
  45328. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45329. + } else if (hcint.b.ahberr) {
  45330. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45331. + } else {
  45332. + DWC_WARN("Unexpected IRQ state on FSM transaction:"
  45333. + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
  45334. + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
  45335. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45336. + }
  45337. + break;
  45338. +
  45339. + case FIQ_NP_SPLIT_LS_ABORTED:
  45340. + /* A few cases can cause this - either an unknown state on a SSPLIT or
  45341. + * STALL/data toggle error response on a CSPLIT */
  45342. + if (hcint.b.stall) {
  45343. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45344. + } else if (hcint.b.datatglerr) {
  45345. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  45346. + } else if (hcint.b.bblerr) {
  45347. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45348. + } else if (hcint.b.ahberr) {
  45349. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45350. + } else {
  45351. + DWC_WARN("Unexpected IRQ state on FSM transaction:"
  45352. + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
  45353. + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
  45354. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45355. + }
  45356. + break;
  45357. +
  45358. + case FIQ_PER_SPLIT_DONE:
  45359. + /* Isoc IN or Interrupt IN/OUT */
  45360. +
  45361. + /* Flow control here is different from the normal execution by the driver.
  45362. + * We need to completely ignore most of the driver's method of handling
  45363. + * split transactions and do it ourselves.
  45364. + */
  45365. + if (hc->ep_type == UE_INTERRUPT) {
  45366. + if (hcint.b.nak) {
  45367. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45368. + } else if (hc->ep_is_in) {
  45369. + int len;
  45370. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  45371. + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
  45372. + qtd->urb->actual_length += len;
  45373. + if (qtd->urb->actual_length >= qtd->urb->length) {
  45374. + qtd->urb->status = 0;
  45375. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45376. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45377. + } else {
  45378. + /* Interrupt transfer not complete yet - is it a short read? */
  45379. + if (len < hc->max_packet) {
  45380. + /* Interrupt transaction complete */
  45381. + qtd->urb->status = 0;
  45382. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45383. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45384. + } else {
  45385. + /* Further transactions required */
  45386. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45387. + }
  45388. + }
  45389. + } else {
  45390. + /* Interrupt OUT complete. */
  45391. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  45392. + qtd->urb->actual_length += hc->xfer_len;
  45393. + if (qtd->urb->actual_length >= qtd->urb->length) {
  45394. + qtd->urb->status = 0;
  45395. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45396. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45397. + } else {
  45398. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45399. + }
  45400. + }
  45401. + } else {
  45402. + /* ISOC IN complete. */
  45403. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45404. + int len = 0;
  45405. + /* Record errors, update qtd. */
  45406. + if (st->nr_errors) {
  45407. + frame_desc->actual_length = 0;
  45408. + frame_desc->status = -DWC_E_PROTOCOL;
  45409. + } else {
  45410. + frame_desc->status = 0;
  45411. + /* Unswizzle dma */
  45412. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  45413. + frame_desc->actual_length = len;
  45414. + }
  45415. + qtd->isoc_frame_index++;
  45416. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45417. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45418. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45419. + } else {
  45420. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45421. + }
  45422. + }
  45423. + break;
  45424. +
  45425. + case FIQ_PER_ISO_OUT_DONE: {
  45426. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45427. + /* Record errors, update qtd. */
  45428. + if (st->nr_errors) {
  45429. + frame_desc->actual_length = 0;
  45430. + frame_desc->status = -DWC_E_PROTOCOL;
  45431. + } else {
  45432. + frame_desc->status = 0;
  45433. + frame_desc->actual_length = frame_desc->length;
  45434. + }
  45435. + qtd->isoc_frame_index++;
  45436. + qtd->isoc_split_offset = 0;
  45437. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45438. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45439. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45440. + } else {
  45441. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45442. + }
  45443. + }
  45444. + break;
  45445. +
  45446. + case FIQ_PER_SPLIT_NYET_ABORTED:
  45447. + /* Doh. lost the data. */
  45448. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  45449. + "- FIQ reported NYET. Data may have been lost.\n",
  45450. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  45451. + if (hc->ep_type == UE_ISOCHRONOUS) {
  45452. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45453. + /* Record errors, update qtd. */
  45454. + frame_desc->actual_length = 0;
  45455. + frame_desc->status = -DWC_E_PROTOCOL;
  45456. + qtd->isoc_frame_index++;
  45457. + qtd->isoc_split_offset = 0;
  45458. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45459. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45460. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45461. + } else {
  45462. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45463. + }
  45464. + } else {
  45465. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45466. + }
  45467. + break;
  45468. +
  45469. + case FIQ_HS_ISOC_DONE:
  45470. + /* The FIQ has performed a whole pile of isochronous transactions.
  45471. + * The status is recorded as the interrupt state should the transaction
  45472. + * fail.
  45473. + */
  45474. + dwc_otg_fiq_unmangle_isoc(hcd, hc->qh, qtd, num);
  45475. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45476. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45477. + break;
  45478. +
  45479. + case FIQ_PER_SPLIT_LS_ABORTED:
  45480. + if (hcint.b.xacterr) {
  45481. + /* Hub has responded with an ERR packet. Device
  45482. + * has been unplugged or the port has been disabled.
  45483. + * TODO: need to issue a reset to the hub port. */
  45484. + qtd->error_count += 3;
  45485. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45486. + } else if (hcint.b.stall) {
  45487. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45488. + } else if (hcint.b.bblerr) {
  45489. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45490. + } else {
  45491. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
  45492. + "- FIQ reported FSM=%d. Data may have been lost.\n",
  45493. + st->fsm, hc->dev_addr, hc->ep_num);
  45494. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45495. + }
  45496. + break;
  45497. +
  45498. + case FIQ_PER_SPLIT_HS_ABORTED:
  45499. + /* Either the SSPLIT phase suffered transaction errors or something
  45500. + * unexpected happened.
  45501. + */
  45502. + qtd->error_count += 3;
  45503. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45504. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45505. + break;
  45506. +
  45507. + case FIQ_PER_SPLIT_TIMEOUT:
  45508. + /* Couldn't complete in the nominated frame */
  45509. + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  45510. + "- FIQ timed out. Data may have been lost.\n",
  45511. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  45512. + if (hc->ep_type == UE_ISOCHRONOUS) {
  45513. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45514. + /* Record errors, update qtd. */
  45515. + frame_desc->actual_length = 0;
  45516. + if (hc->ep_is_in) {
  45517. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  45518. + } else {
  45519. + frame_desc->status = -DWC_E_COMMUNICATION;
  45520. + }
  45521. + qtd->isoc_frame_index++;
  45522. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45523. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45524. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45525. + } else {
  45526. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45527. + }
  45528. + } else {
  45529. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45530. + }
  45531. + break;
  45532. +
  45533. + default:
  45534. + DWC_WARN("Unexpected state received on hc=%d fsm=%d on transfer to device %d ep 0x%x",
  45535. + hc->hc_num, st->fsm, hc->dev_addr, hc->ep_num);
  45536. + qtd->error_count++;
  45537. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45538. + }
  45539. + return;
  45540. +}
  45541. +
  45542. +/** Handles interrupt for a specific Host Channel */
  45543. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  45544. +{
  45545. + int retval = 0;
  45546. + hcint_data_t hcint;
  45547. + hcintmsk_data_t hcintmsk;
  45548. + dwc_hc_t *hc;
  45549. + dwc_otg_hc_regs_t *hc_regs;
  45550. + dwc_otg_qtd_t *qtd;
  45551. +
  45552. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  45553. +
  45554. + hc = dwc_otg_hcd->hc_ptr_array[num];
  45555. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  45556. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  45557. + /* A dequeue was issued for this transfer. Our QTD has gone away
  45558. + * but in the case of a FIQ transfer, the transfer would have run
  45559. + * to completion.
  45560. + */
  45561. + if (fiq_fsm_enable && dwc_otg_hcd->fiq_state->channel[num].fsm != FIQ_PASSTHROUGH) {
  45562. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  45563. + } else {
  45564. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  45565. + }
  45566. + return 1;
  45567. + }
  45568. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  45569. +
  45570. + /*
  45571. + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
  45572. + * Execution path is fundamentally different for the channels after a FIQ has completed
  45573. + * a split transaction.
  45574. + */
  45575. + if (fiq_fsm_enable) {
  45576. + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
  45577. + case FIQ_PASSTHROUGH:
  45578. + break;
  45579. + case FIQ_PASSTHROUGH_ERRORSTATE:
  45580. + /* Hook into the error count */
  45581. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
  45582. + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
  45583. + qtd->error_count = 0;
  45584. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
  45585. + }
  45586. + break;
  45587. + default:
  45588. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  45589. + return 1;
  45590. + }
  45591. + }
  45592. +
  45593. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  45594. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  45595. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  45596. + if (!dwc_otg_hcd->core_if->dma_enable) {
  45597. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  45598. + hcint.b.chhltd = 0;
  45599. + }
  45600. + }
  45601. +
  45602. + if (hcint.b.xfercomp) {
  45603. + retval |=
  45604. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45605. + /*
  45606. + * If NYET occurred at same time as Xfer Complete, the NYET is
  45607. + * handled by the Xfer Complete interrupt handler. Don't want
  45608. + * to call the NYET interrupt handler in this case.
  45609. + */
  45610. + hcint.b.nyet = 0;
  45611. + }
  45612. + if (hcint.b.chhltd) {
  45613. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45614. + }
  45615. + if (hcint.b.ahberr) {
  45616. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45617. + }
  45618. + if (hcint.b.stall) {
  45619. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45620. + }
  45621. + if (hcint.b.nak) {
  45622. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45623. + }
  45624. + if (hcint.b.ack) {
  45625. + if(!hcint.b.chhltd)
  45626. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45627. + }
  45628. + if (hcint.b.nyet) {
  45629. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45630. + }
  45631. + if (hcint.b.xacterr) {
  45632. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45633. + }
  45634. + if (hcint.b.bblerr) {
  45635. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45636. + }
  45637. + if (hcint.b.frmovrun) {
  45638. + retval |=
  45639. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45640. + }
  45641. + if (hcint.b.datatglerr) {
  45642. + retval |=
  45643. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45644. + }
  45645. +
  45646. + return retval;
  45647. +}
  45648. +#endif /* DWC_DEVICE_ONLY */
  45649. --- /dev/null
  45650. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  45651. @@ -0,0 +1,1007 @@
  45652. +
  45653. +/* ==========================================================================
  45654. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  45655. + * $Revision: #20 $
  45656. + * $Date: 2011/10/26 $
  45657. + * $Change: 1872981 $
  45658. + *
  45659. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  45660. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  45661. + * otherwise expressly agreed to in writing between Synopsys and you.
  45662. + *
  45663. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  45664. + * any End User Software License Agreement or Agreement for Licensed Product
  45665. + * with Synopsys or any supplement thereto. You are permitted to use and
  45666. + * redistribute this Software in source and binary forms, with or without
  45667. + * modification, provided that redistributions of source code must retain this
  45668. + * notice. You may not view, use, disclose, copy or distribute this file or
  45669. + * any information contained herein except pursuant to this license grant from
  45670. + * Synopsys. If you do not agree with this notice, including the disclaimer
  45671. + * below, then you are not authorized to use the Software.
  45672. + *
  45673. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  45674. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45675. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45676. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  45677. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  45678. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45679. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45680. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45681. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45682. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  45683. + * DAMAGE.
  45684. + * ========================================================================== */
  45685. +#ifndef DWC_DEVICE_ONLY
  45686. +
  45687. +/**
  45688. + * @file
  45689. + *
  45690. + * This file contains the implementation of the HCD. In Linux, the HCD
  45691. + * implements the hc_driver API.
  45692. + */
  45693. +#include <linux/kernel.h>
  45694. +#include <linux/module.h>
  45695. +#include <linux/moduleparam.h>
  45696. +#include <linux/init.h>
  45697. +#include <linux/device.h>
  45698. +#include <linux/errno.h>
  45699. +#include <linux/list.h>
  45700. +#include <linux/interrupt.h>
  45701. +#include <linux/string.h>
  45702. +#include <linux/dma-mapping.h>
  45703. +#include <linux/version.h>
  45704. +#include <asm/io.h>
  45705. +#include <asm/fiq.h>
  45706. +#include <linux/usb.h>
  45707. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  45708. +#include <../drivers/usb/core/hcd.h>
  45709. +#else
  45710. +#include <linux/usb/hcd.h>
  45711. +#endif
  45712. +#include <asm/bug.h>
  45713. +
  45714. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  45715. +#define USB_URB_EP_LINKING 1
  45716. +#else
  45717. +#define USB_URB_EP_LINKING 0
  45718. +#endif
  45719. +
  45720. +#include "dwc_otg_hcd_if.h"
  45721. +#include "dwc_otg_dbg.h"
  45722. +#include "dwc_otg_driver.h"
  45723. +#include "dwc_otg_hcd.h"
  45724. +
  45725. +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
  45726. +
  45727. +/**
  45728. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  45729. + * qualified with its direction (possible 32 endpoints per device).
  45730. + */
  45731. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  45732. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  45733. +
  45734. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  45735. +
  45736. +extern bool fiq_enable;
  45737. +
  45738. +/** @name Linux HC Driver API Functions */
  45739. +/** @{ */
  45740. +/* manage i/o requests, device state */
  45741. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  45742. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45743. + struct usb_host_endpoint *ep,
  45744. +#endif
  45745. + struct urb *urb, gfp_t mem_flags);
  45746. +
  45747. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  45748. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45749. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  45750. +#endif
  45751. +#else /* kernels at or post 2.6.30 */
  45752. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  45753. + struct urb *urb, int status);
  45754. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  45755. +
  45756. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  45757. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  45758. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  45759. +#endif
  45760. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  45761. +extern int hcd_start(struct usb_hcd *hcd);
  45762. +extern void hcd_stop(struct usb_hcd *hcd);
  45763. +static int get_frame_number(struct usb_hcd *hcd);
  45764. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  45765. +extern int hub_control(struct usb_hcd *hcd,
  45766. + u16 typeReq,
  45767. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  45768. +
  45769. +struct wrapper_priv_data {
  45770. + dwc_otg_hcd_t *dwc_otg_hcd;
  45771. +};
  45772. +
  45773. +/** @} */
  45774. +
  45775. +static struct hc_driver dwc_otg_hc_driver = {
  45776. +
  45777. + .description = dwc_otg_hcd_name,
  45778. + .product_desc = "DWC OTG Controller",
  45779. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  45780. +
  45781. + .irq = dwc_otg_hcd_irq,
  45782. +
  45783. + .flags = HCD_MEMORY | HCD_USB2,
  45784. +
  45785. + //.reset =
  45786. + .start = hcd_start,
  45787. + //.suspend =
  45788. + //.resume =
  45789. + .stop = hcd_stop,
  45790. +
  45791. + .urb_enqueue = dwc_otg_urb_enqueue,
  45792. + .urb_dequeue = dwc_otg_urb_dequeue,
  45793. + .endpoint_disable = endpoint_disable,
  45794. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  45795. + .endpoint_reset = endpoint_reset,
  45796. +#endif
  45797. + .get_frame_number = get_frame_number,
  45798. +
  45799. + .hub_status_data = hub_status_data,
  45800. + .hub_control = hub_control,
  45801. + //.bus_suspend =
  45802. + //.bus_resume =
  45803. +};
  45804. +
  45805. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  45806. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  45807. +{
  45808. + struct wrapper_priv_data *p;
  45809. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  45810. + return p->dwc_otg_hcd;
  45811. +}
  45812. +
  45813. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  45814. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  45815. +{
  45816. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  45817. +}
  45818. +
  45819. +/** Gets the usb_host_endpoint associated with an URB. */
  45820. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  45821. +{
  45822. + struct usb_device *dev = urb->dev;
  45823. + int ep_num = usb_pipeendpoint(urb->pipe);
  45824. +
  45825. + if (usb_pipein(urb->pipe))
  45826. + return dev->ep_in[ep_num];
  45827. + else
  45828. + return dev->ep_out[ep_num];
  45829. +}
  45830. +
  45831. +static int _disconnect(dwc_otg_hcd_t * hcd)
  45832. +{
  45833. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45834. +
  45835. + usb_hcd->self.is_b_host = 0;
  45836. + return 0;
  45837. +}
  45838. +
  45839. +static int _start(dwc_otg_hcd_t * hcd)
  45840. +{
  45841. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45842. +
  45843. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  45844. + hcd_start(usb_hcd);
  45845. +
  45846. + return 0;
  45847. +}
  45848. +
  45849. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  45850. + uint32_t * port_addr)
  45851. +{
  45852. + struct urb *urb = (struct urb *)urb_handle;
  45853. + struct usb_bus *bus;
  45854. +#if 1 //GRAYG - temporary
  45855. + if (NULL == urb_handle)
  45856. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  45857. + if (NULL == urb->dev)
  45858. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  45859. + if (NULL == port_addr)
  45860. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  45861. +#endif
  45862. + if (urb->dev->tt) {
  45863. + if (NULL == urb->dev->tt->hub) {
  45864. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  45865. + __func__); //GRAYG
  45866. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  45867. + *hub_addr = 0; //GRAYG
  45868. + // we probably shouldn't have a transaction translator if
  45869. + // there's no associated hub?
  45870. + } else {
  45871. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  45872. + if (urb->dev->tt->hub == bus->root_hub)
  45873. + *hub_addr = 0;
  45874. + else
  45875. + *hub_addr = urb->dev->tt->hub->devnum;
  45876. + }
  45877. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  45878. + } else {
  45879. + *hub_addr = 0;
  45880. + *port_addr = urb->dev->ttport;
  45881. + }
  45882. + return 0;
  45883. +}
  45884. +
  45885. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  45886. +{
  45887. + struct urb *urb = (struct urb *)urb_handle;
  45888. + return urb->dev->speed;
  45889. +}
  45890. +
  45891. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  45892. +{
  45893. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45894. + return usb_hcd->self.b_hnp_enable;
  45895. +}
  45896. +
  45897. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  45898. + struct urb *urb)
  45899. +{
  45900. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  45901. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45902. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  45903. + } else {
  45904. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  45905. + }
  45906. +}
  45907. +
  45908. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  45909. + struct urb *urb)
  45910. +{
  45911. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  45912. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45913. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  45914. + } else {
  45915. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  45916. + }
  45917. +}
  45918. +
  45919. +/**
  45920. + * Sets the final status of an URB and returns it to the device driver. Any
  45921. + * required cleanup of the URB is performed. The HCD lock should be held on
  45922. + * entry.
  45923. + */
  45924. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  45925. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  45926. +{
  45927. + struct urb *urb = (struct urb *)urb_handle;
  45928. + urb_tq_entry_t *new_entry;
  45929. + int rc = 0;
  45930. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  45931. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  45932. + __func__, urb, usb_pipedevice(urb->pipe),
  45933. + usb_pipeendpoint(urb->pipe),
  45934. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  45935. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45936. + int i;
  45937. + for (i = 0; i < urb->number_of_packets; i++) {
  45938. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  45939. + i, urb->iso_frame_desc[i].status);
  45940. + }
  45941. + }
  45942. + }
  45943. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  45944. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  45945. + /* Convert status value. */
  45946. + switch (status) {
  45947. + case -DWC_E_PROTOCOL:
  45948. + status = -EPROTO;
  45949. + break;
  45950. + case -DWC_E_IN_PROGRESS:
  45951. + status = -EINPROGRESS;
  45952. + break;
  45953. + case -DWC_E_PIPE:
  45954. + status = -EPIPE;
  45955. + break;
  45956. + case -DWC_E_IO:
  45957. + status = -EIO;
  45958. + break;
  45959. + case -DWC_E_TIMEOUT:
  45960. + status = -ETIMEDOUT;
  45961. + break;
  45962. + case -DWC_E_OVERFLOW:
  45963. + status = -EOVERFLOW;
  45964. + break;
  45965. + case -DWC_E_SHUTDOWN:
  45966. + status = -ESHUTDOWN;
  45967. + break;
  45968. + default:
  45969. + if (status) {
  45970. + DWC_PRINTF("Uknown urb status %d\n", status);
  45971. +
  45972. + }
  45973. + }
  45974. +
  45975. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45976. + int i;
  45977. +
  45978. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  45979. + urb->actual_length = 0;
  45980. + for (i = 0; i < urb->number_of_packets; ++i) {
  45981. + urb->iso_frame_desc[i].actual_length =
  45982. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  45983. + (dwc_otg_urb, i);
  45984. + urb->actual_length += urb->iso_frame_desc[i].actual_length;
  45985. + urb->iso_frame_desc[i].status =
  45986. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  45987. + }
  45988. + }
  45989. +
  45990. + urb->status = status;
  45991. + urb->hcpriv = NULL;
  45992. + if (!status) {
  45993. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  45994. + (urb->actual_length < urb->transfer_buffer_length)) {
  45995. + urb->status = -EREMOTEIO;
  45996. + }
  45997. + }
  45998. +
  45999. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  46000. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  46001. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  46002. + if (ep) {
  46003. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  46004. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  46005. + ep->hcpriv),
  46006. + urb);
  46007. + }
  46008. + }
  46009. + DWC_FREE(dwc_otg_urb);
  46010. + if (!new_entry) {
  46011. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  46012. + urb->status = -EPROTO;
  46013. + /* don't schedule the tasklet -
  46014. + * directly return the packet here with error. */
  46015. +#if USB_URB_EP_LINKING
  46016. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  46017. +#endif
  46018. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46019. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  46020. +#else
  46021. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  46022. +#endif
  46023. + } else {
  46024. + new_entry->urb = urb;
  46025. +#if USB_URB_EP_LINKING
  46026. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  46027. + if(0 == rc) {
  46028. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  46029. + }
  46030. +#endif
  46031. + if(0 == rc) {
  46032. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  46033. + urb_tq_entries);
  46034. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  46035. + }
  46036. + }
  46037. + return 0;
  46038. +}
  46039. +
  46040. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  46041. + .start = _start,
  46042. + .disconnect = _disconnect,
  46043. + .hub_info = _hub_info,
  46044. + .speed = _speed,
  46045. + .complete = _complete,
  46046. + .get_b_hnp_enable = _get_b_hnp_enable,
  46047. +};
  46048. +
  46049. +static struct fiq_handler fh = {
  46050. + .name = "usb_fiq",
  46051. +};
  46052. +
  46053. +static void hcd_init_fiq(void *cookie)
  46054. +{
  46055. + dwc_otg_device_t *otg_dev = cookie;
  46056. + dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
  46057. + struct pt_regs regs;
  46058. + int irq;
  46059. +
  46060. + if (claim_fiq(&fh)) {
  46061. + DWC_ERROR("Can't claim FIQ");
  46062. + BUG();
  46063. + }
  46064. + DWC_WARN("FIQ on core %d at 0x%08x",
  46065. + smp_processor_id(),
  46066. + (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
  46067. + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
  46068. + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
  46069. + memset(&regs,0,sizeof(regs));
  46070. +
  46071. + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
  46072. + if (fiq_fsm_enable) {
  46073. + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
  46074. + //regs.ARM_r10 = dwc_otg_hcd->dma;
  46075. + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
  46076. + } else {
  46077. + regs.ARM_fp = (long) dwc_otg_fiq_nop;
  46078. + }
  46079. +
  46080. + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
  46081. +
  46082. +// __show_regs(&regs);
  46083. + set_fiq_regs(&regs);
  46084. +
  46085. + //Set the mphi periph to the required registers
  46086. + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
  46087. + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  46088. + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  46089. + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  46090. + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  46091. + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
  46092. + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
  46093. + //Enable mphi peripheral
  46094. + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
  46095. +#ifdef DEBUG
  46096. + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
  46097. + DWC_WARN("MPHI periph has been enabled");
  46098. + else
  46099. + DWC_WARN("MPHI periph has NOT been enabled");
  46100. +#endif
  46101. + // Enable FIQ interrupt from USB peripheral
  46102. +#ifdef CONFIG_MULTI_IRQ_HANDLER
  46103. + irq = platform_get_irq(otg_dev->os_dep.platformdev, 1);
  46104. +#else
  46105. + irq = INTERRUPT_VC_USB;
  46106. +#endif
  46107. + if (irq < 0) {
  46108. + DWC_ERROR("Can't get FIQ irq");
  46109. + return;
  46110. + }
  46111. + enable_fiq(irq);
  46112. + local_fiq_enable();
  46113. +}
  46114. +
  46115. +/**
  46116. + * Initializes the HCD. This function allocates memory for and initializes the
  46117. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  46118. + * USB bus with the core and calls the hc_driver->start() function. It returns
  46119. + * a negative error on failure.
  46120. + */
  46121. +int hcd_init(dwc_bus_dev_t *_dev)
  46122. +{
  46123. + struct usb_hcd *hcd = NULL;
  46124. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  46125. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  46126. + int retval = 0;
  46127. + u64 dmamask;
  46128. +
  46129. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  46130. +
  46131. + /* Set device flags indicating whether the HCD supports DMA. */
  46132. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  46133. + dmamask = DMA_BIT_MASK(32);
  46134. + else
  46135. + dmamask = 0;
  46136. +
  46137. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  46138. + dma_set_mask(&_dev->dev, dmamask);
  46139. + dma_set_coherent_mask(&_dev->dev, dmamask);
  46140. +#elif defined(PCI_INTERFACE)
  46141. + pci_set_dma_mask(_dev, dmamask);
  46142. + pci_set_consistent_dma_mask(_dev, dmamask);
  46143. +#endif
  46144. +
  46145. + /*
  46146. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  46147. + * Initialize the base HCD.
  46148. + */
  46149. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  46150. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  46151. +#else
  46152. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  46153. + hcd->has_tt = 1;
  46154. +// hcd->uses_new_polling = 1;
  46155. +// hcd->poll_rh = 0;
  46156. +#endif
  46157. + if (!hcd) {
  46158. + retval = -ENOMEM;
  46159. + goto error1;
  46160. + }
  46161. +
  46162. + hcd->regs = otg_dev->os_dep.base;
  46163. +
  46164. +
  46165. + /* Initialize the DWC OTG HCD. */
  46166. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  46167. + if (!dwc_otg_hcd) {
  46168. + goto error2;
  46169. + }
  46170. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  46171. + dwc_otg_hcd;
  46172. + otg_dev->hcd = dwc_otg_hcd;
  46173. + otg_dev->hcd->otg_dev = otg_dev;
  46174. +
  46175. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  46176. + goto error2;
  46177. + }
  46178. +
  46179. + if (fiq_enable) {
  46180. + if (num_online_cpus() > 1) {
  46181. + /* bcm2709: can run the FIQ on a separate core to IRQs */
  46182. + smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
  46183. + } else {
  46184. + smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
  46185. + }
  46186. + }
  46187. +
  46188. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  46189. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  46190. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  46191. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  46192. +#endif
  46193. + /* Don't support SG list at this point */
  46194. + hcd->self.sg_tablesize = 0;
  46195. +#endif
  46196. + /*
  46197. + * Finish generic HCD initialization and start the HCD. This function
  46198. + * allocates the DMA buffer pool, registers the USB bus, requests the
  46199. + * IRQ line, and calls hcd_start method.
  46200. + */
  46201. +#ifdef PLATFORM_INTERFACE
  46202. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED);
  46203. +#else
  46204. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED);
  46205. +#endif
  46206. + if (retval < 0) {
  46207. + goto error2;
  46208. + }
  46209. +
  46210. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  46211. + return 0;
  46212. +
  46213. +error2:
  46214. + usb_put_hcd(hcd);
  46215. +error1:
  46216. + return retval;
  46217. +}
  46218. +
  46219. +/**
  46220. + * Removes the HCD.
  46221. + * Frees memory and resources associated with the HCD and deregisters the bus.
  46222. + */
  46223. +void hcd_remove(dwc_bus_dev_t *_dev)
  46224. +{
  46225. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  46226. + dwc_otg_hcd_t *dwc_otg_hcd;
  46227. + struct usb_hcd *hcd;
  46228. +
  46229. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  46230. +
  46231. + if (!otg_dev) {
  46232. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  46233. + return;
  46234. + }
  46235. +
  46236. + dwc_otg_hcd = otg_dev->hcd;
  46237. +
  46238. + if (!dwc_otg_hcd) {
  46239. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  46240. + return;
  46241. + }
  46242. +
  46243. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  46244. +
  46245. + if (!hcd) {
  46246. + DWC_DEBUGPL(DBG_ANY,
  46247. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  46248. + __func__);
  46249. + return;
  46250. + }
  46251. + usb_remove_hcd(hcd);
  46252. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  46253. + dwc_otg_hcd_remove(dwc_otg_hcd);
  46254. + usb_put_hcd(hcd);
  46255. +}
  46256. +
  46257. +/* =========================================================================
  46258. + * Linux HC Driver Functions
  46259. + * ========================================================================= */
  46260. +
  46261. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  46262. + * mode operation. Activates the root port. Returns 0 on success and a negative
  46263. + * error code on failure. */
  46264. +int hcd_start(struct usb_hcd *hcd)
  46265. +{
  46266. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46267. + struct usb_bus *bus;
  46268. +
  46269. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  46270. + bus = hcd_to_bus(hcd);
  46271. +
  46272. + hcd->state = HC_STATE_RUNNING;
  46273. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  46274. + return 0;
  46275. + }
  46276. +
  46277. + /* Initialize and connect root hub if one is not already attached */
  46278. + if (bus->root_hub) {
  46279. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  46280. + /* Inform the HUB driver to resume. */
  46281. + usb_hcd_resume_root_hub(hcd);
  46282. + }
  46283. +
  46284. + return 0;
  46285. +}
  46286. +
  46287. +/**
  46288. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  46289. + * stopped.
  46290. + */
  46291. +void hcd_stop(struct usb_hcd *hcd)
  46292. +{
  46293. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46294. +
  46295. + dwc_otg_hcd_stop(dwc_otg_hcd);
  46296. +}
  46297. +
  46298. +/** Returns the current frame number. */
  46299. +static int get_frame_number(struct usb_hcd *hcd)
  46300. +{
  46301. + hprt0_data_t hprt0;
  46302. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46303. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  46304. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  46305. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
  46306. + else
  46307. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  46308. +}
  46309. +
  46310. +#ifdef DEBUG
  46311. +static void dump_urb_info(struct urb *urb, char *fn_name)
  46312. +{
  46313. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  46314. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  46315. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  46316. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  46317. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  46318. + char *pipetype;
  46319. + switch (usb_pipetype(urb->pipe)) {
  46320. +case PIPE_CONTROL:
  46321. +pipetype = "CONTROL"; break; case PIPE_BULK:
  46322. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  46323. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  46324. +pipetype = "ISOCHRONOUS"; break; default:
  46325. + pipetype = "UNKNOWN"; break;};
  46326. + pipetype;}
  46327. + )) ;
  46328. + DWC_PRINTF(" Speed: %s\n", ( {
  46329. + char *speed; switch (urb->dev->speed) {
  46330. +case USB_SPEED_HIGH:
  46331. +speed = "HIGH"; break; case USB_SPEED_FULL:
  46332. +speed = "FULL"; break; case USB_SPEED_LOW:
  46333. +speed = "LOW"; break; default:
  46334. + speed = "UNKNOWN"; break;};
  46335. + speed;}
  46336. + )) ;
  46337. + DWC_PRINTF(" Max packet size: %d\n",
  46338. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  46339. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  46340. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  46341. + urb->transfer_buffer, (void *)urb->transfer_dma);
  46342. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  46343. + urb->setup_packet, (void *)urb->setup_dma);
  46344. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  46345. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  46346. + int i;
  46347. + for (i = 0; i < urb->number_of_packets; i++) {
  46348. + DWC_PRINTF(" ISO Desc %d:\n", i);
  46349. + DWC_PRINTF(" offset: %d, length %d\n",
  46350. + urb->iso_frame_desc[i].offset,
  46351. + urb->iso_frame_desc[i].length);
  46352. + }
  46353. + }
  46354. +}
  46355. +#endif
  46356. +
  46357. +/** Starts processing a USB transfer request specified by a USB Request Block
  46358. + * (URB). mem_flags indicates the type of memory allocation to use while
  46359. + * processing this URB. */
  46360. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  46361. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46362. + struct usb_host_endpoint *ep,
  46363. +#endif
  46364. + struct urb *urb, gfp_t mem_flags)
  46365. +{
  46366. + int retval = 0;
  46367. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  46368. + struct usb_host_endpoint *ep = urb->ep;
  46369. +#endif
  46370. + dwc_irqflags_t irqflags;
  46371. + void **ref_ep_hcpriv = &ep->hcpriv;
  46372. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46373. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  46374. + int i;
  46375. + int alloc_bandwidth = 0;
  46376. + uint8_t ep_type = 0;
  46377. + uint32_t flags = 0;
  46378. + void *buf;
  46379. +
  46380. +#ifdef DEBUG
  46381. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46382. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  46383. + }
  46384. +#endif
  46385. +
  46386. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  46387. + return -EINVAL;
  46388. +
  46389. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  46390. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  46391. + if (!dwc_otg_hcd_is_bandwidth_allocated
  46392. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  46393. + alloc_bandwidth = 1;
  46394. + }
  46395. + }
  46396. +
  46397. + switch (usb_pipetype(urb->pipe)) {
  46398. + case PIPE_CONTROL:
  46399. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  46400. + break;
  46401. + case PIPE_ISOCHRONOUS:
  46402. + ep_type = USB_ENDPOINT_XFER_ISOC;
  46403. + break;
  46404. + case PIPE_BULK:
  46405. + ep_type = USB_ENDPOINT_XFER_BULK;
  46406. + break;
  46407. + case PIPE_INTERRUPT:
  46408. + ep_type = USB_ENDPOINT_XFER_INT;
  46409. + break;
  46410. + default:
  46411. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  46412. + }
  46413. +
  46414. + /* # of packets is often 0 - do we really need to call this then? */
  46415. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  46416. + urb->number_of_packets,
  46417. + mem_flags == GFP_ATOMIC ? 1 : 0);
  46418. +
  46419. + if(dwc_otg_urb == NULL)
  46420. + return -ENOMEM;
  46421. +
  46422. + if (!dwc_otg_urb && urb->number_of_packets)
  46423. + return -ENOMEM;
  46424. +
  46425. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  46426. + usb_pipeendpoint(urb->pipe), ep_type,
  46427. + usb_pipein(urb->pipe),
  46428. + usb_maxpacket(urb->dev, urb->pipe,
  46429. + !(usb_pipein(urb->pipe))));
  46430. +
  46431. + buf = urb->transfer_buffer;
  46432. + if (hcd->self.uses_dma && !buf && urb->transfer_buffer_length) {
  46433. + /*
  46434. + * Calculate virtual address from physical address,
  46435. + * because some class driver may not fill transfer_buffer.
  46436. + * In Buffer DMA mode virual address is used,
  46437. + * when handling non DWORD aligned buffers.
  46438. + */
  46439. + buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
  46440. + dev_warn_once(&urb->dev->dev,
  46441. + "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
  46442. + &urb->transfer_dma, buf);
  46443. + }
  46444. +
  46445. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  46446. + flags |= URB_GIVEBACK_ASAP;
  46447. + if (urb->transfer_flags & URB_ZERO_PACKET)
  46448. + flags |= URB_SEND_ZERO_PACKET;
  46449. +
  46450. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  46451. + urb->transfer_dma,
  46452. + urb->transfer_buffer_length,
  46453. + urb->setup_packet,
  46454. + urb->setup_dma, flags, urb->interval);
  46455. +
  46456. + for (i = 0; i < urb->number_of_packets; ++i) {
  46457. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  46458. + urb->
  46459. + iso_frame_desc[i].offset,
  46460. + urb->
  46461. + iso_frame_desc[i].length);
  46462. + }
  46463. +
  46464. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  46465. + urb->hcpriv = dwc_otg_urb;
  46466. +#if USB_URB_EP_LINKING
  46467. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  46468. + if (0 == retval)
  46469. +#endif
  46470. + {
  46471. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  46472. + /*(dwc_otg_qh_t **)*/
  46473. + ref_ep_hcpriv, 1);
  46474. + if (0 == retval) {
  46475. + if (alloc_bandwidth) {
  46476. + allocate_bus_bandwidth(hcd,
  46477. + dwc_otg_hcd_get_ep_bandwidth(
  46478. + dwc_otg_hcd, *ref_ep_hcpriv),
  46479. + urb);
  46480. + }
  46481. + } else {
  46482. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  46483. +#if USB_URB_EP_LINKING
  46484. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  46485. +#endif
  46486. + DWC_FREE(dwc_otg_urb);
  46487. + urb->hcpriv = NULL;
  46488. + if (retval == -DWC_E_NO_DEVICE)
  46489. + retval = -ENODEV;
  46490. + }
  46491. + }
  46492. +#if USB_URB_EP_LINKING
  46493. + else
  46494. + {
  46495. + DWC_FREE(dwc_otg_urb);
  46496. + urb->hcpriv = NULL;
  46497. + }
  46498. +#endif
  46499. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  46500. + return retval;
  46501. +}
  46502. +
  46503. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  46504. + * success. */
  46505. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46506. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  46507. +#else
  46508. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  46509. +#endif
  46510. +{
  46511. + dwc_irqflags_t flags;
  46512. + dwc_otg_hcd_t *dwc_otg_hcd;
  46513. + int rc;
  46514. +
  46515. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  46516. +
  46517. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46518. +
  46519. +#ifdef DEBUG
  46520. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46521. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  46522. + }
  46523. +#endif
  46524. +
  46525. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  46526. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  46527. + if (0 == rc) {
  46528. + if(urb->hcpriv != NULL) {
  46529. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  46530. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  46531. +
  46532. + DWC_FREE(urb->hcpriv);
  46533. + urb->hcpriv = NULL;
  46534. + }
  46535. + }
  46536. +
  46537. + if (0 == rc) {
  46538. + /* Higher layer software sets URB status. */
  46539. +#if USB_URB_EP_LINKING
  46540. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  46541. +#endif
  46542. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46543. +
  46544. +
  46545. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46546. + usb_hcd_giveback_urb(hcd, urb);
  46547. +#else
  46548. + usb_hcd_giveback_urb(hcd, urb, status);
  46549. +#endif
  46550. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46551. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  46552. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  46553. + }
  46554. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  46555. + } else {
  46556. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46557. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  46558. + rc);
  46559. + }
  46560. +
  46561. + return rc;
  46562. +}
  46563. +
  46564. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  46565. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  46566. + * must already be dequeued. */
  46567. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  46568. +{
  46569. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46570. +
  46571. + DWC_DEBUGPL(DBG_HCD,
  46572. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  46573. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  46574. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  46575. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  46576. + ep->hcpriv = NULL;
  46577. +}
  46578. +
  46579. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  46580. +/* Resets endpoint specific parameter values, in current version used to reset
  46581. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  46582. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  46583. +{
  46584. + dwc_irqflags_t flags;
  46585. + struct usb_device *udev = NULL;
  46586. + int epnum = usb_endpoint_num(&ep->desc);
  46587. + int is_out = usb_endpoint_dir_out(&ep->desc);
  46588. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  46589. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46590. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  46591. +
  46592. + if (dev)
  46593. + udev = to_usb_device(dev);
  46594. + else
  46595. + return;
  46596. +
  46597. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  46598. +
  46599. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  46600. + usb_settoggle(udev, epnum, is_out, 0);
  46601. + if (is_control)
  46602. + usb_settoggle(udev, epnum, !is_out, 0);
  46603. +
  46604. + if (ep->hcpriv) {
  46605. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  46606. + }
  46607. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46608. +}
  46609. +#endif
  46610. +
  46611. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  46612. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  46613. + * interrupt.
  46614. + *
  46615. + * This function is called by the USB core when an interrupt occurs */
  46616. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  46617. +{
  46618. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46619. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  46620. + if (retval != 0) {
  46621. + S3C2410X_CLEAR_EINTPEND();
  46622. + }
  46623. + return IRQ_RETVAL(retval);
  46624. +}
  46625. +
  46626. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  46627. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  46628. + * is the status change indicator for the single root port. Returns 1 if either
  46629. + * change indicator is 1, otherwise returns 0. */
  46630. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  46631. +{
  46632. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46633. +
  46634. + buf[0] = 0;
  46635. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  46636. +
  46637. + return (buf[0] != 0);
  46638. +}
  46639. +
  46640. +/** Handles hub class-specific requests. */
  46641. +int hub_control(struct usb_hcd *hcd,
  46642. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  46643. +{
  46644. + int retval;
  46645. +
  46646. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  46647. + typeReq, wValue, wIndex, buf, wLength);
  46648. +
  46649. + switch (retval) {
  46650. + case -DWC_E_INVALID:
  46651. + retval = -EINVAL;
  46652. + break;
  46653. + }
  46654. +
  46655. + return retval;
  46656. +}
  46657. +
  46658. +#endif /* DWC_DEVICE_ONLY */
  46659. --- /dev/null
  46660. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  46661. @@ -0,0 +1,971 @@
  46662. +/* ==========================================================================
  46663. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  46664. + * $Revision: #44 $
  46665. + * $Date: 2011/10/26 $
  46666. + * $Change: 1873028 $
  46667. + *
  46668. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  46669. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  46670. + * otherwise expressly agreed to in writing between Synopsys and you.
  46671. + *
  46672. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  46673. + * any End User Software License Agreement or Agreement for Licensed Product
  46674. + * with Synopsys or any supplement thereto. You are permitted to use and
  46675. + * redistribute this Software in source and binary forms, with or without
  46676. + * modification, provided that redistributions of source code must retain this
  46677. + * notice. You may not view, use, disclose, copy or distribute this file or
  46678. + * any information contained herein except pursuant to this license grant from
  46679. + * Synopsys. If you do not agree with this notice, including the disclaimer
  46680. + * below, then you are not authorized to use the Software.
  46681. + *
  46682. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  46683. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46684. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46685. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  46686. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46687. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  46688. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46689. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46690. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46691. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46692. + * DAMAGE.
  46693. + * ========================================================================== */
  46694. +#ifndef DWC_DEVICE_ONLY
  46695. +
  46696. +/**
  46697. + * @file
  46698. + *
  46699. + * This file contains the functions to manage Queue Heads and Queue
  46700. + * Transfer Descriptors.
  46701. + */
  46702. +
  46703. +#include "dwc_otg_hcd.h"
  46704. +#include "dwc_otg_regs.h"
  46705. +
  46706. +extern bool microframe_schedule;
  46707. +extern unsigned short int_ep_interval_min;
  46708. +
  46709. +/**
  46710. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  46711. + * removed from a list. QTD list should already be empty if called from URB
  46712. + * Dequeue.
  46713. + *
  46714. + * @param hcd HCD instance.
  46715. + * @param qh The QH to free.
  46716. + */
  46717. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  46718. +{
  46719. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  46720. + dwc_irqflags_t flags;
  46721. + uint32_t buf_size = 0;
  46722. + uint8_t *align_buf_virt = NULL;
  46723. + dwc_dma_t align_buf_dma;
  46724. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  46725. +
  46726. + /* Free each QTD in the QTD list */
  46727. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  46728. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  46729. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  46730. + dwc_otg_hcd_qtd_free(qtd);
  46731. + }
  46732. +
  46733. + if (hcd->core_if->dma_desc_enable) {
  46734. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  46735. + } else if (qh->dw_align_buf) {
  46736. + if (qh->ep_type == UE_ISOCHRONOUS) {
  46737. + buf_size = 4096;
  46738. + } else {
  46739. + buf_size = hcd->core_if->core_params->max_transfer_size;
  46740. + }
  46741. + align_buf_virt = qh->dw_align_buf;
  46742. + align_buf_dma = qh->dw_align_buf_dma;
  46743. + }
  46744. +
  46745. + DWC_FREE(qh);
  46746. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  46747. + if (align_buf_virt)
  46748. + DWC_DMA_FREE(dev, buf_size, align_buf_virt, align_buf_dma);
  46749. + return;
  46750. +}
  46751. +
  46752. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  46753. +#define HS_HOST_DELAY 5 /* nanoseconds */
  46754. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  46755. +#define HUB_LS_SETUP 333 /* nanoseconds */
  46756. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  46757. + /* convert & round nanoseconds to microseconds */
  46758. +
  46759. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  46760. +{
  46761. + unsigned long retval;
  46762. +
  46763. + switch (speed) {
  46764. + case USB_SPEED_HIGH:
  46765. + if (is_isoc) {
  46766. + retval =
  46767. + ((38 * 8 * 2083) +
  46768. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  46769. + HS_HOST_DELAY;
  46770. + } else {
  46771. + retval =
  46772. + ((55 * 8 * 2083) +
  46773. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  46774. + HS_HOST_DELAY;
  46775. + }
  46776. + break;
  46777. + case USB_SPEED_FULL:
  46778. + if (is_isoc) {
  46779. + retval =
  46780. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  46781. + if (is_in) {
  46782. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  46783. + } else {
  46784. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  46785. + }
  46786. + } else {
  46787. + retval =
  46788. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  46789. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  46790. + }
  46791. + break;
  46792. + case USB_SPEED_LOW:
  46793. + if (is_in) {
  46794. + retval =
  46795. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  46796. + 1000;
  46797. + retval =
  46798. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  46799. + retval;
  46800. + } else {
  46801. + retval =
  46802. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  46803. + 1000;
  46804. + retval =
  46805. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  46806. + retval;
  46807. + }
  46808. + break;
  46809. + default:
  46810. + DWC_WARN("Unknown device speed\n");
  46811. + retval = -1;
  46812. + }
  46813. +
  46814. + return NS_TO_US(retval);
  46815. +}
  46816. +
  46817. +/**
  46818. + * Initializes a QH structure.
  46819. + *
  46820. + * @param hcd The HCD state structure for the DWC OTG controller.
  46821. + * @param qh The QH to init.
  46822. + * @param urb Holds the information about the device/endpoint that we need
  46823. + * to initialize the QH.
  46824. + */
  46825. +#define SCHEDULE_SLOP 10
  46826. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  46827. +{
  46828. + char *speed, *type;
  46829. + int dev_speed;
  46830. + uint32_t hub_addr, hub_port;
  46831. +
  46832. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  46833. +
  46834. + /* Initialize QH */
  46835. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  46836. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  46837. +
  46838. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  46839. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  46840. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  46841. + DWC_LIST_INIT(&qh->qh_list_entry);
  46842. + qh->channel = NULL;
  46843. +
  46844. + /* FS/LS Enpoint on HS Hub
  46845. + * NOT virtual root hub */
  46846. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  46847. +
  46848. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  46849. + qh->do_split = 0;
  46850. + if (microframe_schedule)
  46851. + qh->speed = dev_speed;
  46852. +
  46853. + qh->nak_frame = 0xffff;
  46854. +
  46855. + if (((dev_speed == USB_SPEED_LOW) ||
  46856. + (dev_speed == USB_SPEED_FULL)) &&
  46857. + (hub_addr != 0 && hub_addr != 1)) {
  46858. + DWC_DEBUGPL(DBG_HCD,
  46859. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  46860. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  46861. + hub_port);
  46862. + qh->do_split = 1;
  46863. + qh->skip_count = 0;
  46864. + }
  46865. +
  46866. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  46867. + /* Compute scheduling parameters once and save them. */
  46868. + hprt0_data_t hprt;
  46869. +
  46870. + /** @todo Account for split transfers in the bus time. */
  46871. + int bytecount =
  46872. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  46873. +
  46874. + qh->usecs =
  46875. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  46876. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  46877. + bytecount);
  46878. + /* Start in a slightly future (micro)frame. */
  46879. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  46880. + SCHEDULE_SLOP);
  46881. + qh->interval = urb->interval;
  46882. +
  46883. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  46884. + if (hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) {
  46885. + if (dev_speed == USB_SPEED_LOW ||
  46886. + dev_speed == USB_SPEED_FULL) {
  46887. + qh->interval *= 8;
  46888. + qh->sched_frame |= 0x7;
  46889. + qh->start_split_frame = qh->sched_frame;
  46890. + } else if (int_ep_interval_min >= 2 &&
  46891. + qh->interval < int_ep_interval_min &&
  46892. + qh->ep_type == UE_INTERRUPT) {
  46893. + qh->interval = int_ep_interval_min;
  46894. + }
  46895. + }
  46896. + }
  46897. +
  46898. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  46899. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  46900. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  46901. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  46902. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  46903. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  46904. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  46905. + switch (dev_speed) {
  46906. + case USB_SPEED_LOW:
  46907. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  46908. + speed = "low";
  46909. + break;
  46910. + case USB_SPEED_FULL:
  46911. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  46912. + speed = "full";
  46913. + break;
  46914. + case USB_SPEED_HIGH:
  46915. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  46916. + speed = "high";
  46917. + break;
  46918. + default:
  46919. + speed = "?";
  46920. + break;
  46921. + }
  46922. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  46923. +
  46924. + switch (qh->ep_type) {
  46925. + case UE_ISOCHRONOUS:
  46926. + type = "isochronous";
  46927. + break;
  46928. + case UE_INTERRUPT:
  46929. + type = "interrupt";
  46930. + break;
  46931. + case UE_CONTROL:
  46932. + type = "control";
  46933. + break;
  46934. + case UE_BULK:
  46935. + type = "bulk";
  46936. + break;
  46937. + default:
  46938. + type = "?";
  46939. + break;
  46940. + }
  46941. +
  46942. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  46943. +
  46944. +#ifdef DEBUG
  46945. + if (qh->ep_type == UE_INTERRUPT) {
  46946. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  46947. + qh->usecs);
  46948. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  46949. + qh->interval);
  46950. + }
  46951. +#endif
  46952. +
  46953. +}
  46954. +
  46955. +/**
  46956. + * This function allocates and initializes a QH.
  46957. + *
  46958. + * @param hcd The HCD state structure for the DWC OTG controller.
  46959. + * @param urb Holds the information about the device/endpoint that we need
  46960. + * to initialize the QH.
  46961. + * @param atomic_alloc Flag to do atomic allocation if needed
  46962. + *
  46963. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  46964. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  46965. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  46966. +{
  46967. + dwc_otg_qh_t *qh;
  46968. +
  46969. + /* Allocate memory */
  46970. + /** @todo add memflags argument */
  46971. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  46972. + if (qh == NULL) {
  46973. + DWC_ERROR("qh allocation failed");
  46974. + return NULL;
  46975. + }
  46976. +
  46977. + qh_init(hcd, qh, urb);
  46978. +
  46979. + if (hcd->core_if->dma_desc_enable
  46980. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  46981. + dwc_otg_hcd_qh_free(hcd, qh);
  46982. + return NULL;
  46983. + }
  46984. +
  46985. + return qh;
  46986. +}
  46987. +
  46988. +/* microframe_schedule=0 start */
  46989. +
  46990. +/**
  46991. + * Checks that a channel is available for a periodic transfer.
  46992. + *
  46993. + * @return 0 if successful, negative error code otherise.
  46994. + */
  46995. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  46996. +{
  46997. + /*
  46998. + * Currently assuming that there is a dedicated host channnel for each
  46999. + * periodic transaction plus at least one host channel for
  47000. + * non-periodic transactions.
  47001. + */
  47002. + int status;
  47003. + int num_channels;
  47004. +
  47005. + num_channels = hcd->core_if->core_params->host_channels;
  47006. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  47007. + && (hcd->periodic_channels < num_channels - 1)) {
  47008. + status = 0;
  47009. + } else {
  47010. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  47011. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  47012. + status = -DWC_E_NO_SPACE;
  47013. + }
  47014. +
  47015. + return status;
  47016. +}
  47017. +
  47018. +/**
  47019. + * Checks that there is sufficient bandwidth for the specified QH in the
  47020. + * periodic schedule. For simplicity, this calculation assumes that all the
  47021. + * transfers in the periodic schedule may occur in the same (micro)frame.
  47022. + *
  47023. + * @param hcd The HCD state structure for the DWC OTG controller.
  47024. + * @param qh QH containing periodic bandwidth required.
  47025. + *
  47026. + * @return 0 if successful, negative error code otherwise.
  47027. + */
  47028. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47029. +{
  47030. + int status;
  47031. + int16_t max_claimed_usecs;
  47032. +
  47033. + status = 0;
  47034. +
  47035. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  47036. + /*
  47037. + * High speed mode.
  47038. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  47039. + */
  47040. +
  47041. + max_claimed_usecs = 100 - qh->usecs;
  47042. + } else {
  47043. + /*
  47044. + * Full speed mode.
  47045. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  47046. + */
  47047. + max_claimed_usecs = 900 - qh->usecs;
  47048. + }
  47049. +
  47050. + if (hcd->periodic_usecs > max_claimed_usecs) {
  47051. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  47052. + status = -DWC_E_NO_SPACE;
  47053. + }
  47054. +
  47055. + return status;
  47056. +}
  47057. +
  47058. +/* microframe_schedule=0 end */
  47059. +
  47060. +/**
  47061. + * Microframe scheduler
  47062. + * track the total use in hcd->frame_usecs
  47063. + * keep each qh use in qh->frame_usecs
  47064. + * when surrendering the qh then donate the time back
  47065. + */
  47066. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  47067. +
  47068. +/*
  47069. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  47070. + */
  47071. +void init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  47072. +{
  47073. + int i;
  47074. + if (_hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  47075. + _hcd->frame_usecs[0] = 900;
  47076. + for (i = 1; i < 8; i++)
  47077. + _hcd->frame_usecs[i] = 0;
  47078. + } else {
  47079. + for (i = 0; i < 8; i++)
  47080. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  47081. + }
  47082. +}
  47083. +
  47084. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  47085. +{
  47086. + int i;
  47087. + unsigned short utime;
  47088. + int t_left;
  47089. + int ret;
  47090. + int done;
  47091. +
  47092. + ret = -1;
  47093. + utime = _qh->usecs;
  47094. + t_left = utime;
  47095. + i = 0;
  47096. + done = 0;
  47097. + while (done == 0) {
  47098. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  47099. + if (utime <= _hcd->frame_usecs[i]) {
  47100. + _hcd->frame_usecs[i] -= utime;
  47101. + _qh->frame_usecs[i] += utime;
  47102. + t_left -= utime;
  47103. + ret = i;
  47104. + done = 1;
  47105. + return ret;
  47106. + } else {
  47107. + i++;
  47108. + if (i == 8) {
  47109. + done = 1;
  47110. + ret = -1;
  47111. + }
  47112. + }
  47113. + }
  47114. + return ret;
  47115. + }
  47116. +
  47117. +/*
  47118. + * use this for FS apps that can span multiple uframes
  47119. + */
  47120. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  47121. +{
  47122. + int i;
  47123. + int j;
  47124. + unsigned short utime;
  47125. + int t_left;
  47126. + int ret;
  47127. + int done;
  47128. + unsigned short xtime;
  47129. +
  47130. + ret = -1;
  47131. + utime = _qh->usecs;
  47132. + t_left = utime;
  47133. + i = 0;
  47134. + done = 0;
  47135. +loop:
  47136. + while (done == 0) {
  47137. + if(_hcd->frame_usecs[i] <= 0) {
  47138. + i++;
  47139. + if (i == 8) {
  47140. + done = 1;
  47141. + ret = -1;
  47142. + }
  47143. + goto loop;
  47144. + }
  47145. +
  47146. + /*
  47147. + * we need n consecutive slots
  47148. + * so use j as a start slot j plus j+1 must be enough time (for now)
  47149. + */
  47150. + xtime= _hcd->frame_usecs[i];
  47151. + for (j = i+1 ; j < 8 ; j++ ) {
  47152. + /*
  47153. + * if we add this frame remaining time to xtime we may
  47154. + * be OK, if not we need to test j for a complete frame
  47155. + */
  47156. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  47157. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  47158. + j = 8;
  47159. + ret = -1;
  47160. + continue;
  47161. + }
  47162. + }
  47163. + if (xtime >= utime) {
  47164. + ret = i;
  47165. + j = 8; /* stop loop with a good value ret */
  47166. + continue;
  47167. + }
  47168. + /* add the frame time to x time */
  47169. + xtime += _hcd->frame_usecs[j];
  47170. + /* we must have a fully available next frame or break */
  47171. + if ((xtime < utime)
  47172. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  47173. + ret = -1;
  47174. + j = 8; /* stop loop with a bad value ret */
  47175. + continue;
  47176. + }
  47177. + }
  47178. + if (ret >= 0) {
  47179. + t_left = utime;
  47180. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  47181. + t_left -= _hcd->frame_usecs[j];
  47182. + if ( t_left <= 0 ) {
  47183. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  47184. + _hcd->frame_usecs[j]= -t_left;
  47185. + ret = i;
  47186. + done = 1;
  47187. + } else {
  47188. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  47189. + _hcd->frame_usecs[j] = 0;
  47190. + }
  47191. + }
  47192. + } else {
  47193. + i++;
  47194. + if (i == 8) {
  47195. + done = 1;
  47196. + ret = -1;
  47197. + }
  47198. + }
  47199. + }
  47200. + return ret;
  47201. +}
  47202. +
  47203. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  47204. +{
  47205. + int ret;
  47206. + ret = -1;
  47207. +
  47208. + if (_qh->speed == USB_SPEED_HIGH ||
  47209. + _hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  47210. + /* if this is a hs transaction we need a full frame - or account for FS usecs */
  47211. + ret = find_single_uframe(_hcd, _qh);
  47212. + } else {
  47213. + /* if this is a fs transaction we may need a sequence of frames */
  47214. + ret = find_multi_uframe(_hcd, _qh);
  47215. + }
  47216. + return ret;
  47217. +}
  47218. +
  47219. +/**
  47220. + * Checks that the max transfer size allowed in a host channel is large enough
  47221. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  47222. + * transfer.
  47223. + *
  47224. + * @param hcd The HCD state structure for the DWC OTG controller.
  47225. + * @param qh QH for a periodic endpoint.
  47226. + *
  47227. + * @return 0 if successful, negative error code otherwise.
  47228. + */
  47229. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47230. +{
  47231. + int status;
  47232. + uint32_t max_xfer_size;
  47233. + uint32_t max_channel_xfer_size;
  47234. +
  47235. + status = 0;
  47236. +
  47237. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  47238. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  47239. +
  47240. + if (max_xfer_size > max_channel_xfer_size) {
  47241. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  47242. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  47243. + status = -DWC_E_NO_SPACE;
  47244. + }
  47245. +
  47246. + return status;
  47247. +}
  47248. +
  47249. +
  47250. +
  47251. +/**
  47252. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  47253. + *
  47254. + * @param hcd The HCD state structure for the DWC OTG controller.
  47255. + * @param qh QH for the periodic transfer. The QH should already contain the
  47256. + * scheduling information.
  47257. + *
  47258. + * @return 0 if successful, negative error code otherwise.
  47259. + */
  47260. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47261. +{
  47262. + int status = 0;
  47263. +
  47264. + if (microframe_schedule) {
  47265. + int frame;
  47266. + status = find_uframe(hcd, qh);
  47267. + frame = -1;
  47268. + if (status == 0) {
  47269. + frame = 7;
  47270. + } else {
  47271. + if (status > 0 )
  47272. + frame = status-1;
  47273. + }
  47274. +
  47275. + /* Set the new frame up */
  47276. + if (frame > -1) {
  47277. + qh->sched_frame &= ~0x7;
  47278. + qh->sched_frame |= (frame & 7);
  47279. + }
  47280. +
  47281. + if (status != -1)
  47282. + status = 0;
  47283. + } else {
  47284. + status = periodic_channel_available(hcd);
  47285. + if (status) {
  47286. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  47287. + return status;
  47288. + }
  47289. +
  47290. + status = check_periodic_bandwidth(hcd, qh);
  47291. + }
  47292. + if (status) {
  47293. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  47294. + "periodic transfer.\n", __func__);
  47295. + return -DWC_E_NO_SPACE;
  47296. + }
  47297. + status = check_max_xfer_size(hcd, qh);
  47298. + if (status) {
  47299. + DWC_INFO("%s: Channel max transfer size too small "
  47300. + "for periodic transfer.\n", __func__);
  47301. + return status;
  47302. + }
  47303. +
  47304. + if (hcd->core_if->dma_desc_enable) {
  47305. + /* Don't rely on SOF and start in ready schedule */
  47306. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  47307. + }
  47308. + else {
  47309. + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
  47310. + {
  47311. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  47312. +
  47313. + }
  47314. + /* Always start in the inactive schedule. */
  47315. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  47316. + }
  47317. +
  47318. + if (!microframe_schedule) {
  47319. + /* Reserve the periodic channel. */
  47320. + hcd->periodic_channels++;
  47321. + }
  47322. +
  47323. + /* Update claimed usecs per (micro)frame. */
  47324. + hcd->periodic_usecs += qh->usecs;
  47325. +
  47326. + return status;
  47327. +}
  47328. +
  47329. +
  47330. +/**
  47331. + * This function adds a QH to either the non periodic or periodic schedule if
  47332. + * it is not already in the schedule. If the QH is already in the schedule, no
  47333. + * action is taken.
  47334. + *
  47335. + * @return 0 if successful, negative error code otherwise.
  47336. + */
  47337. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47338. +{
  47339. + int status = 0;
  47340. + gintmsk_data_t intr_mask = {.d32 = 0 };
  47341. +
  47342. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  47343. + /* QH already in a schedule. */
  47344. + return status;
  47345. + }
  47346. +
  47347. + /* Add the new QH to the appropriate schedule */
  47348. + if (dwc_qh_is_non_per(qh)) {
  47349. + /* Always start in the inactive schedule. */
  47350. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  47351. + &qh->qh_list_entry);
  47352. + //hcd->fiq_state->kick_np_queues = 1;
  47353. + } else {
  47354. + status = schedule_periodic(hcd, qh);
  47355. + if ( !hcd->periodic_qh_count ) {
  47356. + intr_mask.b.sofintr = 1;
  47357. + if (fiq_enable) {
  47358. + local_fiq_disable();
  47359. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  47360. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  47361. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  47362. + local_fiq_enable();
  47363. + } else {
  47364. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  47365. + }
  47366. + }
  47367. + hcd->periodic_qh_count++;
  47368. + }
  47369. +
  47370. + return status;
  47371. +}
  47372. +
  47373. +/**
  47374. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  47375. + *
  47376. + * @param hcd The HCD state structure for the DWC OTG controller.
  47377. + * @param qh QH for the periodic transfer.
  47378. + */
  47379. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47380. +{
  47381. + int i;
  47382. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  47383. +
  47384. + /* Update claimed usecs per (micro)frame. */
  47385. + hcd->periodic_usecs -= qh->usecs;
  47386. +
  47387. + if (!microframe_schedule) {
  47388. + /* Release the periodic channel reservation. */
  47389. + hcd->periodic_channels--;
  47390. + } else {
  47391. + for (i = 0; i < 8; i++) {
  47392. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  47393. + qh->frame_usecs[i] = 0;
  47394. + }
  47395. + }
  47396. +}
  47397. +
  47398. +/**
  47399. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  47400. + * not freed.
  47401. + *
  47402. + * @param hcd The HCD state structure.
  47403. + * @param qh QH to remove from schedule. */
  47404. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47405. +{
  47406. + gintmsk_data_t intr_mask = {.d32 = 0 };
  47407. +
  47408. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  47409. + /* QH is not in a schedule. */
  47410. + return;
  47411. + }
  47412. +
  47413. + if (dwc_qh_is_non_per(qh)) {
  47414. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  47415. + hcd->non_periodic_qh_ptr =
  47416. + hcd->non_periodic_qh_ptr->next;
  47417. + }
  47418. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  47419. + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
  47420. + // hcd->fiq_state->kick_np_queues = 1;
  47421. + } else {
  47422. + deschedule_periodic(hcd, qh);
  47423. + hcd->periodic_qh_count--;
  47424. + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
  47425. + intr_mask.b.sofintr = 1;
  47426. + if (fiq_enable) {
  47427. + local_fiq_disable();
  47428. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  47429. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  47430. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  47431. + local_fiq_enable();
  47432. + } else {
  47433. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  47434. + }
  47435. + }
  47436. + }
  47437. +}
  47438. +
  47439. +/**
  47440. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  47441. + * non-periodic schedule. The QH is added to the inactive non-periodic
  47442. + * schedule if any QTDs are still attached to the QH.
  47443. + *
  47444. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  47445. + * there are any QTDs still attached to the QH, the QH is added to either the
  47446. + * periodic inactive schedule or the periodic ready schedule and its next
  47447. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  47448. + * the scheduled frame has been reached already. Otherwise it's placed in the
  47449. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  47450. + * completely removed from the periodic schedule.
  47451. + */
  47452. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  47453. + int sched_next_periodic_split)
  47454. +{
  47455. + if (dwc_qh_is_non_per(qh)) {
  47456. + dwc_otg_hcd_qh_remove(hcd, qh);
  47457. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  47458. + /* Add back to inactive non-periodic schedule. */
  47459. + dwc_otg_hcd_qh_add(hcd, qh);
  47460. + //hcd->fiq_state->kick_np_queues = 1;
  47461. + } else {
  47462. + if(nak_holdoff && qh->do_split) {
  47463. + qh->nak_frame = 0xFFFF;
  47464. + }
  47465. + }
  47466. + } else {
  47467. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  47468. +
  47469. + if (qh->do_split) {
  47470. + /* Schedule the next continuing periodic split transfer */
  47471. + if (sched_next_periodic_split) {
  47472. +
  47473. + qh->sched_frame = frame_number;
  47474. +
  47475. + if (dwc_frame_num_le(frame_number,
  47476. + dwc_frame_num_inc
  47477. + (qh->start_split_frame,
  47478. + 1))) {
  47479. + /*
  47480. + * Allow one frame to elapse after start
  47481. + * split microframe before scheduling
  47482. + * complete split, but DONT if we are
  47483. + * doing the next start split in the
  47484. + * same frame for an ISOC out.
  47485. + */
  47486. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  47487. + (qh->ep_is_in != 0)) {
  47488. + qh->sched_frame =
  47489. + dwc_frame_num_inc(qh->sched_frame, 1);
  47490. + }
  47491. + }
  47492. + } else {
  47493. + qh->sched_frame =
  47494. + dwc_frame_num_inc(qh->start_split_frame,
  47495. + qh->interval);
  47496. + if (dwc_frame_num_le
  47497. + (qh->sched_frame, frame_number)) {
  47498. + qh->sched_frame = frame_number;
  47499. + }
  47500. + qh->sched_frame |= 0x7;
  47501. + qh->start_split_frame = qh->sched_frame;
  47502. + }
  47503. + } else {
  47504. + qh->sched_frame =
  47505. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  47506. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  47507. + qh->sched_frame = frame_number;
  47508. + }
  47509. + }
  47510. +
  47511. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  47512. + dwc_otg_hcd_qh_remove(hcd, qh);
  47513. + } else {
  47514. + /*
  47515. + * Remove from periodic_sched_queued and move to
  47516. + * appropriate queue.
  47517. + */
  47518. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  47519. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  47520. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  47521. + &qh->qh_list_entry);
  47522. + } else {
  47523. + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
  47524. + {
  47525. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  47526. + }
  47527. +
  47528. + DWC_LIST_MOVE_HEAD
  47529. + (&hcd->periodic_sched_inactive,
  47530. + &qh->qh_list_entry);
  47531. + }
  47532. + }
  47533. + }
  47534. +}
  47535. +
  47536. +/**
  47537. + * This function allocates and initializes a QTD.
  47538. + *
  47539. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  47540. + * pointing to each other so each pair should have a unique correlation.
  47541. + * @param atomic_alloc Flag to do atomic alloc if needed
  47542. + *
  47543. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  47544. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  47545. +{
  47546. + dwc_otg_qtd_t *qtd;
  47547. +
  47548. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  47549. + if (qtd == NULL) {
  47550. + return NULL;
  47551. + }
  47552. +
  47553. + dwc_otg_hcd_qtd_init(qtd, urb);
  47554. + return qtd;
  47555. +}
  47556. +
  47557. +/**
  47558. + * Initializes a QTD structure.
  47559. + *
  47560. + * @param qtd The QTD to initialize.
  47561. + * @param urb The URB to use for initialization. */
  47562. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  47563. +{
  47564. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  47565. + qtd->urb = urb;
  47566. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  47567. + /*
  47568. + * The only time the QTD data toggle is used is on the data
  47569. + * phase of control transfers. This phase always starts with
  47570. + * DATA1.
  47571. + */
  47572. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  47573. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  47574. + }
  47575. +
  47576. + /* start split */
  47577. + qtd->complete_split = 0;
  47578. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  47579. + qtd->isoc_split_offset = 0;
  47580. + qtd->in_process = 0;
  47581. +
  47582. + /* Store the qtd ptr in the urb to reference what QTD. */
  47583. + urb->qtd = qtd;
  47584. + return;
  47585. +}
  47586. +
  47587. +/**
  47588. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  47589. + * QH to place the QTD into. If it does not find a QH, then it will create a
  47590. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  47591. + * is placed into the proper schedule based on its EP type.
  47592. + * HCD lock must be held and interrupts must be disabled on entry
  47593. + *
  47594. + * @param[in] qtd The QTD to add
  47595. + * @param[in] hcd The DWC HCD structure
  47596. + * @param[out] qh out parameter to return queue head
  47597. + * @param atomic_alloc Flag to do atomic alloc if needed
  47598. + *
  47599. + * @return 0 if successful, negative error code otherwise.
  47600. + */
  47601. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  47602. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  47603. +{
  47604. + int retval = 0;
  47605. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  47606. +
  47607. + /*
  47608. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  47609. + * doesn't exist.
  47610. + */
  47611. + if (*qh == NULL) {
  47612. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  47613. + if (*qh == NULL) {
  47614. + retval = -DWC_E_NO_MEMORY;
  47615. + goto done;
  47616. + } else {
  47617. + if (fiq_enable)
  47618. + hcd->fiq_state->kick_np_queues = 1;
  47619. + }
  47620. + }
  47621. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  47622. + if (retval == 0) {
  47623. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  47624. + qtd_list_entry);
  47625. + qtd->qh = *qh;
  47626. + }
  47627. +done:
  47628. +
  47629. + return retval;
  47630. +}
  47631. +
  47632. +#endif /* DWC_DEVICE_ONLY */
  47633. --- /dev/null
  47634. +++ b/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  47635. @@ -0,0 +1,188 @@
  47636. +#ifndef _DWC_OS_DEP_H_
  47637. +#define _DWC_OS_DEP_H_
  47638. +
  47639. +/**
  47640. + * @file
  47641. + *
  47642. + * This file contains OS dependent structures.
  47643. + *
  47644. + */
  47645. +
  47646. +#include <linux/kernel.h>
  47647. +#include <linux/module.h>
  47648. +#include <linux/moduleparam.h>
  47649. +#include <linux/init.h>
  47650. +#include <linux/device.h>
  47651. +#include <linux/errno.h>
  47652. +#include <linux/types.h>
  47653. +#include <linux/slab.h>
  47654. +#include <linux/list.h>
  47655. +#include <linux/interrupt.h>
  47656. +#include <linux/ctype.h>
  47657. +#include <linux/string.h>
  47658. +#include <linux/dma-mapping.h>
  47659. +#include <linux/jiffies.h>
  47660. +#include <linux/delay.h>
  47661. +#include <linux/timer.h>
  47662. +#include <linux/workqueue.h>
  47663. +#include <linux/stat.h>
  47664. +#include <linux/pci.h>
  47665. +
  47666. +#include <linux/version.h>
  47667. +
  47668. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  47669. +# include <linux/irq.h>
  47670. +#endif
  47671. +
  47672. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  47673. +# include <linux/usb/ch9.h>
  47674. +#else
  47675. +# include <linux/usb_ch9.h>
  47676. +#endif
  47677. +
  47678. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  47679. +# include <linux/usb/gadget.h>
  47680. +#else
  47681. +# include <linux/usb_gadget.h>
  47682. +#endif
  47683. +
  47684. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  47685. +# include <asm/irq.h>
  47686. +#endif
  47687. +
  47688. +#ifdef PCI_INTERFACE
  47689. +# include <asm/io.h>
  47690. +#endif
  47691. +
  47692. +#ifdef LM_INTERFACE
  47693. +# include <asm/unaligned.h>
  47694. +# include <asm/sizes.h>
  47695. +# include <asm/param.h>
  47696. +# include <asm/io.h>
  47697. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  47698. +# include <asm/arch/hardware.h>
  47699. +# include <asm/arch/lm.h>
  47700. +# include <asm/arch/irqs.h>
  47701. +# include <asm/arch/regs-irq.h>
  47702. +# else
  47703. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  47704. + here we assume that the machine architecture provides definitions
  47705. + in its own header
  47706. +*/
  47707. +# include <mach/lm.h>
  47708. +# include <mach/hardware.h>
  47709. +# endif
  47710. +#endif
  47711. +
  47712. +#ifdef PLATFORM_INTERFACE
  47713. +#include <linux/platform_device.h>
  47714. +#include <asm/mach/map.h>
  47715. +#endif
  47716. +
  47717. +/** The OS page size */
  47718. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  47719. +
  47720. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  47721. +typedef int gfp_t;
  47722. +#endif
  47723. +
  47724. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  47725. +# define IRQF_SHARED SA_SHIRQ
  47726. +#endif
  47727. +
  47728. +typedef struct os_dependent {
  47729. + /** Base address returned from ioremap() */
  47730. + void *base;
  47731. +
  47732. + /** Register offset for Diagnostic API */
  47733. + uint32_t reg_offset;
  47734. +
  47735. + /** Base address for MPHI peripheral */
  47736. + void *mphi_base;
  47737. +
  47738. +#ifdef LM_INTERFACE
  47739. + struct lm_device *lmdev;
  47740. +#elif defined(PCI_INTERFACE)
  47741. + struct pci_dev *pcidev;
  47742. +
  47743. + /** Start address of a PCI region */
  47744. + resource_size_t rsrc_start;
  47745. +
  47746. + /** Length address of a PCI region */
  47747. + resource_size_t rsrc_len;
  47748. +#elif defined(PLATFORM_INTERFACE)
  47749. + struct platform_device *platformdev;
  47750. +#endif
  47751. +
  47752. +} os_dependent_t;
  47753. +
  47754. +#ifdef __cplusplus
  47755. +}
  47756. +#endif
  47757. +
  47758. +
  47759. +
  47760. +/* Type for the our device on the chosen bus */
  47761. +#if defined(LM_INTERFACE)
  47762. +typedef struct lm_device dwc_bus_dev_t;
  47763. +#elif defined(PCI_INTERFACE)
  47764. +typedef struct pci_dev dwc_bus_dev_t;
  47765. +#elif defined(PLATFORM_INTERFACE)
  47766. +typedef struct platform_device dwc_bus_dev_t;
  47767. +#endif
  47768. +
  47769. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  47770. +#if defined(LM_INTERFACE)
  47771. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  47772. +#elif defined(PCI_INTERFACE)
  47773. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  47774. +#elif defined(PLATFORM_INTERFACE)
  47775. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  47776. +#endif
  47777. +
  47778. +/**
  47779. + * Helper macro returning the otg_device structure of a given struct device
  47780. + *
  47781. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  47782. + */
  47783. +#ifdef LM_INTERFACE
  47784. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47785. + struct lm_device *lm_dev = \
  47786. + container_of(_dev, struct lm_device, dev); \
  47787. + _var = lm_get_drvdata(lm_dev); \
  47788. + } while (0)
  47789. +
  47790. +#elif defined(PCI_INTERFACE)
  47791. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47792. + _var = dev_get_drvdata(_dev); \
  47793. + } while (0)
  47794. +
  47795. +#elif defined(PLATFORM_INTERFACE)
  47796. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47797. + struct platform_device *platform_dev = \
  47798. + container_of(_dev, struct platform_device, dev); \
  47799. + _var = platform_get_drvdata(platform_dev); \
  47800. + } while (0)
  47801. +#endif
  47802. +
  47803. +
  47804. +/**
  47805. + * Helper macro returning the struct dev of the given struct os_dependent
  47806. + *
  47807. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  47808. + */
  47809. +#ifdef LM_INTERFACE
  47810. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47811. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  47812. +#elif defined(PCI_INTERFACE)
  47813. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47814. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  47815. +#elif defined(PLATFORM_INTERFACE)
  47816. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47817. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  47818. +#endif
  47819. +
  47820. +
  47821. +
  47822. +
  47823. +#endif /* _DWC_OS_DEP_H_ */
  47824. --- /dev/null
  47825. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  47826. @@ -0,0 +1,2725 @@
  47827. +/* ==========================================================================
  47828. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  47829. + * $Revision: #101 $
  47830. + * $Date: 2012/08/10 $
  47831. + * $Change: 2047372 $
  47832. + *
  47833. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  47834. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  47835. + * otherwise expressly agreed to in writing between Synopsys and you.
  47836. + *
  47837. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  47838. + * any End User Software License Agreement or Agreement for Licensed Product
  47839. + * with Synopsys or any supplement thereto. You are permitted to use and
  47840. + * redistribute this Software in source and binary forms, with or without
  47841. + * modification, provided that redistributions of source code must retain this
  47842. + * notice. You may not view, use, disclose, copy or distribute this file or
  47843. + * any information contained herein except pursuant to this license grant from
  47844. + * Synopsys. If you do not agree with this notice, including the disclaimer
  47845. + * below, then you are not authorized to use the Software.
  47846. + *
  47847. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  47848. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47849. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47850. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  47851. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  47852. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  47853. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  47854. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47855. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47856. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  47857. + * DAMAGE.
  47858. + * ========================================================================== */
  47859. +#ifndef DWC_HOST_ONLY
  47860. +
  47861. +/** @file
  47862. + * This file implements PCD Core. All code in this file is portable and doesn't
  47863. + * use any OS specific functions.
  47864. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  47865. + * header file, which can be used to implement OS specific PCD interface.
  47866. + *
  47867. + * An important function of the PCD is managing interrupts generated
  47868. + * by the DWC_otg controller. The implementation of the DWC_otg device
  47869. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  47870. + *
  47871. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  47872. + * @todo Does it work when the request size is greater than DEPTSIZ
  47873. + * transfer size
  47874. + *
  47875. + */
  47876. +
  47877. +#include "dwc_otg_pcd.h"
  47878. +
  47879. +#ifdef DWC_UTE_CFI
  47880. +#include "dwc_otg_cfi.h"
  47881. +
  47882. +extern int init_cfi(cfiobject_t * cfiobj);
  47883. +#endif
  47884. +
  47885. +/**
  47886. + * Choose endpoint from ep arrays using usb_ep structure.
  47887. + */
  47888. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  47889. +{
  47890. + int i;
  47891. + if (pcd->ep0.priv == handle) {
  47892. + return &pcd->ep0;
  47893. + }
  47894. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  47895. + if (pcd->in_ep[i].priv == handle)
  47896. + return &pcd->in_ep[i];
  47897. + if (pcd->out_ep[i].priv == handle)
  47898. + return &pcd->out_ep[i];
  47899. + }
  47900. +
  47901. + return NULL;
  47902. +}
  47903. +
  47904. +/**
  47905. + * This function completes a request. It call's the request call back.
  47906. + */
  47907. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  47908. + int32_t status)
  47909. +{
  47910. + unsigned stopped = ep->stopped;
  47911. +
  47912. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  47913. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  47914. +
  47915. + /* don't modify queue heads during completion callback */
  47916. + ep->stopped = 1;
  47917. + /* spin_unlock/spin_lock now done in fops->complete() */
  47918. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  47919. + req->actual);
  47920. +
  47921. + if (ep->pcd->request_pending > 0) {
  47922. + --ep->pcd->request_pending;
  47923. + }
  47924. +
  47925. + ep->stopped = stopped;
  47926. + DWC_FREE(req);
  47927. +}
  47928. +
  47929. +/**
  47930. + * This function terminates all the requsts in the EP request queue.
  47931. + */
  47932. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  47933. +{
  47934. + dwc_otg_pcd_request_t *req;
  47935. +
  47936. + ep->stopped = 1;
  47937. +
  47938. + /* called with irqs blocked?? */
  47939. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  47940. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  47941. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  47942. + }
  47943. +}
  47944. +
  47945. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  47946. + const struct dwc_otg_pcd_function_ops *fops)
  47947. +{
  47948. + pcd->fops = fops;
  47949. +}
  47950. +
  47951. +/**
  47952. + * PCD Callback function for initializing the PCD when switching to
  47953. + * device mode.
  47954. + *
  47955. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47956. + */
  47957. +static int32_t dwc_otg_pcd_start_cb(void *p)
  47958. +{
  47959. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47960. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  47961. +
  47962. + /*
  47963. + * Initialized the Core for Device mode.
  47964. + */
  47965. + if (dwc_otg_is_device_mode(core_if)) {
  47966. + dwc_otg_core_dev_init(core_if);
  47967. + /* Set core_if's lock pointer to the pcd->lock */
  47968. + core_if->lock = pcd->lock;
  47969. + }
  47970. + return 1;
  47971. +}
  47972. +
  47973. +/** CFI-specific buffer allocation function for EP */
  47974. +#ifdef DWC_UTE_CFI
  47975. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  47976. + size_t buflen, int flags)
  47977. +{
  47978. + dwc_otg_pcd_ep_t *ep;
  47979. + ep = get_ep_from_handle(pcd, pep);
  47980. + if (!ep) {
  47981. + DWC_WARN("bad ep\n");
  47982. + return -DWC_E_INVALID;
  47983. + }
  47984. +
  47985. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  47986. + flags);
  47987. +}
  47988. +#else
  47989. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  47990. + size_t buflen, int flags);
  47991. +#endif
  47992. +
  47993. +/**
  47994. + * PCD Callback function for notifying the PCD when resuming from
  47995. + * suspend.
  47996. + *
  47997. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47998. + */
  47999. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  48000. +{
  48001. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  48002. +
  48003. + if (pcd->fops->resume) {
  48004. + pcd->fops->resume(pcd);
  48005. + }
  48006. +
  48007. + /* Stop the SRP timeout timer. */
  48008. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  48009. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  48010. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  48011. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  48012. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  48013. + }
  48014. + }
  48015. + return 1;
  48016. +}
  48017. +
  48018. +/**
  48019. + * PCD Callback function for notifying the PCD device is suspended.
  48020. + *
  48021. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  48022. + */
  48023. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  48024. +{
  48025. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  48026. +
  48027. + if (pcd->fops->suspend) {
  48028. + DWC_SPINUNLOCK(pcd->lock);
  48029. + pcd->fops->suspend(pcd);
  48030. + DWC_SPINLOCK(pcd->lock);
  48031. + }
  48032. +
  48033. + return 1;
  48034. +}
  48035. +
  48036. +/**
  48037. + * PCD Callback function for stopping the PCD when switching to Host
  48038. + * mode.
  48039. + *
  48040. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  48041. + */
  48042. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  48043. +{
  48044. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  48045. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  48046. +
  48047. + dwc_otg_pcd_stop(pcd);
  48048. + return 1;
  48049. +}
  48050. +
  48051. +/**
  48052. + * PCD Callback structure for handling mode switching.
  48053. + */
  48054. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  48055. + .start = dwc_otg_pcd_start_cb,
  48056. + .stop = dwc_otg_pcd_stop_cb,
  48057. + .suspend = dwc_otg_pcd_suspend_cb,
  48058. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  48059. + .p = 0, /* Set at registration */
  48060. +};
  48061. +
  48062. +/**
  48063. + * This function allocates a DMA Descriptor chain for the Endpoint
  48064. + * buffer to be used for a transfer to/from the specified endpoint.
  48065. + */
  48066. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(struct device *dev,
  48067. + dwc_dma_t * dma_desc_addr,
  48068. + uint32_t count)
  48069. +{
  48070. + return DWC_DMA_ALLOC_ATOMIC(dev, count * sizeof(dwc_otg_dev_dma_desc_t),
  48071. + dma_desc_addr);
  48072. +}
  48073. +
  48074. +/**
  48075. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  48076. + */
  48077. +void dwc_otg_ep_free_desc_chain(struct device *dev,
  48078. + dwc_otg_dev_dma_desc_t * desc_addr,
  48079. + uint32_t dma_desc_addr, uint32_t count)
  48080. +{
  48081. + DWC_DMA_FREE(dev, count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  48082. + dma_desc_addr);
  48083. +}
  48084. +
  48085. +#ifdef DWC_EN_ISOC
  48086. +
  48087. +/**
  48088. + * This function initializes a descriptor chain for Isochronous transfer
  48089. + *
  48090. + * @param core_if Programming view of DWC_otg controller.
  48091. + * @param dwc_ep The EP to start the transfer on.
  48092. + *
  48093. + */
  48094. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  48095. + dwc_ep_t * dwc_ep)
  48096. +{
  48097. +
  48098. + dsts_data_t dsts = {.d32 = 0 };
  48099. + depctl_data_t depctl = {.d32 = 0 };
  48100. + volatile uint32_t *addr;
  48101. + int i, j;
  48102. + uint32_t len;
  48103. +
  48104. + if (dwc_ep->is_in)
  48105. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  48106. + else
  48107. + dwc_ep->desc_cnt =
  48108. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48109. + dwc_ep->bInterval;
  48110. +
  48111. + /** Allocate descriptors for double buffering */
  48112. + dwc_ep->iso_desc_addr =
  48113. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  48114. + dwc_ep->desc_cnt * 2);
  48115. + if (dwc_ep->desc_addr) {
  48116. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  48117. + return;
  48118. + }
  48119. +
  48120. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48121. +
  48122. + /** ISO OUT EP */
  48123. + if (dwc_ep->is_in == 0) {
  48124. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  48125. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  48126. + dma_addr_t dma_ad;
  48127. + uint32_t data_per_desc;
  48128. + dwc_otg_dev_out_ep_regs_t *out_regs =
  48129. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  48130. + int offset;
  48131. +
  48132. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  48133. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  48134. +
  48135. + /** Buffer 0 descriptors setup */
  48136. + dma_ad = dwc_ep->dma_addr0;
  48137. +
  48138. + sts.b_iso_out.bs = BS_HOST_READY;
  48139. + sts.b_iso_out.rxsts = 0;
  48140. + sts.b_iso_out.l = 0;
  48141. + sts.b_iso_out.sp = 0;
  48142. + sts.b_iso_out.ioc = 0;
  48143. + sts.b_iso_out.pid = 0;
  48144. + sts.b_iso_out.framenum = 0;
  48145. +
  48146. + offset = 0;
  48147. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  48148. + i += dwc_ep->pkt_per_frm) {
  48149. +
  48150. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  48151. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  48152. + if (len > dwc_ep->data_per_frame)
  48153. + data_per_desc =
  48154. + dwc_ep->data_per_frame -
  48155. + j * dwc_ep->maxpacket;
  48156. + else
  48157. + data_per_desc = dwc_ep->maxpacket;
  48158. + len = data_per_desc % 4;
  48159. + if (len)
  48160. + data_per_desc += 4 - len;
  48161. +
  48162. + sts.b_iso_out.rxbytes = data_per_desc;
  48163. + dma_desc->buf = dma_ad;
  48164. + dma_desc->status.d32 = sts.d32;
  48165. +
  48166. + offset += data_per_desc;
  48167. + dma_desc++;
  48168. + dma_ad += data_per_desc;
  48169. + }
  48170. + }
  48171. +
  48172. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  48173. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  48174. + if (len > dwc_ep->data_per_frame)
  48175. + data_per_desc =
  48176. + dwc_ep->data_per_frame -
  48177. + j * dwc_ep->maxpacket;
  48178. + else
  48179. + data_per_desc = dwc_ep->maxpacket;
  48180. + len = data_per_desc % 4;
  48181. + if (len)
  48182. + data_per_desc += 4 - len;
  48183. + sts.b_iso_out.rxbytes = data_per_desc;
  48184. + dma_desc->buf = dma_ad;
  48185. + dma_desc->status.d32 = sts.d32;
  48186. +
  48187. + offset += data_per_desc;
  48188. + dma_desc++;
  48189. + dma_ad += data_per_desc;
  48190. + }
  48191. +
  48192. + sts.b_iso_out.ioc = 1;
  48193. + len = (j + 1) * dwc_ep->maxpacket;
  48194. + if (len > dwc_ep->data_per_frame)
  48195. + data_per_desc =
  48196. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  48197. + else
  48198. + data_per_desc = dwc_ep->maxpacket;
  48199. + len = data_per_desc % 4;
  48200. + if (len)
  48201. + data_per_desc += 4 - len;
  48202. + sts.b_iso_out.rxbytes = data_per_desc;
  48203. +
  48204. + dma_desc->buf = dma_ad;
  48205. + dma_desc->status.d32 = sts.d32;
  48206. + dma_desc++;
  48207. +
  48208. + /** Buffer 1 descriptors setup */
  48209. + sts.b_iso_out.ioc = 0;
  48210. + dma_ad = dwc_ep->dma_addr1;
  48211. +
  48212. + offset = 0;
  48213. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  48214. + i += dwc_ep->pkt_per_frm) {
  48215. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  48216. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  48217. + if (len > dwc_ep->data_per_frame)
  48218. + data_per_desc =
  48219. + dwc_ep->data_per_frame -
  48220. + j * dwc_ep->maxpacket;
  48221. + else
  48222. + data_per_desc = dwc_ep->maxpacket;
  48223. + len = data_per_desc % 4;
  48224. + if (len)
  48225. + data_per_desc += 4 - len;
  48226. +
  48227. + data_per_desc =
  48228. + sts.b_iso_out.rxbytes = data_per_desc;
  48229. + dma_desc->buf = dma_ad;
  48230. + dma_desc->status.d32 = sts.d32;
  48231. +
  48232. + offset += data_per_desc;
  48233. + dma_desc++;
  48234. + dma_ad += data_per_desc;
  48235. + }
  48236. + }
  48237. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  48238. + data_per_desc =
  48239. + ((j + 1) * dwc_ep->maxpacket >
  48240. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  48241. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  48242. + data_per_desc +=
  48243. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  48244. + sts.b_iso_out.rxbytes = data_per_desc;
  48245. + dma_desc->buf = dma_ad;
  48246. + dma_desc->status.d32 = sts.d32;
  48247. +
  48248. + offset += data_per_desc;
  48249. + dma_desc++;
  48250. + dma_ad += data_per_desc;
  48251. + }
  48252. +
  48253. + sts.b_iso_out.ioc = 1;
  48254. + sts.b_iso_out.l = 1;
  48255. + data_per_desc =
  48256. + ((j + 1) * dwc_ep->maxpacket >
  48257. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  48258. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  48259. + data_per_desc +=
  48260. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  48261. + sts.b_iso_out.rxbytes = data_per_desc;
  48262. +
  48263. + dma_desc->buf = dma_ad;
  48264. + dma_desc->status.d32 = sts.d32;
  48265. +
  48266. + dwc_ep->next_frame = 0;
  48267. +
  48268. + /** Write dma_ad into DOEPDMA register */
  48269. + DWC_WRITE_REG32(&(out_regs->doepdma),
  48270. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  48271. +
  48272. + }
  48273. + /** ISO IN EP */
  48274. + else {
  48275. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  48276. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  48277. + dma_addr_t dma_ad;
  48278. + dwc_otg_dev_in_ep_regs_t *in_regs =
  48279. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  48280. + unsigned int frmnumber;
  48281. + fifosize_data_t txfifosize, rxfifosize;
  48282. +
  48283. + txfifosize.d32 =
  48284. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  48285. + dtxfsts);
  48286. + rxfifosize.d32 =
  48287. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  48288. +
  48289. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  48290. +
  48291. + dma_ad = dwc_ep->dma_addr0;
  48292. +
  48293. + dsts.d32 =
  48294. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48295. +
  48296. + sts.b_iso_in.bs = BS_HOST_READY;
  48297. + sts.b_iso_in.txsts = 0;
  48298. + sts.b_iso_in.sp =
  48299. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  48300. + sts.b_iso_in.ioc = 0;
  48301. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  48302. +
  48303. + frmnumber = dwc_ep->next_frame;
  48304. +
  48305. + sts.b_iso_in.framenum = frmnumber;
  48306. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  48307. + sts.b_iso_in.l = 0;
  48308. +
  48309. + /** Buffer 0 descriptors setup */
  48310. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  48311. + dma_desc->buf = dma_ad;
  48312. + dma_desc->status.d32 = sts.d32;
  48313. + dma_desc++;
  48314. +
  48315. + dma_ad += dwc_ep->data_per_frame;
  48316. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  48317. + }
  48318. +
  48319. + sts.b_iso_in.ioc = 1;
  48320. + dma_desc->buf = dma_ad;
  48321. + dma_desc->status.d32 = sts.d32;
  48322. + ++dma_desc;
  48323. +
  48324. + /** Buffer 1 descriptors setup */
  48325. + sts.b_iso_in.ioc = 0;
  48326. + dma_ad = dwc_ep->dma_addr1;
  48327. +
  48328. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  48329. + i += dwc_ep->pkt_per_frm) {
  48330. + dma_desc->buf = dma_ad;
  48331. + dma_desc->status.d32 = sts.d32;
  48332. + dma_desc++;
  48333. +
  48334. + dma_ad += dwc_ep->data_per_frame;
  48335. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  48336. +
  48337. + sts.b_iso_in.ioc = 0;
  48338. + }
  48339. + sts.b_iso_in.ioc = 1;
  48340. + sts.b_iso_in.l = 1;
  48341. +
  48342. + dma_desc->buf = dma_ad;
  48343. + dma_desc->status.d32 = sts.d32;
  48344. +
  48345. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  48346. +
  48347. + /** Write dma_ad into diepdma register */
  48348. + DWC_WRITE_REG32(&(in_regs->diepdma),
  48349. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  48350. + }
  48351. + /** Enable endpoint, clear nak */
  48352. + depctl.d32 = 0;
  48353. + depctl.b.epena = 1;
  48354. + depctl.b.usbactep = 1;
  48355. + depctl.b.cnak = 1;
  48356. +
  48357. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  48358. + depctl.d32 = DWC_READ_REG32(addr);
  48359. +}
  48360. +
  48361. +/**
  48362. + * This function initializes a descriptor chain for Isochronous transfer
  48363. + *
  48364. + * @param core_if Programming view of DWC_otg controller.
  48365. + * @param ep The EP to start the transfer on.
  48366. + *
  48367. + */
  48368. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  48369. + dwc_ep_t * ep)
  48370. +{
  48371. + depctl_data_t depctl = {.d32 = 0 };
  48372. + volatile uint32_t *addr;
  48373. +
  48374. + if (ep->is_in) {
  48375. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  48376. + } else {
  48377. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  48378. + }
  48379. +
  48380. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  48381. + return;
  48382. + } else {
  48383. + deptsiz_data_t deptsiz = {.d32 = 0 };
  48384. +
  48385. + ep->xfer_len =
  48386. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  48387. + ep->pkt_cnt =
  48388. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  48389. + ep->xfer_count = 0;
  48390. + ep->xfer_buff =
  48391. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  48392. + ep->dma_addr =
  48393. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  48394. +
  48395. + if (ep->is_in) {
  48396. + /* Program the transfer size and packet count
  48397. + * as follows: xfersize = N * maxpacket +
  48398. + * short_packet pktcnt = N + (short_packet
  48399. + * exist ? 1 : 0)
  48400. + */
  48401. + deptsiz.b.mc = ep->pkt_per_frm;
  48402. + deptsiz.b.xfersize = ep->xfer_len;
  48403. + deptsiz.b.pktcnt =
  48404. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  48405. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  48406. + dieptsiz, deptsiz.d32);
  48407. +
  48408. + /* Write the DMA register */
  48409. + DWC_WRITE_REG32(&
  48410. + (core_if->dev_if->in_ep_regs[ep->num]->
  48411. + diepdma), (uint32_t) ep->dma_addr);
  48412. +
  48413. + } else {
  48414. + deptsiz.b.pktcnt =
  48415. + (ep->xfer_len + (ep->maxpacket - 1)) /
  48416. + ep->maxpacket;
  48417. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  48418. +
  48419. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  48420. + doeptsiz, deptsiz.d32);
  48421. +
  48422. + /* Write the DMA register */
  48423. + DWC_WRITE_REG32(&
  48424. + (core_if->dev_if->out_ep_regs[ep->num]->
  48425. + doepdma), (uint32_t) ep->dma_addr);
  48426. +
  48427. + }
  48428. + /** Enable endpoint, clear nak */
  48429. + depctl.d32 = 0;
  48430. + depctl.b.epena = 1;
  48431. + depctl.b.cnak = 1;
  48432. +
  48433. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  48434. + }
  48435. +}
  48436. +
  48437. +/**
  48438. + * This function does the setup for a data transfer for an EP and
  48439. + * starts the transfer. For an IN transfer, the packets will be
  48440. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  48441. + * the packets are unloaded from the Rx FIFO in the ISR.
  48442. + *
  48443. + * @param core_if Programming view of DWC_otg controller.
  48444. + * @param ep The EP to start the transfer on.
  48445. + */
  48446. +
  48447. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  48448. + dwc_ep_t * ep)
  48449. +{
  48450. + if (core_if->dma_enable) {
  48451. + if (core_if->dma_desc_enable) {
  48452. + if (ep->is_in) {
  48453. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  48454. + } else {
  48455. + ep->desc_cnt = ep->pkt_cnt;
  48456. + }
  48457. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  48458. + } else {
  48459. + if (core_if->pti_enh_enable) {
  48460. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  48461. + } else {
  48462. + ep->cur_pkt_addr =
  48463. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  48464. + xfer_buff0;
  48465. + ep->cur_pkt_dma_addr =
  48466. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  48467. + dma_addr0;
  48468. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  48469. + }
  48470. + }
  48471. + } else {
  48472. + ep->cur_pkt_addr =
  48473. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  48474. + ep->cur_pkt_dma_addr =
  48475. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  48476. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  48477. + }
  48478. +}
  48479. +
  48480. +/**
  48481. + * This function stops transfer for an EP and
  48482. + * resets the ep's variables.
  48483. + *
  48484. + * @param core_if Programming view of DWC_otg controller.
  48485. + * @param ep The EP to start the transfer on.
  48486. + */
  48487. +
  48488. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  48489. +{
  48490. + depctl_data_t depctl = {.d32 = 0 };
  48491. + volatile uint32_t *addr;
  48492. +
  48493. + if (ep->is_in == 1) {
  48494. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  48495. + } else {
  48496. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  48497. + }
  48498. +
  48499. + /* disable the ep */
  48500. + depctl.d32 = DWC_READ_REG32(addr);
  48501. +
  48502. + depctl.b.epdis = 1;
  48503. + depctl.b.snak = 1;
  48504. +
  48505. + DWC_WRITE_REG32(addr, depctl.d32);
  48506. +
  48507. + if (core_if->dma_desc_enable &&
  48508. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  48509. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  48510. + ep->iso_dma_desc_addr,
  48511. + ep->desc_cnt * 2);
  48512. + }
  48513. +
  48514. + /* reset varibales */
  48515. + ep->dma_addr0 = 0;
  48516. + ep->dma_addr1 = 0;
  48517. + ep->xfer_buff0 = 0;
  48518. + ep->xfer_buff1 = 0;
  48519. + ep->data_per_frame = 0;
  48520. + ep->data_pattern_frame = 0;
  48521. + ep->sync_frame = 0;
  48522. + ep->buf_proc_intrvl = 0;
  48523. + ep->bInterval = 0;
  48524. + ep->proc_buf_num = 0;
  48525. + ep->pkt_per_frm = 0;
  48526. + ep->pkt_per_frm = 0;
  48527. + ep->desc_cnt = 0;
  48528. + ep->iso_desc_addr = 0;
  48529. + ep->iso_dma_desc_addr = 0;
  48530. +}
  48531. +
  48532. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  48533. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  48534. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  48535. + int data_per_frame, int start_frame,
  48536. + int buf_proc_intrvl, void *req_handle,
  48537. + int atomic_alloc)
  48538. +{
  48539. + dwc_otg_pcd_ep_t *ep;
  48540. + dwc_irqflags_t flags = 0;
  48541. + dwc_ep_t *dwc_ep;
  48542. + int32_t frm_data;
  48543. + dsts_data_t dsts;
  48544. + dwc_otg_core_if_t *core_if;
  48545. +
  48546. + ep = get_ep_from_handle(pcd, ep_handle);
  48547. +
  48548. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  48549. + DWC_WARN("bad ep\n");
  48550. + return -DWC_E_INVALID;
  48551. + }
  48552. +
  48553. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  48554. + core_if = GET_CORE_IF(pcd);
  48555. + dwc_ep = &ep->dwc_ep;
  48556. +
  48557. + if (ep->iso_req_handle) {
  48558. + DWC_WARN("ISO request in progress\n");
  48559. + }
  48560. +
  48561. + dwc_ep->dma_addr0 = dma0;
  48562. + dwc_ep->dma_addr1 = dma1;
  48563. +
  48564. + dwc_ep->xfer_buff0 = buf0;
  48565. + dwc_ep->xfer_buff1 = buf1;
  48566. +
  48567. + dwc_ep->data_per_frame = data_per_frame;
  48568. +
  48569. + /** @todo - pattern data support is to be implemented in the future */
  48570. + dwc_ep->data_pattern_frame = dp_frame;
  48571. + dwc_ep->sync_frame = sync_frame;
  48572. +
  48573. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  48574. +
  48575. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  48576. +
  48577. + dwc_ep->proc_buf_num = 0;
  48578. +
  48579. + dwc_ep->pkt_per_frm = 0;
  48580. + frm_data = ep->dwc_ep.data_per_frame;
  48581. + while (frm_data > 0) {
  48582. + dwc_ep->pkt_per_frm++;
  48583. + frm_data -= ep->dwc_ep.maxpacket;
  48584. + }
  48585. +
  48586. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48587. +
  48588. + if (start_frame == -1) {
  48589. + dwc_ep->next_frame = dsts.b.soffn + 1;
  48590. + if (dwc_ep->bInterval != 1) {
  48591. + dwc_ep->next_frame =
  48592. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  48593. + dwc_ep->next_frame %
  48594. + dwc_ep->bInterval);
  48595. + }
  48596. + } else {
  48597. + dwc_ep->next_frame = start_frame;
  48598. + }
  48599. +
  48600. + if (!core_if->pti_enh_enable) {
  48601. + dwc_ep->pkt_cnt =
  48602. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48603. + dwc_ep->bInterval;
  48604. + } else {
  48605. + dwc_ep->pkt_cnt =
  48606. + (dwc_ep->data_per_frame *
  48607. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  48608. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  48609. + }
  48610. +
  48611. + if (core_if->dma_desc_enable) {
  48612. + dwc_ep->desc_cnt =
  48613. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48614. + dwc_ep->bInterval;
  48615. + }
  48616. +
  48617. + if (atomic_alloc) {
  48618. + dwc_ep->pkt_info =
  48619. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48620. + } else {
  48621. + dwc_ep->pkt_info =
  48622. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48623. + }
  48624. + if (!dwc_ep->pkt_info) {
  48625. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48626. + return -DWC_E_NO_MEMORY;
  48627. + }
  48628. + if (core_if->pti_enh_enable) {
  48629. + dwc_memset(dwc_ep->pkt_info, 0,
  48630. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48631. + }
  48632. +
  48633. + dwc_ep->cur_pkt = 0;
  48634. + ep->iso_req_handle = req_handle;
  48635. +
  48636. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48637. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  48638. + return 0;
  48639. +}
  48640. +
  48641. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  48642. + void *req_handle)
  48643. +{
  48644. + dwc_irqflags_t flags = 0;
  48645. + dwc_otg_pcd_ep_t *ep;
  48646. + dwc_ep_t *dwc_ep;
  48647. +
  48648. + ep = get_ep_from_handle(pcd, ep_handle);
  48649. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  48650. + DWC_WARN("bad ep\n");
  48651. + return -DWC_E_INVALID;
  48652. + }
  48653. + dwc_ep = &ep->dwc_ep;
  48654. +
  48655. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  48656. +
  48657. + DWC_FREE(dwc_ep->pkt_info);
  48658. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  48659. + if (ep->iso_req_handle != req_handle) {
  48660. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48661. + return -DWC_E_INVALID;
  48662. + }
  48663. +
  48664. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48665. +
  48666. + ep->iso_req_handle = 0;
  48667. + return 0;
  48668. +}
  48669. +
  48670. +/**
  48671. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  48672. + * for Isochronous EPs
  48673. + *
  48674. + * - Every time a sync period completes this function is called to
  48675. + * perform data exchange between PCD and gadget
  48676. + */
  48677. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  48678. + void *req_handle)
  48679. +{
  48680. + int i;
  48681. + dwc_ep_t *dwc_ep;
  48682. +
  48683. + dwc_ep = &ep->dwc_ep;
  48684. +
  48685. + DWC_SPINUNLOCK(ep->pcd->lock);
  48686. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  48687. + dwc_ep->proc_buf_num ^ 0x1);
  48688. + DWC_SPINLOCK(ep->pcd->lock);
  48689. +
  48690. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  48691. + dwc_ep->pkt_info[i].status = 0;
  48692. + dwc_ep->pkt_info[i].offset = 0;
  48693. + dwc_ep->pkt_info[i].length = 0;
  48694. + }
  48695. +}
  48696. +
  48697. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  48698. + void *iso_req_handle)
  48699. +{
  48700. + dwc_otg_pcd_ep_t *ep;
  48701. + dwc_ep_t *dwc_ep;
  48702. +
  48703. + ep = get_ep_from_handle(pcd, ep_handle);
  48704. + if (!ep->desc || ep->dwc_ep.num == 0) {
  48705. + DWC_WARN("bad ep\n");
  48706. + return -DWC_E_INVALID;
  48707. + }
  48708. + dwc_ep = &ep->dwc_ep;
  48709. +
  48710. + return dwc_ep->pkt_cnt;
  48711. +}
  48712. +
  48713. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  48714. + void *iso_req_handle, int packet,
  48715. + int *status, int *actual, int *offset)
  48716. +{
  48717. + dwc_otg_pcd_ep_t *ep;
  48718. + dwc_ep_t *dwc_ep;
  48719. +
  48720. + ep = get_ep_from_handle(pcd, ep_handle);
  48721. + if (!ep)
  48722. + DWC_WARN("bad ep\n");
  48723. +
  48724. + dwc_ep = &ep->dwc_ep;
  48725. +
  48726. + *status = dwc_ep->pkt_info[packet].status;
  48727. + *actual = dwc_ep->pkt_info[packet].length;
  48728. + *offset = dwc_ep->pkt_info[packet].offset;
  48729. +}
  48730. +
  48731. +#endif /* DWC_EN_ISOC */
  48732. +
  48733. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  48734. + uint32_t is_in, uint32_t ep_num)
  48735. +{
  48736. + /* Init EP structure */
  48737. + pcd_ep->desc = 0;
  48738. + pcd_ep->pcd = pcd;
  48739. + pcd_ep->stopped = 1;
  48740. + pcd_ep->queue_sof = 0;
  48741. +
  48742. + /* Init DWC ep structure */
  48743. + pcd_ep->dwc_ep.is_in = is_in;
  48744. + pcd_ep->dwc_ep.num = ep_num;
  48745. + pcd_ep->dwc_ep.active = 0;
  48746. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  48747. + /* Control until ep is actvated */
  48748. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  48749. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  48750. + pcd_ep->dwc_ep.dma_addr = 0;
  48751. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  48752. + pcd_ep->dwc_ep.xfer_buff = 0;
  48753. + pcd_ep->dwc_ep.xfer_len = 0;
  48754. + pcd_ep->dwc_ep.xfer_count = 0;
  48755. + pcd_ep->dwc_ep.sent_zlp = 0;
  48756. + pcd_ep->dwc_ep.total_len = 0;
  48757. + pcd_ep->dwc_ep.desc_addr = 0;
  48758. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  48759. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  48760. +}
  48761. +
  48762. +/**
  48763. + * Initialize ep's
  48764. + */
  48765. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  48766. +{
  48767. + int i;
  48768. + uint32_t hwcfg1;
  48769. + dwc_otg_pcd_ep_t *ep;
  48770. + int in_ep_cntr, out_ep_cntr;
  48771. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  48772. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  48773. +
  48774. + /**
  48775. + * Initialize the EP0 structure.
  48776. + */
  48777. + ep = &pcd->ep0;
  48778. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  48779. +
  48780. + in_ep_cntr = 0;
  48781. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  48782. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  48783. + if ((hwcfg1 & 0x1) == 0) {
  48784. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  48785. + in_ep_cntr++;
  48786. + /**
  48787. + * @todo NGS: Add direction to EP, based on contents
  48788. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  48789. + * sprintf(";r
  48790. + */
  48791. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  48792. +
  48793. + DWC_CIRCLEQ_INIT(&ep->queue);
  48794. + }
  48795. + hwcfg1 >>= 2;
  48796. + }
  48797. +
  48798. + out_ep_cntr = 0;
  48799. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  48800. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  48801. + if ((hwcfg1 & 0x1) == 0) {
  48802. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  48803. + out_ep_cntr++;
  48804. + /**
  48805. + * @todo NGS: Add direction to EP, based on contents
  48806. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  48807. + * sprintf(";r
  48808. + */
  48809. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  48810. + DWC_CIRCLEQ_INIT(&ep->queue);
  48811. + }
  48812. + hwcfg1 >>= 2;
  48813. + }
  48814. +
  48815. + pcd->ep0state = EP0_DISCONNECT;
  48816. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  48817. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  48818. +}
  48819. +
  48820. +/**
  48821. + * This function is called when the SRP timer expires. The SRP should
  48822. + * complete within 6 seconds.
  48823. + */
  48824. +static void srp_timeout(void *ptr)
  48825. +{
  48826. + gotgctl_data_t gotgctl;
  48827. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  48828. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  48829. +
  48830. + gotgctl.d32 = DWC_READ_REG32(addr);
  48831. +
  48832. + core_if->srp_timer_started = 0;
  48833. +
  48834. + if (core_if->adp_enable) {
  48835. + if (gotgctl.b.bsesvld == 0) {
  48836. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  48837. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  48838. + /* Power off the core */
  48839. + if (core_if->power_down == 2) {
  48840. + gpwrdn.b.pwrdnswtch = 1;
  48841. + DWC_MODIFY_REG32(&core_if->
  48842. + core_global_regs->gpwrdn,
  48843. + gpwrdn.d32, 0);
  48844. + }
  48845. +
  48846. + gpwrdn.d32 = 0;
  48847. + gpwrdn.b.pmuintsel = 1;
  48848. + gpwrdn.b.pmuactv = 1;
  48849. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  48850. + gpwrdn.d32);
  48851. + dwc_otg_adp_probe_start(core_if);
  48852. + } else {
  48853. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  48854. + core_if->op_state = B_PERIPHERAL;
  48855. + dwc_otg_core_init(core_if);
  48856. + dwc_otg_enable_global_interrupts(core_if);
  48857. + cil_pcd_start(core_if);
  48858. + }
  48859. + }
  48860. +
  48861. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  48862. + (core_if->core_params->i2c_enable)) {
  48863. + DWC_PRINTF("SRP Timeout\n");
  48864. +
  48865. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  48866. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  48867. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  48868. + }
  48869. +
  48870. + /* Clear Session Request */
  48871. + gotgctl.d32 = 0;
  48872. + gotgctl.b.sesreq = 1;
  48873. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  48874. + gotgctl.d32, 0);
  48875. +
  48876. + core_if->srp_success = 0;
  48877. + } else {
  48878. + __DWC_ERROR("Device not connected/responding\n");
  48879. + gotgctl.b.sesreq = 0;
  48880. + DWC_WRITE_REG32(addr, gotgctl.d32);
  48881. + }
  48882. + } else if (gotgctl.b.sesreq) {
  48883. + DWC_PRINTF("SRP Timeout\n");
  48884. +
  48885. + __DWC_ERROR("Device not connected/responding\n");
  48886. + gotgctl.b.sesreq = 0;
  48887. + DWC_WRITE_REG32(addr, gotgctl.d32);
  48888. + } else {
  48889. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  48890. + }
  48891. +}
  48892. +
  48893. +/**
  48894. + * Tasklet
  48895. + *
  48896. + */
  48897. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  48898. +
  48899. +static void start_xfer_tasklet_func(void *data)
  48900. +{
  48901. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  48902. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  48903. +
  48904. + int i;
  48905. + depctl_data_t diepctl;
  48906. +
  48907. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  48908. +
  48909. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  48910. +
  48911. + if (pcd->ep0.queue_sof) {
  48912. + pcd->ep0.queue_sof = 0;
  48913. + start_next_request(&pcd->ep0);
  48914. + // break;
  48915. + }
  48916. +
  48917. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  48918. + depctl_data_t diepctl;
  48919. + diepctl.d32 =
  48920. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  48921. +
  48922. + if (pcd->in_ep[i].queue_sof) {
  48923. + pcd->in_ep[i].queue_sof = 0;
  48924. + start_next_request(&pcd->in_ep[i]);
  48925. + // break;
  48926. + }
  48927. + }
  48928. +
  48929. + return;
  48930. +}
  48931. +
  48932. +/**
  48933. + * This function initialized the PCD portion of the driver.
  48934. + *
  48935. + */
  48936. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev)
  48937. +{
  48938. + struct device *dev = &otg_dev->os_dep.platformdev->dev;
  48939. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  48940. + dwc_otg_pcd_t *pcd = NULL;
  48941. + dwc_otg_dev_if_t *dev_if;
  48942. + int i;
  48943. +
  48944. + /*
  48945. + * Allocate PCD structure
  48946. + */
  48947. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  48948. +
  48949. + if (pcd == NULL) {
  48950. + return NULL;
  48951. + }
  48952. +
  48953. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  48954. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
  48955. +#else
  48956. + pcd->lock = DWC_SPINLOCK_ALLOC();
  48957. +#endif
  48958. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  48959. + pcd, core_if);//GRAYG
  48960. + if (!pcd->lock) {
  48961. + DWC_ERROR("Could not allocate lock for pcd");
  48962. + DWC_FREE(pcd);
  48963. + return NULL;
  48964. + }
  48965. + /* Set core_if's lock pointer to hcd->lock */
  48966. + core_if->lock = pcd->lock;
  48967. + pcd->core_if = core_if;
  48968. +
  48969. + dev_if = core_if->dev_if;
  48970. + dev_if->isoc_ep = NULL;
  48971. +
  48972. + if (core_if->hwcfg4.b.ded_fifo_en) {
  48973. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  48974. + } else {
  48975. + DWC_PRINTF("Shared Tx FIFO mode\n");
  48976. + }
  48977. +
  48978. + /*
  48979. + * Initialized the Core for Device mode here if there is nod ADP support.
  48980. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  48981. + */
  48982. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  48983. + dwc_otg_core_dev_init(core_if);
  48984. + }
  48985. +
  48986. + /*
  48987. + * Register the PCD Callbacks.
  48988. + */
  48989. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  48990. +
  48991. + /*
  48992. + * Initialize the DMA buffer for SETUP packets
  48993. + */
  48994. + if (GET_CORE_IF(pcd)->dma_enable) {
  48995. + pcd->setup_pkt =
  48996. + DWC_DMA_ALLOC(dev, sizeof(*pcd->setup_pkt) * 5,
  48997. + &pcd->setup_pkt_dma_handle);
  48998. + if (pcd->setup_pkt == NULL) {
  48999. + DWC_FREE(pcd);
  49000. + return NULL;
  49001. + }
  49002. +
  49003. + pcd->status_buf =
  49004. + DWC_DMA_ALLOC(dev, sizeof(uint16_t),
  49005. + &pcd->status_buf_dma_handle);
  49006. + if (pcd->status_buf == NULL) {
  49007. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
  49008. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  49009. + DWC_FREE(pcd);
  49010. + return NULL;
  49011. + }
  49012. +
  49013. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49014. + dev_if->setup_desc_addr[0] =
  49015. + dwc_otg_ep_alloc_desc_chain(dev,
  49016. + &dev_if->dma_setup_desc_addr[0], 1);
  49017. + dev_if->setup_desc_addr[1] =
  49018. + dwc_otg_ep_alloc_desc_chain(dev,
  49019. + &dev_if->dma_setup_desc_addr[1], 1);
  49020. + dev_if->in_desc_addr =
  49021. + dwc_otg_ep_alloc_desc_chain(dev,
  49022. + &dev_if->dma_in_desc_addr, 1);
  49023. + dev_if->out_desc_addr =
  49024. + dwc_otg_ep_alloc_desc_chain(dev,
  49025. + &dev_if->dma_out_desc_addr, 1);
  49026. + pcd->data_terminated = 0;
  49027. +
  49028. + if (dev_if->setup_desc_addr[0] == 0
  49029. + || dev_if->setup_desc_addr[1] == 0
  49030. + || dev_if->in_desc_addr == 0
  49031. + || dev_if->out_desc_addr == 0) {
  49032. +
  49033. + if (dev_if->out_desc_addr)
  49034. + dwc_otg_ep_free_desc_chain(dev,
  49035. + dev_if->out_desc_addr,
  49036. + dev_if->dma_out_desc_addr, 1);
  49037. + if (dev_if->in_desc_addr)
  49038. + dwc_otg_ep_free_desc_chain(dev,
  49039. + dev_if->in_desc_addr,
  49040. + dev_if->dma_in_desc_addr, 1);
  49041. + if (dev_if->setup_desc_addr[1])
  49042. + dwc_otg_ep_free_desc_chain(dev,
  49043. + dev_if->setup_desc_addr[1],
  49044. + dev_if->dma_setup_desc_addr[1], 1);
  49045. + if (dev_if->setup_desc_addr[0])
  49046. + dwc_otg_ep_free_desc_chain(dev,
  49047. + dev_if->setup_desc_addr[0],
  49048. + dev_if->dma_setup_desc_addr[0], 1);
  49049. +
  49050. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
  49051. + pcd->setup_pkt,
  49052. + pcd->setup_pkt_dma_handle);
  49053. + DWC_DMA_FREE(dev, sizeof(*pcd->status_buf),
  49054. + pcd->status_buf,
  49055. + pcd->status_buf_dma_handle);
  49056. +
  49057. + DWC_FREE(pcd);
  49058. +
  49059. + return NULL;
  49060. + }
  49061. + }
  49062. + } else {
  49063. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  49064. + if (pcd->setup_pkt == NULL) {
  49065. + DWC_FREE(pcd);
  49066. + return NULL;
  49067. + }
  49068. +
  49069. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  49070. + if (pcd->status_buf == NULL) {
  49071. + DWC_FREE(pcd->setup_pkt);
  49072. + DWC_FREE(pcd);
  49073. + return NULL;
  49074. + }
  49075. + }
  49076. +
  49077. + dwc_otg_pcd_reinit(pcd);
  49078. +
  49079. + /* Allocate the cfi object for the PCD */
  49080. +#ifdef DWC_UTE_CFI
  49081. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  49082. + if (NULL == pcd->cfi)
  49083. + goto fail;
  49084. + if (init_cfi(pcd->cfi)) {
  49085. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  49086. + goto fail;
  49087. + }
  49088. +#endif
  49089. +
  49090. + /* Initialize tasklets */
  49091. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  49092. + start_xfer_tasklet_func, pcd);
  49093. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  49094. + do_test_mode, pcd);
  49095. +
  49096. + /* Initialize SRP timer */
  49097. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  49098. +
  49099. + if (core_if->core_params->dev_out_nak) {
  49100. + /**
  49101. + * Initialize xfer timeout timer. Implemented for
  49102. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  49103. + */
  49104. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  49105. + pcd->core_if->ep_xfer_timer[i] =
  49106. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  49107. + &pcd->core_if->ep_xfer_info[i]);
  49108. + }
  49109. + }
  49110. +
  49111. + return pcd;
  49112. +#ifdef DWC_UTE_CFI
  49113. +fail:
  49114. +#endif
  49115. + if (pcd->setup_pkt)
  49116. + DWC_FREE(pcd->setup_pkt);
  49117. + if (pcd->status_buf)
  49118. + DWC_FREE(pcd->status_buf);
  49119. +#ifdef DWC_UTE_CFI
  49120. + if (pcd->cfi)
  49121. + DWC_FREE(pcd->cfi);
  49122. +#endif
  49123. + if (pcd)
  49124. + DWC_FREE(pcd);
  49125. + return NULL;
  49126. +
  49127. +}
  49128. +
  49129. +/**
  49130. + * Remove PCD specific data
  49131. + */
  49132. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  49133. +{
  49134. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  49135. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49136. + int i;
  49137. +
  49138. + if (pcd->core_if->core_params->dev_out_nak) {
  49139. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  49140. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  49141. + pcd->core_if->ep_xfer_info[i].state = 0;
  49142. + }
  49143. + }
  49144. +
  49145. + if (GET_CORE_IF(pcd)->dma_enable) {
  49146. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  49147. + pcd->setup_pkt_dma_handle);
  49148. + DWC_DMA_FREE(dev, sizeof(uint16_t), pcd->status_buf,
  49149. + pcd->status_buf_dma_handle);
  49150. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49151. + dwc_otg_ep_free_desc_chain(dev,
  49152. + dev_if->setup_desc_addr[0],
  49153. + dev_if->dma_setup_desc_addr
  49154. + [0], 1);
  49155. + dwc_otg_ep_free_desc_chain(dev,
  49156. + dev_if->setup_desc_addr[1],
  49157. + dev_if->dma_setup_desc_addr
  49158. + [1], 1);
  49159. + dwc_otg_ep_free_desc_chain(dev,
  49160. + dev_if->in_desc_addr,
  49161. + dev_if->dma_in_desc_addr, 1);
  49162. + dwc_otg_ep_free_desc_chain(dev,
  49163. + dev_if->out_desc_addr,
  49164. + dev_if->dma_out_desc_addr,
  49165. + 1);
  49166. + }
  49167. + } else {
  49168. + DWC_FREE(pcd->setup_pkt);
  49169. + DWC_FREE(pcd->status_buf);
  49170. + }
  49171. + DWC_SPINLOCK_FREE(pcd->lock);
  49172. + /* Set core_if's lock pointer to NULL */
  49173. + pcd->core_if->lock = NULL;
  49174. +
  49175. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  49176. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  49177. + if (pcd->core_if->core_params->dev_out_nak) {
  49178. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  49179. + if (pcd->core_if->ep_xfer_timer[i]) {
  49180. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  49181. + }
  49182. + }
  49183. + }
  49184. +
  49185. +/* Release the CFI object's dynamic memory */
  49186. +#ifdef DWC_UTE_CFI
  49187. + if (pcd->cfi->ops.release) {
  49188. + pcd->cfi->ops.release(pcd->cfi);
  49189. + }
  49190. +#endif
  49191. +
  49192. + DWC_FREE(pcd);
  49193. +}
  49194. +
  49195. +/**
  49196. + * Returns whether registered pcd is dual speed or not
  49197. + */
  49198. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  49199. +{
  49200. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  49201. +
  49202. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  49203. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  49204. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  49205. + (core_if->core_params->ulpi_fs_ls))) {
  49206. + return 0;
  49207. + }
  49208. +
  49209. + return 1;
  49210. +}
  49211. +
  49212. +/**
  49213. + * Returns whether registered pcd is OTG capable or not
  49214. + */
  49215. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  49216. +{
  49217. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  49218. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  49219. +
  49220. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  49221. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  49222. + return 0;
  49223. + }
  49224. +
  49225. + return 1;
  49226. +}
  49227. +
  49228. +/**
  49229. + * This function assigns periodic Tx FIFO to an periodic EP
  49230. + * in shared Tx FIFO mode
  49231. + */
  49232. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  49233. +{
  49234. + uint32_t TxMsk = 1;
  49235. + int i;
  49236. +
  49237. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  49238. + if ((TxMsk & core_if->tx_msk) == 0) {
  49239. + core_if->tx_msk |= TxMsk;
  49240. + return i + 1;
  49241. + }
  49242. + TxMsk <<= 1;
  49243. + }
  49244. + return 0;
  49245. +}
  49246. +
  49247. +/**
  49248. + * This function assigns periodic Tx FIFO to an periodic EP
  49249. + * in shared Tx FIFO mode
  49250. + */
  49251. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  49252. +{
  49253. + uint32_t PerTxMsk = 1;
  49254. + int i;
  49255. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  49256. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  49257. + core_if->p_tx_msk |= PerTxMsk;
  49258. + return i + 1;
  49259. + }
  49260. + PerTxMsk <<= 1;
  49261. + }
  49262. + return 0;
  49263. +}
  49264. +
  49265. +/**
  49266. + * This function releases periodic Tx FIFO
  49267. + * in shared Tx FIFO mode
  49268. + */
  49269. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  49270. + uint32_t fifo_num)
  49271. +{
  49272. + core_if->p_tx_msk =
  49273. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  49274. +}
  49275. +
  49276. +/**
  49277. + * This function releases periodic Tx FIFO
  49278. + * in shared Tx FIFO mode
  49279. + */
  49280. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  49281. +{
  49282. + core_if->tx_msk =
  49283. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  49284. +}
  49285. +
  49286. +/**
  49287. + * This function is being called from gadget
  49288. + * to enable PCD endpoint.
  49289. + */
  49290. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  49291. + const uint8_t * ep_desc, void *usb_ep)
  49292. +{
  49293. + int num, dir;
  49294. + dwc_otg_pcd_ep_t *ep = NULL;
  49295. + const usb_endpoint_descriptor_t *desc;
  49296. + dwc_irqflags_t flags;
  49297. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  49298. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  49299. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  49300. + int retval = 0;
  49301. + int i, epcount;
  49302. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49303. +
  49304. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  49305. +
  49306. + if (!desc) {
  49307. + pcd->ep0.priv = usb_ep;
  49308. + ep = &pcd->ep0;
  49309. + retval = -DWC_E_INVALID;
  49310. + goto out;
  49311. + }
  49312. +
  49313. + num = UE_GET_ADDR(desc->bEndpointAddress);
  49314. + dir = UE_GET_DIR(desc->bEndpointAddress);
  49315. +
  49316. + if (!desc->wMaxPacketSize) {
  49317. + DWC_WARN("bad maxpacketsize\n");
  49318. + retval = -DWC_E_INVALID;
  49319. + goto out;
  49320. + }
  49321. +
  49322. + if (dir == UE_DIR_IN) {
  49323. + epcount = pcd->core_if->dev_if->num_in_eps;
  49324. + for (i = 0; i < epcount; i++) {
  49325. + if (num == pcd->in_ep[i].dwc_ep.num) {
  49326. + ep = &pcd->in_ep[i];
  49327. + break;
  49328. + }
  49329. + }
  49330. + } else {
  49331. + epcount = pcd->core_if->dev_if->num_out_eps;
  49332. + for (i = 0; i < epcount; i++) {
  49333. + if (num == pcd->out_ep[i].dwc_ep.num) {
  49334. + ep = &pcd->out_ep[i];
  49335. + break;
  49336. + }
  49337. + }
  49338. + }
  49339. +
  49340. + if (!ep) {
  49341. + DWC_WARN("bad address\n");
  49342. + retval = -DWC_E_INVALID;
  49343. + goto out;
  49344. + }
  49345. +
  49346. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49347. +
  49348. + ep->desc = desc;
  49349. + ep->priv = usb_ep;
  49350. +
  49351. + /*
  49352. + * Activate the EP
  49353. + */
  49354. + ep->stopped = 0;
  49355. +
  49356. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  49357. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  49358. +
  49359. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  49360. +
  49361. + if (ep->dwc_ep.is_in) {
  49362. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49363. + ep->dwc_ep.tx_fifo_num = 0;
  49364. +
  49365. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  49366. + /*
  49367. + * if ISOC EP then assign a Periodic Tx FIFO.
  49368. + */
  49369. + ep->dwc_ep.tx_fifo_num =
  49370. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  49371. + }
  49372. + } else {
  49373. + /*
  49374. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  49375. + */
  49376. + ep->dwc_ep.tx_fifo_num =
  49377. + assign_tx_fifo(GET_CORE_IF(pcd));
  49378. + }
  49379. +
  49380. + /* Calculating EP info controller base address */
  49381. + if (ep->dwc_ep.tx_fifo_num
  49382. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49383. + gdfifocfg.d32 =
  49384. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  49385. + core_global_regs->gdfifocfg);
  49386. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  49387. + dptxfsiz.d32 =
  49388. + (DWC_READ_REG32
  49389. + (&GET_CORE_IF(pcd)->core_global_regs->
  49390. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  49391. + gdfifocfg.b.epinfobase =
  49392. + gdfifocfgbase.d32 + dptxfsiz.d32;
  49393. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  49394. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  49395. + core_global_regs->gdfifocfg,
  49396. + gdfifocfg.d32);
  49397. + }
  49398. + }
  49399. + }
  49400. + /* Set initial data PID. */
  49401. + if (ep->dwc_ep.type == UE_BULK) {
  49402. + ep->dwc_ep.data_pid_start = 0;
  49403. + }
  49404. +
  49405. + /* Alloc DMA Descriptors */
  49406. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49407. +#ifndef DWC_UTE_PER_IO
  49408. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  49409. +#endif
  49410. + ep->dwc_ep.desc_addr =
  49411. + dwc_otg_ep_alloc_desc_chain(dev,
  49412. + &ep->dwc_ep.dma_desc_addr,
  49413. + MAX_DMA_DESC_CNT);
  49414. + if (!ep->dwc_ep.desc_addr) {
  49415. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  49416. + __func__);
  49417. + retval = -DWC_E_SHUTDOWN;
  49418. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49419. + goto out;
  49420. + }
  49421. +#ifndef DWC_UTE_PER_IO
  49422. + }
  49423. +#endif
  49424. + }
  49425. +
  49426. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  49427. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  49428. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  49429. +#ifdef DWC_UTE_PER_IO
  49430. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  49431. +#endif
  49432. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  49433. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  49434. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  49435. + }
  49436. +
  49437. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  49438. +
  49439. +#ifdef DWC_UTE_CFI
  49440. + if (pcd->cfi->ops.ep_enable) {
  49441. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  49442. + }
  49443. +#endif
  49444. +
  49445. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49446. +
  49447. +out:
  49448. + return retval;
  49449. +}
  49450. +
  49451. +/**
  49452. + * This function is being called from gadget
  49453. + * to disable PCD endpoint.
  49454. + */
  49455. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  49456. +{
  49457. + dwc_otg_pcd_ep_t *ep;
  49458. + dwc_irqflags_t flags;
  49459. + dwc_otg_dev_dma_desc_t *desc_addr;
  49460. + dwc_dma_t dma_desc_addr;
  49461. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  49462. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  49463. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  49464. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49465. +
  49466. + ep = get_ep_from_handle(pcd, ep_handle);
  49467. +
  49468. + if (!ep || !ep->desc) {
  49469. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  49470. + return -DWC_E_INVALID;
  49471. + }
  49472. +
  49473. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49474. +
  49475. + dwc_otg_request_nuke(ep);
  49476. +
  49477. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  49478. + if (pcd->core_if->core_params->dev_out_nak) {
  49479. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  49480. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  49481. + }
  49482. + ep->desc = NULL;
  49483. + ep->stopped = 1;
  49484. +
  49485. + gdfifocfg.d32 =
  49486. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  49487. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  49488. +
  49489. + if (ep->dwc_ep.is_in) {
  49490. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49491. + /* Flush the Tx FIFO */
  49492. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  49493. + ep->dwc_ep.tx_fifo_num);
  49494. + }
  49495. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  49496. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  49497. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49498. + /* Decreasing EPinfo Base Addr */
  49499. + dptxfsiz.d32 =
  49500. + (DWC_READ_REG32
  49501. + (&GET_CORE_IF(pcd)->
  49502. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  49503. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  49504. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  49505. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  49506. + gdfifocfg.d32);
  49507. + }
  49508. + }
  49509. + }
  49510. +
  49511. + /* Free DMA Descriptors */
  49512. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49513. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  49514. + desc_addr = ep->dwc_ep.desc_addr;
  49515. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  49516. +
  49517. + /* Cannot call dma_free_coherent() with IRQs disabled */
  49518. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49519. + dwc_otg_ep_free_desc_chain(dev, desc_addr, dma_desc_addr,
  49520. + MAX_DMA_DESC_CNT);
  49521. +
  49522. + goto out_unlocked;
  49523. + }
  49524. + }
  49525. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49526. +
  49527. +out_unlocked:
  49528. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  49529. + ep->dwc_ep.is_in ? "IN" : "OUT");
  49530. + return 0;
  49531. +
  49532. +}
  49533. +
  49534. +/******************************************************************************/
  49535. +#ifdef DWC_UTE_PER_IO
  49536. +
  49537. +/**
  49538. + * Free the request and its extended parts
  49539. + *
  49540. + */
  49541. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  49542. +{
  49543. + DWC_FREE(req->ext_req.per_io_frame_descs);
  49544. + DWC_FREE(req);
  49545. +}
  49546. +
  49547. +/**
  49548. + * Start the next request in the endpoint's queue.
  49549. + *
  49550. + */
  49551. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  49552. + dwc_otg_pcd_ep_t * ep)
  49553. +{
  49554. + int i;
  49555. + dwc_otg_pcd_request_t *req = NULL;
  49556. + dwc_ep_t *dwcep = NULL;
  49557. + struct dwc_iso_xreq_port *ereq = NULL;
  49558. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  49559. + uint16_t nat;
  49560. + depctl_data_t diepctl;
  49561. +
  49562. + dwcep = &ep->dwc_ep;
  49563. +
  49564. + if (dwcep->xiso_active_xfers > 0) {
  49565. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  49566. + DWC_WARN("There are currently active transfers for EP%d \
  49567. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  49568. + dwcep->xiso_queued_xfers);
  49569. +#endif
  49570. + return 0;
  49571. + }
  49572. +
  49573. + nat = UGETW(ep->desc->wMaxPacketSize);
  49574. + nat = (nat >> 11) & 0x03;
  49575. +
  49576. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49577. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  49578. + ereq = &req->ext_req;
  49579. + ep->stopped = 0;
  49580. +
  49581. + /* Get the frame number */
  49582. + dwcep->xiso_frame_num =
  49583. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  49584. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  49585. +
  49586. + ddesc_iso = ereq->per_io_frame_descs;
  49587. +
  49588. + if (dwcep->is_in) {
  49589. + /* Setup DMA Descriptor chain for IN Isoc request */
  49590. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49591. + //if ((i % (nat + 1)) == 0)
  49592. + if ( i > 0 )
  49593. + dwcep->xiso_frame_num =
  49594. + (dwcep->xiso_bInterval +
  49595. + dwcep->xiso_frame_num) & 0x3FFF;
  49596. + dwcep->desc_addr[i].buf =
  49597. + req->dma + ddesc_iso[i].offset;
  49598. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  49599. + ddesc_iso[i].length;
  49600. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  49601. + dwcep->xiso_frame_num;
  49602. + dwcep->desc_addr[i].status.b_iso_in.bs =
  49603. + BS_HOST_READY;
  49604. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  49605. + dwcep->desc_addr[i].status.b_iso_in.sp =
  49606. + (ddesc_iso[i].length %
  49607. + dwcep->maxpacket) ? 1 : 0;
  49608. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  49609. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  49610. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  49611. +
  49612. + /* Process the last descriptor */
  49613. + if (i == ereq->pio_pkt_count - 1) {
  49614. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  49615. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  49616. + }
  49617. + }
  49618. +
  49619. + /* Setup and start the transfer for this endpoint */
  49620. + dwcep->xiso_active_xfers++;
  49621. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  49622. + in_ep_regs[dwcep->num]->diepdma,
  49623. + dwcep->dma_desc_addr);
  49624. + diepctl.d32 = 0;
  49625. + diepctl.b.epena = 1;
  49626. + diepctl.b.cnak = 1;
  49627. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  49628. + in_ep_regs[dwcep->num]->diepctl, 0,
  49629. + diepctl.d32);
  49630. + } else {
  49631. + /* Setup DMA Descriptor chain for OUT Isoc request */
  49632. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49633. + //if ((i % (nat + 1)) == 0)
  49634. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  49635. + dwcep->xiso_frame_num) & 0x3FFF;
  49636. + dwcep->desc_addr[i].buf =
  49637. + req->dma + ddesc_iso[i].offset;
  49638. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  49639. + ddesc_iso[i].length;
  49640. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  49641. + dwcep->xiso_frame_num;
  49642. + dwcep->desc_addr[i].status.b_iso_out.bs =
  49643. + BS_HOST_READY;
  49644. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  49645. + dwcep->desc_addr[i].status.b_iso_out.sp =
  49646. + (ddesc_iso[i].length %
  49647. + dwcep->maxpacket) ? 1 : 0;
  49648. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  49649. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  49650. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  49651. +
  49652. + /* Process the last descriptor */
  49653. + if (i == ereq->pio_pkt_count - 1) {
  49654. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  49655. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  49656. + }
  49657. + }
  49658. +
  49659. + /* Setup and start the transfer for this endpoint */
  49660. + dwcep->xiso_active_xfers++;
  49661. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  49662. + dev_if->out_ep_regs[dwcep->num]->
  49663. + doepdma, dwcep->dma_desc_addr);
  49664. + diepctl.d32 = 0;
  49665. + diepctl.b.epena = 1;
  49666. + diepctl.b.cnak = 1;
  49667. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49668. + dev_if->out_ep_regs[dwcep->num]->
  49669. + doepctl, 0, diepctl.d32);
  49670. + }
  49671. +
  49672. + } else {
  49673. + ep->stopped = 1;
  49674. + }
  49675. +
  49676. + return 0;
  49677. +}
  49678. +
  49679. +/**
  49680. + * - Remove the request from the queue
  49681. + */
  49682. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  49683. +{
  49684. + dwc_otg_pcd_request_t *req = NULL;
  49685. + struct dwc_iso_xreq_port *ereq = NULL;
  49686. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  49687. + dwc_ep_t *dwcep = NULL;
  49688. + int i;
  49689. +
  49690. + //DWC_DEBUG();
  49691. + dwcep = &ep->dwc_ep;
  49692. +
  49693. + /* Get the first pending request from the queue */
  49694. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49695. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  49696. + if (!req) {
  49697. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  49698. + return;
  49699. + }
  49700. + dwcep->xiso_active_xfers--;
  49701. + dwcep->xiso_queued_xfers--;
  49702. + /* Remove this request from the queue */
  49703. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  49704. + } else {
  49705. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  49706. + return;
  49707. + }
  49708. +
  49709. + ep->stopped = 1;
  49710. + ereq = &req->ext_req;
  49711. + ddesc_iso = ereq->per_io_frame_descs;
  49712. +
  49713. + if (dwcep->xiso_active_xfers < 0) {
  49714. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  49715. + dwcep->xiso_active_xfers);
  49716. + }
  49717. +
  49718. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  49719. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49720. + if (dwcep->is_in) { /* IN endpoints */
  49721. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  49722. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  49723. + ddesc_iso[i].status =
  49724. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  49725. + } else { /* OUT endpoints */
  49726. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  49727. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  49728. + ddesc_iso[i].status =
  49729. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  49730. + }
  49731. + }
  49732. +
  49733. + DWC_SPINUNLOCK(ep->pcd->lock);
  49734. +
  49735. + /* Call the completion function in the non-portable logic */
  49736. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  49737. + &req->ext_req);
  49738. +
  49739. + DWC_SPINLOCK(ep->pcd->lock);
  49740. +
  49741. + /* Free the request - specific freeing needed for extended request object */
  49742. + dwc_pcd_xiso_ereq_free(ep, req);
  49743. +
  49744. + /* Start the next request */
  49745. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  49746. +
  49747. + return;
  49748. +}
  49749. +
  49750. +/**
  49751. + * Create and initialize the Isoc pkt descriptors of the extended request.
  49752. + *
  49753. + */
  49754. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  49755. + void *ereq_nonport,
  49756. + int atomic_alloc)
  49757. +{
  49758. + struct dwc_iso_xreq_port *ereq = NULL;
  49759. + struct dwc_iso_xreq_port *req_mapped = NULL;
  49760. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  49761. + uint32_t pkt_count;
  49762. + int i;
  49763. +
  49764. + ereq = &req->ext_req;
  49765. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  49766. + pkt_count = req_mapped->pio_pkt_count;
  49767. +
  49768. + /* Create the isoc descs */
  49769. + if (atomic_alloc) {
  49770. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  49771. + } else {
  49772. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  49773. + }
  49774. +
  49775. + if (!ipds) {
  49776. + DWC_ERROR("Failed to allocate isoc descriptors");
  49777. + return -DWC_E_NO_MEMORY;
  49778. + }
  49779. +
  49780. + /* Initialize the extended request fields */
  49781. + ereq->per_io_frame_descs = ipds;
  49782. + ereq->error_count = 0;
  49783. + ereq->pio_alloc_pkt_count = pkt_count;
  49784. + ereq->pio_pkt_count = pkt_count;
  49785. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  49786. +
  49787. + /* Init the Isoc descriptors */
  49788. + for (i = 0; i < pkt_count; i++) {
  49789. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  49790. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  49791. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  49792. + ipds[i].actual_length =
  49793. + req_mapped->per_io_frame_descs[i].actual_length;
  49794. + }
  49795. +
  49796. + return 0;
  49797. +}
  49798. +
  49799. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  49800. +{
  49801. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  49802. + int i;
  49803. +
  49804. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  49805. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  49806. + DWC_DEBUG("error_count=%d", ereq->error_count);
  49807. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  49808. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  49809. + DWC_DEBUG("res=%d", ereq->res);
  49810. +
  49811. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49812. + xfd = &ereq->per_io_frame_descs[0];
  49813. + DWC_DEBUG("FD #%d", i);
  49814. +
  49815. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  49816. + DWC_DEBUG("xfd->length=%d", xfd->length);
  49817. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  49818. + DWC_DEBUG("xfd->status=%d", xfd->status);
  49819. + }
  49820. +}
  49821. +
  49822. +/**
  49823. + *
  49824. + */
  49825. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49826. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  49827. + int zero, void *req_handle, int atomic_alloc,
  49828. + void *ereq_nonport)
  49829. +{
  49830. + dwc_otg_pcd_request_t *req = NULL;
  49831. + dwc_otg_pcd_ep_t *ep;
  49832. + dwc_irqflags_t flags;
  49833. + int res;
  49834. +
  49835. + ep = get_ep_from_handle(pcd, ep_handle);
  49836. + if (!ep) {
  49837. + DWC_WARN("bad ep\n");
  49838. + return -DWC_E_INVALID;
  49839. + }
  49840. +
  49841. + /* We support this extension only for DDMA mode */
  49842. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  49843. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  49844. + return -DWC_E_INVALID;
  49845. +
  49846. + /* Create a dwc_otg_pcd_request_t object */
  49847. + if (atomic_alloc) {
  49848. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  49849. + } else {
  49850. + req = DWC_ALLOC(sizeof(*req));
  49851. + }
  49852. +
  49853. + if (!req) {
  49854. + return -DWC_E_NO_MEMORY;
  49855. + }
  49856. +
  49857. + /* Create the Isoc descs for this request which shall be the exact match
  49858. + * of the structure sent to us from the non-portable logic */
  49859. + res =
  49860. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  49861. + if (res) {
  49862. + DWC_WARN("Failed to init the Isoc descriptors");
  49863. + DWC_FREE(req);
  49864. + return res;
  49865. + }
  49866. +
  49867. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49868. +
  49869. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  49870. + req->buf = buf;
  49871. + req->dma = dma_buf;
  49872. + req->length = buflen;
  49873. + req->sent_zlp = zero;
  49874. + req->priv = req_handle;
  49875. +
  49876. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49877. + ep->dwc_ep.dma_addr = dma_buf;
  49878. + ep->dwc_ep.start_xfer_buff = buf;
  49879. + ep->dwc_ep.xfer_buff = buf;
  49880. + ep->dwc_ep.xfer_len = 0;
  49881. + ep->dwc_ep.xfer_count = 0;
  49882. + ep->dwc_ep.sent_zlp = 0;
  49883. + ep->dwc_ep.total_len = buflen;
  49884. +
  49885. + /* Add this request to the tail */
  49886. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49887. + ep->dwc_ep.xiso_queued_xfers++;
  49888. +
  49889. +//DWC_DEBUG("CP_0");
  49890. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  49891. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  49892. +//prn_ext_request(&req->ext_req);
  49893. +
  49894. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49895. +
  49896. + /* If the req->status == ASAP then check if there is any active transfer
  49897. + * for this endpoint. If no active transfers, then get the first entry
  49898. + * from the queue and start that transfer
  49899. + */
  49900. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  49901. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  49902. + if (res) {
  49903. + DWC_WARN("Failed to start the next Isoc transfer");
  49904. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49905. + DWC_FREE(req);
  49906. + return res;
  49907. + }
  49908. + }
  49909. +
  49910. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49911. + return 0;
  49912. +}
  49913. +
  49914. +#endif
  49915. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  49916. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49917. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  49918. + int zero, void *req_handle, int atomic_alloc)
  49919. +{
  49920. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49921. + dwc_irqflags_t flags;
  49922. + dwc_otg_pcd_request_t *req;
  49923. + dwc_otg_pcd_ep_t *ep;
  49924. + uint32_t max_transfer;
  49925. +
  49926. + ep = get_ep_from_handle(pcd, ep_handle);
  49927. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  49928. + DWC_WARN("bad ep\n");
  49929. + return -DWC_E_INVALID;
  49930. + }
  49931. +
  49932. + if (atomic_alloc) {
  49933. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  49934. + } else {
  49935. + req = DWC_ALLOC(sizeof(*req));
  49936. + }
  49937. +
  49938. + if (!req) {
  49939. + return -DWC_E_NO_MEMORY;
  49940. + }
  49941. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  49942. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  49943. + if (ep->dwc_ep.num != 0) {
  49944. + DWC_ERROR("queue req %p, len %d buf %p\n",
  49945. + req_handle, buflen, buf);
  49946. + }
  49947. + }
  49948. +
  49949. + req->buf = buf;
  49950. + req->dma = dma_buf;
  49951. + req->length = buflen;
  49952. + req->sent_zlp = zero;
  49953. + req->priv = req_handle;
  49954. + req->dw_align_buf = NULL;
  49955. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  49956. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  49957. + req->dw_align_buf = DWC_DMA_ALLOC(dev, buflen,
  49958. + &req->dw_align_buf_dma);
  49959. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49960. +
  49961. + /*
  49962. + * After adding request to the queue for IN ISOC wait for In Token Received
  49963. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  49964. + * Received when EP is disabled interrupt to obtain starting microframe
  49965. + * (odd/even) start transfer
  49966. + */
  49967. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  49968. + if (req != 0) {
  49969. + depctl_data_t depctl = {.d32 =
  49970. + DWC_READ_REG32(&pcd->core_if->dev_if->
  49971. + in_ep_regs[ep->dwc_ep.num]->
  49972. + diepctl) };
  49973. + ++pcd->request_pending;
  49974. +
  49975. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49976. + if (ep->dwc_ep.is_in) {
  49977. + depctl.b.cnak = 1;
  49978. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  49979. + in_ep_regs[ep->dwc_ep.num]->
  49980. + diepctl, depctl.d32);
  49981. + }
  49982. +
  49983. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49984. + }
  49985. + return 0;
  49986. + }
  49987. +
  49988. + /*
  49989. + * For EP0 IN without premature status, zlp is required?
  49990. + */
  49991. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  49992. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  49993. + //_req->zero = 1;
  49994. + }
  49995. +
  49996. + /* Start the transfer */
  49997. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  49998. + /* EP0 Transfer? */
  49999. + if (ep->dwc_ep.num == 0) {
  50000. + switch (pcd->ep0state) {
  50001. + case EP0_IN_DATA_PHASE:
  50002. + DWC_DEBUGPL(DBG_PCD,
  50003. + "%s ep0: EP0_IN_DATA_PHASE\n",
  50004. + __func__);
  50005. + break;
  50006. +
  50007. + case EP0_OUT_DATA_PHASE:
  50008. + DWC_DEBUGPL(DBG_PCD,
  50009. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  50010. + __func__);
  50011. + if (pcd->request_config) {
  50012. + /* Complete STATUS PHASE */
  50013. + ep->dwc_ep.is_in = 1;
  50014. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  50015. + }
  50016. + break;
  50017. +
  50018. + case EP0_IN_STATUS_PHASE:
  50019. + DWC_DEBUGPL(DBG_PCD,
  50020. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  50021. + __func__);
  50022. + break;
  50023. +
  50024. + default:
  50025. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  50026. + pcd->ep0state);
  50027. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50028. + return -DWC_E_SHUTDOWN;
  50029. + }
  50030. +
  50031. + ep->dwc_ep.dma_addr = dma_buf;
  50032. + ep->dwc_ep.start_xfer_buff = buf;
  50033. + ep->dwc_ep.xfer_buff = buf;
  50034. + ep->dwc_ep.xfer_len = buflen;
  50035. + ep->dwc_ep.xfer_count = 0;
  50036. + ep->dwc_ep.sent_zlp = 0;
  50037. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  50038. +
  50039. + if (zero) {
  50040. + if ((ep->dwc_ep.xfer_len %
  50041. + ep->dwc_ep.maxpacket == 0)
  50042. + && (ep->dwc_ep.xfer_len != 0)) {
  50043. + ep->dwc_ep.sent_zlp = 1;
  50044. + }
  50045. +
  50046. + }
  50047. +
  50048. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  50049. + &ep->dwc_ep);
  50050. + } // non-ep0 endpoints
  50051. + else {
  50052. +#ifdef DWC_UTE_CFI
  50053. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  50054. + /* store the request length */
  50055. + ep->dwc_ep.cfi_req_len = buflen;
  50056. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  50057. + ep, req);
  50058. + } else {
  50059. +#endif
  50060. + max_transfer =
  50061. + GET_CORE_IF(ep->pcd)->core_params->
  50062. + max_transfer_size;
  50063. +
  50064. + /* Setup and start the Transfer */
  50065. + if (req->dw_align_buf){
  50066. + if (ep->dwc_ep.is_in)
  50067. + dwc_memcpy(req->dw_align_buf,
  50068. + buf, buflen);
  50069. + ep->dwc_ep.dma_addr =
  50070. + req->dw_align_buf_dma;
  50071. + ep->dwc_ep.start_xfer_buff =
  50072. + req->dw_align_buf;
  50073. + ep->dwc_ep.xfer_buff =
  50074. + req->dw_align_buf;
  50075. + } else {
  50076. + ep->dwc_ep.dma_addr = dma_buf;
  50077. + ep->dwc_ep.start_xfer_buff = buf;
  50078. + ep->dwc_ep.xfer_buff = buf;
  50079. + }
  50080. + ep->dwc_ep.xfer_len = 0;
  50081. + ep->dwc_ep.xfer_count = 0;
  50082. + ep->dwc_ep.sent_zlp = 0;
  50083. + ep->dwc_ep.total_len = buflen;
  50084. +
  50085. + ep->dwc_ep.maxxfer = max_transfer;
  50086. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  50087. + uint32_t out_max_xfer =
  50088. + DDMA_MAX_TRANSFER_SIZE -
  50089. + (DDMA_MAX_TRANSFER_SIZE % 4);
  50090. + if (ep->dwc_ep.is_in) {
  50091. + if (ep->dwc_ep.maxxfer >
  50092. + DDMA_MAX_TRANSFER_SIZE) {
  50093. + ep->dwc_ep.maxxfer =
  50094. + DDMA_MAX_TRANSFER_SIZE;
  50095. + }
  50096. + } else {
  50097. + if (ep->dwc_ep.maxxfer >
  50098. + out_max_xfer) {
  50099. + ep->dwc_ep.maxxfer =
  50100. + out_max_xfer;
  50101. + }
  50102. + }
  50103. + }
  50104. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  50105. + ep->dwc_ep.maxxfer -=
  50106. + (ep->dwc_ep.maxxfer %
  50107. + ep->dwc_ep.maxpacket);
  50108. + }
  50109. +
  50110. + if (zero) {
  50111. + if ((ep->dwc_ep.total_len %
  50112. + ep->dwc_ep.maxpacket == 0)
  50113. + && (ep->dwc_ep.total_len != 0)) {
  50114. + ep->dwc_ep.sent_zlp = 1;
  50115. + }
  50116. + }
  50117. +#ifdef DWC_UTE_CFI
  50118. + }
  50119. +#endif
  50120. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  50121. + &ep->dwc_ep);
  50122. + }
  50123. + }
  50124. +
  50125. + if (req != 0) {
  50126. + ++pcd->request_pending;
  50127. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  50128. + if (ep->dwc_ep.is_in && ep->stopped
  50129. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  50130. + /** @todo NGS Create a function for this. */
  50131. + diepmsk_data_t diepmsk = {.d32 = 0 };
  50132. + diepmsk.b.intktxfemp = 1;
  50133. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  50134. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  50135. + dev_if->dev_global_regs->diepeachintmsk
  50136. + [ep->dwc_ep.num], 0,
  50137. + diepmsk.d32);
  50138. + } else {
  50139. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  50140. + dev_if->dev_global_regs->
  50141. + diepmsk, 0, diepmsk.d32);
  50142. + }
  50143. +
  50144. + }
  50145. + }
  50146. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50147. +
  50148. + return 0;
  50149. +}
  50150. +
  50151. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50152. + void *req_handle)
  50153. +{
  50154. + dwc_irqflags_t flags;
  50155. + dwc_otg_pcd_request_t *req;
  50156. + dwc_otg_pcd_ep_t *ep;
  50157. +
  50158. + ep = get_ep_from_handle(pcd, ep_handle);
  50159. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  50160. + DWC_WARN("bad argument\n");
  50161. + return -DWC_E_INVALID;
  50162. + }
  50163. +
  50164. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50165. +
  50166. + /* make sure it's actually queued on this endpoint */
  50167. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  50168. + if (req->priv == (void *)req_handle) {
  50169. + break;
  50170. + }
  50171. + }
  50172. +
  50173. + if (req->priv != (void *)req_handle) {
  50174. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50175. + return -DWC_E_INVALID;
  50176. + }
  50177. +
  50178. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  50179. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  50180. + } else {
  50181. + req = NULL;
  50182. + }
  50183. +
  50184. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50185. +
  50186. + return req ? 0 : -DWC_E_SHUTDOWN;
  50187. +
  50188. +}
  50189. +
  50190. +/**
  50191. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  50192. + *
  50193. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  50194. + * requests. If the gadget driver clears the halt status, it will
  50195. + * automatically unwedge the endpoint.
  50196. + *
  50197. + * Returns zero on success, else negative DWC error code.
  50198. + */
  50199. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  50200. +{
  50201. + dwc_otg_pcd_ep_t *ep;
  50202. + dwc_irqflags_t flags;
  50203. + int retval = 0;
  50204. +
  50205. + ep = get_ep_from_handle(pcd, ep_handle);
  50206. +
  50207. + if ((!ep->desc && ep != &pcd->ep0) ||
  50208. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  50209. + DWC_WARN("%s, bad ep\n", __func__);
  50210. + return -DWC_E_INVALID;
  50211. + }
  50212. +
  50213. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50214. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  50215. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  50216. + ep->dwc_ep.is_in ? "IN" : "OUT");
  50217. + retval = -DWC_E_AGAIN;
  50218. + } else {
  50219. + /* This code needs to be reviewed */
  50220. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  50221. + dtxfsts_data_t txstatus;
  50222. + fifosize_data_t txfifosize;
  50223. +
  50224. + txfifosize.d32 =
  50225. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  50226. + core_global_regs->dtxfsiz[ep->dwc_ep.
  50227. + tx_fifo_num]);
  50228. + txstatus.d32 =
  50229. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  50230. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  50231. + dtxfsts);
  50232. +
  50233. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  50234. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  50235. + retval = -DWC_E_AGAIN;
  50236. + } else {
  50237. + if (ep->dwc_ep.num == 0) {
  50238. + pcd->ep0state = EP0_STALL;
  50239. + }
  50240. +
  50241. + ep->stopped = 1;
  50242. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  50243. + &ep->dwc_ep);
  50244. + }
  50245. + } else {
  50246. + if (ep->dwc_ep.num == 0) {
  50247. + pcd->ep0state = EP0_STALL;
  50248. + }
  50249. +
  50250. + ep->stopped = 1;
  50251. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50252. + }
  50253. + }
  50254. +
  50255. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50256. +
  50257. + return retval;
  50258. +}
  50259. +
  50260. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  50261. +{
  50262. + dwc_otg_pcd_ep_t *ep;
  50263. + dwc_irqflags_t flags;
  50264. + int retval = 0;
  50265. +
  50266. + ep = get_ep_from_handle(pcd, ep_handle);
  50267. +
  50268. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  50269. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  50270. + DWC_WARN("%s, bad ep\n", __func__);
  50271. + return -DWC_E_INVALID;
  50272. + }
  50273. +
  50274. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50275. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  50276. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  50277. + ep->dwc_ep.is_in ? "IN" : "OUT");
  50278. + retval = -DWC_E_AGAIN;
  50279. + } else if (value == 0) {
  50280. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50281. + } else if (value == 1) {
  50282. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  50283. + dtxfsts_data_t txstatus;
  50284. + fifosize_data_t txfifosize;
  50285. +
  50286. + txfifosize.d32 =
  50287. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  50288. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  50289. + txstatus.d32 =
  50290. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  50291. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  50292. +
  50293. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  50294. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  50295. + retval = -DWC_E_AGAIN;
  50296. + } else {
  50297. + if (ep->dwc_ep.num == 0) {
  50298. + pcd->ep0state = EP0_STALL;
  50299. + }
  50300. +
  50301. + ep->stopped = 1;
  50302. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  50303. + &ep->dwc_ep);
  50304. + }
  50305. + } else {
  50306. + if (ep->dwc_ep.num == 0) {
  50307. + pcd->ep0state = EP0_STALL;
  50308. + }
  50309. +
  50310. + ep->stopped = 1;
  50311. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50312. + }
  50313. + } else if (value == 2) {
  50314. + ep->dwc_ep.stall_clear_flag = 0;
  50315. + } else if (value == 3) {
  50316. + ep->dwc_ep.stall_clear_flag = 1;
  50317. + }
  50318. +
  50319. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50320. +
  50321. + return retval;
  50322. +}
  50323. +
  50324. +/**
  50325. + * This function initiates remote wakeup of the host from suspend state.
  50326. + */
  50327. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  50328. +{
  50329. + dctl_data_t dctl = { 0 };
  50330. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50331. + dsts_data_t dsts;
  50332. +
  50333. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  50334. + if (!dsts.b.suspsts) {
  50335. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  50336. + }
  50337. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  50338. + if (pcd->remote_wakeup_enable) {
  50339. + if (set) {
  50340. +
  50341. + if (core_if->adp_enable) {
  50342. + gpwrdn_data_t gpwrdn;
  50343. +
  50344. + dwc_otg_adp_probe_stop(core_if);
  50345. +
  50346. + /* Mask SRP detected interrupt from Power Down Logic */
  50347. + gpwrdn.d32 = 0;
  50348. + gpwrdn.b.srp_det_msk = 1;
  50349. + DWC_MODIFY_REG32(&core_if->
  50350. + core_global_regs->gpwrdn,
  50351. + gpwrdn.d32, 0);
  50352. +
  50353. + /* Disable Power Down Logic */
  50354. + gpwrdn.d32 = 0;
  50355. + gpwrdn.b.pmuactv = 1;
  50356. + DWC_MODIFY_REG32(&core_if->
  50357. + core_global_regs->gpwrdn,
  50358. + gpwrdn.d32, 0);
  50359. +
  50360. + /*
  50361. + * Initialize the Core for Device mode.
  50362. + */
  50363. + core_if->op_state = B_PERIPHERAL;
  50364. + dwc_otg_core_init(core_if);
  50365. + dwc_otg_enable_global_interrupts(core_if);
  50366. + cil_pcd_start(core_if);
  50367. +
  50368. + dwc_otg_initiate_srp(core_if);
  50369. + }
  50370. +
  50371. + dctl.b.rmtwkupsig = 1;
  50372. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  50373. + dctl, 0, dctl.d32);
  50374. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  50375. +
  50376. + dwc_mdelay(2);
  50377. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  50378. + dctl, dctl.d32, 0);
  50379. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  50380. + }
  50381. + } else {
  50382. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  50383. + }
  50384. +}
  50385. +
  50386. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50387. +/**
  50388. + * This function initiates remote wakeup of the host from L1 sleep state.
  50389. + */
  50390. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  50391. +{
  50392. + glpmcfg_data_t lpmcfg;
  50393. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50394. +
  50395. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  50396. +
  50397. + /* Check if we are in L1 state */
  50398. + if (!lpmcfg.b.prt_sleep_sts) {
  50399. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  50400. + return;
  50401. + }
  50402. +
  50403. + /* Check if host allows remote wakeup */
  50404. + if (!lpmcfg.b.rem_wkup_en) {
  50405. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  50406. + return;
  50407. + }
  50408. +
  50409. + /* Check if Resume OK */
  50410. + if (!lpmcfg.b.sleep_state_resumeok) {
  50411. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  50412. + return;
  50413. + }
  50414. +
  50415. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  50416. + lpmcfg.b.en_utmi_sleep = 0;
  50417. + lpmcfg.b.hird_thres &= (~(1 << 4));
  50418. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  50419. +
  50420. + if (set) {
  50421. + dctl_data_t dctl = {.d32 = 0 };
  50422. + dctl.b.rmtwkupsig = 1;
  50423. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  50424. + * Hardware will automatically clear this bit.
  50425. + */
  50426. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  50427. + 0, dctl.d32);
  50428. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  50429. + }
  50430. +
  50431. +}
  50432. +#endif
  50433. +
  50434. +/**
  50435. + * Performs remote wakeup.
  50436. + */
  50437. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  50438. +{
  50439. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50440. + dwc_irqflags_t flags;
  50441. + if (dwc_otg_is_device_mode(core_if)) {
  50442. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50443. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50444. + if (core_if->lx_state == DWC_OTG_L1) {
  50445. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  50446. + } else {
  50447. +#endif
  50448. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  50449. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50450. + }
  50451. +#endif
  50452. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50453. + }
  50454. + return;
  50455. +}
  50456. +
  50457. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  50458. +{
  50459. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50460. + dctl_data_t dctl = { 0 };
  50461. +
  50462. + if (dwc_otg_is_device_mode(core_if)) {
  50463. + dctl.b.sftdiscon = 1;
  50464. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  50465. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  50466. + dwc_udelay(no_of_usecs);
  50467. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  50468. +
  50469. + } else{
  50470. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  50471. + }
  50472. + return;
  50473. +
  50474. +}
  50475. +
  50476. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  50477. +{
  50478. + dsts_data_t dsts;
  50479. + gotgctl_data_t gotgctl;
  50480. +
  50481. + /*
  50482. + * This function starts the Protocol if no session is in progress. If
  50483. + * a session is already in progress, but the device is suspended,
  50484. + * remote wakeup signaling is started.
  50485. + */
  50486. +
  50487. + /* Check if valid session */
  50488. + gotgctl.d32 =
  50489. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  50490. + if (gotgctl.b.bsesvld) {
  50491. + /* Check if suspend state */
  50492. + dsts.d32 =
  50493. + DWC_READ_REG32(&
  50494. + (GET_CORE_IF(pcd)->dev_if->
  50495. + dev_global_regs->dsts));
  50496. + if (dsts.b.suspsts) {
  50497. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  50498. + }
  50499. + } else {
  50500. + dwc_otg_pcd_initiate_srp(pcd);
  50501. + }
  50502. +
  50503. + return 0;
  50504. +
  50505. +}
  50506. +
  50507. +/**
  50508. + * Start the SRP timer to detect when the SRP does not complete within
  50509. + * 6 seconds.
  50510. + *
  50511. + * @param pcd the pcd structure.
  50512. + */
  50513. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  50514. +{
  50515. + dwc_irqflags_t flags;
  50516. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50517. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  50518. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50519. +}
  50520. +
  50521. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  50522. +{
  50523. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  50524. +}
  50525. +
  50526. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  50527. +{
  50528. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  50529. +}
  50530. +
  50531. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  50532. +{
  50533. + return pcd->b_hnp_enable;
  50534. +}
  50535. +
  50536. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  50537. +{
  50538. + return pcd->a_hnp_support;
  50539. +}
  50540. +
  50541. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  50542. +{
  50543. + return pcd->a_alt_hnp_support;
  50544. +}
  50545. +
  50546. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  50547. +{
  50548. + return pcd->remote_wakeup_enable;
  50549. +}
  50550. +
  50551. +#endif /* DWC_HOST_ONLY */
  50552. --- /dev/null
  50553. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  50554. @@ -0,0 +1,273 @@
  50555. +/* ==========================================================================
  50556. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  50557. + * $Revision: #48 $
  50558. + * $Date: 2012/08/10 $
  50559. + * $Change: 2047372 $
  50560. + *
  50561. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50562. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50563. + * otherwise expressly agreed to in writing between Synopsys and you.
  50564. + *
  50565. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50566. + * any End User Software License Agreement or Agreement for Licensed Product
  50567. + * with Synopsys or any supplement thereto. You are permitted to use and
  50568. + * redistribute this Software in source and binary forms, with or without
  50569. + * modification, provided that redistributions of source code must retain this
  50570. + * notice. You may not view, use, disclose, copy or distribute this file or
  50571. + * any information contained herein except pursuant to this license grant from
  50572. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50573. + * below, then you are not authorized to use the Software.
  50574. + *
  50575. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50576. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50577. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50578. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50579. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50580. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50581. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50582. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50583. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50584. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50585. + * DAMAGE.
  50586. + * ========================================================================== */
  50587. +#ifndef DWC_HOST_ONLY
  50588. +#if !defined(__DWC_PCD_H__)
  50589. +#define __DWC_PCD_H__
  50590. +
  50591. +#include "dwc_otg_os_dep.h"
  50592. +#include "usb.h"
  50593. +#include "dwc_otg_cil.h"
  50594. +#include "dwc_otg_pcd_if.h"
  50595. +#include "dwc_otg_driver.h"
  50596. +
  50597. +struct cfiobject;
  50598. +
  50599. +/**
  50600. + * @file
  50601. + *
  50602. + * This file contains the structures, constants, and interfaces for
  50603. + * the Perpherial Contoller Driver (PCD).
  50604. + *
  50605. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  50606. + * Gadget API, so that the existing Gadget drivers can be used. For
  50607. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  50608. + * (FBS) driver will be used. The FBS driver supports the
  50609. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  50610. + * transports.
  50611. + *
  50612. + */
  50613. +
  50614. +/** Invalid DMA Address */
  50615. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  50616. +
  50617. +/** Max Transfer size for any EP */
  50618. +#define DDMA_MAX_TRANSFER_SIZE 65535
  50619. +
  50620. +/**
  50621. + * Get the pointer to the core_if from the pcd pointer.
  50622. + */
  50623. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  50624. +
  50625. +/**
  50626. + * States of EP0.
  50627. + */
  50628. +typedef enum ep0_state {
  50629. + EP0_DISCONNECT, /* no host */
  50630. + EP0_IDLE,
  50631. + EP0_IN_DATA_PHASE,
  50632. + EP0_OUT_DATA_PHASE,
  50633. + EP0_IN_STATUS_PHASE,
  50634. + EP0_OUT_STATUS_PHASE,
  50635. + EP0_STALL,
  50636. +} ep0state_e;
  50637. +
  50638. +/** Fordward declaration.*/
  50639. +struct dwc_otg_pcd;
  50640. +
  50641. +/** DWC_otg iso request structure.
  50642. + *
  50643. + */
  50644. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  50645. +
  50646. +#ifdef DWC_UTE_PER_IO
  50647. +
  50648. +/**
  50649. + * This shall be the exact analogy of the same type structure defined in the
  50650. + * usb_gadget.h. Each descriptor contains
  50651. + */
  50652. +struct dwc_iso_pkt_desc_port {
  50653. + uint32_t offset;
  50654. + uint32_t length; /* expected length */
  50655. + uint32_t actual_length;
  50656. + uint32_t status;
  50657. +};
  50658. +
  50659. +struct dwc_iso_xreq_port {
  50660. + /** transfer/submission flag */
  50661. + uint32_t tr_sub_flags;
  50662. + /** Start the request ASAP */
  50663. +#define DWC_EREQ_TF_ASAP 0x00000002
  50664. + /** Just enqueue the request w/o initiating a transfer */
  50665. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  50666. +
  50667. + /**
  50668. + * count of ISO packets attached to this request - shall
  50669. + * not exceed the pio_alloc_pkt_count
  50670. + */
  50671. + uint32_t pio_pkt_count;
  50672. + /** count of ISO packets allocated for this request */
  50673. + uint32_t pio_alloc_pkt_count;
  50674. + /** number of ISO packet errors */
  50675. + uint32_t error_count;
  50676. + /** reserved for future extension */
  50677. + uint32_t res;
  50678. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  50679. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  50680. +};
  50681. +#endif
  50682. +/** DWC_otg request structure.
  50683. + * This structure is a list of requests.
  50684. + */
  50685. +typedef struct dwc_otg_pcd_request {
  50686. + void *priv;
  50687. + void *buf;
  50688. + dwc_dma_t dma;
  50689. + uint32_t length;
  50690. + uint32_t actual;
  50691. + unsigned sent_zlp:1;
  50692. + /**
  50693. + * Used instead of original buffer if
  50694. + * it(physical address) is not dword-aligned.
  50695. + **/
  50696. + uint8_t *dw_align_buf;
  50697. + dwc_dma_t dw_align_buf_dma;
  50698. +
  50699. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  50700. +#ifdef DWC_UTE_PER_IO
  50701. + struct dwc_iso_xreq_port ext_req;
  50702. + //void *priv_ereq_nport; /* */
  50703. +#endif
  50704. +} dwc_otg_pcd_request_t;
  50705. +
  50706. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  50707. +
  50708. +/** PCD EP structure.
  50709. + * This structure describes an EP, there is an array of EPs in the PCD
  50710. + * structure.
  50711. + */
  50712. +typedef struct dwc_otg_pcd_ep {
  50713. + /** USB EP Descriptor */
  50714. + const usb_endpoint_descriptor_t *desc;
  50715. +
  50716. + /** queue of dwc_otg_pcd_requests. */
  50717. + struct req_list queue;
  50718. + unsigned stopped:1;
  50719. + unsigned disabling:1;
  50720. + unsigned dma:1;
  50721. + unsigned queue_sof:1;
  50722. +
  50723. +#ifdef DWC_EN_ISOC
  50724. + /** ISOC req handle passed */
  50725. + void *iso_req_handle;
  50726. +#endif //_EN_ISOC_
  50727. +
  50728. + /** DWC_otg ep data. */
  50729. + dwc_ep_t dwc_ep;
  50730. +
  50731. + /** Pointer to PCD */
  50732. + struct dwc_otg_pcd *pcd;
  50733. +
  50734. + void *priv;
  50735. +} dwc_otg_pcd_ep_t;
  50736. +
  50737. +/** DWC_otg PCD Structure.
  50738. + * This structure encapsulates the data for the dwc_otg PCD.
  50739. + */
  50740. +struct dwc_otg_pcd {
  50741. + const struct dwc_otg_pcd_function_ops *fops;
  50742. + /** The DWC otg device pointer */
  50743. + struct dwc_otg_device *otg_dev;
  50744. + /** Core Interface */
  50745. + dwc_otg_core_if_t *core_if;
  50746. + /** State of EP0 */
  50747. + ep0state_e ep0state;
  50748. + /** EP0 Request is pending */
  50749. + unsigned ep0_pending:1;
  50750. + /** Indicates when SET CONFIGURATION Request is in process */
  50751. + unsigned request_config:1;
  50752. + /** The state of the Remote Wakeup Enable. */
  50753. + unsigned remote_wakeup_enable:1;
  50754. + /** The state of the B-Device HNP Enable. */
  50755. + unsigned b_hnp_enable:1;
  50756. + /** The state of A-Device HNP Support. */
  50757. + unsigned a_hnp_support:1;
  50758. + /** The state of the A-Device Alt HNP support. */
  50759. + unsigned a_alt_hnp_support:1;
  50760. + /** Count of pending Requests */
  50761. + unsigned request_pending;
  50762. +
  50763. + /** SETUP packet for EP0
  50764. + * This structure is allocated as a DMA buffer on PCD initialization
  50765. + * with enough space for up to 3 setup packets.
  50766. + */
  50767. + union {
  50768. + usb_device_request_t req;
  50769. + uint32_t d32[2];
  50770. + } *setup_pkt;
  50771. +
  50772. + dwc_dma_t setup_pkt_dma_handle;
  50773. +
  50774. + /* Additional buffer and flag for CTRL_WR premature case */
  50775. + uint8_t *backup_buf;
  50776. + unsigned data_terminated;
  50777. +
  50778. + /** 2-byte dma buffer used to return status from GET_STATUS */
  50779. + uint16_t *status_buf;
  50780. + dwc_dma_t status_buf_dma_handle;
  50781. +
  50782. + /** EP0 */
  50783. + dwc_otg_pcd_ep_t ep0;
  50784. +
  50785. + /** Array of IN EPs. */
  50786. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  50787. + /** Array of OUT EPs. */
  50788. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  50789. + /** number of valid EPs in the above array. */
  50790. +// unsigned num_eps : 4;
  50791. + dwc_spinlock_t *lock;
  50792. +
  50793. + /** Tasklet to defer starting of TEST mode transmissions until
  50794. + * Status Phase has been completed.
  50795. + */
  50796. + dwc_tasklet_t *test_mode_tasklet;
  50797. +
  50798. + /** Tasklet to delay starting of xfer in DMA mode */
  50799. + dwc_tasklet_t *start_xfer_tasklet;
  50800. +
  50801. + /** The test mode to enter when the tasklet is executed. */
  50802. + unsigned test_mode;
  50803. + /** The cfi_api structure that implements most of the CFI API
  50804. + * and OTG specific core configuration functionality
  50805. + */
  50806. +#ifdef DWC_UTE_CFI
  50807. + struct cfiobject *cfi;
  50808. +#endif
  50809. +
  50810. +};
  50811. +
  50812. +static inline struct device *dwc_otg_pcd_to_dev(struct dwc_otg_pcd *pcd)
  50813. +{
  50814. + return &pcd->otg_dev->os_dep.platformdev->dev;
  50815. +}
  50816. +
  50817. +//FIXME this functions should be static, and this prototypes should be removed
  50818. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  50819. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  50820. + dwc_otg_pcd_request_t * req, int32_t status);
  50821. +
  50822. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  50823. + void *req_handle);
  50824. +
  50825. +extern void do_test_mode(void *data);
  50826. +#endif
  50827. +#endif /* DWC_HOST_ONLY */
  50828. --- /dev/null
  50829. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  50830. @@ -0,0 +1,361 @@
  50831. +/* ==========================================================================
  50832. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  50833. + * $Revision: #11 $
  50834. + * $Date: 2011/10/26 $
  50835. + * $Change: 1873028 $
  50836. + *
  50837. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50838. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50839. + * otherwise expressly agreed to in writing between Synopsys and you.
  50840. + *
  50841. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50842. + * any End User Software License Agreement or Agreement for Licensed Product
  50843. + * with Synopsys or any supplement thereto. You are permitted to use and
  50844. + * redistribute this Software in source and binary forms, with or without
  50845. + * modification, provided that redistributions of source code must retain this
  50846. + * notice. You may not view, use, disclose, copy or distribute this file or
  50847. + * any information contained herein except pursuant to this license grant from
  50848. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50849. + * below, then you are not authorized to use the Software.
  50850. + *
  50851. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50852. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50853. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50854. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50855. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50856. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50857. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50858. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50859. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50860. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50861. + * DAMAGE.
  50862. + * ========================================================================== */
  50863. +#ifndef DWC_HOST_ONLY
  50864. +
  50865. +#if !defined(__DWC_PCD_IF_H__)
  50866. +#define __DWC_PCD_IF_H__
  50867. +
  50868. +//#include "dwc_os.h"
  50869. +#include "dwc_otg_core_if.h"
  50870. +#include "dwc_otg_driver.h"
  50871. +
  50872. +/** @file
  50873. + * This file defines DWC_OTG PCD Core API.
  50874. + */
  50875. +
  50876. +struct dwc_otg_pcd;
  50877. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  50878. +
  50879. +/** Maxpacket size for EP0 */
  50880. +#define MAX_EP0_SIZE 64
  50881. +/** Maxpacket size for any EP */
  50882. +#define MAX_PACKET_SIZE 1024
  50883. +
  50884. +/** @name Function Driver Callbacks */
  50885. +/** @{ */
  50886. +
  50887. +/** This function will be called whenever a previously queued request has
  50888. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  50889. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  50890. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  50891. + * parameters. */
  50892. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50893. + void *req_handle, int32_t status,
  50894. + uint32_t actual);
  50895. +/**
  50896. + * This function will be called whenever a previousle queued ISOC request has
  50897. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  50898. + * function.
  50899. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  50900. + * functions.
  50901. + */
  50902. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50903. + void *req_handle, int proc_buf_num);
  50904. +/** This function should handle any SETUP request that cannot be handled by the
  50905. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  50906. + * class-specific requests, etc. The function must non-blocking.
  50907. + *
  50908. + * Returns 0 on success.
  50909. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  50910. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  50911. + * Returns -DWC_E_SHUTDOWN on any other error. */
  50912. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  50913. +/** This is called whenever the device has been disconnected. The function
  50914. + * driver should take appropriate action to clean up all pending requests in the
  50915. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  50916. + * state. */
  50917. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  50918. +/** This function is called when device has been connected. */
  50919. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  50920. +/** This function is called when device has been suspended */
  50921. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  50922. +/** This function is called when device has received LPM tokens, i.e.
  50923. + * device has been sent to sleep state. */
  50924. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  50925. +/** This function is called when device has been resumed
  50926. + * from suspend(L2) or L1 sleep state. */
  50927. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  50928. +/** This function is called whenever hnp params has been changed.
  50929. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  50930. + * to get hnp parameters. */
  50931. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  50932. +/** This function is called whenever USB RESET is detected. */
  50933. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  50934. +
  50935. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  50936. +
  50937. +/**
  50938. + *
  50939. + * @param ep_handle Void pointer to the usb_ep structure
  50940. + * @param ereq_port Pointer to the extended request structure created in the
  50941. + * portable part.
  50942. + */
  50943. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50944. + void *req_handle, int32_t status,
  50945. + void *ereq_port);
  50946. +/** Function Driver Ops Data Structure */
  50947. +struct dwc_otg_pcd_function_ops {
  50948. + dwc_connect_cb_t connect;
  50949. + dwc_disconnect_cb_t disconnect;
  50950. + dwc_setup_cb_t setup;
  50951. + dwc_completion_cb_t complete;
  50952. + dwc_isoc_completion_cb_t isoc_complete;
  50953. + dwc_suspend_cb_t suspend;
  50954. + dwc_sleep_cb_t sleep;
  50955. + dwc_resume_cb_t resume;
  50956. + dwc_reset_cb_t reset;
  50957. + dwc_hnp_params_changed_cb_t hnp_changed;
  50958. + cfi_setup_cb_t cfi_setup;
  50959. +#ifdef DWC_UTE_PER_IO
  50960. + xiso_completion_cb_t xisoc_complete;
  50961. +#endif
  50962. +};
  50963. +/** @} */
  50964. +
  50965. +/** @name Function Driver Functions */
  50966. +/** @{ */
  50967. +
  50968. +/** Call this function to get pointer on dwc_otg_pcd_t,
  50969. + * this pointer will be used for all PCD API functions.
  50970. + *
  50971. + * @param core_if The DWC_OTG Core
  50972. + */
  50973. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev);
  50974. +
  50975. +/** Frees PCD allocated by dwc_otg_pcd_init
  50976. + *
  50977. + * @param pcd The PCD
  50978. + */
  50979. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  50980. +
  50981. +/** Call this to bind the function driver to the PCD Core.
  50982. + *
  50983. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  50984. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  50985. + */
  50986. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  50987. + const struct dwc_otg_pcd_function_ops *fops);
  50988. +
  50989. +/** Enables an endpoint for use. This function enables an endpoint in
  50990. + * the PCD. The endpoint is described by the ep_desc which has the
  50991. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  50992. + * to the endpoint from other API functions and in callbacks. Normally this
  50993. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  50994. + * core for that interface.
  50995. + *
  50996. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50997. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50998. + * Returns 0 on success.
  50999. + *
  51000. + * @param pcd The PCD
  51001. + * @param ep_desc Endpoint descriptor
  51002. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  51003. + */
  51004. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  51005. + const uint8_t * ep_desc, void *usb_ep);
  51006. +
  51007. +/** Disable the endpoint referenced by ep_handle.
  51008. + *
  51009. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  51010. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  51011. + * Returns 0 on success. */
  51012. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  51013. +
  51014. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  51015. + * After the transfer is completes, the complete callback will be called with
  51016. + * the request status.
  51017. + *
  51018. + * @param pcd The PCD
  51019. + * @param ep_handle The handle of the endpoint
  51020. + * @param buf The buffer for the data
  51021. + * @param dma_buf The DMA buffer for the data
  51022. + * @param buflen The length of the data transfer
  51023. + * @param zero Specifies whether to send zero length last packet.
  51024. + * @param req_handle Set this handle to any value to use to reference this
  51025. + * request in the ep_dequeue function or from the complete callback
  51026. + * @param atomic_alloc If driver need to perform atomic allocations
  51027. + * for internal data structures.
  51028. + *
  51029. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  51030. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  51031. + * Returns 0 on success. */
  51032. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  51033. + uint8_t * buf, dwc_dma_t dma_buf,
  51034. + uint32_t buflen, int zero, void *req_handle,
  51035. + int atomic_alloc);
  51036. +#ifdef DWC_UTE_PER_IO
  51037. +/**
  51038. + *
  51039. + * @param ereq_nonport Pointer to the extended request part of the
  51040. + * usb_request structure defined in usb_gadget.h file.
  51041. + */
  51042. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  51043. + uint8_t * buf, dwc_dma_t dma_buf,
  51044. + uint32_t buflen, int zero,
  51045. + void *req_handle, int atomic_alloc,
  51046. + void *ereq_nonport);
  51047. +
  51048. +#endif
  51049. +
  51050. +/** De-queue the specified data transfer that has not yet completed.
  51051. + *
  51052. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  51053. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  51054. + * Returns 0 on success. */
  51055. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  51056. + void *req_handle);
  51057. +
  51058. +/** Halt (STALL) an endpoint or clear it.
  51059. + *
  51060. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  51061. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  51062. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  51063. + * Returns 0 on success. */
  51064. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  51065. +
  51066. +/** This function */
  51067. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  51068. +
  51069. +/** This function should be called on every hardware interrupt */
  51070. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  51071. +
  51072. +/** This function returns current frame number */
  51073. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  51074. +
  51075. +/**
  51076. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  51077. + * For isochronous transfers duble buffering is used.
  51078. + * After processing each of buffers comlete callback will be called with
  51079. + * status for each transaction.
  51080. + *
  51081. + * @param pcd The PCD
  51082. + * @param ep_handle The handle of the endpoint
  51083. + * @param buf0 The virtual address of first data buffer
  51084. + * @param buf1 The virtual address of second data buffer
  51085. + * @param dma0 The DMA address of first data buffer
  51086. + * @param dma1 The DMA address of second data buffer
  51087. + * @param sync_frame Data pattern frame number
  51088. + * @param dp_frame Data size for pattern frame
  51089. + * @param data_per_frame Data size for regular frame
  51090. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  51091. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  51092. + * @param req_handle Handle of ISOC request
  51093. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  51094. + * internal data structures.
  51095. + *
  51096. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  51097. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  51098. + * Returns -DW_E_SHUTDOWN for any other error.
  51099. + * Returns 0 on success
  51100. + */
  51101. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  51102. + uint8_t * buf0, uint8_t * buf1,
  51103. + dwc_dma_t dma0, dwc_dma_t dma1,
  51104. + int sync_frame, int dp_frame,
  51105. + int data_per_frame, int start_frame,
  51106. + int buf_proc_intrvl, void *req_handle,
  51107. + int atomic_alloc);
  51108. +
  51109. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  51110. + *
  51111. + * @param pcd The PCD
  51112. + * @param ep_handle The handle of the endpoint
  51113. + * @param req_handle Handle of ISOC request
  51114. + *
  51115. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  51116. + * Returns 0 on success
  51117. + */
  51118. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  51119. + void *req_handle);
  51120. +
  51121. +/** Get ISOC packet status.
  51122. + *
  51123. + * @param pcd The PCD
  51124. + * @param ep_handle The handle of the endpoint
  51125. + * @param iso_req_handle Isochronoush request handle
  51126. + * @param packet Number of packet
  51127. + * @param status Out parameter for returning status
  51128. + * @param actual Out parameter for returning actual length
  51129. + * @param offset Out parameter for returning offset
  51130. + *
  51131. + */
  51132. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  51133. + void *ep_handle,
  51134. + void *iso_req_handle, int packet,
  51135. + int *status, int *actual,
  51136. + int *offset);
  51137. +
  51138. +/** Get ISOC packet count.
  51139. + *
  51140. + * @param pcd The PCD
  51141. + * @param ep_handle The handle of the endpoint
  51142. + * @param iso_req_handle
  51143. + */
  51144. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  51145. + void *ep_handle,
  51146. + void *iso_req_handle);
  51147. +
  51148. +/** This function starts the SRP Protocol if no session is in progress. If
  51149. + * a session is already in progress, but the device is suspended,
  51150. + * remote wakeup signaling is started.
  51151. + */
  51152. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  51153. +
  51154. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  51155. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  51156. +
  51157. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  51158. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  51159. +
  51160. +/** Initiate SRP */
  51161. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  51162. +
  51163. +/** Starts remote wakeup signaling. */
  51164. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  51165. +
  51166. +/** Starts micorsecond soft disconnect. */
  51167. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  51168. +/** This function returns whether device is dualspeed.*/
  51169. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  51170. +
  51171. +/** This function returns whether device is otg. */
  51172. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  51173. +
  51174. +/** These functions allow to get hnp parameters */
  51175. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  51176. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  51177. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  51178. +
  51179. +/** CFI specific Interface functions */
  51180. +/** Allocate a cfi buffer */
  51181. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  51182. + dwc_dma_t * addr, size_t buflen,
  51183. + int flags);
  51184. +
  51185. +/******************************************************************************/
  51186. +
  51187. +/** @} */
  51188. +
  51189. +#endif /* __DWC_PCD_IF_H__ */
  51190. +
  51191. +#endif /* DWC_HOST_ONLY */
  51192. --- /dev/null
  51193. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  51194. @@ -0,0 +1,5148 @@
  51195. +/* ==========================================================================
  51196. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  51197. + * $Revision: #116 $
  51198. + * $Date: 2012/08/10 $
  51199. + * $Change: 2047372 $
  51200. + *
  51201. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51202. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51203. + * otherwise expressly agreed to in writing between Synopsys and you.
  51204. + *
  51205. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51206. + * any End User Software License Agreement or Agreement for Licensed Product
  51207. + * with Synopsys or any supplement thereto. You are permitted to use and
  51208. + * redistribute this Software in source and binary forms, with or without
  51209. + * modification, provided that redistributions of source code must retain this
  51210. + * notice. You may not view, use, disclose, copy or distribute this file or
  51211. + * any information contained herein except pursuant to this license grant from
  51212. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51213. + * below, then you are not authorized to use the Software.
  51214. + *
  51215. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51216. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51217. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51218. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51219. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51220. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51221. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51222. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51223. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51224. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51225. + * DAMAGE.
  51226. + * ========================================================================== */
  51227. +#ifndef DWC_HOST_ONLY
  51228. +
  51229. +#include "dwc_otg_pcd.h"
  51230. +
  51231. +#ifdef DWC_UTE_CFI
  51232. +#include "dwc_otg_cfi.h"
  51233. +#endif
  51234. +
  51235. +#ifdef DWC_UTE_PER_IO
  51236. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  51237. +#endif
  51238. +//#define PRINT_CFI_DMA_DESCS
  51239. +
  51240. +#define DEBUG_EP0
  51241. +
  51242. +/**
  51243. + * This function updates OTG.
  51244. + */
  51245. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  51246. +{
  51247. +
  51248. + if (reset) {
  51249. + pcd->b_hnp_enable = 0;
  51250. + pcd->a_hnp_support = 0;
  51251. + pcd->a_alt_hnp_support = 0;
  51252. + }
  51253. +
  51254. + if (pcd->fops->hnp_changed) {
  51255. + pcd->fops->hnp_changed(pcd);
  51256. + }
  51257. +}
  51258. +
  51259. +/** @file
  51260. + * This file contains the implementation of the PCD Interrupt handlers.
  51261. + *
  51262. + * The PCD handles the device interrupts. Many conditions can cause a
  51263. + * device interrupt. When an interrupt occurs, the device interrupt
  51264. + * service routine determines the cause of the interrupt and
  51265. + * dispatches handling to the appropriate function. These interrupt
  51266. + * handling functions are described below.
  51267. + * All interrupt registers are processed from LSB to MSB.
  51268. + */
  51269. +
  51270. +/**
  51271. + * This function prints the ep0 state for debug purposes.
  51272. + */
  51273. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  51274. +{
  51275. +#ifdef DEBUG
  51276. + char str[40];
  51277. +
  51278. + switch (pcd->ep0state) {
  51279. + case EP0_DISCONNECT:
  51280. + dwc_strcpy(str, "EP0_DISCONNECT");
  51281. + break;
  51282. + case EP0_IDLE:
  51283. + dwc_strcpy(str, "EP0_IDLE");
  51284. + break;
  51285. + case EP0_IN_DATA_PHASE:
  51286. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  51287. + break;
  51288. + case EP0_OUT_DATA_PHASE:
  51289. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  51290. + break;
  51291. + case EP0_IN_STATUS_PHASE:
  51292. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  51293. + break;
  51294. + case EP0_OUT_STATUS_PHASE:
  51295. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  51296. + break;
  51297. + case EP0_STALL:
  51298. + dwc_strcpy(str, "EP0_STALL");
  51299. + break;
  51300. + default:
  51301. + dwc_strcpy(str, "EP0_INVALID");
  51302. + }
  51303. +
  51304. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  51305. +#endif
  51306. +}
  51307. +
  51308. +/**
  51309. + * This function calculate the size of the payload in the memory
  51310. + * for out endpoints and prints size for debug purposes(used in
  51311. + * 2.93a DevOutNak feature).
  51312. + */
  51313. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  51314. +{
  51315. +#ifdef DEBUG
  51316. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  51317. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  51318. + int pack_num;
  51319. + unsigned payload;
  51320. +
  51321. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  51322. + deptsiz_updt.d32 =
  51323. + DWC_READ_REG32(&pcd->core_if->dev_if->
  51324. + out_ep_regs[ep->num]->doeptsiz);
  51325. + /* Payload will be */
  51326. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  51327. + /* Packet count is decremented every time a packet
  51328. + * is written to the RxFIFO not in to the external memory
  51329. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  51330. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  51331. + DWC_DEBUGPL(DBG_PCDV,
  51332. + "Payload for EP%d-%s\n",
  51333. + ep->num, (ep->is_in ? "IN" : "OUT"));
  51334. + DWC_DEBUGPL(DBG_PCDV,
  51335. + "Number of transfered bytes = 0x%08x\n", payload);
  51336. + DWC_DEBUGPL(DBG_PCDV,
  51337. + "Number of transfered packets = %d\n", pack_num);
  51338. +#endif
  51339. +}
  51340. +
  51341. +
  51342. +#ifdef DWC_UTE_CFI
  51343. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  51344. + const uint8_t * epname, int descnum)
  51345. +{
  51346. + CFI_INFO
  51347. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  51348. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  51349. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  51350. + ddesc->status.b.bs);
  51351. +}
  51352. +#endif
  51353. +
  51354. +/**
  51355. + * This function returns pointer to in ep struct with number ep_num
  51356. + */
  51357. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  51358. +{
  51359. + int i;
  51360. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  51361. + if (ep_num == 0) {
  51362. + return &pcd->ep0;
  51363. + } else {
  51364. + for (i = 0; i < num_in_eps; ++i) {
  51365. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  51366. + return &pcd->in_ep[i];
  51367. + }
  51368. + return 0;
  51369. + }
  51370. +}
  51371. +
  51372. +/**
  51373. + * This function returns pointer to out ep struct with number ep_num
  51374. + */
  51375. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  51376. +{
  51377. + int i;
  51378. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  51379. + if (ep_num == 0) {
  51380. + return &pcd->ep0;
  51381. + } else {
  51382. + for (i = 0; i < num_out_eps; ++i) {
  51383. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  51384. + return &pcd->out_ep[i];
  51385. + }
  51386. + return 0;
  51387. + }
  51388. +}
  51389. +
  51390. +/**
  51391. + * This functions gets a pointer to an EP from the wIndex address
  51392. + * value of the control request.
  51393. + */
  51394. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  51395. +{
  51396. + dwc_otg_pcd_ep_t *ep;
  51397. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  51398. +
  51399. + if (ep_num == 0) {
  51400. + ep = &pcd->ep0;
  51401. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  51402. + ep = &pcd->in_ep[ep_num - 1];
  51403. + } else {
  51404. + ep = &pcd->out_ep[ep_num - 1];
  51405. + }
  51406. +
  51407. + return ep;
  51408. +}
  51409. +
  51410. +/**
  51411. + * This function checks the EP request queue, if the queue is not
  51412. + * empty the next request is started.
  51413. + */
  51414. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  51415. +{
  51416. + dwc_otg_pcd_request_t *req = 0;
  51417. + uint32_t max_transfer =
  51418. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  51419. +
  51420. +#ifdef DWC_UTE_CFI
  51421. + struct dwc_otg_pcd *pcd;
  51422. + pcd = ep->pcd;
  51423. +#endif
  51424. +
  51425. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  51426. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  51427. +
  51428. +#ifdef DWC_UTE_CFI
  51429. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  51430. + ep->dwc_ep.cfi_req_len = req->length;
  51431. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  51432. + } else {
  51433. +#endif
  51434. + /* Setup and start the Transfer */
  51435. + if (req->dw_align_buf) {
  51436. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  51437. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  51438. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  51439. + } else {
  51440. + ep->dwc_ep.dma_addr = req->dma;
  51441. + ep->dwc_ep.start_xfer_buff = req->buf;
  51442. + ep->dwc_ep.xfer_buff = req->buf;
  51443. + }
  51444. + ep->dwc_ep.sent_zlp = 0;
  51445. + ep->dwc_ep.total_len = req->length;
  51446. + ep->dwc_ep.xfer_len = 0;
  51447. + ep->dwc_ep.xfer_count = 0;
  51448. +
  51449. + ep->dwc_ep.maxxfer = max_transfer;
  51450. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  51451. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  51452. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  51453. + if (ep->dwc_ep.is_in) {
  51454. + if (ep->dwc_ep.maxxfer >
  51455. + DDMA_MAX_TRANSFER_SIZE) {
  51456. + ep->dwc_ep.maxxfer =
  51457. + DDMA_MAX_TRANSFER_SIZE;
  51458. + }
  51459. + } else {
  51460. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  51461. + ep->dwc_ep.maxxfer =
  51462. + out_max_xfer;
  51463. + }
  51464. + }
  51465. + }
  51466. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  51467. + ep->dwc_ep.maxxfer -=
  51468. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  51469. + }
  51470. + if (req->sent_zlp) {
  51471. + if ((ep->dwc_ep.total_len %
  51472. + ep->dwc_ep.maxpacket == 0)
  51473. + && (ep->dwc_ep.total_len != 0)) {
  51474. + ep->dwc_ep.sent_zlp = 1;
  51475. + }
  51476. +
  51477. + }
  51478. +#ifdef DWC_UTE_CFI
  51479. + }
  51480. +#endif
  51481. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  51482. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  51483. + DWC_PRINTF("There are no more ISOC requests \n");
  51484. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  51485. + }
  51486. +}
  51487. +
  51488. +/**
  51489. + * This function handles the SOF Interrupts. At this time the SOF
  51490. + * Interrupt is disabled.
  51491. + */
  51492. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  51493. +{
  51494. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51495. +
  51496. + gintsts_data_t gintsts;
  51497. +
  51498. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  51499. +
  51500. + /* Clear interrupt */
  51501. + gintsts.d32 = 0;
  51502. + gintsts.b.sofintr = 1;
  51503. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  51504. +
  51505. + return 1;
  51506. +}
  51507. +
  51508. +/**
  51509. + * This function handles the Rx Status Queue Level Interrupt, which
  51510. + * indicates that there is a least one packet in the Rx FIFO. The
  51511. + * packets are moved from the FIFO to memory, where they will be
  51512. + * processed when the Endpoint Interrupt Register indicates Transfer
  51513. + * Complete or SETUP Phase Done.
  51514. + *
  51515. + * Repeat the following until the Rx Status Queue is empty:
  51516. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  51517. + * info
  51518. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  51519. + * and exit
  51520. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  51521. + * SETUP data to the buffer
  51522. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  51523. + * to the destination buffer
  51524. + */
  51525. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  51526. +{
  51527. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51528. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  51529. + gintmsk_data_t gintmask = {.d32 = 0 };
  51530. + device_grxsts_data_t status;
  51531. + dwc_otg_pcd_ep_t *ep;
  51532. + gintsts_data_t gintsts;
  51533. +#ifdef DEBUG
  51534. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  51535. +#endif
  51536. +
  51537. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  51538. + /* Disable the Rx Status Queue Level interrupt */
  51539. + gintmask.b.rxstsqlvl = 1;
  51540. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  51541. +
  51542. + /* Get the Status from the top of the FIFO */
  51543. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  51544. +
  51545. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  51546. + "pktsts:%x Frame:%d(0x%0x)\n",
  51547. + status.b.epnum, status.b.bcnt,
  51548. + dpid_str[status.b.dpid],
  51549. + status.b.pktsts, status.b.fn, status.b.fn);
  51550. + /* Get pointer to EP structure */
  51551. + ep = get_out_ep(pcd, status.b.epnum);
  51552. +
  51553. + switch (status.b.pktsts) {
  51554. + case DWC_DSTS_GOUT_NAK:
  51555. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  51556. + break;
  51557. + case DWC_STS_DATA_UPDT:
  51558. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  51559. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  51560. + /** @todo NGS Check for buffer overflow? */
  51561. + dwc_otg_read_packet(core_if,
  51562. + ep->dwc_ep.xfer_buff,
  51563. + status.b.bcnt);
  51564. + ep->dwc_ep.xfer_count += status.b.bcnt;
  51565. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  51566. + }
  51567. + break;
  51568. + case DWC_STS_XFER_COMP:
  51569. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  51570. + break;
  51571. + case DWC_DSTS_SETUP_COMP:
  51572. +#ifdef DEBUG_EP0
  51573. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  51574. +#endif
  51575. + break;
  51576. + case DWC_DSTS_SETUP_UPDT:
  51577. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  51578. +#ifdef DEBUG_EP0
  51579. + DWC_DEBUGPL(DBG_PCD,
  51580. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  51581. + pcd->setup_pkt->req.bmRequestType,
  51582. + pcd->setup_pkt->req.bRequest,
  51583. + UGETW(pcd->setup_pkt->req.wValue),
  51584. + UGETW(pcd->setup_pkt->req.wIndex),
  51585. + UGETW(pcd->setup_pkt->req.wLength));
  51586. +#endif
  51587. + ep->dwc_ep.xfer_count += status.b.bcnt;
  51588. + break;
  51589. + default:
  51590. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  51591. + status.b.pktsts);
  51592. + break;
  51593. + }
  51594. +
  51595. + /* Enable the Rx Status Queue Level interrupt */
  51596. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  51597. + /* Clear interrupt */
  51598. + gintsts.d32 = 0;
  51599. + gintsts.b.rxstsqlvl = 1;
  51600. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  51601. +
  51602. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  51603. + return 1;
  51604. +}
  51605. +
  51606. +/**
  51607. + * This function examines the Device IN Token Learning Queue to
  51608. + * determine the EP number of the last IN token received. This
  51609. + * implementation is for the Mass Storage device where there are only
  51610. + * 2 IN EPs (Control-IN and BULK-IN).
  51611. + *
  51612. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  51613. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  51614. + *
  51615. + * @param core_if Programming view of DWC_otg controller.
  51616. + *
  51617. + */
  51618. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  51619. +{
  51620. + dwc_otg_device_global_regs_t *dev_global_regs =
  51621. + core_if->dev_if->dev_global_regs;
  51622. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  51623. + /* Number of Token Queue Registers */
  51624. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  51625. + dtknq1_data_t dtknqr1;
  51626. + uint32_t in_tkn_epnums[4];
  51627. + int ndx = 0;
  51628. + int i = 0;
  51629. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  51630. + int epnum = 0;
  51631. +
  51632. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  51633. +
  51634. + /* Read the DTKNQ Registers */
  51635. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  51636. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  51637. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  51638. + in_tkn_epnums[i]);
  51639. + if (addr == &dev_global_regs->dvbusdis) {
  51640. + addr = &dev_global_regs->dtknqr3_dthrctl;
  51641. + } else {
  51642. + ++addr;
  51643. + }
  51644. +
  51645. + }
  51646. +
  51647. + /* Copy the DTKNQR1 data to the bit field. */
  51648. + dtknqr1.d32 = in_tkn_epnums[0];
  51649. + /* Get the EP numbers */
  51650. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  51651. + ndx = dtknqr1.b.intknwptr - 1;
  51652. +
  51653. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  51654. + if (ndx == -1) {
  51655. + /** @todo Find a simpler way to calculate the max
  51656. + * queue position.*/
  51657. + int cnt = TOKEN_Q_DEPTH;
  51658. + if (TOKEN_Q_DEPTH <= 6) {
  51659. + cnt = TOKEN_Q_DEPTH - 1;
  51660. + } else if (TOKEN_Q_DEPTH <= 14) {
  51661. + cnt = TOKEN_Q_DEPTH - 7;
  51662. + } else if (TOKEN_Q_DEPTH <= 22) {
  51663. + cnt = TOKEN_Q_DEPTH - 15;
  51664. + } else {
  51665. + cnt = TOKEN_Q_DEPTH - 23;
  51666. + }
  51667. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  51668. + } else {
  51669. + if (ndx <= 5) {
  51670. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  51671. + } else if (ndx <= 13) {
  51672. + ndx -= 6;
  51673. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  51674. + } else if (ndx <= 21) {
  51675. + ndx -= 14;
  51676. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  51677. + } else if (ndx <= 29) {
  51678. + ndx -= 22;
  51679. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  51680. + }
  51681. + }
  51682. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  51683. + return epnum;
  51684. +}
  51685. +
  51686. +/**
  51687. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  51688. + * The active request is checked for the next packet to be loaded into
  51689. + * the non-periodic Tx FIFO.
  51690. + */
  51691. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  51692. +{
  51693. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51694. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  51695. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  51696. + gnptxsts_data_t txstatus = {.d32 = 0 };
  51697. + gintsts_data_t gintsts;
  51698. +
  51699. + int epnum = 0;
  51700. + dwc_otg_pcd_ep_t *ep = 0;
  51701. + uint32_t len = 0;
  51702. + int dwords;
  51703. +
  51704. + /* Get the epnum from the IN Token Learning Queue. */
  51705. + epnum = get_ep_of_last_in_token(core_if);
  51706. + ep = get_in_ep(pcd, epnum);
  51707. +
  51708. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  51709. +
  51710. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  51711. +
  51712. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51713. + if (len > ep->dwc_ep.maxpacket) {
  51714. + len = ep->dwc_ep.maxpacket;
  51715. + }
  51716. + dwords = (len + 3) / 4;
  51717. +
  51718. + /* While there is space in the queue and space in the FIFO and
  51719. + * More data to tranfer, Write packets to the Tx FIFO */
  51720. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  51721. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  51722. +
  51723. + while (txstatus.b.nptxqspcavail > 0 &&
  51724. + txstatus.b.nptxfspcavail > dwords &&
  51725. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  51726. + /* Write the FIFO */
  51727. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  51728. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51729. +
  51730. + if (len > ep->dwc_ep.maxpacket) {
  51731. + len = ep->dwc_ep.maxpacket;
  51732. + }
  51733. +
  51734. + dwords = (len + 3) / 4;
  51735. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  51736. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  51737. + }
  51738. +
  51739. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  51740. + DWC_READ_REG32(&global_regs->gnptxsts));
  51741. +
  51742. + /* Clear interrupt */
  51743. + gintsts.d32 = 0;
  51744. + gintsts.b.nptxfempty = 1;
  51745. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  51746. +
  51747. + return 1;
  51748. +}
  51749. +
  51750. +/**
  51751. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  51752. + * The active request is checked for the next packet to be loaded into
  51753. + * apropriate Tx FIFO.
  51754. + */
  51755. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  51756. +{
  51757. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51758. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51759. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  51760. + dtxfsts_data_t txstatus = {.d32 = 0 };
  51761. + dwc_otg_pcd_ep_t *ep = 0;
  51762. + uint32_t len = 0;
  51763. + int dwords;
  51764. +
  51765. + ep = get_in_ep(pcd, epnum);
  51766. +
  51767. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  51768. +
  51769. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  51770. +
  51771. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51772. +
  51773. + if (len > ep->dwc_ep.maxpacket) {
  51774. + len = ep->dwc_ep.maxpacket;
  51775. + }
  51776. +
  51777. + dwords = (len + 3) / 4;
  51778. +
  51779. + /* While there is space in the queue and space in the FIFO and
  51780. + * More data to tranfer, Write packets to the Tx FIFO */
  51781. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  51782. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  51783. +
  51784. + while (txstatus.b.txfspcavail > dwords &&
  51785. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  51786. + ep->dwc_ep.xfer_len != 0) {
  51787. + /* Write the FIFO */
  51788. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  51789. +
  51790. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51791. + if (len > ep->dwc_ep.maxpacket) {
  51792. + len = ep->dwc_ep.maxpacket;
  51793. + }
  51794. +
  51795. + dwords = (len + 3) / 4;
  51796. + txstatus.d32 =
  51797. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  51798. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  51799. + txstatus.d32);
  51800. + }
  51801. +
  51802. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  51803. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  51804. +
  51805. + return 1;
  51806. +}
  51807. +
  51808. +/**
  51809. + * This function is called when the Device is disconnected. It stops
  51810. + * any active requests and informs the Gadget driver of the
  51811. + * disconnect.
  51812. + */
  51813. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  51814. +{
  51815. + int i, num_in_eps, num_out_eps;
  51816. + dwc_otg_pcd_ep_t *ep;
  51817. +
  51818. + gintmsk_data_t intr_mask = {.d32 = 0 };
  51819. +
  51820. + DWC_SPINLOCK(pcd->lock);
  51821. +
  51822. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  51823. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  51824. +
  51825. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  51826. + /* don't disconnect drivers more than once */
  51827. + if (pcd->ep0state == EP0_DISCONNECT) {
  51828. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  51829. + DWC_SPINUNLOCK(pcd->lock);
  51830. + return;
  51831. + }
  51832. + pcd->ep0state = EP0_DISCONNECT;
  51833. +
  51834. + /* Reset the OTG state. */
  51835. + dwc_otg_pcd_update_otg(pcd, 1);
  51836. +
  51837. + /* Disable the NP Tx Fifo Empty Interrupt. */
  51838. + intr_mask.b.nptxfempty = 1;
  51839. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  51840. + intr_mask.d32, 0);
  51841. +
  51842. + /* Flush the FIFOs */
  51843. + /**@todo NGS Flush Periodic FIFOs */
  51844. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  51845. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  51846. +
  51847. + /* prevent new request submissions, kill any outstanding requests */
  51848. + ep = &pcd->ep0;
  51849. + dwc_otg_request_nuke(ep);
  51850. + /* prevent new request submissions, kill any outstanding requests */
  51851. + for (i = 0; i < num_in_eps; i++) {
  51852. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  51853. + dwc_otg_request_nuke(ep);
  51854. + }
  51855. + /* prevent new request submissions, kill any outstanding requests */
  51856. + for (i = 0; i < num_out_eps; i++) {
  51857. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  51858. + dwc_otg_request_nuke(ep);
  51859. + }
  51860. +
  51861. + /* report disconnect; the driver is already quiesced */
  51862. + if (pcd->fops->disconnect) {
  51863. + DWC_SPINUNLOCK(pcd->lock);
  51864. + pcd->fops->disconnect(pcd);
  51865. + DWC_SPINLOCK(pcd->lock);
  51866. + }
  51867. + DWC_SPINUNLOCK(pcd->lock);
  51868. +}
  51869. +
  51870. +/**
  51871. + * This interrupt indicates that ...
  51872. + */
  51873. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  51874. +{
  51875. + gintmsk_data_t intr_mask = {.d32 = 0 };
  51876. + gintsts_data_t gintsts;
  51877. +
  51878. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  51879. + intr_mask.b.i2cintr = 1;
  51880. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  51881. + intr_mask.d32, 0);
  51882. +
  51883. + /* Clear interrupt */
  51884. + gintsts.d32 = 0;
  51885. + gintsts.b.i2cintr = 1;
  51886. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  51887. + gintsts.d32);
  51888. + return 1;
  51889. +}
  51890. +
  51891. +/**
  51892. + * This interrupt indicates that ...
  51893. + */
  51894. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  51895. +{
  51896. + gintsts_data_t gintsts;
  51897. +#if defined(VERBOSE)
  51898. + DWC_PRINTF("Early Suspend Detected\n");
  51899. +#endif
  51900. +
  51901. + /* Clear interrupt */
  51902. + gintsts.d32 = 0;
  51903. + gintsts.b.erlysuspend = 1;
  51904. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  51905. + gintsts.d32);
  51906. + return 1;
  51907. +}
  51908. +
  51909. +/**
  51910. + * This function configures EPO to receive SETUP packets.
  51911. + *
  51912. + * @todo NGS: Update the comments from the HW FS.
  51913. + *
  51914. + * -# Program the following fields in the endpoint specific registers
  51915. + * for Control OUT EP 0, in order to receive a setup packet
  51916. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  51917. + * setup packets)
  51918. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  51919. + * to back setup packets)
  51920. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  51921. + * store any setup packets received
  51922. + *
  51923. + * @param core_if Programming view of DWC_otg controller.
  51924. + * @param pcd Programming view of the PCD.
  51925. + */
  51926. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  51927. + dwc_otg_pcd_t * pcd)
  51928. +{
  51929. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51930. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  51931. + dwc_otg_dev_dma_desc_t *dma_desc;
  51932. + depctl_data_t doepctl = {.d32 = 0 };
  51933. +
  51934. +#ifdef VERBOSE
  51935. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  51936. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  51937. +#endif
  51938. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  51939. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  51940. + if (doepctl.b.epena) {
  51941. + return;
  51942. + }
  51943. + }
  51944. +
  51945. + doeptsize0.b.supcnt = 3;
  51946. + doeptsize0.b.pktcnt = 1;
  51947. + doeptsize0.b.xfersize = 8 * 3;
  51948. +
  51949. + if (core_if->dma_enable) {
  51950. + if (!core_if->dma_desc_enable) {
  51951. + /** put here as for Hermes mode deptisz register should not be written */
  51952. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  51953. + doeptsize0.d32);
  51954. +
  51955. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  51956. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  51957. + pcd->setup_pkt_dma_handle);
  51958. + } else {
  51959. + dev_if->setup_desc_index =
  51960. + (dev_if->setup_desc_index + 1) & 1;
  51961. + dma_desc =
  51962. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  51963. +
  51964. + /** DMA Descriptor Setup */
  51965. + dma_desc->status.b.bs = BS_HOST_BUSY;
  51966. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  51967. + dma_desc->status.b.sr = 0;
  51968. + dma_desc->status.b.mtrf = 0;
  51969. + }
  51970. + dma_desc->status.b.l = 1;
  51971. + dma_desc->status.b.ioc = 1;
  51972. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  51973. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  51974. + dma_desc->status.b.sts = 0;
  51975. + dma_desc->status.b.bs = BS_HOST_READY;
  51976. +
  51977. + /** DOEPDMA0 Register write */
  51978. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  51979. + dev_if->dma_setup_desc_addr
  51980. + [dev_if->setup_desc_index]);
  51981. + }
  51982. +
  51983. + } else {
  51984. + /** put here as for Hermes mode deptisz register should not be written */
  51985. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  51986. + doeptsize0.d32);
  51987. + }
  51988. +
  51989. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  51990. + doepctl.d32 = 0;
  51991. + doepctl.b.epena = 1;
  51992. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  51993. + doepctl.b.cnak = 1;
  51994. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  51995. + } else {
  51996. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  51997. + }
  51998. +
  51999. +#ifdef VERBOSE
  52000. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  52001. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  52002. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  52003. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  52004. +#endif
  52005. +}
  52006. +
  52007. +/**
  52008. + * This interrupt occurs when a USB Reset is detected. When the USB
  52009. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  52010. + * EP0 state is set to IDLE.
  52011. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  52012. + * -# Unmask the following interrupt bits
  52013. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  52014. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  52015. + * - DOEPMSK.SETUP = 1
  52016. + * - DOEPMSK.XferCompl = 1
  52017. + * - DIEPMSK.XferCompl = 1
  52018. + * - DIEPMSK.TimeOut = 1
  52019. + * -# Program the following fields in the endpoint specific registers
  52020. + * for Control OUT EP 0, in order to receive a setup packet
  52021. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  52022. + * setup packets)
  52023. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  52024. + * to back setup packets)
  52025. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  52026. + * store any setup packets received
  52027. + * At this point, all the required initialization, except for enabling
  52028. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  52029. + */
  52030. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  52031. +{
  52032. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52033. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  52034. + depctl_data_t doepctl = {.d32 = 0 };
  52035. + depctl_data_t diepctl = {.d32 = 0 };
  52036. + daint_data_t daintmsk = {.d32 = 0 };
  52037. + doepmsk_data_t doepmsk = {.d32 = 0 };
  52038. + diepmsk_data_t diepmsk = {.d32 = 0 };
  52039. + dcfg_data_t dcfg = {.d32 = 0 };
  52040. + grstctl_t resetctl = {.d32 = 0 };
  52041. + dctl_data_t dctl = {.d32 = 0 };
  52042. + int i = 0;
  52043. + gintsts_data_t gintsts;
  52044. + pcgcctl_data_t power = {.d32 = 0 };
  52045. +
  52046. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  52047. + if (power.b.stoppclk) {
  52048. + power.d32 = 0;
  52049. + power.b.stoppclk = 1;
  52050. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  52051. +
  52052. + power.b.pwrclmp = 1;
  52053. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  52054. +
  52055. + power.b.rstpdwnmodule = 1;
  52056. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  52057. + }
  52058. +
  52059. + core_if->lx_state = DWC_OTG_L0;
  52060. +
  52061. + DWC_PRINTF("USB RESET\n");
  52062. +#ifdef DWC_EN_ISOC
  52063. + for (i = 1; i < 16; ++i) {
  52064. + dwc_otg_pcd_ep_t *ep;
  52065. + dwc_ep_t *dwc_ep;
  52066. + ep = get_in_ep(pcd, i);
  52067. + if (ep != 0) {
  52068. + dwc_ep = &ep->dwc_ep;
  52069. + dwc_ep->next_frame = 0xffffffff;
  52070. + }
  52071. + }
  52072. +#endif /* DWC_EN_ISOC */
  52073. +
  52074. + /* reset the HNP settings */
  52075. + dwc_otg_pcd_update_otg(pcd, 1);
  52076. +
  52077. + /* Clear the Remote Wakeup Signalling */
  52078. + dctl.b.rmtwkupsig = 1;
  52079. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  52080. +
  52081. + /* Set NAK for all OUT EPs */
  52082. + doepctl.b.snak = 1;
  52083. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  52084. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  52085. + }
  52086. +
  52087. + /* Flush the NP Tx FIFO */
  52088. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  52089. + /* Flush the Learning Queue */
  52090. + resetctl.b.intknqflsh = 1;
  52091. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  52092. +
  52093. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  52094. + core_if->start_predict = 0;
  52095. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  52096. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  52097. + }
  52098. + core_if->nextep_seq[0] = 0;
  52099. + core_if->first_in_nextep_seq = 0;
  52100. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  52101. + diepctl.b.nextep = 0;
  52102. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  52103. +
  52104. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  52105. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  52106. + dcfg.b.epmscnt = 2;
  52107. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  52108. +
  52109. + DWC_DEBUGPL(DBG_PCDV,
  52110. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  52111. + __func__, core_if->first_in_nextep_seq);
  52112. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  52113. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  52114. + }
  52115. + }
  52116. +
  52117. + if (core_if->multiproc_int_enable) {
  52118. + daintmsk.b.inep0 = 1;
  52119. + daintmsk.b.outep0 = 1;
  52120. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  52121. + daintmsk.d32);
  52122. +
  52123. + doepmsk.b.setup = 1;
  52124. + doepmsk.b.xfercompl = 1;
  52125. + doepmsk.b.ahberr = 1;
  52126. + doepmsk.b.epdisabled = 1;
  52127. +
  52128. + if ((core_if->dma_desc_enable) ||
  52129. + (core_if->dma_enable
  52130. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  52131. + doepmsk.b.stsphsercvd = 1;
  52132. + }
  52133. + if (core_if->dma_desc_enable)
  52134. + doepmsk.b.bna = 1;
  52135. +/*
  52136. + doepmsk.b.babble = 1;
  52137. + doepmsk.b.nyet = 1;
  52138. +
  52139. + if (core_if->dma_enable) {
  52140. + doepmsk.b.nak = 1;
  52141. + }
  52142. +*/
  52143. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  52144. + doepmsk.d32);
  52145. +
  52146. + diepmsk.b.xfercompl = 1;
  52147. + diepmsk.b.timeout = 1;
  52148. + diepmsk.b.epdisabled = 1;
  52149. + diepmsk.b.ahberr = 1;
  52150. + diepmsk.b.intknepmis = 1;
  52151. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  52152. + diepmsk.b.intknepmis = 0;
  52153. +
  52154. +/* if (core_if->dma_desc_enable) {
  52155. + diepmsk.b.bna = 1;
  52156. + }
  52157. +*/
  52158. +/*
  52159. + if (core_if->dma_enable) {
  52160. + diepmsk.b.nak = 1;
  52161. + }
  52162. +*/
  52163. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  52164. + diepmsk.d32);
  52165. + } else {
  52166. + daintmsk.b.inep0 = 1;
  52167. + daintmsk.b.outep0 = 1;
  52168. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  52169. + daintmsk.d32);
  52170. +
  52171. + doepmsk.b.setup = 1;
  52172. + doepmsk.b.xfercompl = 1;
  52173. + doepmsk.b.ahberr = 1;
  52174. + doepmsk.b.epdisabled = 1;
  52175. +
  52176. + if ((core_if->dma_desc_enable) ||
  52177. + (core_if->dma_enable
  52178. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  52179. + doepmsk.b.stsphsercvd = 1;
  52180. + }
  52181. + if (core_if->dma_desc_enable)
  52182. + doepmsk.b.bna = 1;
  52183. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  52184. +
  52185. + diepmsk.b.xfercompl = 1;
  52186. + diepmsk.b.timeout = 1;
  52187. + diepmsk.b.epdisabled = 1;
  52188. + diepmsk.b.ahberr = 1;
  52189. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  52190. + diepmsk.b.intknepmis = 0;
  52191. +/*
  52192. + if (core_if->dma_desc_enable) {
  52193. + diepmsk.b.bna = 1;
  52194. + }
  52195. +*/
  52196. +
  52197. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  52198. + }
  52199. +
  52200. + /* Reset Device Address */
  52201. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  52202. + dcfg.b.devaddr = 0;
  52203. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  52204. +
  52205. + /* setup EP0 to receive SETUP packets */
  52206. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  52207. + ep0_out_start(core_if, pcd);
  52208. +
  52209. + /* Clear interrupt */
  52210. + gintsts.d32 = 0;
  52211. + gintsts.b.usbreset = 1;
  52212. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52213. +
  52214. + return 1;
  52215. +}
  52216. +
  52217. +/**
  52218. + * Get the device speed from the device status register and convert it
  52219. + * to USB speed constant.
  52220. + *
  52221. + * @param core_if Programming view of DWC_otg controller.
  52222. + */
  52223. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  52224. +{
  52225. + dsts_data_t dsts;
  52226. + int speed = 0;
  52227. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  52228. +
  52229. + switch (dsts.b.enumspd) {
  52230. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  52231. + speed = USB_SPEED_HIGH;
  52232. + break;
  52233. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  52234. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  52235. + speed = USB_SPEED_FULL;
  52236. + break;
  52237. +
  52238. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  52239. + speed = USB_SPEED_LOW;
  52240. + break;
  52241. + }
  52242. +
  52243. + return speed;
  52244. +}
  52245. +
  52246. +/**
  52247. + * Read the device status register and set the device speed in the
  52248. + * data structure.
  52249. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  52250. + */
  52251. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  52252. +{
  52253. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52254. + gintsts_data_t gintsts;
  52255. + gusbcfg_data_t gusbcfg;
  52256. + dwc_otg_core_global_regs_t *global_regs =
  52257. + GET_CORE_IF(pcd)->core_global_regs;
  52258. + uint8_t utmi16b, utmi8b;
  52259. + int speed;
  52260. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  52261. +
  52262. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  52263. + utmi16b = 6; //vahrama old value was 6;
  52264. + utmi8b = 9;
  52265. + } else {
  52266. + utmi16b = 4;
  52267. + utmi8b = 8;
  52268. + }
  52269. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52270. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  52271. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52272. + }
  52273. +
  52274. +#ifdef DEBUG_EP0
  52275. + print_ep0_state(pcd);
  52276. +#endif
  52277. +
  52278. + if (pcd->ep0state == EP0_DISCONNECT) {
  52279. + pcd->ep0state = EP0_IDLE;
  52280. + } else if (pcd->ep0state == EP0_STALL) {
  52281. + pcd->ep0state = EP0_IDLE;
  52282. + }
  52283. +
  52284. + pcd->ep0state = EP0_IDLE;
  52285. +
  52286. + ep0->stopped = 0;
  52287. +
  52288. + speed = get_device_speed(GET_CORE_IF(pcd));
  52289. + pcd->fops->connect(pcd, speed);
  52290. +
  52291. + /* Set USB turnaround time based on device speed and PHY interface. */
  52292. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  52293. + if (speed == USB_SPEED_HIGH) {
  52294. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52295. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  52296. + /* ULPI interface */
  52297. + gusbcfg.b.usbtrdtim = 9;
  52298. + }
  52299. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52300. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  52301. + /* UTMI+ interface */
  52302. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  52303. + gusbcfg.b.usbtrdtim = utmi8b;
  52304. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  52305. + b.utmi_phy_data_width == 1) {
  52306. + gusbcfg.b.usbtrdtim = utmi16b;
  52307. + } else if (GET_CORE_IF(pcd)->
  52308. + core_params->phy_utmi_width == 8) {
  52309. + gusbcfg.b.usbtrdtim = utmi8b;
  52310. + } else {
  52311. + gusbcfg.b.usbtrdtim = utmi16b;
  52312. + }
  52313. + }
  52314. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52315. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  52316. + /* UTMI+ OR ULPI interface */
  52317. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  52318. + /* ULPI interface */
  52319. + gusbcfg.b.usbtrdtim = 9;
  52320. + } else {
  52321. + /* UTMI+ interface */
  52322. + if (GET_CORE_IF(pcd)->
  52323. + core_params->phy_utmi_width == 16) {
  52324. + gusbcfg.b.usbtrdtim = utmi16b;
  52325. + } else {
  52326. + gusbcfg.b.usbtrdtim = utmi8b;
  52327. + }
  52328. + }
  52329. + }
  52330. + } else {
  52331. + /* Full or low speed */
  52332. + gusbcfg.b.usbtrdtim = 9;
  52333. + }
  52334. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  52335. +
  52336. + /* Clear interrupt */
  52337. + gintsts.d32 = 0;
  52338. + gintsts.b.enumdone = 1;
  52339. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52340. + gintsts.d32);
  52341. + return 1;
  52342. +}
  52343. +
  52344. +/**
  52345. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  52346. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  52347. + * read all the data from the Rx FIFO.
  52348. + */
  52349. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  52350. +{
  52351. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52352. + gintsts_data_t gintsts;
  52353. +
  52354. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  52355. + "ISOC Out Dropped");
  52356. +
  52357. + intr_mask.b.isooutdrop = 1;
  52358. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  52359. + intr_mask.d32, 0);
  52360. +
  52361. + /* Clear interrupt */
  52362. + gintsts.d32 = 0;
  52363. + gintsts.b.isooutdrop = 1;
  52364. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52365. + gintsts.d32);
  52366. +
  52367. + return 1;
  52368. +}
  52369. +
  52370. +/**
  52371. + * This interrupt indicates the end of the portion of the micro-frame
  52372. + * for periodic transactions. If there is a periodic transaction for
  52373. + * the next frame, load the packets into the EP periodic Tx FIFO.
  52374. + */
  52375. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  52376. +{
  52377. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52378. + gintsts_data_t gintsts;
  52379. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  52380. +
  52381. + intr_mask.b.eopframe = 1;
  52382. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  52383. + intr_mask.d32, 0);
  52384. +
  52385. + /* Clear interrupt */
  52386. + gintsts.d32 = 0;
  52387. + gintsts.b.eopframe = 1;
  52388. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52389. + gintsts.d32);
  52390. +
  52391. + return 1;
  52392. +}
  52393. +
  52394. +/**
  52395. + * This interrupt indicates that EP of the packet on the top of the
  52396. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  52397. + *
  52398. + * The "Device IN Token Queue" Registers are read to determine the
  52399. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  52400. + * is flushed, so it can be reloaded in the order seen in the IN Token
  52401. + * Queue.
  52402. + */
  52403. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  52404. +{
  52405. + gintsts_data_t gintsts;
  52406. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52407. + dctl_data_t dctl;
  52408. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52409. +
  52410. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  52411. + core_if->start_predict = 1;
  52412. +
  52413. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  52414. +
  52415. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  52416. + if (!gintsts.b.ginnakeff) {
  52417. + /* Disable EP Mismatch interrupt */
  52418. + intr_mask.d32 = 0;
  52419. + intr_mask.b.epmismatch = 1;
  52420. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  52421. + /* Enable the Global IN NAK Effective Interrupt */
  52422. + intr_mask.d32 = 0;
  52423. + intr_mask.b.ginnakeff = 1;
  52424. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  52425. + /* Set the global non-periodic IN NAK handshake */
  52426. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  52427. + dctl.b.sgnpinnak = 1;
  52428. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  52429. + } else {
  52430. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  52431. + }
  52432. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  52433. + * handler after Global IN NAK Effective interrupt will be asserted */
  52434. + }
  52435. + /* Clear interrupt */
  52436. + gintsts.d32 = 0;
  52437. + gintsts.b.epmismatch = 1;
  52438. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52439. +
  52440. + return 1;
  52441. +}
  52442. +
  52443. +/**
  52444. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  52445. + * core has stopped fetching data for IN endpoints due to the unavailability of
  52446. + * TxFIFO space or Request Queue space. This interrupt is used by the
  52447. + * application for an endpoint mismatch algorithm.
  52448. + *
  52449. + * @param pcd The PCD
  52450. + */
  52451. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  52452. +{
  52453. + gintsts_data_t gintsts;
  52454. + gintmsk_data_t gintmsk_data;
  52455. + dctl_data_t dctl;
  52456. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52457. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  52458. +
  52459. + /* Clear the global non-periodic IN NAK handshake */
  52460. + dctl.d32 = 0;
  52461. + dctl.b.cgnpinnak = 1;
  52462. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  52463. +
  52464. + /* Mask GINTSTS.FETSUSP interrupt */
  52465. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  52466. + gintmsk_data.b.fetsusp = 0;
  52467. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  52468. +
  52469. + /* Clear interrupt */
  52470. + gintsts.d32 = 0;
  52471. + gintsts.b.fetsusp = 1;
  52472. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52473. +
  52474. + return 1;
  52475. +}
  52476. +/**
  52477. + * This funcion stalls EP0.
  52478. + */
  52479. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  52480. +{
  52481. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52482. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  52483. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  52484. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  52485. +
  52486. + ep0->dwc_ep.is_in = 1;
  52487. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52488. + pcd->ep0.stopped = 1;
  52489. + pcd->ep0state = EP0_IDLE;
  52490. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52491. +}
  52492. +
  52493. +/**
  52494. + * This functions delegates the setup command to the gadget driver.
  52495. + */
  52496. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  52497. + usb_device_request_t * ctrl)
  52498. +{
  52499. + int ret = 0;
  52500. + DWC_SPINUNLOCK(pcd->lock);
  52501. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  52502. + DWC_SPINLOCK(pcd->lock);
  52503. + if (ret < 0) {
  52504. + ep0_do_stall(pcd, ret);
  52505. + }
  52506. +
  52507. + /** @todo This is a g_file_storage gadget driver specific
  52508. + * workaround: a DELAYED_STATUS result from the fsg_setup
  52509. + * routine will result in the gadget queueing a EP0 IN status
  52510. + * phase for a two-stage control transfer. Exactly the same as
  52511. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  52512. + * specific request. Need a generic way to know when the gadget
  52513. + * driver will queue the status phase. Can we assume when we
  52514. + * call the gadget driver setup() function that it will always
  52515. + * queue and require the following flag? Need to look into
  52516. + * this.
  52517. + */
  52518. +
  52519. + if (ret == 256 + 999) {
  52520. + pcd->request_config = 1;
  52521. + }
  52522. +}
  52523. +
  52524. +#ifdef DWC_UTE_CFI
  52525. +/**
  52526. + * This functions delegates the CFI setup commands to the gadget driver.
  52527. + * This function will return a negative value to indicate a failure.
  52528. + */
  52529. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  52530. + struct cfi_usb_ctrlrequest *ctrl_req)
  52531. +{
  52532. + int ret = 0;
  52533. +
  52534. + if (pcd->fops && pcd->fops->cfi_setup) {
  52535. + DWC_SPINUNLOCK(pcd->lock);
  52536. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  52537. + DWC_SPINLOCK(pcd->lock);
  52538. + if (ret < 0) {
  52539. + ep0_do_stall(pcd, ret);
  52540. + return ret;
  52541. + }
  52542. + }
  52543. +
  52544. + return ret;
  52545. +}
  52546. +#endif
  52547. +
  52548. +/**
  52549. + * This function starts the Zero-Length Packet for the IN status phase
  52550. + * of a 2 stage control transfer.
  52551. + */
  52552. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  52553. +{
  52554. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52555. + if (pcd->ep0state == EP0_STALL) {
  52556. + return;
  52557. + }
  52558. +
  52559. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  52560. +
  52561. + /* Prepare for more SETUP Packets */
  52562. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  52563. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  52564. + && (pcd->core_if->dma_desc_enable)
  52565. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  52566. + DWC_DEBUGPL(DBG_PCDV,
  52567. + "Data terminated wait next packet in out_desc_addr\n");
  52568. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  52569. + pcd->data_terminated = 1;
  52570. + }
  52571. + ep0->dwc_ep.xfer_len = 0;
  52572. + ep0->dwc_ep.xfer_count = 0;
  52573. + ep0->dwc_ep.is_in = 1;
  52574. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  52575. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52576. +
  52577. + /* Prepare for more SETUP Packets */
  52578. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  52579. +}
  52580. +
  52581. +/**
  52582. + * This function starts the Zero-Length Packet for the OUT status phase
  52583. + * of a 2 stage control transfer.
  52584. + */
  52585. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  52586. +{
  52587. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52588. + if (pcd->ep0state == EP0_STALL) {
  52589. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  52590. + return;
  52591. + }
  52592. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  52593. +
  52594. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  52595. + ep0->dwc_ep.xfer_len = 0;
  52596. + ep0->dwc_ep.xfer_count = 0;
  52597. + ep0->dwc_ep.is_in = 0;
  52598. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  52599. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52600. +
  52601. + /* Prepare for more SETUP Packets */
  52602. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  52603. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52604. + }
  52605. +}
  52606. +
  52607. +/**
  52608. + * Clear the EP halt (STALL) and if pending requests start the
  52609. + * transfer.
  52610. + */
  52611. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  52612. +{
  52613. + if (ep->dwc_ep.stall_clear_flag == 0)
  52614. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  52615. +
  52616. + /* Reactive the EP */
  52617. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  52618. + if (ep->stopped) {
  52619. + ep->stopped = 0;
  52620. + /* If there is a request in the EP queue start it */
  52621. +
  52622. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  52623. + * epmismatch not yet implemented. */
  52624. +
  52625. + /*
  52626. + * Above fixme is solved by implmenting a tasklet to call the
  52627. + * start_next_request(), outside of interrupt context at some
  52628. + * time after the current time, after a clear-halt setup packet.
  52629. + * Still need to implement ep mismatch in the future if a gadget
  52630. + * ever uses more than one endpoint at once
  52631. + */
  52632. + ep->queue_sof = 1;
  52633. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  52634. + }
  52635. + /* Start Control Status Phase */
  52636. + do_setup_in_status_phase(pcd);
  52637. +}
  52638. +
  52639. +/**
  52640. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  52641. + * is sent from the host. The Device Control register is written with
  52642. + * the Test Mode bits set to the specified Test Mode. This is done as
  52643. + * a tasklet so that the "Status" phase of the control transfer
  52644. + * completes before transmitting the TEST packets.
  52645. + *
  52646. + * @todo This has not been tested since the tasklet struct was put
  52647. + * into the PCD struct!
  52648. + *
  52649. + */
  52650. +void do_test_mode(void *data)
  52651. +{
  52652. + dctl_data_t dctl;
  52653. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  52654. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52655. + int test_mode = pcd->test_mode;
  52656. +
  52657. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  52658. +
  52659. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  52660. + switch (test_mode) {
  52661. + case 1: // TEST_J
  52662. + dctl.b.tstctl = 1;
  52663. + break;
  52664. +
  52665. + case 2: // TEST_K
  52666. + dctl.b.tstctl = 2;
  52667. + break;
  52668. +
  52669. + case 3: // TEST_SE0_NAK
  52670. + dctl.b.tstctl = 3;
  52671. + break;
  52672. +
  52673. + case 4: // TEST_PACKET
  52674. + dctl.b.tstctl = 4;
  52675. + break;
  52676. +
  52677. + case 5: // TEST_FORCE_ENABLE
  52678. + dctl.b.tstctl = 5;
  52679. + break;
  52680. + }
  52681. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  52682. +}
  52683. +
  52684. +/**
  52685. + * This function process the GET_STATUS Setup Commands.
  52686. + */
  52687. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  52688. +{
  52689. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52690. + dwc_otg_pcd_ep_t *ep;
  52691. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52692. + uint16_t *status = pcd->status_buf;
  52693. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52694. +
  52695. +#ifdef DEBUG_EP0
  52696. + DWC_DEBUGPL(DBG_PCD,
  52697. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  52698. + ctrl.bmRequestType, ctrl.bRequest,
  52699. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52700. + UGETW(ctrl.wLength));
  52701. +#endif
  52702. +
  52703. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52704. + case UT_DEVICE:
  52705. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  52706. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  52707. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  52708. + DWC_PRINTF("OTG CAP - %d, %d\n",
  52709. + core_if->core_params->otg_cap,
  52710. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  52711. + if (core_if->otg_ver == 1
  52712. + && core_if->core_params->otg_cap ==
  52713. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52714. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  52715. + *otgsts = (core_if->otg_sts & 0x1);
  52716. + pcd->ep0_pending = 1;
  52717. + ep0->dwc_ep.start_xfer_buff =
  52718. + (uint8_t *) otgsts;
  52719. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  52720. + ep0->dwc_ep.dma_addr =
  52721. + pcd->status_buf_dma_handle;
  52722. + ep0->dwc_ep.xfer_len = 1;
  52723. + ep0->dwc_ep.xfer_count = 0;
  52724. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  52725. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  52726. + &ep0->dwc_ep);
  52727. + return;
  52728. + } else {
  52729. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52730. + return;
  52731. + }
  52732. + break;
  52733. + } else {
  52734. + *status = 0x1; /* Self powered */
  52735. + *status |= pcd->remote_wakeup_enable << 1;
  52736. + break;
  52737. + }
  52738. + case UT_INTERFACE:
  52739. + *status = 0;
  52740. + break;
  52741. +
  52742. + case UT_ENDPOINT:
  52743. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52744. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  52745. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52746. + return;
  52747. + }
  52748. + /** @todo check for EP stall */
  52749. + *status = ep->stopped;
  52750. + break;
  52751. + }
  52752. + pcd->ep0_pending = 1;
  52753. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  52754. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  52755. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  52756. + ep0->dwc_ep.xfer_len = 2;
  52757. + ep0->dwc_ep.xfer_count = 0;
  52758. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  52759. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52760. +}
  52761. +
  52762. +/**
  52763. + * This function process the SET_FEATURE Setup Commands.
  52764. + */
  52765. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  52766. +{
  52767. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52768. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  52769. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52770. + dwc_otg_pcd_ep_t *ep = 0;
  52771. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  52772. + gotgctl_data_t gotgctl = {.d32 = 0 };
  52773. +
  52774. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  52775. + ctrl.bmRequestType, ctrl.bRequest,
  52776. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52777. + UGETW(ctrl.wLength));
  52778. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  52779. +
  52780. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52781. + case UT_DEVICE:
  52782. + switch (UGETW(ctrl.wValue)) {
  52783. + case UF_DEVICE_REMOTE_WAKEUP:
  52784. + pcd->remote_wakeup_enable = 1;
  52785. + break;
  52786. +
  52787. + case UF_TEST_MODE:
  52788. + /* Setup the Test Mode tasklet to do the Test
  52789. + * Packet generation after the SETUP Status
  52790. + * phase has completed. */
  52791. +
  52792. + /** @todo This has not been tested since the
  52793. + * tasklet struct was put into the PCD
  52794. + * struct! */
  52795. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  52796. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  52797. + break;
  52798. +
  52799. + case UF_DEVICE_B_HNP_ENABLE:
  52800. + DWC_DEBUGPL(DBG_PCDV,
  52801. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  52802. +
  52803. + /* dev may initiate HNP */
  52804. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52805. + pcd->b_hnp_enable = 1;
  52806. + dwc_otg_pcd_update_otg(pcd, 0);
  52807. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  52808. + /**@todo Is the gotgctl.devhnpen cleared
  52809. + * by a USB Reset? */
  52810. + gotgctl.b.devhnpen = 1;
  52811. + gotgctl.b.hnpreq = 1;
  52812. + DWC_WRITE_REG32(&global_regs->gotgctl,
  52813. + gotgctl.d32);
  52814. + } else {
  52815. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52816. + return;
  52817. + }
  52818. + break;
  52819. +
  52820. + case UF_DEVICE_A_HNP_SUPPORT:
  52821. + /* RH port supports HNP */
  52822. + DWC_DEBUGPL(DBG_PCDV,
  52823. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  52824. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52825. + pcd->a_hnp_support = 1;
  52826. + dwc_otg_pcd_update_otg(pcd, 0);
  52827. + } else {
  52828. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52829. + return;
  52830. + }
  52831. + break;
  52832. +
  52833. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  52834. + /* other RH port does */
  52835. + DWC_DEBUGPL(DBG_PCDV,
  52836. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  52837. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52838. + pcd->a_alt_hnp_support = 1;
  52839. + dwc_otg_pcd_update_otg(pcd, 0);
  52840. + } else {
  52841. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52842. + return;
  52843. + }
  52844. + break;
  52845. +
  52846. + default:
  52847. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52848. + return;
  52849. +
  52850. + }
  52851. + do_setup_in_status_phase(pcd);
  52852. + break;
  52853. +
  52854. + case UT_INTERFACE:
  52855. + do_gadget_setup(pcd, &ctrl);
  52856. + break;
  52857. +
  52858. + case UT_ENDPOINT:
  52859. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  52860. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52861. + if (ep == 0) {
  52862. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52863. + return;
  52864. + }
  52865. + ep->stopped = 1;
  52866. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  52867. + }
  52868. + do_setup_in_status_phase(pcd);
  52869. + break;
  52870. + }
  52871. +}
  52872. +
  52873. +/**
  52874. + * This function process the CLEAR_FEATURE Setup Commands.
  52875. + */
  52876. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  52877. +{
  52878. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52879. + dwc_otg_pcd_ep_t *ep = 0;
  52880. +
  52881. + DWC_DEBUGPL(DBG_PCD,
  52882. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  52883. + ctrl.bmRequestType, ctrl.bRequest,
  52884. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52885. + UGETW(ctrl.wLength));
  52886. +
  52887. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52888. + case UT_DEVICE:
  52889. + switch (UGETW(ctrl.wValue)) {
  52890. + case UF_DEVICE_REMOTE_WAKEUP:
  52891. + pcd->remote_wakeup_enable = 0;
  52892. + break;
  52893. +
  52894. + case UF_TEST_MODE:
  52895. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  52896. + break;
  52897. +
  52898. + default:
  52899. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52900. + return;
  52901. + }
  52902. + do_setup_in_status_phase(pcd);
  52903. + break;
  52904. +
  52905. + case UT_ENDPOINT:
  52906. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52907. + if (ep == 0) {
  52908. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52909. + return;
  52910. + }
  52911. +
  52912. + pcd_clear_halt(pcd, ep);
  52913. +
  52914. + break;
  52915. + }
  52916. +}
  52917. +
  52918. +/**
  52919. + * This function process the SET_ADDRESS Setup Commands.
  52920. + */
  52921. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  52922. +{
  52923. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  52924. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52925. +
  52926. + if (ctrl.bmRequestType == UT_DEVICE) {
  52927. + dcfg_data_t dcfg = {.d32 = 0 };
  52928. +
  52929. +#ifdef DEBUG_EP0
  52930. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  52931. +#endif
  52932. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  52933. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  52934. + do_setup_in_status_phase(pcd);
  52935. + }
  52936. +}
  52937. +
  52938. +/**
  52939. + * This function processes SETUP commands. In Linux, the USB Command
  52940. + * processing is done in two places - the first being the PCD and the
  52941. + * second in the Gadget Driver (for example, the File-Backed Storage
  52942. + * Gadget Driver).
  52943. + *
  52944. + * <table>
  52945. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  52946. + *
  52947. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  52948. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  52949. + * </td></tr>
  52950. + *
  52951. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  52952. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  52953. + * interface requests are ignored.</td></tr>
  52954. + *
  52955. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  52956. + * requests are processed by the PCD. Interface requests are passed
  52957. + * to the Gadget Driver.</td></tr>
  52958. + *
  52959. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  52960. + * with device address received </td></tr>
  52961. + *
  52962. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  52963. + * requested descriptor</td></tr>
  52964. + *
  52965. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  52966. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  52967. + *
  52968. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  52969. + * all EPs and enable EPs for new configuration.</td></tr>
  52970. + *
  52971. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  52972. + * the current configuration</td></tr>
  52973. + *
  52974. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  52975. + * EPs and enable EPs for new configuration.</td></tr>
  52976. + *
  52977. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  52978. + * current interface.</td></tr>
  52979. + *
  52980. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  52981. + * message.</td></tr>
  52982. + * </table>
  52983. + *
  52984. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  52985. + * processed by pcd_setup. Calling the Function Driver's setup function from
  52986. + * pcd_setup processes the gadget SETUP commands.
  52987. + */
  52988. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  52989. +{
  52990. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52991. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  52992. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52993. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52994. +
  52995. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  52996. +
  52997. +#ifdef DWC_UTE_CFI
  52998. + int retval = 0;
  52999. + struct cfi_usb_ctrlrequest cfi_req;
  53000. +#endif
  53001. +
  53002. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  53003. +
  53004. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  53005. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  53006. + && (doeptsize0.b.supcnt < 2)
  53007. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  53008. + DWC_ERROR
  53009. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  53010. + }
  53011. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  53012. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  53013. + ctrl =
  53014. + (pcd->setup_pkt +
  53015. + (3 - doeptsize0.b.supcnt - 1 +
  53016. + ep0->dwc_ep.stp_rollover))->req;
  53017. + }
  53018. +#ifdef DEBUG_EP0
  53019. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  53020. + ctrl.bmRequestType, ctrl.bRequest,
  53021. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  53022. + UGETW(ctrl.wLength));
  53023. +#endif
  53024. +
  53025. + /* Clean up the request queue */
  53026. + dwc_otg_request_nuke(ep0);
  53027. + ep0->stopped = 0;
  53028. +
  53029. + if (ctrl.bmRequestType & UE_DIR_IN) {
  53030. + ep0->dwc_ep.is_in = 1;
  53031. + pcd->ep0state = EP0_IN_DATA_PHASE;
  53032. + } else {
  53033. + ep0->dwc_ep.is_in = 0;
  53034. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  53035. + }
  53036. +
  53037. + if (UGETW(ctrl.wLength) == 0) {
  53038. + ep0->dwc_ep.is_in = 1;
  53039. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  53040. + }
  53041. +
  53042. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  53043. +
  53044. +#ifdef DWC_UTE_CFI
  53045. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  53046. +
  53047. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  53048. + ctrl.bRequestType, ctrl.bRequest);
  53049. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  53050. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  53051. + retval = cfi_setup(pcd, &cfi_req);
  53052. + if (retval < 0) {
  53053. + ep0_do_stall(pcd, retval);
  53054. + pcd->ep0_pending = 0;
  53055. + return;
  53056. + }
  53057. +
  53058. + /* if need gadget setup then call it and check the retval */
  53059. + if (pcd->cfi->need_gadget_att) {
  53060. + retval =
  53061. + cfi_gadget_setup(pcd,
  53062. + &pcd->
  53063. + cfi->ctrl_req);
  53064. + if (retval < 0) {
  53065. + pcd->ep0_pending = 0;
  53066. + return;
  53067. + }
  53068. + }
  53069. +
  53070. + if (pcd->cfi->need_status_in_complete) {
  53071. + do_setup_in_status_phase(pcd);
  53072. + }
  53073. + return;
  53074. + }
  53075. + }
  53076. +#endif
  53077. +
  53078. + /* handle non-standard (class/vendor) requests in the gadget driver */
  53079. + do_gadget_setup(pcd, &ctrl);
  53080. + return;
  53081. + }
  53082. +
  53083. + /** @todo NGS: Handle bad setup packet? */
  53084. +
  53085. +///////////////////////////////////////////
  53086. +//// --- Standard Request handling --- ////
  53087. +
  53088. + switch (ctrl.bRequest) {
  53089. + case UR_GET_STATUS:
  53090. + do_get_status(pcd);
  53091. + break;
  53092. +
  53093. + case UR_CLEAR_FEATURE:
  53094. + do_clear_feature(pcd);
  53095. + break;
  53096. +
  53097. + case UR_SET_FEATURE:
  53098. + do_set_feature(pcd);
  53099. + break;
  53100. +
  53101. + case UR_SET_ADDRESS:
  53102. + do_set_address(pcd);
  53103. + break;
  53104. +
  53105. + case UR_SET_INTERFACE:
  53106. + case UR_SET_CONFIG:
  53107. +// _pcd->request_config = 1; /* Configuration changed */
  53108. + do_gadget_setup(pcd, &ctrl);
  53109. + break;
  53110. +
  53111. + case UR_SYNCH_FRAME:
  53112. + do_gadget_setup(pcd, &ctrl);
  53113. + break;
  53114. +
  53115. + default:
  53116. + /* Call the Gadget Driver's setup functions */
  53117. + do_gadget_setup(pcd, &ctrl);
  53118. + break;
  53119. + }
  53120. +}
  53121. +
  53122. +/**
  53123. + * This function completes the ep0 control transfer.
  53124. + */
  53125. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  53126. +{
  53127. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  53128. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  53129. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  53130. + dev_if->in_ep_regs[ep->dwc_ep.num];
  53131. +#ifdef DEBUG_EP0
  53132. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  53133. + dev_if->out_ep_regs[ep->dwc_ep.num];
  53134. +#endif
  53135. + deptsiz0_data_t deptsiz;
  53136. + dev_dma_desc_sts_t desc_sts;
  53137. + dwc_otg_pcd_request_t *req;
  53138. + int is_last = 0;
  53139. + dwc_otg_pcd_t *pcd = ep->pcd;
  53140. +
  53141. +#ifdef DWC_UTE_CFI
  53142. + struct cfi_usb_ctrlrequest *ctrlreq;
  53143. + int retval = -DWC_E_NOT_SUPPORTED;
  53144. +#endif
  53145. +
  53146. + desc_sts.b.bytes = 0;
  53147. +
  53148. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  53149. + if (ep->dwc_ep.is_in) {
  53150. +#ifdef DEBUG_EP0
  53151. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  53152. +#endif
  53153. + do_setup_out_status_phase(pcd);
  53154. + } else {
  53155. +#ifdef DEBUG_EP0
  53156. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  53157. +#endif
  53158. +
  53159. +#ifdef DWC_UTE_CFI
  53160. + ctrlreq = &pcd->cfi->ctrl_req;
  53161. +
  53162. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  53163. + if (ctrlreq->bRequest > 0xB0
  53164. + && ctrlreq->bRequest < 0xBF) {
  53165. +
  53166. + /* Return if the PCD failed to handle the request */
  53167. + if ((retval =
  53168. + pcd->cfi->ops.
  53169. + ctrl_write_complete(pcd->cfi,
  53170. + pcd)) < 0) {
  53171. + CFI_INFO
  53172. + ("ERROR setting a new value in the PCD(%d)\n",
  53173. + retval);
  53174. + ep0_do_stall(pcd, retval);
  53175. + pcd->ep0_pending = 0;
  53176. + return 0;
  53177. + }
  53178. +
  53179. + /* If the gadget needs to be notified on the request */
  53180. + if (pcd->cfi->need_gadget_att == 1) {
  53181. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  53182. + retval =
  53183. + cfi_gadget_setup(pcd,
  53184. + &pcd->cfi->
  53185. + ctrl_req);
  53186. +
  53187. + /* Return from the function if the gadget failed to process
  53188. + * the request properly - this should never happen !!!
  53189. + */
  53190. + if (retval < 0) {
  53191. + CFI_INFO
  53192. + ("ERROR setting a new value in the gadget(%d)\n",
  53193. + retval);
  53194. + pcd->ep0_pending = 0;
  53195. + return 0;
  53196. + }
  53197. + }
  53198. +
  53199. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  53200. + retval);
  53201. + /* If we hit here then the PCD and the gadget has properly
  53202. + * handled the request - so send the ZLP IN to the host.
  53203. + */
  53204. + /* @todo: MAS - decide whether we need to start the setup
  53205. + * stage based on the need_setup value of the cfi object
  53206. + */
  53207. + do_setup_in_status_phase(pcd);
  53208. + pcd->ep0_pending = 0;
  53209. + return 1;
  53210. + }
  53211. + }
  53212. +#endif
  53213. +
  53214. + do_setup_in_status_phase(pcd);
  53215. + }
  53216. + pcd->ep0_pending = 0;
  53217. + return 1;
  53218. + }
  53219. +
  53220. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  53221. + return 0;
  53222. + }
  53223. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  53224. +
  53225. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  53226. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  53227. + is_last = 1;
  53228. + } else if (ep->dwc_ep.is_in) {
  53229. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  53230. + if (core_if->dma_desc_enable != 0)
  53231. + desc_sts = dev_if->in_desc_addr->status;
  53232. +#ifdef DEBUG_EP0
  53233. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  53234. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  53235. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53236. +#endif
  53237. +
  53238. + if (((core_if->dma_desc_enable == 0)
  53239. + && (deptsiz.b.xfersize == 0))
  53240. + || ((core_if->dma_desc_enable != 0)
  53241. + && (desc_sts.b.bytes == 0))) {
  53242. + req->actual = ep->dwc_ep.xfer_count;
  53243. + /* Is a Zero Len Packet needed? */
  53244. + if (req->sent_zlp) {
  53245. +#ifdef DEBUG_EP0
  53246. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  53247. +#endif
  53248. + req->sent_zlp = 0;
  53249. + }
  53250. + do_setup_out_status_phase(pcd);
  53251. + }
  53252. + } else {
  53253. + /* ep0-OUT */
  53254. +#ifdef DEBUG_EP0
  53255. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  53256. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  53257. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  53258. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53259. +#endif
  53260. + req->actual = ep->dwc_ep.xfer_count;
  53261. +
  53262. + /* Is a Zero Len Packet needed? */
  53263. + if (req->sent_zlp) {
  53264. +#ifdef DEBUG_EP0
  53265. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  53266. +#endif
  53267. + req->sent_zlp = 0;
  53268. + }
  53269. + /* For older cores do setup in status phase in Slave/BDMA modes,
  53270. + * starting from 3.00 do that only in slave, and for DMA modes
  53271. + * just re-enable ep 0 OUT here*/
  53272. + if (core_if->dma_enable == 0
  53273. + || (core_if->dma_desc_enable == 0
  53274. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  53275. + do_setup_in_status_phase(pcd);
  53276. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  53277. + DWC_DEBUGPL(DBG_PCDV,
  53278. + "Enable out ep before in status phase\n");
  53279. + ep0_out_start(core_if, pcd);
  53280. + }
  53281. + }
  53282. +
  53283. + /* Complete the request */
  53284. + if (is_last) {
  53285. + dwc_otg_request_done(ep, req, 0);
  53286. + ep->dwc_ep.start_xfer_buff = 0;
  53287. + ep->dwc_ep.xfer_buff = 0;
  53288. + ep->dwc_ep.xfer_len = 0;
  53289. + return 1;
  53290. + }
  53291. + return 0;
  53292. +}
  53293. +
  53294. +#ifdef DWC_UTE_CFI
  53295. +/**
  53296. + * This function calculates traverses all the CFI DMA descriptors and
  53297. + * and accumulates the bytes that are left to be transfered.
  53298. + *
  53299. + * @return The total bytes left to transfered, or a negative value as failure
  53300. + */
  53301. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  53302. +{
  53303. + int32_t ret = 0;
  53304. + int i;
  53305. + struct dwc_otg_dma_desc *ddesc = NULL;
  53306. + struct cfi_ep *cfiep;
  53307. +
  53308. + /* See if the pcd_ep has its respective cfi_ep mapped */
  53309. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  53310. + if (!cfiep) {
  53311. + CFI_INFO("%s: Failed to find ep\n", __func__);
  53312. + return -1;
  53313. + }
  53314. +
  53315. + ddesc = ep->dwc_ep.descs;
  53316. +
  53317. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  53318. +
  53319. +#if defined(PRINT_CFI_DMA_DESCS)
  53320. + print_desc(ddesc, ep->ep.name, i);
  53321. +#endif
  53322. + ret += ddesc->status.b.bytes;
  53323. + ddesc++;
  53324. + }
  53325. +
  53326. + if (ret)
  53327. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  53328. + ret);
  53329. +
  53330. + return ret;
  53331. +}
  53332. +#endif
  53333. +
  53334. +/**
  53335. + * This function completes the request for the EP. If there are
  53336. + * additional requests for the EP in the queue they will be started.
  53337. + */
  53338. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  53339. +{
  53340. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  53341. + struct device *dev = dwc_otg_pcd_to_dev(ep->pcd);
  53342. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  53343. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  53344. + dev_if->in_ep_regs[ep->dwc_ep.num];
  53345. + deptsiz_data_t deptsiz;
  53346. + dev_dma_desc_sts_t desc_sts;
  53347. + dwc_otg_pcd_request_t *req = 0;
  53348. + dwc_otg_dev_dma_desc_t *dma_desc;
  53349. + uint32_t byte_count = 0;
  53350. + int is_last = 0;
  53351. + int i;
  53352. +
  53353. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  53354. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  53355. +
  53356. + /* Get any pending requests */
  53357. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  53358. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  53359. + if (!req) {
  53360. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  53361. + return;
  53362. + }
  53363. + } else {
  53364. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  53365. + return;
  53366. + }
  53367. +
  53368. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  53369. +
  53370. + if (ep->dwc_ep.is_in) {
  53371. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  53372. +
  53373. + if (core_if->dma_enable) {
  53374. + if (core_if->dma_desc_enable == 0) {
  53375. + if (deptsiz.b.xfersize == 0
  53376. + && deptsiz.b.pktcnt == 0) {
  53377. + byte_count =
  53378. + ep->dwc_ep.xfer_len -
  53379. + ep->dwc_ep.xfer_count;
  53380. +
  53381. + ep->dwc_ep.xfer_buff += byte_count;
  53382. + ep->dwc_ep.dma_addr += byte_count;
  53383. + ep->dwc_ep.xfer_count += byte_count;
  53384. +
  53385. + DWC_DEBUGPL(DBG_PCDV,
  53386. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  53387. + ep->dwc_ep.num,
  53388. + (ep->dwc_ep.
  53389. + is_in ? "IN" : "OUT"),
  53390. + ep->dwc_ep.xfer_len,
  53391. + deptsiz.b.xfersize,
  53392. + deptsiz.b.pktcnt);
  53393. +
  53394. + if (ep->dwc_ep.xfer_len <
  53395. + ep->dwc_ep.total_len) {
  53396. + dwc_otg_ep_start_transfer
  53397. + (core_if, &ep->dwc_ep);
  53398. + } else if (ep->dwc_ep.sent_zlp) {
  53399. + /*
  53400. + * This fragment of code should initiate 0
  53401. + * length transfer in case if it is queued
  53402. + * a transfer with size divisible to EPs max
  53403. + * packet size and with usb_request zero field
  53404. + * is set, which means that after data is transfered,
  53405. + * it is also should be transfered
  53406. + * a 0 length packet at the end. For Slave and
  53407. + * Buffer DMA modes in this case SW has
  53408. + * to initiate 2 transfers one with transfer size,
  53409. + * and the second with 0 size. For Descriptor
  53410. + * DMA mode SW is able to initiate a transfer,
  53411. + * which will handle all the packets including
  53412. + * the last 0 length.
  53413. + */
  53414. + ep->dwc_ep.sent_zlp = 0;
  53415. + dwc_otg_ep_start_zl_transfer
  53416. + (core_if, &ep->dwc_ep);
  53417. + } else {
  53418. + is_last = 1;
  53419. + }
  53420. + } else {
  53421. + if (ep->dwc_ep.type ==
  53422. + DWC_OTG_EP_TYPE_ISOC) {
  53423. + req->actual = 0;
  53424. + dwc_otg_request_done(ep, req, 0);
  53425. +
  53426. + ep->dwc_ep.start_xfer_buff = 0;
  53427. + ep->dwc_ep.xfer_buff = 0;
  53428. + ep->dwc_ep.xfer_len = 0;
  53429. +
  53430. + /* If there is a request in the queue start it. */
  53431. + start_next_request(ep);
  53432. + } else
  53433. + DWC_WARN
  53434. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  53435. + ep->dwc_ep.num,
  53436. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  53437. + deptsiz.b.xfersize,
  53438. + deptsiz.b.pktcnt);
  53439. + }
  53440. + } else {
  53441. + dma_desc = ep->dwc_ep.desc_addr;
  53442. + byte_count = 0;
  53443. + ep->dwc_ep.sent_zlp = 0;
  53444. +
  53445. +#ifdef DWC_UTE_CFI
  53446. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  53447. + ep->dwc_ep.buff_mode);
  53448. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53449. + int residue;
  53450. +
  53451. + residue = cfi_calc_desc_residue(ep);
  53452. + if (residue < 0)
  53453. + return;
  53454. +
  53455. + byte_count = residue;
  53456. + } else {
  53457. +#endif
  53458. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  53459. + ++i) {
  53460. + desc_sts = dma_desc->status;
  53461. + byte_count += desc_sts.b.bytes;
  53462. + dma_desc++;
  53463. + }
  53464. +#ifdef DWC_UTE_CFI
  53465. + }
  53466. +#endif
  53467. + if (byte_count == 0) {
  53468. + ep->dwc_ep.xfer_count =
  53469. + ep->dwc_ep.total_len;
  53470. + is_last = 1;
  53471. + } else {
  53472. + DWC_WARN("Incomplete transfer\n");
  53473. + }
  53474. + }
  53475. + } else {
  53476. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  53477. + DWC_DEBUGPL(DBG_PCDV,
  53478. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  53479. + ep->dwc_ep.num,
  53480. + ep->dwc_ep.is_in ? "IN" : "OUT",
  53481. + ep->dwc_ep.xfer_len,
  53482. + deptsiz.b.xfersize,
  53483. + deptsiz.b.pktcnt);
  53484. +
  53485. + /* Check if the whole transfer was completed,
  53486. + * if no, setup transfer for next portion of data
  53487. + */
  53488. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53489. + dwc_otg_ep_start_transfer(core_if,
  53490. + &ep->dwc_ep);
  53491. + } else if (ep->dwc_ep.sent_zlp) {
  53492. + /*
  53493. + * This fragment of code should initiate 0
  53494. + * length trasfer in case if it is queued
  53495. + * a trasfer with size divisible to EPs max
  53496. + * packet size and with usb_request zero field
  53497. + * is set, which means that after data is transfered,
  53498. + * it is also should be transfered
  53499. + * a 0 length packet at the end. For Slave and
  53500. + * Buffer DMA modes in this case SW has
  53501. + * to initiate 2 transfers one with transfer size,
  53502. + * and the second with 0 size. For Desriptor
  53503. + * DMA mode SW is able to initiate a transfer,
  53504. + * which will handle all the packets including
  53505. + * the last 0 legth.
  53506. + */
  53507. + ep->dwc_ep.sent_zlp = 0;
  53508. + dwc_otg_ep_start_zl_transfer(core_if,
  53509. + &ep->dwc_ep);
  53510. + } else {
  53511. + is_last = 1;
  53512. + }
  53513. + } else {
  53514. + DWC_WARN
  53515. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  53516. + ep->dwc_ep.num,
  53517. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  53518. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53519. + }
  53520. + }
  53521. + } else {
  53522. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  53523. + dev_if->out_ep_regs[ep->dwc_ep.num];
  53524. + desc_sts.d32 = 0;
  53525. + if (core_if->dma_enable) {
  53526. + if (core_if->dma_desc_enable) {
  53527. + dma_desc = ep->dwc_ep.desc_addr;
  53528. + byte_count = 0;
  53529. + ep->dwc_ep.sent_zlp = 0;
  53530. +
  53531. +#ifdef DWC_UTE_CFI
  53532. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  53533. + ep->dwc_ep.buff_mode);
  53534. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53535. + int residue;
  53536. + residue = cfi_calc_desc_residue(ep);
  53537. + if (residue < 0)
  53538. + return;
  53539. + byte_count = residue;
  53540. + } else {
  53541. +#endif
  53542. +
  53543. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  53544. + ++i) {
  53545. + desc_sts = dma_desc->status;
  53546. + byte_count += desc_sts.b.bytes;
  53547. + dma_desc++;
  53548. + }
  53549. +
  53550. +#ifdef DWC_UTE_CFI
  53551. + }
  53552. +#endif
  53553. + /* Checking for interrupt Out transfers with not
  53554. + * dword aligned mps sizes
  53555. + */
  53556. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  53557. + (ep->dwc_ep.maxpacket%4)) {
  53558. + ep->dwc_ep.xfer_count =
  53559. + ep->dwc_ep.total_len - byte_count;
  53560. + if ((ep->dwc_ep.xfer_len %
  53561. + ep->dwc_ep.maxpacket)
  53562. + && (ep->dwc_ep.xfer_len /
  53563. + ep->dwc_ep.maxpacket <
  53564. + MAX_DMA_DESC_CNT))
  53565. + ep->dwc_ep.xfer_len -=
  53566. + (ep->dwc_ep.desc_cnt -
  53567. + 1) * ep->dwc_ep.maxpacket +
  53568. + ep->dwc_ep.xfer_len %
  53569. + ep->dwc_ep.maxpacket;
  53570. + else
  53571. + ep->dwc_ep.xfer_len -=
  53572. + ep->dwc_ep.desc_cnt *
  53573. + ep->dwc_ep.maxpacket;
  53574. + if (ep->dwc_ep.xfer_len > 0) {
  53575. + dwc_otg_ep_start_transfer
  53576. + (core_if, &ep->dwc_ep);
  53577. + } else {
  53578. + is_last = 1;
  53579. + }
  53580. + } else {
  53581. + ep->dwc_ep.xfer_count =
  53582. + ep->dwc_ep.total_len - byte_count +
  53583. + ((4 -
  53584. + (ep->dwc_ep.
  53585. + total_len & 0x3)) & 0x3);
  53586. + is_last = 1;
  53587. + }
  53588. + } else {
  53589. + deptsiz.d32 = 0;
  53590. + deptsiz.d32 =
  53591. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  53592. +
  53593. + byte_count = (ep->dwc_ep.xfer_len -
  53594. + ep->dwc_ep.xfer_count -
  53595. + deptsiz.b.xfersize);
  53596. + ep->dwc_ep.xfer_buff += byte_count;
  53597. + ep->dwc_ep.dma_addr += byte_count;
  53598. + ep->dwc_ep.xfer_count += byte_count;
  53599. +
  53600. + /* Check if the whole transfer was completed,
  53601. + * if no, setup transfer for next portion of data
  53602. + */
  53603. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53604. + dwc_otg_ep_start_transfer(core_if,
  53605. + &ep->dwc_ep);
  53606. + } else if (ep->dwc_ep.sent_zlp) {
  53607. + /*
  53608. + * This fragment of code should initiate 0
  53609. + * length trasfer in case if it is queued
  53610. + * a trasfer with size divisible to EPs max
  53611. + * packet size and with usb_request zero field
  53612. + * is set, which means that after data is transfered,
  53613. + * it is also should be transfered
  53614. + * a 0 length packet at the end. For Slave and
  53615. + * Buffer DMA modes in this case SW has
  53616. + * to initiate 2 transfers one with transfer size,
  53617. + * and the second with 0 size. For Desriptor
  53618. + * DMA mode SW is able to initiate a transfer,
  53619. + * which will handle all the packets including
  53620. + * the last 0 legth.
  53621. + */
  53622. + ep->dwc_ep.sent_zlp = 0;
  53623. + dwc_otg_ep_start_zl_transfer(core_if,
  53624. + &ep->dwc_ep);
  53625. + } else {
  53626. + is_last = 1;
  53627. + }
  53628. + }
  53629. + } else {
  53630. + /* Check if the whole transfer was completed,
  53631. + * if no, setup transfer for next portion of data
  53632. + */
  53633. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53634. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  53635. + } else if (ep->dwc_ep.sent_zlp) {
  53636. + /*
  53637. + * This fragment of code should initiate 0
  53638. + * length transfer in case if it is queued
  53639. + * a transfer with size divisible to EPs max
  53640. + * packet size and with usb_request zero field
  53641. + * is set, which means that after data is transfered,
  53642. + * it is also should be transfered
  53643. + * a 0 length packet at the end. For Slave and
  53644. + * Buffer DMA modes in this case SW has
  53645. + * to initiate 2 transfers one with transfer size,
  53646. + * and the second with 0 size. For Descriptor
  53647. + * DMA mode SW is able to initiate a transfer,
  53648. + * which will handle all the packets including
  53649. + * the last 0 length.
  53650. + */
  53651. + ep->dwc_ep.sent_zlp = 0;
  53652. + dwc_otg_ep_start_zl_transfer(core_if,
  53653. + &ep->dwc_ep);
  53654. + } else {
  53655. + is_last = 1;
  53656. + }
  53657. + }
  53658. +
  53659. + DWC_DEBUGPL(DBG_PCDV,
  53660. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  53661. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  53662. + ep->dwc_ep.is_in ? "IN" : "OUT",
  53663. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  53664. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53665. + }
  53666. +
  53667. + /* Complete the request */
  53668. + if (is_last) {
  53669. +#ifdef DWC_UTE_CFI
  53670. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53671. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  53672. + } else {
  53673. +#endif
  53674. + req->actual = ep->dwc_ep.xfer_count;
  53675. +#ifdef DWC_UTE_CFI
  53676. + }
  53677. +#endif
  53678. + if (req->dw_align_buf) {
  53679. + if (!ep->dwc_ep.is_in) {
  53680. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  53681. + }
  53682. + DWC_DMA_FREE(dev, req->length, req->dw_align_buf,
  53683. + req->dw_align_buf_dma);
  53684. + }
  53685. +
  53686. + dwc_otg_request_done(ep, req, 0);
  53687. +
  53688. + ep->dwc_ep.start_xfer_buff = 0;
  53689. + ep->dwc_ep.xfer_buff = 0;
  53690. + ep->dwc_ep.xfer_len = 0;
  53691. +
  53692. + /* If there is a request in the queue start it. */
  53693. + start_next_request(ep);
  53694. + }
  53695. +}
  53696. +
  53697. +#ifdef DWC_EN_ISOC
  53698. +
  53699. +/**
  53700. + * This function BNA interrupt for Isochronous EPs
  53701. + *
  53702. + */
  53703. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  53704. +{
  53705. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  53706. + volatile uint32_t *addr;
  53707. + depctl_data_t depctl = {.d32 = 0 };
  53708. + dwc_otg_pcd_t *pcd = ep->pcd;
  53709. + dwc_otg_dev_dma_desc_t *dma_desc;
  53710. + int i;
  53711. +
  53712. + dma_desc =
  53713. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  53714. +
  53715. + if (dwc_ep->is_in) {
  53716. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53717. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  53718. + sts.d32 = dma_desc->status.d32;
  53719. + sts.b_iso_in.bs = BS_HOST_READY;
  53720. + dma_desc->status.d32 = sts.d32;
  53721. + }
  53722. + } else {
  53723. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53724. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  53725. + sts.d32 = dma_desc->status.d32;
  53726. + sts.b_iso_out.bs = BS_HOST_READY;
  53727. + dma_desc->status.d32 = sts.d32;
  53728. + }
  53729. + }
  53730. +
  53731. + if (dwc_ep->is_in == 0) {
  53732. + addr =
  53733. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  53734. + num]->doepctl;
  53735. + } else {
  53736. + addr =
  53737. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  53738. + }
  53739. + depctl.b.epena = 1;
  53740. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  53741. +}
  53742. +
  53743. +/**
  53744. + * This function sets latest iso packet information(non-PTI mode)
  53745. + *
  53746. + * @param core_if Programming view of DWC_otg controller.
  53747. + * @param ep The EP to start the transfer on.
  53748. + *
  53749. + */
  53750. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  53751. +{
  53752. + deptsiz_data_t deptsiz = {.d32 = 0 };
  53753. + dma_addr_t dma_addr;
  53754. + uint32_t offset;
  53755. +
  53756. + if (ep->proc_buf_num)
  53757. + dma_addr = ep->dma_addr1;
  53758. + else
  53759. + dma_addr = ep->dma_addr0;
  53760. +
  53761. + if (ep->is_in) {
  53762. + deptsiz.d32 =
  53763. + DWC_READ_REG32(&core_if->dev_if->
  53764. + in_ep_regs[ep->num]->dieptsiz);
  53765. + offset = ep->data_per_frame;
  53766. + } else {
  53767. + deptsiz.d32 =
  53768. + DWC_READ_REG32(&core_if->dev_if->
  53769. + out_ep_regs[ep->num]->doeptsiz);
  53770. + offset =
  53771. + ep->data_per_frame +
  53772. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  53773. + }
  53774. +
  53775. + if (!deptsiz.b.xfersize) {
  53776. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  53777. + ep->pkt_info[ep->cur_pkt].offset =
  53778. + ep->cur_pkt_dma_addr - dma_addr;
  53779. + ep->pkt_info[ep->cur_pkt].status = 0;
  53780. + } else {
  53781. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  53782. + ep->pkt_info[ep->cur_pkt].offset =
  53783. + ep->cur_pkt_dma_addr - dma_addr;
  53784. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  53785. + }
  53786. + ep->cur_pkt_addr += offset;
  53787. + ep->cur_pkt_dma_addr += offset;
  53788. + ep->cur_pkt++;
  53789. +}
  53790. +
  53791. +/**
  53792. + * This function sets latest iso packet information(DDMA mode)
  53793. + *
  53794. + * @param core_if Programming view of DWC_otg controller.
  53795. + * @param dwc_ep The EP to start the transfer on.
  53796. + *
  53797. + */
  53798. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  53799. + dwc_ep_t * dwc_ep)
  53800. +{
  53801. + dwc_otg_dev_dma_desc_t *dma_desc;
  53802. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53803. + iso_pkt_info_t *iso_packet;
  53804. + uint32_t data_per_desc;
  53805. + uint32_t offset;
  53806. + int i, j;
  53807. +
  53808. + iso_packet = dwc_ep->pkt_info;
  53809. +
  53810. + /** Reinit closed DMA Descriptors*/
  53811. + /** ISO OUT EP */
  53812. + if (dwc_ep->is_in == 0) {
  53813. + dma_desc =
  53814. + dwc_ep->iso_desc_addr +
  53815. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53816. + offset = 0;
  53817. +
  53818. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  53819. + i += dwc_ep->pkt_per_frm) {
  53820. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  53821. + data_per_desc =
  53822. + ((j + 1) * dwc_ep->maxpacket >
  53823. + dwc_ep->
  53824. + data_per_frame) ? dwc_ep->data_per_frame -
  53825. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53826. + data_per_desc +=
  53827. + (data_per_desc % 4) ? (4 -
  53828. + data_per_desc %
  53829. + 4) : 0;
  53830. +
  53831. + sts.d32 = dma_desc->status.d32;
  53832. +
  53833. + /* Write status in iso_packet_decsriptor */
  53834. + iso_packet->status =
  53835. + sts.b_iso_out.rxsts +
  53836. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53837. + if (iso_packet->status) {
  53838. + iso_packet->status = -DWC_E_NO_DATA;
  53839. + }
  53840. +
  53841. + /* Received data length */
  53842. + if (!sts.b_iso_out.rxbytes) {
  53843. + iso_packet->length =
  53844. + data_per_desc -
  53845. + sts.b_iso_out.rxbytes;
  53846. + } else {
  53847. + iso_packet->length =
  53848. + data_per_desc -
  53849. + sts.b_iso_out.rxbytes + (4 -
  53850. + dwc_ep->data_per_frame
  53851. + % 4);
  53852. + }
  53853. +
  53854. + iso_packet->offset = offset;
  53855. +
  53856. + offset += data_per_desc;
  53857. + dma_desc++;
  53858. + iso_packet++;
  53859. + }
  53860. + }
  53861. +
  53862. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  53863. + data_per_desc =
  53864. + ((j + 1) * dwc_ep->maxpacket >
  53865. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53866. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53867. + data_per_desc +=
  53868. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53869. +
  53870. + sts.d32 = dma_desc->status.d32;
  53871. +
  53872. + /* Write status in iso_packet_decsriptor */
  53873. + iso_packet->status =
  53874. + sts.b_iso_out.rxsts +
  53875. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53876. + if (iso_packet->status) {
  53877. + iso_packet->status = -DWC_E_NO_DATA;
  53878. + }
  53879. +
  53880. + /* Received data length */
  53881. + iso_packet->length =
  53882. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  53883. +
  53884. + iso_packet->offset = offset;
  53885. +
  53886. + offset += data_per_desc;
  53887. + iso_packet++;
  53888. + dma_desc++;
  53889. + }
  53890. +
  53891. + sts.d32 = dma_desc->status.d32;
  53892. +
  53893. + /* Write status in iso_packet_decsriptor */
  53894. + iso_packet->status =
  53895. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53896. + if (iso_packet->status) {
  53897. + iso_packet->status = -DWC_E_NO_DATA;
  53898. + }
  53899. + /* Received data length */
  53900. + if (!sts.b_iso_out.rxbytes) {
  53901. + iso_packet->length =
  53902. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  53903. + } else {
  53904. + iso_packet->length =
  53905. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  53906. + (4 - dwc_ep->data_per_frame % 4);
  53907. + }
  53908. +
  53909. + iso_packet->offset = offset;
  53910. + } else {
  53911. +/** ISO IN EP */
  53912. +
  53913. + dma_desc =
  53914. + dwc_ep->iso_desc_addr +
  53915. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53916. +
  53917. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  53918. + sts.d32 = dma_desc->status.d32;
  53919. +
  53920. + /* Write status in iso packet descriptor */
  53921. + iso_packet->status =
  53922. + sts.b_iso_in.txsts +
  53923. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  53924. + if (iso_packet->status != 0) {
  53925. + iso_packet->status = -DWC_E_NO_DATA;
  53926. +
  53927. + }
  53928. + /* Bytes has been transfered */
  53929. + iso_packet->length =
  53930. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  53931. +
  53932. + dma_desc++;
  53933. + iso_packet++;
  53934. + }
  53935. +
  53936. + sts.d32 = dma_desc->status.d32;
  53937. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  53938. + sts.d32 = dma_desc->status.d32;
  53939. + }
  53940. +
  53941. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  53942. + iso_packet->status =
  53943. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  53944. + if (iso_packet->status != 0) {
  53945. + iso_packet->status = -DWC_E_NO_DATA;
  53946. + }
  53947. +
  53948. + /* Bytes has been transfered */
  53949. + iso_packet->length =
  53950. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  53951. + }
  53952. +}
  53953. +
  53954. +/**
  53955. + * This function reinitialize DMA Descriptors for Isochronous transfer
  53956. + *
  53957. + * @param core_if Programming view of DWC_otg controller.
  53958. + * @param dwc_ep The EP to start the transfer on.
  53959. + *
  53960. + */
  53961. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  53962. +{
  53963. + int i, j;
  53964. + dwc_otg_dev_dma_desc_t *dma_desc;
  53965. + dma_addr_t dma_ad;
  53966. + volatile uint32_t *addr;
  53967. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53968. + uint32_t data_per_desc;
  53969. +
  53970. + if (dwc_ep->is_in == 0) {
  53971. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  53972. + } else {
  53973. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  53974. + }
  53975. +
  53976. + if (dwc_ep->proc_buf_num == 0) {
  53977. + /** Buffer 0 descriptors setup */
  53978. + dma_ad = dwc_ep->dma_addr0;
  53979. + } else {
  53980. + /** Buffer 1 descriptors setup */
  53981. + dma_ad = dwc_ep->dma_addr1;
  53982. + }
  53983. +
  53984. + /** Reinit closed DMA Descriptors*/
  53985. + /** ISO OUT EP */
  53986. + if (dwc_ep->is_in == 0) {
  53987. + dma_desc =
  53988. + dwc_ep->iso_desc_addr +
  53989. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53990. +
  53991. + sts.b_iso_out.bs = BS_HOST_READY;
  53992. + sts.b_iso_out.rxsts = 0;
  53993. + sts.b_iso_out.l = 0;
  53994. + sts.b_iso_out.sp = 0;
  53995. + sts.b_iso_out.ioc = 0;
  53996. + sts.b_iso_out.pid = 0;
  53997. + sts.b_iso_out.framenum = 0;
  53998. +
  53999. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  54000. + i += dwc_ep->pkt_per_frm) {
  54001. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  54002. + data_per_desc =
  54003. + ((j + 1) * dwc_ep->maxpacket >
  54004. + dwc_ep->
  54005. + data_per_frame) ? dwc_ep->data_per_frame -
  54006. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  54007. + data_per_desc +=
  54008. + (data_per_desc % 4) ? (4 -
  54009. + data_per_desc %
  54010. + 4) : 0;
  54011. + sts.b_iso_out.rxbytes = data_per_desc;
  54012. + dma_desc->buf = dma_ad;
  54013. + dma_desc->status.d32 = sts.d32;
  54014. +
  54015. + dma_ad += data_per_desc;
  54016. + dma_desc++;
  54017. + }
  54018. + }
  54019. +
  54020. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  54021. +
  54022. + data_per_desc =
  54023. + ((j + 1) * dwc_ep->maxpacket >
  54024. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  54025. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  54026. + data_per_desc +=
  54027. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  54028. + sts.b_iso_out.rxbytes = data_per_desc;
  54029. +
  54030. + dma_desc->buf = dma_ad;
  54031. + dma_desc->status.d32 = sts.d32;
  54032. +
  54033. + dma_desc++;
  54034. + dma_ad += data_per_desc;
  54035. + }
  54036. +
  54037. + sts.b_iso_out.ioc = 1;
  54038. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  54039. +
  54040. + data_per_desc =
  54041. + ((j + 1) * dwc_ep->maxpacket >
  54042. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  54043. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  54044. + data_per_desc +=
  54045. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  54046. + sts.b_iso_out.rxbytes = data_per_desc;
  54047. +
  54048. + dma_desc->buf = dma_ad;
  54049. + dma_desc->status.d32 = sts.d32;
  54050. + } else {
  54051. +/** ISO IN EP */
  54052. +
  54053. + dma_desc =
  54054. + dwc_ep->iso_desc_addr +
  54055. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  54056. +
  54057. + sts.b_iso_in.bs = BS_HOST_READY;
  54058. + sts.b_iso_in.txsts = 0;
  54059. + sts.b_iso_in.sp = 0;
  54060. + sts.b_iso_in.ioc = 0;
  54061. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  54062. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  54063. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  54064. + sts.b_iso_in.l = 0;
  54065. +
  54066. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  54067. + dma_desc->buf = dma_ad;
  54068. + dma_desc->status.d32 = sts.d32;
  54069. +
  54070. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  54071. + dma_ad += dwc_ep->data_per_frame;
  54072. + dma_desc++;
  54073. + }
  54074. +
  54075. + sts.b_iso_in.ioc = 1;
  54076. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  54077. +
  54078. + dma_desc->buf = dma_ad;
  54079. + dma_desc->status.d32 = sts.d32;
  54080. +
  54081. + dwc_ep->next_frame =
  54082. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  54083. + }
  54084. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54085. +}
  54086. +
  54087. +/**
  54088. + * This function is to handle Iso EP transfer complete interrupt
  54089. + * in case Iso out packet was dropped
  54090. + *
  54091. + * @param core_if Programming view of DWC_otg controller.
  54092. + * @param dwc_ep The EP for wihich transfer complete was asserted
  54093. + *
  54094. + */
  54095. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  54096. + dwc_ep_t * dwc_ep)
  54097. +{
  54098. + uint32_t dma_addr;
  54099. + uint32_t drp_pkt;
  54100. + uint32_t drp_pkt_cnt;
  54101. + deptsiz_data_t deptsiz = {.d32 = 0 };
  54102. + depctl_data_t depctl = {.d32 = 0 };
  54103. + int i;
  54104. +
  54105. + deptsiz.d32 =
  54106. + DWC_READ_REG32(&core_if->dev_if->
  54107. + out_ep_regs[dwc_ep->num]->doeptsiz);
  54108. +
  54109. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  54110. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  54111. +
  54112. + /* Setting dropped packets status */
  54113. + for (i = 0; i < drp_pkt_cnt; ++i) {
  54114. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  54115. + drp_pkt++;
  54116. + deptsiz.b.pktcnt--;
  54117. + }
  54118. +
  54119. + if (deptsiz.b.pktcnt > 0) {
  54120. + deptsiz.b.xfersize =
  54121. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  54122. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  54123. + } else {
  54124. + deptsiz.b.xfersize = 0;
  54125. + deptsiz.b.pktcnt = 0;
  54126. + }
  54127. +
  54128. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  54129. + deptsiz.d32);
  54130. +
  54131. + if (deptsiz.b.pktcnt > 0) {
  54132. + if (dwc_ep->proc_buf_num) {
  54133. + dma_addr =
  54134. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  54135. + deptsiz.b.xfersize;
  54136. + } else {
  54137. + dma_addr =
  54138. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  54139. + deptsiz.b.xfersize;;
  54140. + }
  54141. +
  54142. + DWC_WRITE_REG32(&core_if->dev_if->
  54143. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  54144. +
  54145. + /** Re-enable endpoint, clear nak */
  54146. + depctl.d32 = 0;
  54147. + depctl.b.epena = 1;
  54148. + depctl.b.cnak = 1;
  54149. +
  54150. + DWC_MODIFY_REG32(&core_if->dev_if->
  54151. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  54152. + depctl.d32);
  54153. + return 0;
  54154. + } else {
  54155. + return 1;
  54156. + }
  54157. +}
  54158. +
  54159. +/**
  54160. + * This function sets iso packets information(PTI mode)
  54161. + *
  54162. + * @param core_if Programming view of DWC_otg controller.
  54163. + * @param ep The EP to start the transfer on.
  54164. + *
  54165. + */
  54166. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  54167. +{
  54168. + int i, j;
  54169. + dma_addr_t dma_ad;
  54170. + iso_pkt_info_t *packet_info = ep->pkt_info;
  54171. + uint32_t offset;
  54172. + uint32_t frame_data;
  54173. + deptsiz_data_t deptsiz;
  54174. +
  54175. + if (ep->proc_buf_num == 0) {
  54176. + /** Buffer 0 descriptors setup */
  54177. + dma_ad = ep->dma_addr0;
  54178. + } else {
  54179. + /** Buffer 1 descriptors setup */
  54180. + dma_ad = ep->dma_addr1;
  54181. + }
  54182. +
  54183. + if (ep->is_in) {
  54184. + deptsiz.d32 =
  54185. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  54186. + dieptsiz);
  54187. + } else {
  54188. + deptsiz.d32 =
  54189. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  54190. + doeptsiz);
  54191. + }
  54192. +
  54193. + if (!deptsiz.b.xfersize) {
  54194. + offset = 0;
  54195. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  54196. + frame_data = ep->data_per_frame;
  54197. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  54198. +
  54199. + /* Packet status - is not set as initially
  54200. + * it is set to 0 and if packet was sent
  54201. + successfully, status field will remain 0*/
  54202. +
  54203. + /* Bytes has been transfered */
  54204. + packet_info->length =
  54205. + (ep->maxpacket <
  54206. + frame_data) ? ep->maxpacket : frame_data;
  54207. +
  54208. + /* Received packet offset */
  54209. + packet_info->offset = offset;
  54210. + offset += packet_info->length;
  54211. + frame_data -= packet_info->length;
  54212. +
  54213. + packet_info++;
  54214. + }
  54215. + }
  54216. + return 1;
  54217. + } else {
  54218. + /* This is a workaround for in case of Transfer Complete with
  54219. + * PktDrpSts interrupts merging - in this case Transfer complete
  54220. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  54221. + * set and with DOEPTSIZ register non zero. Investigations showed,
  54222. + * that this happens when Out packet is dropped, but because of
  54223. + * interrupts merging during first interrupt handling PktDrpSts
  54224. + * bit is cleared and for next merged interrupts it is not reset.
  54225. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  54226. + */
  54227. + if (ep->is_in) {
  54228. + return 1;
  54229. + } else {
  54230. + return handle_iso_out_pkt_dropped(core_if, ep);
  54231. + }
  54232. + }
  54233. +}
  54234. +
  54235. +/**
  54236. + * This function is to handle Iso EP transfer complete interrupt
  54237. + *
  54238. + * @param pcd The PCD
  54239. + * @param ep The EP for which transfer complete was asserted
  54240. + *
  54241. + */
  54242. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  54243. +{
  54244. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  54245. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  54246. + uint8_t is_last = 0;
  54247. +
  54248. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  54249. + DWC_WARN("Next frame is not set!\n");
  54250. + return;
  54251. + }
  54252. +
  54253. + if (core_if->dma_enable) {
  54254. + if (core_if->dma_desc_enable) {
  54255. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  54256. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  54257. + is_last = 1;
  54258. + } else {
  54259. + if (core_if->pti_enh_enable) {
  54260. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  54261. + dwc_ep->proc_buf_num =
  54262. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54263. + dwc_otg_iso_ep_start_buf_transfer
  54264. + (core_if, dwc_ep);
  54265. + is_last = 1;
  54266. + }
  54267. + } else {
  54268. + set_current_pkt_info(core_if, dwc_ep);
  54269. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  54270. + is_last = 1;
  54271. + dwc_ep->cur_pkt = 0;
  54272. + dwc_ep->proc_buf_num =
  54273. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54274. + if (dwc_ep->proc_buf_num) {
  54275. + dwc_ep->cur_pkt_addr =
  54276. + dwc_ep->xfer_buff1;
  54277. + dwc_ep->cur_pkt_dma_addr =
  54278. + dwc_ep->dma_addr1;
  54279. + } else {
  54280. + dwc_ep->cur_pkt_addr =
  54281. + dwc_ep->xfer_buff0;
  54282. + dwc_ep->cur_pkt_dma_addr =
  54283. + dwc_ep->dma_addr0;
  54284. + }
  54285. +
  54286. + }
  54287. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  54288. + dwc_ep);
  54289. + }
  54290. + }
  54291. + } else {
  54292. + set_current_pkt_info(core_if, dwc_ep);
  54293. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  54294. + is_last = 1;
  54295. + dwc_ep->cur_pkt = 0;
  54296. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54297. + if (dwc_ep->proc_buf_num) {
  54298. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  54299. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  54300. + } else {
  54301. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  54302. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  54303. + }
  54304. +
  54305. + }
  54306. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  54307. + }
  54308. + if (is_last)
  54309. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  54310. +}
  54311. +#endif /* DWC_EN_ISOC */
  54312. +
  54313. +/**
  54314. + * This function handle BNA interrupt for Non Isochronous EPs
  54315. + *
  54316. + */
  54317. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  54318. +{
  54319. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  54320. + volatile uint32_t *addr;
  54321. + depctl_data_t depctl = {.d32 = 0 };
  54322. + dwc_otg_pcd_t *pcd = ep->pcd;
  54323. + dwc_otg_dev_dma_desc_t *dma_desc;
  54324. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  54325. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  54326. + int i, start;
  54327. +
  54328. + if (!dwc_ep->desc_cnt)
  54329. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  54330. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  54331. +
  54332. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  54333. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  54334. + uint32_t doepdma;
  54335. + dwc_otg_dev_out_ep_regs_t *out_regs =
  54336. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  54337. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  54338. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  54339. + dma_desc = &(dwc_ep->desc_addr[start]);
  54340. + } else {
  54341. + start = 0;
  54342. + dma_desc = dwc_ep->desc_addr;
  54343. + }
  54344. +
  54345. +
  54346. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  54347. + sts.d32 = dma_desc->status.d32;
  54348. + sts.b.bs = BS_HOST_READY;
  54349. + dma_desc->status.d32 = sts.d32;
  54350. + }
  54351. +
  54352. + if (dwc_ep->is_in == 0) {
  54353. + addr =
  54354. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  54355. + doepctl;
  54356. + } else {
  54357. + addr =
  54358. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  54359. + }
  54360. + depctl.b.epena = 1;
  54361. + depctl.b.cnak = 1;
  54362. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  54363. +}
  54364. +
  54365. +/**
  54366. + * This function handles EP0 Control transfers.
  54367. + *
  54368. + * The state of the control transfers are tracked in
  54369. + * <code>ep0state</code>.
  54370. + */
  54371. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  54372. +{
  54373. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54374. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  54375. + dev_dma_desc_sts_t desc_sts;
  54376. + deptsiz0_data_t deptsiz;
  54377. + uint32_t byte_count;
  54378. +
  54379. +#ifdef DEBUG_EP0
  54380. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  54381. + print_ep0_state(pcd);
  54382. +#endif
  54383. +
  54384. +// DWC_PRINTF("HANDLE EP0\n");
  54385. +
  54386. + switch (pcd->ep0state) {
  54387. + case EP0_DISCONNECT:
  54388. + break;
  54389. +
  54390. + case EP0_IDLE:
  54391. + pcd->request_config = 0;
  54392. +
  54393. + pcd_setup(pcd);
  54394. + break;
  54395. +
  54396. + case EP0_IN_DATA_PHASE:
  54397. +#ifdef DEBUG_EP0
  54398. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  54399. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  54400. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  54401. +#endif
  54402. +
  54403. + if (core_if->dma_enable != 0) {
  54404. + /*
  54405. + * For EP0 we can only program 1 packet at a time so we
  54406. + * need to do the make calculations after each complete.
  54407. + * Call write_packet to make the calculations, as in
  54408. + * slave mode, and use those values to determine if we
  54409. + * can complete.
  54410. + */
  54411. + if (core_if->dma_desc_enable == 0) {
  54412. + deptsiz.d32 =
  54413. + DWC_READ_REG32(&core_if->
  54414. + dev_if->in_ep_regs[0]->
  54415. + dieptsiz);
  54416. + byte_count =
  54417. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  54418. + } else {
  54419. + desc_sts =
  54420. + core_if->dev_if->in_desc_addr->status;
  54421. + byte_count =
  54422. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  54423. + }
  54424. + ep0->dwc_ep.xfer_count += byte_count;
  54425. + ep0->dwc_ep.xfer_buff += byte_count;
  54426. + ep0->dwc_ep.dma_addr += byte_count;
  54427. + }
  54428. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  54429. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54430. + &ep0->dwc_ep);
  54431. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  54432. + } else if (ep0->dwc_ep.sent_zlp) {
  54433. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54434. + &ep0->dwc_ep);
  54435. + ep0->dwc_ep.sent_zlp = 0;
  54436. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  54437. + } else {
  54438. + ep0_complete_request(ep0);
  54439. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  54440. + }
  54441. + break;
  54442. + case EP0_OUT_DATA_PHASE:
  54443. +#ifdef DEBUG_EP0
  54444. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  54445. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  54446. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  54447. +#endif
  54448. + if (core_if->dma_enable != 0) {
  54449. + if (core_if->dma_desc_enable == 0) {
  54450. + deptsiz.d32 =
  54451. + DWC_READ_REG32(&core_if->
  54452. + dev_if->out_ep_regs[0]->
  54453. + doeptsiz);
  54454. + byte_count =
  54455. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  54456. + } else {
  54457. + desc_sts =
  54458. + core_if->dev_if->out_desc_addr->status;
  54459. + byte_count =
  54460. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  54461. + }
  54462. + ep0->dwc_ep.xfer_count += byte_count;
  54463. + ep0->dwc_ep.xfer_buff += byte_count;
  54464. + ep0->dwc_ep.dma_addr += byte_count;
  54465. + }
  54466. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  54467. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54468. + &ep0->dwc_ep);
  54469. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  54470. + } else if (ep0->dwc_ep.sent_zlp) {
  54471. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54472. + &ep0->dwc_ep);
  54473. + ep0->dwc_ep.sent_zlp = 0;
  54474. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  54475. + } else {
  54476. + ep0_complete_request(ep0);
  54477. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  54478. + }
  54479. + break;
  54480. +
  54481. + case EP0_IN_STATUS_PHASE:
  54482. + case EP0_OUT_STATUS_PHASE:
  54483. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  54484. + ep0_complete_request(ep0);
  54485. + pcd->ep0state = EP0_IDLE;
  54486. + ep0->stopped = 1;
  54487. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  54488. +
  54489. + /* Prepare for more SETUP Packets */
  54490. + if (core_if->dma_enable) {
  54491. + ep0_out_start(core_if, pcd);
  54492. + }
  54493. + break;
  54494. +
  54495. + case EP0_STALL:
  54496. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  54497. + break;
  54498. + }
  54499. +#ifdef DEBUG_EP0
  54500. + print_ep0_state(pcd);
  54501. +#endif
  54502. +}
  54503. +
  54504. +/**
  54505. + * Restart transfer
  54506. + */
  54507. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  54508. +{
  54509. + dwc_otg_core_if_t *core_if;
  54510. + dwc_otg_dev_if_t *dev_if;
  54511. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54512. + dwc_otg_pcd_ep_t *ep;
  54513. +
  54514. + ep = get_in_ep(pcd, epnum);
  54515. +
  54516. +#ifdef DWC_EN_ISOC
  54517. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  54518. + return;
  54519. + }
  54520. +#endif /* DWC_EN_ISOC */
  54521. +
  54522. + core_if = GET_CORE_IF(pcd);
  54523. + dev_if = core_if->dev_if;
  54524. +
  54525. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  54526. +
  54527. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  54528. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  54529. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  54530. + /*
  54531. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  54532. + */
  54533. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  54534. + ep->dwc_ep.start_xfer_buff != 0) {
  54535. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  54536. + ep->dwc_ep.xfer_count = 0;
  54537. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  54538. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  54539. + } else {
  54540. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  54541. + /* convert packet size to dwords. */
  54542. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  54543. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  54544. + }
  54545. + ep->stopped = 0;
  54546. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  54547. + "xfer_len=%0x stopped=%d\n",
  54548. + ep->dwc_ep.xfer_buff,
  54549. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  54550. + ep->stopped);
  54551. + if (epnum == 0) {
  54552. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  54553. + } else {
  54554. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  54555. + }
  54556. + }
  54557. +}
  54558. +
  54559. +/*
  54560. + * This function create new nextep sequnce based on Learn Queue.
  54561. + *
  54562. + * @param core_if Programming view of DWC_otg controller
  54563. + */
  54564. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  54565. +{
  54566. + dwc_otg_device_global_regs_t *dev_global_regs =
  54567. + core_if->dev_if->dev_global_regs;
  54568. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  54569. + /* Number of Token Queue Registers */
  54570. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  54571. + dtknq1_data_t dtknqr1;
  54572. + uint32_t in_tkn_epnums[4];
  54573. + uint8_t seqnum[MAX_EPS_CHANNELS];
  54574. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  54575. + grstctl_t resetctl = {.d32 = 0 };
  54576. + uint8_t temp;
  54577. + int ndx = 0;
  54578. + int start = 0;
  54579. + int end = 0;
  54580. + int sort_done = 0;
  54581. + int i = 0;
  54582. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  54583. +
  54584. +
  54585. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  54586. +
  54587. + /* Read the DTKNQ Registers */
  54588. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  54589. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  54590. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  54591. + in_tkn_epnums[i]);
  54592. + if (addr == &dev_global_regs->dvbusdis) {
  54593. + addr = &dev_global_regs->dtknqr3_dthrctl;
  54594. + } else {
  54595. + ++addr;
  54596. + }
  54597. +
  54598. + }
  54599. +
  54600. + /* Copy the DTKNQR1 data to the bit field. */
  54601. + dtknqr1.d32 = in_tkn_epnums[0];
  54602. + if (dtknqr1.b.wrap_bit) {
  54603. + ndx = dtknqr1.b.intknwptr;
  54604. + end = ndx -1;
  54605. + if (end < 0)
  54606. + end = TOKEN_Q_DEPTH -1;
  54607. + } else {
  54608. + ndx = 0;
  54609. + end = dtknqr1.b.intknwptr -1;
  54610. + if (end < 0)
  54611. + end = 0;
  54612. + }
  54613. + start = ndx;
  54614. +
  54615. + /* Fill seqnum[] by initial values: EP number + 31 */
  54616. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  54617. + seqnum[i] = i +31;
  54618. + }
  54619. +
  54620. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  54621. + for (i=0; i < 6; i++)
  54622. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  54623. +
  54624. + if (TOKEN_Q_DEPTH > 6) {
  54625. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54626. + for (i=6; i < 14; i++)
  54627. + intkn_seq[i] =
  54628. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  54629. + }
  54630. +
  54631. + if (TOKEN_Q_DEPTH > 14) {
  54632. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54633. + for (i=14; i < 22; i++)
  54634. + intkn_seq[i] =
  54635. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  54636. + }
  54637. +
  54638. + if (TOKEN_Q_DEPTH > 22) {
  54639. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54640. + for (i=22; i < 30; i++)
  54641. + intkn_seq[i] =
  54642. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  54643. + }
  54644. +
  54645. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  54646. + start, end);
  54647. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  54648. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  54649. +
  54650. + /* Update seqnum based on intkn_seq[] */
  54651. + i = 0;
  54652. + do {
  54653. + seqnum[intkn_seq[ndx]] = i;
  54654. + ndx++;
  54655. + i++;
  54656. + if (ndx == TOKEN_Q_DEPTH)
  54657. + ndx = 0;
  54658. + } while ( i < TOKEN_Q_DEPTH );
  54659. +
  54660. + /* Mark non active EP's in seqnum[] by 0xff */
  54661. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  54662. + if (core_if->nextep_seq[i] == 0xff )
  54663. + seqnum[i] = 0xff;
  54664. + }
  54665. +
  54666. + /* Sort seqnum[] */
  54667. + sort_done = 0;
  54668. + while (!sort_done) {
  54669. + sort_done = 1;
  54670. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  54671. + if (seqnum[i] > seqnum[i+1]) {
  54672. + temp = seqnum[i];
  54673. + seqnum[i] = seqnum[i+1];
  54674. + seqnum[i+1] = temp;
  54675. + sort_done = 0;
  54676. + }
  54677. + }
  54678. + }
  54679. +
  54680. + ndx = start + seqnum[0];
  54681. + if (ndx >= TOKEN_Q_DEPTH)
  54682. + ndx = ndx % TOKEN_Q_DEPTH;
  54683. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  54684. +
  54685. + /* Update seqnum[] by EP numbers */
  54686. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  54687. + ndx = start + i;
  54688. + if (seqnum[i] < 31) {
  54689. + ndx = start + seqnum[i];
  54690. + if (ndx >= TOKEN_Q_DEPTH)
  54691. + ndx = ndx % TOKEN_Q_DEPTH;
  54692. + seqnum[i] = intkn_seq[ndx];
  54693. + } else {
  54694. + if (seqnum[i] < 0xff) {
  54695. + seqnum[i] = seqnum[i] - 31;
  54696. + } else {
  54697. + break;
  54698. + }
  54699. + }
  54700. + }
  54701. +
  54702. + /* Update nextep_seq[] based on seqnum[] */
  54703. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  54704. + if (seqnum[i] != 0xff) {
  54705. + if (seqnum[i+1] != 0xff) {
  54706. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  54707. + } else {
  54708. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  54709. + break;
  54710. + }
  54711. + } else {
  54712. + break;
  54713. + }
  54714. + }
  54715. +
  54716. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  54717. + __func__, core_if->first_in_nextep_seq);
  54718. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  54719. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  54720. + }
  54721. +
  54722. + /* Flush the Learning Queue */
  54723. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  54724. + resetctl.b.intknqflsh = 1;
  54725. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  54726. +
  54727. +
  54728. +}
  54729. +
  54730. +/**
  54731. + * handle the IN EP disable interrupt.
  54732. + */
  54733. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  54734. + const uint32_t epnum)
  54735. +{
  54736. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54737. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54738. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54739. + dctl_data_t dctl = {.d32 = 0 };
  54740. + dwc_otg_pcd_ep_t *ep;
  54741. + dwc_ep_t *dwc_ep;
  54742. + gintmsk_data_t gintmsk_data;
  54743. + depctl_data_t depctl;
  54744. + uint32_t diepdma;
  54745. + uint32_t remain_to_transfer = 0;
  54746. + uint8_t i;
  54747. + uint32_t xfer_size;
  54748. +
  54749. + ep = get_in_ep(pcd, epnum);
  54750. + dwc_ep = &ep->dwc_ep;
  54751. +
  54752. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54753. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  54754. + complete_ep(ep);
  54755. + return;
  54756. + }
  54757. +
  54758. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  54759. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  54760. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  54761. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  54762. +
  54763. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  54764. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  54765. +
  54766. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  54767. + if (ep->stopped) {
  54768. + if (core_if->en_multiple_tx_fifo)
  54769. + /* Flush the Tx FIFO */
  54770. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  54771. + /* Clear the Global IN NP NAK */
  54772. + dctl.d32 = 0;
  54773. + dctl.b.cgnpinnak = 1;
  54774. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54775. + /* Restart the transaction */
  54776. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  54777. + restart_transfer(pcd, epnum);
  54778. + }
  54779. + } else {
  54780. + /* Restart the transaction */
  54781. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  54782. + restart_transfer(pcd, epnum);
  54783. + }
  54784. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  54785. + }
  54786. + return;
  54787. + }
  54788. +
  54789. + if (core_if->start_predict > 2) { // NP IN EP
  54790. + core_if->start_predict--;
  54791. + return;
  54792. + }
  54793. +
  54794. + core_if->start_predict--;
  54795. +
  54796. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  54797. +
  54798. + predict_nextep_seq(core_if);
  54799. +
  54800. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  54801. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  54802. + depctl.d32 =
  54803. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54804. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  54805. + depctl.b.nextep = core_if->nextep_seq[i];
  54806. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  54807. + }
  54808. + }
  54809. + /* Flush Shared NP TxFIFO */
  54810. + dwc_otg_flush_tx_fifo(core_if, 0);
  54811. + /* Rewind buffers */
  54812. + if (!core_if->dma_desc_enable) {
  54813. + i = core_if->first_in_nextep_seq;
  54814. + do {
  54815. + ep = get_in_ep(pcd, i);
  54816. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  54817. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  54818. + if (xfer_size > ep->dwc_ep.maxxfer)
  54819. + xfer_size = ep->dwc_ep.maxxfer;
  54820. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54821. + if (dieptsiz.b.pktcnt != 0) {
  54822. + if (xfer_size == 0) {
  54823. + remain_to_transfer = 0;
  54824. + } else {
  54825. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  54826. + remain_to_transfer =
  54827. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  54828. + } else {
  54829. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  54830. + + (xfer_size % ep->dwc_ep.maxpacket);
  54831. + }
  54832. + }
  54833. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  54834. + dieptsiz.b.xfersize = remain_to_transfer;
  54835. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  54836. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  54837. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  54838. + }
  54839. + i = core_if->nextep_seq[i];
  54840. + } while (i != core_if->first_in_nextep_seq);
  54841. + } else { // dma_desc_enable
  54842. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  54843. + }
  54844. +
  54845. + /* Restart transfers in predicted sequences */
  54846. + i = core_if->first_in_nextep_seq;
  54847. + do {
  54848. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  54849. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54850. + if (dieptsiz.b.pktcnt != 0) {
  54851. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54852. + depctl.b.epena = 1;
  54853. + depctl.b.cnak = 1;
  54854. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  54855. + }
  54856. + i = core_if->nextep_seq[i];
  54857. + } while (i != core_if->first_in_nextep_seq);
  54858. +
  54859. + /* Clear the global non-periodic IN NAK handshake */
  54860. + dctl.d32 = 0;
  54861. + dctl.b.cgnpinnak = 1;
  54862. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54863. +
  54864. + /* Unmask EP Mismatch interrupt */
  54865. + gintmsk_data.d32 = 0;
  54866. + gintmsk_data.b.epmismatch = 1;
  54867. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  54868. +
  54869. + core_if->start_predict = 0;
  54870. +
  54871. + }
  54872. +}
  54873. +
  54874. +/**
  54875. + * Handler for the IN EP timeout handshake interrupt.
  54876. + */
  54877. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  54878. + const uint32_t epnum)
  54879. +{
  54880. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54881. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54882. +
  54883. +#ifdef DEBUG
  54884. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54885. + uint32_t num = 0;
  54886. +#endif
  54887. + dctl_data_t dctl = {.d32 = 0 };
  54888. + dwc_otg_pcd_ep_t *ep;
  54889. +
  54890. + gintmsk_data_t intr_mask = {.d32 = 0 };
  54891. +
  54892. + ep = get_in_ep(pcd, epnum);
  54893. +
  54894. + /* Disable the NP Tx Fifo Empty Interrrupt */
  54895. + if (!core_if->dma_enable) {
  54896. + intr_mask.b.nptxfempty = 1;
  54897. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  54898. + intr_mask.d32, 0);
  54899. + }
  54900. + /** @todo NGS Check EP type.
  54901. + * Implement for Periodic EPs */
  54902. + /*
  54903. + * Non-periodic EP
  54904. + */
  54905. + /* Enable the Global IN NAK Effective Interrupt */
  54906. + intr_mask.b.ginnakeff = 1;
  54907. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  54908. +
  54909. + /* Set Global IN NAK */
  54910. + dctl.b.sgnpinnak = 1;
  54911. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54912. +
  54913. + ep->stopped = 1;
  54914. +
  54915. +#ifdef DEBUG
  54916. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  54917. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  54918. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  54919. +#endif
  54920. +
  54921. +#ifdef DISABLE_PERIODIC_EP
  54922. + /*
  54923. + * Set the NAK bit for this EP to
  54924. + * start the disable process.
  54925. + */
  54926. + diepctl.d32 = 0;
  54927. + diepctl.b.snak = 1;
  54928. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  54929. + diepctl.d32);
  54930. + ep->disabling = 1;
  54931. + ep->stopped = 1;
  54932. +#endif
  54933. +}
  54934. +
  54935. +/**
  54936. + * Handler for the IN EP NAK interrupt.
  54937. + */
  54938. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  54939. + const uint32_t epnum)
  54940. +{
  54941. + /** @todo implement ISR */
  54942. + dwc_otg_core_if_t *core_if;
  54943. + diepmsk_data_t intr_mask = {.d32 = 0 };
  54944. +
  54945. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  54946. + core_if = GET_CORE_IF(pcd);
  54947. + intr_mask.b.nak = 1;
  54948. +
  54949. + if (core_if->multiproc_int_enable) {
  54950. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54951. + diepeachintmsk[epnum], intr_mask.d32, 0);
  54952. + } else {
  54953. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  54954. + intr_mask.d32, 0);
  54955. + }
  54956. +
  54957. + return 1;
  54958. +}
  54959. +
  54960. +/**
  54961. + * Handler for the OUT EP Babble interrupt.
  54962. + */
  54963. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  54964. + const uint32_t epnum)
  54965. +{
  54966. + /** @todo implement ISR */
  54967. + dwc_otg_core_if_t *core_if;
  54968. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54969. +
  54970. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  54971. + "OUT EP Babble");
  54972. + core_if = GET_CORE_IF(pcd);
  54973. + intr_mask.b.babble = 1;
  54974. +
  54975. + if (core_if->multiproc_int_enable) {
  54976. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54977. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54978. + } else {
  54979. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54980. + intr_mask.d32, 0);
  54981. + }
  54982. +
  54983. + return 1;
  54984. +}
  54985. +
  54986. +/**
  54987. + * Handler for the OUT EP NAK interrupt.
  54988. + */
  54989. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  54990. + const uint32_t epnum)
  54991. +{
  54992. + /** @todo implement ISR */
  54993. + dwc_otg_core_if_t *core_if;
  54994. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54995. +
  54996. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  54997. + core_if = GET_CORE_IF(pcd);
  54998. + intr_mask.b.nak = 1;
  54999. +
  55000. + if (core_if->multiproc_int_enable) {
  55001. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  55002. + doepeachintmsk[epnum], intr_mask.d32, 0);
  55003. + } else {
  55004. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  55005. + intr_mask.d32, 0);
  55006. + }
  55007. +
  55008. + return 1;
  55009. +}
  55010. +
  55011. +/**
  55012. + * Handler for the OUT EP NYET interrupt.
  55013. + */
  55014. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  55015. + const uint32_t epnum)
  55016. +{
  55017. + /** @todo implement ISR */
  55018. + dwc_otg_core_if_t *core_if;
  55019. + doepmsk_data_t intr_mask = {.d32 = 0 };
  55020. +
  55021. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  55022. + core_if = GET_CORE_IF(pcd);
  55023. + intr_mask.b.nyet = 1;
  55024. +
  55025. + if (core_if->multiproc_int_enable) {
  55026. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  55027. + doepeachintmsk[epnum], intr_mask.d32, 0);
  55028. + } else {
  55029. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  55030. + intr_mask.d32, 0);
  55031. + }
  55032. +
  55033. + return 1;
  55034. +}
  55035. +
  55036. +/**
  55037. + * This interrupt indicates that an IN EP has a pending Interrupt.
  55038. + * The sequence for handling the IN EP interrupt is shown below:
  55039. + * -# Read the Device All Endpoint Interrupt register
  55040. + * -# Repeat the following for each IN EP interrupt bit set (from
  55041. + * LSB to MSB).
  55042. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  55043. + * -# If "Transfer Complete" call the request complete function
  55044. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  55045. + * -# If "AHB Error Interrupt" log error
  55046. + * -# If "Time-out Handshake" log error
  55047. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  55048. + * FIFO.
  55049. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  55050. + * Mismatch Interrupt)
  55051. + */
  55052. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  55053. +{
  55054. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  55055. +do { \
  55056. + diepint_data_t diepint = {.d32=0}; \
  55057. + diepint.b.__intr = 1; \
  55058. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  55059. + diepint.d32); \
  55060. +} while (0)
  55061. +
  55062. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55063. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  55064. + diepint_data_t diepint = {.d32 = 0 };
  55065. + depctl_data_t depctl = {.d32 = 0 };
  55066. + uint32_t ep_intr;
  55067. + uint32_t epnum = 0;
  55068. + dwc_otg_pcd_ep_t *ep;
  55069. + dwc_ep_t *dwc_ep;
  55070. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55071. +
  55072. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  55073. +
  55074. + /* Read in the device interrupt bits */
  55075. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  55076. +
  55077. + /* Service the Device IN interrupts for each endpoint */
  55078. + while (ep_intr) {
  55079. + if (ep_intr & 0x1) {
  55080. + uint32_t empty_msk;
  55081. + /* Get EP pointer */
  55082. + ep = get_in_ep(pcd, epnum);
  55083. + dwc_ep = &ep->dwc_ep;
  55084. +
  55085. + depctl.d32 =
  55086. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  55087. + empty_msk =
  55088. + DWC_READ_REG32(&dev_if->
  55089. + dev_global_regs->dtknqr4_fifoemptymsk);
  55090. +
  55091. + DWC_DEBUGPL(DBG_PCDV,
  55092. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  55093. + epnum, empty_msk, depctl.d32);
  55094. +
  55095. + DWC_DEBUGPL(DBG_PCD,
  55096. + "EP%d-%s: type=%d, mps=%d\n",
  55097. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  55098. + dwc_ep->type, dwc_ep->maxpacket);
  55099. +
  55100. + diepint.d32 =
  55101. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  55102. +
  55103. + DWC_DEBUGPL(DBG_PCDV,
  55104. + "EP %d Interrupt Register - 0x%x\n", epnum,
  55105. + diepint.d32);
  55106. + /* Transfer complete */
  55107. + if (diepint.b.xfercompl) {
  55108. + /* Disable the NP Tx FIFO Empty
  55109. + * Interrupt */
  55110. + if (core_if->en_multiple_tx_fifo == 0) {
  55111. + intr_mask.b.nptxfempty = 1;
  55112. + DWC_MODIFY_REG32
  55113. + (&core_if->core_global_regs->gintmsk,
  55114. + intr_mask.d32, 0);
  55115. + } else {
  55116. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  55117. + uint32_t fifoemptymsk =
  55118. + 0x1 << dwc_ep->num;
  55119. + DWC_MODIFY_REG32(&core_if->
  55120. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  55121. + fifoemptymsk, 0);
  55122. + }
  55123. + /* Clear the bit in DIEPINTn for this interrupt */
  55124. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  55125. +
  55126. + /* Complete the transfer */
  55127. + if (epnum == 0) {
  55128. + handle_ep0(pcd);
  55129. + }
  55130. +#ifdef DWC_EN_ISOC
  55131. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55132. + if (!ep->stopped)
  55133. + complete_iso_ep(pcd, ep);
  55134. + }
  55135. +#endif /* DWC_EN_ISOC */
  55136. +#ifdef DWC_UTE_PER_IO
  55137. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55138. + if (!ep->stopped)
  55139. + complete_xiso_ep(ep);
  55140. + }
  55141. +#endif /* DWC_UTE_PER_IO */
  55142. + else {
  55143. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  55144. + dwc_ep->bInterval > 1) {
  55145. + dwc_ep->frame_num += dwc_ep->bInterval;
  55146. + if (dwc_ep->frame_num > 0x3FFF)
  55147. + {
  55148. + dwc_ep->frm_overrun = 1;
  55149. + dwc_ep->frame_num &= 0x3FFF;
  55150. + } else
  55151. + dwc_ep->frm_overrun = 0;
  55152. + }
  55153. + complete_ep(ep);
  55154. + if(diepint.b.nak)
  55155. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  55156. + }
  55157. + }
  55158. + /* Endpoint disable */
  55159. + if (diepint.b.epdisabled) {
  55160. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  55161. + epnum);
  55162. + handle_in_ep_disable_intr(pcd, epnum);
  55163. +
  55164. + /* Clear the bit in DIEPINTn for this interrupt */
  55165. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  55166. + }
  55167. + /* AHB Error */
  55168. + if (diepint.b.ahberr) {
  55169. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  55170. + /* Clear the bit in DIEPINTn for this interrupt */
  55171. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  55172. + }
  55173. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  55174. + if (diepint.b.timeout) {
  55175. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  55176. + handle_in_ep_timeout_intr(pcd, epnum);
  55177. +
  55178. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  55179. + }
  55180. + /** IN Token received with TxF Empty */
  55181. + if (diepint.b.intktxfemp) {
  55182. + DWC_DEBUGPL(DBG_ANY,
  55183. + "EP%d IN TKN TxFifo Empty\n",
  55184. + epnum);
  55185. + if (!ep->stopped && epnum != 0) {
  55186. +
  55187. + diepmsk_data_t diepmsk = {.d32 = 0 };
  55188. + diepmsk.b.intktxfemp = 1;
  55189. +
  55190. + if (core_if->multiproc_int_enable) {
  55191. + DWC_MODIFY_REG32
  55192. + (&dev_if->dev_global_regs->diepeachintmsk
  55193. + [epnum], diepmsk.d32, 0);
  55194. + } else {
  55195. + DWC_MODIFY_REG32
  55196. + (&dev_if->dev_global_regs->diepmsk,
  55197. + diepmsk.d32, 0);
  55198. + }
  55199. + } else if (core_if->dma_desc_enable
  55200. + && epnum == 0
  55201. + && pcd->ep0state ==
  55202. + EP0_OUT_STATUS_PHASE) {
  55203. + // EP0 IN set STALL
  55204. + depctl.d32 =
  55205. + DWC_READ_REG32(&dev_if->in_ep_regs
  55206. + [epnum]->diepctl);
  55207. +
  55208. + /* set the disable and stall bits */
  55209. + if (depctl.b.epena) {
  55210. + depctl.b.epdis = 1;
  55211. + }
  55212. + depctl.b.stall = 1;
  55213. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  55214. + [epnum]->diepctl,
  55215. + depctl.d32);
  55216. + }
  55217. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  55218. + }
  55219. + /** IN Token Received with EP mismatch */
  55220. + if (diepint.b.intknepmis) {
  55221. + DWC_DEBUGPL(DBG_ANY,
  55222. + "EP%d IN TKN EP Mismatch\n", epnum);
  55223. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  55224. + }
  55225. + /** IN Endpoint NAK Effective */
  55226. + if (diepint.b.inepnakeff) {
  55227. + DWC_DEBUGPL(DBG_ANY,
  55228. + "EP%d IN EP NAK Effective\n",
  55229. + epnum);
  55230. + /* Periodic EP */
  55231. + if (ep->disabling) {
  55232. + depctl.d32 = 0;
  55233. + depctl.b.snak = 1;
  55234. + depctl.b.epdis = 1;
  55235. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  55236. + [epnum]->diepctl,
  55237. + depctl.d32,
  55238. + depctl.d32);
  55239. + }
  55240. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  55241. +
  55242. + }
  55243. +
  55244. + /** IN EP Tx FIFO Empty Intr */
  55245. + if (diepint.b.emptyintr) {
  55246. + DWC_DEBUGPL(DBG_ANY,
  55247. + "EP%d Tx FIFO Empty Intr \n",
  55248. + epnum);
  55249. + write_empty_tx_fifo(pcd, epnum);
  55250. +
  55251. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  55252. +
  55253. + }
  55254. +
  55255. + /** IN EP BNA Intr */
  55256. + if (diepint.b.bna) {
  55257. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  55258. + if (core_if->dma_desc_enable) {
  55259. +#ifdef DWC_EN_ISOC
  55260. + if (dwc_ep->type ==
  55261. + DWC_OTG_EP_TYPE_ISOC) {
  55262. + /*
  55263. + * This checking is performed to prevent first "false" BNA
  55264. + * handling occuring right after reconnect
  55265. + */
  55266. + if (dwc_ep->next_frame !=
  55267. + 0xffffffff)
  55268. + dwc_otg_pcd_handle_iso_bna(ep);
  55269. + } else
  55270. +#endif /* DWC_EN_ISOC */
  55271. + {
  55272. + dwc_otg_pcd_handle_noniso_bna(ep);
  55273. + }
  55274. + }
  55275. + }
  55276. + /* NAK Interrutp */
  55277. + if (diepint.b.nak) {
  55278. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  55279. + epnum);
  55280. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55281. + depctl_data_t depctl;
  55282. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  55283. + ep->dwc_ep.frame_num = core_if->frame_num;
  55284. + if (ep->dwc_ep.bInterval > 1) {
  55285. + depctl.d32 = 0;
  55286. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  55287. + if (ep->dwc_ep.frame_num & 0x1) {
  55288. + depctl.b.setd1pid = 1;
  55289. + depctl.b.setd0pid = 0;
  55290. + } else {
  55291. + depctl.b.setd0pid = 1;
  55292. + depctl.b.setd1pid = 0;
  55293. + }
  55294. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  55295. + }
  55296. + start_next_request(ep);
  55297. + }
  55298. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  55299. + if (dwc_ep->frame_num > 0x3FFF) {
  55300. + dwc_ep->frm_overrun = 1;
  55301. + dwc_ep->frame_num &= 0x3FFF;
  55302. + } else
  55303. + dwc_ep->frm_overrun = 0;
  55304. + }
  55305. +
  55306. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  55307. + }
  55308. + }
  55309. + epnum++;
  55310. + ep_intr >>= 1;
  55311. + }
  55312. +
  55313. + return 1;
  55314. +#undef CLEAR_IN_EP_INTR
  55315. +}
  55316. +
  55317. +/**
  55318. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  55319. + * The sequence for handling the OUT EP interrupt is shown below:
  55320. + * -# Read the Device All Endpoint Interrupt register
  55321. + * -# Repeat the following for each OUT EP interrupt bit set (from
  55322. + * LSB to MSB).
  55323. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  55324. + * -# If "Transfer Complete" call the request complete function
  55325. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  55326. + * -# If "AHB Error Interrupt" log error
  55327. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  55328. + * Command Processing)
  55329. + */
  55330. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  55331. +{
  55332. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  55333. +do { \
  55334. + doepint_data_t doepint = {.d32=0}; \
  55335. + doepint.b.__intr = 1; \
  55336. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  55337. + doepint.d32); \
  55338. +} while (0)
  55339. +
  55340. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55341. + uint32_t ep_intr;
  55342. + doepint_data_t doepint = {.d32 = 0 };
  55343. + uint32_t epnum = 0;
  55344. + dwc_otg_pcd_ep_t *ep;
  55345. + dwc_ep_t *dwc_ep;
  55346. + dctl_data_t dctl = {.d32 = 0 };
  55347. + gintmsk_data_t gintmsk = {.d32 = 0 };
  55348. +
  55349. +
  55350. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  55351. +
  55352. + /* Read in the device interrupt bits */
  55353. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  55354. +
  55355. + while (ep_intr) {
  55356. + if (ep_intr & 0x1) {
  55357. + /* Get EP pointer */
  55358. + ep = get_out_ep(pcd, epnum);
  55359. + dwc_ep = &ep->dwc_ep;
  55360. +
  55361. +#ifdef VERBOSE
  55362. + DWC_DEBUGPL(DBG_PCDV,
  55363. + "EP%d-%s: type=%d, mps=%d\n",
  55364. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  55365. + dwc_ep->type, dwc_ep->maxpacket);
  55366. +#endif
  55367. + doepint.d32 =
  55368. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  55369. + /* Moved this interrupt upper due to core deffect of asserting
  55370. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  55371. + if (doepint.b.stsphsercvd) {
  55372. + deptsiz0_data_t deptsiz;
  55373. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  55374. + deptsiz.d32 =
  55375. + DWC_READ_REG32(&core_if->dev_if->
  55376. + out_ep_regs[0]->doeptsiz);
  55377. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  55378. + && core_if->dma_enable
  55379. + && core_if->dma_desc_enable == 0
  55380. + && doepint.b.xfercompl
  55381. + && deptsiz.b.xfersize == 24) {
  55382. + CLEAR_OUT_EP_INTR(core_if, epnum,
  55383. + xfercompl);
  55384. + doepint.b.xfercompl = 0;
  55385. + ep0_out_start(core_if, pcd);
  55386. + }
  55387. + if ((core_if->dma_desc_enable) ||
  55388. + (core_if->dma_enable
  55389. + && core_if->snpsid >=
  55390. + OTG_CORE_REV_3_00a)) {
  55391. + do_setup_in_status_phase(pcd);
  55392. + }
  55393. + }
  55394. + /* Transfer complete */
  55395. + if (doepint.b.xfercompl) {
  55396. +
  55397. + if (epnum == 0) {
  55398. + /* Clear the bit in DOEPINTn for this interrupt */
  55399. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  55400. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  55401. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  55402. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  55403. + doepint.d32);
  55404. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  55405. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  55406. +
  55407. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  55408. + && core_if->dma_enable == 0) {
  55409. + doepint_data_t doepint;
  55410. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55411. + out_ep_regs[0]->doepint);
  55412. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  55413. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55414. + goto exit_xfercompl;
  55415. + }
  55416. + }
  55417. + /* In case of DDMA look at SR bit to go to the Data Stage */
  55418. + if (core_if->dma_desc_enable) {
  55419. + dev_dma_desc_sts_t status = {.d32 = 0};
  55420. + if (pcd->ep0state == EP0_IDLE) {
  55421. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  55422. + dev_if->setup_desc_index]->status.d32;
  55423. + if(pcd->data_terminated) {
  55424. + pcd->data_terminated = 0;
  55425. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  55426. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  55427. + }
  55428. + if (status.b.sr) {
  55429. + if (doepint.b.setup) {
  55430. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  55431. + /* Already started data stage, clear setup */
  55432. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55433. + doepint.b.setup = 0;
  55434. + handle_ep0(pcd);
  55435. + /* Prepare for more setup packets */
  55436. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  55437. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  55438. + ep0_out_start(core_if, pcd);
  55439. + }
  55440. +
  55441. + goto exit_xfercompl;
  55442. + } else {
  55443. + /* Prepare for more setup packets */
  55444. + DWC_DEBUGPL(DBG_PCDV,
  55445. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  55446. + ep0_out_start(core_if, pcd);
  55447. + }
  55448. + }
  55449. + } else {
  55450. + dwc_otg_pcd_request_t *req;
  55451. + dev_dma_desc_sts_t status = {.d32 = 0};
  55452. + diepint_data_t diepint0;
  55453. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55454. + in_ep_regs[0]->diepint);
  55455. +
  55456. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  55457. + DWC_ERROR("EP0 is stalled/disconnected\n");
  55458. + }
  55459. +
  55460. + /* Clear IN xfercompl if set */
  55461. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  55462. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  55463. + DWC_WRITE_REG32(&core_if->dev_if->
  55464. + in_ep_regs[0]->diepint, diepint0.d32);
  55465. + }
  55466. +
  55467. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  55468. + dev_if->setup_desc_index]->status.d32;
  55469. +
  55470. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  55471. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  55472. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  55473. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  55474. + status.d32 = core_if->dev_if->
  55475. + out_desc_addr->status.d32;
  55476. +
  55477. + if (status.b.sr) {
  55478. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55479. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  55480. + } else {
  55481. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  55482. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55483. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  55484. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  55485. + /* Read arrived setup packet from req->buf */
  55486. + dwc_memcpy(&pcd->setup_pkt->req,
  55487. + req->buf + ep->dwc_ep.xfer_count, 8);
  55488. + }
  55489. + req->actual = ep->dwc_ep.xfer_count;
  55490. + dwc_otg_request_done(ep, req, -ECONNRESET);
  55491. + ep->dwc_ep.start_xfer_buff = 0;
  55492. + ep->dwc_ep.xfer_buff = 0;
  55493. + ep->dwc_ep.xfer_len = 0;
  55494. + }
  55495. + pcd->ep0state = EP0_IDLE;
  55496. + if (doepint.b.setup) {
  55497. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  55498. + /* Data stage started, clear setup */
  55499. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55500. + doepint.b.setup = 0;
  55501. + handle_ep0(pcd);
  55502. + /* Prepare for setup packets if ep0in was enabled*/
  55503. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55504. + ep0_out_start(core_if, pcd);
  55505. + }
  55506. +
  55507. + goto exit_xfercompl;
  55508. + } else {
  55509. + /* Prepare for more setup packets */
  55510. + DWC_DEBUGPL(DBG_PCDV,
  55511. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  55512. + ep0_out_start(core_if, pcd);
  55513. + }
  55514. + }
  55515. + }
  55516. + }
  55517. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  55518. + && core_if->dma_desc_enable == 0) {
  55519. + doepint_data_t doepint_temp = {.d32 = 0};
  55520. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  55521. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  55522. + out_ep_regs[ep->dwc_ep.num]->doepint);
  55523. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55524. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  55525. + if (pcd->ep0state == EP0_IDLE) {
  55526. + if (doepint_temp.b.sr) {
  55527. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55528. + }
  55529. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55530. + out_ep_regs[0]->doepint);
  55531. + if (doeptsize0.b.supcnt == 3) {
  55532. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  55533. + ep->dwc_ep.stp_rollover = 1;
  55534. + }
  55535. + if (doepint.b.setup) {
  55536. +retry:
  55537. + /* Already started data stage, clear setup */
  55538. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55539. + doepint.b.setup = 0;
  55540. + handle_ep0(pcd);
  55541. + ep->dwc_ep.stp_rollover = 0;
  55542. + /* Prepare for more setup packets */
  55543. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  55544. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  55545. + ep0_out_start(core_if, pcd);
  55546. + }
  55547. + goto exit_xfercompl;
  55548. + } else {
  55549. + /* Prepare for more setup packets */
  55550. + DWC_DEBUGPL(DBG_ANY,
  55551. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  55552. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55553. + out_ep_regs[0]->doepint);
  55554. + if(doepint.b.setup)
  55555. + goto retry;
  55556. + ep0_out_start(core_if, pcd);
  55557. + }
  55558. + } else {
  55559. + dwc_otg_pcd_request_t *req;
  55560. + diepint_data_t diepint0 = {.d32 = 0};
  55561. + doepint_data_t doepint_temp = {.d32 = 0};
  55562. + depctl_data_t diepctl0;
  55563. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55564. + in_ep_regs[0]->diepint);
  55565. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55566. + in_ep_regs[0]->diepctl);
  55567. +
  55568. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  55569. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55570. + if (diepint0.b.xfercompl) {
  55571. + DWC_WRITE_REG32(&core_if->dev_if->
  55572. + in_ep_regs[0]->diepint, diepint0.d32);
  55573. + }
  55574. + if (diepctl0.b.epena) {
  55575. + diepint_data_t diepint = {.d32 = 0};
  55576. + diepctl0.b.snak = 1;
  55577. + DWC_WRITE_REG32(&core_if->dev_if->
  55578. + in_ep_regs[0]->diepctl, diepctl0.d32);
  55579. + do {
  55580. + dwc_udelay(10);
  55581. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55582. + in_ep_regs[0]->diepint);
  55583. + } while (!diepint.b.inepnakeff);
  55584. + diepint.b.inepnakeff = 1;
  55585. + DWC_WRITE_REG32(&core_if->dev_if->
  55586. + in_ep_regs[0]->diepint, diepint.d32);
  55587. + diepctl0.d32 = 0;
  55588. + diepctl0.b.epdis = 1;
  55589. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  55590. + diepctl0.d32);
  55591. + do {
  55592. + dwc_udelay(10);
  55593. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55594. + in_ep_regs[0]->diepint);
  55595. + } while (!diepint.b.epdisabled);
  55596. + diepint.b.epdisabled = 1;
  55597. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  55598. + diepint.d32);
  55599. + }
  55600. + }
  55601. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  55602. + out_ep_regs[ep->dwc_ep.num]->doepint);
  55603. + if (doepint_temp.b.sr) {
  55604. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55605. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55606. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  55607. + } else {
  55608. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  55609. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55610. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  55611. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  55612. + /* Read arrived setup packet from req->buf */
  55613. + dwc_memcpy(&pcd->setup_pkt->req,
  55614. + req->buf + ep->dwc_ep.xfer_count, 8);
  55615. + }
  55616. + req->actual = ep->dwc_ep.xfer_count;
  55617. + dwc_otg_request_done(ep, req, -ECONNRESET);
  55618. + ep->dwc_ep.start_xfer_buff = 0;
  55619. + ep->dwc_ep.xfer_buff = 0;
  55620. + ep->dwc_ep.xfer_len = 0;
  55621. + }
  55622. + pcd->ep0state = EP0_IDLE;
  55623. + if (doepint.b.setup) {
  55624. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  55625. + /* Data stage started, clear setup */
  55626. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55627. + doepint.b.setup = 0;
  55628. + handle_ep0(pcd);
  55629. + /* Prepare for setup packets if ep0in was enabled*/
  55630. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55631. + ep0_out_start(core_if, pcd);
  55632. + }
  55633. + goto exit_xfercompl;
  55634. + } else {
  55635. + /* Prepare for more setup packets */
  55636. + DWC_DEBUGPL(DBG_PCDV,
  55637. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  55638. + ep0_out_start(core_if, pcd);
  55639. + }
  55640. + }
  55641. + }
  55642. + }
  55643. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  55644. + handle_ep0(pcd);
  55645. +exit_xfercompl:
  55646. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  55647. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  55648. + } else {
  55649. + if (core_if->dma_desc_enable == 0
  55650. + || pcd->ep0state != EP0_IDLE)
  55651. + handle_ep0(pcd);
  55652. + }
  55653. +#ifdef DWC_EN_ISOC
  55654. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55655. + if (doepint.b.pktdrpsts == 0) {
  55656. + /* Clear the bit in DOEPINTn for this interrupt */
  55657. + CLEAR_OUT_EP_INTR(core_if,
  55658. + epnum,
  55659. + xfercompl);
  55660. + complete_iso_ep(pcd, ep);
  55661. + } else {
  55662. +
  55663. + doepint_data_t doepint = {.d32 = 0 };
  55664. + doepint.b.xfercompl = 1;
  55665. + doepint.b.pktdrpsts = 1;
  55666. + DWC_WRITE_REG32
  55667. + (&core_if->dev_if->out_ep_regs
  55668. + [epnum]->doepint,
  55669. + doepint.d32);
  55670. + if (handle_iso_out_pkt_dropped
  55671. + (core_if, dwc_ep)) {
  55672. + complete_iso_ep(pcd,
  55673. + ep);
  55674. + }
  55675. + }
  55676. +#endif /* DWC_EN_ISOC */
  55677. +#ifdef DWC_UTE_PER_IO
  55678. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55679. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  55680. + if (!ep->stopped)
  55681. + complete_xiso_ep(ep);
  55682. +#endif /* DWC_UTE_PER_IO */
  55683. + } else {
  55684. + /* Clear the bit in DOEPINTn for this interrupt */
  55685. + CLEAR_OUT_EP_INTR(core_if, epnum,
  55686. + xfercompl);
  55687. +
  55688. + if (core_if->core_params->dev_out_nak) {
  55689. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  55690. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  55691. +#ifdef DEBUG
  55692. + print_memory_payload(pcd, dwc_ep);
  55693. +#endif
  55694. + }
  55695. + complete_ep(ep);
  55696. + }
  55697. +
  55698. + }
  55699. +
  55700. + /* Endpoint disable */
  55701. + if (doepint.b.epdisabled) {
  55702. +
  55703. + /* Clear the bit in DOEPINTn for this interrupt */
  55704. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  55705. + if (core_if->core_params->dev_out_nak) {
  55706. +#ifdef DEBUG
  55707. + print_memory_payload(pcd, dwc_ep);
  55708. +#endif
  55709. + /* In case of timeout condition */
  55710. + if (core_if->ep_xfer_info[epnum].state == 2) {
  55711. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55712. + dev_global_regs->dctl);
  55713. + dctl.b.cgoutnak = 1;
  55714. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55715. + dctl.d32);
  55716. + /* Unmask goutnakeff interrupt which was masked
  55717. + * during handle nak out interrupt */
  55718. + gintmsk.b.goutnakeff = 1;
  55719. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  55720. + 0, gintmsk.d32);
  55721. +
  55722. + complete_ep(ep);
  55723. + }
  55724. + }
  55725. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  55726. + {
  55727. + dctl_data_t dctl;
  55728. + gintmsk_data_t intr_mask = {.d32 = 0};
  55729. + dwc_otg_pcd_request_t *req = 0;
  55730. +
  55731. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55732. + dev_global_regs->dctl);
  55733. + dctl.b.cgoutnak = 1;
  55734. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55735. + dctl.d32);
  55736. +
  55737. + intr_mask.d32 = 0;
  55738. + intr_mask.b.incomplisoout = 1;
  55739. +
  55740. + /* Get any pending requests */
  55741. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55742. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55743. + if (!req) {
  55744. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  55745. + } else {
  55746. + dwc_otg_request_done(ep, req, 0);
  55747. + start_next_request(ep);
  55748. + }
  55749. + } else {
  55750. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  55751. + }
  55752. + }
  55753. + }
  55754. + /* AHB Error */
  55755. + if (doepint.b.ahberr) {
  55756. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  55757. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  55758. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  55759. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  55760. + }
  55761. + /* Setup Phase Done (contorl EPs) */
  55762. + if (doepint.b.setup) {
  55763. +#ifdef DEBUG_EP0
  55764. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  55765. +#endif
  55766. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55767. +
  55768. + handle_ep0(pcd);
  55769. + }
  55770. +
  55771. + /** OUT EP BNA Intr */
  55772. + if (doepint.b.bna) {
  55773. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  55774. + if (core_if->dma_desc_enable) {
  55775. +#ifdef DWC_EN_ISOC
  55776. + if (dwc_ep->type ==
  55777. + DWC_OTG_EP_TYPE_ISOC) {
  55778. + /*
  55779. + * This checking is performed to prevent first "false" BNA
  55780. + * handling occuring right after reconnect
  55781. + */
  55782. + if (dwc_ep->next_frame !=
  55783. + 0xffffffff)
  55784. + dwc_otg_pcd_handle_iso_bna(ep);
  55785. + } else
  55786. +#endif /* DWC_EN_ISOC */
  55787. + {
  55788. + dwc_otg_pcd_handle_noniso_bna(ep);
  55789. + }
  55790. + }
  55791. + }
  55792. + /* Babble Interrupt */
  55793. + if (doepint.b.babble) {
  55794. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  55795. + epnum);
  55796. + handle_out_ep_babble_intr(pcd, epnum);
  55797. +
  55798. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  55799. + }
  55800. + if (doepint.b.outtknepdis) {
  55801. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  55802. + disabled\n",epnum);
  55803. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55804. + doepmsk_data_t doepmsk = {.d32 = 0};
  55805. + ep->dwc_ep.frame_num = core_if->frame_num;
  55806. + if (ep->dwc_ep.bInterval > 1) {
  55807. + depctl_data_t depctl;
  55808. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55809. + out_ep_regs[epnum]->doepctl);
  55810. + if (ep->dwc_ep.frame_num & 0x1) {
  55811. + depctl.b.setd1pid = 1;
  55812. + depctl.b.setd0pid = 0;
  55813. + } else {
  55814. + depctl.b.setd0pid = 1;
  55815. + depctl.b.setd1pid = 0;
  55816. + }
  55817. + DWC_WRITE_REG32(&core_if->dev_if->
  55818. + out_ep_regs[epnum]->doepctl, depctl.d32);
  55819. + }
  55820. + start_next_request(ep);
  55821. + doepmsk.b.outtknepdis = 1;
  55822. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  55823. + doepmsk.d32, 0);
  55824. + }
  55825. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  55826. + }
  55827. +
  55828. + /* NAK Interrutp */
  55829. + if (doepint.b.nak) {
  55830. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  55831. + handle_out_ep_nak_intr(pcd, epnum);
  55832. +
  55833. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  55834. + }
  55835. + /* NYET Interrutp */
  55836. + if (doepint.b.nyet) {
  55837. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  55838. + handle_out_ep_nyet_intr(pcd, epnum);
  55839. +
  55840. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  55841. + }
  55842. + }
  55843. +
  55844. + epnum++;
  55845. + ep_intr >>= 1;
  55846. + }
  55847. +
  55848. + return 1;
  55849. +
  55850. +#undef CLEAR_OUT_EP_INTR
  55851. +}
  55852. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  55853. +{
  55854. + int retval = 0;
  55855. + if(!frm_overrun && curr_fr >= trgt_fr)
  55856. + retval = 1;
  55857. + else if (frm_overrun
  55858. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  55859. + retval = 1;
  55860. + return retval;
  55861. +}
  55862. +/**
  55863. + * Incomplete ISO IN Transfer Interrupt.
  55864. + * This interrupt indicates one of the following conditions occurred
  55865. + * while transmitting an ISOC transaction.
  55866. + * - Corrupted IN Token for ISOC EP.
  55867. + * - Packet not complete in FIFO.
  55868. + * The follow actions will be taken:
  55869. + * -# Determine the EP
  55870. + * -# Set incomplete flag in dwc_ep structure
  55871. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  55872. + * Flush FIFO
  55873. + */
  55874. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  55875. +{
  55876. + gintsts_data_t gintsts;
  55877. +
  55878. +#ifdef DWC_EN_ISOC
  55879. + dwc_otg_dev_if_t *dev_if;
  55880. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55881. + depctl_data_t depctl = {.d32 = 0 };
  55882. + dsts_data_t dsts = {.d32 = 0 };
  55883. + dwc_ep_t *dwc_ep;
  55884. + int i;
  55885. +
  55886. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55887. +
  55888. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  55889. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  55890. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55891. + deptsiz.d32 =
  55892. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  55893. + depctl.d32 =
  55894. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55895. +
  55896. + if (depctl.b.epdis && deptsiz.d32) {
  55897. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  55898. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  55899. + dwc_ep->cur_pkt = 0;
  55900. + dwc_ep->proc_buf_num =
  55901. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  55902. +
  55903. + if (dwc_ep->proc_buf_num) {
  55904. + dwc_ep->cur_pkt_addr =
  55905. + dwc_ep->xfer_buff1;
  55906. + dwc_ep->cur_pkt_dma_addr =
  55907. + dwc_ep->dma_addr1;
  55908. + } else {
  55909. + dwc_ep->cur_pkt_addr =
  55910. + dwc_ep->xfer_buff0;
  55911. + dwc_ep->cur_pkt_dma_addr =
  55912. + dwc_ep->dma_addr0;
  55913. + }
  55914. +
  55915. + }
  55916. +
  55917. + dsts.d32 =
  55918. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  55919. + dev_global_regs->dsts);
  55920. + dwc_ep->next_frame = dsts.b.soffn;
  55921. +
  55922. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  55923. + (pcd),
  55924. + dwc_ep);
  55925. + }
  55926. + }
  55927. + }
  55928. +
  55929. +#else
  55930. + depctl_data_t depctl = {.d32 = 0 };
  55931. + dwc_ep_t *dwc_ep;
  55932. + dwc_otg_dev_if_t *dev_if;
  55933. + int i;
  55934. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55935. +
  55936. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  55937. +
  55938. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  55939. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  55940. + depctl.d32 =
  55941. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55942. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55943. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  55944. + dwc_ep->frm_overrun))
  55945. + {
  55946. + depctl.d32 =
  55947. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55948. + depctl.b.snak = 1;
  55949. + depctl.b.epdis = 1;
  55950. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  55951. + }
  55952. + }
  55953. + }
  55954. +
  55955. + /*intr_mask.b.incomplisoin = 1;
  55956. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55957. + intr_mask.d32, 0); */
  55958. +#endif //DWC_EN_ISOC
  55959. +
  55960. + /* Clear interrupt */
  55961. + gintsts.d32 = 0;
  55962. + gintsts.b.incomplisoin = 1;
  55963. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55964. + gintsts.d32);
  55965. +
  55966. + return 1;
  55967. +}
  55968. +
  55969. +/**
  55970. + * Incomplete ISO OUT Transfer Interrupt.
  55971. + *
  55972. + * This interrupt indicates that the core has dropped an ISO OUT
  55973. + * packet. The following conditions can be the cause:
  55974. + * - FIFO Full, the entire packet would not fit in the FIFO.
  55975. + * - CRC Error
  55976. + * - Corrupted Token
  55977. + * The follow actions will be taken:
  55978. + * -# Determine the EP
  55979. + * -# Set incomplete flag in dwc_ep structure
  55980. + * -# Read any data from the FIFO
  55981. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  55982. + * re-enable EP.
  55983. + */
  55984. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  55985. +{
  55986. +
  55987. + gintsts_data_t gintsts;
  55988. +
  55989. +#ifdef DWC_EN_ISOC
  55990. + dwc_otg_dev_if_t *dev_if;
  55991. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55992. + depctl_data_t depctl = {.d32 = 0 };
  55993. + dsts_data_t dsts = {.d32 = 0 };
  55994. + dwc_ep_t *dwc_ep;
  55995. + int i;
  55996. +
  55997. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55998. +
  55999. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  56000. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  56001. + if (pcd->out_ep[i].dwc_ep.active &&
  56002. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  56003. + deptsiz.d32 =
  56004. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  56005. + depctl.d32 =
  56006. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  56007. +
  56008. + if (depctl.b.epdis && deptsiz.d32) {
  56009. + set_current_pkt_info(GET_CORE_IF(pcd),
  56010. + &pcd->out_ep[i].dwc_ep);
  56011. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  56012. + dwc_ep->cur_pkt = 0;
  56013. + dwc_ep->proc_buf_num =
  56014. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  56015. +
  56016. + if (dwc_ep->proc_buf_num) {
  56017. + dwc_ep->cur_pkt_addr =
  56018. + dwc_ep->xfer_buff1;
  56019. + dwc_ep->cur_pkt_dma_addr =
  56020. + dwc_ep->dma_addr1;
  56021. + } else {
  56022. + dwc_ep->cur_pkt_addr =
  56023. + dwc_ep->xfer_buff0;
  56024. + dwc_ep->cur_pkt_dma_addr =
  56025. + dwc_ep->dma_addr0;
  56026. + }
  56027. +
  56028. + }
  56029. +
  56030. + dsts.d32 =
  56031. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  56032. + dev_global_regs->dsts);
  56033. + dwc_ep->next_frame = dsts.b.soffn;
  56034. +
  56035. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  56036. + (pcd),
  56037. + dwc_ep);
  56038. + }
  56039. + }
  56040. + }
  56041. +#else
  56042. + /** @todo implement ISR */
  56043. + gintmsk_data_t intr_mask = {.d32 = 0 };
  56044. + dwc_otg_core_if_t *core_if;
  56045. + deptsiz_data_t deptsiz = {.d32 = 0 };
  56046. + depctl_data_t depctl = {.d32 = 0 };
  56047. + dctl_data_t dctl = {.d32 = 0 };
  56048. + dwc_ep_t *dwc_ep = NULL;
  56049. + int i;
  56050. + core_if = GET_CORE_IF(pcd);
  56051. +
  56052. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  56053. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  56054. + depctl.d32 =
  56055. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  56056. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  56057. + core_if->dev_if->isoc_ep = dwc_ep;
  56058. + deptsiz.d32 =
  56059. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  56060. + break;
  56061. + }
  56062. + }
  56063. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  56064. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  56065. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  56066. +
  56067. + if (!intr_mask.b.goutnakeff) {
  56068. + /* Unmask it */
  56069. + intr_mask.b.goutnakeff = 1;
  56070. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  56071. + }
  56072. + if (!gintsts.b.goutnakeff) {
  56073. + dctl.b.sgoutnak = 1;
  56074. + }
  56075. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  56076. +
  56077. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  56078. + if (depctl.b.epena) {
  56079. + depctl.b.epdis = 1;
  56080. + depctl.b.snak = 1;
  56081. + }
  56082. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  56083. +
  56084. + intr_mask.d32 = 0;
  56085. + intr_mask.b.incomplisoout = 1;
  56086. +
  56087. +#endif /* DWC_EN_ISOC */
  56088. +
  56089. + /* Clear interrupt */
  56090. + gintsts.d32 = 0;
  56091. + gintsts.b.incomplisoout = 1;
  56092. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  56093. + gintsts.d32);
  56094. +
  56095. + return 1;
  56096. +}
  56097. +
  56098. +/**
  56099. + * This function handles the Global IN NAK Effective interrupt.
  56100. + *
  56101. + */
  56102. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  56103. +{
  56104. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  56105. + depctl_data_t diepctl = {.d32 = 0 };
  56106. + gintmsk_data_t intr_mask = {.d32 = 0 };
  56107. + gintsts_data_t gintsts;
  56108. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  56109. + int i;
  56110. +
  56111. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  56112. +
  56113. + /* Disable all active IN EPs */
  56114. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  56115. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  56116. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  56117. + if (core_if->start_predict > 0)
  56118. + core_if->start_predict++;
  56119. + diepctl.b.epdis = 1;
  56120. + diepctl.b.snak = 1;
  56121. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  56122. + }
  56123. + }
  56124. +
  56125. +
  56126. + /* Disable the Global IN NAK Effective Interrupt */
  56127. + intr_mask.b.ginnakeff = 1;
  56128. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  56129. + intr_mask.d32, 0);
  56130. +
  56131. + /* Clear interrupt */
  56132. + gintsts.d32 = 0;
  56133. + gintsts.b.ginnakeff = 1;
  56134. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  56135. + gintsts.d32);
  56136. +
  56137. + return 1;
  56138. +}
  56139. +
  56140. +/**
  56141. + * OUT NAK Effective.
  56142. + *
  56143. + */
  56144. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  56145. +{
  56146. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  56147. + gintmsk_data_t intr_mask = {.d32 = 0 };
  56148. + gintsts_data_t gintsts;
  56149. + depctl_data_t doepctl;
  56150. + int i;
  56151. +
  56152. + /* Disable the Global OUT NAK Effective Interrupt */
  56153. + intr_mask.b.goutnakeff = 1;
  56154. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  56155. + intr_mask.d32, 0);
  56156. +
  56157. + /* If DEV OUT NAK enabled*/
  56158. + if (pcd->core_if->core_params->dev_out_nak) {
  56159. + /* Run over all out endpoints to determine the ep number on
  56160. + * which the timeout has happened
  56161. + */
  56162. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  56163. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  56164. + break;
  56165. + }
  56166. + if (i > dev_if->num_out_eps) {
  56167. + dctl_data_t dctl;
  56168. + dctl.d32 =
  56169. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  56170. + dctl.b.cgoutnak = 1;
  56171. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  56172. + dctl.d32);
  56173. + goto out;
  56174. + }
  56175. +
  56176. + /* Disable the endpoint */
  56177. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  56178. + if (doepctl.b.epena) {
  56179. + doepctl.b.epdis = 1;
  56180. + doepctl.b.snak = 1;
  56181. + }
  56182. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  56183. + return 1;
  56184. + }
  56185. + /* We come here from Incomplete ISO OUT handler */
  56186. + if (dev_if->isoc_ep) {
  56187. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  56188. + uint32_t epnum = dwc_ep->num;
  56189. + doepint_data_t doepint;
  56190. + doepint.d32 =
  56191. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  56192. + dev_if->isoc_ep = NULL;
  56193. + doepctl.d32 =
  56194. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  56195. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  56196. + if (doepctl.b.epena) {
  56197. + doepctl.b.epdis = 1;
  56198. + doepctl.b.snak = 1;
  56199. + }
  56200. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  56201. + doepctl.d32);
  56202. + return 1;
  56203. + } else
  56204. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  56205. + "Global OUT NAK Effective\n");
  56206. +
  56207. +out:
  56208. + /* Clear interrupt */
  56209. + gintsts.d32 = 0;
  56210. + gintsts.b.goutnakeff = 1;
  56211. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  56212. + gintsts.d32);
  56213. +
  56214. + return 1;
  56215. +}
  56216. +
  56217. +/**
  56218. + * PCD interrupt handler.
  56219. + *
  56220. + * The PCD handles the device interrupts. Many conditions can cause a
  56221. + * device interrupt. When an interrupt occurs, the device interrupt
  56222. + * service routine determines the cause of the interrupt and
  56223. + * dispatches handling to the appropriate function. These interrupt
  56224. + * handling functions are described below.
  56225. + *
  56226. + * All interrupt registers are processed from LSB to MSB.
  56227. + *
  56228. + */
  56229. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  56230. +{
  56231. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  56232. +#ifdef VERBOSE
  56233. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56234. +#endif
  56235. + gintsts_data_t gintr_status;
  56236. + int32_t retval = 0;
  56237. +
  56238. + /* Exit from ISR if core is hibernated */
  56239. + if (core_if->hibernation_suspend == 1) {
  56240. + return retval;
  56241. + }
  56242. +#ifdef VERBOSE
  56243. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  56244. + __func__,
  56245. + DWC_READ_REG32(&global_regs->gintsts),
  56246. + DWC_READ_REG32(&global_regs->gintmsk));
  56247. +#endif
  56248. +
  56249. + if (dwc_otg_is_device_mode(core_if)) {
  56250. + DWC_SPINLOCK(pcd->lock);
  56251. +#ifdef VERBOSE
  56252. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  56253. + __func__,
  56254. + DWC_READ_REG32(&global_regs->gintsts),
  56255. + DWC_READ_REG32(&global_regs->gintmsk));
  56256. +#endif
  56257. +
  56258. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  56259. +
  56260. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  56261. + __func__, gintr_status.d32);
  56262. +
  56263. + if (gintr_status.b.sofintr) {
  56264. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  56265. + }
  56266. + if (gintr_status.b.rxstsqlvl) {
  56267. + retval |=
  56268. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  56269. + }
  56270. + if (gintr_status.b.nptxfempty) {
  56271. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  56272. + }
  56273. + if (gintr_status.b.goutnakeff) {
  56274. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  56275. + }
  56276. + if (gintr_status.b.i2cintr) {
  56277. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  56278. + }
  56279. + if (gintr_status.b.erlysuspend) {
  56280. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  56281. + }
  56282. + if (gintr_status.b.usbreset) {
  56283. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  56284. + }
  56285. + if (gintr_status.b.enumdone) {
  56286. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  56287. + }
  56288. + if (gintr_status.b.isooutdrop) {
  56289. + retval |=
  56290. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  56291. + (pcd);
  56292. + }
  56293. + if (gintr_status.b.eopframe) {
  56294. + retval |=
  56295. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  56296. + }
  56297. + if (gintr_status.b.inepint) {
  56298. + if (!core_if->multiproc_int_enable) {
  56299. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  56300. + }
  56301. + }
  56302. + if (gintr_status.b.outepintr) {
  56303. + if (!core_if->multiproc_int_enable) {
  56304. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  56305. + }
  56306. + }
  56307. + if (gintr_status.b.epmismatch) {
  56308. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  56309. + }
  56310. + if (gintr_status.b.fetsusp) {
  56311. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  56312. + }
  56313. + if (gintr_status.b.ginnakeff) {
  56314. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  56315. + }
  56316. + if (gintr_status.b.incomplisoin) {
  56317. + retval |=
  56318. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  56319. + }
  56320. + if (gintr_status.b.incomplisoout) {
  56321. + retval |=
  56322. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  56323. + }
  56324. +
  56325. + /* In MPI mode Device Endpoints interrupts are asserted
  56326. + * without setting outepintr and inepint bits set, so these
  56327. + * Interrupt handlers are called without checking these bit-fields
  56328. + */
  56329. + if (core_if->multiproc_int_enable) {
  56330. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  56331. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  56332. + }
  56333. +#ifdef VERBOSE
  56334. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  56335. + DWC_READ_REG32(&global_regs->gintsts));
  56336. +#endif
  56337. + DWC_SPINUNLOCK(pcd->lock);
  56338. + }
  56339. + return retval;
  56340. +}
  56341. +
  56342. +#endif /* DWC_HOST_ONLY */
  56343. --- /dev/null
  56344. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  56345. @@ -0,0 +1,1280 @@
  56346. + /* ==========================================================================
  56347. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  56348. + * $Revision: #21 $
  56349. + * $Date: 2012/08/10 $
  56350. + * $Change: 2047372 $
  56351. + *
  56352. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  56353. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  56354. + * otherwise expressly agreed to in writing between Synopsys and you.
  56355. + *
  56356. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  56357. + * any End User Software License Agreement or Agreement for Licensed Product
  56358. + * with Synopsys or any supplement thereto. You are permitted to use and
  56359. + * redistribute this Software in source and binary forms, with or without
  56360. + * modification, provided that redistributions of source code must retain this
  56361. + * notice. You may not view, use, disclose, copy or distribute this file or
  56362. + * any information contained herein except pursuant to this license grant from
  56363. + * Synopsys. If you do not agree with this notice, including the disclaimer
  56364. + * below, then you are not authorized to use the Software.
  56365. + *
  56366. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  56367. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  56368. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  56369. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  56370. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  56371. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  56372. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56373. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56374. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56375. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  56376. + * DAMAGE.
  56377. + * ========================================================================== */
  56378. +#ifndef DWC_HOST_ONLY
  56379. +
  56380. +/** @file
  56381. + * This file implements the Peripheral Controller Driver.
  56382. + *
  56383. + * The Peripheral Controller Driver (PCD) is responsible for
  56384. + * translating requests from the Function Driver into the appropriate
  56385. + * actions on the DWC_otg controller. It isolates the Function Driver
  56386. + * from the specifics of the controller by providing an API to the
  56387. + * Function Driver.
  56388. + *
  56389. + * The Peripheral Controller Driver for Linux will implement the
  56390. + * Gadget API, so that the existing Gadget drivers can be used.
  56391. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  56392. + *
  56393. + * The Linux Gadget API is defined in the header file
  56394. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  56395. + * defined in the structure <code>usb_ep_ops</code> and the USB
  56396. + * Controller API is defined in the structure
  56397. + * <code>usb_gadget_ops</code>.
  56398. + *
  56399. + */
  56400. +
  56401. +#include "dwc_otg_os_dep.h"
  56402. +#include "dwc_otg_pcd_if.h"
  56403. +#include "dwc_otg_pcd.h"
  56404. +#include "dwc_otg_driver.h"
  56405. +#include "dwc_otg_dbg.h"
  56406. +
  56407. +extern bool fiq_enable;
  56408. +
  56409. +static struct gadget_wrapper {
  56410. + dwc_otg_pcd_t *pcd;
  56411. +
  56412. + struct usb_gadget gadget;
  56413. + struct usb_gadget_driver *driver;
  56414. +
  56415. + struct usb_ep ep0;
  56416. + struct usb_ep in_ep[16];
  56417. + struct usb_ep out_ep[16];
  56418. +
  56419. +} *gadget_wrapper;
  56420. +
  56421. +/* Display the contents of the buffer */
  56422. +extern void dump_msg(const u8 * buf, unsigned int length);
  56423. +/**
  56424. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  56425. + * if the endpoint is not found
  56426. + */
  56427. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  56428. +{
  56429. + int i;
  56430. + if (pcd->ep0.priv == handle) {
  56431. + return &pcd->ep0;
  56432. + }
  56433. +
  56434. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  56435. + if (pcd->in_ep[i].priv == handle)
  56436. + return &pcd->in_ep[i];
  56437. + if (pcd->out_ep[i].priv == handle)
  56438. + return &pcd->out_ep[i];
  56439. + }
  56440. +
  56441. + return NULL;
  56442. +}
  56443. +
  56444. +/* USB Endpoint Operations */
  56445. +/*
  56446. + * The following sections briefly describe the behavior of the Gadget
  56447. + * API endpoint operations implemented in the DWC_otg driver
  56448. + * software. Detailed descriptions of the generic behavior of each of
  56449. + * these functions can be found in the Linux header file
  56450. + * include/linux/usb_gadget.h.
  56451. + *
  56452. + * The Gadget API provides wrapper functions for each of the function
  56453. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  56454. + * function, which then calls the underlying PCD function. The
  56455. + * following sections are named according to the wrapper
  56456. + * functions. Within each section, the corresponding DWC_otg PCD
  56457. + * function name is specified.
  56458. + *
  56459. + */
  56460. +
  56461. +/**
  56462. + * This function is called by the Gadget Driver for each EP to be
  56463. + * configured for the current configuration (SET_CONFIGURATION).
  56464. + *
  56465. + * This function initializes the dwc_otg_ep_t data structure, and then
  56466. + * calls dwc_otg_ep_activate.
  56467. + */
  56468. +static int ep_enable(struct usb_ep *usb_ep,
  56469. + const struct usb_endpoint_descriptor *ep_desc)
  56470. +{
  56471. + int retval;
  56472. +
  56473. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  56474. +
  56475. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  56476. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  56477. + return -EINVAL;
  56478. + }
  56479. + if (usb_ep == &gadget_wrapper->ep0) {
  56480. + DWC_WARN("%s, bad ep(0)\n", __func__);
  56481. + return -EINVAL;
  56482. + }
  56483. +
  56484. + /* Check FIFO size? */
  56485. + if (!ep_desc->wMaxPacketSize) {
  56486. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  56487. + return -ERANGE;
  56488. + }
  56489. +
  56490. + if (!gadget_wrapper->driver ||
  56491. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56492. + DWC_WARN("%s, bogus device state\n", __func__);
  56493. + return -ESHUTDOWN;
  56494. + }
  56495. +
  56496. + /* Delete after check - MAS */
  56497. +#if 0
  56498. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  56499. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  56500. + nat = (nat >> 11) & 0x03;
  56501. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  56502. +#endif
  56503. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  56504. + (const uint8_t *)ep_desc,
  56505. + (void *)usb_ep);
  56506. + if (retval) {
  56507. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  56508. + return -EINVAL;
  56509. + }
  56510. +
  56511. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  56512. +
  56513. + return 0;
  56514. +}
  56515. +
  56516. +/**
  56517. + * This function is called when an EP is disabled due to disconnect or
  56518. + * change in configuration. Any pending requests will terminate with a
  56519. + * status of -ESHUTDOWN.
  56520. + *
  56521. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  56522. + * and then calls dwc_otg_ep_deactivate.
  56523. + */
  56524. +static int ep_disable(struct usb_ep *usb_ep)
  56525. +{
  56526. + int retval;
  56527. +
  56528. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  56529. + if (!usb_ep) {
  56530. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  56531. + usb_ep ? usb_ep->name : NULL);
  56532. + return -EINVAL;
  56533. + }
  56534. +
  56535. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  56536. + if (retval) {
  56537. + retval = -EINVAL;
  56538. + }
  56539. +
  56540. + return retval;
  56541. +}
  56542. +
  56543. +/**
  56544. + * This function allocates a request object to use with the specified
  56545. + * endpoint.
  56546. + *
  56547. + * @param ep The endpoint to be used with with the request
  56548. + * @param gfp_flags the GFP_* flags to use.
  56549. + */
  56550. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  56551. + gfp_t gfp_flags)
  56552. +{
  56553. + struct usb_request *usb_req;
  56554. +
  56555. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  56556. + if (0 == ep) {
  56557. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  56558. + return 0;
  56559. + }
  56560. + usb_req = kzalloc(sizeof(*usb_req), gfp_flags);
  56561. + if (0 == usb_req) {
  56562. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  56563. + return 0;
  56564. + }
  56565. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  56566. +
  56567. + return usb_req;
  56568. +}
  56569. +
  56570. +/**
  56571. + * This function frees a request object.
  56572. + *
  56573. + * @param ep The endpoint associated with the request
  56574. + * @param req The request being freed
  56575. + */
  56576. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  56577. +{
  56578. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  56579. +
  56580. + if (0 == ep || 0 == req) {
  56581. + DWC_WARN("%s() %s\n", __func__,
  56582. + "Invalid ep or req argument!\n");
  56583. + return;
  56584. + }
  56585. +
  56586. + kfree(req);
  56587. +}
  56588. +
  56589. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56590. +/**
  56591. + * This function allocates an I/O buffer to be used for a transfer
  56592. + * to/from the specified endpoint.
  56593. + *
  56594. + * @param usb_ep The endpoint to be used with with the request
  56595. + * @param bytes The desired number of bytes for the buffer
  56596. + * @param dma Pointer to the buffer's DMA address; must be valid
  56597. + * @param gfp_flags the GFP_* flags to use.
  56598. + * @return address of a new buffer or null is buffer could not be allocated.
  56599. + */
  56600. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  56601. + dma_addr_t * dma, gfp_t gfp_flags)
  56602. +{
  56603. + void *buf;
  56604. + dwc_otg_pcd_t *pcd = 0;
  56605. +
  56606. + pcd = gadget_wrapper->pcd;
  56607. +
  56608. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  56609. + dma, gfp_flags);
  56610. +
  56611. + /* Check dword alignment */
  56612. + if ((bytes & 0x3UL) != 0) {
  56613. + DWC_WARN("%s() Buffer size is not a multiple of"
  56614. + "DWORD size (%d)", __func__, bytes);
  56615. + }
  56616. +
  56617. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  56618. + WARN_ON(!buf);
  56619. +
  56620. + /* Check dword alignment */
  56621. + if (((int)buf & 0x3UL) != 0) {
  56622. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  56623. + __func__, buf);
  56624. + }
  56625. +
  56626. + return buf;
  56627. +}
  56628. +
  56629. +/**
  56630. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  56631. + *
  56632. + * @param usb_ep the endpoint associated with the buffer
  56633. + * @param buf address of the buffer
  56634. + * @param dma The buffer's DMA address
  56635. + * @param bytes The number of bytes of the buffer
  56636. + */
  56637. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  56638. + dma_addr_t dma, unsigned bytes)
  56639. +{
  56640. + dwc_otg_pcd_t *pcd = 0;
  56641. +
  56642. + pcd = gadget_wrapper->pcd;
  56643. +
  56644. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  56645. +
  56646. + dma_free_coherent(NULL, bytes, buf, dma);
  56647. +}
  56648. +#endif
  56649. +
  56650. +/**
  56651. + * This function is used to submit an I/O Request to an EP.
  56652. + *
  56653. + * - When the request completes the request's completion callback
  56654. + * is called to return the request to the driver.
  56655. + * - An EP, except control EPs, may have multiple requests
  56656. + * pending.
  56657. + * - Once submitted the request cannot be examined or modified.
  56658. + * - Each request is turned into one or more packets.
  56659. + * - A BULK EP can queue any amount of data; the transfer is
  56660. + * packetized.
  56661. + * - Zero length Packets are specified with the request 'zero'
  56662. + * flag.
  56663. + */
  56664. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  56665. + gfp_t gfp_flags)
  56666. +{
  56667. + dwc_otg_pcd_t *pcd;
  56668. + struct dwc_otg_pcd_ep *ep = NULL;
  56669. + int retval = 0, is_isoc_ep = 0;
  56670. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  56671. +
  56672. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  56673. + __func__, usb_ep, usb_req, gfp_flags);
  56674. +
  56675. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  56676. + DWC_WARN("bad params\n");
  56677. + return -EINVAL;
  56678. + }
  56679. +
  56680. + if (!usb_ep) {
  56681. + DWC_WARN("bad ep\n");
  56682. + return -EINVAL;
  56683. + }
  56684. +
  56685. + pcd = gadget_wrapper->pcd;
  56686. + if (!gadget_wrapper->driver ||
  56687. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56688. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  56689. + gadget_wrapper->gadget.speed);
  56690. + DWC_WARN("bogus device state\n");
  56691. + return -ESHUTDOWN;
  56692. + }
  56693. +
  56694. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  56695. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  56696. +
  56697. + usb_req->status = -EINPROGRESS;
  56698. + usb_req->actual = 0;
  56699. +
  56700. + ep = ep_from_handle(pcd, usb_ep);
  56701. + if (ep == NULL)
  56702. + is_isoc_ep = 0;
  56703. + else
  56704. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  56705. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56706. + dma_addr = usb_req->dma;
  56707. +#else
  56708. + if (GET_CORE_IF(pcd)->dma_enable) {
  56709. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  56710. + struct device *dev = NULL;
  56711. +
  56712. + if (otg_dev != NULL)
  56713. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  56714. +
  56715. + if (usb_req->length != 0 &&
  56716. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  56717. + dma_addr = dma_map_single(dev, usb_req->buf,
  56718. + usb_req->length,
  56719. + ep->dwc_ep.is_in ?
  56720. + DMA_TO_DEVICE:
  56721. + DMA_FROM_DEVICE);
  56722. + }
  56723. + }
  56724. +#endif
  56725. +
  56726. +#ifdef DWC_UTE_PER_IO
  56727. + if (is_isoc_ep == 1) {
  56728. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  56729. + usb_req->length, usb_req->zero, usb_req,
  56730. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  56731. + if (retval)
  56732. + return -EINVAL;
  56733. +
  56734. + return 0;
  56735. + }
  56736. +#endif
  56737. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  56738. + usb_req->length, usb_req->zero, usb_req,
  56739. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  56740. + if (retval) {
  56741. + return -EINVAL;
  56742. + }
  56743. +
  56744. + return 0;
  56745. +}
  56746. +
  56747. +/**
  56748. + * This function cancels an I/O request from an EP.
  56749. + */
  56750. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  56751. +{
  56752. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  56753. +
  56754. + if (!usb_ep || !usb_req) {
  56755. + DWC_WARN("bad argument\n");
  56756. + return -EINVAL;
  56757. + }
  56758. + if (!gadget_wrapper->driver ||
  56759. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56760. + DWC_WARN("bogus device state\n");
  56761. + return -ESHUTDOWN;
  56762. + }
  56763. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  56764. + return -EINVAL;
  56765. + }
  56766. +
  56767. + return 0;
  56768. +}
  56769. +
  56770. +/**
  56771. + * usb_ep_set_halt stalls an endpoint.
  56772. + *
  56773. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  56774. + * toggle.
  56775. + *
  56776. + * Both of these functions are implemented with the same underlying
  56777. + * function. The behavior depends on the value argument.
  56778. + *
  56779. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  56780. + * @param[in] value
  56781. + * - 0 means clear_halt.
  56782. + * - 1 means set_halt,
  56783. + * - 2 means clear stall lock flag.
  56784. + * - 3 means set stall lock flag.
  56785. + */
  56786. +static int ep_halt(struct usb_ep *usb_ep, int value)
  56787. +{
  56788. + int retval = 0;
  56789. +
  56790. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  56791. +
  56792. + if (!usb_ep) {
  56793. + DWC_WARN("bad ep\n");
  56794. + return -EINVAL;
  56795. + }
  56796. +
  56797. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  56798. + if (retval == -DWC_E_AGAIN) {
  56799. + return -EAGAIN;
  56800. + } else if (retval) {
  56801. + retval = -EINVAL;
  56802. + }
  56803. +
  56804. + return retval;
  56805. +}
  56806. +
  56807. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  56808. +#if 0
  56809. +/**
  56810. + * ep_wedge: sets the halt feature and ignores clear requests
  56811. + *
  56812. + * @usb_ep: the endpoint being wedged
  56813. + *
  56814. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  56815. + * requests. If the gadget driver clears the halt status, it will
  56816. + * automatically unwedge the endpoint.
  56817. + *
  56818. + * Returns zero on success, else negative errno. *
  56819. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  56820. + */
  56821. +static int ep_wedge(struct usb_ep *usb_ep)
  56822. +{
  56823. + int retval = 0;
  56824. +
  56825. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  56826. +
  56827. + if (!usb_ep) {
  56828. + DWC_WARN("bad ep\n");
  56829. + return -EINVAL;
  56830. + }
  56831. +
  56832. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  56833. + if (retval == -DWC_E_AGAIN) {
  56834. + retval = -EAGAIN;
  56835. + } else if (retval) {
  56836. + retval = -EINVAL;
  56837. + }
  56838. +
  56839. + return retval;
  56840. +}
  56841. +#endif
  56842. +
  56843. +#ifdef DWC_EN_ISOC
  56844. +/**
  56845. + * This function is used to submit an ISOC Transfer Request to an EP.
  56846. + *
  56847. + * - Every time a sync period completes the request's completion callback
  56848. + * is called to provide data to the gadget driver.
  56849. + * - Once submitted the request cannot be modified.
  56850. + * - Each request is turned into periodic data packets untill ISO
  56851. + * Transfer is stopped..
  56852. + */
  56853. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  56854. + gfp_t gfp_flags)
  56855. +{
  56856. + int retval = 0;
  56857. +
  56858. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  56859. + DWC_WARN("bad params\n");
  56860. + return -EINVAL;
  56861. + }
  56862. +
  56863. + if (!usb_ep) {
  56864. + DWC_PRINTF("bad params\n");
  56865. + return -EINVAL;
  56866. + }
  56867. +
  56868. + req->status = -EINPROGRESS;
  56869. +
  56870. + retval =
  56871. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  56872. + req->buf1, req->dma0, req->dma1,
  56873. + req->sync_frame, req->data_pattern_frame,
  56874. + req->data_per_frame,
  56875. + req->
  56876. + flags & USB_REQ_ISO_ASAP ? -1 :
  56877. + req->start_frame, req->buf_proc_intrvl,
  56878. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  56879. +
  56880. + if (retval) {
  56881. + return -EINVAL;
  56882. + }
  56883. +
  56884. + return retval;
  56885. +}
  56886. +
  56887. +/**
  56888. + * This function stops ISO EP Periodic Data Transfer.
  56889. + */
  56890. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  56891. +{
  56892. + int retval = 0;
  56893. + if (!usb_ep) {
  56894. + DWC_WARN("bad ep\n");
  56895. + }
  56896. +
  56897. + if (!gadget_wrapper->driver ||
  56898. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56899. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  56900. + gadget_wrapper->gadget.speed);
  56901. + DWC_WARN("bogus device state\n");
  56902. + }
  56903. +
  56904. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  56905. + if (retval) {
  56906. + retval = -EINVAL;
  56907. + }
  56908. +
  56909. + return retval;
  56910. +}
  56911. +
  56912. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  56913. + int packets, gfp_t gfp_flags)
  56914. +{
  56915. + struct usb_iso_request *pReq = NULL;
  56916. + uint32_t req_size;
  56917. +
  56918. + req_size = sizeof(struct usb_iso_request);
  56919. + req_size +=
  56920. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  56921. +
  56922. + pReq = kmalloc(req_size, gfp_flags);
  56923. + if (!pReq) {
  56924. + DWC_WARN("Can't allocate Iso Request\n");
  56925. + return 0;
  56926. + }
  56927. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  56928. +
  56929. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  56930. +
  56931. + return pReq;
  56932. +}
  56933. +
  56934. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  56935. +{
  56936. + kfree(req);
  56937. +}
  56938. +
  56939. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  56940. + .ep_ops = {
  56941. + .enable = ep_enable,
  56942. + .disable = ep_disable,
  56943. +
  56944. + .alloc_request = dwc_otg_pcd_alloc_request,
  56945. + .free_request = dwc_otg_pcd_free_request,
  56946. +
  56947. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56948. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  56949. + .free_buffer = dwc_otg_pcd_free_buffer,
  56950. +#endif
  56951. +
  56952. + .queue = ep_queue,
  56953. + .dequeue = ep_dequeue,
  56954. +
  56955. + .set_halt = ep_halt,
  56956. + .fifo_status = 0,
  56957. + .fifo_flush = 0,
  56958. + },
  56959. + .iso_ep_start = iso_ep_start,
  56960. + .iso_ep_stop = iso_ep_stop,
  56961. + .alloc_iso_request = alloc_iso_request,
  56962. + .free_iso_request = free_iso_request,
  56963. +};
  56964. +
  56965. +#else
  56966. +
  56967. + int (*enable) (struct usb_ep *ep,
  56968. + const struct usb_endpoint_descriptor *desc);
  56969. + int (*disable) (struct usb_ep *ep);
  56970. +
  56971. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  56972. + gfp_t gfp_flags);
  56973. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  56974. +
  56975. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  56976. + gfp_t gfp_flags);
  56977. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  56978. +
  56979. + int (*set_halt) (struct usb_ep *ep, int value);
  56980. + int (*set_wedge) (struct usb_ep *ep);
  56981. +
  56982. + int (*fifo_status) (struct usb_ep *ep);
  56983. + void (*fifo_flush) (struct usb_ep *ep);
  56984. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  56985. + .enable = ep_enable,
  56986. + .disable = ep_disable,
  56987. +
  56988. + .alloc_request = dwc_otg_pcd_alloc_request,
  56989. + .free_request = dwc_otg_pcd_free_request,
  56990. +
  56991. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56992. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  56993. + .free_buffer = dwc_otg_pcd_free_buffer,
  56994. +#else
  56995. + /* .set_wedge = ep_wedge, */
  56996. + .set_wedge = NULL, /* uses set_halt instead */
  56997. +#endif
  56998. +
  56999. + .queue = ep_queue,
  57000. + .dequeue = ep_dequeue,
  57001. +
  57002. + .set_halt = ep_halt,
  57003. + .fifo_status = 0,
  57004. + .fifo_flush = 0,
  57005. +
  57006. +};
  57007. +
  57008. +#endif /* _EN_ISOC_ */
  57009. +/* Gadget Operations */
  57010. +/**
  57011. + * The following gadget operations will be implemented in the DWC_otg
  57012. + * PCD. Functions in the API that are not described below are not
  57013. + * implemented.
  57014. + *
  57015. + * The Gadget API provides wrapper functions for each of the function
  57016. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  57017. + * wrapper function, which then calls the underlying PCD function. The
  57018. + * following sections are named according to the wrapper functions
  57019. + * (except for ioctl, which doesn't have a wrapper function). Within
  57020. + * each section, the corresponding DWC_otg PCD function name is
  57021. + * specified.
  57022. + *
  57023. + */
  57024. +
  57025. +/**
  57026. + *Gets the USB Frame number of the last SOF.
  57027. + */
  57028. +static int get_frame_number(struct usb_gadget *gadget)
  57029. +{
  57030. + struct gadget_wrapper *d;
  57031. +
  57032. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  57033. +
  57034. + if (gadget == 0) {
  57035. + return -ENODEV;
  57036. + }
  57037. +
  57038. + d = container_of(gadget, struct gadget_wrapper, gadget);
  57039. + return dwc_otg_pcd_get_frame_number(d->pcd);
  57040. +}
  57041. +
  57042. +#ifdef CONFIG_USB_DWC_OTG_LPM
  57043. +static int test_lpm_enabled(struct usb_gadget *gadget)
  57044. +{
  57045. + struct gadget_wrapper *d;
  57046. +
  57047. + d = container_of(gadget, struct gadget_wrapper, gadget);
  57048. +
  57049. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  57050. +}
  57051. +#endif
  57052. +
  57053. +/**
  57054. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  57055. + * session is in progress. If a session is already in progress, but
  57056. + * the device is suspended, remote wakeup signaling is started.
  57057. + *
  57058. + */
  57059. +static int wakeup(struct usb_gadget *gadget)
  57060. +{
  57061. + struct gadget_wrapper *d;
  57062. +
  57063. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  57064. +
  57065. + if (gadget == 0) {
  57066. + return -ENODEV;
  57067. + } else {
  57068. + d = container_of(gadget, struct gadget_wrapper, gadget);
  57069. + }
  57070. + dwc_otg_pcd_wakeup(d->pcd);
  57071. + return 0;
  57072. +}
  57073. +
  57074. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  57075. + .get_frame = get_frame_number,
  57076. + .wakeup = wakeup,
  57077. +#ifdef CONFIG_USB_DWC_OTG_LPM
  57078. + .lpm_support = test_lpm_enabled,
  57079. +#endif
  57080. + // current versions must always be self-powered
  57081. +};
  57082. +
  57083. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  57084. +{
  57085. + int retval = -DWC_E_NOT_SUPPORTED;
  57086. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  57087. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  57088. + (struct usb_ctrlrequest
  57089. + *)bytes);
  57090. + }
  57091. +
  57092. + if (retval == -ENOTSUPP) {
  57093. + retval = -DWC_E_NOT_SUPPORTED;
  57094. + } else if (retval < 0) {
  57095. + retval = -DWC_E_INVALID;
  57096. + }
  57097. +
  57098. + return retval;
  57099. +}
  57100. +
  57101. +#ifdef DWC_EN_ISOC
  57102. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  57103. + void *req_handle, int proc_buf_num)
  57104. +{
  57105. + int i, packet_count;
  57106. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  57107. + struct usb_iso_request *iso_req = req_handle;
  57108. +
  57109. + if (proc_buf_num) {
  57110. + iso_packet = iso_req->iso_packet_desc1;
  57111. + } else {
  57112. + iso_packet = iso_req->iso_packet_desc0;
  57113. + }
  57114. + packet_count =
  57115. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  57116. + for (i = 0; i < packet_count; ++i) {
  57117. + int status;
  57118. + int actual;
  57119. + int offset;
  57120. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  57121. + i, &status, &actual, &offset);
  57122. + switch (status) {
  57123. + case -DWC_E_NO_DATA:
  57124. + status = -ENODATA;
  57125. + break;
  57126. + default:
  57127. + if (status) {
  57128. + DWC_PRINTF("unknown status in isoc packet\n");
  57129. + }
  57130. +
  57131. + }
  57132. + iso_packet[i].status = status;
  57133. + iso_packet[i].offset = offset;
  57134. + iso_packet[i].actual_length = actual;
  57135. + }
  57136. +
  57137. + iso_req->status = 0;
  57138. + iso_req->process_buffer(ep_handle, iso_req);
  57139. +
  57140. + return 0;
  57141. +}
  57142. +#endif /* DWC_EN_ISOC */
  57143. +
  57144. +#ifdef DWC_UTE_PER_IO
  57145. +/**
  57146. + * Copy the contents of the extended request to the Linux usb_request's
  57147. + * extended part and call the gadget's completion.
  57148. + *
  57149. + * @param pcd Pointer to the pcd structure
  57150. + * @param ep_handle Void pointer to the usb_ep structure
  57151. + * @param req_handle Void pointer to the usb_request structure
  57152. + * @param status Request status returned from the portable logic
  57153. + * @param ereq_port Void pointer to the extended request structure
  57154. + * created in the the portable part that contains the
  57155. + * results of the processed iso packets.
  57156. + */
  57157. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  57158. + void *req_handle, int32_t status, void *ereq_port)
  57159. +{
  57160. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  57161. + struct dwc_iso_xreq_port *ereqport = NULL;
  57162. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  57163. + int i;
  57164. + struct usb_request *req;
  57165. + //struct dwc_ute_iso_packet_descriptor *
  57166. + //int status = 0;
  57167. +
  57168. + req = (struct usb_request *)req_handle;
  57169. + ereqorg = &req->ext_req;
  57170. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  57171. + desc_org = ereqorg->per_io_frame_descs;
  57172. +
  57173. + if (req && req->complete) {
  57174. + /* Copy the request data from the portable logic to our request */
  57175. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  57176. + desc_org[i].actual_length =
  57177. + ereqport->per_io_frame_descs[i].actual_length;
  57178. + desc_org[i].status =
  57179. + ereqport->per_io_frame_descs[i].status;
  57180. + }
  57181. +
  57182. + switch (status) {
  57183. + case -DWC_E_SHUTDOWN:
  57184. + req->status = -ESHUTDOWN;
  57185. + break;
  57186. + case -DWC_E_RESTART:
  57187. + req->status = -ECONNRESET;
  57188. + break;
  57189. + case -DWC_E_INVALID:
  57190. + req->status = -EINVAL;
  57191. + break;
  57192. + case -DWC_E_TIMEOUT:
  57193. + req->status = -ETIMEDOUT;
  57194. + break;
  57195. + default:
  57196. + req->status = status;
  57197. + }
  57198. +
  57199. + /* And call the gadget's completion */
  57200. + req->complete(ep_handle, req);
  57201. + }
  57202. +
  57203. + return 0;
  57204. +}
  57205. +#endif /* DWC_UTE_PER_IO */
  57206. +
  57207. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  57208. + void *req_handle, int32_t status, uint32_t actual)
  57209. +{
  57210. + struct usb_request *req = (struct usb_request *)req_handle;
  57211. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  57212. + struct dwc_otg_pcd_ep *ep = NULL;
  57213. +#endif
  57214. +
  57215. + if (req && req->complete) {
  57216. + switch (status) {
  57217. + case -DWC_E_SHUTDOWN:
  57218. + req->status = -ESHUTDOWN;
  57219. + break;
  57220. + case -DWC_E_RESTART:
  57221. + req->status = -ECONNRESET;
  57222. + break;
  57223. + case -DWC_E_INVALID:
  57224. + req->status = -EINVAL;
  57225. + break;
  57226. + case -DWC_E_TIMEOUT:
  57227. + req->status = -ETIMEDOUT;
  57228. + break;
  57229. + default:
  57230. + req->status = status;
  57231. +
  57232. + }
  57233. +
  57234. + req->actual = actual;
  57235. + DWC_SPINUNLOCK(pcd->lock);
  57236. + req->complete(ep_handle, req);
  57237. + DWC_SPINLOCK(pcd->lock);
  57238. + }
  57239. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  57240. + ep = ep_from_handle(pcd, ep_handle);
  57241. + if (GET_CORE_IF(pcd)->dma_enable) {
  57242. + if (req->length != 0) {
  57243. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  57244. + struct device *dev = NULL;
  57245. +
  57246. + if (otg_dev != NULL)
  57247. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  57248. +
  57249. + dma_unmap_single(dev, req->dma, req->length,
  57250. + ep->dwc_ep.is_in ?
  57251. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  57252. + }
  57253. + }
  57254. +#endif
  57255. +
  57256. + return 0;
  57257. +}
  57258. +
  57259. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  57260. +{
  57261. + gadget_wrapper->gadget.speed = speed;
  57262. + return 0;
  57263. +}
  57264. +
  57265. +static int _disconnect(dwc_otg_pcd_t * pcd)
  57266. +{
  57267. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  57268. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  57269. + }
  57270. + return 0;
  57271. +}
  57272. +
  57273. +static int _resume(dwc_otg_pcd_t * pcd)
  57274. +{
  57275. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  57276. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  57277. + }
  57278. +
  57279. + return 0;
  57280. +}
  57281. +
  57282. +static int _suspend(dwc_otg_pcd_t * pcd)
  57283. +{
  57284. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  57285. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  57286. + }
  57287. + return 0;
  57288. +}
  57289. +
  57290. +/**
  57291. + * This function updates the otg values in the gadget structure.
  57292. + */
  57293. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  57294. +{
  57295. +
  57296. + if (!gadget_wrapper->gadget.is_otg)
  57297. + return 0;
  57298. +
  57299. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  57300. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  57301. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  57302. + return 0;
  57303. +}
  57304. +
  57305. +static int _reset(dwc_otg_pcd_t * pcd)
  57306. +{
  57307. + return 0;
  57308. +}
  57309. +
  57310. +#ifdef DWC_UTE_CFI
  57311. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  57312. +{
  57313. + int retval = -DWC_E_INVALID;
  57314. + if (gadget_wrapper->driver->cfi_feature_setup) {
  57315. + retval =
  57316. + gadget_wrapper->driver->
  57317. + cfi_feature_setup(&gadget_wrapper->gadget,
  57318. + (struct cfi_usb_ctrlrequest *)cfi_req);
  57319. + }
  57320. +
  57321. + return retval;
  57322. +}
  57323. +#endif
  57324. +
  57325. +static const struct dwc_otg_pcd_function_ops fops = {
  57326. + .complete = _complete,
  57327. +#ifdef DWC_EN_ISOC
  57328. + .isoc_complete = _isoc_complete,
  57329. +#endif
  57330. + .setup = _setup,
  57331. + .disconnect = _disconnect,
  57332. + .connect = _connect,
  57333. + .resume = _resume,
  57334. + .suspend = _suspend,
  57335. + .hnp_changed = _hnp_changed,
  57336. + .reset = _reset,
  57337. +#ifdef DWC_UTE_CFI
  57338. + .cfi_setup = _cfi_setup,
  57339. +#endif
  57340. +#ifdef DWC_UTE_PER_IO
  57341. + .xisoc_complete = _xisoc_complete,
  57342. +#endif
  57343. +};
  57344. +
  57345. +/**
  57346. + * This function is the top level PCD interrupt handler.
  57347. + */
  57348. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  57349. +{
  57350. + dwc_otg_pcd_t *pcd = dev;
  57351. + int32_t retval = IRQ_NONE;
  57352. +
  57353. + retval = dwc_otg_pcd_handle_intr(pcd);
  57354. + if (retval != 0) {
  57355. + S3C2410X_CLEAR_EINTPEND();
  57356. + }
  57357. + return IRQ_RETVAL(retval);
  57358. +}
  57359. +
  57360. +/**
  57361. + * This function initialized the usb_ep structures to there default
  57362. + * state.
  57363. + *
  57364. + * @param d Pointer on gadget_wrapper.
  57365. + */
  57366. +void gadget_add_eps(struct gadget_wrapper *d)
  57367. +{
  57368. + static const char *names[] = {
  57369. +
  57370. + "ep0",
  57371. + "ep1in",
  57372. + "ep2in",
  57373. + "ep3in",
  57374. + "ep4in",
  57375. + "ep5in",
  57376. + "ep6in",
  57377. + "ep7in",
  57378. + "ep8in",
  57379. + "ep9in",
  57380. + "ep10in",
  57381. + "ep11in",
  57382. + "ep12in",
  57383. + "ep13in",
  57384. + "ep14in",
  57385. + "ep15in",
  57386. + "ep1out",
  57387. + "ep2out",
  57388. + "ep3out",
  57389. + "ep4out",
  57390. + "ep5out",
  57391. + "ep6out",
  57392. + "ep7out",
  57393. + "ep8out",
  57394. + "ep9out",
  57395. + "ep10out",
  57396. + "ep11out",
  57397. + "ep12out",
  57398. + "ep13out",
  57399. + "ep14out",
  57400. + "ep15out"
  57401. + };
  57402. +
  57403. + int i;
  57404. + struct usb_ep *ep;
  57405. + int8_t dev_endpoints;
  57406. +
  57407. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  57408. +
  57409. + INIT_LIST_HEAD(&d->gadget.ep_list);
  57410. + d->gadget.ep0 = &d->ep0;
  57411. + d->gadget.speed = USB_SPEED_UNKNOWN;
  57412. +
  57413. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  57414. +
  57415. + /**
  57416. + * Initialize the EP0 structure.
  57417. + */
  57418. + ep = &d->ep0;
  57419. +
  57420. + /* Init the usb_ep structure. */
  57421. + ep->name = names[0];
  57422. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57423. +
  57424. + /**
  57425. + * @todo NGS: What should the max packet size be set to
  57426. + * here? Before EP type is set?
  57427. + */
  57428. + ep->maxpacket = MAX_PACKET_SIZE;
  57429. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  57430. +
  57431. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57432. +
  57433. + /**
  57434. + * Initialize the EP structures.
  57435. + */
  57436. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  57437. +
  57438. + for (i = 0; i < dev_endpoints; i++) {
  57439. + ep = &d->in_ep[i];
  57440. +
  57441. + /* Init the usb_ep structure. */
  57442. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  57443. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57444. +
  57445. + /**
  57446. + * @todo NGS: What should the max packet size be set to
  57447. + * here? Before EP type is set?
  57448. + */
  57449. + ep->maxpacket = MAX_PACKET_SIZE;
  57450. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57451. + }
  57452. +
  57453. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  57454. +
  57455. + for (i = 0; i < dev_endpoints; i++) {
  57456. + ep = &d->out_ep[i];
  57457. +
  57458. + /* Init the usb_ep structure. */
  57459. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  57460. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57461. +
  57462. + /**
  57463. + * @todo NGS: What should the max packet size be set to
  57464. + * here? Before EP type is set?
  57465. + */
  57466. + ep->maxpacket = MAX_PACKET_SIZE;
  57467. +
  57468. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57469. + }
  57470. +
  57471. + /* remove ep0 from the list. There is a ep0 pointer. */
  57472. + list_del_init(&d->ep0.ep_list);
  57473. +
  57474. + d->ep0.maxpacket = MAX_EP0_SIZE;
  57475. +}
  57476. +
  57477. +/**
  57478. + * This function releases the Gadget device.
  57479. + * required by device_unregister().
  57480. + *
  57481. + * @todo Should this do something? Should it free the PCD?
  57482. + */
  57483. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  57484. +{
  57485. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  57486. +}
  57487. +
  57488. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  57489. +{
  57490. + static char pcd_name[] = "dwc_otg_pcd";
  57491. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57492. + struct gadget_wrapper *d;
  57493. + int retval;
  57494. +
  57495. + d = DWC_ALLOC(sizeof(*d));
  57496. + if (d == NULL) {
  57497. + return NULL;
  57498. + }
  57499. +
  57500. + memset(d, 0, sizeof(*d));
  57501. +
  57502. + d->gadget.name = pcd_name;
  57503. + d->pcd = otg_dev->pcd;
  57504. +
  57505. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  57506. + strcpy(d->gadget.dev.bus_id, "gadget");
  57507. +#else
  57508. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  57509. +#endif
  57510. +
  57511. + d->gadget.dev.parent = &_dev->dev;
  57512. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  57513. + d->gadget.ops = &dwc_otg_pcd_ops;
  57514. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  57515. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  57516. +
  57517. + d->driver = 0;
  57518. + /* Register the gadget device */
  57519. + retval = device_register(&d->gadget.dev);
  57520. + if (retval != 0) {
  57521. + DWC_ERROR("device_register failed\n");
  57522. + DWC_FREE(d);
  57523. + return NULL;
  57524. + }
  57525. +
  57526. + return d;
  57527. +}
  57528. +
  57529. +static void free_wrapper(struct gadget_wrapper *d)
  57530. +{
  57531. + if (d->driver) {
  57532. + /* should have been done already by driver model core */
  57533. + DWC_WARN("driver '%s' is still registered\n",
  57534. + d->driver->driver.name);
  57535. +#ifdef CONFIG_USB_GADGET
  57536. + usb_gadget_unregister_driver(d->driver);
  57537. +#endif
  57538. + }
  57539. +
  57540. + device_unregister(&d->gadget.dev);
  57541. + DWC_FREE(d);
  57542. +}
  57543. +
  57544. +/**
  57545. + * This function initialized the PCD portion of the driver.
  57546. + *
  57547. + */
  57548. +int pcd_init(dwc_bus_dev_t *_dev)
  57549. +{
  57550. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57551. + int retval = 0;
  57552. +
  57553. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  57554. +
  57555. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev);
  57556. +
  57557. + if (!otg_dev->pcd) {
  57558. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  57559. + return -ENOMEM;
  57560. + }
  57561. +
  57562. + otg_dev->pcd->otg_dev = otg_dev;
  57563. + gadget_wrapper = alloc_wrapper(_dev);
  57564. +
  57565. + /*
  57566. + * Initialize EP structures
  57567. + */
  57568. + gadget_add_eps(gadget_wrapper);
  57569. + /*
  57570. + * Setup interupt handler
  57571. + */
  57572. +#ifdef PLATFORM_INTERFACE
  57573. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  57574. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  57575. + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
  57576. + IRQF_SHARED, gadget_wrapper->gadget.name,
  57577. + otg_dev->pcd);
  57578. + if (retval != 0) {
  57579. + DWC_ERROR("request of irq%d failed\n",
  57580. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  57581. + free_wrapper(gadget_wrapper);
  57582. + return -EBUSY;
  57583. + }
  57584. +#else
  57585. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  57586. + _dev->irq);
  57587. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  57588. + IRQF_SHARED | IRQF_DISABLED,
  57589. + gadget_wrapper->gadget.name, otg_dev->pcd);
  57590. + if (retval != 0) {
  57591. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  57592. + free_wrapper(gadget_wrapper);
  57593. + return -EBUSY;
  57594. + }
  57595. +#endif
  57596. +
  57597. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  57598. +
  57599. + return retval;
  57600. +}
  57601. +
  57602. +/**
  57603. + * Cleanup the PCD.
  57604. + */
  57605. +void pcd_remove(dwc_bus_dev_t *_dev)
  57606. +{
  57607. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57608. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  57609. +
  57610. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  57611. +
  57612. + /*
  57613. + * Free the IRQ
  57614. + */
  57615. +#ifdef PLATFORM_INTERFACE
  57616. + free_irq(platform_get_irq(_dev, 0), pcd);
  57617. +#else
  57618. + free_irq(_dev->irq, pcd);
  57619. +#endif
  57620. + dwc_otg_pcd_remove(otg_dev->pcd);
  57621. + free_wrapper(gadget_wrapper);
  57622. + otg_dev->pcd = 0;
  57623. +}
  57624. +
  57625. +#endif /* DWC_HOST_ONLY */
  57626. --- /dev/null
  57627. +++ b/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  57628. @@ -0,0 +1,2550 @@
  57629. +/* ==========================================================================
  57630. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  57631. + * $Revision: #98 $
  57632. + * $Date: 2012/08/10 $
  57633. + * $Change: 2047372 $
  57634. + *
  57635. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  57636. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  57637. + * otherwise expressly agreed to in writing between Synopsys and you.
  57638. + *
  57639. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  57640. + * any End User Software License Agreement or Agreement for Licensed Product
  57641. + * with Synopsys or any supplement thereto. You are permitted to use and
  57642. + * redistribute this Software in source and binary forms, with or without
  57643. + * modification, provided that redistributions of source code must retain this
  57644. + * notice. You may not view, use, disclose, copy or distribute this file or
  57645. + * any information contained herein except pursuant to this license grant from
  57646. + * Synopsys. If you do not agree with this notice, including the disclaimer
  57647. + * below, then you are not authorized to use the Software.
  57648. + *
  57649. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  57650. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57651. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57652. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  57653. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57654. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57655. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57656. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57657. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57658. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57659. + * DAMAGE.
  57660. + * ========================================================================== */
  57661. +
  57662. +#ifndef __DWC_OTG_REGS_H__
  57663. +#define __DWC_OTG_REGS_H__
  57664. +
  57665. +#include "dwc_otg_core_if.h"
  57666. +
  57667. +/**
  57668. + * @file
  57669. + *
  57670. + * This file contains the data structures for accessing the DWC_otg core registers.
  57671. + *
  57672. + * The application interfaces with the HS OTG core by reading from and
  57673. + * writing to the Control and Status Register (CSR) space through the
  57674. + * AHB Slave interface. These registers are 32 bits wide, and the
  57675. + * addresses are 32-bit-block aligned.
  57676. + * CSRs are classified as follows:
  57677. + * - Core Global Registers
  57678. + * - Device Mode Registers
  57679. + * - Device Global Registers
  57680. + * - Device Endpoint Specific Registers
  57681. + * - Host Mode Registers
  57682. + * - Host Global Registers
  57683. + * - Host Port CSRs
  57684. + * - Host Channel Specific Registers
  57685. + *
  57686. + * Only the Core Global registers can be accessed in both Device and
  57687. + * Host modes. When the HS OTG core is operating in one mode, either
  57688. + * Device or Host, the application must not access registers from the
  57689. + * other mode. When the core switches from one mode to another, the
  57690. + * registers in the new mode of operation must be reprogrammed as they
  57691. + * would be after a power-on reset.
  57692. + */
  57693. +
  57694. +/****************************************************************************/
  57695. +/** DWC_otg Core registers .
  57696. + * The dwc_otg_core_global_regs structure defines the size
  57697. + * and relative field offsets for the Core Global registers.
  57698. + */
  57699. +typedef struct dwc_otg_core_global_regs {
  57700. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  57701. + volatile uint32_t gotgctl;
  57702. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  57703. + volatile uint32_t gotgint;
  57704. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  57705. + volatile uint32_t gahbcfg;
  57706. +
  57707. +#define DWC_GLBINTRMASK 0x0001
  57708. +#define DWC_DMAENABLE 0x0020
  57709. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  57710. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  57711. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  57712. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  57713. +
  57714. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  57715. + volatile uint32_t gusbcfg;
  57716. + /**Core Reset Register. <i>Offset: 010h</i> */
  57717. + volatile uint32_t grstctl;
  57718. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  57719. + volatile uint32_t gintsts;
  57720. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  57721. + volatile uint32_t gintmsk;
  57722. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  57723. + volatile uint32_t grxstsr;
  57724. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  57725. + volatile uint32_t grxstsp;
  57726. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  57727. + volatile uint32_t grxfsiz;
  57728. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  57729. + volatile uint32_t gnptxfsiz;
  57730. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  57731. + * Only). <i>Offset: 02Ch</i> */
  57732. + volatile uint32_t gnptxsts;
  57733. + /**I2C Access Register. <i>Offset: 030h</i> */
  57734. + volatile uint32_t gi2cctl;
  57735. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  57736. + volatile uint32_t gpvndctl;
  57737. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  57738. + volatile uint32_t ggpio;
  57739. + /**User ID Register. <i>Offset: 03Ch</i> */
  57740. + volatile uint32_t guid;
  57741. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  57742. + volatile uint32_t gsnpsid;
  57743. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  57744. + volatile uint32_t ghwcfg1;
  57745. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  57746. + volatile uint32_t ghwcfg2;
  57747. +#define DWC_SLAVE_ONLY_ARCH 0
  57748. +#define DWC_EXT_DMA_ARCH 1
  57749. +#define DWC_INT_DMA_ARCH 2
  57750. +
  57751. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  57752. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  57753. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  57754. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  57755. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  57756. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  57757. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  57758. +
  57759. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  57760. + volatile uint32_t ghwcfg3;
  57761. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  57762. + volatile uint32_t ghwcfg4;
  57763. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  57764. + volatile uint32_t glpmcfg;
  57765. + /** Global PowerDn Register <i>Offset: 058h</i> */
  57766. + volatile uint32_t gpwrdn;
  57767. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  57768. + volatile uint32_t gdfifocfg;
  57769. + /** ADP Control Register <i>Offset: 060h</i> */
  57770. + volatile uint32_t adpctl;
  57771. + /** Reserved <i>Offset: 064h-0FFh</i> */
  57772. + volatile uint32_t reserved39[39];
  57773. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  57774. + volatile uint32_t hptxfsiz;
  57775. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  57776. + otherwise Device Transmit FIFO#n Register.
  57777. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  57778. + volatile uint32_t dtxfsiz[15];
  57779. +} dwc_otg_core_global_regs_t;
  57780. +
  57781. +/**
  57782. + * This union represents the bit fields of the Core OTG Control
  57783. + * and Status Register (GOTGCTL). Set the bits using the bit
  57784. + * fields then write the <i>d32</i> value to the register.
  57785. + */
  57786. +typedef union gotgctl_data {
  57787. + /** raw register data */
  57788. + uint32_t d32;
  57789. + /** register bits */
  57790. + struct {
  57791. + unsigned sesreqscs:1;
  57792. + unsigned sesreq:1;
  57793. + unsigned vbvalidoven:1;
  57794. + unsigned vbvalidovval:1;
  57795. + unsigned avalidoven:1;
  57796. + unsigned avalidovval:1;
  57797. + unsigned bvalidoven:1;
  57798. + unsigned bvalidovval:1;
  57799. + unsigned hstnegscs:1;
  57800. + unsigned hnpreq:1;
  57801. + unsigned hstsethnpen:1;
  57802. + unsigned devhnpen:1;
  57803. + unsigned reserved12_15:4;
  57804. + unsigned conidsts:1;
  57805. + unsigned dbnctime:1;
  57806. + unsigned asesvld:1;
  57807. + unsigned bsesvld:1;
  57808. + unsigned otgver:1;
  57809. + unsigned reserved1:1;
  57810. + unsigned multvalidbc:5;
  57811. + unsigned chirpen:1;
  57812. + unsigned reserved28_31:4;
  57813. + } b;
  57814. +} gotgctl_data_t;
  57815. +
  57816. +/**
  57817. + * This union represents the bit fields of the Core OTG Interrupt Register
  57818. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  57819. + * value to the register.
  57820. + */
  57821. +typedef union gotgint_data {
  57822. + /** raw register data */
  57823. + uint32_t d32;
  57824. + /** register bits */
  57825. + struct {
  57826. + /** Current Mode */
  57827. + unsigned reserved0_1:2;
  57828. +
  57829. + /** Session End Detected */
  57830. + unsigned sesenddet:1;
  57831. +
  57832. + unsigned reserved3_7:5;
  57833. +
  57834. + /** Session Request Success Status Change */
  57835. + unsigned sesreqsucstschng:1;
  57836. + /** Host Negotiation Success Status Change */
  57837. + unsigned hstnegsucstschng:1;
  57838. +
  57839. + unsigned reserved10_16:7;
  57840. +
  57841. + /** Host Negotiation Detected */
  57842. + unsigned hstnegdet:1;
  57843. + /** A-Device Timeout Change */
  57844. + unsigned adevtoutchng:1;
  57845. + /** Debounce Done */
  57846. + unsigned debdone:1;
  57847. + /** Multi-Valued input changed */
  57848. + unsigned mvic:1;
  57849. +
  57850. + unsigned reserved31_21:11;
  57851. +
  57852. + } b;
  57853. +} gotgint_data_t;
  57854. +
  57855. +/**
  57856. + * This union represents the bit fields of the Core AHB Configuration
  57857. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  57858. + * write the <i>d32</i> value to the register.
  57859. + */
  57860. +typedef union gahbcfg_data {
  57861. + /** raw register data */
  57862. + uint32_t d32;
  57863. + /** register bits */
  57864. + struct {
  57865. + unsigned glblintrmsk:1;
  57866. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  57867. +
  57868. + unsigned hburstlen:4;
  57869. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  57870. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  57871. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  57872. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  57873. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  57874. +
  57875. + unsigned dmaenable:1;
  57876. +#define DWC_GAHBCFG_DMAENABLE 1
  57877. + unsigned reserved:1;
  57878. + unsigned nptxfemplvl_txfemplvl:1;
  57879. + unsigned ptxfemplvl:1;
  57880. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  57881. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  57882. + unsigned reserved9_20:12;
  57883. + unsigned remmemsupp:1;
  57884. + unsigned notialldmawrit:1;
  57885. + unsigned ahbsingle:1;
  57886. + unsigned reserved24_31:8;
  57887. + } b;
  57888. +} gahbcfg_data_t;
  57889. +
  57890. +/**
  57891. + * This union represents the bit fields of the Core USB Configuration
  57892. + * Register (GUSBCFG). Set the bits using the bit fields then write
  57893. + * the <i>d32</i> value to the register.
  57894. + */
  57895. +typedef union gusbcfg_data {
  57896. + /** raw register data */
  57897. + uint32_t d32;
  57898. + /** register bits */
  57899. + struct {
  57900. + unsigned toutcal:3;
  57901. + unsigned phyif:1;
  57902. + unsigned ulpi_utmi_sel:1;
  57903. + unsigned fsintf:1;
  57904. + unsigned physel:1;
  57905. + unsigned ddrsel:1;
  57906. + unsigned srpcap:1;
  57907. + unsigned hnpcap:1;
  57908. + unsigned usbtrdtim:4;
  57909. + unsigned reserved1:1;
  57910. + unsigned phylpwrclksel:1;
  57911. + unsigned otgutmifssel:1;
  57912. + unsigned ulpi_fsls:1;
  57913. + unsigned ulpi_auto_res:1;
  57914. + unsigned ulpi_clk_sus_m:1;
  57915. + unsigned ulpi_ext_vbus_drv:1;
  57916. + unsigned ulpi_int_vbus_indicator:1;
  57917. + unsigned term_sel_dl_pulse:1;
  57918. + unsigned indicator_complement:1;
  57919. + unsigned indicator_pass_through:1;
  57920. + unsigned ulpi_int_prot_dis:1;
  57921. + unsigned ic_usb_cap:1;
  57922. + unsigned ic_traffic_pull_remove:1;
  57923. + unsigned tx_end_delay:1;
  57924. + unsigned force_host_mode:1;
  57925. + unsigned force_dev_mode:1;
  57926. + unsigned reserved31:1;
  57927. + } b;
  57928. +} gusbcfg_data_t;
  57929. +
  57930. +/**
  57931. + * This union represents the bit fields of the Core Reset Register
  57932. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  57933. + * <i>d32</i> value to the register.
  57934. + */
  57935. +typedef union grstctl_data {
  57936. + /** raw register data */
  57937. + uint32_t d32;
  57938. + /** register bits */
  57939. + struct {
  57940. + /** Core Soft Reset (CSftRst) (Device and Host)
  57941. + *
  57942. + * The application can flush the control logic in the
  57943. + * entire core using this bit. This bit resets the
  57944. + * pipelines in the AHB Clock domain as well as the
  57945. + * PHY Clock domain.
  57946. + *
  57947. + * The state machines are reset to an IDLE state, the
  57948. + * control bits in the CSRs are cleared, all the
  57949. + * transmit FIFOs and the receive FIFO are flushed.
  57950. + *
  57951. + * The status mask bits that control the generation of
  57952. + * the interrupt, are cleared, to clear the
  57953. + * interrupt. The interrupt status bits are not
  57954. + * cleared, so the application can get the status of
  57955. + * any events that occurred in the core after it has
  57956. + * set this bit.
  57957. + *
  57958. + * Any transactions on the AHB are terminated as soon
  57959. + * as possible following the protocol. Any
  57960. + * transactions on the USB are terminated immediately.
  57961. + *
  57962. + * The configuration settings in the CSRs are
  57963. + * unchanged, so the software doesn't have to
  57964. + * reprogram these registers (Device
  57965. + * Configuration/Host Configuration/Core System
  57966. + * Configuration/Core PHY Configuration).
  57967. + *
  57968. + * The application can write to this bit, any time it
  57969. + * wants to reset the core. This is a self clearing
  57970. + * bit and the core clears this bit after all the
  57971. + * necessary logic is reset in the core, which may
  57972. + * take several clocks, depending on the current state
  57973. + * of the core.
  57974. + */
  57975. + unsigned csftrst:1;
  57976. + /** Hclk Soft Reset
  57977. + *
  57978. + * The application uses this bit to reset the control logic in
  57979. + * the AHB clock domain. Only AHB clock domain pipelines are
  57980. + * reset.
  57981. + */
  57982. + unsigned hsftrst:1;
  57983. + /** Host Frame Counter Reset (Host Only)<br>
  57984. + *
  57985. + * The application can reset the (micro)frame number
  57986. + * counter inside the core, using this bit. When the
  57987. + * (micro)frame counter is reset, the subsequent SOF
  57988. + * sent out by the core, will have a (micro)frame
  57989. + * number of 0.
  57990. + */
  57991. + unsigned hstfrm:1;
  57992. + /** In Token Sequence Learning Queue Flush
  57993. + * (INTknQFlsh) (Device Only)
  57994. + */
  57995. + unsigned intknqflsh:1;
  57996. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  57997. + *
  57998. + * The application can flush the entire Receive FIFO
  57999. + * using this bit. The application must first
  58000. + * ensure that the core is not in the middle of a
  58001. + * transaction. The application should write into
  58002. + * this bit, only after making sure that neither the
  58003. + * DMA engine is reading from the RxFIFO nor the MAC
  58004. + * is writing the data in to the FIFO. The
  58005. + * application should wait until the bit is cleared
  58006. + * before performing any other operations. This bit
  58007. + * will takes 8 clocks (slowest of PHY or AHB clock)
  58008. + * to clear.
  58009. + */
  58010. + unsigned rxfflsh:1;
  58011. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  58012. + *
  58013. + * This bit is used to selectively flush a single or
  58014. + * all transmit FIFOs. The application must first
  58015. + * ensure that the core is not in the middle of a
  58016. + * transaction. The application should write into
  58017. + * this bit, only after making sure that neither the
  58018. + * DMA engine is writing into the TxFIFO nor the MAC
  58019. + * is reading the data out of the FIFO. The
  58020. + * application should wait until the core clears this
  58021. + * bit, before performing any operations. This bit
  58022. + * will takes 8 clocks (slowest of PHY or AHB clock)
  58023. + * to clear.
  58024. + */
  58025. + unsigned txfflsh:1;
  58026. +
  58027. + /** TxFIFO Number (TxFNum) (Device and Host).
  58028. + *
  58029. + * This is the FIFO number which needs to be flushed,
  58030. + * using the TxFIFO Flush bit. This field should not
  58031. + * be changed until the TxFIFO Flush bit is cleared by
  58032. + * the core.
  58033. + * - 0x0 : Non Periodic TxFIFO Flush
  58034. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  58035. + * or Periodic TxFIFO in host mode
  58036. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  58037. + * - ...
  58038. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  58039. + * - 0x10: Flush all the Transmit NonPeriodic and
  58040. + * Transmit Periodic FIFOs in the core
  58041. + */
  58042. + unsigned txfnum:5;
  58043. + /** Reserved */
  58044. + unsigned reserved11_29:19;
  58045. + /** DMA Request Signal. Indicated DMA request is in
  58046. + * probress. Used for debug purpose. */
  58047. + unsigned dmareq:1;
  58048. + /** AHB Master Idle. Indicates the AHB Master State
  58049. + * Machine is in IDLE condition. */
  58050. + unsigned ahbidle:1;
  58051. + } b;
  58052. +} grstctl_t;
  58053. +
  58054. +/**
  58055. + * This union represents the bit fields of the Core Interrupt Mask
  58056. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  58057. + * write the <i>d32</i> value to the register.
  58058. + */
  58059. +typedef union gintmsk_data {
  58060. + /** raw register data */
  58061. + uint32_t d32;
  58062. + /** register bits */
  58063. + struct {
  58064. + unsigned reserved0:1;
  58065. + unsigned modemismatch:1;
  58066. + unsigned otgintr:1;
  58067. + unsigned sofintr:1;
  58068. + unsigned rxstsqlvl:1;
  58069. + unsigned nptxfempty:1;
  58070. + unsigned ginnakeff:1;
  58071. + unsigned goutnakeff:1;
  58072. + unsigned ulpickint:1;
  58073. + unsigned i2cintr:1;
  58074. + unsigned erlysuspend:1;
  58075. + unsigned usbsuspend:1;
  58076. + unsigned usbreset:1;
  58077. + unsigned enumdone:1;
  58078. + unsigned isooutdrop:1;
  58079. + unsigned eopframe:1;
  58080. + unsigned restoredone:1;
  58081. + unsigned epmismatch:1;
  58082. + unsigned inepintr:1;
  58083. + unsigned outepintr:1;
  58084. + unsigned incomplisoin:1;
  58085. + unsigned incomplisoout:1;
  58086. + unsigned fetsusp:1;
  58087. + unsigned resetdet:1;
  58088. + unsigned portintr:1;
  58089. + unsigned hcintr:1;
  58090. + unsigned ptxfempty:1;
  58091. + unsigned lpmtranrcvd:1;
  58092. + unsigned conidstschng:1;
  58093. + unsigned disconnect:1;
  58094. + unsigned sessreqintr:1;
  58095. + unsigned wkupintr:1;
  58096. + } b;
  58097. +} gintmsk_data_t;
  58098. +/**
  58099. + * This union represents the bit fields of the Core Interrupt Register
  58100. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  58101. + * <i>d32</i> value to the register.
  58102. + */
  58103. +typedef union gintsts_data {
  58104. + /** raw register data */
  58105. + uint32_t d32;
  58106. +#define DWC_SOF_INTR_MASK 0x0008
  58107. + /** register bits */
  58108. + struct {
  58109. +#define DWC_HOST_MODE 1
  58110. + unsigned curmode:1;
  58111. + unsigned modemismatch:1;
  58112. + unsigned otgintr:1;
  58113. + unsigned sofintr:1;
  58114. + unsigned rxstsqlvl:1;
  58115. + unsigned nptxfempty:1;
  58116. + unsigned ginnakeff:1;
  58117. + unsigned goutnakeff:1;
  58118. + unsigned ulpickint:1;
  58119. + unsigned i2cintr:1;
  58120. + unsigned erlysuspend:1;
  58121. + unsigned usbsuspend:1;
  58122. + unsigned usbreset:1;
  58123. + unsigned enumdone:1;
  58124. + unsigned isooutdrop:1;
  58125. + unsigned eopframe:1;
  58126. + unsigned restoredone:1;
  58127. + unsigned epmismatch:1;
  58128. + unsigned inepint:1;
  58129. + unsigned outepintr:1;
  58130. + unsigned incomplisoin:1;
  58131. + unsigned incomplisoout:1;
  58132. + unsigned fetsusp:1;
  58133. + unsigned resetdet:1;
  58134. + unsigned portintr:1;
  58135. + unsigned hcintr:1;
  58136. + unsigned ptxfempty:1;
  58137. + unsigned lpmtranrcvd:1;
  58138. + unsigned conidstschng:1;
  58139. + unsigned disconnect:1;
  58140. + unsigned sessreqintr:1;
  58141. + unsigned wkupintr:1;
  58142. + } b;
  58143. +} gintsts_data_t;
  58144. +
  58145. +/**
  58146. + * This union represents the bit fields in the Device Receive Status Read and
  58147. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  58148. + * element then read out the bits using the <i>b</i>it elements.
  58149. + */
  58150. +typedef union device_grxsts_data {
  58151. + /** raw register data */
  58152. + uint32_t d32;
  58153. + /** register bits */
  58154. + struct {
  58155. + unsigned epnum:4;
  58156. + unsigned bcnt:11;
  58157. + unsigned dpid:2;
  58158. +
  58159. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  58160. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  58161. +
  58162. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  58163. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  58164. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  58165. + unsigned pktsts:4;
  58166. + unsigned fn:4;
  58167. + unsigned reserved25_31:7;
  58168. + } b;
  58169. +} device_grxsts_data_t;
  58170. +
  58171. +/**
  58172. + * This union represents the bit fields in the Host Receive Status Read and
  58173. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  58174. + * element then read out the bits using the <i>b</i>it elements.
  58175. + */
  58176. +typedef union host_grxsts_data {
  58177. + /** raw register data */
  58178. + uint32_t d32;
  58179. + /** register bits */
  58180. + struct {
  58181. + unsigned chnum:4;
  58182. + unsigned bcnt:11;
  58183. + unsigned dpid:2;
  58184. +
  58185. + unsigned pktsts:4;
  58186. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  58187. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  58188. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  58189. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  58190. +
  58191. + unsigned reserved21_31:11;
  58192. + } b;
  58193. +} host_grxsts_data_t;
  58194. +
  58195. +/**
  58196. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  58197. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  58198. + * then read out the bits using the <i>b</i>it elements.
  58199. + */
  58200. +typedef union fifosize_data {
  58201. + /** raw register data */
  58202. + uint32_t d32;
  58203. + /** register bits */
  58204. + struct {
  58205. + unsigned startaddr:16;
  58206. + unsigned depth:16;
  58207. + } b;
  58208. +} fifosize_data_t;
  58209. +
  58210. +/**
  58211. + * This union represents the bit fields in the Non-Periodic Transmit
  58212. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  58213. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  58214. + * elements.
  58215. + */
  58216. +typedef union gnptxsts_data {
  58217. + /** raw register data */
  58218. + uint32_t d32;
  58219. + /** register bits */
  58220. + struct {
  58221. + unsigned nptxfspcavail:16;
  58222. + unsigned nptxqspcavail:8;
  58223. + /** Top of the Non-Periodic Transmit Request Queue
  58224. + * - bit 24 - Terminate (Last entry for the selected
  58225. + * channel/EP)
  58226. + * - bits 26:25 - Token Type
  58227. + * - 2'b00 - IN/OUT
  58228. + * - 2'b01 - Zero Length OUT
  58229. + * - 2'b10 - PING/Complete Split
  58230. + * - 2'b11 - Channel Halt
  58231. + * - bits 30:27 - Channel/EP Number
  58232. + */
  58233. + unsigned nptxqtop_terminate:1;
  58234. + unsigned nptxqtop_token:2;
  58235. + unsigned nptxqtop_chnep:4;
  58236. + unsigned reserved:1;
  58237. + } b;
  58238. +} gnptxsts_data_t;
  58239. +
  58240. +/**
  58241. + * This union represents the bit fields in the Transmit
  58242. + * FIFO Status Register (DTXFSTS). Read the register into the
  58243. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  58244. + * elements.
  58245. + */
  58246. +typedef union dtxfsts_data {
  58247. + /** raw register data */
  58248. + uint32_t d32;
  58249. + /** register bits */
  58250. + struct {
  58251. + unsigned txfspcavail:16;
  58252. + unsigned reserved:16;
  58253. + } b;
  58254. +} dtxfsts_data_t;
  58255. +
  58256. +/**
  58257. + * This union represents the bit fields in the I2C Control Register
  58258. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  58259. + * bits using the <i>b</i>it elements.
  58260. + */
  58261. +typedef union gi2cctl_data {
  58262. + /** raw register data */
  58263. + uint32_t d32;
  58264. + /** register bits */
  58265. + struct {
  58266. + unsigned rwdata:8;
  58267. + unsigned regaddr:8;
  58268. + unsigned addr:7;
  58269. + unsigned i2cen:1;
  58270. + unsigned ack:1;
  58271. + unsigned i2csuspctl:1;
  58272. + unsigned i2cdevaddr:2;
  58273. + unsigned i2cdatse0:1;
  58274. + unsigned reserved:1;
  58275. + unsigned rw:1;
  58276. + unsigned bsydne:1;
  58277. + } b;
  58278. +} gi2cctl_data_t;
  58279. +
  58280. +/**
  58281. + * This union represents the bit fields in the PHY Vendor Control Register
  58282. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  58283. + * bits using the <i>b</i>it elements.
  58284. + */
  58285. +typedef union gpvndctl_data {
  58286. + /** raw register data */
  58287. + uint32_t d32;
  58288. + /** register bits */
  58289. + struct {
  58290. + unsigned regdata:8;
  58291. + unsigned vctrl:8;
  58292. + unsigned regaddr16_21:6;
  58293. + unsigned regwr:1;
  58294. + unsigned reserved23_24:2;
  58295. + unsigned newregreq:1;
  58296. + unsigned vstsbsy:1;
  58297. + unsigned vstsdone:1;
  58298. + unsigned reserved28_30:3;
  58299. + unsigned disulpidrvr:1;
  58300. + } b;
  58301. +} gpvndctl_data_t;
  58302. +
  58303. +/**
  58304. + * This union represents the bit fields in the General Purpose
  58305. + * Input/Output Register (GGPIO).
  58306. + * Read the register into the <i>d32</i> element then read out the
  58307. + * bits using the <i>b</i>it elements.
  58308. + */
  58309. +typedef union ggpio_data {
  58310. + /** raw register data */
  58311. + uint32_t d32;
  58312. + /** register bits */
  58313. + struct {
  58314. + unsigned gpi:16;
  58315. + unsigned gpo:16;
  58316. + } b;
  58317. +} ggpio_data_t;
  58318. +
  58319. +/**
  58320. + * This union represents the bit fields in the User ID Register
  58321. + * (GUID). Read the register into the <i>d32</i> element then read out the
  58322. + * bits using the <i>b</i>it elements.
  58323. + */
  58324. +typedef union guid_data {
  58325. + /** raw register data */
  58326. + uint32_t d32;
  58327. + /** register bits */
  58328. + struct {
  58329. + unsigned rwdata:32;
  58330. + } b;
  58331. +} guid_data_t;
  58332. +
  58333. +/**
  58334. + * This union represents the bit fields in the Synopsys ID Register
  58335. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  58336. + * bits using the <i>b</i>it elements.
  58337. + */
  58338. +typedef union gsnpsid_data {
  58339. + /** raw register data */
  58340. + uint32_t d32;
  58341. + /** register bits */
  58342. + struct {
  58343. + unsigned rwdata:32;
  58344. + } b;
  58345. +} gsnpsid_data_t;
  58346. +
  58347. +/**
  58348. + * This union represents the bit fields in the User HW Config1
  58349. + * Register. Read the register into the <i>d32</i> element then read
  58350. + * out the bits using the <i>b</i>it elements.
  58351. + */
  58352. +typedef union hwcfg1_data {
  58353. + /** raw register data */
  58354. + uint32_t d32;
  58355. + /** register bits */
  58356. + struct {
  58357. + unsigned ep_dir0:2;
  58358. + unsigned ep_dir1:2;
  58359. + unsigned ep_dir2:2;
  58360. + unsigned ep_dir3:2;
  58361. + unsigned ep_dir4:2;
  58362. + unsigned ep_dir5:2;
  58363. + unsigned ep_dir6:2;
  58364. + unsigned ep_dir7:2;
  58365. + unsigned ep_dir8:2;
  58366. + unsigned ep_dir9:2;
  58367. + unsigned ep_dir10:2;
  58368. + unsigned ep_dir11:2;
  58369. + unsigned ep_dir12:2;
  58370. + unsigned ep_dir13:2;
  58371. + unsigned ep_dir14:2;
  58372. + unsigned ep_dir15:2;
  58373. + } b;
  58374. +} hwcfg1_data_t;
  58375. +
  58376. +/**
  58377. + * This union represents the bit fields in the User HW Config2
  58378. + * Register. Read the register into the <i>d32</i> element then read
  58379. + * out the bits using the <i>b</i>it elements.
  58380. + */
  58381. +typedef union hwcfg2_data {
  58382. + /** raw register data */
  58383. + uint32_t d32;
  58384. + /** register bits */
  58385. + struct {
  58386. + /* GHWCFG2 */
  58387. + unsigned op_mode:3;
  58388. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  58389. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  58390. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  58391. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  58392. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  58393. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  58394. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  58395. +
  58396. + unsigned architecture:2;
  58397. + unsigned point2point:1;
  58398. + unsigned hs_phy_type:2;
  58399. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  58400. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  58401. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  58402. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  58403. +
  58404. + unsigned fs_phy_type:2;
  58405. + unsigned num_dev_ep:4;
  58406. + unsigned num_host_chan:4;
  58407. + unsigned perio_ep_supported:1;
  58408. + unsigned dynamic_fifo:1;
  58409. + unsigned multi_proc_int:1;
  58410. + unsigned reserved21:1;
  58411. + unsigned nonperio_tx_q_depth:2;
  58412. + unsigned host_perio_tx_q_depth:2;
  58413. + unsigned dev_token_q_depth:5;
  58414. + unsigned otg_enable_ic_usb:1;
  58415. + } b;
  58416. +} hwcfg2_data_t;
  58417. +
  58418. +/**
  58419. + * This union represents the bit fields in the User HW Config3
  58420. + * Register. Read the register into the <i>d32</i> element then read
  58421. + * out the bits using the <i>b</i>it elements.
  58422. + */
  58423. +typedef union hwcfg3_data {
  58424. + /** raw register data */
  58425. + uint32_t d32;
  58426. + /** register bits */
  58427. + struct {
  58428. + /* GHWCFG3 */
  58429. + unsigned xfer_size_cntr_width:4;
  58430. + unsigned packet_size_cntr_width:3;
  58431. + unsigned otg_func:1;
  58432. + unsigned i2c:1;
  58433. + unsigned vendor_ctrl_if:1;
  58434. + unsigned optional_features:1;
  58435. + unsigned synch_reset_type:1;
  58436. + unsigned adp_supp:1;
  58437. + unsigned otg_enable_hsic:1;
  58438. + unsigned bc_support:1;
  58439. + unsigned otg_lpm_en:1;
  58440. + unsigned dfifo_depth:16;
  58441. + } b;
  58442. +} hwcfg3_data_t;
  58443. +
  58444. +/**
  58445. + * This union represents the bit fields in the User HW Config4
  58446. + * Register. Read the register into the <i>d32</i> element then read
  58447. + * out the bits using the <i>b</i>it elements.
  58448. + */
  58449. +typedef union hwcfg4_data {
  58450. + /** raw register data */
  58451. + uint32_t d32;
  58452. + /** register bits */
  58453. + struct {
  58454. + unsigned num_dev_perio_in_ep:4;
  58455. + unsigned power_optimiz:1;
  58456. + unsigned min_ahb_freq:1;
  58457. + unsigned hiber:1;
  58458. + unsigned xhiber:1;
  58459. + unsigned reserved:6;
  58460. + unsigned utmi_phy_data_width:2;
  58461. + unsigned num_dev_mode_ctrl_ep:4;
  58462. + unsigned iddig_filt_en:1;
  58463. + unsigned vbus_valid_filt_en:1;
  58464. + unsigned a_valid_filt_en:1;
  58465. + unsigned b_valid_filt_en:1;
  58466. + unsigned session_end_filt_en:1;
  58467. + unsigned ded_fifo_en:1;
  58468. + unsigned num_in_eps:4;
  58469. + unsigned desc_dma:1;
  58470. + unsigned desc_dma_dyn:1;
  58471. + } b;
  58472. +} hwcfg4_data_t;
  58473. +
  58474. +/**
  58475. + * This union represents the bit fields of the Core LPM Configuration
  58476. + * Register (GLPMCFG). Set the bits using bit fields then write
  58477. + * the <i>d32</i> value to the register.
  58478. + */
  58479. +typedef union glpmctl_data {
  58480. + /** raw register data */
  58481. + uint32_t d32;
  58482. + /** register bits */
  58483. + struct {
  58484. + /** LPM-Capable (LPMCap) (Device and Host)
  58485. + * The application uses this bit to control
  58486. + * the DWC_otg core LPM capabilities.
  58487. + */
  58488. + unsigned lpm_cap_en:1;
  58489. + /** LPM response programmed by application (AppL1Res) (Device)
  58490. + * Handshake response to LPM token pre-programmed
  58491. + * by device application software.
  58492. + */
  58493. + unsigned appl_resp:1;
  58494. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  58495. + * In Host mode this field indicates the value of HIRD
  58496. + * to be sent in an LPM transaction.
  58497. + * In Device mode this field is updated with the
  58498. + * Received LPM Token HIRD bmAttribute
  58499. + * when an ACK/NYET/STALL response is sent
  58500. + * to an LPM transaction.
  58501. + */
  58502. + unsigned hird:4;
  58503. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  58504. + * In Host mode this bit indicates the value of remote
  58505. + * wake up to be sent in wIndex field of LPM transaction.
  58506. + * In Device mode this field is updated with the
  58507. + * Received LPM Token bRemoteWake bmAttribute
  58508. + * when an ACK/NYET/STALL response is sent
  58509. + * to an LPM transaction.
  58510. + */
  58511. + unsigned rem_wkup_en:1;
  58512. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  58513. + * The application uses this bit to control
  58514. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  58515. + */
  58516. + unsigned en_utmi_sleep:1;
  58517. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  58518. + */
  58519. + unsigned hird_thres:5;
  58520. + /** LPM Response (CoreL1Res) (Device and Host)
  58521. + * In Host mode this bit contains handsake response to
  58522. + * LPM transaction.
  58523. + * In Device mode the response of the core to
  58524. + * LPM transaction received is reflected in these two bits.
  58525. + - 0x0 : ERROR (No handshake response)
  58526. + - 0x1 : STALL
  58527. + - 0x2 : NYET
  58528. + - 0x3 : ACK
  58529. + */
  58530. + unsigned lpm_resp:2;
  58531. + /** Port Sleep Status (SlpSts) (Device and Host)
  58532. + * This bit is set as long as a Sleep condition
  58533. + * is present on the USB bus.
  58534. + */
  58535. + unsigned prt_sleep_sts:1;
  58536. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  58537. + * Indicates that the application or host
  58538. + * can start resume from Sleep state.
  58539. + */
  58540. + unsigned sleep_state_resumeok:1;
  58541. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  58542. + * The channel number on which the LPM transaction
  58543. + * has to be applied while sending
  58544. + * an LPM transaction to the local device.
  58545. + */
  58546. + unsigned lpm_chan_index:4;
  58547. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  58548. + * Number host retries that would be performed
  58549. + * if the device response was not valid response.
  58550. + */
  58551. + unsigned retry_count:3;
  58552. + /** Send LPM Transaction (SndLPM) (Host)
  58553. + * When set by application software,
  58554. + * an LPM transaction containing two tokens
  58555. + * is sent.
  58556. + */
  58557. + unsigned send_lpm:1;
  58558. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  58559. + * Number of LPM Host Retries still remaining
  58560. + * to be transmitted for the current LPM sequence
  58561. + */
  58562. + unsigned retry_count_sts:3;
  58563. + unsigned reserved28_29:2;
  58564. + /** In host mode once this bit is set, the host
  58565. + * configures to drive the HSIC Idle state on the bus.
  58566. + * It then waits for the device to initiate the Connect sequence.
  58567. + * In device mode once this bit is set, the device waits for
  58568. + * the HSIC Idle line state on the bus. Upon receving the Idle
  58569. + * line state, it initiates the HSIC Connect sequence.
  58570. + */
  58571. + unsigned hsic_connect:1;
  58572. + /** This bit overrides and functionally inverts
  58573. + * the if_select_hsic input port signal.
  58574. + */
  58575. + unsigned inv_sel_hsic:1;
  58576. + } b;
  58577. +} glpmcfg_data_t;
  58578. +
  58579. +/**
  58580. + * This union represents the bit fields of the Core ADP Timer, Control and
  58581. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  58582. + * the <i>d32</i> value to the register.
  58583. + */
  58584. +typedef union adpctl_data {
  58585. + /** raw register data */
  58586. + uint32_t d32;
  58587. + /** register bits */
  58588. + struct {
  58589. + /** Probe Discharge (PRB_DSCHG)
  58590. + * These bits set the times for TADP_DSCHG.
  58591. + * These bits are defined as follows:
  58592. + * 2'b00 - 4 msec
  58593. + * 2'b01 - 8 msec
  58594. + * 2'b10 - 16 msec
  58595. + * 2'b11 - 32 msec
  58596. + */
  58597. + unsigned prb_dschg:2;
  58598. + /** Probe Delta (PRB_DELTA)
  58599. + * These bits set the resolution for RTIM value.
  58600. + * The bits are defined in units of 32 kHz clock cycles as follows:
  58601. + * 2'b00 - 1 cycles
  58602. + * 2'b01 - 2 cycles
  58603. + * 2'b10 - 3 cycles
  58604. + * 2'b11 - 4 cycles
  58605. + * For example if this value is chosen to 2'b01, it means that RTIM
  58606. + * increments for every 3(three) 32Khz clock cycles.
  58607. + */
  58608. + unsigned prb_delta:2;
  58609. + /** Probe Period (PRB_PER)
  58610. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  58611. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  58612. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  58613. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  58614. + * 2'b11 - Reserved
  58615. + */
  58616. + unsigned prb_per:2;
  58617. + /** These bits capture the latest time it took for VBUS to ramp from
  58618. + * VADP_SINK to VADP_PRB.
  58619. + * 0x000 - 1 cycles
  58620. + * 0x001 - 2 cycles
  58621. + * 0x002 - 3 cycles
  58622. + * etc
  58623. + * 0x7FF - 2048 cycles
  58624. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  58625. + */
  58626. + unsigned rtim:11;
  58627. + /** Enable Probe (EnaPrb)
  58628. + * When programmed to 1'b1, the core performs a probe operation.
  58629. + * This bit is valid only if OTG_Ver = 1'b1.
  58630. + */
  58631. + unsigned enaprb:1;
  58632. + /** Enable Sense (EnaSns)
  58633. + * When programmed to 1'b1, the core performs a Sense operation.
  58634. + * This bit is valid only if OTG_Ver = 1'b1.
  58635. + */
  58636. + unsigned enasns:1;
  58637. + /** ADP Reset (ADPRes)
  58638. + * When set, ADP controller is reset.
  58639. + * This bit is valid only if OTG_Ver = 1'b1.
  58640. + */
  58641. + unsigned adpres:1;
  58642. + /** ADP Enable (ADPEn)
  58643. + * When set, the core performs either ADP probing or sensing
  58644. + * based on EnaPrb or EnaSns.
  58645. + * This bit is valid only if OTG_Ver = 1'b1.
  58646. + */
  58647. + unsigned adpen:1;
  58648. + /** ADP Probe Interrupt (ADP_PRB_INT)
  58649. + * When this bit is set, it means that the VBUS
  58650. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  58651. + * This bit is valid only if OTG_Ver = 1'b1.
  58652. + */
  58653. + unsigned adp_prb_int:1;
  58654. + /**
  58655. + * ADP Sense Interrupt (ADP_SNS_INT)
  58656. + * When this bit is set, it means that the VBUS voltage is greater than
  58657. + * VADP_SNS value or VADP_SNS is reached.
  58658. + * This bit is valid only if OTG_Ver = 1'b1.
  58659. + */
  58660. + unsigned adp_sns_int:1;
  58661. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  58662. + * This bit is relevant only for an ADP probe.
  58663. + * When this bit is set, it means that the ramp time has
  58664. + * completed ie ADPCTL.RTIM has reached its terminal value
  58665. + * of 0x7FF. This is a debug feature that allows software
  58666. + * to read the ramp time after each cycle.
  58667. + * This bit is valid only if OTG_Ver = 1'b1.
  58668. + */
  58669. + unsigned adp_tmout_int:1;
  58670. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  58671. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  58672. + * This bit is valid only if OTG_Ver = 1'b1.
  58673. + */
  58674. + unsigned adp_prb_int_msk:1;
  58675. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  58676. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  58677. + * This bit is valid only if OTG_Ver = 1'b1.
  58678. + */
  58679. + unsigned adp_sns_int_msk:1;
  58680. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  58681. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  58682. + * This bit is valid only if OTG_Ver = 1'b1.
  58683. + */
  58684. + unsigned adp_tmout_int_msk:1;
  58685. + /** Access Request
  58686. + * 2'b00 - Read/Write Valid (updated by the core)
  58687. + * 2'b01 - Read
  58688. + * 2'b00 - Write
  58689. + * 2'b00 - Reserved
  58690. + */
  58691. + unsigned ar:2;
  58692. + /** Reserved */
  58693. + unsigned reserved29_31:3;
  58694. + } b;
  58695. +} adpctl_data_t;
  58696. +
  58697. +////////////////////////////////////////////
  58698. +// Device Registers
  58699. +/**
  58700. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  58701. + *
  58702. + * The following structures define the size and relative field offsets
  58703. + * for the Device Mode Registers.
  58704. + *
  58705. + * <i>These registers are visible only in Device mode and must not be
  58706. + * accessed in Host mode, as the results are unknown.</i>
  58707. + */
  58708. +typedef struct dwc_otg_dev_global_regs {
  58709. + /** Device Configuration Register. <i>Offset 800h</i> */
  58710. + volatile uint32_t dcfg;
  58711. + /** Device Control Register. <i>Offset: 804h</i> */
  58712. + volatile uint32_t dctl;
  58713. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  58714. + volatile uint32_t dsts;
  58715. + /** Reserved. <i>Offset: 80Ch</i> */
  58716. + uint32_t unused;
  58717. + /** Device IN Endpoint Common Interrupt Mask
  58718. + * Register. <i>Offset: 810h</i> */
  58719. + volatile uint32_t diepmsk;
  58720. + /** Device OUT Endpoint Common Interrupt Mask
  58721. + * Register. <i>Offset: 814h</i> */
  58722. + volatile uint32_t doepmsk;
  58723. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  58724. + volatile uint32_t daint;
  58725. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  58726. + * 81Ch</i> */
  58727. + volatile uint32_t daintmsk;
  58728. + /** Device IN Token Queue Read Register-1 (Read Only).
  58729. + * <i>Offset: 820h</i> */
  58730. + volatile uint32_t dtknqr1;
  58731. + /** Device IN Token Queue Read Register-2 (Read Only).
  58732. + * <i>Offset: 824h</i> */
  58733. + volatile uint32_t dtknqr2;
  58734. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  58735. + volatile uint32_t dvbusdis;
  58736. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  58737. + volatile uint32_t dvbuspulse;
  58738. + /** Device IN Token Queue Read Register-3 (Read Only). /
  58739. + * Device Thresholding control register (Read/Write)
  58740. + * <i>Offset: 830h</i> */
  58741. + volatile uint32_t dtknqr3_dthrctl;
  58742. + /** Device IN Token Queue Read Register-4 (Read Only). /
  58743. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  58744. + * <i>Offset: 834h</i> */
  58745. + volatile uint32_t dtknqr4_fifoemptymsk;
  58746. + /** Device Each Endpoint Interrupt Register (Read Only). /
  58747. + * <i>Offset: 838h</i> */
  58748. + volatile uint32_t deachint;
  58749. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  58750. + * <i>Offset: 83Ch</i> */
  58751. + volatile uint32_t deachintmsk;
  58752. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  58753. + * <i>Offset: 840h</i> */
  58754. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  58755. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  58756. + * <i>Offset: 880h</i> */
  58757. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  58758. +} dwc_otg_device_global_regs_t;
  58759. +
  58760. +/**
  58761. + * This union represents the bit fields in the Device Configuration
  58762. + * Register. Read the register into the <i>d32</i> member then
  58763. + * set/clear the bits using the <i>b</i>it elements. Write the
  58764. + * <i>d32</i> member to the dcfg register.
  58765. + */
  58766. +typedef union dcfg_data {
  58767. + /** raw register data */
  58768. + uint32_t d32;
  58769. + /** register bits */
  58770. + struct {
  58771. + /** Device Speed */
  58772. + unsigned devspd:2;
  58773. + /** Non Zero Length Status OUT Handshake */
  58774. + unsigned nzstsouthshk:1;
  58775. +#define DWC_DCFG_SEND_STALL 1
  58776. +
  58777. + unsigned ena32khzs:1;
  58778. + /** Device Addresses */
  58779. + unsigned devaddr:7;
  58780. + /** Periodic Frame Interval */
  58781. + unsigned perfrint:2;
  58782. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  58783. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  58784. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  58785. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  58786. +
  58787. + /** Enable Device OUT NAK for bulk in DDMA mode */
  58788. + unsigned endevoutnak:1;
  58789. +
  58790. + unsigned reserved14_17:4;
  58791. + /** In Endpoint Mis-match count */
  58792. + unsigned epmscnt:5;
  58793. + /** Enable Descriptor DMA in Device mode */
  58794. + unsigned descdma:1;
  58795. + unsigned perschintvl:2;
  58796. + unsigned resvalid:6;
  58797. + } b;
  58798. +} dcfg_data_t;
  58799. +
  58800. +/**
  58801. + * This union represents the bit fields in the Device Control
  58802. + * Register. Read the register into the <i>d32</i> member then
  58803. + * set/clear the bits using the <i>b</i>it elements.
  58804. + */
  58805. +typedef union dctl_data {
  58806. + /** raw register data */
  58807. + uint32_t d32;
  58808. + /** register bits */
  58809. + struct {
  58810. + /** Remote Wakeup */
  58811. + unsigned rmtwkupsig:1;
  58812. + /** Soft Disconnect */
  58813. + unsigned sftdiscon:1;
  58814. + /** Global Non-Periodic IN NAK Status */
  58815. + unsigned gnpinnaksts:1;
  58816. + /** Global OUT NAK Status */
  58817. + unsigned goutnaksts:1;
  58818. + /** Test Control */
  58819. + unsigned tstctl:3;
  58820. + /** Set Global Non-Periodic IN NAK */
  58821. + unsigned sgnpinnak:1;
  58822. + /** Clear Global Non-Periodic IN NAK */
  58823. + unsigned cgnpinnak:1;
  58824. + /** Set Global OUT NAK */
  58825. + unsigned sgoutnak:1;
  58826. + /** Clear Global OUT NAK */
  58827. + unsigned cgoutnak:1;
  58828. + /** Power-On Programming Done */
  58829. + unsigned pwronprgdone:1;
  58830. + /** Reserved */
  58831. + unsigned reserved:1;
  58832. + /** Global Multi Count */
  58833. + unsigned gmc:2;
  58834. + /** Ignore Frame Number for ISOC EPs */
  58835. + unsigned ifrmnum:1;
  58836. + /** NAK on Babble */
  58837. + unsigned nakonbble:1;
  58838. + /** Enable Continue on BNA */
  58839. + unsigned encontonbna:1;
  58840. +
  58841. + unsigned reserved18_31:14;
  58842. + } b;
  58843. +} dctl_data_t;
  58844. +
  58845. +/**
  58846. + * This union represents the bit fields in the Device Status
  58847. + * Register. Read the register into the <i>d32</i> member then
  58848. + * set/clear the bits using the <i>b</i>it elements.
  58849. + */
  58850. +typedef union dsts_data {
  58851. + /** raw register data */
  58852. + uint32_t d32;
  58853. + /** register bits */
  58854. + struct {
  58855. + /** Suspend Status */
  58856. + unsigned suspsts:1;
  58857. + /** Enumerated Speed */
  58858. + unsigned enumspd:2;
  58859. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  58860. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  58861. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  58862. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  58863. + /** Erratic Error */
  58864. + unsigned errticerr:1;
  58865. + unsigned reserved4_7:4;
  58866. + /** Frame or Microframe Number of the received SOF */
  58867. + unsigned soffn:14;
  58868. + unsigned reserved22_31:10;
  58869. + } b;
  58870. +} dsts_data_t;
  58871. +
  58872. +/**
  58873. + * This union represents the bit fields in the Device IN EP Interrupt
  58874. + * Register and the Device IN EP Common Mask Register.
  58875. + *
  58876. + * - Read the register into the <i>d32</i> member then set/clear the
  58877. + * bits using the <i>b</i>it elements.
  58878. + */
  58879. +typedef union diepint_data {
  58880. + /** raw register data */
  58881. + uint32_t d32;
  58882. + /** register bits */
  58883. + struct {
  58884. + /** Transfer complete mask */
  58885. + unsigned xfercompl:1;
  58886. + /** Endpoint disable mask */
  58887. + unsigned epdisabled:1;
  58888. + /** AHB Error mask */
  58889. + unsigned ahberr:1;
  58890. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  58891. + unsigned timeout:1;
  58892. + /** IN Token received with TxF Empty mask */
  58893. + unsigned intktxfemp:1;
  58894. + /** IN Token Received with EP mismatch mask */
  58895. + unsigned intknepmis:1;
  58896. + /** IN Endpoint NAK Effective mask */
  58897. + unsigned inepnakeff:1;
  58898. + /** Reserved */
  58899. + unsigned emptyintr:1;
  58900. +
  58901. + unsigned txfifoundrn:1;
  58902. +
  58903. + /** BNA Interrupt mask */
  58904. + unsigned bna:1;
  58905. +
  58906. + unsigned reserved10_12:3;
  58907. + /** BNA Interrupt mask */
  58908. + unsigned nak:1;
  58909. +
  58910. + unsigned reserved14_31:18;
  58911. + } b;
  58912. +} diepint_data_t;
  58913. +
  58914. +/**
  58915. + * This union represents the bit fields in the Device IN EP
  58916. + * Common/Dedicated Interrupt Mask Register.
  58917. + */
  58918. +typedef union diepint_data diepmsk_data_t;
  58919. +
  58920. +/**
  58921. + * This union represents the bit fields in the Device OUT EP Interrupt
  58922. + * Registerand Device OUT EP Common Interrupt Mask Register.
  58923. + *
  58924. + * - Read the register into the <i>d32</i> member then set/clear the
  58925. + * bits using the <i>b</i>it elements.
  58926. + */
  58927. +typedef union doepint_data {
  58928. + /** raw register data */
  58929. + uint32_t d32;
  58930. + /** register bits */
  58931. + struct {
  58932. + /** Transfer complete */
  58933. + unsigned xfercompl:1;
  58934. + /** Endpoint disable */
  58935. + unsigned epdisabled:1;
  58936. + /** AHB Error */
  58937. + unsigned ahberr:1;
  58938. + /** Setup Phase Done (contorl EPs) */
  58939. + unsigned setup:1;
  58940. + /** OUT Token Received when Endpoint Disabled */
  58941. + unsigned outtknepdis:1;
  58942. +
  58943. + unsigned stsphsercvd:1;
  58944. + /** Back-to-Back SETUP Packets Received */
  58945. + unsigned back2backsetup:1;
  58946. +
  58947. + unsigned reserved7:1;
  58948. + /** OUT packet Error */
  58949. + unsigned outpkterr:1;
  58950. + /** BNA Interrupt */
  58951. + unsigned bna:1;
  58952. +
  58953. + unsigned reserved10:1;
  58954. + /** Packet Drop Status */
  58955. + unsigned pktdrpsts:1;
  58956. + /** Babble Interrupt */
  58957. + unsigned babble:1;
  58958. + /** NAK Interrupt */
  58959. + unsigned nak:1;
  58960. + /** NYET Interrupt */
  58961. + unsigned nyet:1;
  58962. + /** Bit indicating setup packet received */
  58963. + unsigned sr:1;
  58964. +
  58965. + unsigned reserved16_31:16;
  58966. + } b;
  58967. +} doepint_data_t;
  58968. +
  58969. +/**
  58970. + * This union represents the bit fields in the Device OUT EP
  58971. + * Common/Dedicated Interrupt Mask Register.
  58972. + */
  58973. +typedef union doepint_data doepmsk_data_t;
  58974. +
  58975. +/**
  58976. + * This union represents the bit fields in the Device All EP Interrupt
  58977. + * and Mask Registers.
  58978. + * - Read the register into the <i>d32</i> member then set/clear the
  58979. + * bits using the <i>b</i>it elements.
  58980. + */
  58981. +typedef union daint_data {
  58982. + /** raw register data */
  58983. + uint32_t d32;
  58984. + /** register bits */
  58985. + struct {
  58986. + /** IN Endpoint bits */
  58987. + unsigned in:16;
  58988. + /** OUT Endpoint bits */
  58989. + unsigned out:16;
  58990. + } ep;
  58991. + struct {
  58992. + /** IN Endpoint bits */
  58993. + unsigned inep0:1;
  58994. + unsigned inep1:1;
  58995. + unsigned inep2:1;
  58996. + unsigned inep3:1;
  58997. + unsigned inep4:1;
  58998. + unsigned inep5:1;
  58999. + unsigned inep6:1;
  59000. + unsigned inep7:1;
  59001. + unsigned inep8:1;
  59002. + unsigned inep9:1;
  59003. + unsigned inep10:1;
  59004. + unsigned inep11:1;
  59005. + unsigned inep12:1;
  59006. + unsigned inep13:1;
  59007. + unsigned inep14:1;
  59008. + unsigned inep15:1;
  59009. + /** OUT Endpoint bits */
  59010. + unsigned outep0:1;
  59011. + unsigned outep1:1;
  59012. + unsigned outep2:1;
  59013. + unsigned outep3:1;
  59014. + unsigned outep4:1;
  59015. + unsigned outep5:1;
  59016. + unsigned outep6:1;
  59017. + unsigned outep7:1;
  59018. + unsigned outep8:1;
  59019. + unsigned outep9:1;
  59020. + unsigned outep10:1;
  59021. + unsigned outep11:1;
  59022. + unsigned outep12:1;
  59023. + unsigned outep13:1;
  59024. + unsigned outep14:1;
  59025. + unsigned outep15:1;
  59026. + } b;
  59027. +} daint_data_t;
  59028. +
  59029. +/**
  59030. + * This union represents the bit fields in the Device IN Token Queue
  59031. + * Read Registers.
  59032. + * - Read the register into the <i>d32</i> member.
  59033. + * - READ-ONLY Register
  59034. + */
  59035. +typedef union dtknq1_data {
  59036. + /** raw register data */
  59037. + uint32_t d32;
  59038. + /** register bits */
  59039. + struct {
  59040. + /** In Token Queue Write Pointer */
  59041. + unsigned intknwptr:5;
  59042. + /** Reserved */
  59043. + unsigned reserved05_06:2;
  59044. + /** write pointer has wrapped. */
  59045. + unsigned wrap_bit:1;
  59046. + /** EP Numbers of IN Tokens 0 ... 4 */
  59047. + unsigned epnums0_5:24;
  59048. + } b;
  59049. +} dtknq1_data_t;
  59050. +
  59051. +/**
  59052. + * This union represents Threshold control Register
  59053. + * - Read and write the register into the <i>d32</i> member.
  59054. + * - READ-WRITABLE Register
  59055. + */
  59056. +typedef union dthrctl_data {
  59057. + /** raw register data */
  59058. + uint32_t d32;
  59059. + /** register bits */
  59060. + struct {
  59061. + /** non ISO Tx Thr. Enable */
  59062. + unsigned non_iso_thr_en:1;
  59063. + /** ISO Tx Thr. Enable */
  59064. + unsigned iso_thr_en:1;
  59065. + /** Tx Thr. Length */
  59066. + unsigned tx_thr_len:9;
  59067. + /** AHB Threshold ratio */
  59068. + unsigned ahb_thr_ratio:2;
  59069. + /** Reserved */
  59070. + unsigned reserved13_15:3;
  59071. + /** Rx Thr. Enable */
  59072. + unsigned rx_thr_en:1;
  59073. + /** Rx Thr. Length */
  59074. + unsigned rx_thr_len:9;
  59075. + unsigned reserved26:1;
  59076. + /** Arbiter Parking Enable*/
  59077. + unsigned arbprken:1;
  59078. + /** Reserved */
  59079. + unsigned reserved28_31:4;
  59080. + } b;
  59081. +} dthrctl_data_t;
  59082. +
  59083. +/**
  59084. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  59085. + * 900h-AFCh</i>
  59086. + *
  59087. + * There will be one set of endpoint registers per logical endpoint
  59088. + * implemented.
  59089. + *
  59090. + * <i>These registers are visible only in Device mode and must not be
  59091. + * accessed in Host mode, as the results are unknown.</i>
  59092. + */
  59093. +typedef struct dwc_otg_dev_in_ep_regs {
  59094. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  59095. + * (ep_num * 20h) + 00h</i> */
  59096. + volatile uint32_t diepctl;
  59097. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  59098. + uint32_t reserved04;
  59099. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  59100. + * (ep_num * 20h) + 08h</i> */
  59101. + volatile uint32_t diepint;
  59102. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  59103. + uint32_t reserved0C;
  59104. + /** Device IN Endpoint Transfer Size
  59105. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  59106. + volatile uint32_t dieptsiz;
  59107. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  59108. + * (ep_num * 20h) + 14h</i> */
  59109. + volatile uint32_t diepdma;
  59110. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  59111. + * (ep_num * 20h) + 18h</i> */
  59112. + volatile uint32_t dtxfsts;
  59113. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  59114. + * (ep_num * 20h) + 1Ch</i> */
  59115. + volatile uint32_t diepdmab;
  59116. +} dwc_otg_dev_in_ep_regs_t;
  59117. +
  59118. +/**
  59119. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  59120. + * B00h-CFCh</i>
  59121. + *
  59122. + * There will be one set of endpoint registers per logical endpoint
  59123. + * implemented.
  59124. + *
  59125. + * <i>These registers are visible only in Device mode and must not be
  59126. + * accessed in Host mode, as the results are unknown.</i>
  59127. + */
  59128. +typedef struct dwc_otg_dev_out_ep_regs {
  59129. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  59130. + * (ep_num * 20h) + 00h</i> */
  59131. + volatile uint32_t doepctl;
  59132. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  59133. + uint32_t reserved04;
  59134. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  59135. + * (ep_num * 20h) + 08h</i> */
  59136. + volatile uint32_t doepint;
  59137. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  59138. + uint32_t reserved0C;
  59139. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  59140. + * B00h + (ep_num * 20h) + 10h</i> */
  59141. + volatile uint32_t doeptsiz;
  59142. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  59143. + * + (ep_num * 20h) + 14h</i> */
  59144. + volatile uint32_t doepdma;
  59145. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  59146. + uint32_t unused;
  59147. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  59148. + * + (ep_num * 20h) + 1Ch</i> */
  59149. + uint32_t doepdmab;
  59150. +} dwc_otg_dev_out_ep_regs_t;
  59151. +
  59152. +/**
  59153. + * This union represents the bit fields in the Device EP Control
  59154. + * Register. Read the register into the <i>d32</i> member then
  59155. + * set/clear the bits using the <i>b</i>it elements.
  59156. + */
  59157. +typedef union depctl_data {
  59158. + /** raw register data */
  59159. + uint32_t d32;
  59160. + /** register bits */
  59161. + struct {
  59162. + /** Maximum Packet Size
  59163. + * IN/OUT EPn
  59164. + * IN/OUT EP0 - 2 bits
  59165. + * 2'b00: 64 Bytes
  59166. + * 2'b01: 32
  59167. + * 2'b10: 16
  59168. + * 2'b11: 8 */
  59169. + unsigned mps:11;
  59170. +#define DWC_DEP0CTL_MPS_64 0
  59171. +#define DWC_DEP0CTL_MPS_32 1
  59172. +#define DWC_DEP0CTL_MPS_16 2
  59173. +#define DWC_DEP0CTL_MPS_8 3
  59174. +
  59175. + /** Next Endpoint
  59176. + * IN EPn/IN EP0
  59177. + * OUT EPn/OUT EP0 - reserved */
  59178. + unsigned nextep:4;
  59179. +
  59180. + /** USB Active Endpoint */
  59181. + unsigned usbactep:1;
  59182. +
  59183. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  59184. + * This field contains the PID of the packet going to
  59185. + * be received or transmitted on this endpoint. The
  59186. + * application should program the PID of the first
  59187. + * packet going to be received or transmitted on this
  59188. + * endpoint , after the endpoint is
  59189. + * activated. Application use the SetD1PID and
  59190. + * SetD0PID fields of this register to program either
  59191. + * D0 or D1 PID.
  59192. + *
  59193. + * The encoding for this field is
  59194. + * - 0: D0
  59195. + * - 1: D1
  59196. + */
  59197. + unsigned dpid:1;
  59198. +
  59199. + /** NAK Status */
  59200. + unsigned naksts:1;
  59201. +
  59202. + /** Endpoint Type
  59203. + * 2'b00: Control
  59204. + * 2'b01: Isochronous
  59205. + * 2'b10: Bulk
  59206. + * 2'b11: Interrupt */
  59207. + unsigned eptype:2;
  59208. +
  59209. + /** Snoop Mode
  59210. + * OUT EPn/OUT EP0
  59211. + * IN EPn/IN EP0 - reserved */
  59212. + unsigned snp:1;
  59213. +
  59214. + /** Stall Handshake */
  59215. + unsigned stall:1;
  59216. +
  59217. + /** Tx Fifo Number
  59218. + * IN EPn/IN EP0
  59219. + * OUT EPn/OUT EP0 - reserved */
  59220. + unsigned txfnum:4;
  59221. +
  59222. + /** Clear NAK */
  59223. + unsigned cnak:1;
  59224. + /** Set NAK */
  59225. + unsigned snak:1;
  59226. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  59227. + * Writing to this field sets the Endpoint DPID (DPID)
  59228. + * field in this register to DATA0. Set Even
  59229. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  59230. + * Writing to this field sets the Even/Odd
  59231. + * (micro)frame (EO_FrNum) field to even (micro)
  59232. + * frame.
  59233. + */
  59234. + unsigned setd0pid:1;
  59235. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  59236. + * Writing to this field sets the Endpoint DPID (DPID)
  59237. + * field in this register to DATA1 Set Odd
  59238. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  59239. + * Writing to this field sets the Even/Odd
  59240. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  59241. + */
  59242. + unsigned setd1pid:1;
  59243. +
  59244. + /** Endpoint Disable */
  59245. + unsigned epdis:1;
  59246. + /** Endpoint Enable */
  59247. + unsigned epena:1;
  59248. + } b;
  59249. +} depctl_data_t;
  59250. +
  59251. +/**
  59252. + * This union represents the bit fields in the Device EP Transfer
  59253. + * Size Register. Read the register into the <i>d32</i> member then
  59254. + * set/clear the bits using the <i>b</i>it elements.
  59255. + */
  59256. +typedef union deptsiz_data {
  59257. + /** raw register data */
  59258. + uint32_t d32;
  59259. + /** register bits */
  59260. + struct {
  59261. + /** Transfer size */
  59262. + unsigned xfersize:19;
  59263. +/** Max packet count for EP (pow(2,10)-1) */
  59264. +#define MAX_PKT_CNT 1023
  59265. + /** Packet Count */
  59266. + unsigned pktcnt:10;
  59267. + /** Multi Count - Periodic IN endpoints */
  59268. + unsigned mc:2;
  59269. + unsigned reserved:1;
  59270. + } b;
  59271. +} deptsiz_data_t;
  59272. +
  59273. +/**
  59274. + * This union represents the bit fields in the Device EP 0 Transfer
  59275. + * Size Register. Read the register into the <i>d32</i> member then
  59276. + * set/clear the bits using the <i>b</i>it elements.
  59277. + */
  59278. +typedef union deptsiz0_data {
  59279. + /** raw register data */
  59280. + uint32_t d32;
  59281. + /** register bits */
  59282. + struct {
  59283. + /** Transfer size */
  59284. + unsigned xfersize:7;
  59285. + /** Reserved */
  59286. + unsigned reserved7_18:12;
  59287. + /** Packet Count */
  59288. + unsigned pktcnt:2;
  59289. + /** Reserved */
  59290. + unsigned reserved21_28:8;
  59291. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  59292. + unsigned supcnt:2;
  59293. + unsigned reserved31;
  59294. + } b;
  59295. +} deptsiz0_data_t;
  59296. +
  59297. +/////////////////////////////////////////////////
  59298. +// DMA Descriptor Specific Structures
  59299. +//
  59300. +
  59301. +/** Buffer status definitions */
  59302. +
  59303. +#define BS_HOST_READY 0x0
  59304. +#define BS_DMA_BUSY 0x1
  59305. +#define BS_DMA_DONE 0x2
  59306. +#define BS_HOST_BUSY 0x3
  59307. +
  59308. +/** Receive/Transmit status definitions */
  59309. +
  59310. +#define RTS_SUCCESS 0x0
  59311. +#define RTS_BUFFLUSH 0x1
  59312. +#define RTS_RESERVED 0x2
  59313. +#define RTS_BUFERR 0x3
  59314. +
  59315. +/**
  59316. + * This union represents the bit fields in the DMA Descriptor
  59317. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  59318. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  59319. + * <i>b_iso_in</i> elements.
  59320. + */
  59321. +typedef union dev_dma_desc_sts {
  59322. + /** raw register data */
  59323. + uint32_t d32;
  59324. + /** quadlet bits */
  59325. + struct {
  59326. + /** Received number of bytes */
  59327. + unsigned bytes:16;
  59328. + /** NAK bit - only for OUT EPs */
  59329. + unsigned nak:1;
  59330. + unsigned reserved17_22:6;
  59331. + /** Multiple Transfer - only for OUT EPs */
  59332. + unsigned mtrf:1;
  59333. + /** Setup Packet received - only for OUT EPs */
  59334. + unsigned sr:1;
  59335. + /** Interrupt On Complete */
  59336. + unsigned ioc:1;
  59337. + /** Short Packet */
  59338. + unsigned sp:1;
  59339. + /** Last */
  59340. + unsigned l:1;
  59341. + /** Receive Status */
  59342. + unsigned sts:2;
  59343. + /** Buffer Status */
  59344. + unsigned bs:2;
  59345. + } b;
  59346. +
  59347. +//#ifdef DWC_EN_ISOC
  59348. + /** iso out quadlet bits */
  59349. + struct {
  59350. + /** Received number of bytes */
  59351. + unsigned rxbytes:11;
  59352. +
  59353. + unsigned reserved11:1;
  59354. + /** Frame Number */
  59355. + unsigned framenum:11;
  59356. + /** Received ISO Data PID */
  59357. + unsigned pid:2;
  59358. + /** Interrupt On Complete */
  59359. + unsigned ioc:1;
  59360. + /** Short Packet */
  59361. + unsigned sp:1;
  59362. + /** Last */
  59363. + unsigned l:1;
  59364. + /** Receive Status */
  59365. + unsigned rxsts:2;
  59366. + /** Buffer Status */
  59367. + unsigned bs:2;
  59368. + } b_iso_out;
  59369. +
  59370. + /** iso in quadlet bits */
  59371. + struct {
  59372. + /** Transmited number of bytes */
  59373. + unsigned txbytes:12;
  59374. + /** Frame Number */
  59375. + unsigned framenum:11;
  59376. + /** Transmited ISO Data PID */
  59377. + unsigned pid:2;
  59378. + /** Interrupt On Complete */
  59379. + unsigned ioc:1;
  59380. + /** Short Packet */
  59381. + unsigned sp:1;
  59382. + /** Last */
  59383. + unsigned l:1;
  59384. + /** Transmit Status */
  59385. + unsigned txsts:2;
  59386. + /** Buffer Status */
  59387. + unsigned bs:2;
  59388. + } b_iso_in;
  59389. +//#endif /* DWC_EN_ISOC */
  59390. +} dev_dma_desc_sts_t;
  59391. +
  59392. +/**
  59393. + * DMA Descriptor structure
  59394. + *
  59395. + * DMA Descriptor structure contains two quadlets:
  59396. + * Status quadlet and Data buffer pointer.
  59397. + */
  59398. +typedef struct dwc_otg_dev_dma_desc {
  59399. + /** DMA Descriptor status quadlet */
  59400. + dev_dma_desc_sts_t status;
  59401. + /** DMA Descriptor data buffer pointer */
  59402. + uint32_t buf;
  59403. +} dwc_otg_dev_dma_desc_t;
  59404. +
  59405. +/**
  59406. + * The dwc_otg_dev_if structure contains information needed to manage
  59407. + * the DWC_otg controller acting in device mode. It represents the
  59408. + * programming view of the device-specific aspects of the controller.
  59409. + */
  59410. +typedef struct dwc_otg_dev_if {
  59411. + /** Pointer to device Global registers.
  59412. + * Device Global Registers starting at offset 800h
  59413. + */
  59414. + dwc_otg_device_global_regs_t *dev_global_regs;
  59415. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  59416. +
  59417. + /**
  59418. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  59419. + */
  59420. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  59421. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  59422. +#define DWC_EP_REG_OFFSET 0x20
  59423. +
  59424. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  59425. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  59426. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  59427. +
  59428. + /* Device configuration information */
  59429. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  59430. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  59431. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  59432. +
  59433. + /** Size of periodic FIFOs (Bytes) */
  59434. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  59435. +
  59436. + /** Size of Tx FIFOs (Bytes) */
  59437. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  59438. +
  59439. + /** Thresholding enable flags and length varaiables **/
  59440. + uint16_t rx_thr_en;
  59441. + uint16_t iso_tx_thr_en;
  59442. + uint16_t non_iso_tx_thr_en;
  59443. +
  59444. + uint16_t rx_thr_length;
  59445. + uint16_t tx_thr_length;
  59446. +
  59447. + /**
  59448. + * Pointers to the DMA Descriptors for EP0 Control
  59449. + * transfers (virtual and physical)
  59450. + */
  59451. +
  59452. + /** 2 descriptors for SETUP packets */
  59453. + dwc_dma_t dma_setup_desc_addr[2];
  59454. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  59455. +
  59456. + /** Pointer to Descriptor with latest SETUP packet */
  59457. + dwc_otg_dev_dma_desc_t *psetup;
  59458. +
  59459. + /** Index of current SETUP handler descriptor */
  59460. + uint32_t setup_desc_index;
  59461. +
  59462. + /** Descriptor for Data In or Status In phases */
  59463. + dwc_dma_t dma_in_desc_addr;
  59464. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  59465. +
  59466. + /** Descriptor for Data Out or Status Out phases */
  59467. + dwc_dma_t dma_out_desc_addr;
  59468. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  59469. +
  59470. + /** Setup Packet Detected - if set clear NAK when queueing */
  59471. + uint32_t spd;
  59472. + /** Isoc ep pointer on which incomplete happens */
  59473. + void *isoc_ep;
  59474. +
  59475. +} dwc_otg_dev_if_t;
  59476. +
  59477. +/////////////////////////////////////////////////
  59478. +// Host Mode Register Structures
  59479. +//
  59480. +/**
  59481. + * The Host Global Registers structure defines the size and relative
  59482. + * field offsets for the Host Mode Global Registers. Host Global
  59483. + * Registers offsets 400h-7FFh.
  59484. +*/
  59485. +typedef struct dwc_otg_host_global_regs {
  59486. + /** Host Configuration Register. <i>Offset: 400h</i> */
  59487. + volatile uint32_t hcfg;
  59488. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  59489. + volatile uint32_t hfir;
  59490. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  59491. + volatile uint32_t hfnum;
  59492. + /** Reserved. <i>Offset: 40Ch</i> */
  59493. + uint32_t reserved40C;
  59494. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  59495. + volatile uint32_t hptxsts;
  59496. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  59497. + volatile uint32_t haint;
  59498. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  59499. + volatile uint32_t haintmsk;
  59500. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  59501. + volatile uint32_t hflbaddr;
  59502. +} dwc_otg_host_global_regs_t;
  59503. +
  59504. +/**
  59505. + * This union represents the bit fields in the Host Configuration Register.
  59506. + * Read the register into the <i>d32</i> member then set/clear the bits using
  59507. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  59508. + */
  59509. +typedef union hcfg_data {
  59510. + /** raw register data */
  59511. + uint32_t d32;
  59512. +
  59513. + /** register bits */
  59514. + struct {
  59515. + /** FS/LS Phy Clock Select */
  59516. + unsigned fslspclksel:2;
  59517. +#define DWC_HCFG_30_60_MHZ 0
  59518. +#define DWC_HCFG_48_MHZ 1
  59519. +#define DWC_HCFG_6_MHZ 2
  59520. +
  59521. + /** FS/LS Only Support */
  59522. + unsigned fslssupp:1;
  59523. + unsigned reserved3_6:4;
  59524. + /** Enable 32-KHz Suspend Mode */
  59525. + unsigned ena32khzs:1;
  59526. + /** Resume Validation Periiod */
  59527. + unsigned resvalid:8;
  59528. + unsigned reserved16_22:7;
  59529. + /** Enable Scatter/gather DMA in Host mode */
  59530. + unsigned descdma:1;
  59531. + /** Frame List Entries */
  59532. + unsigned frlisten:2;
  59533. + /** Enable Periodic Scheduling */
  59534. + unsigned perschedena:1;
  59535. + unsigned reserved27_30:4;
  59536. + unsigned modechtimen:1;
  59537. + } b;
  59538. +} hcfg_data_t;
  59539. +
  59540. +/**
  59541. + * This union represents the bit fields in the Host Frame Remaing/Number
  59542. + * Register.
  59543. + */
  59544. +typedef union hfir_data {
  59545. + /** raw register data */
  59546. + uint32_t d32;
  59547. +
  59548. + /** register bits */
  59549. + struct {
  59550. + unsigned frint:16;
  59551. + unsigned hfirrldctrl:1;
  59552. + unsigned reserved:15;
  59553. + } b;
  59554. +} hfir_data_t;
  59555. +
  59556. +/**
  59557. + * This union represents the bit fields in the Host Frame Remaing/Number
  59558. + * Register.
  59559. + */
  59560. +typedef union hfnum_data {
  59561. + /** raw register data */
  59562. + uint32_t d32;
  59563. +
  59564. + /** register bits */
  59565. + struct {
  59566. + unsigned frnum:16;
  59567. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  59568. + unsigned frrem:16;
  59569. + } b;
  59570. +} hfnum_data_t;
  59571. +
  59572. +typedef union hptxsts_data {
  59573. + /** raw register data */
  59574. + uint32_t d32;
  59575. +
  59576. + /** register bits */
  59577. + struct {
  59578. + unsigned ptxfspcavail:16;
  59579. + unsigned ptxqspcavail:8;
  59580. + /** Top of the Periodic Transmit Request Queue
  59581. + * - bit 24 - Terminate (last entry for the selected channel)
  59582. + * - bits 26:25 - Token Type
  59583. + * - 2'b00 - Zero length
  59584. + * - 2'b01 - Ping
  59585. + * - 2'b10 - Disable
  59586. + * - bits 30:27 - Channel Number
  59587. + * - bit 31 - Odd/even microframe
  59588. + */
  59589. + unsigned ptxqtop_terminate:1;
  59590. + unsigned ptxqtop_token:2;
  59591. + unsigned ptxqtop_chnum:4;
  59592. + unsigned ptxqtop_odd:1;
  59593. + } b;
  59594. +} hptxsts_data_t;
  59595. +
  59596. +/**
  59597. + * This union represents the bit fields in the Host Port Control and Status
  59598. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59599. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59600. + * hprt0 register.
  59601. + */
  59602. +typedef union hprt0_data {
  59603. + /** raw register data */
  59604. + uint32_t d32;
  59605. + /** register bits */
  59606. + struct {
  59607. + unsigned prtconnsts:1;
  59608. + unsigned prtconndet:1;
  59609. + unsigned prtena:1;
  59610. + unsigned prtenchng:1;
  59611. + unsigned prtovrcurract:1;
  59612. + unsigned prtovrcurrchng:1;
  59613. + unsigned prtres:1;
  59614. + unsigned prtsusp:1;
  59615. + unsigned prtrst:1;
  59616. + unsigned reserved9:1;
  59617. + unsigned prtlnsts:2;
  59618. + unsigned prtpwr:1;
  59619. + unsigned prttstctl:4;
  59620. + unsigned prtspd:2;
  59621. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  59622. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  59623. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  59624. + unsigned reserved19_31:13;
  59625. + } b;
  59626. +} hprt0_data_t;
  59627. +
  59628. +/**
  59629. + * This union represents the bit fields in the Host All Interrupt
  59630. + * Register.
  59631. + */
  59632. +typedef union haint_data {
  59633. + /** raw register data */
  59634. + uint32_t d32;
  59635. + /** register bits */
  59636. + struct {
  59637. + unsigned ch0:1;
  59638. + unsigned ch1:1;
  59639. + unsigned ch2:1;
  59640. + unsigned ch3:1;
  59641. + unsigned ch4:1;
  59642. + unsigned ch5:1;
  59643. + unsigned ch6:1;
  59644. + unsigned ch7:1;
  59645. + unsigned ch8:1;
  59646. + unsigned ch9:1;
  59647. + unsigned ch10:1;
  59648. + unsigned ch11:1;
  59649. + unsigned ch12:1;
  59650. + unsigned ch13:1;
  59651. + unsigned ch14:1;
  59652. + unsigned ch15:1;
  59653. + unsigned reserved:16;
  59654. + } b;
  59655. +
  59656. + struct {
  59657. + unsigned chint:16;
  59658. + unsigned reserved:16;
  59659. + } b2;
  59660. +} haint_data_t;
  59661. +
  59662. +/**
  59663. + * This union represents the bit fields in the Host All Interrupt
  59664. + * Register.
  59665. + */
  59666. +typedef union haintmsk_data {
  59667. + /** raw register data */
  59668. + uint32_t d32;
  59669. + /** register bits */
  59670. + struct {
  59671. + unsigned ch0:1;
  59672. + unsigned ch1:1;
  59673. + unsigned ch2:1;
  59674. + unsigned ch3:1;
  59675. + unsigned ch4:1;
  59676. + unsigned ch5:1;
  59677. + unsigned ch6:1;
  59678. + unsigned ch7:1;
  59679. + unsigned ch8:1;
  59680. + unsigned ch9:1;
  59681. + unsigned ch10:1;
  59682. + unsigned ch11:1;
  59683. + unsigned ch12:1;
  59684. + unsigned ch13:1;
  59685. + unsigned ch14:1;
  59686. + unsigned ch15:1;
  59687. + unsigned reserved:16;
  59688. + } b;
  59689. +
  59690. + struct {
  59691. + unsigned chint:16;
  59692. + unsigned reserved:16;
  59693. + } b2;
  59694. +} haintmsk_data_t;
  59695. +
  59696. +/**
  59697. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  59698. + */
  59699. +typedef struct dwc_otg_hc_regs {
  59700. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  59701. + volatile uint32_t hcchar;
  59702. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  59703. + volatile uint32_t hcsplt;
  59704. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  59705. + volatile uint32_t hcint;
  59706. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  59707. + volatile uint32_t hcintmsk;
  59708. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  59709. + volatile uint32_t hctsiz;
  59710. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  59711. + volatile uint32_t hcdma;
  59712. + volatile uint32_t reserved;
  59713. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  59714. + volatile uint32_t hcdmab;
  59715. +} dwc_otg_hc_regs_t;
  59716. +
  59717. +/**
  59718. + * This union represents the bit fields in the Host Channel Characteristics
  59719. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59720. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59721. + * hcchar register.
  59722. + */
  59723. +typedef union hcchar_data {
  59724. + /** raw register data */
  59725. + uint32_t d32;
  59726. +
  59727. + /** register bits */
  59728. + struct {
  59729. + /** Maximum packet size in bytes */
  59730. + unsigned mps:11;
  59731. +
  59732. + /** Endpoint number */
  59733. + unsigned epnum:4;
  59734. +
  59735. + /** 0: OUT, 1: IN */
  59736. + unsigned epdir:1;
  59737. +
  59738. + unsigned reserved:1;
  59739. +
  59740. + /** 0: Full/high speed device, 1: Low speed device */
  59741. + unsigned lspddev:1;
  59742. +
  59743. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  59744. + unsigned eptype:2;
  59745. +
  59746. + /** Packets per frame for periodic transfers. 0 is reserved. */
  59747. + unsigned multicnt:2;
  59748. +
  59749. + /** Device address */
  59750. + unsigned devaddr:7;
  59751. +
  59752. + /**
  59753. + * Frame to transmit periodic transaction.
  59754. + * 0: even, 1: odd
  59755. + */
  59756. + unsigned oddfrm:1;
  59757. +
  59758. + /** Channel disable */
  59759. + unsigned chdis:1;
  59760. +
  59761. + /** Channel enable */
  59762. + unsigned chen:1;
  59763. + } b;
  59764. +} hcchar_data_t;
  59765. +
  59766. +typedef union hcsplt_data {
  59767. + /** raw register data */
  59768. + uint32_t d32;
  59769. +
  59770. + /** register bits */
  59771. + struct {
  59772. + /** Port Address */
  59773. + unsigned prtaddr:7;
  59774. +
  59775. + /** Hub Address */
  59776. + unsigned hubaddr:7;
  59777. +
  59778. + /** Transaction Position */
  59779. + unsigned xactpos:2;
  59780. +#define DWC_HCSPLIT_XACTPOS_MID 0
  59781. +#define DWC_HCSPLIT_XACTPOS_END 1
  59782. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  59783. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  59784. +
  59785. + /** Do Complete Split */
  59786. + unsigned compsplt:1;
  59787. +
  59788. + /** Reserved */
  59789. + unsigned reserved:14;
  59790. +
  59791. + /** Split Enble */
  59792. + unsigned spltena:1;
  59793. + } b;
  59794. +} hcsplt_data_t;
  59795. +
  59796. +/**
  59797. + * This union represents the bit fields in the Host All Interrupt
  59798. + * Register.
  59799. + */
  59800. +typedef union hcint_data {
  59801. + /** raw register data */
  59802. + uint32_t d32;
  59803. + /** register bits */
  59804. + struct {
  59805. + /** Transfer Complete */
  59806. + unsigned xfercomp:1;
  59807. + /** Channel Halted */
  59808. + unsigned chhltd:1;
  59809. + /** AHB Error */
  59810. + unsigned ahberr:1;
  59811. + /** STALL Response Received */
  59812. + unsigned stall:1;
  59813. + /** NAK Response Received */
  59814. + unsigned nak:1;
  59815. + /** ACK Response Received */
  59816. + unsigned ack:1;
  59817. + /** NYET Response Received */
  59818. + unsigned nyet:1;
  59819. + /** Transaction Err */
  59820. + unsigned xacterr:1;
  59821. + /** Babble Error */
  59822. + unsigned bblerr:1;
  59823. + /** Frame Overrun */
  59824. + unsigned frmovrun:1;
  59825. + /** Data Toggle Error */
  59826. + unsigned datatglerr:1;
  59827. + /** Buffer Not Available (only for DDMA mode) */
  59828. + unsigned bna:1;
  59829. + /** Exessive transaction error (only for DDMA mode) */
  59830. + unsigned xcs_xact:1;
  59831. + /** Frame List Rollover interrupt */
  59832. + unsigned frm_list_roll:1;
  59833. + /** Reserved */
  59834. + unsigned reserved14_31:18;
  59835. + } b;
  59836. +} hcint_data_t;
  59837. +
  59838. +/**
  59839. + * This union represents the bit fields in the Host Channel Interrupt Mask
  59840. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59841. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59842. + * hcintmsk register.
  59843. + */
  59844. +typedef union hcintmsk_data {
  59845. + /** raw register data */
  59846. + uint32_t d32;
  59847. +
  59848. + /** register bits */
  59849. + struct {
  59850. + unsigned xfercompl:1;
  59851. + unsigned chhltd:1;
  59852. + unsigned ahberr:1;
  59853. + unsigned stall:1;
  59854. + unsigned nak:1;
  59855. + unsigned ack:1;
  59856. + unsigned nyet:1;
  59857. + unsigned xacterr:1;
  59858. + unsigned bblerr:1;
  59859. + unsigned frmovrun:1;
  59860. + unsigned datatglerr:1;
  59861. + unsigned bna:1;
  59862. + unsigned xcs_xact:1;
  59863. + unsigned frm_list_roll:1;
  59864. + unsigned reserved14_31:18;
  59865. + } b;
  59866. +} hcintmsk_data_t;
  59867. +
  59868. +/**
  59869. + * This union represents the bit fields in the Host Channel Transfer Size
  59870. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59871. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59872. + * hcchar register.
  59873. + */
  59874. +
  59875. +typedef union hctsiz_data {
  59876. + /** raw register data */
  59877. + uint32_t d32;
  59878. +
  59879. + /** register bits */
  59880. + struct {
  59881. + /** Total transfer size in bytes */
  59882. + unsigned xfersize:19;
  59883. +
  59884. + /** Data packets to transfer */
  59885. + unsigned pktcnt:10;
  59886. +
  59887. + /**
  59888. + * Packet ID for next data packet
  59889. + * 0: DATA0
  59890. + * 1: DATA2
  59891. + * 2: DATA1
  59892. + * 3: MDATA (non-Control), SETUP (Control)
  59893. + */
  59894. + unsigned pid:2;
  59895. +#define DWC_HCTSIZ_DATA0 0
  59896. +#define DWC_HCTSIZ_DATA1 2
  59897. +#define DWC_HCTSIZ_DATA2 1
  59898. +#define DWC_HCTSIZ_MDATA 3
  59899. +#define DWC_HCTSIZ_SETUP 3
  59900. +
  59901. + /** Do PING protocol when 1 */
  59902. + unsigned dopng:1;
  59903. + } b;
  59904. +
  59905. + /** register bits */
  59906. + struct {
  59907. + /** Scheduling information */
  59908. + unsigned schinfo:8;
  59909. +
  59910. + /** Number of transfer descriptors.
  59911. + * Max value:
  59912. + * 64 in general,
  59913. + * 256 only for HS isochronous endpoint.
  59914. + */
  59915. + unsigned ntd:8;
  59916. +
  59917. + /** Data packets to transfer */
  59918. + unsigned reserved16_28:13;
  59919. +
  59920. + /**
  59921. + * Packet ID for next data packet
  59922. + * 0: DATA0
  59923. + * 1: DATA2
  59924. + * 2: DATA1
  59925. + * 3: MDATA (non-Control)
  59926. + */
  59927. + unsigned pid:2;
  59928. +
  59929. + /** Do PING protocol when 1 */
  59930. + unsigned dopng:1;
  59931. + } b_ddma;
  59932. +} hctsiz_data_t;
  59933. +
  59934. +/**
  59935. + * This union represents the bit fields in the Host DMA Address
  59936. + * Register used in Descriptor DMA mode.
  59937. + */
  59938. +typedef union hcdma_data {
  59939. + /** raw register data */
  59940. + uint32_t d32;
  59941. + /** register bits */
  59942. + struct {
  59943. + unsigned reserved0_2:3;
  59944. + /** Current Transfer Descriptor. Not used for ISOC */
  59945. + unsigned ctd:8;
  59946. + /** Start Address of Descriptor List */
  59947. + unsigned dma_addr:21;
  59948. + } b;
  59949. +} hcdma_data_t;
  59950. +
  59951. +/**
  59952. + * This union represents the bit fields in the DMA Descriptor
  59953. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  59954. + * set/clear the bits using the <i>b</i>it elements.
  59955. + */
  59956. +typedef union host_dma_desc_sts {
  59957. + /** raw register data */
  59958. + uint32_t d32;
  59959. + /** quadlet bits */
  59960. +
  59961. + /* for non-isochronous */
  59962. + struct {
  59963. + /** Number of bytes */
  59964. + unsigned n_bytes:17;
  59965. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  59966. + unsigned qtd_offset:6;
  59967. + /**
  59968. + * Set to request the core to jump to alternate QTD if
  59969. + * Short Packet received - only for IN EPs
  59970. + */
  59971. + unsigned a_qtd:1;
  59972. + /**
  59973. + * Setup Packet bit. When set indicates that buffer contains
  59974. + * setup packet.
  59975. + */
  59976. + unsigned sup:1;
  59977. + /** Interrupt On Complete */
  59978. + unsigned ioc:1;
  59979. + /** End of List */
  59980. + unsigned eol:1;
  59981. + unsigned reserved27:1;
  59982. + /** Rx/Tx Status */
  59983. + unsigned sts:2;
  59984. +#define DMA_DESC_STS_PKTERR 1
  59985. + unsigned reserved30:1;
  59986. + /** Active Bit */
  59987. + unsigned a:1;
  59988. + } b;
  59989. + /* for isochronous */
  59990. + struct {
  59991. + /** Number of bytes */
  59992. + unsigned n_bytes:12;
  59993. + unsigned reserved12_24:13;
  59994. + /** Interrupt On Complete */
  59995. + unsigned ioc:1;
  59996. + unsigned reserved26_27:2;
  59997. + /** Rx/Tx Status */
  59998. + unsigned sts:2;
  59999. + unsigned reserved30:1;
  60000. + /** Active Bit */
  60001. + unsigned a:1;
  60002. + } b_isoc;
  60003. +} host_dma_desc_sts_t;
  60004. +
  60005. +#define MAX_DMA_DESC_SIZE 131071
  60006. +#define MAX_DMA_DESC_NUM_GENERIC 64
  60007. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  60008. +#define MAX_FRLIST_EN_NUM 64
  60009. +/**
  60010. + * Host-mode DMA Descriptor structure
  60011. + *
  60012. + * DMA Descriptor structure contains two quadlets:
  60013. + * Status quadlet and Data buffer pointer.
  60014. + */
  60015. +typedef struct dwc_otg_host_dma_desc {
  60016. + /** DMA Descriptor status quadlet */
  60017. + host_dma_desc_sts_t status;
  60018. + /** DMA Descriptor data buffer pointer */
  60019. + uint32_t buf;
  60020. +} dwc_otg_host_dma_desc_t;
  60021. +
  60022. +/** OTG Host Interface Structure.
  60023. + *
  60024. + * The OTG Host Interface Structure structure contains information
  60025. + * needed to manage the DWC_otg controller acting in host mode. It
  60026. + * represents the programming view of the host-specific aspects of the
  60027. + * controller.
  60028. + */
  60029. +typedef struct dwc_otg_host_if {
  60030. + /** Host Global Registers starting at offset 400h.*/
  60031. + dwc_otg_host_global_regs_t *host_global_regs;
  60032. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  60033. +
  60034. + /** Host Port 0 Control and Status Register */
  60035. + volatile uint32_t *hprt0;
  60036. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  60037. +
  60038. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  60039. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  60040. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  60041. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  60042. +
  60043. + /* Host configuration information */
  60044. + /** Number of Host Channels (range: 1-16) */
  60045. + uint8_t num_host_channels;
  60046. + /** Periodic EPs supported (0: no, 1: yes) */
  60047. + uint8_t perio_eps_supported;
  60048. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  60049. + uint16_t perio_tx_fifo_size;
  60050. +
  60051. +} dwc_otg_host_if_t;
  60052. +
  60053. +/**
  60054. + * This union represents the bit fields in the Power and Clock Gating Control
  60055. + * Register. Read the register into the <i>d32</i> member then set/clear the
  60056. + * bits using the <i>b</i>it elements.
  60057. + */
  60058. +typedef union pcgcctl_data {
  60059. + /** raw register data */
  60060. + uint32_t d32;
  60061. +
  60062. + /** register bits */
  60063. + struct {
  60064. + /** Stop Pclk */
  60065. + unsigned stoppclk:1;
  60066. + /** Gate Hclk */
  60067. + unsigned gatehclk:1;
  60068. + /** Power Clamp */
  60069. + unsigned pwrclmp:1;
  60070. + /** Reset Power Down Modules */
  60071. + unsigned rstpdwnmodule:1;
  60072. + /** Reserved */
  60073. + unsigned reserved:1;
  60074. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  60075. + unsigned enbl_sleep_gating:1;
  60076. + /** PHY In Sleep (PhySleep) */
  60077. + unsigned phy_in_sleep:1;
  60078. + /** Deep Sleep*/
  60079. + unsigned deep_sleep:1;
  60080. + unsigned resetaftsusp:1;
  60081. + unsigned restoremode:1;
  60082. + unsigned enbl_extnd_hiber:1;
  60083. + unsigned extnd_hiber_pwrclmp:1;
  60084. + unsigned extnd_hiber_switch:1;
  60085. + unsigned ess_reg_restored:1;
  60086. + unsigned prt_clk_sel:2;
  60087. + unsigned port_power:1;
  60088. + unsigned max_xcvrselect:2;
  60089. + unsigned max_termsel:1;
  60090. + unsigned mac_dev_addr:7;
  60091. + unsigned p2hd_dev_enum_spd:2;
  60092. + unsigned p2hd_prt_spd:2;
  60093. + unsigned if_dev_mode:1;
  60094. + } b;
  60095. +} pcgcctl_data_t;
  60096. +
  60097. +/**
  60098. + * This union represents the bit fields in the Global Data FIFO Software
  60099. + * Configuration Register. Read the register into the <i>d32</i> member then
  60100. + * set/clear the bits using the <i>b</i>it elements.
  60101. + */
  60102. +typedef union gdfifocfg_data {
  60103. + /* raw register data */
  60104. + uint32_t d32;
  60105. + /** register bits */
  60106. + struct {
  60107. + /** OTG Data FIFO depth */
  60108. + unsigned gdfifocfg:16;
  60109. + /** Start address of EP info controller */
  60110. + unsigned epinfobase:16;
  60111. + } b;
  60112. +} gdfifocfg_data_t;
  60113. +
  60114. +/**
  60115. + * This union represents the bit fields in the Global Power Down Register
  60116. + * Register. Read the register into the <i>d32</i> member then set/clear the
  60117. + * bits using the <i>b</i>it elements.
  60118. + */
  60119. +typedef union gpwrdn_data {
  60120. + /* raw register data */
  60121. + uint32_t d32;
  60122. +
  60123. + /** register bits */
  60124. + struct {
  60125. + /** PMU Interrupt Select */
  60126. + unsigned pmuintsel:1;
  60127. + /** PMU Active */
  60128. + unsigned pmuactv:1;
  60129. + /** Restore */
  60130. + unsigned restore:1;
  60131. + /** Power Down Clamp */
  60132. + unsigned pwrdnclmp:1;
  60133. + /** Power Down Reset */
  60134. + unsigned pwrdnrstn:1;
  60135. + /** Power Down Switch */
  60136. + unsigned pwrdnswtch:1;
  60137. + /** Disable VBUS */
  60138. + unsigned dis_vbus:1;
  60139. + /** Line State Change */
  60140. + unsigned lnstschng:1;
  60141. + /** Line state change mask */
  60142. + unsigned lnstchng_msk:1;
  60143. + /** Reset Detected */
  60144. + unsigned rst_det:1;
  60145. + /** Reset Detect mask */
  60146. + unsigned rst_det_msk:1;
  60147. + /** Disconnect Detected */
  60148. + unsigned disconn_det:1;
  60149. + /** Disconnect Detect mask */
  60150. + unsigned disconn_det_msk:1;
  60151. + /** Connect Detected*/
  60152. + unsigned connect_det:1;
  60153. + /** Connect Detected Mask*/
  60154. + unsigned connect_det_msk:1;
  60155. + /** SRP Detected */
  60156. + unsigned srp_det:1;
  60157. + /** SRP Detect mask */
  60158. + unsigned srp_det_msk:1;
  60159. + /** Status Change Interrupt */
  60160. + unsigned sts_chngint:1;
  60161. + /** Status Change Interrupt Mask */
  60162. + unsigned sts_chngint_msk:1;
  60163. + /** Line State */
  60164. + unsigned linestate:2;
  60165. + /** Indicates current mode(status of IDDIG signal) */
  60166. + unsigned idsts:1;
  60167. + /** B Session Valid signal status*/
  60168. + unsigned bsessvld:1;
  60169. + /** ADP Event Detected */
  60170. + unsigned adp_int:1;
  60171. + /** Multi Valued ID pin */
  60172. + unsigned mult_val_id_bc:5;
  60173. + /** Reserved 24_31 */
  60174. + unsigned reserved29_31:3;
  60175. + } b;
  60176. +} gpwrdn_data_t;
  60177. +
  60178. +#endif
  60179. --- /dev/null
  60180. +++ b/drivers/usb/host/dwc_otg/test/Makefile
  60181. @@ -0,0 +1,16 @@
  60182. +
  60183. +PERL=/usr/bin/perl
  60184. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  60185. +
  60186. +.PHONY : test
  60187. +test : perl_tests
  60188. +
  60189. +perl_tests :
  60190. + @echo
  60191. + @echo Running perl tests
  60192. + @for test in $(PL_TESTS); do \
  60193. + if $(PERL) ./$$test ; then \
  60194. + echo "=======> $$test, PASSED" ; \
  60195. + else echo "=======> $$test, FAILED" ; \
  60196. + fi \
  60197. + done
  60198. --- /dev/null
  60199. +++ b/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  60200. @@ -0,0 +1,337 @@
  60201. +package dwc_otg_test;
  60202. +
  60203. +use strict;
  60204. +use Exporter ();
  60205. +
  60206. +use vars qw(@ISA @EXPORT
  60207. +$sysfsdir $paramdir $errors $params
  60208. +);
  60209. +
  60210. +@ISA = qw(Exporter);
  60211. +
  60212. +#
  60213. +# Globals
  60214. +#
  60215. +$sysfsdir = "/sys/devices/lm0";
  60216. +$paramdir = "/sys/module/dwc_otg";
  60217. +$errors = 0;
  60218. +
  60219. +$params = [
  60220. + {
  60221. + NAME => "otg_cap",
  60222. + DEFAULT => 0,
  60223. + ENUM => [],
  60224. + LOW => 0,
  60225. + HIGH => 2
  60226. + },
  60227. + {
  60228. + NAME => "dma_enable",
  60229. + DEFAULT => 0,
  60230. + ENUM => [],
  60231. + LOW => 0,
  60232. + HIGH => 1
  60233. + },
  60234. + {
  60235. + NAME => "dma_burst_size",
  60236. + DEFAULT => 32,
  60237. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  60238. + LOW => 1,
  60239. + HIGH => 256
  60240. + },
  60241. + {
  60242. + NAME => "host_speed",
  60243. + DEFAULT => 0,
  60244. + ENUM => [],
  60245. + LOW => 0,
  60246. + HIGH => 1
  60247. + },
  60248. + {
  60249. + NAME => "host_support_fs_ls_low_power",
  60250. + DEFAULT => 0,
  60251. + ENUM => [],
  60252. + LOW => 0,
  60253. + HIGH => 1
  60254. + },
  60255. + {
  60256. + NAME => "host_ls_low_power_phy_clk",
  60257. + DEFAULT => 0,
  60258. + ENUM => [],
  60259. + LOW => 0,
  60260. + HIGH => 1
  60261. + },
  60262. + {
  60263. + NAME => "dev_speed",
  60264. + DEFAULT => 0,
  60265. + ENUM => [],
  60266. + LOW => 0,
  60267. + HIGH => 1
  60268. + },
  60269. + {
  60270. + NAME => "enable_dynamic_fifo",
  60271. + DEFAULT => 1,
  60272. + ENUM => [],
  60273. + LOW => 0,
  60274. + HIGH => 1
  60275. + },
  60276. + {
  60277. + NAME => "data_fifo_size",
  60278. + DEFAULT => 8192,
  60279. + ENUM => [],
  60280. + LOW => 32,
  60281. + HIGH => 32768
  60282. + },
  60283. + {
  60284. + NAME => "dev_rx_fifo_size",
  60285. + DEFAULT => 1064,
  60286. + ENUM => [],
  60287. + LOW => 16,
  60288. + HIGH => 32768
  60289. + },
  60290. + {
  60291. + NAME => "dev_nperio_tx_fifo_size",
  60292. + DEFAULT => 1024,
  60293. + ENUM => [],
  60294. + LOW => 16,
  60295. + HIGH => 32768
  60296. + },
  60297. + {
  60298. + NAME => "dev_perio_tx_fifo_size_1",
  60299. + DEFAULT => 256,
  60300. + ENUM => [],
  60301. + LOW => 4,
  60302. + HIGH => 768
  60303. + },
  60304. + {
  60305. + NAME => "dev_perio_tx_fifo_size_2",
  60306. + DEFAULT => 256,
  60307. + ENUM => [],
  60308. + LOW => 4,
  60309. + HIGH => 768
  60310. + },
  60311. + {
  60312. + NAME => "dev_perio_tx_fifo_size_3",
  60313. + DEFAULT => 256,
  60314. + ENUM => [],
  60315. + LOW => 4,
  60316. + HIGH => 768
  60317. + },
  60318. + {
  60319. + NAME => "dev_perio_tx_fifo_size_4",
  60320. + DEFAULT => 256,
  60321. + ENUM => [],
  60322. + LOW => 4,
  60323. + HIGH => 768
  60324. + },
  60325. + {
  60326. + NAME => "dev_perio_tx_fifo_size_5",
  60327. + DEFAULT => 256,
  60328. + ENUM => [],
  60329. + LOW => 4,
  60330. + HIGH => 768
  60331. + },
  60332. + {
  60333. + NAME => "dev_perio_tx_fifo_size_6",
  60334. + DEFAULT => 256,
  60335. + ENUM => [],
  60336. + LOW => 4,
  60337. + HIGH => 768
  60338. + },
  60339. + {
  60340. + NAME => "dev_perio_tx_fifo_size_7",
  60341. + DEFAULT => 256,
  60342. + ENUM => [],
  60343. + LOW => 4,
  60344. + HIGH => 768
  60345. + },
  60346. + {
  60347. + NAME => "dev_perio_tx_fifo_size_8",
  60348. + DEFAULT => 256,
  60349. + ENUM => [],
  60350. + LOW => 4,
  60351. + HIGH => 768
  60352. + },
  60353. + {
  60354. + NAME => "dev_perio_tx_fifo_size_9",
  60355. + DEFAULT => 256,
  60356. + ENUM => [],
  60357. + LOW => 4,
  60358. + HIGH => 768
  60359. + },
  60360. + {
  60361. + NAME => "dev_perio_tx_fifo_size_10",
  60362. + DEFAULT => 256,
  60363. + ENUM => [],
  60364. + LOW => 4,
  60365. + HIGH => 768
  60366. + },
  60367. + {
  60368. + NAME => "dev_perio_tx_fifo_size_11",
  60369. + DEFAULT => 256,
  60370. + ENUM => [],
  60371. + LOW => 4,
  60372. + HIGH => 768
  60373. + },
  60374. + {
  60375. + NAME => "dev_perio_tx_fifo_size_12",
  60376. + DEFAULT => 256,
  60377. + ENUM => [],
  60378. + LOW => 4,
  60379. + HIGH => 768
  60380. + },
  60381. + {
  60382. + NAME => "dev_perio_tx_fifo_size_13",
  60383. + DEFAULT => 256,
  60384. + ENUM => [],
  60385. + LOW => 4,
  60386. + HIGH => 768
  60387. + },
  60388. + {
  60389. + NAME => "dev_perio_tx_fifo_size_14",
  60390. + DEFAULT => 256,
  60391. + ENUM => [],
  60392. + LOW => 4,
  60393. + HIGH => 768
  60394. + },
  60395. + {
  60396. + NAME => "dev_perio_tx_fifo_size_15",
  60397. + DEFAULT => 256,
  60398. + ENUM => [],
  60399. + LOW => 4,
  60400. + HIGH => 768
  60401. + },
  60402. + {
  60403. + NAME => "host_rx_fifo_size",
  60404. + DEFAULT => 1024,
  60405. + ENUM => [],
  60406. + LOW => 16,
  60407. + HIGH => 32768
  60408. + },
  60409. + {
  60410. + NAME => "host_nperio_tx_fifo_size",
  60411. + DEFAULT => 1024,
  60412. + ENUM => [],
  60413. + LOW => 16,
  60414. + HIGH => 32768
  60415. + },
  60416. + {
  60417. + NAME => "host_perio_tx_fifo_size",
  60418. + DEFAULT => 1024,
  60419. + ENUM => [],
  60420. + LOW => 16,
  60421. + HIGH => 32768
  60422. + },
  60423. + {
  60424. + NAME => "max_transfer_size",
  60425. + DEFAULT => 65535,
  60426. + ENUM => [],
  60427. + LOW => 2047,
  60428. + HIGH => 65535
  60429. + },
  60430. + {
  60431. + NAME => "max_packet_count",
  60432. + DEFAULT => 511,
  60433. + ENUM => [],
  60434. + LOW => 15,
  60435. + HIGH => 511
  60436. + },
  60437. + {
  60438. + NAME => "host_channels",
  60439. + DEFAULT => 12,
  60440. + ENUM => [],
  60441. + LOW => 1,
  60442. + HIGH => 16
  60443. + },
  60444. + {
  60445. + NAME => "dev_endpoints",
  60446. + DEFAULT => 6,
  60447. + ENUM => [],
  60448. + LOW => 1,
  60449. + HIGH => 15
  60450. + },
  60451. + {
  60452. + NAME => "phy_type",
  60453. + DEFAULT => 1,
  60454. + ENUM => [],
  60455. + LOW => 0,
  60456. + HIGH => 2
  60457. + },
  60458. + {
  60459. + NAME => "phy_utmi_width",
  60460. + DEFAULT => 16,
  60461. + ENUM => [8, 16],
  60462. + LOW => 8,
  60463. + HIGH => 16
  60464. + },
  60465. + {
  60466. + NAME => "phy_ulpi_ddr",
  60467. + DEFAULT => 0,
  60468. + ENUM => [],
  60469. + LOW => 0,
  60470. + HIGH => 1
  60471. + },
  60472. + ];
  60473. +
  60474. +
  60475. +#
  60476. +#
  60477. +sub check_arch {
  60478. + $_ = `uname -m`;
  60479. + chomp;
  60480. + unless (m/armv4tl/) {
  60481. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  60482. + return 0;
  60483. + }
  60484. + return 1;
  60485. +}
  60486. +
  60487. +#
  60488. +#
  60489. +sub load_module {
  60490. + my $params = shift;
  60491. + print "\nRemoving Module\n";
  60492. + system "rmmod dwc_otg";
  60493. + print "Loading Module\n";
  60494. + if ($params ne "") {
  60495. + print "Module Parameters: $params\n";
  60496. + }
  60497. + if (system("modprobe dwc_otg $params")) {
  60498. + warn "Unable to load module\n";
  60499. + return 0;
  60500. + }
  60501. + return 1;
  60502. +}
  60503. +
  60504. +#
  60505. +#
  60506. +sub test_status {
  60507. + my $arg = shift;
  60508. +
  60509. + print "\n";
  60510. +
  60511. + if (defined $arg) {
  60512. + warn "WARNING: $arg\n";
  60513. + }
  60514. +
  60515. + if ($errors > 0) {
  60516. + warn "TEST FAILED with $errors errors\n";
  60517. + return 0;
  60518. + } else {
  60519. + print "TEST PASSED\n";
  60520. + return 0 if (defined $arg);
  60521. + }
  60522. + return 1;
  60523. +}
  60524. +
  60525. +#
  60526. +#
  60527. +@EXPORT = qw(
  60528. +$sysfsdir
  60529. +$paramdir
  60530. +$params
  60531. +$errors
  60532. +check_arch
  60533. +load_module
  60534. +test_status
  60535. +);
  60536. +
  60537. +1;
  60538. --- /dev/null
  60539. +++ b/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  60540. @@ -0,0 +1,133 @@
  60541. +#!/usr/bin/perl -w
  60542. +#
  60543. +# Run this program on the integrator.
  60544. +#
  60545. +# - Tests module parameter default values.
  60546. +# - Tests setting of valid module parameter values via modprobe.
  60547. +# - Tests invalid module parameter values.
  60548. +# -----------------------------------------------------------------------------
  60549. +use strict;
  60550. +use dwc_otg_test;
  60551. +
  60552. +check_arch() or die;
  60553. +
  60554. +#
  60555. +#
  60556. +sub test {
  60557. + my ($param,$expected) = @_;
  60558. + my $value = get($param);
  60559. +
  60560. + if ($value == $expected) {
  60561. + print "$param = $value, okay\n";
  60562. + }
  60563. +
  60564. + else {
  60565. + warn "ERROR: value of $param != $expected, $value\n";
  60566. + $errors ++;
  60567. + }
  60568. +}
  60569. +
  60570. +#
  60571. +#
  60572. +sub get {
  60573. + my $param = shift;
  60574. + my $tmp = `cat $paramdir/$param`;
  60575. + chomp $tmp;
  60576. + return $tmp;
  60577. +}
  60578. +
  60579. +#
  60580. +#
  60581. +sub test_main {
  60582. +
  60583. + print "\nTesting Module Parameters\n";
  60584. +
  60585. + load_module("") or die;
  60586. +
  60587. + # Test initial values
  60588. + print "\nTesting Default Values\n";
  60589. + foreach (@{$params}) {
  60590. + test ($_->{NAME}, $_->{DEFAULT});
  60591. + }
  60592. +
  60593. + # Test low value
  60594. + print "\nTesting Low Value\n";
  60595. + my $cmd_params = "";
  60596. + foreach (@{$params}) {
  60597. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  60598. + }
  60599. + load_module($cmd_params) or die;
  60600. +
  60601. + foreach (@{$params}) {
  60602. + test ($_->{NAME}, $_->{LOW});
  60603. + }
  60604. +
  60605. + # Test high value
  60606. + print "\nTesting High Value\n";
  60607. + $cmd_params = "";
  60608. + foreach (@{$params}) {
  60609. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  60610. + }
  60611. + load_module($cmd_params) or die;
  60612. +
  60613. + foreach (@{$params}) {
  60614. + test ($_->{NAME}, $_->{HIGH});
  60615. + }
  60616. +
  60617. + # Test Enum
  60618. + print "\nTesting Enumerated\n";
  60619. + foreach (@{$params}) {
  60620. + if (defined $_->{ENUM}) {
  60621. + my $value;
  60622. + foreach $value (@{$_->{ENUM}}) {
  60623. + $cmd_params = "$_->{NAME}=$value";
  60624. + load_module($cmd_params) or die;
  60625. + test ($_->{NAME}, $value);
  60626. + }
  60627. + }
  60628. + }
  60629. +
  60630. + # Test Invalid Values
  60631. + print "\nTesting Invalid Values\n";
  60632. + $cmd_params = "";
  60633. + foreach (@{$params}) {
  60634. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  60635. + }
  60636. + load_module($cmd_params) or die;
  60637. +
  60638. + foreach (@{$params}) {
  60639. + test ($_->{NAME}, $_->{DEFAULT});
  60640. + }
  60641. +
  60642. + $cmd_params = "";
  60643. + foreach (@{$params}) {
  60644. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  60645. + }
  60646. + load_module($cmd_params) or die;
  60647. +
  60648. + foreach (@{$params}) {
  60649. + test ($_->{NAME}, $_->{DEFAULT});
  60650. + }
  60651. +
  60652. + print "\nTesting Enumerated\n";
  60653. + foreach (@{$params}) {
  60654. + if (defined $_->{ENUM}) {
  60655. + my $value;
  60656. + foreach $value (@{$_->{ENUM}}) {
  60657. + $value = $value + 1;
  60658. + $cmd_params = "$_->{NAME}=$value";
  60659. + load_module($cmd_params) or die;
  60660. + test ($_->{NAME}, $_->{DEFAULT});
  60661. + $value = $value - 2;
  60662. + $cmd_params = "$_->{NAME}=$value";
  60663. + load_module($cmd_params) or die;
  60664. + test ($_->{NAME}, $_->{DEFAULT});
  60665. + }
  60666. + }
  60667. + }
  60668. +
  60669. + test_status() or die;
  60670. +}
  60671. +
  60672. +test_main();
  60673. +0;
  60674. --- /dev/null
  60675. +++ b/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  60676. @@ -0,0 +1,193 @@
  60677. +#!/usr/bin/perl -w
  60678. +#
  60679. +# Run this program on the integrator
  60680. +# - Tests select sysfs attributes.
  60681. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  60682. +# -----------------------------------------------------------------------------
  60683. +use strict;
  60684. +use dwc_otg_test;
  60685. +
  60686. +check_arch() or die;
  60687. +
  60688. +#
  60689. +#
  60690. +sub test {
  60691. + my ($attr,$expected) = @_;
  60692. + my $string = get($attr);
  60693. +
  60694. + if ($string eq $expected) {
  60695. + printf("$attr = $string, okay\n");
  60696. + }
  60697. + else {
  60698. + warn "ERROR: value of $attr != $expected, $string\n";
  60699. + $errors ++;
  60700. + }
  60701. +}
  60702. +
  60703. +#
  60704. +#
  60705. +sub set {
  60706. + my ($reg, $value) = @_;
  60707. + system "echo $value > $sysfsdir/$reg";
  60708. +}
  60709. +
  60710. +#
  60711. +#
  60712. +sub get {
  60713. + my $attr = shift;
  60714. + my $string = `cat $sysfsdir/$attr`;
  60715. + chomp $string;
  60716. + if ($string =~ m/\s\=\s/) {
  60717. + my $tmp;
  60718. + ($tmp, $string) = split /\s=\s/, $string;
  60719. + }
  60720. + return $string;
  60721. +}
  60722. +
  60723. +#
  60724. +#
  60725. +sub test_main {
  60726. + print("\nTesting Sysfs Attributes\n");
  60727. +
  60728. + load_module("") or die;
  60729. +
  60730. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  60731. + print("\nTesting Default Values\n");
  60732. +
  60733. + test("regoffset", "0xffffffff");
  60734. + test("regvalue", "invalid offset");
  60735. + test("guid", "0x12345678"); # this will fail if it has been changed
  60736. + test("gsnpsid", "0x4f54200a");
  60737. +
  60738. + # Test operation of regoffset/regvalue
  60739. + print("\nTesting regoffset\n");
  60740. + set('regoffset', '5a5a5a5a');
  60741. + test("regoffset", "0xffffffff");
  60742. +
  60743. + set('regoffset', '0');
  60744. + test("regoffset", "0x00000000");
  60745. +
  60746. + set('regoffset', '40000');
  60747. + test("regoffset", "0x00000000");
  60748. +
  60749. + set('regoffset', '3ffff');
  60750. + test("regoffset", "0x0003ffff");
  60751. +
  60752. + set('regoffset', '1');
  60753. + test("regoffset", "0x00000001");
  60754. +
  60755. + print("\nTesting regvalue\n");
  60756. + set('regoffset', '3c');
  60757. + test("regvalue", "0x12345678");
  60758. + set('regvalue', '5a5a5a5a');
  60759. + test("regvalue", "0x5a5a5a5a");
  60760. + set('regvalue','a5a5a5a5');
  60761. + test("regvalue", "0xa5a5a5a5");
  60762. + set('guid','12345678');
  60763. +
  60764. + # Test HNP Capable
  60765. + print("\nTesting HNP Capable bit\n");
  60766. + set('hnpcapable', '1');
  60767. + test("hnpcapable", "0x1");
  60768. + set('hnpcapable','0');
  60769. + test("hnpcapable", "0x0");
  60770. +
  60771. + set('regoffset','0c');
  60772. +
  60773. + my $old = get('gusbcfg');
  60774. + print("setting hnpcapable\n");
  60775. + set('hnpcapable', '1');
  60776. + test("hnpcapable", "0x1");
  60777. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  60778. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  60779. +
  60780. + $old = get('gusbcfg');
  60781. + print("clearing hnpcapable\n");
  60782. + set('hnpcapable', '0');
  60783. + test("hnpcapable", "0x0");
  60784. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  60785. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  60786. +
  60787. + # Test SRP Capable
  60788. + print("\nTesting SRP Capable bit\n");
  60789. + set('srpcapable', '1');
  60790. + test("srpcapable", "0x1");
  60791. + set('srpcapable','0');
  60792. + test("srpcapable", "0x0");
  60793. +
  60794. + set('regoffset','0c');
  60795. +
  60796. + $old = get('gusbcfg');
  60797. + print("setting srpcapable\n");
  60798. + set('srpcapable', '1');
  60799. + test("srpcapable", "0x1");
  60800. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  60801. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  60802. +
  60803. + $old = get('gusbcfg');
  60804. + print("clearing srpcapable\n");
  60805. + set('srpcapable', '0');
  60806. + test("srpcapable", "0x0");
  60807. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  60808. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  60809. +
  60810. + # Test GGPIO
  60811. + print("\nTesting GGPIO\n");
  60812. + set('ggpio','5a5a5a5a');
  60813. + test('ggpio','0x5a5a0000');
  60814. + set('ggpio','a5a5a5a5');
  60815. + test('ggpio','0xa5a50000');
  60816. + set('ggpio','11110000');
  60817. + test('ggpio','0x11110000');
  60818. + set('ggpio','00001111');
  60819. + test('ggpio','0x00000000');
  60820. +
  60821. + # Test DEVSPEED
  60822. + print("\nTesting DEVSPEED\n");
  60823. + set('regoffset','800');
  60824. + $old = get('regvalue');
  60825. + set('devspeed','0');
  60826. + test('devspeed','0x0');
  60827. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  60828. + set('devspeed','1');
  60829. + test('devspeed','0x1');
  60830. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  60831. + set('devspeed','2');
  60832. + test('devspeed','0x2');
  60833. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  60834. + set('devspeed','3');
  60835. + test('devspeed','0x3');
  60836. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  60837. + set('devspeed','4');
  60838. + test('devspeed','0x0');
  60839. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  60840. + set('devspeed','5');
  60841. + test('devspeed','0x1');
  60842. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  60843. +
  60844. +
  60845. + # mode Returns the current mode:0 for device mode1 for host mode Read
  60846. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  60847. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  60848. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  60849. + # bussuspend Suspend the USB bus. Read/Write
  60850. + # busconnected Get the connection status of the bus Read
  60851. +
  60852. + # gotgctl Get or set the Core Control Status Register. Read/Write
  60853. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  60854. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  60855. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  60856. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  60857. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  60858. + ## guid Get or set the value of the User ID Register Read/Write
  60859. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  60860. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  60861. + # enumspeed Gets the device enumeration Speed. Read
  60862. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  60863. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  60864. +
  60865. + test_status("TEST NYI") or die;
  60866. +}
  60867. +
  60868. +test_main();
  60869. +0;