0029-Add-dwc_otg-driver.patch 1.7 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780
  1. From afd337386d8e58d2590d8f6c6ac85cfc1ba244a5 Mon Sep 17 00:00:00 2001
  2. From: popcornmix <popcornmix@gmail.com>
  3. Date: Wed, 1 May 2013 19:46:17 +0100
  4. Subject: [PATCH 029/381] 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 <popcornmix@gmail.com>
  9. usb: dwc: fix lockdep false positive
  10. Signed-off-by: Kari Suvanto <karis79@gmail.com>
  11. usb: dwc: fix inconsistent lock state
  12. Signed-off-by: Kari Suvanto <karis79@gmail.com>
  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 <noralf@tronnes.org>
  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 <noralf@tronnes.org>
  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 <noralf@tronnes.org>
  318. dwc_otg: Remove duplicate gadget probe/unregister function
  319. ---
  320. arch/arm/include/asm/irqflags.h | 16 +-
  321. arch/arm/kernel/fiqasm.S | 4 +
  322. drivers/usb/Makefile | 1 +
  323. drivers/usb/core/generic.c | 1 +
  324. drivers/usb/core/hub.c | 2 +-
  325. drivers/usb/core/message.c | 79 +
  326. drivers/usb/core/otg_whitelist.h | 114 +-
  327. drivers/usb/gadget/file_storage.c | 3676 ++++++++++
  328. drivers/usb/host/Kconfig | 13 +
  329. drivers/usb/host/Makefile | 2 +
  330. drivers/usb/host/dwc_common_port/Makefile | 58 +
  331. drivers/usb/host/dwc_common_port/Makefile.fbsd | 17 +
  332. drivers/usb/host/dwc_common_port/Makefile.linux | 49 +
  333. drivers/usb/host/dwc_common_port/changes.txt | 174 +
  334. drivers/usb/host/dwc_common_port/doc/doxygen.cfg | 270 +
  335. drivers/usb/host/dwc_common_port/dwc_cc.c | 532 ++
  336. drivers/usb/host/dwc_common_port/dwc_cc.h | 224 +
  337. drivers/usb/host/dwc_common_port/dwc_common_fbsd.c | 1308 ++++
  338. .../usb/host/dwc_common_port/dwc_common_linux.c | 1433 ++++
  339. drivers/usb/host/dwc_common_port/dwc_common_nbsd.c | 1275 ++++
  340. drivers/usb/host/dwc_common_port/dwc_crypto.c | 308 +
  341. drivers/usb/host/dwc_common_port/dwc_crypto.h | 111 +
  342. drivers/usb/host/dwc_common_port/dwc_dh.c | 291 +
  343. drivers/usb/host/dwc_common_port/dwc_dh.h | 106 +
  344. drivers/usb/host/dwc_common_port/dwc_list.h | 594 ++
  345. drivers/usb/host/dwc_common_port/dwc_mem.c | 245 +
  346. drivers/usb/host/dwc_common_port/dwc_modpow.c | 636 ++
  347. drivers/usb/host/dwc_common_port/dwc_modpow.h | 34 +
  348. drivers/usb/host/dwc_common_port/dwc_notifier.c | 319 +
  349. drivers/usb/host/dwc_common_port/dwc_notifier.h | 122 +
  350. drivers/usb/host/dwc_common_port/dwc_os.h | 1276 ++++
  351. drivers/usb/host/dwc_common_port/usb.h | 946 +++
  352. drivers/usb/host/dwc_otg/Makefile | 82 +
  353. drivers/usb/host/dwc_otg/doc/doxygen.cfg | 224 +
  354. drivers/usb/host/dwc_otg/dummy_audio.c | 1575 +++++
  355. drivers/usb/host/dwc_otg/dwc_cfi_common.h | 142 +
  356. drivers/usb/host/dwc_otg/dwc_otg_adp.c | 854 +++
  357. drivers/usb/host/dwc_otg/dwc_otg_adp.h | 80 +
  358. drivers/usb/host/dwc_otg/dwc_otg_attr.c | 1210 ++++
  359. drivers/usb/host/dwc_otg/dwc_otg_attr.h | 89 +
  360. drivers/usb/host/dwc_otg/dwc_otg_cfi.c | 1876 +++++
  361. drivers/usb/host/dwc_otg/dwc_otg_cfi.h | 320 +
  362. drivers/usb/host/dwc_otg/dwc_otg_cil.c | 7141 ++++++++++++++++++++
  363. drivers/usb/host/dwc_otg/dwc_otg_cil.h | 1464 ++++
  364. drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c | 1594 +++++
  365. drivers/usb/host/dwc_otg/dwc_otg_core_if.h | 705 ++
  366. drivers/usb/host/dwc_otg/dwc_otg_dbg.h | 117 +
  367. drivers/usb/host/dwc_otg/dwc_otg_driver.c | 1757 +++++
  368. drivers/usb/host/dwc_otg/dwc_otg_driver.h | 86 +
  369. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 1355 ++++
  370. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 370 +
  371. drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S | 80 +
  372. drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 4257 ++++++++++++
  373. drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 862 +++
  374. drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c | 1132 ++++
  375. drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h | 417 ++
  376. drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 2714 ++++++++
  377. drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 1005 +++
  378. drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c | 957 +++
  379. drivers/usb/host/dwc_otg/dwc_otg_os_dep.h | 188 +
  380. drivers/usb/host/dwc_otg/dwc_otg_pcd.c | 2712 ++++++++
  381. drivers/usb/host/dwc_otg/dwc_otg_pcd.h | 266 +
  382. drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h | 360 +
  383. drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c | 5147 ++++++++++++++
  384. drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c | 1280 ++++
  385. drivers/usb/host/dwc_otg/dwc_otg_regs.h | 2550 +++++++
  386. drivers/usb/host/dwc_otg/test/Makefile | 16 +
  387. drivers/usb/host/dwc_otg/test/dwc_otg_test.pm | 337 +
  388. drivers/usb/host/dwc_otg/test/test_mod_param.pl | 133 +
  389. drivers/usb/host/dwc_otg/test/test_sysfs.pl | 193 +
  390. 70 files changed, 59867 insertions(+), 16 deletions(-)
  391. create mode 100644 drivers/usb/gadget/file_storage.c
  392. create mode 100644 drivers/usb/host/dwc_common_port/Makefile
  393. create mode 100644 drivers/usb/host/dwc_common_port/Makefile.fbsd
  394. create mode 100644 drivers/usb/host/dwc_common_port/Makefile.linux
  395. create mode 100644 drivers/usb/host/dwc_common_port/changes.txt
  396. create mode 100644 drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  397. create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.c
  398. create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.h
  399. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  400. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_linux.c
  401. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  402. create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.c
  403. create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.h
  404. create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.c
  405. create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.h
  406. create mode 100644 drivers/usb/host/dwc_common_port/dwc_list.h
  407. create mode 100644 drivers/usb/host/dwc_common_port/dwc_mem.c
  408. create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.c
  409. create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.h
  410. create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.c
  411. create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.h
  412. create mode 100644 drivers/usb/host/dwc_common_port/dwc_os.h
  413. create mode 100644 drivers/usb/host/dwc_common_port/usb.h
  414. create mode 100644 drivers/usb/host/dwc_otg/Makefile
  415. create mode 100644 drivers/usb/host/dwc_otg/doc/doxygen.cfg
  416. create mode 100644 drivers/usb/host/dwc_otg/dummy_audio.c
  417. create mode 100644 drivers/usb/host/dwc_otg/dwc_cfi_common.h
  418. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.c
  419. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.h
  420. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.c
  421. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.h
  422. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  423. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  424. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.c
  425. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.h
  426. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  427. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  428. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  429. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.c
  430. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.h
  431. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  432. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  433. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  434. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  435. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  436. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  437. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  438. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  439. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  440. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  441. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  442. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  443. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  444. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  445. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  446. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  447. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_regs.h
  448. create mode 100644 drivers/usb/host/dwc_otg/test/Makefile
  449. create mode 100644 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  450. create mode 100644 drivers/usb/host/dwc_otg/test/test_mod_param.pl
  451. create mode 100644 drivers/usb/host/dwc_otg/test/test_sysfs.pl
  452. --- a/arch/arm/include/asm/irqflags.h
  453. +++ b/arch/arm/include/asm/irqflags.h
  454. @@ -162,13 +162,23 @@ static inline unsigned long arch_local_s
  455. }
  456. /*
  457. - * restore saved IRQ & FIQ state
  458. + * restore saved IRQ state
  459. */
  460. #define arch_local_irq_restore arch_local_irq_restore
  461. static inline void arch_local_irq_restore(unsigned long flags)
  462. {
  463. - asm volatile(
  464. - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
  465. + unsigned long temp = 0;
  466. + flags &= ~(1 << 6);
  467. + asm volatile (
  468. + " mrs %0, cpsr"
  469. + : "=r" (temp)
  470. + :
  471. + : "memory", "cc");
  472. + /* Preserve FIQ bit */
  473. + temp &= (1 << 6);
  474. + flags = flags | temp;
  475. + asm volatile (
  476. + " msr cpsr_c, %0 @ local_irq_restore"
  477. :
  478. : "r" (flags)
  479. : "memory", "cc");
  480. --- a/arch/arm/kernel/fiqasm.S
  481. +++ b/arch/arm/kernel/fiqasm.S
  482. @@ -47,3 +47,7 @@ ENTRY(__get_fiq_regs)
  483. mov r0, r0 @ avoid hazard prior to ARMv4
  484. ret lr
  485. ENDPROC(__get_fiq_regs)
  486. +
  487. +ENTRY(__FIQ_Branch)
  488. + mov pc, r8
  489. +ENDPROC(__FIQ_Branch)
  490. --- a/drivers/usb/Makefile
  491. +++ b/drivers/usb/Makefile
  492. @@ -7,6 +7,7 @@
  493. obj-$(CONFIG_USB) += core/
  494. obj-$(CONFIG_USB_SUPPORT) += phy/
  495. +obj-$(CONFIG_USB_DWCOTG) += host/
  496. obj-$(CONFIG_USB_DWC3) += dwc3/
  497. obj-$(CONFIG_USB_DWC2) += dwc2/
  498. obj-$(CONFIG_USB_ISP1760) += isp1760/
  499. --- a/drivers/usb/core/generic.c
  500. +++ b/drivers/usb/core/generic.c
  501. @@ -152,6 +152,7 @@ int usb_choose_configuration(struct usb_
  502. dev_warn(&udev->dev,
  503. "no configuration chosen from %d choice%s\n",
  504. num_configs, plural(num_configs));
  505. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  506. }
  507. return i;
  508. }
  509. --- a/drivers/usb/core/hub.c
  510. +++ b/drivers/usb/core/hub.c
  511. @@ -4973,7 +4973,7 @@ static void port_event(struct usb_hub *h
  512. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  513. u16 status = 0, unused;
  514. - dev_dbg(&port_dev->dev, "over-current change\n");
  515. + dev_notice(&port_dev->dev, "over-current change\n");
  516. usb_clear_port_feature(hdev, port1,
  517. USB_PORT_FEAT_C_OVER_CURRENT);
  518. msleep(100); /* Cool down */
  519. --- a/drivers/usb/core/message.c
  520. +++ b/drivers/usb/core/message.c
  521. @@ -1909,6 +1909,85 @@ free_interfaces:
  522. if (cp->string == NULL &&
  523. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  524. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  525. +/* Uncomment this define to enable the HS Electrical Test support */
  526. +#define DWC_HS_ELECT_TST 1
  527. +#ifdef DWC_HS_ELECT_TST
  528. + /* Here we implement the HS Electrical Test support. The
  529. + * tester uses a vendor ID of 0x1A0A to indicate we should
  530. + * run a special test sequence. The product ID tells us
  531. + * which sequence to run. We invoke the test sequence by
  532. + * sending a non-standard SetFeature command to our root
  533. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  534. + * recognize the command and perform the desired test
  535. + * sequence.
  536. + */
  537. + if (dev->descriptor.idVendor == 0x1A0A) {
  538. + /* HSOTG Electrical Test */
  539. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  540. +
  541. + if (dev->bus && dev->bus->root_hub) {
  542. + struct usb_device *hdev = dev->bus->root_hub;
  543. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  544. +
  545. + switch (dev->descriptor.idProduct) {
  546. + case 0x0101: /* TEST_SE0_NAK */
  547. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  548. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  549. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  550. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  551. + break;
  552. +
  553. + case 0x0102: /* TEST_J */
  554. + dev_warn(&dev->dev, "TEST_J\n");
  555. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  556. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  557. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  558. + break;
  559. +
  560. + case 0x0103: /* TEST_K */
  561. + dev_warn(&dev->dev, "TEST_K\n");
  562. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  563. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  564. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  565. + break;
  566. +
  567. + case 0x0104: /* TEST_PACKET */
  568. + dev_warn(&dev->dev, "TEST_PACKET\n");
  569. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  570. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  571. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  572. + break;
  573. +
  574. + case 0x0105: /* TEST_FORCE_ENABLE */
  575. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  576. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  577. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  578. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  579. + break;
  580. +
  581. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  582. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  583. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  584. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  585. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  586. + break;
  587. +
  588. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  589. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  590. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  591. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  592. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  593. + break;
  594. +
  595. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  596. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  597. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  598. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  599. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  600. + }
  601. + }
  602. + }
  603. +#endif /* DWC_HS_ELECT_TST */
  604. /* Now that the interfaces are installed, re-enable LPM. */
  605. usb_unlocked_enable_lpm(dev);
  606. --- a/drivers/usb/core/otg_whitelist.h
  607. +++ b/drivers/usb/core/otg_whitelist.h
  608. @@ -19,33 +19,82 @@
  609. static struct usb_device_id whitelist_table[] = {
  610. /* hubs are optional in OTG, but very handy ... */
  611. +#define CERT_WITHOUT_HUBS
  612. +#if defined(CERT_WITHOUT_HUBS)
  613. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  614. +#else
  615. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  616. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  617. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  618. +#endif
  619. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  620. /* FIXME actually, printers are NOT supposed to use device classes;
  621. * they're supposed to use interface classes...
  622. */
  623. -{ USB_DEVICE_INFO(7, 1, 1) },
  624. -{ USB_DEVICE_INFO(7, 1, 2) },
  625. -{ USB_DEVICE_INFO(7, 1, 3) },
  626. +//{ USB_DEVICE_INFO(7, 1, 1) },
  627. +//{ USB_DEVICE_INFO(7, 1, 2) },
  628. +//{ USB_DEVICE_INFO(7, 1, 3) },
  629. #endif
  630. #ifdef CONFIG_USB_NET_CDCETHER
  631. /* Linux-USB CDC Ethernet gadget */
  632. -{ USB_DEVICE(0x0525, 0xa4a1), },
  633. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  634. /* Linux-USB CDC Ethernet + RNDIS gadget */
  635. -{ USB_DEVICE(0x0525, 0xa4a2), },
  636. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  637. #endif
  638. #if defined(CONFIG_USB_TEST) || defined(CONFIG_USB_TEST_MODULE)
  639. /* gadget zero, for testing */
  640. -{ USB_DEVICE(0x0525, 0xa4a0), },
  641. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  642. #endif
  643. +/* OPT Tester */
  644. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  645. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  646. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  647. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  648. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  649. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  650. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  651. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  652. +
  653. +/* Sony cameras */
  654. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  655. +
  656. +/* Memory Devices */
  657. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  658. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  659. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  660. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  661. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  662. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  663. +
  664. +/* HP Printers */
  665. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  666. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  667. +
  668. +/* Speakers */
  669. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  670. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  671. +
  672. { } /* Terminating entry */
  673. };
  674. +static inline void report_errors(struct usb_device *dev)
  675. +{
  676. + /* OTG MESSAGE: report errors here, customize to match your product */
  677. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  678. + le16_to_cpu(dev->descriptor.idVendor),
  679. + le16_to_cpu(dev->descriptor.idProduct));
  680. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  681. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  682. + } else {
  683. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  684. + }
  685. +}
  686. +
  687. +
  688. static int is_targeted(struct usb_device *dev)
  689. {
  690. struct usb_device_id *id = whitelist_table;
  691. @@ -95,16 +144,57 @@ static int is_targeted(struct usb_device
  692. continue;
  693. return 1;
  694. - }
  695. + /* NOTE: can't use usb_match_id() since interface caches
  696. + * aren't set up yet. this is cut/paste from that code.
  697. + */
  698. + for (id = whitelist_table; id->match_flags; id++) {
  699. +#ifdef DEBUG
  700. + dev_dbg(&dev->dev,
  701. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  702. + id->idVendor,
  703. + id->idProduct,
  704. + id->bDeviceClass,
  705. + id->bDeviceSubClass,
  706. + id->bDeviceProtocol);
  707. +#endif
  708. - /* add other match criteria here ... */
  709. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  710. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  711. + continue;
  712. +
  713. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  714. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  715. + continue;
  716. +
  717. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  718. + greater than any unsigned number. */
  719. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  720. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  721. + continue;
  722. +
  723. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  724. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  725. + continue;
  726. +
  727. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  728. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  729. + continue;
  730. +
  731. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  732. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  733. + continue;
  734. +
  735. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  736. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  737. + continue;
  738. + return 1;
  739. + }
  740. + }
  741. - /* OTG MESSAGE: report errors here, customize to match your product */
  742. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  743. - le16_to_cpu(dev->descriptor.idVendor),
  744. - le16_to_cpu(dev->descriptor.idProduct));
  745. + /* add other match criteria here ... */
  746. + report_errors(dev);
  747. return 0;
  748. }
  749. --- /dev/null
  750. +++ b/drivers/usb/gadget/file_storage.c
  751. @@ -0,0 +1,3676 @@
  752. +/*
  753. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  754. + *
  755. + * Copyright (C) 2003-2008 Alan Stern
  756. + * All rights reserved.
  757. + *
  758. + * Redistribution and use in source and binary forms, with or without
  759. + * modification, are permitted provided that the following conditions
  760. + * are met:
  761. + * 1. Redistributions of source code must retain the above copyright
  762. + * notice, this list of conditions, and the following disclaimer,
  763. + * without modification.
  764. + * 2. Redistributions in binary form must reproduce the above copyright
  765. + * notice, this list of conditions and the following disclaimer in the
  766. + * documentation and/or other materials provided with the distribution.
  767. + * 3. The names of the above-listed copyright holders may not be used
  768. + * to endorse or promote products derived from this software without
  769. + * specific prior written permission.
  770. + *
  771. + * ALTERNATIVELY, this software may be distributed under the terms of the
  772. + * GNU General Public License ("GPL") as published by the Free Software
  773. + * Foundation, either version 2 of that License or (at your option) any
  774. + * later version.
  775. + *
  776. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  777. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  778. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  779. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  780. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  781. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  782. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  783. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  784. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  785. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  786. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  787. + */
  788. +
  789. +
  790. +/*
  791. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  792. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  793. + * to providing an example of a genuinely useful gadget driver for a USB
  794. + * device, it also illustrates a technique of double-buffering for increased
  795. + * throughput. Last but not least, it gives an easy way to probe the
  796. + * behavior of the Mass Storage drivers in a USB host.
  797. + *
  798. + * Backing storage is provided by a regular file or a block device, specified
  799. + * by the "file" module parameter. Access can be limited to read-only by
  800. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  801. + * access is always read-only.) The gadget will indicate that it has
  802. + * removable media if the optional "removable" module parameter is set.
  803. + *
  804. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  805. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  806. + * by the optional "transport" module parameter. It also supports the
  807. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  808. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  809. + * the optional "protocol" module parameter. In addition, the default
  810. + * Vendor ID, Product ID, release number and serial number can be overridden.
  811. + *
  812. + * There is support for multiple logical units (LUNs), each of which has
  813. + * its own backing file. The number of LUNs can be set using the optional
  814. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  815. + * files are specified using comma-separated lists for "file" and "ro".
  816. + * The default number of LUNs is taken from the number of "file" elements;
  817. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  818. + * file must be specified for each LUN. If it is set, then an unspecified
  819. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  820. + * each LUN would be settable independently as a disk drive or a CD-ROM
  821. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  822. + * emulation includes a single data track and no audio tracks; hence there
  823. + * need be only one backing file per LUN.
  824. + *
  825. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  826. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  827. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  828. + * Support is included for both full-speed and high-speed operation.
  829. + *
  830. + * Note that the driver is slightly non-portable in that it assumes a
  831. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  832. + * interrupt-in endpoints. With most device controllers this isn't an
  833. + * issue, but there may be some with hardware restrictions that prevent
  834. + * a buffer from being used by more than one endpoint.
  835. + *
  836. + * Module options:
  837. + *
  838. + * file=filename[,filename...]
  839. + * Required if "removable" is not set, names of
  840. + * the files or block devices used for
  841. + * backing storage
  842. + * serial=HHHH... Required serial number (string of hex chars)
  843. + * ro=b[,b...] Default false, booleans for read-only access
  844. + * removable Default false, boolean for removable media
  845. + * luns=N Default N = number of filenames, number of
  846. + * LUNs to support
  847. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  848. + * in SCSI WRITE(10,12) commands
  849. + * stall Default determined according to the type of
  850. + * USB device controller (usually true),
  851. + * boolean to permit the driver to halt
  852. + * bulk endpoints
  853. + * cdrom Default false, boolean for whether to emulate
  854. + * a CD-ROM drive
  855. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  856. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  857. + * ATAPI, QIC, UFI, 8070, or SCSI;
  858. + * also 1 - 6)
  859. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  860. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  861. + * release=0xRRRR Override the USB release number (bcdDevice)
  862. + * buflen=N Default N=16384, buffer size used (will be
  863. + * rounded down to a multiple of
  864. + * PAGE_CACHE_SIZE)
  865. + *
  866. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  867. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  868. + * default values are used for everything else.
  869. + *
  870. + * The pathnames of the backing files and the ro settings are available in
  871. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  872. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  873. + * these files will simulate ejecting/loading the medium (writing an empty
  874. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  875. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  876. + * is being used.
  877. + *
  878. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  879. + * The driver's SCSI command interface was based on the "Information
  880. + * technology - Small Computer System Interface - 2" document from
  881. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  882. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  883. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  884. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  885. + * document, Revision 1.0, December 14, 1998, available at
  886. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  887. + */
  888. +
  889. +
  890. +/*
  891. + * Driver Design
  892. + *
  893. + * The FSG driver is fairly straightforward. There is a main kernel
  894. + * thread that handles most of the work. Interrupt routines field
  895. + * callbacks from the controller driver: bulk- and interrupt-request
  896. + * completion notifications, endpoint-0 events, and disconnect events.
  897. + * Completion events are passed to the main thread by wakeup calls. Many
  898. + * ep0 requests are handled at interrupt time, but SetInterface,
  899. + * SetConfiguration, and device reset requests are forwarded to the
  900. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  901. + * should interrupt any ongoing file I/O operations).
  902. + *
  903. + * The thread's main routine implements the standard command/data/status
  904. + * parts of a SCSI interaction. It and its subroutines are full of tests
  905. + * for pending signals/exceptions -- all this polling is necessary since
  906. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  907. + * indication that the driver really wants to be running in userspace.)
  908. + * An important point is that so long as the thread is alive it keeps an
  909. + * open reference to the backing file. This will prevent unmounting
  910. + * the backing file's underlying filesystem and could cause problems
  911. + * during system shutdown, for example. To prevent such problems, the
  912. + * thread catches INT, TERM, and KILL signals and converts them into
  913. + * an EXIT exception.
  914. + *
  915. + * In normal operation the main thread is started during the gadget's
  916. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  917. + * exit when it receives a signal, and there's no point leaving the
  918. + * gadget running when the thread is dead. So just before the thread
  919. + * exits, it deregisters the gadget driver. This makes things a little
  920. + * tricky: The driver is deregistered at two places, and the exiting
  921. + * thread can indirectly call fsg_unbind() which in turn can tell the
  922. + * thread to exit. The first problem is resolved through the use of the
  923. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  924. + * The second problem is resolved by having fsg_unbind() check
  925. + * fsg->state; it won't try to stop the thread if the state is already
  926. + * FSG_STATE_TERMINATED.
  927. + *
  928. + * To provide maximum throughput, the driver uses a circular pipeline of
  929. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  930. + * arbitrarily long; in practice the benefits don't justify having more
  931. + * than 2 stages (i.e., double buffering). But it helps to think of the
  932. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  933. + * a bulk-out request pointer (since the buffer can be used for both
  934. + * output and input -- directions always are given from the host's
  935. + * point of view) as well as a pointer to the buffer and various state
  936. + * variables.
  937. + *
  938. + * Use of the pipeline follows a simple protocol. There is a variable
  939. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  940. + * At any time that buffer head may still be in use from an earlier
  941. + * request, so each buffer head has a state variable indicating whether
  942. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  943. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  944. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  945. + * head FULL when the I/O is complete. Then the buffer will be emptied
  946. + * (again possibly by USB I/O, during which it is marked BUSY) and
  947. + * finally marked EMPTY again (possibly by a completion routine).
  948. + *
  949. + * A module parameter tells the driver to avoid stalling the bulk
  950. + * endpoints wherever the transport specification allows. This is
  951. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  952. + * halt on a bulk endpoint. However, under certain circumstances the
  953. + * Bulk-only specification requires a stall. In such cases the driver
  954. + * will halt the endpoint and set a flag indicating that it should clear
  955. + * the halt in software during the next device reset. Hopefully this
  956. + * will permit everything to work correctly. Furthermore, although the
  957. + * specification allows the bulk-out endpoint to halt when the host sends
  958. + * too much data, implementing this would cause an unavoidable race.
  959. + * The driver will always use the "no-stall" approach for OUT transfers.
  960. + *
  961. + * One subtle point concerns sending status-stage responses for ep0
  962. + * requests. Some of these requests, such as device reset, can involve
  963. + * interrupting an ongoing file I/O operation, which might take an
  964. + * arbitrarily long time. During that delay the host might give up on
  965. + * the original ep0 request and issue a new one. When that happens the
  966. + * driver should not notify the host about completion of the original
  967. + * request, as the host will no longer be waiting for it. So the driver
  968. + * assigns to each ep0 request a unique tag, and it keeps track of the
  969. + * tag value of the request associated with a long-running exception
  970. + * (device-reset, interface-change, or configuration-change). When the
  971. + * exception handler is finished, the status-stage response is submitted
  972. + * only if the current ep0 request tag is equal to the exception request
  973. + * tag. Thus only the most recently received ep0 request will get a
  974. + * status-stage response.
  975. + *
  976. + * Warning: This driver source file is too long. It ought to be split up
  977. + * into a header file plus about 3 separate .c files, to handle the details
  978. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  979. + */
  980. +
  981. +
  982. +/* #define VERBOSE_DEBUG */
  983. +/* #define DUMP_MSGS */
  984. +
  985. +
  986. +#include <linux/blkdev.h>
  987. +#include <linux/completion.h>
  988. +#include <linux/dcache.h>
  989. +#include <linux/delay.h>
  990. +#include <linux/device.h>
  991. +#include <linux/fcntl.h>
  992. +#include <linux/file.h>
  993. +#include <linux/fs.h>
  994. +#include <linux/kref.h>
  995. +#include <linux/kthread.h>
  996. +#include <linux/limits.h>
  997. +#include <linux/module.h>
  998. +#include <linux/rwsem.h>
  999. +#include <linux/slab.h>
  1000. +#include <linux/spinlock.h>
  1001. +#include <linux/string.h>
  1002. +#include <linux/freezer.h>
  1003. +#include <linux/utsname.h>
  1004. +
  1005. +#include <linux/usb/ch9.h>
  1006. +#include <linux/usb/gadget.h>
  1007. +
  1008. +#include "gadget_chips.h"
  1009. +
  1010. +
  1011. +
  1012. +/*
  1013. + * Kbuild is not very cooperative with respect to linking separately
  1014. + * compiled library objects into one module. So for now we won't use
  1015. + * separate compilation ... ensuring init/exit sections work to shrink
  1016. + * the runtime footprint, and giving us at least some parts of what
  1017. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  1018. + */
  1019. +#include "usbstring.c"
  1020. +#include "config.c"
  1021. +#include "epautoconf.c"
  1022. +
  1023. +/*-------------------------------------------------------------------------*/
  1024. +
  1025. +#define DRIVER_DESC "File-backed Storage Gadget"
  1026. +#define DRIVER_NAME "g_file_storage"
  1027. +#define DRIVER_VERSION "1 September 2010"
  1028. +
  1029. +static char fsg_string_manufacturer[64];
  1030. +static const char fsg_string_product[] = DRIVER_DESC;
  1031. +static const char fsg_string_config[] = "Self-powered";
  1032. +static const char fsg_string_interface[] = "Mass Storage";
  1033. +
  1034. +
  1035. +#include "storage_common.c"
  1036. +
  1037. +
  1038. +MODULE_DESCRIPTION(DRIVER_DESC);
  1039. +MODULE_AUTHOR("Alan Stern");
  1040. +MODULE_LICENSE("Dual BSD/GPL");
  1041. +
  1042. +/*
  1043. + * This driver assumes self-powered hardware and has no way for users to
  1044. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  1045. + * and endpoint addresses.
  1046. + */
  1047. +
  1048. +
  1049. +/*-------------------------------------------------------------------------*/
  1050. +
  1051. +
  1052. +/* Encapsulate the module parameter settings */
  1053. +
  1054. +static struct {
  1055. + char *file[FSG_MAX_LUNS];
  1056. + char *serial;
  1057. + bool ro[FSG_MAX_LUNS];
  1058. + bool nofua[FSG_MAX_LUNS];
  1059. + unsigned int num_filenames;
  1060. + unsigned int num_ros;
  1061. + unsigned int num_nofuas;
  1062. + unsigned int nluns;
  1063. +
  1064. + bool removable;
  1065. + bool can_stall;
  1066. + bool cdrom;
  1067. +
  1068. + char *transport_parm;
  1069. + char *protocol_parm;
  1070. + unsigned short vendor;
  1071. + unsigned short product;
  1072. + unsigned short release;
  1073. + unsigned int buflen;
  1074. +
  1075. + int transport_type;
  1076. + char *transport_name;
  1077. + int protocol_type;
  1078. + char *protocol_name;
  1079. +
  1080. +} mod_data = { // Default values
  1081. + .transport_parm = "BBB",
  1082. + .protocol_parm = "SCSI",
  1083. + .removable = 0,
  1084. + .can_stall = 1,
  1085. + .cdrom = 0,
  1086. + .vendor = FSG_VENDOR_ID,
  1087. + .product = FSG_PRODUCT_ID,
  1088. + .release = 0xffff, // Use controller chip type
  1089. + .buflen = 16384,
  1090. + };
  1091. +
  1092. +
  1093. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  1094. + S_IRUGO);
  1095. +MODULE_PARM_DESC(file, "names of backing files or devices");
  1096. +
  1097. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  1098. +MODULE_PARM_DESC(serial, "USB serial number");
  1099. +
  1100. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  1101. +MODULE_PARM_DESC(ro, "true to force read-only");
  1102. +
  1103. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  1104. + S_IRUGO);
  1105. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  1106. +
  1107. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  1108. +MODULE_PARM_DESC(luns, "number of LUNs");
  1109. +
  1110. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  1111. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  1112. +
  1113. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  1114. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  1115. +
  1116. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  1117. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  1118. +
  1119. +/* In the non-TEST version, only the module parameters listed above
  1120. + * are available. */
  1121. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1122. +
  1123. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  1124. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  1125. +
  1126. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  1127. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  1128. + "8070, or SCSI)");
  1129. +
  1130. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  1131. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  1132. +
  1133. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  1134. +MODULE_PARM_DESC(product, "USB Product ID");
  1135. +
  1136. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  1137. +MODULE_PARM_DESC(release, "USB release number");
  1138. +
  1139. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  1140. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  1141. +
  1142. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1143. +
  1144. +
  1145. +/*
  1146. + * These definitions will permit the compiler to avoid generating code for
  1147. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  1148. + * can recognize when a test of a constant expression yields a dead code
  1149. + * path.
  1150. + */
  1151. +
  1152. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1153. +
  1154. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  1155. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  1156. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  1157. +
  1158. +#else
  1159. +
  1160. +#define transport_is_bbb() 1
  1161. +#define transport_is_cbi() 0
  1162. +#define protocol_is_scsi() 1
  1163. +
  1164. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1165. +
  1166. +
  1167. +/*-------------------------------------------------------------------------*/
  1168. +
  1169. +
  1170. +struct fsg_dev {
  1171. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  1172. + spinlock_t lock;
  1173. + struct usb_gadget *gadget;
  1174. +
  1175. + /* filesem protects: backing files in use */
  1176. + struct rw_semaphore filesem;
  1177. +
  1178. + /* reference counting: wait until all LUNs are released */
  1179. + struct kref ref;
  1180. +
  1181. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  1182. + struct usb_request *ep0req; // For control responses
  1183. + unsigned int ep0_req_tag;
  1184. + const char *ep0req_name;
  1185. +
  1186. + struct usb_request *intreq; // For interrupt responses
  1187. + int intreq_busy;
  1188. + struct fsg_buffhd *intr_buffhd;
  1189. +
  1190. + unsigned int bulk_out_maxpacket;
  1191. + enum fsg_state state; // For exception handling
  1192. + unsigned int exception_req_tag;
  1193. +
  1194. + u8 config, new_config;
  1195. +
  1196. + unsigned int running : 1;
  1197. + unsigned int bulk_in_enabled : 1;
  1198. + unsigned int bulk_out_enabled : 1;
  1199. + unsigned int intr_in_enabled : 1;
  1200. + unsigned int phase_error : 1;
  1201. + unsigned int short_packet_received : 1;
  1202. + unsigned int bad_lun_okay : 1;
  1203. +
  1204. + unsigned long atomic_bitflags;
  1205. +#define REGISTERED 0
  1206. +#define IGNORE_BULK_OUT 1
  1207. +#define SUSPENDED 2
  1208. +
  1209. + struct usb_ep *bulk_in;
  1210. + struct usb_ep *bulk_out;
  1211. + struct usb_ep *intr_in;
  1212. +
  1213. + struct fsg_buffhd *next_buffhd_to_fill;
  1214. + struct fsg_buffhd *next_buffhd_to_drain;
  1215. +
  1216. + int thread_wakeup_needed;
  1217. + struct completion thread_notifier;
  1218. + struct task_struct *thread_task;
  1219. +
  1220. + int cmnd_size;
  1221. + u8 cmnd[MAX_COMMAND_SIZE];
  1222. + enum data_direction data_dir;
  1223. + u32 data_size;
  1224. + u32 data_size_from_cmnd;
  1225. + u32 tag;
  1226. + unsigned int lun;
  1227. + u32 residue;
  1228. + u32 usb_amount_left;
  1229. +
  1230. + /* The CB protocol offers no way for a host to know when a command
  1231. + * has completed. As a result the next command may arrive early,
  1232. + * and we will still have to handle it. For that reason we need
  1233. + * a buffer to store new commands when using CB (or CBI, which
  1234. + * does not oblige a host to wait for command completion either). */
  1235. + int cbbuf_cmnd_size;
  1236. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  1237. +
  1238. + unsigned int nluns;
  1239. + struct fsg_lun *luns;
  1240. + struct fsg_lun *curlun;
  1241. + /* Must be the last entry */
  1242. + struct fsg_buffhd buffhds[];
  1243. +};
  1244. +
  1245. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  1246. +
  1247. +static int exception_in_progress(struct fsg_dev *fsg)
  1248. +{
  1249. + return (fsg->state > FSG_STATE_IDLE);
  1250. +}
  1251. +
  1252. +/* Make bulk-out requests be divisible by the maxpacket size */
  1253. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  1254. + struct fsg_buffhd *bh, unsigned int length)
  1255. +{
  1256. + unsigned int rem;
  1257. +
  1258. + bh->bulk_out_intended_length = length;
  1259. + rem = length % fsg->bulk_out_maxpacket;
  1260. + if (rem > 0)
  1261. + length += fsg->bulk_out_maxpacket - rem;
  1262. + bh->outreq->length = length;
  1263. +}
  1264. +
  1265. +static struct fsg_dev *the_fsg;
  1266. +static struct usb_gadget_driver fsg_driver;
  1267. +
  1268. +
  1269. +/*-------------------------------------------------------------------------*/
  1270. +
  1271. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  1272. +{
  1273. + const char *name;
  1274. +
  1275. + if (ep == fsg->bulk_in)
  1276. + name = "bulk-in";
  1277. + else if (ep == fsg->bulk_out)
  1278. + name = "bulk-out";
  1279. + else
  1280. + name = ep->name;
  1281. + DBG(fsg, "%s set halt\n", name);
  1282. + return usb_ep_set_halt(ep);
  1283. +}
  1284. +
  1285. +
  1286. +/*-------------------------------------------------------------------------*/
  1287. +
  1288. +/*
  1289. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  1290. + * descriptors are built on demand. Also the (static) config and interface
  1291. + * descriptors are adjusted during fsg_bind().
  1292. + */
  1293. +
  1294. +/* There is only one configuration. */
  1295. +#define CONFIG_VALUE 1
  1296. +
  1297. +static struct usb_device_descriptor
  1298. +device_desc = {
  1299. + .bLength = sizeof device_desc,
  1300. + .bDescriptorType = USB_DT_DEVICE,
  1301. +
  1302. + .bcdUSB = cpu_to_le16(0x0200),
  1303. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1304. +
  1305. + /* The next three values can be overridden by module parameters */
  1306. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  1307. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  1308. + .bcdDevice = cpu_to_le16(0xffff),
  1309. +
  1310. + .iManufacturer = FSG_STRING_MANUFACTURER,
  1311. + .iProduct = FSG_STRING_PRODUCT,
  1312. + .iSerialNumber = FSG_STRING_SERIAL,
  1313. + .bNumConfigurations = 1,
  1314. +};
  1315. +
  1316. +static struct usb_config_descriptor
  1317. +config_desc = {
  1318. + .bLength = sizeof config_desc,
  1319. + .bDescriptorType = USB_DT_CONFIG,
  1320. +
  1321. + /* wTotalLength computed by usb_gadget_config_buf() */
  1322. + .bNumInterfaces = 1,
  1323. + .bConfigurationValue = CONFIG_VALUE,
  1324. + .iConfiguration = FSG_STRING_CONFIG,
  1325. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  1326. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  1327. +};
  1328. +
  1329. +
  1330. +static struct usb_qualifier_descriptor
  1331. +dev_qualifier = {
  1332. + .bLength = sizeof dev_qualifier,
  1333. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  1334. +
  1335. + .bcdUSB = cpu_to_le16(0x0200),
  1336. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1337. +
  1338. + .bNumConfigurations = 1,
  1339. +};
  1340. +
  1341. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  1342. +{
  1343. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  1344. + buf += USB_DT_BOS_SIZE;
  1345. +
  1346. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  1347. + buf += USB_DT_USB_EXT_CAP_SIZE;
  1348. +
  1349. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  1350. +
  1351. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  1352. + + USB_DT_USB_EXT_CAP_SIZE;
  1353. +}
  1354. +
  1355. +/*
  1356. + * Config descriptors must agree with the code that sets configurations
  1357. + * and with code managing interfaces and their altsettings. They must
  1358. + * also handle different speeds and other-speed requests.
  1359. + */
  1360. +static int populate_config_buf(struct usb_gadget *gadget,
  1361. + u8 *buf, u8 type, unsigned index)
  1362. +{
  1363. + enum usb_device_speed speed = gadget->speed;
  1364. + int len;
  1365. + const struct usb_descriptor_header **function;
  1366. +
  1367. + if (index > 0)
  1368. + return -EINVAL;
  1369. +
  1370. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  1371. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  1372. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  1373. + ? (const struct usb_descriptor_header **)fsg_hs_function
  1374. + : (const struct usb_descriptor_header **)fsg_fs_function;
  1375. +
  1376. + /* for now, don't advertise srp-only devices */
  1377. + if (!gadget_is_otg(gadget))
  1378. + function++;
  1379. +
  1380. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  1381. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  1382. + return len;
  1383. +}
  1384. +
  1385. +
  1386. +/*-------------------------------------------------------------------------*/
  1387. +
  1388. +/* These routines may be called in process context or in_irq */
  1389. +
  1390. +/* Caller must hold fsg->lock */
  1391. +static void wakeup_thread(struct fsg_dev *fsg)
  1392. +{
  1393. + /* Tell the main thread that something has happened */
  1394. + fsg->thread_wakeup_needed = 1;
  1395. + if (fsg->thread_task)
  1396. + wake_up_process(fsg->thread_task);
  1397. +}
  1398. +
  1399. +
  1400. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  1401. +{
  1402. + unsigned long flags;
  1403. +
  1404. + /* Do nothing if a higher-priority exception is already in progress.
  1405. + * If a lower-or-equal priority exception is in progress, preempt it
  1406. + * and notify the main thread by sending it a signal. */
  1407. + spin_lock_irqsave(&fsg->lock, flags);
  1408. + if (fsg->state <= new_state) {
  1409. + fsg->exception_req_tag = fsg->ep0_req_tag;
  1410. + fsg->state = new_state;
  1411. + if (fsg->thread_task)
  1412. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  1413. + fsg->thread_task);
  1414. + }
  1415. + spin_unlock_irqrestore(&fsg->lock, flags);
  1416. +}
  1417. +
  1418. +
  1419. +/*-------------------------------------------------------------------------*/
  1420. +
  1421. +/* The disconnect callback and ep0 routines. These always run in_irq,
  1422. + * except that ep0_queue() is called in the main thread to acknowledge
  1423. + * completion of various requests: set config, set interface, and
  1424. + * Bulk-only device reset. */
  1425. +
  1426. +static void fsg_disconnect(struct usb_gadget *gadget)
  1427. +{
  1428. + struct fsg_dev *fsg = get_gadget_data(gadget);
  1429. +
  1430. + DBG(fsg, "disconnect or port reset\n");
  1431. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  1432. +}
  1433. +
  1434. +
  1435. +static int ep0_queue(struct fsg_dev *fsg)
  1436. +{
  1437. + int rc;
  1438. +
  1439. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  1440. + if (rc != 0 && rc != -ESHUTDOWN) {
  1441. +
  1442. + /* We can't do much more than wait for a reset */
  1443. + WARNING(fsg, "error in submission: %s --> %d\n",
  1444. + fsg->ep0->name, rc);
  1445. + }
  1446. + return rc;
  1447. +}
  1448. +
  1449. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  1450. +{
  1451. + struct fsg_dev *fsg = ep->driver_data;
  1452. +
  1453. + if (req->actual > 0)
  1454. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  1455. + if (req->status || req->actual != req->length)
  1456. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1457. + req->status, req->actual, req->length);
  1458. + if (req->status == -ECONNRESET) // Request was cancelled
  1459. + usb_ep_fifo_flush(ep);
  1460. +
  1461. + if (req->status == 0 && req->context)
  1462. + ((fsg_routine_t) (req->context))(fsg);
  1463. +}
  1464. +
  1465. +
  1466. +/*-------------------------------------------------------------------------*/
  1467. +
  1468. +/* Bulk and interrupt endpoint completion handlers.
  1469. + * These always run in_irq. */
  1470. +
  1471. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  1472. +{
  1473. + struct fsg_dev *fsg = ep->driver_data;
  1474. + struct fsg_buffhd *bh = req->context;
  1475. +
  1476. + if (req->status || req->actual != req->length)
  1477. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1478. + req->status, req->actual, req->length);
  1479. + if (req->status == -ECONNRESET) // Request was cancelled
  1480. + usb_ep_fifo_flush(ep);
  1481. +
  1482. + /* Hold the lock while we update the request and buffer states */
  1483. + smp_wmb();
  1484. + spin_lock(&fsg->lock);
  1485. + bh->inreq_busy = 0;
  1486. + bh->state = BUF_STATE_EMPTY;
  1487. + wakeup_thread(fsg);
  1488. + spin_unlock(&fsg->lock);
  1489. +}
  1490. +
  1491. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  1492. +{
  1493. + struct fsg_dev *fsg = ep->driver_data;
  1494. + struct fsg_buffhd *bh = req->context;
  1495. +
  1496. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  1497. + if (req->status || req->actual != bh->bulk_out_intended_length)
  1498. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1499. + req->status, req->actual,
  1500. + bh->bulk_out_intended_length);
  1501. + if (req->status == -ECONNRESET) // Request was cancelled
  1502. + usb_ep_fifo_flush(ep);
  1503. +
  1504. + /* Hold the lock while we update the request and buffer states */
  1505. + smp_wmb();
  1506. + spin_lock(&fsg->lock);
  1507. + bh->outreq_busy = 0;
  1508. + bh->state = BUF_STATE_FULL;
  1509. + wakeup_thread(fsg);
  1510. + spin_unlock(&fsg->lock);
  1511. +}
  1512. +
  1513. +
  1514. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1515. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  1516. +{
  1517. + struct fsg_dev *fsg = ep->driver_data;
  1518. + struct fsg_buffhd *bh = req->context;
  1519. +
  1520. + if (req->status || req->actual != req->length)
  1521. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1522. + req->status, req->actual, req->length);
  1523. + if (req->status == -ECONNRESET) // Request was cancelled
  1524. + usb_ep_fifo_flush(ep);
  1525. +
  1526. + /* Hold the lock while we update the request and buffer states */
  1527. + smp_wmb();
  1528. + spin_lock(&fsg->lock);
  1529. + fsg->intreq_busy = 0;
  1530. + bh->state = BUF_STATE_EMPTY;
  1531. + wakeup_thread(fsg);
  1532. + spin_unlock(&fsg->lock);
  1533. +}
  1534. +
  1535. +#else
  1536. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  1537. +{}
  1538. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1539. +
  1540. +
  1541. +/*-------------------------------------------------------------------------*/
  1542. +
  1543. +/* Ep0 class-specific handlers. These always run in_irq. */
  1544. +
  1545. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1546. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1547. +{
  1548. + struct usb_request *req = fsg->ep0req;
  1549. + static u8 cbi_reset_cmnd[6] = {
  1550. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  1551. +
  1552. + /* Error in command transfer? */
  1553. + if (req->status || req->length != req->actual ||
  1554. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  1555. +
  1556. + /* Not all controllers allow a protocol stall after
  1557. + * receiving control-out data, but we'll try anyway. */
  1558. + fsg_set_halt(fsg, fsg->ep0);
  1559. + return; // Wait for reset
  1560. + }
  1561. +
  1562. + /* Is it the special reset command? */
  1563. + if (req->actual >= sizeof cbi_reset_cmnd &&
  1564. + memcmp(req->buf, cbi_reset_cmnd,
  1565. + sizeof cbi_reset_cmnd) == 0) {
  1566. +
  1567. + /* Raise an exception to stop the current operation
  1568. + * and reinitialize our state. */
  1569. + DBG(fsg, "cbi reset request\n");
  1570. + raise_exception(fsg, FSG_STATE_RESET);
  1571. + return;
  1572. + }
  1573. +
  1574. + VDBG(fsg, "CB[I] accept device-specific command\n");
  1575. + spin_lock(&fsg->lock);
  1576. +
  1577. + /* Save the command for later */
  1578. + if (fsg->cbbuf_cmnd_size)
  1579. + WARNING(fsg, "CB[I] overwriting previous command\n");
  1580. + fsg->cbbuf_cmnd_size = req->actual;
  1581. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  1582. +
  1583. + wakeup_thread(fsg);
  1584. + spin_unlock(&fsg->lock);
  1585. +}
  1586. +
  1587. +#else
  1588. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1589. +{}
  1590. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1591. +
  1592. +
  1593. +static int class_setup_req(struct fsg_dev *fsg,
  1594. + const struct usb_ctrlrequest *ctrl)
  1595. +{
  1596. + struct usb_request *req = fsg->ep0req;
  1597. + int value = -EOPNOTSUPP;
  1598. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  1599. + u16 w_value = le16_to_cpu(ctrl->wValue);
  1600. + u16 w_length = le16_to_cpu(ctrl->wLength);
  1601. +
  1602. + if (!fsg->config)
  1603. + return value;
  1604. +
  1605. + /* Handle Bulk-only class-specific requests */
  1606. + if (transport_is_bbb()) {
  1607. + switch (ctrl->bRequest) {
  1608. +
  1609. + case US_BULK_RESET_REQUEST:
  1610. + if (ctrl->bRequestType != (USB_DIR_OUT |
  1611. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1612. + break;
  1613. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  1614. + value = -EDOM;
  1615. + break;
  1616. + }
  1617. +
  1618. + /* Raise an exception to stop the current operation
  1619. + * and reinitialize our state. */
  1620. + DBG(fsg, "bulk reset request\n");
  1621. + raise_exception(fsg, FSG_STATE_RESET);
  1622. + value = DELAYED_STATUS;
  1623. + break;
  1624. +
  1625. + case US_BULK_GET_MAX_LUN:
  1626. + if (ctrl->bRequestType != (USB_DIR_IN |
  1627. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1628. + break;
  1629. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  1630. + value = -EDOM;
  1631. + break;
  1632. + }
  1633. + VDBG(fsg, "get max LUN\n");
  1634. + *(u8 *) req->buf = fsg->nluns - 1;
  1635. + value = 1;
  1636. + break;
  1637. + }
  1638. + }
  1639. +
  1640. + /* Handle CBI class-specific requests */
  1641. + else {
  1642. + switch (ctrl->bRequest) {
  1643. +
  1644. + case USB_CBI_ADSC_REQUEST:
  1645. + if (ctrl->bRequestType != (USB_DIR_OUT |
  1646. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1647. + break;
  1648. + if (w_index != 0 || w_value != 0) {
  1649. + value = -EDOM;
  1650. + break;
  1651. + }
  1652. + if (w_length > MAX_COMMAND_SIZE) {
  1653. + value = -EOVERFLOW;
  1654. + break;
  1655. + }
  1656. + value = w_length;
  1657. + fsg->ep0req->context = received_cbi_adsc;
  1658. + break;
  1659. + }
  1660. + }
  1661. +
  1662. + if (value == -EOPNOTSUPP)
  1663. + VDBG(fsg,
  1664. + "unknown class-specific control req "
  1665. + "%02x.%02x v%04x i%04x l%u\n",
  1666. + ctrl->bRequestType, ctrl->bRequest,
  1667. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  1668. + return value;
  1669. +}
  1670. +
  1671. +
  1672. +/*-------------------------------------------------------------------------*/
  1673. +
  1674. +/* Ep0 standard request handlers. These always run in_irq. */
  1675. +
  1676. +static int standard_setup_req(struct fsg_dev *fsg,
  1677. + const struct usb_ctrlrequest *ctrl)
  1678. +{
  1679. + struct usb_request *req = fsg->ep0req;
  1680. + int value = -EOPNOTSUPP;
  1681. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  1682. + u16 w_value = le16_to_cpu(ctrl->wValue);
  1683. +
  1684. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  1685. + * but config change events will also reconfigure hardware. */
  1686. + switch (ctrl->bRequest) {
  1687. +
  1688. + case USB_REQ_GET_DESCRIPTOR:
  1689. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1690. + USB_RECIP_DEVICE))
  1691. + break;
  1692. + switch (w_value >> 8) {
  1693. +
  1694. + case USB_DT_DEVICE:
  1695. + VDBG(fsg, "get device descriptor\n");
  1696. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  1697. + value = sizeof device_desc;
  1698. + memcpy(req->buf, &device_desc, value);
  1699. + break;
  1700. + case USB_DT_DEVICE_QUALIFIER:
  1701. + VDBG(fsg, "get device qualifier\n");
  1702. + if (!gadget_is_dualspeed(fsg->gadget) ||
  1703. + fsg->gadget->speed == USB_SPEED_SUPER)
  1704. + break;
  1705. + /*
  1706. + * Assume ep0 uses the same maxpacket value for both
  1707. + * speeds
  1708. + */
  1709. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  1710. + value = sizeof dev_qualifier;
  1711. + memcpy(req->buf, &dev_qualifier, value);
  1712. + break;
  1713. +
  1714. + case USB_DT_OTHER_SPEED_CONFIG:
  1715. + VDBG(fsg, "get other-speed config descriptor\n");
  1716. + if (!gadget_is_dualspeed(fsg->gadget) ||
  1717. + fsg->gadget->speed == USB_SPEED_SUPER)
  1718. + break;
  1719. + goto get_config;
  1720. + case USB_DT_CONFIG:
  1721. + VDBG(fsg, "get configuration descriptor\n");
  1722. +get_config:
  1723. + value = populate_config_buf(fsg->gadget,
  1724. + req->buf,
  1725. + w_value >> 8,
  1726. + w_value & 0xff);
  1727. + break;
  1728. +
  1729. + case USB_DT_STRING:
  1730. + VDBG(fsg, "get string descriptor\n");
  1731. +
  1732. + /* wIndex == language code */
  1733. + value = usb_gadget_get_string(&fsg_stringtab,
  1734. + w_value & 0xff, req->buf);
  1735. + break;
  1736. +
  1737. + case USB_DT_BOS:
  1738. + VDBG(fsg, "get bos descriptor\n");
  1739. +
  1740. + if (gadget_is_superspeed(fsg->gadget))
  1741. + value = populate_bos(fsg, req->buf);
  1742. + break;
  1743. + }
  1744. +
  1745. + break;
  1746. +
  1747. + /* One config, two speeds */
  1748. + case USB_REQ_SET_CONFIGURATION:
  1749. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  1750. + USB_RECIP_DEVICE))
  1751. + break;
  1752. + VDBG(fsg, "set configuration\n");
  1753. + if (w_value == CONFIG_VALUE || w_value == 0) {
  1754. + fsg->new_config = w_value;
  1755. +
  1756. + /* Raise an exception to wipe out previous transaction
  1757. + * state (queued bufs, etc) and set the new config. */
  1758. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  1759. + value = DELAYED_STATUS;
  1760. + }
  1761. + break;
  1762. + case USB_REQ_GET_CONFIGURATION:
  1763. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1764. + USB_RECIP_DEVICE))
  1765. + break;
  1766. + VDBG(fsg, "get configuration\n");
  1767. + *(u8 *) req->buf = fsg->config;
  1768. + value = 1;
  1769. + break;
  1770. +
  1771. + case USB_REQ_SET_INTERFACE:
  1772. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  1773. + USB_RECIP_INTERFACE))
  1774. + break;
  1775. + if (fsg->config && w_index == 0) {
  1776. +
  1777. + /* Raise an exception to wipe out previous transaction
  1778. + * state (queued bufs, etc) and install the new
  1779. + * interface altsetting. */
  1780. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  1781. + value = DELAYED_STATUS;
  1782. + }
  1783. + break;
  1784. + case USB_REQ_GET_INTERFACE:
  1785. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1786. + USB_RECIP_INTERFACE))
  1787. + break;
  1788. + if (!fsg->config)
  1789. + break;
  1790. + if (w_index != 0) {
  1791. + value = -EDOM;
  1792. + break;
  1793. + }
  1794. + VDBG(fsg, "get interface\n");
  1795. + *(u8 *) req->buf = 0;
  1796. + value = 1;
  1797. + break;
  1798. +
  1799. + default:
  1800. + VDBG(fsg,
  1801. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  1802. + ctrl->bRequestType, ctrl->bRequest,
  1803. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  1804. + }
  1805. +
  1806. + return value;
  1807. +}
  1808. +
  1809. +
  1810. +static int fsg_setup(struct usb_gadget *gadget,
  1811. + const struct usb_ctrlrequest *ctrl)
  1812. +{
  1813. + struct fsg_dev *fsg = get_gadget_data(gadget);
  1814. + int rc;
  1815. + int w_length = le16_to_cpu(ctrl->wLength);
  1816. +
  1817. + ++fsg->ep0_req_tag; // Record arrival of a new request
  1818. + fsg->ep0req->context = NULL;
  1819. + fsg->ep0req->length = 0;
  1820. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  1821. +
  1822. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  1823. + rc = class_setup_req(fsg, ctrl);
  1824. + else
  1825. + rc = standard_setup_req(fsg, ctrl);
  1826. +
  1827. + /* Respond with data/status or defer until later? */
  1828. + if (rc >= 0 && rc != DELAYED_STATUS) {
  1829. + rc = min(rc, w_length);
  1830. + fsg->ep0req->length = rc;
  1831. + fsg->ep0req->zero = rc < w_length;
  1832. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  1833. + "ep0-in" : "ep0-out");
  1834. + rc = ep0_queue(fsg);
  1835. + }
  1836. +
  1837. + /* Device either stalls (rc < 0) or reports success */
  1838. + return rc;
  1839. +}
  1840. +
  1841. +
  1842. +/*-------------------------------------------------------------------------*/
  1843. +
  1844. +/* All the following routines run in process context */
  1845. +
  1846. +
  1847. +/* Use this for bulk or interrupt transfers, not ep0 */
  1848. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  1849. + struct usb_request *req, int *pbusy,
  1850. + enum fsg_buffer_state *state)
  1851. +{
  1852. + int rc;
  1853. +
  1854. + if (ep == fsg->bulk_in)
  1855. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  1856. + else if (ep == fsg->intr_in)
  1857. + dump_msg(fsg, "intr-in", req->buf, req->length);
  1858. +
  1859. + spin_lock_irq(&fsg->lock);
  1860. + *pbusy = 1;
  1861. + *state = BUF_STATE_BUSY;
  1862. + spin_unlock_irq(&fsg->lock);
  1863. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  1864. + if (rc != 0) {
  1865. + *pbusy = 0;
  1866. + *state = BUF_STATE_EMPTY;
  1867. +
  1868. + /* We can't do much more than wait for a reset */
  1869. +
  1870. + /* Note: currently the net2280 driver fails zero-length
  1871. + * submissions if DMA is enabled. */
  1872. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  1873. + req->length == 0))
  1874. + WARNING(fsg, "error in submission: %s --> %d\n",
  1875. + ep->name, rc);
  1876. + }
  1877. +}
  1878. +
  1879. +
  1880. +static int sleep_thread(struct fsg_dev *fsg)
  1881. +{
  1882. + int rc = 0;
  1883. +
  1884. + /* Wait until a signal arrives or we are woken up */
  1885. + for (;;) {
  1886. + try_to_freeze();
  1887. + set_current_state(TASK_INTERRUPTIBLE);
  1888. + if (signal_pending(current)) {
  1889. + rc = -EINTR;
  1890. + break;
  1891. + }
  1892. + if (fsg->thread_wakeup_needed)
  1893. + break;
  1894. + schedule();
  1895. + }
  1896. + __set_current_state(TASK_RUNNING);
  1897. + fsg->thread_wakeup_needed = 0;
  1898. + return rc;
  1899. +}
  1900. +
  1901. +
  1902. +/*-------------------------------------------------------------------------*/
  1903. +
  1904. +static int do_read(struct fsg_dev *fsg)
  1905. +{
  1906. + struct fsg_lun *curlun = fsg->curlun;
  1907. + u32 lba;
  1908. + struct fsg_buffhd *bh;
  1909. + int rc;
  1910. + u32 amount_left;
  1911. + loff_t file_offset, file_offset_tmp;
  1912. + unsigned int amount;
  1913. + ssize_t nread;
  1914. +
  1915. + /* Get the starting Logical Block Address and check that it's
  1916. + * not too big */
  1917. + if (fsg->cmnd[0] == READ_6)
  1918. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  1919. + else {
  1920. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  1921. +
  1922. + /* We allow DPO (Disable Page Out = don't save data in the
  1923. + * cache) and FUA (Force Unit Access = don't read from the
  1924. + * cache), but we don't implement them. */
  1925. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  1926. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1927. + return -EINVAL;
  1928. + }
  1929. + }
  1930. + if (lba >= curlun->num_sectors) {
  1931. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1932. + return -EINVAL;
  1933. + }
  1934. + file_offset = ((loff_t) lba) << curlun->blkbits;
  1935. +
  1936. + /* Carry out the file reads */
  1937. + amount_left = fsg->data_size_from_cmnd;
  1938. + if (unlikely(amount_left == 0))
  1939. + return -EIO; // No default reply
  1940. +
  1941. + for (;;) {
  1942. +
  1943. + /* Figure out how much we need to read:
  1944. + * Try to read the remaining amount.
  1945. + * But don't read more than the buffer size.
  1946. + * And don't try to read past the end of the file.
  1947. + */
  1948. + amount = min((unsigned int) amount_left, mod_data.buflen);
  1949. + amount = min((loff_t) amount,
  1950. + curlun->file_length - file_offset);
  1951. +
  1952. + /* Wait for the next buffer to become available */
  1953. + bh = fsg->next_buffhd_to_fill;
  1954. + while (bh->state != BUF_STATE_EMPTY) {
  1955. + rc = sleep_thread(fsg);
  1956. + if (rc)
  1957. + return rc;
  1958. + }
  1959. +
  1960. + /* If we were asked to read past the end of file,
  1961. + * end with an empty buffer. */
  1962. + if (amount == 0) {
  1963. + curlun->sense_data =
  1964. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1965. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  1966. + curlun->info_valid = 1;
  1967. + bh->inreq->length = 0;
  1968. + bh->state = BUF_STATE_FULL;
  1969. + break;
  1970. + }
  1971. +
  1972. + /* Perform the read */
  1973. + file_offset_tmp = file_offset;
  1974. + nread = vfs_read(curlun->filp,
  1975. + (char __user *) bh->buf,
  1976. + amount, &file_offset_tmp);
  1977. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  1978. + (unsigned long long) file_offset,
  1979. + (int) nread);
  1980. + if (signal_pending(current))
  1981. + return -EINTR;
  1982. +
  1983. + if (nread < 0) {
  1984. + LDBG(curlun, "error in file read: %d\n",
  1985. + (int) nread);
  1986. + nread = 0;
  1987. + } else if (nread < amount) {
  1988. + LDBG(curlun, "partial file read: %d/%u\n",
  1989. + (int) nread, amount);
  1990. + nread = round_down(nread, curlun->blksize);
  1991. + }
  1992. + file_offset += nread;
  1993. + amount_left -= nread;
  1994. + fsg->residue -= nread;
  1995. +
  1996. + /* Except at the end of the transfer, nread will be
  1997. + * equal to the buffer size, which is divisible by the
  1998. + * bulk-in maxpacket size.
  1999. + */
  2000. + bh->inreq->length = nread;
  2001. + bh->state = BUF_STATE_FULL;
  2002. +
  2003. + /* If an error occurred, report it and its position */
  2004. + if (nread < amount) {
  2005. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  2006. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2007. + curlun->info_valid = 1;
  2008. + break;
  2009. + }
  2010. +
  2011. + if (amount_left == 0)
  2012. + break; // No more left to read
  2013. +
  2014. + /* Send this buffer and go read some more */
  2015. + bh->inreq->zero = 0;
  2016. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2017. + &bh->inreq_busy, &bh->state);
  2018. + fsg->next_buffhd_to_fill = bh->next;
  2019. + }
  2020. +
  2021. + return -EIO; // No default reply
  2022. +}
  2023. +
  2024. +
  2025. +/*-------------------------------------------------------------------------*/
  2026. +
  2027. +static int do_write(struct fsg_dev *fsg)
  2028. +{
  2029. + struct fsg_lun *curlun = fsg->curlun;
  2030. + u32 lba;
  2031. + struct fsg_buffhd *bh;
  2032. + int get_some_more;
  2033. + u32 amount_left_to_req, amount_left_to_write;
  2034. + loff_t usb_offset, file_offset, file_offset_tmp;
  2035. + unsigned int amount;
  2036. + ssize_t nwritten;
  2037. + int rc;
  2038. +
  2039. + if (curlun->ro) {
  2040. + curlun->sense_data = SS_WRITE_PROTECTED;
  2041. + return -EINVAL;
  2042. + }
  2043. + spin_lock(&curlun->filp->f_lock);
  2044. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  2045. + spin_unlock(&curlun->filp->f_lock);
  2046. +
  2047. + /* Get the starting Logical Block Address and check that it's
  2048. + * not too big */
  2049. + if (fsg->cmnd[0] == WRITE_6)
  2050. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  2051. + else {
  2052. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2053. +
  2054. + /* We allow DPO (Disable Page Out = don't save data in the
  2055. + * cache) and FUA (Force Unit Access = write directly to the
  2056. + * medium). We don't implement DPO; we implement FUA by
  2057. + * performing synchronous output. */
  2058. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  2059. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2060. + return -EINVAL;
  2061. + }
  2062. + /* FUA */
  2063. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  2064. + spin_lock(&curlun->filp->f_lock);
  2065. + curlun->filp->f_flags |= O_DSYNC;
  2066. + spin_unlock(&curlun->filp->f_lock);
  2067. + }
  2068. + }
  2069. + if (lba >= curlun->num_sectors) {
  2070. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2071. + return -EINVAL;
  2072. + }
  2073. +
  2074. + /* Carry out the file writes */
  2075. + get_some_more = 1;
  2076. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  2077. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  2078. +
  2079. + while (amount_left_to_write > 0) {
  2080. +
  2081. + /* Queue a request for more data from the host */
  2082. + bh = fsg->next_buffhd_to_fill;
  2083. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  2084. +
  2085. + /* Figure out how much we want to get:
  2086. + * Try to get the remaining amount,
  2087. + * but not more than the buffer size.
  2088. + */
  2089. + amount = min(amount_left_to_req, mod_data.buflen);
  2090. +
  2091. + /* Beyond the end of the backing file? */
  2092. + if (usb_offset >= curlun->file_length) {
  2093. + get_some_more = 0;
  2094. + curlun->sense_data =
  2095. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2096. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  2097. + curlun->info_valid = 1;
  2098. + continue;
  2099. + }
  2100. +
  2101. + /* Get the next buffer */
  2102. + usb_offset += amount;
  2103. + fsg->usb_amount_left -= amount;
  2104. + amount_left_to_req -= amount;
  2105. + if (amount_left_to_req == 0)
  2106. + get_some_more = 0;
  2107. +
  2108. + /* Except at the end of the transfer, amount will be
  2109. + * equal to the buffer size, which is divisible by
  2110. + * the bulk-out maxpacket size.
  2111. + */
  2112. + set_bulk_out_req_length(fsg, bh, amount);
  2113. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  2114. + &bh->outreq_busy, &bh->state);
  2115. + fsg->next_buffhd_to_fill = bh->next;
  2116. + continue;
  2117. + }
  2118. +
  2119. + /* Write the received data to the backing file */
  2120. + bh = fsg->next_buffhd_to_drain;
  2121. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  2122. + break; // We stopped early
  2123. + if (bh->state == BUF_STATE_FULL) {
  2124. + smp_rmb();
  2125. + fsg->next_buffhd_to_drain = bh->next;
  2126. + bh->state = BUF_STATE_EMPTY;
  2127. +
  2128. + /* Did something go wrong with the transfer? */
  2129. + if (bh->outreq->status != 0) {
  2130. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  2131. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2132. + curlun->info_valid = 1;
  2133. + break;
  2134. + }
  2135. +
  2136. + amount = bh->outreq->actual;
  2137. + if (curlun->file_length - file_offset < amount) {
  2138. + LERROR(curlun,
  2139. + "write %u @ %llu beyond end %llu\n",
  2140. + amount, (unsigned long long) file_offset,
  2141. + (unsigned long long) curlun->file_length);
  2142. + amount = curlun->file_length - file_offset;
  2143. + }
  2144. +
  2145. + /* Don't accept excess data. The spec doesn't say
  2146. + * what to do in this case. We'll ignore the error.
  2147. + */
  2148. + amount = min(amount, bh->bulk_out_intended_length);
  2149. +
  2150. + /* Don't write a partial block */
  2151. + amount = round_down(amount, curlun->blksize);
  2152. + if (amount == 0)
  2153. + goto empty_write;
  2154. +
  2155. + /* Perform the write */
  2156. + file_offset_tmp = file_offset;
  2157. + nwritten = vfs_write(curlun->filp,
  2158. + (char __user *) bh->buf,
  2159. + amount, &file_offset_tmp);
  2160. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  2161. + (unsigned long long) file_offset,
  2162. + (int) nwritten);
  2163. + if (signal_pending(current))
  2164. + return -EINTR; // Interrupted!
  2165. +
  2166. + if (nwritten < 0) {
  2167. + LDBG(curlun, "error in file write: %d\n",
  2168. + (int) nwritten);
  2169. + nwritten = 0;
  2170. + } else if (nwritten < amount) {
  2171. + LDBG(curlun, "partial file write: %d/%u\n",
  2172. + (int) nwritten, amount);
  2173. + nwritten = round_down(nwritten, curlun->blksize);
  2174. + }
  2175. + file_offset += nwritten;
  2176. + amount_left_to_write -= nwritten;
  2177. + fsg->residue -= nwritten;
  2178. +
  2179. + /* If an error occurred, report it and its position */
  2180. + if (nwritten < amount) {
  2181. + curlun->sense_data = SS_WRITE_ERROR;
  2182. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2183. + curlun->info_valid = 1;
  2184. + break;
  2185. + }
  2186. +
  2187. + empty_write:
  2188. + /* Did the host decide to stop early? */
  2189. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  2190. + fsg->short_packet_received = 1;
  2191. + break;
  2192. + }
  2193. + continue;
  2194. + }
  2195. +
  2196. + /* Wait for something to happen */
  2197. + rc = sleep_thread(fsg);
  2198. + if (rc)
  2199. + return rc;
  2200. + }
  2201. +
  2202. + return -EIO; // No default reply
  2203. +}
  2204. +
  2205. +
  2206. +/*-------------------------------------------------------------------------*/
  2207. +
  2208. +static int do_synchronize_cache(struct fsg_dev *fsg)
  2209. +{
  2210. + struct fsg_lun *curlun = fsg->curlun;
  2211. + int rc;
  2212. +
  2213. + /* We ignore the requested LBA and write out all file's
  2214. + * dirty data buffers. */
  2215. + rc = fsg_lun_fsync_sub(curlun);
  2216. + if (rc)
  2217. + curlun->sense_data = SS_WRITE_ERROR;
  2218. + return 0;
  2219. +}
  2220. +
  2221. +
  2222. +/*-------------------------------------------------------------------------*/
  2223. +
  2224. +static void invalidate_sub(struct fsg_lun *curlun)
  2225. +{
  2226. + struct file *filp = curlun->filp;
  2227. + struct inode *inode = filp->f_path.dentry->d_inode;
  2228. + unsigned long rc;
  2229. +
  2230. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  2231. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  2232. +}
  2233. +
  2234. +static int do_verify(struct fsg_dev *fsg)
  2235. +{
  2236. + struct fsg_lun *curlun = fsg->curlun;
  2237. + u32 lba;
  2238. + u32 verification_length;
  2239. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  2240. + loff_t file_offset, file_offset_tmp;
  2241. + u32 amount_left;
  2242. + unsigned int amount;
  2243. + ssize_t nread;
  2244. +
  2245. + /* Get the starting Logical Block Address and check that it's
  2246. + * not too big */
  2247. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2248. + if (lba >= curlun->num_sectors) {
  2249. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2250. + return -EINVAL;
  2251. + }
  2252. +
  2253. + /* We allow DPO (Disable Page Out = don't save data in the
  2254. + * cache) but we don't implement it. */
  2255. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  2256. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2257. + return -EINVAL;
  2258. + }
  2259. +
  2260. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  2261. + if (unlikely(verification_length == 0))
  2262. + return -EIO; // No default reply
  2263. +
  2264. + /* Prepare to carry out the file verify */
  2265. + amount_left = verification_length << curlun->blkbits;
  2266. + file_offset = ((loff_t) lba) << curlun->blkbits;
  2267. +
  2268. + /* Write out all the dirty buffers before invalidating them */
  2269. + fsg_lun_fsync_sub(curlun);
  2270. + if (signal_pending(current))
  2271. + return -EINTR;
  2272. +
  2273. + invalidate_sub(curlun);
  2274. + if (signal_pending(current))
  2275. + return -EINTR;
  2276. +
  2277. + /* Just try to read the requested blocks */
  2278. + while (amount_left > 0) {
  2279. +
  2280. + /* Figure out how much we need to read:
  2281. + * Try to read the remaining amount, but not more than
  2282. + * the buffer size.
  2283. + * And don't try to read past the end of the file.
  2284. + */
  2285. + amount = min((unsigned int) amount_left, mod_data.buflen);
  2286. + amount = min((loff_t) amount,
  2287. + curlun->file_length - file_offset);
  2288. + if (amount == 0) {
  2289. + curlun->sense_data =
  2290. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2291. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2292. + curlun->info_valid = 1;
  2293. + break;
  2294. + }
  2295. +
  2296. + /* Perform the read */
  2297. + file_offset_tmp = file_offset;
  2298. + nread = vfs_read(curlun->filp,
  2299. + (char __user *) bh->buf,
  2300. + amount, &file_offset_tmp);
  2301. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  2302. + (unsigned long long) file_offset,
  2303. + (int) nread);
  2304. + if (signal_pending(current))
  2305. + return -EINTR;
  2306. +
  2307. + if (nread < 0) {
  2308. + LDBG(curlun, "error in file verify: %d\n",
  2309. + (int) nread);
  2310. + nread = 0;
  2311. + } else if (nread < amount) {
  2312. + LDBG(curlun, "partial file verify: %d/%u\n",
  2313. + (int) nread, amount);
  2314. + nread = round_down(nread, curlun->blksize);
  2315. + }
  2316. + if (nread == 0) {
  2317. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  2318. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2319. + curlun->info_valid = 1;
  2320. + break;
  2321. + }
  2322. + file_offset += nread;
  2323. + amount_left -= nread;
  2324. + }
  2325. + return 0;
  2326. +}
  2327. +
  2328. +
  2329. +/*-------------------------------------------------------------------------*/
  2330. +
  2331. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2332. +{
  2333. + u8 *buf = (u8 *) bh->buf;
  2334. +
  2335. + static char vendor_id[] = "Linux ";
  2336. + static char product_disk_id[] = "File-Stor Gadget";
  2337. + static char product_cdrom_id[] = "File-CD Gadget ";
  2338. +
  2339. + if (!fsg->curlun) { // Unsupported LUNs are okay
  2340. + fsg->bad_lun_okay = 1;
  2341. + memset(buf, 0, 36);
  2342. + buf[0] = 0x7f; // Unsupported, no device-type
  2343. + buf[4] = 31; // Additional length
  2344. + return 36;
  2345. + }
  2346. +
  2347. + memset(buf, 0, 8);
  2348. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  2349. + if (mod_data.removable)
  2350. + buf[1] = 0x80;
  2351. + buf[2] = 2; // ANSI SCSI level 2
  2352. + buf[3] = 2; // SCSI-2 INQUIRY data format
  2353. + buf[4] = 31; // Additional length
  2354. + // No special options
  2355. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  2356. + (mod_data.cdrom ? product_cdrom_id :
  2357. + product_disk_id),
  2358. + mod_data.release);
  2359. + return 36;
  2360. +}
  2361. +
  2362. +
  2363. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2364. +{
  2365. + struct fsg_lun *curlun = fsg->curlun;
  2366. + u8 *buf = (u8 *) bh->buf;
  2367. + u32 sd, sdinfo;
  2368. + int valid;
  2369. +
  2370. + /*
  2371. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  2372. + *
  2373. + * If a REQUEST SENSE command is received from an initiator
  2374. + * with a pending unit attention condition (before the target
  2375. + * generates the contingent allegiance condition), then the
  2376. + * target shall either:
  2377. + * a) report any pending sense data and preserve the unit
  2378. + * attention condition on the logical unit, or,
  2379. + * b) report the unit attention condition, may discard any
  2380. + * pending sense data, and clear the unit attention
  2381. + * condition on the logical unit for that initiator.
  2382. + *
  2383. + * FSG normally uses option a); enable this code to use option b).
  2384. + */
  2385. +#if 0
  2386. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  2387. + curlun->sense_data = curlun->unit_attention_data;
  2388. + curlun->unit_attention_data = SS_NO_SENSE;
  2389. + }
  2390. +#endif
  2391. +
  2392. + if (!curlun) { // Unsupported LUNs are okay
  2393. + fsg->bad_lun_okay = 1;
  2394. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  2395. + sdinfo = 0;
  2396. + valid = 0;
  2397. + } else {
  2398. + sd = curlun->sense_data;
  2399. + sdinfo = curlun->sense_data_info;
  2400. + valid = curlun->info_valid << 7;
  2401. + curlun->sense_data = SS_NO_SENSE;
  2402. + curlun->sense_data_info = 0;
  2403. + curlun->info_valid = 0;
  2404. + }
  2405. +
  2406. + memset(buf, 0, 18);
  2407. + buf[0] = valid | 0x70; // Valid, current error
  2408. + buf[2] = SK(sd);
  2409. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  2410. + buf[7] = 18 - 8; // Additional sense length
  2411. + buf[12] = ASC(sd);
  2412. + buf[13] = ASCQ(sd);
  2413. + return 18;
  2414. +}
  2415. +
  2416. +
  2417. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2418. +{
  2419. + struct fsg_lun *curlun = fsg->curlun;
  2420. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  2421. + int pmi = fsg->cmnd[8];
  2422. + u8 *buf = (u8 *) bh->buf;
  2423. +
  2424. + /* Check the PMI and LBA fields */
  2425. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  2426. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2427. + return -EINVAL;
  2428. + }
  2429. +
  2430. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  2431. + /* Max logical block */
  2432. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  2433. + return 8;
  2434. +}
  2435. +
  2436. +
  2437. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2438. +{
  2439. + struct fsg_lun *curlun = fsg->curlun;
  2440. + int msf = fsg->cmnd[1] & 0x02;
  2441. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  2442. + u8 *buf = (u8 *) bh->buf;
  2443. +
  2444. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  2445. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2446. + return -EINVAL;
  2447. + }
  2448. + if (lba >= curlun->num_sectors) {
  2449. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2450. + return -EINVAL;
  2451. + }
  2452. +
  2453. + memset(buf, 0, 8);
  2454. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  2455. + store_cdrom_address(&buf[4], msf, lba);
  2456. + return 8;
  2457. +}
  2458. +
  2459. +
  2460. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2461. +{
  2462. + struct fsg_lun *curlun = fsg->curlun;
  2463. + int msf = fsg->cmnd[1] & 0x02;
  2464. + int start_track = fsg->cmnd[6];
  2465. + u8 *buf = (u8 *) bh->buf;
  2466. +
  2467. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  2468. + start_track > 1) {
  2469. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2470. + return -EINVAL;
  2471. + }
  2472. +
  2473. + memset(buf, 0, 20);
  2474. + buf[1] = (20-2); /* TOC data length */
  2475. + buf[2] = 1; /* First track number */
  2476. + buf[3] = 1; /* Last track number */
  2477. + buf[5] = 0x16; /* Data track, copying allowed */
  2478. + buf[6] = 0x01; /* Only track is number 1 */
  2479. + store_cdrom_address(&buf[8], msf, 0);
  2480. +
  2481. + buf[13] = 0x16; /* Lead-out track is data */
  2482. + buf[14] = 0xAA; /* Lead-out track number */
  2483. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  2484. + return 20;
  2485. +}
  2486. +
  2487. +
  2488. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2489. +{
  2490. + struct fsg_lun *curlun = fsg->curlun;
  2491. + int mscmnd = fsg->cmnd[0];
  2492. + u8 *buf = (u8 *) bh->buf;
  2493. + u8 *buf0 = buf;
  2494. + int pc, page_code;
  2495. + int changeable_values, all_pages;
  2496. + int valid_page = 0;
  2497. + int len, limit;
  2498. +
  2499. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  2500. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2501. + return -EINVAL;
  2502. + }
  2503. + pc = fsg->cmnd[2] >> 6;
  2504. + page_code = fsg->cmnd[2] & 0x3f;
  2505. + if (pc == 3) {
  2506. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  2507. + return -EINVAL;
  2508. + }
  2509. + changeable_values = (pc == 1);
  2510. + all_pages = (page_code == 0x3f);
  2511. +
  2512. + /* Write the mode parameter header. Fixed values are: default
  2513. + * medium type, no cache control (DPOFUA), and no block descriptors.
  2514. + * The only variable value is the WriteProtect bit. We will fill in
  2515. + * the mode data length later. */
  2516. + memset(buf, 0, 8);
  2517. + if (mscmnd == MODE_SENSE) {
  2518. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  2519. + buf += 4;
  2520. + limit = 255;
  2521. + } else { // MODE_SENSE_10
  2522. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  2523. + buf += 8;
  2524. + limit = 65535; // Should really be mod_data.buflen
  2525. + }
  2526. +
  2527. + /* No block descriptors */
  2528. +
  2529. + /* The mode pages, in numerical order. The only page we support
  2530. + * is the Caching page. */
  2531. + if (page_code == 0x08 || all_pages) {
  2532. + valid_page = 1;
  2533. + buf[0] = 0x08; // Page code
  2534. + buf[1] = 10; // Page length
  2535. + memset(buf+2, 0, 10); // None of the fields are changeable
  2536. +
  2537. + if (!changeable_values) {
  2538. + buf[2] = 0x04; // Write cache enable,
  2539. + // Read cache not disabled
  2540. + // No cache retention priorities
  2541. + put_unaligned_be16(0xffff, &buf[4]);
  2542. + /* Don't disable prefetch */
  2543. + /* Minimum prefetch = 0 */
  2544. + put_unaligned_be16(0xffff, &buf[8]);
  2545. + /* Maximum prefetch */
  2546. + put_unaligned_be16(0xffff, &buf[10]);
  2547. + /* Maximum prefetch ceiling */
  2548. + }
  2549. + buf += 12;
  2550. + }
  2551. +
  2552. + /* Check that a valid page was requested and the mode data length
  2553. + * isn't too long. */
  2554. + len = buf - buf0;
  2555. + if (!valid_page || len > limit) {
  2556. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2557. + return -EINVAL;
  2558. + }
  2559. +
  2560. + /* Store the mode data length */
  2561. + if (mscmnd == MODE_SENSE)
  2562. + buf0[0] = len - 1;
  2563. + else
  2564. + put_unaligned_be16(len - 2, buf0);
  2565. + return len;
  2566. +}
  2567. +
  2568. +
  2569. +static int do_start_stop(struct fsg_dev *fsg)
  2570. +{
  2571. + struct fsg_lun *curlun = fsg->curlun;
  2572. + int loej, start;
  2573. +
  2574. + if (!mod_data.removable) {
  2575. + curlun->sense_data = SS_INVALID_COMMAND;
  2576. + return -EINVAL;
  2577. + }
  2578. +
  2579. + // int immed = fsg->cmnd[1] & 0x01;
  2580. + loej = fsg->cmnd[4] & 0x02;
  2581. + start = fsg->cmnd[4] & 0x01;
  2582. +
  2583. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  2584. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  2585. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  2586. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2587. + return -EINVAL;
  2588. + }
  2589. +
  2590. + if (!start) {
  2591. +
  2592. + /* Are we allowed to unload the media? */
  2593. + if (curlun->prevent_medium_removal) {
  2594. + LDBG(curlun, "unload attempt prevented\n");
  2595. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  2596. + return -EINVAL;
  2597. + }
  2598. + if (loej) { // Simulate an unload/eject
  2599. + up_read(&fsg->filesem);
  2600. + down_write(&fsg->filesem);
  2601. + fsg_lun_close(curlun);
  2602. + up_write(&fsg->filesem);
  2603. + down_read(&fsg->filesem);
  2604. + }
  2605. + } else {
  2606. +
  2607. + /* Our emulation doesn't support mounting; the medium is
  2608. + * available for use as soon as it is loaded. */
  2609. + if (!fsg_lun_is_open(curlun)) {
  2610. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  2611. + return -EINVAL;
  2612. + }
  2613. + }
  2614. +#endif
  2615. + return 0;
  2616. +}
  2617. +
  2618. +
  2619. +static int do_prevent_allow(struct fsg_dev *fsg)
  2620. +{
  2621. + struct fsg_lun *curlun = fsg->curlun;
  2622. + int prevent;
  2623. +
  2624. + if (!mod_data.removable) {
  2625. + curlun->sense_data = SS_INVALID_COMMAND;
  2626. + return -EINVAL;
  2627. + }
  2628. +
  2629. + prevent = fsg->cmnd[4] & 0x01;
  2630. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  2631. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2632. + return -EINVAL;
  2633. + }
  2634. +
  2635. + if (curlun->prevent_medium_removal && !prevent)
  2636. + fsg_lun_fsync_sub(curlun);
  2637. + curlun->prevent_medium_removal = prevent;
  2638. + return 0;
  2639. +}
  2640. +
  2641. +
  2642. +static int do_read_format_capacities(struct fsg_dev *fsg,
  2643. + struct fsg_buffhd *bh)
  2644. +{
  2645. + struct fsg_lun *curlun = fsg->curlun;
  2646. + u8 *buf = (u8 *) bh->buf;
  2647. +
  2648. + buf[0] = buf[1] = buf[2] = 0;
  2649. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  2650. + buf += 4;
  2651. +
  2652. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  2653. + /* Number of blocks */
  2654. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  2655. + buf[4] = 0x02; /* Current capacity */
  2656. + return 12;
  2657. +}
  2658. +
  2659. +
  2660. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2661. +{
  2662. + struct fsg_lun *curlun = fsg->curlun;
  2663. +
  2664. + /* We don't support MODE SELECT */
  2665. + curlun->sense_data = SS_INVALID_COMMAND;
  2666. + return -EINVAL;
  2667. +}
  2668. +
  2669. +
  2670. +/*-------------------------------------------------------------------------*/
  2671. +
  2672. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  2673. +{
  2674. + int rc;
  2675. +
  2676. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  2677. + if (rc == -EAGAIN)
  2678. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  2679. + while (rc != 0) {
  2680. + if (rc != -EAGAIN) {
  2681. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  2682. + rc = 0;
  2683. + break;
  2684. + }
  2685. +
  2686. + /* Wait for a short time and then try again */
  2687. + if (msleep_interruptible(100) != 0)
  2688. + return -EINTR;
  2689. + rc = usb_ep_set_halt(fsg->bulk_in);
  2690. + }
  2691. + return rc;
  2692. +}
  2693. +
  2694. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  2695. +{
  2696. + int rc;
  2697. +
  2698. + DBG(fsg, "bulk-in set wedge\n");
  2699. + rc = usb_ep_set_wedge(fsg->bulk_in);
  2700. + if (rc == -EAGAIN)
  2701. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  2702. + while (rc != 0) {
  2703. + if (rc != -EAGAIN) {
  2704. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  2705. + rc = 0;
  2706. + break;
  2707. + }
  2708. +
  2709. + /* Wait for a short time and then try again */
  2710. + if (msleep_interruptible(100) != 0)
  2711. + return -EINTR;
  2712. + rc = usb_ep_set_wedge(fsg->bulk_in);
  2713. + }
  2714. + return rc;
  2715. +}
  2716. +
  2717. +static int throw_away_data(struct fsg_dev *fsg)
  2718. +{
  2719. + struct fsg_buffhd *bh;
  2720. + u32 amount;
  2721. + int rc;
  2722. +
  2723. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  2724. + fsg->usb_amount_left > 0) {
  2725. +
  2726. + /* Throw away the data in a filled buffer */
  2727. + if (bh->state == BUF_STATE_FULL) {
  2728. + smp_rmb();
  2729. + bh->state = BUF_STATE_EMPTY;
  2730. + fsg->next_buffhd_to_drain = bh->next;
  2731. +
  2732. + /* A short packet or an error ends everything */
  2733. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  2734. + bh->outreq->status != 0) {
  2735. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2736. + return -EINTR;
  2737. + }
  2738. + continue;
  2739. + }
  2740. +
  2741. + /* Try to submit another request if we need one */
  2742. + bh = fsg->next_buffhd_to_fill;
  2743. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  2744. + amount = min(fsg->usb_amount_left,
  2745. + (u32) mod_data.buflen);
  2746. +
  2747. + /* Except at the end of the transfer, amount will be
  2748. + * equal to the buffer size, which is divisible by
  2749. + * the bulk-out maxpacket size.
  2750. + */
  2751. + set_bulk_out_req_length(fsg, bh, amount);
  2752. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  2753. + &bh->outreq_busy, &bh->state);
  2754. + fsg->next_buffhd_to_fill = bh->next;
  2755. + fsg->usb_amount_left -= amount;
  2756. + continue;
  2757. + }
  2758. +
  2759. + /* Otherwise wait for something to happen */
  2760. + rc = sleep_thread(fsg);
  2761. + if (rc)
  2762. + return rc;
  2763. + }
  2764. + return 0;
  2765. +}
  2766. +
  2767. +
  2768. +static int finish_reply(struct fsg_dev *fsg)
  2769. +{
  2770. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  2771. + int rc = 0;
  2772. +
  2773. + switch (fsg->data_dir) {
  2774. + case DATA_DIR_NONE:
  2775. + break; // Nothing to send
  2776. +
  2777. + /* If we don't know whether the host wants to read or write,
  2778. + * this must be CB or CBI with an unknown command. We mustn't
  2779. + * try to send or receive any data. So stall both bulk pipes
  2780. + * if we can and wait for a reset. */
  2781. + case DATA_DIR_UNKNOWN:
  2782. + if (mod_data.can_stall) {
  2783. + fsg_set_halt(fsg, fsg->bulk_out);
  2784. + rc = halt_bulk_in_endpoint(fsg);
  2785. + }
  2786. + break;
  2787. +
  2788. + /* All but the last buffer of data must have already been sent */
  2789. + case DATA_DIR_TO_HOST:
  2790. + if (fsg->data_size == 0)
  2791. + ; // Nothing to send
  2792. +
  2793. + /* If there's no residue, simply send the last buffer */
  2794. + else if (fsg->residue == 0) {
  2795. + bh->inreq->zero = 0;
  2796. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2797. + &bh->inreq_busy, &bh->state);
  2798. + fsg->next_buffhd_to_fill = bh->next;
  2799. + }
  2800. +
  2801. + /* There is a residue. For CB and CBI, simply mark the end
  2802. + * of the data with a short packet. However, if we are
  2803. + * allowed to stall, there was no data at all (residue ==
  2804. + * data_size), and the command failed (invalid LUN or
  2805. + * sense data is set), then halt the bulk-in endpoint
  2806. + * instead. */
  2807. + else if (!transport_is_bbb()) {
  2808. + if (mod_data.can_stall &&
  2809. + fsg->residue == fsg->data_size &&
  2810. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  2811. + bh->state = BUF_STATE_EMPTY;
  2812. + rc = halt_bulk_in_endpoint(fsg);
  2813. + } else {
  2814. + bh->inreq->zero = 1;
  2815. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2816. + &bh->inreq_busy, &bh->state);
  2817. + fsg->next_buffhd_to_fill = bh->next;
  2818. + }
  2819. + }
  2820. +
  2821. + /*
  2822. + * For Bulk-only, mark the end of the data with a short
  2823. + * packet. If we are allowed to stall, halt the bulk-in
  2824. + * endpoint. (Note: This violates the Bulk-Only Transport
  2825. + * specification, which requires us to pad the data if we
  2826. + * don't halt the endpoint. Presumably nobody will mind.)
  2827. + */
  2828. + else {
  2829. + bh->inreq->zero = 1;
  2830. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2831. + &bh->inreq_busy, &bh->state);
  2832. + fsg->next_buffhd_to_fill = bh->next;
  2833. + if (mod_data.can_stall)
  2834. + rc = halt_bulk_in_endpoint(fsg);
  2835. + }
  2836. + break;
  2837. +
  2838. + /* We have processed all we want from the data the host has sent.
  2839. + * There may still be outstanding bulk-out requests. */
  2840. + case DATA_DIR_FROM_HOST:
  2841. + if (fsg->residue == 0)
  2842. + ; // Nothing to receive
  2843. +
  2844. + /* Did the host stop sending unexpectedly early? */
  2845. + else if (fsg->short_packet_received) {
  2846. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2847. + rc = -EINTR;
  2848. + }
  2849. +
  2850. + /* We haven't processed all the incoming data. Even though
  2851. + * we may be allowed to stall, doing so would cause a race.
  2852. + * The controller may already have ACK'ed all the remaining
  2853. + * bulk-out packets, in which case the host wouldn't see a
  2854. + * STALL. Not realizing the endpoint was halted, it wouldn't
  2855. + * clear the halt -- leading to problems later on. */
  2856. +#if 0
  2857. + else if (mod_data.can_stall) {
  2858. + fsg_set_halt(fsg, fsg->bulk_out);
  2859. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2860. + rc = -EINTR;
  2861. + }
  2862. +#endif
  2863. +
  2864. + /* We can't stall. Read in the excess data and throw it
  2865. + * all away. */
  2866. + else
  2867. + rc = throw_away_data(fsg);
  2868. + break;
  2869. + }
  2870. + return rc;
  2871. +}
  2872. +
  2873. +
  2874. +static int send_status(struct fsg_dev *fsg)
  2875. +{
  2876. + struct fsg_lun *curlun = fsg->curlun;
  2877. + struct fsg_buffhd *bh;
  2878. + int rc;
  2879. + u8 status = US_BULK_STAT_OK;
  2880. + u32 sd, sdinfo = 0;
  2881. +
  2882. + /* Wait for the next buffer to become available */
  2883. + bh = fsg->next_buffhd_to_fill;
  2884. + while (bh->state != BUF_STATE_EMPTY) {
  2885. + rc = sleep_thread(fsg);
  2886. + if (rc)
  2887. + return rc;
  2888. + }
  2889. +
  2890. + if (curlun) {
  2891. + sd = curlun->sense_data;
  2892. + sdinfo = curlun->sense_data_info;
  2893. + } else if (fsg->bad_lun_okay)
  2894. + sd = SS_NO_SENSE;
  2895. + else
  2896. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  2897. +
  2898. + if (fsg->phase_error) {
  2899. + DBG(fsg, "sending phase-error status\n");
  2900. + status = US_BULK_STAT_PHASE;
  2901. + sd = SS_INVALID_COMMAND;
  2902. + } else if (sd != SS_NO_SENSE) {
  2903. + DBG(fsg, "sending command-failure status\n");
  2904. + status = US_BULK_STAT_FAIL;
  2905. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  2906. + " info x%x\n",
  2907. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  2908. + }
  2909. +
  2910. + if (transport_is_bbb()) {
  2911. + struct bulk_cs_wrap *csw = bh->buf;
  2912. +
  2913. + /* Store and send the Bulk-only CSW */
  2914. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  2915. + csw->Tag = fsg->tag;
  2916. + csw->Residue = cpu_to_le32(fsg->residue);
  2917. + csw->Status = status;
  2918. +
  2919. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  2920. + bh->inreq->zero = 0;
  2921. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2922. + &bh->inreq_busy, &bh->state);
  2923. +
  2924. + } else if (mod_data.transport_type == USB_PR_CB) {
  2925. +
  2926. + /* Control-Bulk transport has no status phase! */
  2927. + return 0;
  2928. +
  2929. + } else { // USB_PR_CBI
  2930. + struct interrupt_data *buf = bh->buf;
  2931. +
  2932. + /* Store and send the Interrupt data. UFI sends the ASC
  2933. + * and ASCQ bytes. Everything else sends a Type (which
  2934. + * is always 0) and the status Value. */
  2935. + if (mod_data.protocol_type == USB_SC_UFI) {
  2936. + buf->bType = ASC(sd);
  2937. + buf->bValue = ASCQ(sd);
  2938. + } else {
  2939. + buf->bType = 0;
  2940. + buf->bValue = status;
  2941. + }
  2942. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  2943. +
  2944. + fsg->intr_buffhd = bh; // Point to the right buffhd
  2945. + fsg->intreq->buf = bh->inreq->buf;
  2946. + fsg->intreq->context = bh;
  2947. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  2948. + &fsg->intreq_busy, &bh->state);
  2949. + }
  2950. +
  2951. + fsg->next_buffhd_to_fill = bh->next;
  2952. + return 0;
  2953. +}
  2954. +
  2955. +
  2956. +/*-------------------------------------------------------------------------*/
  2957. +
  2958. +/* Check whether the command is properly formed and whether its data size
  2959. + * and direction agree with the values we already have. */
  2960. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  2961. + enum data_direction data_dir, unsigned int mask,
  2962. + int needs_medium, const char *name)
  2963. +{
  2964. + int i;
  2965. + int lun = fsg->cmnd[1] >> 5;
  2966. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  2967. + char hdlen[20];
  2968. + struct fsg_lun *curlun;
  2969. +
  2970. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  2971. + * Transparent SCSI doesn't pad. */
  2972. + if (protocol_is_scsi())
  2973. + ;
  2974. +
  2975. + /* There's some disagreement as to whether RBC pads commands or not.
  2976. + * We'll play it safe and accept either form. */
  2977. + else if (mod_data.protocol_type == USB_SC_RBC) {
  2978. + if (fsg->cmnd_size == 12)
  2979. + cmnd_size = 12;
  2980. +
  2981. + /* All the other protocols pad to 12 bytes */
  2982. + } else
  2983. + cmnd_size = 12;
  2984. +
  2985. + hdlen[0] = 0;
  2986. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  2987. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  2988. + fsg->data_size);
  2989. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  2990. + name, cmnd_size, dirletter[(int) data_dir],
  2991. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  2992. +
  2993. + /* We can't reply at all until we know the correct data direction
  2994. + * and size. */
  2995. + if (fsg->data_size_from_cmnd == 0)
  2996. + data_dir = DATA_DIR_NONE;
  2997. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  2998. + fsg->data_dir = data_dir;
  2999. + fsg->data_size = fsg->data_size_from_cmnd;
  3000. +
  3001. + } else { // Bulk-only
  3002. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  3003. +
  3004. + /* Host data size < Device data size is a phase error.
  3005. + * Carry out the command, but only transfer as much
  3006. + * as we are allowed. */
  3007. + fsg->data_size_from_cmnd = fsg->data_size;
  3008. + fsg->phase_error = 1;
  3009. + }
  3010. + }
  3011. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  3012. +
  3013. + /* Conflicting data directions is a phase error */
  3014. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  3015. + fsg->phase_error = 1;
  3016. + return -EINVAL;
  3017. + }
  3018. +
  3019. + /* Verify the length of the command itself */
  3020. + if (cmnd_size != fsg->cmnd_size) {
  3021. +
  3022. + /* Special case workaround: There are plenty of buggy SCSI
  3023. + * implementations. Many have issues with cbw->Length
  3024. + * field passing a wrong command size. For those cases we
  3025. + * always try to work around the problem by using the length
  3026. + * sent by the host side provided it is at least as large
  3027. + * as the correct command length.
  3028. + * Examples of such cases would be MS-Windows, which issues
  3029. + * REQUEST SENSE with cbw->Length == 12 where it should
  3030. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  3031. + * REQUEST SENSE with cbw->Length == 10 where it should
  3032. + * be 6 as well.
  3033. + */
  3034. + if (cmnd_size <= fsg->cmnd_size) {
  3035. + DBG(fsg, "%s is buggy! Expected length %d "
  3036. + "but we got %d\n", name,
  3037. + cmnd_size, fsg->cmnd_size);
  3038. + cmnd_size = fsg->cmnd_size;
  3039. + } else {
  3040. + fsg->phase_error = 1;
  3041. + return -EINVAL;
  3042. + }
  3043. + }
  3044. +
  3045. + /* Check that the LUN values are consistent */
  3046. + if (transport_is_bbb()) {
  3047. + if (fsg->lun != lun)
  3048. + DBG(fsg, "using LUN %d from CBW, "
  3049. + "not LUN %d from CDB\n",
  3050. + fsg->lun, lun);
  3051. + }
  3052. +
  3053. + /* Check the LUN */
  3054. + curlun = fsg->curlun;
  3055. + if (curlun) {
  3056. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  3057. + curlun->sense_data = SS_NO_SENSE;
  3058. + curlun->sense_data_info = 0;
  3059. + curlun->info_valid = 0;
  3060. + }
  3061. + } else {
  3062. + fsg->bad_lun_okay = 0;
  3063. +
  3064. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  3065. + * to use unsupported LUNs; all others may not. */
  3066. + if (fsg->cmnd[0] != INQUIRY &&
  3067. + fsg->cmnd[0] != REQUEST_SENSE) {
  3068. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  3069. + return -EINVAL;
  3070. + }
  3071. + }
  3072. +
  3073. + /* If a unit attention condition exists, only INQUIRY and
  3074. + * REQUEST SENSE commands are allowed; anything else must fail. */
  3075. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  3076. + fsg->cmnd[0] != INQUIRY &&
  3077. + fsg->cmnd[0] != REQUEST_SENSE) {
  3078. + curlun->sense_data = curlun->unit_attention_data;
  3079. + curlun->unit_attention_data = SS_NO_SENSE;
  3080. + return -EINVAL;
  3081. + }
  3082. +
  3083. + /* Check that only command bytes listed in the mask are non-zero */
  3084. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  3085. + for (i = 1; i < cmnd_size; ++i) {
  3086. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  3087. + if (curlun)
  3088. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  3089. + return -EINVAL;
  3090. + }
  3091. + }
  3092. +
  3093. + /* If the medium isn't mounted and the command needs to access
  3094. + * it, return an error. */
  3095. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  3096. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  3097. + return -EINVAL;
  3098. + }
  3099. +
  3100. + return 0;
  3101. +}
  3102. +
  3103. +/* wrapper of check_command for data size in blocks handling */
  3104. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  3105. + enum data_direction data_dir, unsigned int mask,
  3106. + int needs_medium, const char *name)
  3107. +{
  3108. + if (fsg->curlun)
  3109. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  3110. + return check_command(fsg, cmnd_size, data_dir,
  3111. + mask, needs_medium, name);
  3112. +}
  3113. +
  3114. +static int do_scsi_command(struct fsg_dev *fsg)
  3115. +{
  3116. + struct fsg_buffhd *bh;
  3117. + int rc;
  3118. + int reply = -EINVAL;
  3119. + int i;
  3120. + static char unknown[16];
  3121. +
  3122. + dump_cdb(fsg);
  3123. +
  3124. + /* Wait for the next buffer to become available for data or status */
  3125. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  3126. + while (bh->state != BUF_STATE_EMPTY) {
  3127. + rc = sleep_thread(fsg);
  3128. + if (rc)
  3129. + return rc;
  3130. + }
  3131. + fsg->phase_error = 0;
  3132. + fsg->short_packet_received = 0;
  3133. +
  3134. + down_read(&fsg->filesem); // We're using the backing file
  3135. + switch (fsg->cmnd[0]) {
  3136. +
  3137. + case INQUIRY:
  3138. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3139. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3140. + (1<<4), 0,
  3141. + "INQUIRY")) == 0)
  3142. + reply = do_inquiry(fsg, bh);
  3143. + break;
  3144. +
  3145. + case MODE_SELECT:
  3146. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3147. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  3148. + (1<<1) | (1<<4), 0,
  3149. + "MODE SELECT(6)")) == 0)
  3150. + reply = do_mode_select(fsg, bh);
  3151. + break;
  3152. +
  3153. + case MODE_SELECT_10:
  3154. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3155. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  3156. + (1<<1) | (3<<7), 0,
  3157. + "MODE SELECT(10)")) == 0)
  3158. + reply = do_mode_select(fsg, bh);
  3159. + break;
  3160. +
  3161. + case MODE_SENSE:
  3162. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3163. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3164. + (1<<1) | (1<<2) | (1<<4), 0,
  3165. + "MODE SENSE(6)")) == 0)
  3166. + reply = do_mode_sense(fsg, bh);
  3167. + break;
  3168. +
  3169. + case MODE_SENSE_10:
  3170. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3171. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3172. + (1<<1) | (1<<2) | (3<<7), 0,
  3173. + "MODE SENSE(10)")) == 0)
  3174. + reply = do_mode_sense(fsg, bh);
  3175. + break;
  3176. +
  3177. + case ALLOW_MEDIUM_REMOVAL:
  3178. + fsg->data_size_from_cmnd = 0;
  3179. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  3180. + (1<<4), 0,
  3181. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  3182. + reply = do_prevent_allow(fsg);
  3183. + break;
  3184. +
  3185. + case READ_6:
  3186. + i = fsg->cmnd[4];
  3187. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  3188. + if ((reply = check_command_size_in_blocks(fsg, 6,
  3189. + DATA_DIR_TO_HOST,
  3190. + (7<<1) | (1<<4), 1,
  3191. + "READ(6)")) == 0)
  3192. + reply = do_read(fsg);
  3193. + break;
  3194. +
  3195. + case READ_10:
  3196. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3197. + if ((reply = check_command_size_in_blocks(fsg, 10,
  3198. + DATA_DIR_TO_HOST,
  3199. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3200. + "READ(10)")) == 0)
  3201. + reply = do_read(fsg);
  3202. + break;
  3203. +
  3204. + case READ_12:
  3205. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  3206. + if ((reply = check_command_size_in_blocks(fsg, 12,
  3207. + DATA_DIR_TO_HOST,
  3208. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  3209. + "READ(12)")) == 0)
  3210. + reply = do_read(fsg);
  3211. + break;
  3212. +
  3213. + case READ_CAPACITY:
  3214. + fsg->data_size_from_cmnd = 8;
  3215. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3216. + (0xf<<2) | (1<<8), 1,
  3217. + "READ CAPACITY")) == 0)
  3218. + reply = do_read_capacity(fsg, bh);
  3219. + break;
  3220. +
  3221. + case READ_HEADER:
  3222. + if (!mod_data.cdrom)
  3223. + goto unknown_cmnd;
  3224. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3225. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3226. + (3<<7) | (0x1f<<1), 1,
  3227. + "READ HEADER")) == 0)
  3228. + reply = do_read_header(fsg, bh);
  3229. + break;
  3230. +
  3231. + case READ_TOC:
  3232. + if (!mod_data.cdrom)
  3233. + goto unknown_cmnd;
  3234. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3235. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3236. + (7<<6) | (1<<1), 1,
  3237. + "READ TOC")) == 0)
  3238. + reply = do_read_toc(fsg, bh);
  3239. + break;
  3240. +
  3241. + case READ_FORMAT_CAPACITIES:
  3242. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3243. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3244. + (3<<7), 1,
  3245. + "READ FORMAT CAPACITIES")) == 0)
  3246. + reply = do_read_format_capacities(fsg, bh);
  3247. + break;
  3248. +
  3249. + case REQUEST_SENSE:
  3250. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3251. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3252. + (1<<4), 0,
  3253. + "REQUEST SENSE")) == 0)
  3254. + reply = do_request_sense(fsg, bh);
  3255. + break;
  3256. +
  3257. + case START_STOP:
  3258. + fsg->data_size_from_cmnd = 0;
  3259. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  3260. + (1<<1) | (1<<4), 0,
  3261. + "START-STOP UNIT")) == 0)
  3262. + reply = do_start_stop(fsg);
  3263. + break;
  3264. +
  3265. + case SYNCHRONIZE_CACHE:
  3266. + fsg->data_size_from_cmnd = 0;
  3267. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  3268. + (0xf<<2) | (3<<7), 1,
  3269. + "SYNCHRONIZE CACHE")) == 0)
  3270. + reply = do_synchronize_cache(fsg);
  3271. + break;
  3272. +
  3273. + case TEST_UNIT_READY:
  3274. + fsg->data_size_from_cmnd = 0;
  3275. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  3276. + 0, 1,
  3277. + "TEST UNIT READY");
  3278. + break;
  3279. +
  3280. + /* Although optional, this command is used by MS-Windows. We
  3281. + * support a minimal version: BytChk must be 0. */
  3282. + case VERIFY:
  3283. + fsg->data_size_from_cmnd = 0;
  3284. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  3285. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3286. + "VERIFY")) == 0)
  3287. + reply = do_verify(fsg);
  3288. + break;
  3289. +
  3290. + case WRITE_6:
  3291. + i = fsg->cmnd[4];
  3292. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  3293. + if ((reply = check_command_size_in_blocks(fsg, 6,
  3294. + DATA_DIR_FROM_HOST,
  3295. + (7<<1) | (1<<4), 1,
  3296. + "WRITE(6)")) == 0)
  3297. + reply = do_write(fsg);
  3298. + break;
  3299. +
  3300. + case WRITE_10:
  3301. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3302. + if ((reply = check_command_size_in_blocks(fsg, 10,
  3303. + DATA_DIR_FROM_HOST,
  3304. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3305. + "WRITE(10)")) == 0)
  3306. + reply = do_write(fsg);
  3307. + break;
  3308. +
  3309. + case WRITE_12:
  3310. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  3311. + if ((reply = check_command_size_in_blocks(fsg, 12,
  3312. + DATA_DIR_FROM_HOST,
  3313. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  3314. + "WRITE(12)")) == 0)
  3315. + reply = do_write(fsg);
  3316. + break;
  3317. +
  3318. + /* Some mandatory commands that we recognize but don't implement.
  3319. + * They don't mean much in this setting. It's left as an exercise
  3320. + * for anyone interested to implement RESERVE and RELEASE in terms
  3321. + * of Posix locks. */
  3322. + case FORMAT_UNIT:
  3323. + case RELEASE:
  3324. + case RESERVE:
  3325. + case SEND_DIAGNOSTIC:
  3326. + // Fall through
  3327. +
  3328. + default:
  3329. + unknown_cmnd:
  3330. + fsg->data_size_from_cmnd = 0;
  3331. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  3332. + if ((reply = check_command(fsg, fsg->cmnd_size,
  3333. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  3334. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  3335. + reply = -EINVAL;
  3336. + }
  3337. + break;
  3338. + }
  3339. + up_read(&fsg->filesem);
  3340. +
  3341. + if (reply == -EINTR || signal_pending(current))
  3342. + return -EINTR;
  3343. +
  3344. + /* Set up the single reply buffer for finish_reply() */
  3345. + if (reply == -EINVAL)
  3346. + reply = 0; // Error reply length
  3347. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  3348. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  3349. + bh->inreq->length = reply;
  3350. + bh->state = BUF_STATE_FULL;
  3351. + fsg->residue -= reply;
  3352. + } // Otherwise it's already set
  3353. +
  3354. + return 0;
  3355. +}
  3356. +
  3357. +
  3358. +/*-------------------------------------------------------------------------*/
  3359. +
  3360. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  3361. +{
  3362. + struct usb_request *req = bh->outreq;
  3363. + struct bulk_cb_wrap *cbw = req->buf;
  3364. +
  3365. + /* Was this a real packet? Should it be ignored? */
  3366. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  3367. + return -EINVAL;
  3368. +
  3369. + /* Is the CBW valid? */
  3370. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  3371. + cbw->Signature != cpu_to_le32(
  3372. + US_BULK_CB_SIGN)) {
  3373. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  3374. + req->actual,
  3375. + le32_to_cpu(cbw->Signature));
  3376. +
  3377. + /* The Bulk-only spec says we MUST stall the IN endpoint
  3378. + * (6.6.1), so it's unavoidable. It also says we must
  3379. + * retain this state until the next reset, but there's
  3380. + * no way to tell the controller driver it should ignore
  3381. + * Clear-Feature(HALT) requests.
  3382. + *
  3383. + * We aren't required to halt the OUT endpoint; instead
  3384. + * we can simply accept and discard any data received
  3385. + * until the next reset. */
  3386. + wedge_bulk_in_endpoint(fsg);
  3387. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  3388. + return -EINVAL;
  3389. + }
  3390. +
  3391. + /* Is the CBW meaningful? */
  3392. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  3393. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  3394. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  3395. + "cmdlen %u\n",
  3396. + cbw->Lun, cbw->Flags, cbw->Length);
  3397. +
  3398. + /* We can do anything we want here, so let's stall the
  3399. + * bulk pipes if we are allowed to. */
  3400. + if (mod_data.can_stall) {
  3401. + fsg_set_halt(fsg, fsg->bulk_out);
  3402. + halt_bulk_in_endpoint(fsg);
  3403. + }
  3404. + return -EINVAL;
  3405. + }
  3406. +
  3407. + /* Save the command for later */
  3408. + fsg->cmnd_size = cbw->Length;
  3409. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  3410. + if (cbw->Flags & US_BULK_FLAG_IN)
  3411. + fsg->data_dir = DATA_DIR_TO_HOST;
  3412. + else
  3413. + fsg->data_dir = DATA_DIR_FROM_HOST;
  3414. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  3415. + if (fsg->data_size == 0)
  3416. + fsg->data_dir = DATA_DIR_NONE;
  3417. + fsg->lun = cbw->Lun;
  3418. + fsg->tag = cbw->Tag;
  3419. + return 0;
  3420. +}
  3421. +
  3422. +
  3423. +static int get_next_command(struct fsg_dev *fsg)
  3424. +{
  3425. + struct fsg_buffhd *bh;
  3426. + int rc = 0;
  3427. +
  3428. + if (transport_is_bbb()) {
  3429. +
  3430. + /* Wait for the next buffer to become available */
  3431. + bh = fsg->next_buffhd_to_fill;
  3432. + while (bh->state != BUF_STATE_EMPTY) {
  3433. + rc = sleep_thread(fsg);
  3434. + if (rc)
  3435. + return rc;
  3436. + }
  3437. +
  3438. + /* Queue a request to read a Bulk-only CBW */
  3439. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  3440. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  3441. + &bh->outreq_busy, &bh->state);
  3442. +
  3443. + /* We will drain the buffer in software, which means we
  3444. + * can reuse it for the next filling. No need to advance
  3445. + * next_buffhd_to_fill. */
  3446. +
  3447. + /* Wait for the CBW to arrive */
  3448. + while (bh->state != BUF_STATE_FULL) {
  3449. + rc = sleep_thread(fsg);
  3450. + if (rc)
  3451. + return rc;
  3452. + }
  3453. + smp_rmb();
  3454. + rc = received_cbw(fsg, bh);
  3455. + bh->state = BUF_STATE_EMPTY;
  3456. +
  3457. + } else { // USB_PR_CB or USB_PR_CBI
  3458. +
  3459. + /* Wait for the next command to arrive */
  3460. + while (fsg->cbbuf_cmnd_size == 0) {
  3461. + rc = sleep_thread(fsg);
  3462. + if (rc)
  3463. + return rc;
  3464. + }
  3465. +
  3466. + /* Is the previous status interrupt request still busy?
  3467. + * The host is allowed to skip reading the status,
  3468. + * so we must cancel it. */
  3469. + if (fsg->intreq_busy)
  3470. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  3471. +
  3472. + /* Copy the command and mark the buffer empty */
  3473. + fsg->data_dir = DATA_DIR_UNKNOWN;
  3474. + spin_lock_irq(&fsg->lock);
  3475. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  3476. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  3477. + fsg->cbbuf_cmnd_size = 0;
  3478. + spin_unlock_irq(&fsg->lock);
  3479. +
  3480. + /* Use LUN from the command */
  3481. + fsg->lun = fsg->cmnd[1] >> 5;
  3482. + }
  3483. +
  3484. + /* Update current lun */
  3485. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  3486. + fsg->curlun = &fsg->luns[fsg->lun];
  3487. + else
  3488. + fsg->curlun = NULL;
  3489. +
  3490. + return rc;
  3491. +}
  3492. +
  3493. +
  3494. +/*-------------------------------------------------------------------------*/
  3495. +
  3496. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  3497. + const struct usb_endpoint_descriptor *d)
  3498. +{
  3499. + int rc;
  3500. +
  3501. + ep->driver_data = fsg;
  3502. + ep->desc = d;
  3503. + rc = usb_ep_enable(ep);
  3504. + if (rc)
  3505. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  3506. + return rc;
  3507. +}
  3508. +
  3509. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  3510. + struct usb_request **preq)
  3511. +{
  3512. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  3513. + if (*preq)
  3514. + return 0;
  3515. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  3516. + return -ENOMEM;
  3517. +}
  3518. +
  3519. +/*
  3520. + * Reset interface setting and re-init endpoint state (toggle etc).
  3521. + * Call with altsetting < 0 to disable the interface. The only other
  3522. + * available altsetting is 0, which enables the interface.
  3523. + */
  3524. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  3525. +{
  3526. + int rc = 0;
  3527. + int i;
  3528. + const struct usb_endpoint_descriptor *d;
  3529. +
  3530. + if (fsg->running)
  3531. + DBG(fsg, "reset interface\n");
  3532. +
  3533. +reset:
  3534. + /* Deallocate the requests */
  3535. + for (i = 0; i < fsg_num_buffers; ++i) {
  3536. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  3537. +
  3538. + if (bh->inreq) {
  3539. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  3540. + bh->inreq = NULL;
  3541. + }
  3542. + if (bh->outreq) {
  3543. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  3544. + bh->outreq = NULL;
  3545. + }
  3546. + }
  3547. + if (fsg->intreq) {
  3548. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  3549. + fsg->intreq = NULL;
  3550. + }
  3551. +
  3552. + /* Disable the endpoints */
  3553. + if (fsg->bulk_in_enabled) {
  3554. + usb_ep_disable(fsg->bulk_in);
  3555. + fsg->bulk_in_enabled = 0;
  3556. + }
  3557. + if (fsg->bulk_out_enabled) {
  3558. + usb_ep_disable(fsg->bulk_out);
  3559. + fsg->bulk_out_enabled = 0;
  3560. + }
  3561. + if (fsg->intr_in_enabled) {
  3562. + usb_ep_disable(fsg->intr_in);
  3563. + fsg->intr_in_enabled = 0;
  3564. + }
  3565. +
  3566. + fsg->running = 0;
  3567. + if (altsetting < 0 || rc != 0)
  3568. + return rc;
  3569. +
  3570. + DBG(fsg, "set interface %d\n", altsetting);
  3571. +
  3572. + /* Enable the endpoints */
  3573. + d = fsg_ep_desc(fsg->gadget,
  3574. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  3575. + &fsg_ss_bulk_in_desc);
  3576. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  3577. + goto reset;
  3578. + fsg->bulk_in_enabled = 1;
  3579. +
  3580. + d = fsg_ep_desc(fsg->gadget,
  3581. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  3582. + &fsg_ss_bulk_out_desc);
  3583. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  3584. + goto reset;
  3585. + fsg->bulk_out_enabled = 1;
  3586. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  3587. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  3588. +
  3589. + if (transport_is_cbi()) {
  3590. + d = fsg_ep_desc(fsg->gadget,
  3591. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  3592. + &fsg_ss_intr_in_desc);
  3593. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  3594. + goto reset;
  3595. + fsg->intr_in_enabled = 1;
  3596. + }
  3597. +
  3598. + /* Allocate the requests */
  3599. + for (i = 0; i < fsg_num_buffers; ++i) {
  3600. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  3601. +
  3602. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  3603. + goto reset;
  3604. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  3605. + goto reset;
  3606. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  3607. + bh->inreq->context = bh->outreq->context = bh;
  3608. + bh->inreq->complete = bulk_in_complete;
  3609. + bh->outreq->complete = bulk_out_complete;
  3610. + }
  3611. + if (transport_is_cbi()) {
  3612. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  3613. + goto reset;
  3614. + fsg->intreq->complete = intr_in_complete;
  3615. + }
  3616. +
  3617. + fsg->running = 1;
  3618. + for (i = 0; i < fsg->nluns; ++i)
  3619. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  3620. + return rc;
  3621. +}
  3622. +
  3623. +
  3624. +/*
  3625. + * Change our operational configuration. This code must agree with the code
  3626. + * that returns config descriptors, and with interface altsetting code.
  3627. + *
  3628. + * It's also responsible for power management interactions. Some
  3629. + * configurations might not work with our current power sources.
  3630. + * For now we just assume the gadget is always self-powered.
  3631. + */
  3632. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  3633. +{
  3634. + int rc = 0;
  3635. +
  3636. + /* Disable the single interface */
  3637. + if (fsg->config != 0) {
  3638. + DBG(fsg, "reset config\n");
  3639. + fsg->config = 0;
  3640. + rc = do_set_interface(fsg, -1);
  3641. + }
  3642. +
  3643. + /* Enable the interface */
  3644. + if (new_config != 0) {
  3645. + fsg->config = new_config;
  3646. + if ((rc = do_set_interface(fsg, 0)) != 0)
  3647. + fsg->config = 0; // Reset on errors
  3648. + else
  3649. + INFO(fsg, "%s config #%d\n",
  3650. + usb_speed_string(fsg->gadget->speed),
  3651. + fsg->config);
  3652. + }
  3653. + return rc;
  3654. +}
  3655. +
  3656. +
  3657. +/*-------------------------------------------------------------------------*/
  3658. +
  3659. +static void handle_exception(struct fsg_dev *fsg)
  3660. +{
  3661. + siginfo_t info;
  3662. + int sig;
  3663. + int i;
  3664. + int num_active;
  3665. + struct fsg_buffhd *bh;
  3666. + enum fsg_state old_state;
  3667. + u8 new_config;
  3668. + struct fsg_lun *curlun;
  3669. + unsigned int exception_req_tag;
  3670. + int rc;
  3671. +
  3672. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  3673. + * into a high-priority EXIT exception. */
  3674. + for (;;) {
  3675. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  3676. + if (!sig)
  3677. + break;
  3678. + if (sig != SIGUSR1) {
  3679. + if (fsg->state < FSG_STATE_EXIT)
  3680. + DBG(fsg, "Main thread exiting on signal\n");
  3681. + raise_exception(fsg, FSG_STATE_EXIT);
  3682. + }
  3683. + }
  3684. +
  3685. + /* Cancel all the pending transfers */
  3686. + if (fsg->intreq_busy)
  3687. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  3688. + for (i = 0; i < fsg_num_buffers; ++i) {
  3689. + bh = &fsg->buffhds[i];
  3690. + if (bh->inreq_busy)
  3691. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  3692. + if (bh->outreq_busy)
  3693. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  3694. + }
  3695. +
  3696. + /* Wait until everything is idle */
  3697. + for (;;) {
  3698. + num_active = fsg->intreq_busy;
  3699. + for (i = 0; i < fsg_num_buffers; ++i) {
  3700. + bh = &fsg->buffhds[i];
  3701. + num_active += bh->inreq_busy + bh->outreq_busy;
  3702. + }
  3703. + if (num_active == 0)
  3704. + break;
  3705. + if (sleep_thread(fsg))
  3706. + return;
  3707. + }
  3708. +
  3709. + /* Clear out the controller's fifos */
  3710. + if (fsg->bulk_in_enabled)
  3711. + usb_ep_fifo_flush(fsg->bulk_in);
  3712. + if (fsg->bulk_out_enabled)
  3713. + usb_ep_fifo_flush(fsg->bulk_out);
  3714. + if (fsg->intr_in_enabled)
  3715. + usb_ep_fifo_flush(fsg->intr_in);
  3716. +
  3717. + /* Reset the I/O buffer states and pointers, the SCSI
  3718. + * state, and the exception. Then invoke the handler. */
  3719. + spin_lock_irq(&fsg->lock);
  3720. +
  3721. + for (i = 0; i < fsg_num_buffers; ++i) {
  3722. + bh = &fsg->buffhds[i];
  3723. + bh->state = BUF_STATE_EMPTY;
  3724. + }
  3725. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  3726. + &fsg->buffhds[0];
  3727. +
  3728. + exception_req_tag = fsg->exception_req_tag;
  3729. + new_config = fsg->new_config;
  3730. + old_state = fsg->state;
  3731. +
  3732. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  3733. + fsg->state = FSG_STATE_STATUS_PHASE;
  3734. + else {
  3735. + for (i = 0; i < fsg->nluns; ++i) {
  3736. + curlun = &fsg->luns[i];
  3737. + curlun->prevent_medium_removal = 0;
  3738. + curlun->sense_data = curlun->unit_attention_data =
  3739. + SS_NO_SENSE;
  3740. + curlun->sense_data_info = 0;
  3741. + curlun->info_valid = 0;
  3742. + }
  3743. + fsg->state = FSG_STATE_IDLE;
  3744. + }
  3745. + spin_unlock_irq(&fsg->lock);
  3746. +
  3747. + /* Carry out any extra actions required for the exception */
  3748. + switch (old_state) {
  3749. + default:
  3750. + break;
  3751. +
  3752. + case FSG_STATE_ABORT_BULK_OUT:
  3753. + send_status(fsg);
  3754. + spin_lock_irq(&fsg->lock);
  3755. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  3756. + fsg->state = FSG_STATE_IDLE;
  3757. + spin_unlock_irq(&fsg->lock);
  3758. + break;
  3759. +
  3760. + case FSG_STATE_RESET:
  3761. + /* In case we were forced against our will to halt a
  3762. + * bulk endpoint, clear the halt now. (The SuperH UDC
  3763. + * requires this.) */
  3764. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  3765. + usb_ep_clear_halt(fsg->bulk_in);
  3766. +
  3767. + if (transport_is_bbb()) {
  3768. + if (fsg->ep0_req_tag == exception_req_tag)
  3769. + ep0_queue(fsg); // Complete the status stage
  3770. +
  3771. + } else if (transport_is_cbi())
  3772. + send_status(fsg); // Status by interrupt pipe
  3773. +
  3774. + /* Technically this should go here, but it would only be
  3775. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  3776. + * CONFIG_CHANGE cases. */
  3777. + // for (i = 0; i < fsg->nluns; ++i)
  3778. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  3779. + break;
  3780. +
  3781. + case FSG_STATE_INTERFACE_CHANGE:
  3782. + rc = do_set_interface(fsg, 0);
  3783. + if (fsg->ep0_req_tag != exception_req_tag)
  3784. + break;
  3785. + if (rc != 0) // STALL on errors
  3786. + fsg_set_halt(fsg, fsg->ep0);
  3787. + else // Complete the status stage
  3788. + ep0_queue(fsg);
  3789. + break;
  3790. +
  3791. + case FSG_STATE_CONFIG_CHANGE:
  3792. + rc = do_set_config(fsg, new_config);
  3793. + if (fsg->ep0_req_tag != exception_req_tag)
  3794. + break;
  3795. + if (rc != 0) // STALL on errors
  3796. + fsg_set_halt(fsg, fsg->ep0);
  3797. + else // Complete the status stage
  3798. + ep0_queue(fsg);
  3799. + break;
  3800. +
  3801. + case FSG_STATE_DISCONNECT:
  3802. + for (i = 0; i < fsg->nluns; ++i)
  3803. + fsg_lun_fsync_sub(fsg->luns + i);
  3804. + do_set_config(fsg, 0); // Unconfigured state
  3805. + break;
  3806. +
  3807. + case FSG_STATE_EXIT:
  3808. + case FSG_STATE_TERMINATED:
  3809. + do_set_config(fsg, 0); // Free resources
  3810. + spin_lock_irq(&fsg->lock);
  3811. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  3812. + spin_unlock_irq(&fsg->lock);
  3813. + break;
  3814. + }
  3815. +}
  3816. +
  3817. +
  3818. +/*-------------------------------------------------------------------------*/
  3819. +
  3820. +static int fsg_main_thread(void *fsg_)
  3821. +{
  3822. + struct fsg_dev *fsg = fsg_;
  3823. +
  3824. + /* Allow the thread to be killed by a signal, but set the signal mask
  3825. + * to block everything but INT, TERM, KILL, and USR1. */
  3826. + allow_signal(SIGINT);
  3827. + allow_signal(SIGTERM);
  3828. + allow_signal(SIGKILL);
  3829. + allow_signal(SIGUSR1);
  3830. +
  3831. + /* Allow the thread to be frozen */
  3832. + set_freezable();
  3833. +
  3834. + /* Arrange for userspace references to be interpreted as kernel
  3835. + * pointers. That way we can pass a kernel pointer to a routine
  3836. + * that expects a __user pointer and it will work okay. */
  3837. + set_fs(get_ds());
  3838. +
  3839. + /* The main loop */
  3840. + while (fsg->state != FSG_STATE_TERMINATED) {
  3841. + if (exception_in_progress(fsg) || signal_pending(current)) {
  3842. + handle_exception(fsg);
  3843. + continue;
  3844. + }
  3845. +
  3846. + if (!fsg->running) {
  3847. + sleep_thread(fsg);
  3848. + continue;
  3849. + }
  3850. +
  3851. + if (get_next_command(fsg))
  3852. + continue;
  3853. +
  3854. + spin_lock_irq(&fsg->lock);
  3855. + if (!exception_in_progress(fsg))
  3856. + fsg->state = FSG_STATE_DATA_PHASE;
  3857. + spin_unlock_irq(&fsg->lock);
  3858. +
  3859. + if (do_scsi_command(fsg) || finish_reply(fsg))
  3860. + continue;
  3861. +
  3862. + spin_lock_irq(&fsg->lock);
  3863. + if (!exception_in_progress(fsg))
  3864. + fsg->state = FSG_STATE_STATUS_PHASE;
  3865. + spin_unlock_irq(&fsg->lock);
  3866. +
  3867. + if (send_status(fsg))
  3868. + continue;
  3869. +
  3870. + spin_lock_irq(&fsg->lock);
  3871. + if (!exception_in_progress(fsg))
  3872. + fsg->state = FSG_STATE_IDLE;
  3873. + spin_unlock_irq(&fsg->lock);
  3874. + }
  3875. +
  3876. + spin_lock_irq(&fsg->lock);
  3877. + fsg->thread_task = NULL;
  3878. + spin_unlock_irq(&fsg->lock);
  3879. +
  3880. + /* If we are exiting because of a signal, unregister the
  3881. + * gadget driver. */
  3882. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  3883. + usb_gadget_unregister_driver(&fsg_driver);
  3884. +
  3885. + /* Let the unbind and cleanup routines know the thread has exited */
  3886. + complete_and_exit(&fsg->thread_notifier, 0);
  3887. +}
  3888. +
  3889. +
  3890. +/*-------------------------------------------------------------------------*/
  3891. +
  3892. +
  3893. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  3894. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  3895. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  3896. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  3897. +
  3898. +
  3899. +/*-------------------------------------------------------------------------*/
  3900. +
  3901. +static void fsg_release(struct kref *ref)
  3902. +{
  3903. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  3904. +
  3905. + kfree(fsg->luns);
  3906. + kfree(fsg);
  3907. +}
  3908. +
  3909. +static void lun_release(struct device *dev)
  3910. +{
  3911. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  3912. + struct fsg_dev *fsg =
  3913. + container_of(filesem, struct fsg_dev, filesem);
  3914. +
  3915. + kref_put(&fsg->ref, fsg_release);
  3916. +}
  3917. +
  3918. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  3919. +{
  3920. + struct fsg_dev *fsg = get_gadget_data(gadget);
  3921. + int i;
  3922. + struct fsg_lun *curlun;
  3923. + struct usb_request *req = fsg->ep0req;
  3924. +
  3925. + DBG(fsg, "unbind\n");
  3926. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  3927. +
  3928. + /* If the thread isn't already dead, tell it to exit now */
  3929. + if (fsg->state != FSG_STATE_TERMINATED) {
  3930. + raise_exception(fsg, FSG_STATE_EXIT);
  3931. + wait_for_completion(&fsg->thread_notifier);
  3932. +
  3933. + /* The cleanup routine waits for this completion also */
  3934. + complete(&fsg->thread_notifier);
  3935. + }
  3936. +
  3937. + /* Unregister the sysfs attribute files and the LUNs */
  3938. + for (i = 0; i < fsg->nluns; ++i) {
  3939. + curlun = &fsg->luns[i];
  3940. + if (curlun->registered) {
  3941. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  3942. + device_remove_file(&curlun->dev, &dev_attr_ro);
  3943. + device_remove_file(&curlun->dev, &dev_attr_file);
  3944. + fsg_lun_close(curlun);
  3945. + device_unregister(&curlun->dev);
  3946. + curlun->registered = 0;
  3947. + }
  3948. + }
  3949. +
  3950. + /* Free the data buffers */
  3951. + for (i = 0; i < fsg_num_buffers; ++i)
  3952. + kfree(fsg->buffhds[i].buf);
  3953. +
  3954. + /* Free the request and buffer for endpoint 0 */
  3955. + if (req) {
  3956. + kfree(req->buf);
  3957. + usb_ep_free_request(fsg->ep0, req);
  3958. + }
  3959. +
  3960. + set_gadget_data(gadget, NULL);
  3961. +}
  3962. +
  3963. +
  3964. +static int __init check_parameters(struct fsg_dev *fsg)
  3965. +{
  3966. + int prot;
  3967. + int gcnum;
  3968. +
  3969. + /* Store the default values */
  3970. + mod_data.transport_type = USB_PR_BULK;
  3971. + mod_data.transport_name = "Bulk-only";
  3972. + mod_data.protocol_type = USB_SC_SCSI;
  3973. + mod_data.protocol_name = "Transparent SCSI";
  3974. +
  3975. + /* Some peripheral controllers are known not to be able to
  3976. + * halt bulk endpoints correctly. If one of them is present,
  3977. + * disable stalls.
  3978. + */
  3979. + if (gadget_is_at91(fsg->gadget))
  3980. + mod_data.can_stall = 0;
  3981. +
  3982. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  3983. + gcnum = usb_gadget_controller_number(fsg->gadget);
  3984. + if (gcnum >= 0)
  3985. + mod_data.release = 0x0300 + gcnum;
  3986. + else {
  3987. + WARNING(fsg, "controller '%s' not recognized\n",
  3988. + fsg->gadget->name);
  3989. + mod_data.release = 0x0399;
  3990. + }
  3991. + }
  3992. +
  3993. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  3994. +
  3995. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  3996. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  3997. + ; // Use default setting
  3998. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  3999. + mod_data.transport_type = USB_PR_CB;
  4000. + mod_data.transport_name = "Control-Bulk";
  4001. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  4002. + mod_data.transport_type = USB_PR_CBI;
  4003. + mod_data.transport_name = "Control-Bulk-Interrupt";
  4004. + } else {
  4005. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  4006. + return -EINVAL;
  4007. + }
  4008. +
  4009. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  4010. + prot == USB_SC_SCSI) {
  4011. + ; // Use default setting
  4012. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  4013. + prot == USB_SC_RBC) {
  4014. + mod_data.protocol_type = USB_SC_RBC;
  4015. + mod_data.protocol_name = "RBC";
  4016. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  4017. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  4018. + prot == USB_SC_8020) {
  4019. + mod_data.protocol_type = USB_SC_8020;
  4020. + mod_data.protocol_name = "8020i (ATAPI)";
  4021. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  4022. + prot == USB_SC_QIC) {
  4023. + mod_data.protocol_type = USB_SC_QIC;
  4024. + mod_data.protocol_name = "QIC-157";
  4025. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  4026. + prot == USB_SC_UFI) {
  4027. + mod_data.protocol_type = USB_SC_UFI;
  4028. + mod_data.protocol_name = "UFI";
  4029. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  4030. + prot == USB_SC_8070) {
  4031. + mod_data.protocol_type = USB_SC_8070;
  4032. + mod_data.protocol_name = "8070i";
  4033. + } else {
  4034. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  4035. + return -EINVAL;
  4036. + }
  4037. +
  4038. + mod_data.buflen &= PAGE_CACHE_MASK;
  4039. + if (mod_data.buflen <= 0) {
  4040. + ERROR(fsg, "invalid buflen\n");
  4041. + return -ETOOSMALL;
  4042. + }
  4043. +
  4044. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  4045. +
  4046. + /* Serial string handling.
  4047. + * On a real device, the serial string would be loaded
  4048. + * from permanent storage. */
  4049. + if (mod_data.serial) {
  4050. + const char *ch;
  4051. + unsigned len = 0;
  4052. +
  4053. + /* Sanity check :
  4054. + * The CB[I] specification limits the serial string to
  4055. + * 12 uppercase hexadecimal characters.
  4056. + * BBB need at least 12 uppercase hexadecimal characters,
  4057. + * with a maximum of 126. */
  4058. + for (ch = mod_data.serial; *ch; ++ch) {
  4059. + ++len;
  4060. + if ((*ch < '0' || *ch > '9') &&
  4061. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  4062. + WARNING(fsg,
  4063. + "Invalid serial string character: %c\n",
  4064. + *ch);
  4065. + goto no_serial;
  4066. + }
  4067. + }
  4068. + if (len > 126 ||
  4069. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  4070. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  4071. + WARNING(fsg, "Invalid serial string length!\n");
  4072. + goto no_serial;
  4073. + }
  4074. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  4075. + } else {
  4076. + WARNING(fsg, "No serial-number string provided!\n");
  4077. + no_serial:
  4078. + device_desc.iSerialNumber = 0;
  4079. + }
  4080. +
  4081. + return 0;
  4082. +}
  4083. +
  4084. +
  4085. +static int __init fsg_bind(struct usb_gadget *gadget)
  4086. +{
  4087. + struct fsg_dev *fsg = the_fsg;
  4088. + int rc;
  4089. + int i;
  4090. + struct fsg_lun *curlun;
  4091. + struct usb_ep *ep;
  4092. + struct usb_request *req;
  4093. + char *pathbuf, *p;
  4094. +
  4095. + fsg->gadget = gadget;
  4096. + set_gadget_data(gadget, fsg);
  4097. + fsg->ep0 = gadget->ep0;
  4098. + fsg->ep0->driver_data = fsg;
  4099. +
  4100. + if ((rc = check_parameters(fsg)) != 0)
  4101. + goto out;
  4102. +
  4103. + if (mod_data.removable) { // Enable the store_xxx attributes
  4104. + dev_attr_file.attr.mode = 0644;
  4105. + dev_attr_file.store = fsg_store_file;
  4106. + if (!mod_data.cdrom) {
  4107. + dev_attr_ro.attr.mode = 0644;
  4108. + dev_attr_ro.store = fsg_store_ro;
  4109. + }
  4110. + }
  4111. +
  4112. + /* Only for removable media? */
  4113. + dev_attr_nofua.attr.mode = 0644;
  4114. + dev_attr_nofua.store = fsg_store_nofua;
  4115. +
  4116. + /* Find out how many LUNs there should be */
  4117. + i = mod_data.nluns;
  4118. + if (i == 0)
  4119. + i = max(mod_data.num_filenames, 1u);
  4120. + if (i > FSG_MAX_LUNS) {
  4121. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  4122. + rc = -EINVAL;
  4123. + goto out;
  4124. + }
  4125. +
  4126. + /* Create the LUNs, open their backing files, and register the
  4127. + * LUN devices in sysfs. */
  4128. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  4129. + if (!fsg->luns) {
  4130. + rc = -ENOMEM;
  4131. + goto out;
  4132. + }
  4133. + fsg->nluns = i;
  4134. +
  4135. + for (i = 0; i < fsg->nluns; ++i) {
  4136. + curlun = &fsg->luns[i];
  4137. + curlun->cdrom = !!mod_data.cdrom;
  4138. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  4139. + curlun->initially_ro = curlun->ro;
  4140. + curlun->removable = mod_data.removable;
  4141. + curlun->nofua = mod_data.nofua[i];
  4142. + curlun->dev.release = lun_release;
  4143. + curlun->dev.parent = &gadget->dev;
  4144. + curlun->dev.driver = &fsg_driver.driver;
  4145. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  4146. + dev_set_name(&curlun->dev,"%s-lun%d",
  4147. + dev_name(&gadget->dev), i);
  4148. +
  4149. + kref_get(&fsg->ref);
  4150. + rc = device_register(&curlun->dev);
  4151. + if (rc) {
  4152. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  4153. + put_device(&curlun->dev);
  4154. + goto out;
  4155. + }
  4156. + curlun->registered = 1;
  4157. +
  4158. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  4159. + if (rc)
  4160. + goto out;
  4161. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  4162. + if (rc)
  4163. + goto out;
  4164. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  4165. + if (rc)
  4166. + goto out;
  4167. +
  4168. + if (mod_data.file[i] && *mod_data.file[i]) {
  4169. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  4170. + if (rc)
  4171. + goto out;
  4172. + } else if (!mod_data.removable) {
  4173. + ERROR(fsg, "no file given for LUN%d\n", i);
  4174. + rc = -EINVAL;
  4175. + goto out;
  4176. + }
  4177. + }
  4178. +
  4179. + /* Find all the endpoints we will use */
  4180. + usb_ep_autoconfig_reset(gadget);
  4181. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  4182. + if (!ep)
  4183. + goto autoconf_fail;
  4184. + ep->driver_data = fsg; // claim the endpoint
  4185. + fsg->bulk_in = ep;
  4186. +
  4187. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  4188. + if (!ep)
  4189. + goto autoconf_fail;
  4190. + ep->driver_data = fsg; // claim the endpoint
  4191. + fsg->bulk_out = ep;
  4192. +
  4193. + if (transport_is_cbi()) {
  4194. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  4195. + if (!ep)
  4196. + goto autoconf_fail;
  4197. + ep->driver_data = fsg; // claim the endpoint
  4198. + fsg->intr_in = ep;
  4199. + }
  4200. +
  4201. + /* Fix up the descriptors */
  4202. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  4203. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  4204. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  4205. +
  4206. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  4207. + fsg_intf_desc.bNumEndpoints = i;
  4208. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  4209. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  4210. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4211. +
  4212. + if (gadget_is_dualspeed(gadget)) {
  4213. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4214. +
  4215. + /* Assume endpoint addresses are the same for both speeds */
  4216. + fsg_hs_bulk_in_desc.bEndpointAddress =
  4217. + fsg_fs_bulk_in_desc.bEndpointAddress;
  4218. + fsg_hs_bulk_out_desc.bEndpointAddress =
  4219. + fsg_fs_bulk_out_desc.bEndpointAddress;
  4220. + fsg_hs_intr_in_desc.bEndpointAddress =
  4221. + fsg_fs_intr_in_desc.bEndpointAddress;
  4222. + }
  4223. +
  4224. + if (gadget_is_superspeed(gadget)) {
  4225. + unsigned max_burst;
  4226. +
  4227. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4228. +
  4229. + /* Calculate bMaxBurst, we know packet size is 1024 */
  4230. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  4231. +
  4232. + /* Assume endpoint addresses are the same for both speeds */
  4233. + fsg_ss_bulk_in_desc.bEndpointAddress =
  4234. + fsg_fs_bulk_in_desc.bEndpointAddress;
  4235. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  4236. +
  4237. + fsg_ss_bulk_out_desc.bEndpointAddress =
  4238. + fsg_fs_bulk_out_desc.bEndpointAddress;
  4239. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  4240. + }
  4241. +
  4242. + if (gadget_is_otg(gadget))
  4243. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  4244. +
  4245. + rc = -ENOMEM;
  4246. +
  4247. + /* Allocate the request and buffer for endpoint 0 */
  4248. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  4249. + if (!req)
  4250. + goto out;
  4251. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  4252. + if (!req->buf)
  4253. + goto out;
  4254. + req->complete = ep0_complete;
  4255. +
  4256. + /* Allocate the data buffers */
  4257. + for (i = 0; i < fsg_num_buffers; ++i) {
  4258. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  4259. +
  4260. + /* Allocate for the bulk-in endpoint. We assume that
  4261. + * the buffer will also work with the bulk-out (and
  4262. + * interrupt-in) endpoint. */
  4263. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  4264. + if (!bh->buf)
  4265. + goto out;
  4266. + bh->next = bh + 1;
  4267. + }
  4268. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  4269. +
  4270. + /* This should reflect the actual gadget power source */
  4271. + usb_gadget_set_selfpowered(gadget);
  4272. +
  4273. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  4274. + "%s %s with %s",
  4275. + init_utsname()->sysname, init_utsname()->release,
  4276. + gadget->name);
  4277. +
  4278. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  4279. + "file-storage-gadget");
  4280. + if (IS_ERR(fsg->thread_task)) {
  4281. + rc = PTR_ERR(fsg->thread_task);
  4282. + goto out;
  4283. + }
  4284. +
  4285. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  4286. + INFO(fsg, "NOTE: This driver is deprecated. "
  4287. + "Consider using g_mass_storage instead.\n");
  4288. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  4289. +
  4290. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  4291. + for (i = 0; i < fsg->nluns; ++i) {
  4292. + curlun = &fsg->luns[i];
  4293. + if (fsg_lun_is_open(curlun)) {
  4294. + p = NULL;
  4295. + if (pathbuf) {
  4296. + p = d_path(&curlun->filp->f_path,
  4297. + pathbuf, PATH_MAX);
  4298. + if (IS_ERR(p))
  4299. + p = NULL;
  4300. + }
  4301. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  4302. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  4303. + }
  4304. + }
  4305. + kfree(pathbuf);
  4306. +
  4307. + DBG(fsg, "transport=%s (x%02x)\n",
  4308. + mod_data.transport_name, mod_data.transport_type);
  4309. + DBG(fsg, "protocol=%s (x%02x)\n",
  4310. + mod_data.protocol_name, mod_data.protocol_type);
  4311. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  4312. + mod_data.vendor, mod_data.product, mod_data.release);
  4313. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  4314. + mod_data.removable, mod_data.can_stall,
  4315. + mod_data.cdrom, mod_data.buflen);
  4316. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  4317. +
  4318. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  4319. +
  4320. + /* Tell the thread to start working */
  4321. + wake_up_process(fsg->thread_task);
  4322. + return 0;
  4323. +
  4324. +autoconf_fail:
  4325. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  4326. + rc = -ENOTSUPP;
  4327. +
  4328. +out:
  4329. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  4330. + fsg_unbind(gadget);
  4331. + complete(&fsg->thread_notifier);
  4332. + return rc;
  4333. +}
  4334. +
  4335. +
  4336. +/*-------------------------------------------------------------------------*/
  4337. +
  4338. +static void fsg_suspend(struct usb_gadget *gadget)
  4339. +{
  4340. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4341. +
  4342. + DBG(fsg, "suspend\n");
  4343. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  4344. +}
  4345. +
  4346. +static void fsg_resume(struct usb_gadget *gadget)
  4347. +{
  4348. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4349. +
  4350. + DBG(fsg, "resume\n");
  4351. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  4352. +}
  4353. +
  4354. +
  4355. +/*-------------------------------------------------------------------------*/
  4356. +
  4357. +static struct usb_gadget_driver fsg_driver = {
  4358. + .max_speed = USB_SPEED_SUPER,
  4359. + .function = (char *) fsg_string_product,
  4360. + .unbind = fsg_unbind,
  4361. + .disconnect = fsg_disconnect,
  4362. + .setup = fsg_setup,
  4363. + .suspend = fsg_suspend,
  4364. + .resume = fsg_resume,
  4365. +
  4366. + .driver = {
  4367. + .name = DRIVER_NAME,
  4368. + .owner = THIS_MODULE,
  4369. + // .release = ...
  4370. + // .suspend = ...
  4371. + // .resume = ...
  4372. + },
  4373. +};
  4374. +
  4375. +
  4376. +static int __init fsg_alloc(void)
  4377. +{
  4378. + struct fsg_dev *fsg;
  4379. +
  4380. + fsg = kzalloc(sizeof *fsg +
  4381. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  4382. +
  4383. + if (!fsg)
  4384. + return -ENOMEM;
  4385. + spin_lock_init(&fsg->lock);
  4386. + init_rwsem(&fsg->filesem);
  4387. + kref_init(&fsg->ref);
  4388. + init_completion(&fsg->thread_notifier);
  4389. +
  4390. + the_fsg = fsg;
  4391. + return 0;
  4392. +}
  4393. +
  4394. +
  4395. +static int __init fsg_init(void)
  4396. +{
  4397. + int rc;
  4398. + struct fsg_dev *fsg;
  4399. +
  4400. + rc = fsg_num_buffers_validate();
  4401. + if (rc != 0)
  4402. + return rc;
  4403. +
  4404. + if ((rc = fsg_alloc()) != 0)
  4405. + return rc;
  4406. + fsg = the_fsg;
  4407. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  4408. + kref_put(&fsg->ref, fsg_release);
  4409. + return rc;
  4410. +}
  4411. +module_init(fsg_init);
  4412. +
  4413. +
  4414. +static void __exit fsg_cleanup(void)
  4415. +{
  4416. + struct fsg_dev *fsg = the_fsg;
  4417. +
  4418. + /* Unregister the driver iff the thread hasn't already done so */
  4419. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  4420. + usb_gadget_unregister_driver(&fsg_driver);
  4421. +
  4422. + /* Wait for the thread to finish up */
  4423. + wait_for_completion(&fsg->thread_notifier);
  4424. +
  4425. + kref_put(&fsg->ref, fsg_release);
  4426. +}
  4427. +module_exit(fsg_cleanup);
  4428. --- a/drivers/usb/host/Kconfig
  4429. +++ b/drivers/usb/host/Kconfig
  4430. @@ -735,6 +735,19 @@ config USB_HWA_HCD
  4431. To compile this driver a module, choose M here: the module
  4432. will be called "hwa-hc".
  4433. +config USB_DWCOTG
  4434. + tristate "Synopsis DWC host support"
  4435. + depends on USB
  4436. + help
  4437. + The Synopsis DWC controller is a dual-role
  4438. + host/peripheral/OTG ("On The Go") USB controllers.
  4439. +
  4440. + Enable this option to support this IP in host controller mode.
  4441. + If unsure, say N.
  4442. +
  4443. + To compile this driver as a module, choose M here: the
  4444. + modules built will be called dwc_otg and dwc_common_port.
  4445. +
  4446. config USB_IMX21_HCD
  4447. tristate "i.MX21 HCD support"
  4448. depends on ARM && ARCH_MXC
  4449. --- a/drivers/usb/host/Makefile
  4450. +++ b/drivers/usb/host/Makefile
  4451. @@ -69,6 +69,8 @@ obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
  4452. obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
  4453. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  4454. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  4455. +
  4456. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  4457. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  4458. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o
  4459. obj-$(CONFIG_USB_EHCI_FSL) += ehci-fsl.o
  4460. --- /dev/null
  4461. +++ b/drivers/usb/host/dwc_common_port/Makefile
  4462. @@ -0,0 +1,58 @@
  4463. +#
  4464. +# Makefile for DWC_common library
  4465. +#
  4466. +
  4467. +ifneq ($(KERNELRELEASE),)
  4468. +
  4469. +ccflags-y += -DDWC_LINUX
  4470. +#ccflags-y += -DDEBUG
  4471. +#ccflags-y += -DDWC_DEBUG_REGS
  4472. +#ccflags-y += -DDWC_DEBUG_MEMORY
  4473. +
  4474. +ccflags-y += -DDWC_LIBMODULE
  4475. +ccflags-y += -DDWC_CCLIB
  4476. +#ccflags-y += -DDWC_CRYPTOLIB
  4477. +ccflags-y += -DDWC_NOTIFYLIB
  4478. +ccflags-y += -DDWC_UTFLIB
  4479. +
  4480. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  4481. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  4482. + dwc_crypto.o dwc_notifier.o \
  4483. + dwc_common_linux.o dwc_mem.o
  4484. +
  4485. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  4486. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  4487. +
  4488. +ifneq ($(kernrel3),2.6.20)
  4489. +# grayg - I only know that we use ccflags-y in 2.6.31 actually
  4490. +ccflags-y += $(CPPFLAGS)
  4491. +endif
  4492. +
  4493. +else
  4494. +
  4495. +#ifeq ($(KDIR),)
  4496. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  4497. +#endif
  4498. +
  4499. +ifeq ($(ARCH),)
  4500. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  4501. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  4502. +endif
  4503. +
  4504. +ifeq ($(DOXYGEN),)
  4505. +DOXYGEN := doxygen
  4506. +endif
  4507. +
  4508. +default:
  4509. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  4510. +
  4511. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  4512. + $(DOXYGEN) doc/doxygen.cfg
  4513. +
  4514. +tags: $(wildcard *.[hc])
  4515. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  4516. +
  4517. +endif
  4518. +
  4519. +clean:
  4520. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  4521. --- /dev/null
  4522. +++ b/drivers/usb/host/dwc_common_port/Makefile.fbsd
  4523. @@ -0,0 +1,17 @@
  4524. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  4525. +CFLAGS += -DDWC_FREEBSD
  4526. +CFLAGS += -DDEBUG
  4527. +#CFLAGS += -DDWC_DEBUG_REGS
  4528. +#CFLAGS += -DDWC_DEBUG_MEMORY
  4529. +
  4530. +#CFLAGS += -DDWC_LIBMODULE
  4531. +#CFLAGS += -DDWC_CCLIB
  4532. +#CFLAGS += -DDWC_CRYPTOLIB
  4533. +#CFLAGS += -DDWC_NOTIFYLIB
  4534. +#CFLAGS += -DDWC_UTFLIB
  4535. +
  4536. +KMOD = dwc_common_port_lib
  4537. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  4538. + dwc_common_fbsd.c dwc_mem.c
  4539. +
  4540. +.include <bsd.kmod.mk>
  4541. --- /dev/null
  4542. +++ b/drivers/usb/host/dwc_common_port/Makefile.linux
  4543. @@ -0,0 +1,49 @@
  4544. +#
  4545. +# Makefile for DWC_common library
  4546. +#
  4547. +ifneq ($(KERNELRELEASE),)
  4548. +
  4549. +ccflags-y += -DDWC_LINUX
  4550. +#ccflags-y += -DDEBUG
  4551. +#ccflags-y += -DDWC_DEBUG_REGS
  4552. +#ccflags-y += -DDWC_DEBUG_MEMORY
  4553. +
  4554. +ccflags-y += -DDWC_LIBMODULE
  4555. +ccflags-y += -DDWC_CCLIB
  4556. +ccflags-y += -DDWC_CRYPTOLIB
  4557. +ccflags-y += -DDWC_NOTIFYLIB
  4558. +ccflags-y += -DDWC_UTFLIB
  4559. +
  4560. +obj-m := dwc_common_port_lib.o
  4561. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  4562. + dwc_crypto.o dwc_notifier.o \
  4563. + dwc_common_linux.o dwc_mem.o
  4564. +
  4565. +else
  4566. +
  4567. +ifeq ($(KDIR),)
  4568. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  4569. +endif
  4570. +
  4571. +ifeq ($(ARCH),)
  4572. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  4573. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  4574. +endif
  4575. +
  4576. +ifeq ($(DOXYGEN),)
  4577. +DOXYGEN := doxygen
  4578. +endif
  4579. +
  4580. +default:
  4581. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  4582. +
  4583. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  4584. + $(DOXYGEN) doc/doxygen.cfg
  4585. +
  4586. +tags: $(wildcard *.[hc])
  4587. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  4588. +
  4589. +endif
  4590. +
  4591. +clean:
  4592. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  4593. --- /dev/null
  4594. +++ b/drivers/usb/host/dwc_common_port/changes.txt
  4595. @@ -0,0 +1,174 @@
  4596. +
  4597. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  4598. +IO context struct. The IO context struct should live in an os-dependent struct
  4599. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  4600. +named 'os_dep' embedded in the main device struct. So there these calls look
  4601. +like this:
  4602. +
  4603. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  4604. +
  4605. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  4606. + &pcd->dev_global_regs->dcfg, 0);
  4607. +
  4608. +Note that for the existing Linux driver ports, it is not necessary to actually
  4609. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  4610. +require an IO context, its macros for dwc_read_reg32() and friends do not
  4611. +use the context pointer, so it is optimized away by the compiler. But it is
  4612. +necessary to add the pointer parameter to all of the call sites, to be ready
  4613. +for any future ports (such as FreeBSD) which do require an IO context.
  4614. +
  4615. +
  4616. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  4617. +take an additional parameter, a pointer to a memory context. Examples:
  4618. +
  4619. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  4620. +
  4621. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  4622. +
  4623. +Again, for the Linux ports, it is not necessary to actually define the memctx
  4624. +member, but it is necessary to add the pointer parameter to all of the call
  4625. +sites.
  4626. +
  4627. +
  4628. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  4629. +
  4630. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  4631. +
  4632. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  4633. +
  4634. +
  4635. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  4636. +
  4637. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  4638. +
  4639. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  4640. +
  4641. +
  4642. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  4643. +
  4644. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  4645. +
  4646. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  4647. +
  4648. +
  4649. +Same for dwc_timer_alloc(). Example:
  4650. +
  4651. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  4652. + cb_func, cb_data);
  4653. +
  4654. +
  4655. +Same for dwc_waitq_alloc(). Example:
  4656. +
  4657. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  4658. +
  4659. +
  4660. +Same for dwc_thread_run(). Example:
  4661. +
  4662. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  4663. + "dwc_usb3_thd1", data);
  4664. +
  4665. +
  4666. +Same for dwc_workq_alloc(). Example:
  4667. +
  4668. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  4669. +
  4670. +
  4671. +Same for dwc_task_alloc(). Example:
  4672. +
  4673. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  4674. + cb_func, cb_data);
  4675. +
  4676. +
  4677. +In addition to the context pointer additions, a few core functions have had
  4678. +other changes made to their parameters:
  4679. +
  4680. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  4681. +has been changed from a uint64_t to a dwc_irqflags_t.
  4682. +
  4683. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  4684. +FreeBSD equivalent of that function requires it.
  4685. +
  4686. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  4687. +'char *name' parameter, to be consistent with dwc_thread_run() and
  4688. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  4689. +requires a unique name.
  4690. +
  4691. +
  4692. +Here is a complete list of the core functions that now take a pointer to a
  4693. +context as their first parameter:
  4694. +
  4695. + dwc_read_reg32
  4696. + dwc_read_reg64
  4697. + dwc_write_reg32
  4698. + dwc_write_reg64
  4699. + dwc_modify_reg32
  4700. + dwc_modify_reg64
  4701. + dwc_alloc
  4702. + dwc_alloc_atomic
  4703. + dwc_strdup
  4704. + dwc_free
  4705. + dwc_dma_alloc
  4706. + dwc_dma_free
  4707. + dwc_mutex_alloc
  4708. + dwc_mutex_free
  4709. + dwc_spinlock_alloc
  4710. + dwc_spinlock_free
  4711. + dwc_timer_alloc
  4712. + dwc_waitq_alloc
  4713. + dwc_thread_run
  4714. + dwc_workq_alloc
  4715. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  4716. +
  4717. +And here are the core functions that have other changes to their parameters:
  4718. +
  4719. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  4720. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  4721. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  4722. +
  4723. +
  4724. +
  4725. +The changes to the core functions also require some of the other library
  4726. +functions to change:
  4727. +
  4728. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  4729. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  4730. + (for mutex allocation) as the 2nd param.
  4731. +
  4732. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  4733. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  4734. + 'void *memctx' as the 1st param.
  4735. +
  4736. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  4737. + 'void *memctx' as the 1st param.
  4738. +
  4739. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  4740. +
  4741. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  4742. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  4743. + param, and also now returns an integer value that is non-zero if
  4744. + allocation of its data structures or work queue fails.
  4745. +
  4746. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  4747. +
  4748. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  4749. + param, and also now returns an integer value that is non-zero if
  4750. + allocation of its data structures fails.
  4751. +
  4752. +
  4753. +
  4754. +Other miscellaneous changes:
  4755. +
  4756. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  4757. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  4758. +
  4759. +The following #define's have been added to allow selectively compiling library
  4760. +features:
  4761. +
  4762. + DWC_CCLIB
  4763. + DWC_CRYPTOLIB
  4764. + DWC_NOTIFYLIB
  4765. + DWC_UTFLIB
  4766. +
  4767. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  4768. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  4769. +library code directly into a driver module, instead of as a standalone module.
  4770. --- /dev/null
  4771. +++ b/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  4772. @@ -0,0 +1,270 @@
  4773. +# Doxyfile 1.4.5
  4774. +
  4775. +#---------------------------------------------------------------------------
  4776. +# Project related configuration options
  4777. +#---------------------------------------------------------------------------
  4778. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  4779. +PROJECT_NUMBER =
  4780. +OUTPUT_DIRECTORY = doc
  4781. +CREATE_SUBDIRS = NO
  4782. +OUTPUT_LANGUAGE = English
  4783. +BRIEF_MEMBER_DESC = YES
  4784. +REPEAT_BRIEF = YES
  4785. +ABBREVIATE_BRIEF = "The $name class" \
  4786. + "The $name widget" \
  4787. + "The $name file" \
  4788. + is \
  4789. + provides \
  4790. + specifies \
  4791. + contains \
  4792. + represents \
  4793. + a \
  4794. + an \
  4795. + the
  4796. +ALWAYS_DETAILED_SEC = YES
  4797. +INLINE_INHERITED_MEMB = NO
  4798. +FULL_PATH_NAMES = NO
  4799. +STRIP_FROM_PATH = ..
  4800. +STRIP_FROM_INC_PATH =
  4801. +SHORT_NAMES = NO
  4802. +JAVADOC_AUTOBRIEF = YES
  4803. +MULTILINE_CPP_IS_BRIEF = NO
  4804. +DETAILS_AT_TOP = YES
  4805. +INHERIT_DOCS = YES
  4806. +SEPARATE_MEMBER_PAGES = NO
  4807. +TAB_SIZE = 8
  4808. +ALIASES =
  4809. +OPTIMIZE_OUTPUT_FOR_C = YES
  4810. +OPTIMIZE_OUTPUT_JAVA = NO
  4811. +BUILTIN_STL_SUPPORT = NO
  4812. +DISTRIBUTE_GROUP_DOC = NO
  4813. +SUBGROUPING = NO
  4814. +#---------------------------------------------------------------------------
  4815. +# Build related configuration options
  4816. +#---------------------------------------------------------------------------
  4817. +EXTRACT_ALL = NO
  4818. +EXTRACT_PRIVATE = NO
  4819. +EXTRACT_STATIC = YES
  4820. +EXTRACT_LOCAL_CLASSES = NO
  4821. +EXTRACT_LOCAL_METHODS = NO
  4822. +HIDE_UNDOC_MEMBERS = NO
  4823. +HIDE_UNDOC_CLASSES = NO
  4824. +HIDE_FRIEND_COMPOUNDS = NO
  4825. +HIDE_IN_BODY_DOCS = NO
  4826. +INTERNAL_DOCS = NO
  4827. +CASE_SENSE_NAMES = YES
  4828. +HIDE_SCOPE_NAMES = NO
  4829. +SHOW_INCLUDE_FILES = NO
  4830. +INLINE_INFO = YES
  4831. +SORT_MEMBER_DOCS = NO
  4832. +SORT_BRIEF_DOCS = NO
  4833. +SORT_BY_SCOPE_NAME = NO
  4834. +GENERATE_TODOLIST = YES
  4835. +GENERATE_TESTLIST = YES
  4836. +GENERATE_BUGLIST = YES
  4837. +GENERATE_DEPRECATEDLIST= YES
  4838. +ENABLED_SECTIONS =
  4839. +MAX_INITIALIZER_LINES = 30
  4840. +SHOW_USED_FILES = YES
  4841. +SHOW_DIRECTORIES = YES
  4842. +FILE_VERSION_FILTER =
  4843. +#---------------------------------------------------------------------------
  4844. +# configuration options related to warning and progress messages
  4845. +#---------------------------------------------------------------------------
  4846. +QUIET = YES
  4847. +WARNINGS = YES
  4848. +WARN_IF_UNDOCUMENTED = NO
  4849. +WARN_IF_DOC_ERROR = YES
  4850. +WARN_NO_PARAMDOC = YES
  4851. +WARN_FORMAT = "$file:$line: $text"
  4852. +WARN_LOGFILE =
  4853. +#---------------------------------------------------------------------------
  4854. +# configuration options related to the input files
  4855. +#---------------------------------------------------------------------------
  4856. +INPUT = .
  4857. +FILE_PATTERNS = *.c \
  4858. + *.cc \
  4859. + *.cxx \
  4860. + *.cpp \
  4861. + *.c++ \
  4862. + *.d \
  4863. + *.java \
  4864. + *.ii \
  4865. + *.ixx \
  4866. + *.ipp \
  4867. + *.i++ \
  4868. + *.inl \
  4869. + *.h \
  4870. + *.hh \
  4871. + *.hxx \
  4872. + *.hpp \
  4873. + *.h++ \
  4874. + *.idl \
  4875. + *.odl \
  4876. + *.cs \
  4877. + *.php \
  4878. + *.php3 \
  4879. + *.inc \
  4880. + *.m \
  4881. + *.mm \
  4882. + *.dox \
  4883. + *.py \
  4884. + *.C \
  4885. + *.CC \
  4886. + *.C++ \
  4887. + *.II \
  4888. + *.I++ \
  4889. + *.H \
  4890. + *.HH \
  4891. + *.H++ \
  4892. + *.CS \
  4893. + *.PHP \
  4894. + *.PHP3 \
  4895. + *.M \
  4896. + *.MM \
  4897. + *.PY
  4898. +RECURSIVE = NO
  4899. +EXCLUDE =
  4900. +EXCLUDE_SYMLINKS = NO
  4901. +EXCLUDE_PATTERNS =
  4902. +EXAMPLE_PATH =
  4903. +EXAMPLE_PATTERNS = *
  4904. +EXAMPLE_RECURSIVE = NO
  4905. +IMAGE_PATH =
  4906. +INPUT_FILTER =
  4907. +FILTER_PATTERNS =
  4908. +FILTER_SOURCE_FILES = NO
  4909. +#---------------------------------------------------------------------------
  4910. +# configuration options related to source browsing
  4911. +#---------------------------------------------------------------------------
  4912. +SOURCE_BROWSER = NO
  4913. +INLINE_SOURCES = NO
  4914. +STRIP_CODE_COMMENTS = YES
  4915. +REFERENCED_BY_RELATION = YES
  4916. +REFERENCES_RELATION = YES
  4917. +USE_HTAGS = NO
  4918. +VERBATIM_HEADERS = NO
  4919. +#---------------------------------------------------------------------------
  4920. +# configuration options related to the alphabetical class index
  4921. +#---------------------------------------------------------------------------
  4922. +ALPHABETICAL_INDEX = NO
  4923. +COLS_IN_ALPHA_INDEX = 5
  4924. +IGNORE_PREFIX =
  4925. +#---------------------------------------------------------------------------
  4926. +# configuration options related to the HTML output
  4927. +#---------------------------------------------------------------------------
  4928. +GENERATE_HTML = YES
  4929. +HTML_OUTPUT = html
  4930. +HTML_FILE_EXTENSION = .html
  4931. +HTML_HEADER =
  4932. +HTML_FOOTER =
  4933. +HTML_STYLESHEET =
  4934. +HTML_ALIGN_MEMBERS = YES
  4935. +GENERATE_HTMLHELP = NO
  4936. +CHM_FILE =
  4937. +HHC_LOCATION =
  4938. +GENERATE_CHI = NO
  4939. +BINARY_TOC = NO
  4940. +TOC_EXPAND = NO
  4941. +DISABLE_INDEX = NO
  4942. +ENUM_VALUES_PER_LINE = 4
  4943. +GENERATE_TREEVIEW = YES
  4944. +TREEVIEW_WIDTH = 250
  4945. +#---------------------------------------------------------------------------
  4946. +# configuration options related to the LaTeX output
  4947. +#---------------------------------------------------------------------------
  4948. +GENERATE_LATEX = NO
  4949. +LATEX_OUTPUT = latex
  4950. +LATEX_CMD_NAME = latex
  4951. +MAKEINDEX_CMD_NAME = makeindex
  4952. +COMPACT_LATEX = NO
  4953. +PAPER_TYPE = a4wide
  4954. +EXTRA_PACKAGES =
  4955. +LATEX_HEADER =
  4956. +PDF_HYPERLINKS = NO
  4957. +USE_PDFLATEX = NO
  4958. +LATEX_BATCHMODE = NO
  4959. +LATEX_HIDE_INDICES = NO
  4960. +#---------------------------------------------------------------------------
  4961. +# configuration options related to the RTF output
  4962. +#---------------------------------------------------------------------------
  4963. +GENERATE_RTF = NO
  4964. +RTF_OUTPUT = rtf
  4965. +COMPACT_RTF = NO
  4966. +RTF_HYPERLINKS = NO
  4967. +RTF_STYLESHEET_FILE =
  4968. +RTF_EXTENSIONS_FILE =
  4969. +#---------------------------------------------------------------------------
  4970. +# configuration options related to the man page output
  4971. +#---------------------------------------------------------------------------
  4972. +GENERATE_MAN = NO
  4973. +MAN_OUTPUT = man
  4974. +MAN_EXTENSION = .3
  4975. +MAN_LINKS = NO
  4976. +#---------------------------------------------------------------------------
  4977. +# configuration options related to the XML output
  4978. +#---------------------------------------------------------------------------
  4979. +GENERATE_XML = NO
  4980. +XML_OUTPUT = xml
  4981. +XML_SCHEMA =
  4982. +XML_DTD =
  4983. +XML_PROGRAMLISTING = YES
  4984. +#---------------------------------------------------------------------------
  4985. +# configuration options for the AutoGen Definitions output
  4986. +#---------------------------------------------------------------------------
  4987. +GENERATE_AUTOGEN_DEF = NO
  4988. +#---------------------------------------------------------------------------
  4989. +# configuration options related to the Perl module output
  4990. +#---------------------------------------------------------------------------
  4991. +GENERATE_PERLMOD = NO
  4992. +PERLMOD_LATEX = NO
  4993. +PERLMOD_PRETTY = YES
  4994. +PERLMOD_MAKEVAR_PREFIX =
  4995. +#---------------------------------------------------------------------------
  4996. +# Configuration options related to the preprocessor
  4997. +#---------------------------------------------------------------------------
  4998. +ENABLE_PREPROCESSING = YES
  4999. +MACRO_EXPANSION = NO
  5000. +EXPAND_ONLY_PREDEF = NO
  5001. +SEARCH_INCLUDES = YES
  5002. +INCLUDE_PATH =
  5003. +INCLUDE_FILE_PATTERNS =
  5004. +PREDEFINED = DEBUG DEBUG_MEMORY
  5005. +EXPAND_AS_DEFINED =
  5006. +SKIP_FUNCTION_MACROS = YES
  5007. +#---------------------------------------------------------------------------
  5008. +# Configuration::additions related to external references
  5009. +#---------------------------------------------------------------------------
  5010. +TAGFILES =
  5011. +GENERATE_TAGFILE =
  5012. +ALLEXTERNALS = NO
  5013. +EXTERNAL_GROUPS = YES
  5014. +PERL_PATH = /usr/bin/perl
  5015. +#---------------------------------------------------------------------------
  5016. +# Configuration options related to the dot tool
  5017. +#---------------------------------------------------------------------------
  5018. +CLASS_DIAGRAMS = YES
  5019. +HIDE_UNDOC_RELATIONS = YES
  5020. +HAVE_DOT = NO
  5021. +CLASS_GRAPH = YES
  5022. +COLLABORATION_GRAPH = YES
  5023. +GROUP_GRAPHS = YES
  5024. +UML_LOOK = NO
  5025. +TEMPLATE_RELATIONS = NO
  5026. +INCLUDE_GRAPH = NO
  5027. +INCLUDED_BY_GRAPH = YES
  5028. +CALL_GRAPH = NO
  5029. +GRAPHICAL_HIERARCHY = YES
  5030. +DIRECTORY_GRAPH = YES
  5031. +DOT_IMAGE_FORMAT = png
  5032. +DOT_PATH =
  5033. +DOTFILE_DIRS =
  5034. +MAX_DOT_GRAPH_DEPTH = 1000
  5035. +DOT_TRANSPARENT = NO
  5036. +DOT_MULTI_TARGETS = NO
  5037. +GENERATE_LEGEND = YES
  5038. +DOT_CLEANUP = YES
  5039. +#---------------------------------------------------------------------------
  5040. +# Configuration::additions related to the search engine
  5041. +#---------------------------------------------------------------------------
  5042. +SEARCHENGINE = NO
  5043. --- /dev/null
  5044. +++ b/drivers/usb/host/dwc_common_port/dwc_cc.c
  5045. @@ -0,0 +1,532 @@
  5046. +/* =========================================================================
  5047. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  5048. + * $Revision: #4 $
  5049. + * $Date: 2010/11/04 $
  5050. + * $Change: 1621692 $
  5051. + *
  5052. + * Synopsys Portability Library Software and documentation
  5053. + * (hereinafter, "Software") is an Unsupported proprietary work of
  5054. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  5055. + * between Synopsys and you.
  5056. + *
  5057. + * The Software IS NOT an item of Licensed Software or Licensed Product
  5058. + * under any End User Software License Agreement or Agreement for
  5059. + * Licensed Product with Synopsys or any supplement thereto. You are
  5060. + * permitted to use and redistribute this Software in source and binary
  5061. + * forms, with or without modification, provided that redistributions
  5062. + * of source code must retain this notice. You may not view, use,
  5063. + * disclose, copy or distribute this file or any information contained
  5064. + * herein except pursuant to this license grant from Synopsys. If you
  5065. + * do not agree with this notice, including the disclaimer below, then
  5066. + * you are not authorized to use the Software.
  5067. + *
  5068. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  5069. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5070. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  5071. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  5072. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5073. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5074. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5075. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  5076. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5077. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  5078. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  5079. + * DAMAGE.
  5080. + * ========================================================================= */
  5081. +#ifdef DWC_CCLIB
  5082. +
  5083. +#include "dwc_cc.h"
  5084. +
  5085. +typedef struct dwc_cc
  5086. +{
  5087. + uint32_t uid;
  5088. + uint8_t chid[16];
  5089. + uint8_t cdid[16];
  5090. + uint8_t ck[16];
  5091. + uint8_t *name;
  5092. + uint8_t length;
  5093. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  5094. +} dwc_cc_t;
  5095. +
  5096. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  5097. +
  5098. +/** The main structure for CC management. */
  5099. +struct dwc_cc_if
  5100. +{
  5101. + dwc_mutex_t *mutex;
  5102. + char *filename;
  5103. +
  5104. + unsigned is_host:1;
  5105. +
  5106. + dwc_notifier_t *notifier;
  5107. +
  5108. + struct context_list list;
  5109. +};
  5110. +
  5111. +#ifdef DEBUG
  5112. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  5113. +{
  5114. + int i;
  5115. + DWC_PRINTF("%s: ", name);
  5116. + for (i=0; i<len; i++) {
  5117. + DWC_PRINTF("%02x ", bytes[i]);
  5118. + }
  5119. + DWC_PRINTF("\n");
  5120. +}
  5121. +#else
  5122. +#define dump_bytes(x...)
  5123. +#endif
  5124. +
  5125. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  5126. +{
  5127. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  5128. + if (!cc) {
  5129. + return NULL;
  5130. + }
  5131. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  5132. +
  5133. + if (name) {
  5134. + cc->length = length;
  5135. + cc->name = dwc_alloc(mem_ctx, length);
  5136. + if (!cc->name) {
  5137. + dwc_free(mem_ctx, cc);
  5138. + return NULL;
  5139. + }
  5140. +
  5141. + DWC_MEMCPY(cc->name, name, length);
  5142. + }
  5143. +
  5144. + return cc;
  5145. +}
  5146. +
  5147. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  5148. +{
  5149. + if (cc->name) {
  5150. + dwc_free(mem_ctx, cc->name);
  5151. + }
  5152. + dwc_free(mem_ctx, cc);
  5153. +}
  5154. +
  5155. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  5156. +{
  5157. + uint32_t uid = 0;
  5158. + dwc_cc_t *cc;
  5159. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5160. + if (cc->uid > uid) {
  5161. + uid = cc->uid;
  5162. + }
  5163. + }
  5164. +
  5165. + if (uid == 0) {
  5166. + uid = 255;
  5167. + }
  5168. +
  5169. + return uid + 1;
  5170. +}
  5171. +
  5172. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  5173. +{
  5174. + dwc_cc_t *cc;
  5175. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5176. + if (cc->uid == uid) {
  5177. + return cc;
  5178. + }
  5179. + }
  5180. + return NULL;
  5181. +}
  5182. +
  5183. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  5184. +{
  5185. + unsigned int size = 0;
  5186. + dwc_cc_t *cc;
  5187. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5188. + size += (48 + 1);
  5189. + if (cc->name) {
  5190. + size += cc->length;
  5191. + }
  5192. + }
  5193. + return size;
  5194. +}
  5195. +
  5196. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  5197. +{
  5198. + uint32_t uid = 0;
  5199. + dwc_cc_t *cc;
  5200. +
  5201. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5202. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  5203. + uid = cc->uid;
  5204. + break;
  5205. + }
  5206. + }
  5207. + return uid;
  5208. +}
  5209. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  5210. +{
  5211. + uint32_t uid = 0;
  5212. + dwc_cc_t *cc;
  5213. +
  5214. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5215. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  5216. + uid = cc->uid;
  5217. + break;
  5218. + }
  5219. + }
  5220. + return uid;
  5221. +}
  5222. +
  5223. +/* Internal cc_add */
  5224. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5225. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5226. +{
  5227. + dwc_cc_t *cc;
  5228. + uint32_t uid;
  5229. +
  5230. + if (cc_if->is_host) {
  5231. + uid = cc_match_cdid(cc_if, cdid);
  5232. + }
  5233. + else {
  5234. + uid = cc_match_chid(cc_if, chid);
  5235. + }
  5236. +
  5237. + if (uid) {
  5238. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  5239. + cc = cc_find(cc_if, uid);
  5240. + }
  5241. + else {
  5242. + cc = alloc_cc(mem_ctx, name, length);
  5243. + cc->uid = next_uid(cc_if);
  5244. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  5245. + }
  5246. +
  5247. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  5248. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  5249. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  5250. +
  5251. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  5252. + dump_bytes("CHID", cc->chid, 16);
  5253. + dump_bytes("CDID", cc->cdid, 16);
  5254. + dump_bytes("CK", cc->ck, 16);
  5255. + return cc->uid;
  5256. +}
  5257. +
  5258. +/* Internal cc_clear */
  5259. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  5260. +{
  5261. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  5262. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  5263. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  5264. + free_cc(mem_ctx, cc);
  5265. + }
  5266. +}
  5267. +
  5268. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  5269. + dwc_notifier_t *notifier, unsigned is_host)
  5270. +{
  5271. + dwc_cc_if_t *cc_if = NULL;
  5272. +
  5273. + /* Allocate a common_cc_if structure */
  5274. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  5275. +
  5276. + if (!cc_if)
  5277. + return NULL;
  5278. +
  5279. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  5280. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  5281. +#else
  5282. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  5283. +#endif
  5284. + if (!cc_if->mutex) {
  5285. + dwc_free(mem_ctx, cc_if);
  5286. + return NULL;
  5287. + }
  5288. +
  5289. + DWC_CIRCLEQ_INIT(&cc_if->list);
  5290. + cc_if->is_host = is_host;
  5291. + cc_if->notifier = notifier;
  5292. + return cc_if;
  5293. +}
  5294. +
  5295. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  5296. +{
  5297. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  5298. + DWC_MUTEX_FREE(cc_if->mutex);
  5299. +#else
  5300. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  5301. +#endif
  5302. + cc_clear(mem_ctx, cc_if);
  5303. + dwc_free(mem_ctx, cc_if);
  5304. +}
  5305. +
  5306. +static void cc_changed(dwc_cc_if_t *cc_if)
  5307. +{
  5308. + if (cc_if->notifier) {
  5309. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  5310. + }
  5311. +}
  5312. +
  5313. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  5314. +{
  5315. + DWC_MUTEX_LOCK(cc_if->mutex);
  5316. + cc_clear(mem_ctx, cc_if);
  5317. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5318. + cc_changed(cc_if);
  5319. +}
  5320. +
  5321. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5322. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5323. +{
  5324. + uint32_t uid;
  5325. +
  5326. + DWC_MUTEX_LOCK(cc_if->mutex);
  5327. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  5328. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5329. + cc_changed(cc_if);
  5330. +
  5331. + return uid;
  5332. +}
  5333. +
  5334. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  5335. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5336. +{
  5337. + dwc_cc_t* cc;
  5338. +
  5339. + DWC_DEBUGC("Change connection context %d", id);
  5340. +
  5341. + DWC_MUTEX_LOCK(cc_if->mutex);
  5342. + cc = cc_find(cc_if, id);
  5343. + if (!cc) {
  5344. + DWC_ERROR("Uid %d not found in cc list\n", id);
  5345. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5346. + return;
  5347. + }
  5348. +
  5349. + if (chid) {
  5350. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  5351. + }
  5352. + if (cdid) {
  5353. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  5354. + }
  5355. + if (ck) {
  5356. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  5357. + }
  5358. +
  5359. + if (name) {
  5360. + if (cc->name) {
  5361. + dwc_free(mem_ctx, cc->name);
  5362. + }
  5363. + cc->name = dwc_alloc(mem_ctx, length);
  5364. + if (!cc->name) {
  5365. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  5366. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5367. + return;
  5368. + }
  5369. + cc->length = length;
  5370. + DWC_MEMCPY(cc->name, name, length);
  5371. + }
  5372. +
  5373. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5374. +
  5375. + cc_changed(cc_if);
  5376. +
  5377. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  5378. + dump_bytes("New CHID", cc->chid, 16);
  5379. + dump_bytes("New CDID", cc->cdid, 16);
  5380. + dump_bytes("New CK", cc->ck, 16);
  5381. +}
  5382. +
  5383. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  5384. +{
  5385. + dwc_cc_t *cc;
  5386. +
  5387. + DWC_DEBUGC("Removing connection context %d", id);
  5388. +
  5389. + DWC_MUTEX_LOCK(cc_if->mutex);
  5390. + cc = cc_find(cc_if, id);
  5391. + if (!cc) {
  5392. + DWC_ERROR("Uid %d not found in cc list\n", id);
  5393. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5394. + return;
  5395. + }
  5396. +
  5397. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  5398. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5399. + free_cc(mem_ctx, cc);
  5400. +
  5401. + cc_changed(cc_if);
  5402. +}
  5403. +
  5404. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  5405. +{
  5406. + uint8_t *buf, *x;
  5407. + uint8_t zero = 0;
  5408. + dwc_cc_t *cc;
  5409. +
  5410. + DWC_MUTEX_LOCK(cc_if->mutex);
  5411. + *length = cc_data_size(cc_if);
  5412. + if (!(*length)) {
  5413. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5414. + return NULL;
  5415. + }
  5416. +
  5417. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  5418. +
  5419. + buf = dwc_alloc(mem_ctx, *length);
  5420. + if (!buf) {
  5421. + *length = 0;
  5422. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5423. + return NULL;
  5424. + }
  5425. +
  5426. + x = buf;
  5427. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5428. + DWC_MEMCPY(x, cc->chid, 16);
  5429. + x += 16;
  5430. + DWC_MEMCPY(x, cc->cdid, 16);
  5431. + x += 16;
  5432. + DWC_MEMCPY(x, cc->ck, 16);
  5433. + x += 16;
  5434. + if (cc->name) {
  5435. + DWC_MEMCPY(x, &cc->length, 1);
  5436. + x += 1;
  5437. + DWC_MEMCPY(x, cc->name, cc->length);
  5438. + x += cc->length;
  5439. + }
  5440. + else {
  5441. + DWC_MEMCPY(x, &zero, 1);
  5442. + x += 1;
  5443. + }
  5444. + }
  5445. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5446. +
  5447. + return buf;
  5448. +}
  5449. +
  5450. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  5451. +{
  5452. + uint8_t name_length;
  5453. + uint8_t *name;
  5454. + uint8_t *chid;
  5455. + uint8_t *cdid;
  5456. + uint8_t *ck;
  5457. + uint32_t i = 0;
  5458. +
  5459. + DWC_MUTEX_LOCK(cc_if->mutex);
  5460. + cc_clear(mem_ctx, cc_if);
  5461. +
  5462. + while (i < length) {
  5463. + chid = &data[i];
  5464. + i += 16;
  5465. + cdid = &data[i];
  5466. + i += 16;
  5467. + ck = &data[i];
  5468. + i += 16;
  5469. +
  5470. + name_length = data[i];
  5471. + i ++;
  5472. +
  5473. + if (name_length) {
  5474. + name = &data[i];
  5475. + i += name_length;
  5476. + }
  5477. + else {
  5478. + name = NULL;
  5479. + }
  5480. +
  5481. + /* check to see if we haven't overflown the buffer */
  5482. + if (i > length) {
  5483. + DWC_ERROR("Data format error while attempting to load CCs "
  5484. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  5485. + break;
  5486. + }
  5487. +
  5488. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  5489. + }
  5490. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5491. +
  5492. + cc_changed(cc_if);
  5493. +}
  5494. +
  5495. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  5496. +{
  5497. + uint32_t uid = 0;
  5498. +
  5499. + DWC_MUTEX_LOCK(cc_if->mutex);
  5500. + uid = cc_match_chid(cc_if, chid);
  5501. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5502. + return uid;
  5503. +}
  5504. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  5505. +{
  5506. + uint32_t uid = 0;
  5507. +
  5508. + DWC_MUTEX_LOCK(cc_if->mutex);
  5509. + uid = cc_match_cdid(cc_if, cdid);
  5510. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5511. + return uid;
  5512. +}
  5513. +
  5514. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  5515. +{
  5516. + uint8_t *ck = NULL;
  5517. + dwc_cc_t *cc;
  5518. +
  5519. + DWC_MUTEX_LOCK(cc_if->mutex);
  5520. + cc = cc_find(cc_if, id);
  5521. + if (cc) {
  5522. + ck = cc->ck;
  5523. + }
  5524. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5525. +
  5526. + return ck;
  5527. +
  5528. +}
  5529. +
  5530. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  5531. +{
  5532. + uint8_t *retval = NULL;
  5533. + dwc_cc_t *cc;
  5534. +
  5535. + DWC_MUTEX_LOCK(cc_if->mutex);
  5536. + cc = cc_find(cc_if, id);
  5537. + if (cc) {
  5538. + retval = cc->chid;
  5539. + }
  5540. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5541. +
  5542. + return retval;
  5543. +}
  5544. +
  5545. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  5546. +{
  5547. + uint8_t *retval = NULL;
  5548. + dwc_cc_t *cc;
  5549. +
  5550. + DWC_MUTEX_LOCK(cc_if->mutex);
  5551. + cc = cc_find(cc_if, id);
  5552. + if (cc) {
  5553. + retval = cc->cdid;
  5554. + }
  5555. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5556. +
  5557. + return retval;
  5558. +}
  5559. +
  5560. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  5561. +{
  5562. + uint8_t *retval = NULL;
  5563. + dwc_cc_t *cc;
  5564. +
  5565. + DWC_MUTEX_LOCK(cc_if->mutex);
  5566. + *length = 0;
  5567. + cc = cc_find(cc_if, id);
  5568. + if (cc) {
  5569. + *length = cc->length;
  5570. + retval = cc->name;
  5571. + }
  5572. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5573. +
  5574. + return retval;
  5575. +}
  5576. +
  5577. +#endif /* DWC_CCLIB */
  5578. --- /dev/null
  5579. +++ b/drivers/usb/host/dwc_common_port/dwc_cc.h
  5580. @@ -0,0 +1,224 @@
  5581. +/* =========================================================================
  5582. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  5583. + * $Revision: #4 $
  5584. + * $Date: 2010/09/28 $
  5585. + * $Change: 1596182 $
  5586. + *
  5587. + * Synopsys Portability Library Software and documentation
  5588. + * (hereinafter, "Software") is an Unsupported proprietary work of
  5589. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  5590. + * between Synopsys and you.
  5591. + *
  5592. + * The Software IS NOT an item of Licensed Software or Licensed Product
  5593. + * under any End User Software License Agreement or Agreement for
  5594. + * Licensed Product with Synopsys or any supplement thereto. You are
  5595. + * permitted to use and redistribute this Software in source and binary
  5596. + * forms, with or without modification, provided that redistributions
  5597. + * of source code must retain this notice. You may not view, use,
  5598. + * disclose, copy or distribute this file or any information contained
  5599. + * herein except pursuant to this license grant from Synopsys. If you
  5600. + * do not agree with this notice, including the disclaimer below, then
  5601. + * you are not authorized to use the Software.
  5602. + *
  5603. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  5604. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5605. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  5606. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  5607. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5608. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5609. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5610. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  5611. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5612. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  5613. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  5614. + * DAMAGE.
  5615. + * ========================================================================= */
  5616. +#ifndef _DWC_CC_H_
  5617. +#define _DWC_CC_H_
  5618. +
  5619. +#ifdef __cplusplus
  5620. +extern "C" {
  5621. +#endif
  5622. +
  5623. +/** @file
  5624. + *
  5625. + * This file defines the Context Context library.
  5626. + *
  5627. + * The main data structure is dwc_cc_if_t which is returned by either the
  5628. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  5629. + * function. The data structure is opaque and should only be manipulated via the
  5630. + * functions provied in this API.
  5631. + *
  5632. + * It manages a list of connection contexts and operations can be performed to
  5633. + * add, remove, query, search, and change, those contexts. Additionally,
  5634. + * a dwc_notifier_t object can be requested from the manager so that
  5635. + * the user can be notified whenever the context list has changed.
  5636. + */
  5637. +
  5638. +#include "dwc_os.h"
  5639. +#include "dwc_list.h"
  5640. +#include "dwc_notifier.h"
  5641. +
  5642. +
  5643. +/* Notifications */
  5644. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  5645. +
  5646. +struct dwc_cc_if;
  5647. +typedef struct dwc_cc_if dwc_cc_if_t;
  5648. +
  5649. +
  5650. +/** @name Connection Context Operations */
  5651. +/** @{ */
  5652. +
  5653. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  5654. + * fields to default values, and returns a pointer to the structure or NULL on
  5655. + * error. */
  5656. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  5657. + dwc_notifier_t *notifier, unsigned is_host);
  5658. +
  5659. +/** Frees the memory for the specified CC structure allocated from
  5660. + * dwc_cc_if_alloc(). */
  5661. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  5662. +
  5663. +/** Removes all contexts from the connection context list */
  5664. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  5665. +
  5666. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  5667. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  5668. + * not overwritten.
  5669. + *
  5670. + * @param cc_if The cc_if structure.
  5671. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  5672. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  5673. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  5674. + * @param name An optional host friendly name as defined in the association model
  5675. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  5676. + * @param length The length othe unicode string.
  5677. + * @return A unique identifier used to refer to this context that is valid for
  5678. + * as long as this context is still in the list. */
  5679. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5680. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  5681. + uint8_t length);
  5682. +
  5683. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  5684. + * list, preserving any accumulated statistics. This would typically be called
  5685. + * if the host decideds to change the context with a SET_CONNECTION request.
  5686. + *
  5687. + * @param cc_if The cc_if structure.
  5688. + * @param id The identifier of the connection context.
  5689. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  5690. + * indicates no change.
  5691. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  5692. + * indicates no change.
  5693. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  5694. + * indicates no change.
  5695. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  5696. + * @param length Length of name. */
  5697. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  5698. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  5699. + uint8_t *name, uint8_t length);
  5700. +
  5701. +/** Remove the specified connection context.
  5702. + * @param cc_if The cc_if structure.
  5703. + * @param id The identifier of the connection context to remove. */
  5704. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  5705. +
  5706. +/** Get a binary block of data for the connection context list and attributes.
  5707. + * This data can be used by the OS specific driver to save the connection
  5708. + * context list into non-volatile memory.
  5709. + *
  5710. + * @param cc_if The cc_if structure.
  5711. + * @param length Return the length of the data buffer.
  5712. + * @return A pointer to the data buffer. The memory for this buffer should be
  5713. + * freed with DWC_FREE() after use. */
  5714. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  5715. + unsigned int *length);
  5716. +
  5717. +/** Restore the connection context list from the binary data that was previously
  5718. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  5719. + * driver to load a connection context list from non-volatile memory.
  5720. + *
  5721. + * @param cc_if The cc_if structure.
  5722. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  5723. + * @param length The length of the data. */
  5724. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  5725. + uint8_t *data, unsigned int length);
  5726. +
  5727. +/** Find the connection context from the specified CHID.
  5728. + *
  5729. + * @param cc_if The cc_if structure.
  5730. + * @param chid A pointer to the CHID data.
  5731. + * @return A non-zero identifier of the connection context if the CHID matches.
  5732. + * Otherwise returns 0. */
  5733. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  5734. +
  5735. +/** Find the connection context from the specified CDID.
  5736. + *
  5737. + * @param cc_if The cc_if structure.
  5738. + * @param cdid A pointer to the CDID data.
  5739. + * @return A non-zero identifier of the connection context if the CHID matches.
  5740. + * Otherwise returns 0. */
  5741. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  5742. +
  5743. +/** Retrieve the CK from the specified connection context.
  5744. + *
  5745. + * @param cc_if The cc_if structure.
  5746. + * @param id The identifier of the connection context.
  5747. + * @return A pointer to the CK data. The memory does not need to be freed. */
  5748. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  5749. +
  5750. +/** Retrieve the CHID from the specified connection context.
  5751. + *
  5752. + * @param cc_if The cc_if structure.
  5753. + * @param id The identifier of the connection context.
  5754. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  5755. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  5756. +
  5757. +/** Retrieve the CDID from the specified connection context.
  5758. + *
  5759. + * @param cc_if The cc_if structure.
  5760. + * @param id The identifier of the connection context.
  5761. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  5762. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  5763. +
  5764. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  5765. +
  5766. +/** Checks a buffer for non-zero.
  5767. + * @param id A pointer to a 16 byte buffer.
  5768. + * @return true if the 16 byte value is non-zero. */
  5769. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  5770. + int i;
  5771. + for (i=0; i<16; i++) {
  5772. + if (id[i]) return 1;
  5773. + }
  5774. + return 0;
  5775. +}
  5776. +
  5777. +/** Checks a buffer for zero.
  5778. + * @param id A pointer to a 16 byte buffer.
  5779. + * @return true if the 16 byte value is zero. */
  5780. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  5781. + return !dwc_assoc_is_not_zero_id(id);
  5782. +}
  5783. +
  5784. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  5785. + * buffer. */
  5786. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  5787. + char *ptr = buffer;
  5788. + int i;
  5789. + for (i=0; i<16; i++) {
  5790. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  5791. + if (i < 15) {
  5792. + ptr += DWC_SPRINTF(ptr, " ");
  5793. + }
  5794. + }
  5795. + return ptr - buffer;
  5796. +}
  5797. +
  5798. +/** @} */
  5799. +
  5800. +#ifdef __cplusplus
  5801. +}
  5802. +#endif
  5803. +
  5804. +#endif /* _DWC_CC_H_ */
  5805. --- /dev/null
  5806. +++ b/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  5807. @@ -0,0 +1,1308 @@
  5808. +#include "dwc_os.h"
  5809. +#include "dwc_list.h"
  5810. +
  5811. +#ifdef DWC_CCLIB
  5812. +# include "dwc_cc.h"
  5813. +#endif
  5814. +
  5815. +#ifdef DWC_CRYPTOLIB
  5816. +# include "dwc_modpow.h"
  5817. +# include "dwc_dh.h"
  5818. +# include "dwc_crypto.h"
  5819. +#endif
  5820. +
  5821. +#ifdef DWC_NOTIFYLIB
  5822. +# include "dwc_notifier.h"
  5823. +#endif
  5824. +
  5825. +/* OS-Level Implementations */
  5826. +
  5827. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  5828. +
  5829. +
  5830. +/* MISC */
  5831. +
  5832. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  5833. +{
  5834. + return memset(dest, byte, size);
  5835. +}
  5836. +
  5837. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  5838. +{
  5839. + return memcpy(dest, src, size);
  5840. +}
  5841. +
  5842. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  5843. +{
  5844. + bcopy(src, dest, size);
  5845. + return dest;
  5846. +}
  5847. +
  5848. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  5849. +{
  5850. + return memcmp(m1, m2, size);
  5851. +}
  5852. +
  5853. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  5854. +{
  5855. + return strncmp(s1, s2, size);
  5856. +}
  5857. +
  5858. +int DWC_STRCMP(void *s1, void *s2)
  5859. +{
  5860. + return strcmp(s1, s2);
  5861. +}
  5862. +
  5863. +int DWC_STRLEN(char const *str)
  5864. +{
  5865. + return strlen(str);
  5866. +}
  5867. +
  5868. +char *DWC_STRCPY(char *to, char const *from)
  5869. +{
  5870. + return strcpy(to, from);
  5871. +}
  5872. +
  5873. +char *DWC_STRDUP(char const *str)
  5874. +{
  5875. + int len = DWC_STRLEN(str) + 1;
  5876. + char *new = DWC_ALLOC_ATOMIC(len);
  5877. +
  5878. + if (!new) {
  5879. + return NULL;
  5880. + }
  5881. +
  5882. + DWC_MEMCPY(new, str, len);
  5883. + return new;
  5884. +}
  5885. +
  5886. +int DWC_ATOI(char *str, int32_t *value)
  5887. +{
  5888. + char *end = NULL;
  5889. +
  5890. + *value = strtol(str, &end, 0);
  5891. + if (*end == '\0') {
  5892. + return 0;
  5893. + }
  5894. +
  5895. + return -1;
  5896. +}
  5897. +
  5898. +int DWC_ATOUI(char *str, uint32_t *value)
  5899. +{
  5900. + char *end = NULL;
  5901. +
  5902. + *value = strtoul(str, &end, 0);
  5903. + if (*end == '\0') {
  5904. + return 0;
  5905. + }
  5906. +
  5907. + return -1;
  5908. +}
  5909. +
  5910. +
  5911. +#ifdef DWC_UTFLIB
  5912. +/* From usbstring.c */
  5913. +
  5914. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  5915. +{
  5916. + int count = 0;
  5917. + u8 c;
  5918. + u16 uchar;
  5919. +
  5920. + /* this insists on correct encodings, though not minimal ones.
  5921. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  5922. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  5923. + */
  5924. + while (len != 0 && (c = (u8) *s++) != 0) {
  5925. + if (unlikely(c & 0x80)) {
  5926. + // 2-byte sequence:
  5927. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  5928. + if ((c & 0xe0) == 0xc0) {
  5929. + uchar = (c & 0x1f) << 6;
  5930. +
  5931. + c = (u8) *s++;
  5932. + if ((c & 0xc0) != 0xc0)
  5933. + goto fail;
  5934. + c &= 0x3f;
  5935. + uchar |= c;
  5936. +
  5937. + // 3-byte sequence (most CJKV characters):
  5938. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  5939. + } else if ((c & 0xf0) == 0xe0) {
  5940. + uchar = (c & 0x0f) << 12;
  5941. +
  5942. + c = (u8) *s++;
  5943. + if ((c & 0xc0) != 0xc0)
  5944. + goto fail;
  5945. + c &= 0x3f;
  5946. + uchar |= c << 6;
  5947. +
  5948. + c = (u8) *s++;
  5949. + if ((c & 0xc0) != 0xc0)
  5950. + goto fail;
  5951. + c &= 0x3f;
  5952. + uchar |= c;
  5953. +
  5954. + /* no bogus surrogates */
  5955. + if (0xd800 <= uchar && uchar <= 0xdfff)
  5956. + goto fail;
  5957. +
  5958. + // 4-byte sequence (surrogate pairs, currently rare):
  5959. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  5960. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  5961. + // (uuuuu = wwww + 1)
  5962. + // FIXME accept the surrogate code points (only)
  5963. + } else
  5964. + goto fail;
  5965. + } else
  5966. + uchar = c;
  5967. + put_unaligned (cpu_to_le16 (uchar), cp++);
  5968. + count++;
  5969. + len--;
  5970. + }
  5971. + return count;
  5972. +fail:
  5973. + return -1;
  5974. +}
  5975. +
  5976. +#endif /* DWC_UTFLIB */
  5977. +
  5978. +
  5979. +/* dwc_debug.h */
  5980. +
  5981. +dwc_bool_t DWC_IN_IRQ(void)
  5982. +{
  5983. +// return in_irq();
  5984. + return 0;
  5985. +}
  5986. +
  5987. +dwc_bool_t DWC_IN_BH(void)
  5988. +{
  5989. +// return in_softirq();
  5990. + return 0;
  5991. +}
  5992. +
  5993. +void DWC_VPRINTF(char *format, va_list args)
  5994. +{
  5995. + vprintf(format, args);
  5996. +}
  5997. +
  5998. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  5999. +{
  6000. + return vsnprintf(str, size, format, args);
  6001. +}
  6002. +
  6003. +void DWC_PRINTF(char *format, ...)
  6004. +{
  6005. + va_list args;
  6006. +
  6007. + va_start(args, format);
  6008. + DWC_VPRINTF(format, args);
  6009. + va_end(args);
  6010. +}
  6011. +
  6012. +int DWC_SPRINTF(char *buffer, char *format, ...)
  6013. +{
  6014. + int retval;
  6015. + va_list args;
  6016. +
  6017. + va_start(args, format);
  6018. + retval = vsprintf(buffer, format, args);
  6019. + va_end(args);
  6020. + return retval;
  6021. +}
  6022. +
  6023. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  6024. +{
  6025. + int retval;
  6026. + va_list args;
  6027. +
  6028. + va_start(args, format);
  6029. + retval = vsnprintf(buffer, size, format, args);
  6030. + va_end(args);
  6031. + return retval;
  6032. +}
  6033. +
  6034. +void __DWC_WARN(char *format, ...)
  6035. +{
  6036. + va_list args;
  6037. +
  6038. + va_start(args, format);
  6039. + DWC_VPRINTF(format, args);
  6040. + va_end(args);
  6041. +}
  6042. +
  6043. +void __DWC_ERROR(char *format, ...)
  6044. +{
  6045. + va_list args;
  6046. +
  6047. + va_start(args, format);
  6048. + DWC_VPRINTF(format, args);
  6049. + va_end(args);
  6050. +}
  6051. +
  6052. +void DWC_EXCEPTION(char *format, ...)
  6053. +{
  6054. + va_list args;
  6055. +
  6056. + va_start(args, format);
  6057. + DWC_VPRINTF(format, args);
  6058. + va_end(args);
  6059. +// BUG_ON(1); ???
  6060. +}
  6061. +
  6062. +#ifdef DEBUG
  6063. +void __DWC_DEBUG(char *format, ...)
  6064. +{
  6065. + va_list args;
  6066. +
  6067. + va_start(args, format);
  6068. + DWC_VPRINTF(format, args);
  6069. + va_end(args);
  6070. +}
  6071. +#endif
  6072. +
  6073. +
  6074. +/* dwc_mem.h */
  6075. +
  6076. +#if 0
  6077. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  6078. + uint32_t align,
  6079. + uint32_t alloc)
  6080. +{
  6081. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  6082. + size, align, alloc);
  6083. + return (dwc_pool_t *)pool;
  6084. +}
  6085. +
  6086. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  6087. +{
  6088. + dma_pool_destroy((struct dma_pool *)pool);
  6089. +}
  6090. +
  6091. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  6092. +{
  6093. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  6094. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  6095. +}
  6096. +
  6097. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  6098. +{
  6099. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  6100. + memset(..);
  6101. +}
  6102. +
  6103. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  6104. +{
  6105. + dma_pool_free(pool, vaddr, daddr);
  6106. +}
  6107. +#endif
  6108. +
  6109. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  6110. +{
  6111. + if (error)
  6112. + return;
  6113. + *(bus_addr_t *)arg = segs[0].ds_addr;
  6114. +}
  6115. +
  6116. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  6117. +{
  6118. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  6119. + int error;
  6120. +
  6121. + error = bus_dma_tag_create(
  6122. +#if __FreeBSD_version >= 700000
  6123. + bus_get_dma_tag(dma->dev), /* parent */
  6124. +#else
  6125. + NULL, /* parent */
  6126. +#endif
  6127. + 4, 0, /* alignment, bounds */
  6128. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  6129. + BUS_SPACE_MAXADDR, /* highaddr */
  6130. + NULL, NULL, /* filter, filterarg */
  6131. + size, /* maxsize */
  6132. + 1, /* nsegments */
  6133. + size, /* maxsegsize */
  6134. + 0, /* flags */
  6135. + NULL, /* lockfunc */
  6136. + NULL, /* lockarg */
  6137. + &dma->dma_tag);
  6138. + if (error) {
  6139. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  6140. + __func__, error);
  6141. + goto fail_0;
  6142. + }
  6143. +
  6144. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  6145. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  6146. + if (error) {
  6147. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  6148. + __func__, (uintmax_t)size, error);
  6149. + goto fail_1;
  6150. + }
  6151. +
  6152. + dma->dma_paddr = 0;
  6153. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  6154. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  6155. + if (error || dma->dma_paddr == 0) {
  6156. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  6157. + __func__, error);
  6158. + goto fail_2;
  6159. + }
  6160. +
  6161. + *dma_addr = dma->dma_paddr;
  6162. + return dma->dma_vaddr;
  6163. +
  6164. +fail_2:
  6165. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  6166. +fail_1:
  6167. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  6168. + bus_dma_tag_destroy(dma->dma_tag);
  6169. +fail_0:
  6170. + dma->dma_map = NULL;
  6171. + dma->dma_tag = NULL;
  6172. +
  6173. + return NULL;
  6174. +}
  6175. +
  6176. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  6177. +{
  6178. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  6179. +
  6180. + if (dma->dma_tag == NULL)
  6181. + return;
  6182. + if (dma->dma_map != NULL) {
  6183. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  6184. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  6185. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  6186. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  6187. + dma->dma_map = NULL;
  6188. + }
  6189. +
  6190. + bus_dma_tag_destroy(dma->dma_tag);
  6191. + dma->dma_tag = NULL;
  6192. +}
  6193. +
  6194. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  6195. +{
  6196. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  6197. +}
  6198. +
  6199. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  6200. +{
  6201. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  6202. +}
  6203. +
  6204. +void __DWC_FREE(void *mem_ctx, void *addr)
  6205. +{
  6206. + free(addr, M_DEVBUF);
  6207. +}
  6208. +
  6209. +
  6210. +#ifdef DWC_CRYPTOLIB
  6211. +/* dwc_crypto.h */
  6212. +
  6213. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  6214. +{
  6215. + get_random_bytes(buffer, length);
  6216. +}
  6217. +
  6218. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  6219. +{
  6220. + struct crypto_blkcipher *tfm;
  6221. + struct blkcipher_desc desc;
  6222. + struct scatterlist sgd;
  6223. + struct scatterlist sgs;
  6224. +
  6225. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  6226. + if (tfm == NULL) {
  6227. + printk("failed to load transform for aes CBC\n");
  6228. + return -1;
  6229. + }
  6230. +
  6231. + crypto_blkcipher_setkey(tfm, key, keylen);
  6232. + crypto_blkcipher_set_iv(tfm, iv, 16);
  6233. +
  6234. + sg_init_one(&sgd, out, messagelen);
  6235. + sg_init_one(&sgs, message, messagelen);
  6236. +
  6237. + desc.tfm = tfm;
  6238. + desc.flags = 0;
  6239. +
  6240. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  6241. + crypto_free_blkcipher(tfm);
  6242. + DWC_ERROR("AES CBC encryption failed");
  6243. + return -1;
  6244. + }
  6245. +
  6246. + crypto_free_blkcipher(tfm);
  6247. + return 0;
  6248. +}
  6249. +
  6250. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  6251. +{
  6252. + struct crypto_hash *tfm;
  6253. + struct hash_desc desc;
  6254. + struct scatterlist sg;
  6255. +
  6256. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  6257. + if (IS_ERR(tfm)) {
  6258. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  6259. + return 0;
  6260. + }
  6261. + desc.tfm = tfm;
  6262. + desc.flags = 0;
  6263. +
  6264. + sg_init_one(&sg, message, len);
  6265. + crypto_hash_digest(&desc, &sg, len, out);
  6266. + crypto_free_hash(tfm);
  6267. +
  6268. + return 1;
  6269. +}
  6270. +
  6271. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  6272. + uint8_t *key, uint32_t keylen, uint8_t *out)
  6273. +{
  6274. + struct crypto_hash *tfm;
  6275. + struct hash_desc desc;
  6276. + struct scatterlist sg;
  6277. +
  6278. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  6279. + if (IS_ERR(tfm)) {
  6280. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  6281. + return 0;
  6282. + }
  6283. + desc.tfm = tfm;
  6284. + desc.flags = 0;
  6285. +
  6286. + sg_init_one(&sg, message, messagelen);
  6287. + crypto_hash_setkey(tfm, key, keylen);
  6288. + crypto_hash_digest(&desc, &sg, messagelen, out);
  6289. + crypto_free_hash(tfm);
  6290. +
  6291. + return 1;
  6292. +}
  6293. +
  6294. +#endif /* DWC_CRYPTOLIB */
  6295. +
  6296. +
  6297. +/* Byte Ordering Conversions */
  6298. +
  6299. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  6300. +{
  6301. +#ifdef __LITTLE_ENDIAN
  6302. + return *p;
  6303. +#else
  6304. + uint8_t *u_p = (uint8_t *)p;
  6305. +
  6306. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6307. +#endif
  6308. +}
  6309. +
  6310. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  6311. +{
  6312. +#ifdef __BIG_ENDIAN
  6313. + return *p;
  6314. +#else
  6315. + uint8_t *u_p = (uint8_t *)p;
  6316. +
  6317. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6318. +#endif
  6319. +}
  6320. +
  6321. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  6322. +{
  6323. +#ifdef __LITTLE_ENDIAN
  6324. + return *p;
  6325. +#else
  6326. + uint8_t *u_p = (uint8_t *)p;
  6327. +
  6328. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6329. +#endif
  6330. +}
  6331. +
  6332. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  6333. +{
  6334. +#ifdef __BIG_ENDIAN
  6335. + return *p;
  6336. +#else
  6337. + uint8_t *u_p = (uint8_t *)p;
  6338. +
  6339. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6340. +#endif
  6341. +}
  6342. +
  6343. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  6344. +{
  6345. +#ifdef __LITTLE_ENDIAN
  6346. + return *p;
  6347. +#else
  6348. + uint8_t *u_p = (uint8_t *)p;
  6349. + return (u_p[1] | (u_p[0] << 8));
  6350. +#endif
  6351. +}
  6352. +
  6353. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  6354. +{
  6355. +#ifdef __BIG_ENDIAN
  6356. + return *p;
  6357. +#else
  6358. + uint8_t *u_p = (uint8_t *)p;
  6359. + return (u_p[1] | (u_p[0] << 8));
  6360. +#endif
  6361. +}
  6362. +
  6363. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  6364. +{
  6365. +#ifdef __LITTLE_ENDIAN
  6366. + return *p;
  6367. +#else
  6368. + uint8_t *u_p = (uint8_t *)p;
  6369. + return (u_p[1] | (u_p[0] << 8));
  6370. +#endif
  6371. +}
  6372. +
  6373. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  6374. +{
  6375. +#ifdef __BIG_ENDIAN
  6376. + return *p;
  6377. +#else
  6378. + uint8_t *u_p = (uint8_t *)p;
  6379. + return (u_p[1] | (u_p[0] << 8));
  6380. +#endif
  6381. +}
  6382. +
  6383. +
  6384. +/* Registers */
  6385. +
  6386. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  6387. +{
  6388. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6389. + bus_size_t ior = (bus_size_t)reg;
  6390. +
  6391. + return bus_space_read_4(io->iot, io->ioh, ior);
  6392. +}
  6393. +
  6394. +#if 0
  6395. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  6396. +{
  6397. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6398. + bus_size_t ior = (bus_size_t)reg;
  6399. +
  6400. + return bus_space_read_8(io->iot, io->ioh, ior);
  6401. +}
  6402. +#endif
  6403. +
  6404. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  6405. +{
  6406. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6407. + bus_size_t ior = (bus_size_t)reg;
  6408. +
  6409. + bus_space_write_4(io->iot, io->ioh, ior, value);
  6410. +}
  6411. +
  6412. +#if 0
  6413. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  6414. +{
  6415. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6416. + bus_size_t ior = (bus_size_t)reg;
  6417. +
  6418. + bus_space_write_8(io->iot, io->ioh, ior, value);
  6419. +}
  6420. +#endif
  6421. +
  6422. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  6423. + uint32_t set_mask)
  6424. +{
  6425. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6426. + bus_size_t ior = (bus_size_t)reg;
  6427. +
  6428. + bus_space_write_4(io->iot, io->ioh, ior,
  6429. + (bus_space_read_4(io->iot, io->ioh, ior) &
  6430. + ~clear_mask) | set_mask);
  6431. +}
  6432. +
  6433. +#if 0
  6434. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  6435. + uint64_t set_mask)
  6436. +{
  6437. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6438. + bus_size_t ior = (bus_size_t)reg;
  6439. +
  6440. + bus_space_write_8(io->iot, io->ioh, ior,
  6441. + (bus_space_read_8(io->iot, io->ioh, ior) &
  6442. + ~clear_mask) | set_mask);
  6443. +}
  6444. +#endif
  6445. +
  6446. +
  6447. +/* Locking */
  6448. +
  6449. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  6450. +{
  6451. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  6452. +
  6453. + if (!sl) {
  6454. + DWC_ERROR("Cannot allocate memory for spinlock");
  6455. + return NULL;
  6456. + }
  6457. +
  6458. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  6459. + return (dwc_spinlock_t *)sl;
  6460. +}
  6461. +
  6462. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  6463. +{
  6464. + struct mtx *sl = (struct mtx *)lock;
  6465. +
  6466. + mtx_destroy(sl);
  6467. + DWC_FREE(sl);
  6468. +}
  6469. +
  6470. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  6471. +{
  6472. + mtx_lock_spin((struct mtx *)lock); // ???
  6473. +}
  6474. +
  6475. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  6476. +{
  6477. + mtx_unlock_spin((struct mtx *)lock); // ???
  6478. +}
  6479. +
  6480. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  6481. +{
  6482. + mtx_lock_spin((struct mtx *)lock);
  6483. +}
  6484. +
  6485. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  6486. +{
  6487. + mtx_unlock_spin((struct mtx *)lock);
  6488. +}
  6489. +
  6490. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  6491. +{
  6492. + struct mtx *m;
  6493. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  6494. +
  6495. + if (!mutex) {
  6496. + DWC_ERROR("Cannot allocate memory for mutex");
  6497. + return NULL;
  6498. + }
  6499. +
  6500. + m = (struct mtx *)mutex;
  6501. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  6502. + return mutex;
  6503. +}
  6504. +
  6505. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  6506. +#else
  6507. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  6508. +{
  6509. + mtx_destroy((struct mtx *)mutex);
  6510. + DWC_FREE(mutex);
  6511. +}
  6512. +#endif
  6513. +
  6514. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  6515. +{
  6516. + struct mtx *m = (struct mtx *)mutex;
  6517. +
  6518. + mtx_lock(m);
  6519. +}
  6520. +
  6521. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  6522. +{
  6523. + struct mtx *m = (struct mtx *)mutex;
  6524. +
  6525. + return mtx_trylock(m);
  6526. +}
  6527. +
  6528. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  6529. +{
  6530. + struct mtx *m = (struct mtx *)mutex;
  6531. +
  6532. + mtx_unlock(m);
  6533. +}
  6534. +
  6535. +
  6536. +/* Timing */
  6537. +
  6538. +void DWC_UDELAY(uint32_t usecs)
  6539. +{
  6540. + DELAY(usecs);
  6541. +}
  6542. +
  6543. +void DWC_MDELAY(uint32_t msecs)
  6544. +{
  6545. + do {
  6546. + DELAY(1000);
  6547. + } while (--msecs);
  6548. +}
  6549. +
  6550. +void DWC_MSLEEP(uint32_t msecs)
  6551. +{
  6552. + struct timeval tv;
  6553. +
  6554. + tv.tv_sec = msecs / 1000;
  6555. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  6556. + pause("dw3slp", tvtohz(&tv));
  6557. +}
  6558. +
  6559. +uint32_t DWC_TIME(void)
  6560. +{
  6561. + struct timeval tv;
  6562. +
  6563. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  6564. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  6565. +}
  6566. +
  6567. +
  6568. +/* Timers */
  6569. +
  6570. +struct dwc_timer {
  6571. + struct callout t;
  6572. + char *name;
  6573. + dwc_spinlock_t *lock;
  6574. + dwc_timer_callback_t cb;
  6575. + void *data;
  6576. +};
  6577. +
  6578. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  6579. +{
  6580. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  6581. +
  6582. + if (!t) {
  6583. + DWC_ERROR("Cannot allocate memory for timer");
  6584. + return NULL;
  6585. + }
  6586. +
  6587. + callout_init(&t->t, 1);
  6588. +
  6589. + t->name = DWC_STRDUP(name);
  6590. + if (!t->name) {
  6591. + DWC_ERROR("Cannot allocate memory for timer->name");
  6592. + goto no_name;
  6593. + }
  6594. +
  6595. + t->lock = DWC_SPINLOCK_ALLOC();
  6596. + if (!t->lock) {
  6597. + DWC_ERROR("Cannot allocate memory for lock");
  6598. + goto no_lock;
  6599. + }
  6600. +
  6601. + t->cb = cb;
  6602. + t->data = data;
  6603. +
  6604. + return t;
  6605. +
  6606. + no_lock:
  6607. + DWC_FREE(t->name);
  6608. + no_name:
  6609. + DWC_FREE(t);
  6610. +
  6611. + return NULL;
  6612. +}
  6613. +
  6614. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  6615. +{
  6616. + callout_stop(&timer->t);
  6617. + DWC_SPINLOCK_FREE(timer->lock);
  6618. + DWC_FREE(timer->name);
  6619. + DWC_FREE(timer);
  6620. +}
  6621. +
  6622. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  6623. +{
  6624. + struct timeval tv;
  6625. +
  6626. + tv.tv_sec = time / 1000;
  6627. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  6628. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  6629. +}
  6630. +
  6631. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  6632. +{
  6633. + callout_stop(&timer->t);
  6634. +}
  6635. +
  6636. +
  6637. +/* Wait Queues */
  6638. +
  6639. +struct dwc_waitq {
  6640. + struct mtx lock;
  6641. + int abort;
  6642. +};
  6643. +
  6644. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  6645. +{
  6646. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  6647. +
  6648. + if (!wq) {
  6649. + DWC_ERROR("Cannot allocate memory for waitqueue");
  6650. + return NULL;
  6651. + }
  6652. +
  6653. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  6654. + wq->abort = 0;
  6655. +
  6656. + return wq;
  6657. +}
  6658. +
  6659. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  6660. +{
  6661. + mtx_destroy(&wq->lock);
  6662. + DWC_FREE(wq);
  6663. +}
  6664. +
  6665. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  6666. +{
  6667. +// intrmask_t ipl;
  6668. + int result = 0;
  6669. +
  6670. + mtx_lock(&wq->lock);
  6671. +// ipl = splbio();
  6672. +
  6673. + /* Skip the sleep if already aborted or triggered */
  6674. + if (!wq->abort && !cond(data)) {
  6675. +// splx(ipl);
  6676. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  6677. +// ipl = splbio();
  6678. + }
  6679. +
  6680. + if (result == ERESTART) { // signaled - restart
  6681. + result = -DWC_E_RESTART;
  6682. +
  6683. + } else if (result == EINTR) { // signaled - interrupt
  6684. + result = -DWC_E_ABORT;
  6685. +
  6686. + } else if (wq->abort) {
  6687. + result = -DWC_E_ABORT;
  6688. +
  6689. + } else {
  6690. + result = 0;
  6691. + }
  6692. +
  6693. + wq->abort = 0;
  6694. +// splx(ipl);
  6695. + mtx_unlock(&wq->lock);
  6696. + return result;
  6697. +}
  6698. +
  6699. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  6700. + void *data, int32_t msecs)
  6701. +{
  6702. + struct timeval tv, tv1, tv2;
  6703. +// intrmask_t ipl;
  6704. + int result = 0;
  6705. +
  6706. + tv.tv_sec = msecs / 1000;
  6707. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  6708. +
  6709. + mtx_lock(&wq->lock);
  6710. +// ipl = splbio();
  6711. +
  6712. + /* Skip the sleep if already aborted or triggered */
  6713. + if (!wq->abort && !cond(data)) {
  6714. +// splx(ipl);
  6715. + getmicrouptime(&tv1);
  6716. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  6717. + getmicrouptime(&tv2);
  6718. +// ipl = splbio();
  6719. + }
  6720. +
  6721. + if (result == 0) { // awoken
  6722. + if (wq->abort) {
  6723. + result = -DWC_E_ABORT;
  6724. + } else {
  6725. + tv2.tv_usec -= tv1.tv_usec;
  6726. + if (tv2.tv_usec < 0) {
  6727. + tv2.tv_usec += 1000000;
  6728. + tv2.tv_sec--;
  6729. + }
  6730. +
  6731. + tv2.tv_sec -= tv1.tv_sec;
  6732. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  6733. + result = msecs - result;
  6734. + if (result <= 0)
  6735. + result = 1;
  6736. + }
  6737. + } else if (result == ERESTART) { // signaled - restart
  6738. + result = -DWC_E_RESTART;
  6739. +
  6740. + } else if (result == EINTR) { // signaled - interrupt
  6741. + result = -DWC_E_ABORT;
  6742. +
  6743. + } else { // timed out
  6744. + result = -DWC_E_TIMEOUT;
  6745. + }
  6746. +
  6747. + wq->abort = 0;
  6748. +// splx(ipl);
  6749. + mtx_unlock(&wq->lock);
  6750. + return result;
  6751. +}
  6752. +
  6753. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  6754. +{
  6755. + wakeup(wq);
  6756. +}
  6757. +
  6758. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  6759. +{
  6760. +// intrmask_t ipl;
  6761. +
  6762. + mtx_lock(&wq->lock);
  6763. +// ipl = splbio();
  6764. + wq->abort = 1;
  6765. + wakeup(wq);
  6766. +// splx(ipl);
  6767. + mtx_unlock(&wq->lock);
  6768. +}
  6769. +
  6770. +
  6771. +/* Threading */
  6772. +
  6773. +struct dwc_thread {
  6774. + struct proc *proc;
  6775. + int abort;
  6776. +};
  6777. +
  6778. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  6779. +{
  6780. + int retval;
  6781. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  6782. +
  6783. + if (!thread) {
  6784. + return NULL;
  6785. + }
  6786. +
  6787. + thread->abort = 0;
  6788. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  6789. + RFPROC | RFNOWAIT, 0, "%s", name);
  6790. + if (retval) {
  6791. + DWC_FREE(thread);
  6792. + return NULL;
  6793. + }
  6794. +
  6795. + return thread;
  6796. +}
  6797. +
  6798. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  6799. +{
  6800. + int retval;
  6801. +
  6802. + thread->abort = 1;
  6803. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  6804. +
  6805. + if (retval == 0) {
  6806. + /* DWC_THREAD_EXIT() will free the thread struct */
  6807. + return 0;
  6808. + }
  6809. +
  6810. + /* NOTE: We leak the thread struct if thread doesn't die */
  6811. +
  6812. + if (retval == EWOULDBLOCK) {
  6813. + return -DWC_E_TIMEOUT;
  6814. + }
  6815. +
  6816. + return -DWC_E_UNKNOWN;
  6817. +}
  6818. +
  6819. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  6820. +{
  6821. + return thread->abort;
  6822. +}
  6823. +
  6824. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  6825. +{
  6826. + wakeup(&thread->abort);
  6827. + DWC_FREE(thread);
  6828. + kthread_exit(0);
  6829. +}
  6830. +
  6831. +
  6832. +/* tasklets
  6833. + - Runs in interrupt context (cannot sleep)
  6834. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  6835. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  6836. + */
  6837. +struct dwc_tasklet {
  6838. + struct task t;
  6839. + dwc_tasklet_callback_t cb;
  6840. + void *data;
  6841. +};
  6842. +
  6843. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  6844. +{
  6845. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  6846. +
  6847. + task->cb(task->data);
  6848. +}
  6849. +
  6850. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  6851. +{
  6852. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  6853. +
  6854. + if (task) {
  6855. + task->cb = cb;
  6856. + task->data = data;
  6857. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  6858. + } else {
  6859. + DWC_ERROR("Cannot allocate memory for tasklet");
  6860. + }
  6861. +
  6862. + return task;
  6863. +}
  6864. +
  6865. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  6866. +{
  6867. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  6868. + DWC_FREE(task);
  6869. +}
  6870. +
  6871. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  6872. +{
  6873. + /* Uses predefined system queue */
  6874. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  6875. +}
  6876. +
  6877. +
  6878. +/* workqueues
  6879. + - Runs in process context (can sleep)
  6880. + */
  6881. +typedef struct work_container {
  6882. + dwc_work_callback_t cb;
  6883. + void *data;
  6884. + dwc_workq_t *wq;
  6885. + char *name;
  6886. + int hz;
  6887. +
  6888. +#ifdef DEBUG
  6889. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  6890. +#endif
  6891. + struct task task;
  6892. +} work_container_t;
  6893. +
  6894. +#ifdef DEBUG
  6895. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  6896. +#endif
  6897. +
  6898. +struct dwc_workq {
  6899. + struct taskqueue *taskq;
  6900. + dwc_spinlock_t *lock;
  6901. + dwc_waitq_t *waitq;
  6902. + int pending;
  6903. +
  6904. +#ifdef DEBUG
  6905. + struct work_container_queue entries;
  6906. +#endif
  6907. +};
  6908. +
  6909. +static void do_work(void *data, int pending) // what to do with pending ???
  6910. +{
  6911. + work_container_t *container = (work_container_t *)data;
  6912. + dwc_workq_t *wq = container->wq;
  6913. + dwc_irqflags_t flags;
  6914. +
  6915. + if (container->hz) {
  6916. + pause("dw3wrk", container->hz);
  6917. + }
  6918. +
  6919. + container->cb(container->data);
  6920. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  6921. +
  6922. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  6923. +
  6924. +#ifdef DEBUG
  6925. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  6926. +#endif
  6927. + if (container->name)
  6928. + DWC_FREE(container->name);
  6929. + DWC_FREE(container);
  6930. + wq->pending--;
  6931. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  6932. + DWC_WAITQ_TRIGGER(wq->waitq);
  6933. +}
  6934. +
  6935. +static int work_done(void *data)
  6936. +{
  6937. + dwc_workq_t *workq = (dwc_workq_t *)data;
  6938. +
  6939. + return workq->pending == 0;
  6940. +}
  6941. +
  6942. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  6943. +{
  6944. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  6945. +}
  6946. +
  6947. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  6948. +{
  6949. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  6950. +
  6951. + if (!wq) {
  6952. + DWC_ERROR("Cannot allocate memory for workqueue");
  6953. + return NULL;
  6954. + }
  6955. +
  6956. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  6957. + if (!wq->taskq) {
  6958. + DWC_ERROR("Cannot allocate memory for taskqueue");
  6959. + goto no_taskq;
  6960. + }
  6961. +
  6962. + wq->pending = 0;
  6963. +
  6964. + wq->lock = DWC_SPINLOCK_ALLOC();
  6965. + if (!wq->lock) {
  6966. + DWC_ERROR("Cannot allocate memory for spinlock");
  6967. + goto no_lock;
  6968. + }
  6969. +
  6970. + wq->waitq = DWC_WAITQ_ALLOC();
  6971. + if (!wq->waitq) {
  6972. + DWC_ERROR("Cannot allocate memory for waitqueue");
  6973. + goto no_waitq;
  6974. + }
  6975. +
  6976. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  6977. +
  6978. +#ifdef DEBUG
  6979. + DWC_CIRCLEQ_INIT(&wq->entries);
  6980. +#endif
  6981. + return wq;
  6982. +
  6983. + no_waitq:
  6984. + DWC_SPINLOCK_FREE(wq->lock);
  6985. + no_lock:
  6986. + taskqueue_free(wq->taskq);
  6987. + no_taskq:
  6988. + DWC_FREE(wq);
  6989. +
  6990. + return NULL;
  6991. +}
  6992. +
  6993. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  6994. +{
  6995. +#ifdef DEBUG
  6996. + dwc_irqflags_t flags;
  6997. +
  6998. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  6999. +
  7000. + if (wq->pending != 0) {
  7001. + struct work_container *container;
  7002. +
  7003. + DWC_ERROR("Destroying work queue with pending work");
  7004. +
  7005. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  7006. + DWC_ERROR("Work %s still pending", container->name);
  7007. + }
  7008. + }
  7009. +
  7010. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7011. +#endif
  7012. + DWC_WAITQ_FREE(wq->waitq);
  7013. + DWC_SPINLOCK_FREE(wq->lock);
  7014. + taskqueue_free(wq->taskq);
  7015. + DWC_FREE(wq);
  7016. +}
  7017. +
  7018. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  7019. + char *format, ...)
  7020. +{
  7021. + dwc_irqflags_t flags;
  7022. + work_container_t *container;
  7023. + static char name[128];
  7024. + va_list args;
  7025. +
  7026. + va_start(args, format);
  7027. + DWC_VSNPRINTF(name, 128, format, args);
  7028. + va_end(args);
  7029. +
  7030. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7031. + wq->pending++;
  7032. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7033. + DWC_WAITQ_TRIGGER(wq->waitq);
  7034. +
  7035. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  7036. + if (!container) {
  7037. + DWC_ERROR("Cannot allocate memory for container");
  7038. + return;
  7039. + }
  7040. +
  7041. + container->name = DWC_STRDUP(name);
  7042. + if (!container->name) {
  7043. + DWC_ERROR("Cannot allocate memory for container->name");
  7044. + DWC_FREE(container);
  7045. + return;
  7046. + }
  7047. +
  7048. + container->cb = cb;
  7049. + container->data = data;
  7050. + container->wq = wq;
  7051. + container->hz = 0;
  7052. +
  7053. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  7054. +
  7055. + TASK_INIT(&container->task, 0, do_work, container);
  7056. +
  7057. +#ifdef DEBUG
  7058. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  7059. +#endif
  7060. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  7061. +}
  7062. +
  7063. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  7064. + void *data, uint32_t time, char *format, ...)
  7065. +{
  7066. + dwc_irqflags_t flags;
  7067. + work_container_t *container;
  7068. + static char name[128];
  7069. + struct timeval tv;
  7070. + va_list args;
  7071. +
  7072. + va_start(args, format);
  7073. + DWC_VSNPRINTF(name, 128, format, args);
  7074. + va_end(args);
  7075. +
  7076. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7077. + wq->pending++;
  7078. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7079. + DWC_WAITQ_TRIGGER(wq->waitq);
  7080. +
  7081. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  7082. + if (!container) {
  7083. + DWC_ERROR("Cannot allocate memory for container");
  7084. + return;
  7085. + }
  7086. +
  7087. + container->name = DWC_STRDUP(name);
  7088. + if (!container->name) {
  7089. + DWC_ERROR("Cannot allocate memory for container->name");
  7090. + DWC_FREE(container);
  7091. + return;
  7092. + }
  7093. +
  7094. + container->cb = cb;
  7095. + container->data = data;
  7096. + container->wq = wq;
  7097. +
  7098. + tv.tv_sec = time / 1000;
  7099. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  7100. + container->hz = tvtohz(&tv);
  7101. +
  7102. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  7103. +
  7104. + TASK_INIT(&container->task, 0, do_work, container);
  7105. +
  7106. +#ifdef DEBUG
  7107. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  7108. +#endif
  7109. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  7110. +}
  7111. +
  7112. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  7113. +{
  7114. + return wq->pending;
  7115. +}
  7116. --- /dev/null
  7117. +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  7118. @@ -0,0 +1,1433 @@
  7119. +#include <linux/kernel.h>
  7120. +#include <linux/init.h>
  7121. +#include <linux/module.h>
  7122. +#include <linux/kthread.h>
  7123. +
  7124. +#ifdef DWC_CCLIB
  7125. +# include "dwc_cc.h"
  7126. +#endif
  7127. +
  7128. +#ifdef DWC_CRYPTOLIB
  7129. +# include "dwc_modpow.h"
  7130. +# include "dwc_dh.h"
  7131. +# include "dwc_crypto.h"
  7132. +#endif
  7133. +
  7134. +#ifdef DWC_NOTIFYLIB
  7135. +# include "dwc_notifier.h"
  7136. +#endif
  7137. +
  7138. +/* OS-Level Implementations */
  7139. +
  7140. +/* This is the Linux kernel implementation of the DWC platform library. */
  7141. +#include <linux/moduleparam.h>
  7142. +#include <linux/ctype.h>
  7143. +#include <linux/crypto.h>
  7144. +#include <linux/delay.h>
  7145. +#include <linux/device.h>
  7146. +#include <linux/dma-mapping.h>
  7147. +#include <linux/cdev.h>
  7148. +#include <linux/errno.h>
  7149. +#include <linux/interrupt.h>
  7150. +#include <linux/jiffies.h>
  7151. +#include <linux/list.h>
  7152. +#include <linux/pci.h>
  7153. +#include <linux/random.h>
  7154. +#include <linux/scatterlist.h>
  7155. +#include <linux/slab.h>
  7156. +#include <linux/stat.h>
  7157. +#include <linux/string.h>
  7158. +#include <linux/timer.h>
  7159. +#include <linux/usb.h>
  7160. +
  7161. +#include <linux/version.h>
  7162. +
  7163. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  7164. +# include <linux/usb/gadget.h>
  7165. +#else
  7166. +# include <linux/usb_gadget.h>
  7167. +#endif
  7168. +
  7169. +#include <asm/io.h>
  7170. +#include <asm/page.h>
  7171. +#include <asm/uaccess.h>
  7172. +#include <asm/unaligned.h>
  7173. +
  7174. +#include "dwc_os.h"
  7175. +#include "dwc_list.h"
  7176. +
  7177. +
  7178. +/* MISC */
  7179. +
  7180. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  7181. +{
  7182. + return memset(dest, byte, size);
  7183. +}
  7184. +
  7185. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  7186. +{
  7187. + return memcpy(dest, src, size);
  7188. +}
  7189. +
  7190. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  7191. +{
  7192. + return memmove(dest, src, size);
  7193. +}
  7194. +
  7195. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  7196. +{
  7197. + return memcmp(m1, m2, size);
  7198. +}
  7199. +
  7200. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  7201. +{
  7202. + return strncmp(s1, s2, size);
  7203. +}
  7204. +
  7205. +int DWC_STRCMP(void *s1, void *s2)
  7206. +{
  7207. + return strcmp(s1, s2);
  7208. +}
  7209. +
  7210. +int DWC_STRLEN(char const *str)
  7211. +{
  7212. + return strlen(str);
  7213. +}
  7214. +
  7215. +char *DWC_STRCPY(char *to, char const *from)
  7216. +{
  7217. + return strcpy(to, from);
  7218. +}
  7219. +
  7220. +char *DWC_STRDUP(char const *str)
  7221. +{
  7222. + int len = DWC_STRLEN(str) + 1;
  7223. + char *new = DWC_ALLOC_ATOMIC(len);
  7224. +
  7225. + if (!new) {
  7226. + return NULL;
  7227. + }
  7228. +
  7229. + DWC_MEMCPY(new, str, len);
  7230. + return new;
  7231. +}
  7232. +
  7233. +int DWC_ATOI(const char *str, int32_t *value)
  7234. +{
  7235. + char *end = NULL;
  7236. +
  7237. + *value = simple_strtol(str, &end, 0);
  7238. + if (*end == '\0') {
  7239. + return 0;
  7240. + }
  7241. +
  7242. + return -1;
  7243. +}
  7244. +
  7245. +int DWC_ATOUI(const char *str, uint32_t *value)
  7246. +{
  7247. + char *end = NULL;
  7248. +
  7249. + *value = simple_strtoul(str, &end, 0);
  7250. + if (*end == '\0') {
  7251. + return 0;
  7252. + }
  7253. +
  7254. + return -1;
  7255. +}
  7256. +
  7257. +
  7258. +#ifdef DWC_UTFLIB
  7259. +/* From usbstring.c */
  7260. +
  7261. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  7262. +{
  7263. + int count = 0;
  7264. + u8 c;
  7265. + u16 uchar;
  7266. +
  7267. + /* this insists on correct encodings, though not minimal ones.
  7268. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  7269. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  7270. + */
  7271. + while (len != 0 && (c = (u8) *s++) != 0) {
  7272. + if (unlikely(c & 0x80)) {
  7273. + // 2-byte sequence:
  7274. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  7275. + if ((c & 0xe0) == 0xc0) {
  7276. + uchar = (c & 0x1f) << 6;
  7277. +
  7278. + c = (u8) *s++;
  7279. + if ((c & 0xc0) != 0xc0)
  7280. + goto fail;
  7281. + c &= 0x3f;
  7282. + uchar |= c;
  7283. +
  7284. + // 3-byte sequence (most CJKV characters):
  7285. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  7286. + } else if ((c & 0xf0) == 0xe0) {
  7287. + uchar = (c & 0x0f) << 12;
  7288. +
  7289. + c = (u8) *s++;
  7290. + if ((c & 0xc0) != 0xc0)
  7291. + goto fail;
  7292. + c &= 0x3f;
  7293. + uchar |= c << 6;
  7294. +
  7295. + c = (u8) *s++;
  7296. + if ((c & 0xc0) != 0xc0)
  7297. + goto fail;
  7298. + c &= 0x3f;
  7299. + uchar |= c;
  7300. +
  7301. + /* no bogus surrogates */
  7302. + if (0xd800 <= uchar && uchar <= 0xdfff)
  7303. + goto fail;
  7304. +
  7305. + // 4-byte sequence (surrogate pairs, currently rare):
  7306. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  7307. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  7308. + // (uuuuu = wwww + 1)
  7309. + // FIXME accept the surrogate code points (only)
  7310. + } else
  7311. + goto fail;
  7312. + } else
  7313. + uchar = c;
  7314. + put_unaligned (cpu_to_le16 (uchar), cp++);
  7315. + count++;
  7316. + len--;
  7317. + }
  7318. + return count;
  7319. +fail:
  7320. + return -1;
  7321. +}
  7322. +#endif /* DWC_UTFLIB */
  7323. +
  7324. +
  7325. +/* dwc_debug.h */
  7326. +
  7327. +dwc_bool_t DWC_IN_IRQ(void)
  7328. +{
  7329. + return in_irq();
  7330. +}
  7331. +
  7332. +dwc_bool_t DWC_IN_BH(void)
  7333. +{
  7334. + return in_softirq();
  7335. +}
  7336. +
  7337. +void DWC_VPRINTF(char *format, va_list args)
  7338. +{
  7339. + vprintk(format, args);
  7340. +}
  7341. +
  7342. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  7343. +{
  7344. + return vsnprintf(str, size, format, args);
  7345. +}
  7346. +
  7347. +void DWC_PRINTF(char *format, ...)
  7348. +{
  7349. + va_list args;
  7350. +
  7351. + va_start(args, format);
  7352. + DWC_VPRINTF(format, args);
  7353. + va_end(args);
  7354. +}
  7355. +
  7356. +int DWC_SPRINTF(char *buffer, char *format, ...)
  7357. +{
  7358. + int retval;
  7359. + va_list args;
  7360. +
  7361. + va_start(args, format);
  7362. + retval = vsprintf(buffer, format, args);
  7363. + va_end(args);
  7364. + return retval;
  7365. +}
  7366. +
  7367. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  7368. +{
  7369. + int retval;
  7370. + va_list args;
  7371. +
  7372. + va_start(args, format);
  7373. + retval = vsnprintf(buffer, size, format, args);
  7374. + va_end(args);
  7375. + return retval;
  7376. +}
  7377. +
  7378. +void __DWC_WARN(char *format, ...)
  7379. +{
  7380. + va_list args;
  7381. +
  7382. + va_start(args, format);
  7383. + DWC_PRINTF(KERN_WARNING);
  7384. + DWC_VPRINTF(format, args);
  7385. + va_end(args);
  7386. +}
  7387. +
  7388. +void __DWC_ERROR(char *format, ...)
  7389. +{
  7390. + va_list args;
  7391. +
  7392. + va_start(args, format);
  7393. + DWC_PRINTF(KERN_ERR);
  7394. + DWC_VPRINTF(format, args);
  7395. + va_end(args);
  7396. +}
  7397. +
  7398. +void DWC_EXCEPTION(char *format, ...)
  7399. +{
  7400. + va_list args;
  7401. +
  7402. + va_start(args, format);
  7403. + DWC_PRINTF(KERN_ERR);
  7404. + DWC_VPRINTF(format, args);
  7405. + va_end(args);
  7406. + BUG_ON(1);
  7407. +}
  7408. +
  7409. +#ifdef DEBUG
  7410. +void __DWC_DEBUG(char *format, ...)
  7411. +{
  7412. + va_list args;
  7413. +
  7414. + va_start(args, format);
  7415. + DWC_PRINTF(KERN_DEBUG);
  7416. + DWC_VPRINTF(format, args);
  7417. + va_end(args);
  7418. +}
  7419. +#endif
  7420. +
  7421. +
  7422. +/* dwc_mem.h */
  7423. +
  7424. +#if 0
  7425. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  7426. + uint32_t align,
  7427. + uint32_t alloc)
  7428. +{
  7429. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  7430. + size, align, alloc);
  7431. + return (dwc_pool_t *)pool;
  7432. +}
  7433. +
  7434. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  7435. +{
  7436. + dma_pool_destroy((struct dma_pool *)pool);
  7437. +}
  7438. +
  7439. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  7440. +{
  7441. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  7442. +}
  7443. +
  7444. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  7445. +{
  7446. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  7447. + memset(..);
  7448. +}
  7449. +
  7450. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  7451. +{
  7452. + dma_pool_free(pool, vaddr, daddr);
  7453. +}
  7454. +#endif
  7455. +
  7456. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  7457. +{
  7458. +#ifdef xxCOSIM /* Only works for 32-bit cosim */
  7459. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL);
  7460. +#else
  7461. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
  7462. +#endif
  7463. + if (!buf) {
  7464. + return NULL;
  7465. + }
  7466. +
  7467. + memset(buf, 0, (size_t)size);
  7468. + return buf;
  7469. +}
  7470. +
  7471. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  7472. +{
  7473. + void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
  7474. + if (!buf) {
  7475. + return NULL;
  7476. + }
  7477. + memset(buf, 0, (size_t)size);
  7478. + return buf;
  7479. +}
  7480. +
  7481. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  7482. +{
  7483. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  7484. +}
  7485. +
  7486. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  7487. +{
  7488. + return kzalloc(size, GFP_KERNEL);
  7489. +}
  7490. +
  7491. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  7492. +{
  7493. + return kzalloc(size, GFP_ATOMIC);
  7494. +}
  7495. +
  7496. +void __DWC_FREE(void *mem_ctx, void *addr)
  7497. +{
  7498. + kfree(addr);
  7499. +}
  7500. +
  7501. +
  7502. +#ifdef DWC_CRYPTOLIB
  7503. +/* dwc_crypto.h */
  7504. +
  7505. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  7506. +{
  7507. + get_random_bytes(buffer, length);
  7508. +}
  7509. +
  7510. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  7511. +{
  7512. + struct crypto_blkcipher *tfm;
  7513. + struct blkcipher_desc desc;
  7514. + struct scatterlist sgd;
  7515. + struct scatterlist sgs;
  7516. +
  7517. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  7518. + if (tfm == NULL) {
  7519. + printk("failed to load transform for aes CBC\n");
  7520. + return -1;
  7521. + }
  7522. +
  7523. + crypto_blkcipher_setkey(tfm, key, keylen);
  7524. + crypto_blkcipher_set_iv(tfm, iv, 16);
  7525. +
  7526. + sg_init_one(&sgd, out, messagelen);
  7527. + sg_init_one(&sgs, message, messagelen);
  7528. +
  7529. + desc.tfm = tfm;
  7530. + desc.flags = 0;
  7531. +
  7532. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  7533. + crypto_free_blkcipher(tfm);
  7534. + DWC_ERROR("AES CBC encryption failed");
  7535. + return -1;
  7536. + }
  7537. +
  7538. + crypto_free_blkcipher(tfm);
  7539. + return 0;
  7540. +}
  7541. +
  7542. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  7543. +{
  7544. + struct crypto_hash *tfm;
  7545. + struct hash_desc desc;
  7546. + struct scatterlist sg;
  7547. +
  7548. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  7549. + if (IS_ERR(tfm)) {
  7550. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  7551. + return 0;
  7552. + }
  7553. + desc.tfm = tfm;
  7554. + desc.flags = 0;
  7555. +
  7556. + sg_init_one(&sg, message, len);
  7557. + crypto_hash_digest(&desc, &sg, len, out);
  7558. + crypto_free_hash(tfm);
  7559. +
  7560. + return 1;
  7561. +}
  7562. +
  7563. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  7564. + uint8_t *key, uint32_t keylen, uint8_t *out)
  7565. +{
  7566. + struct crypto_hash *tfm;
  7567. + struct hash_desc desc;
  7568. + struct scatterlist sg;
  7569. +
  7570. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  7571. + if (IS_ERR(tfm)) {
  7572. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  7573. + return 0;
  7574. + }
  7575. + desc.tfm = tfm;
  7576. + desc.flags = 0;
  7577. +
  7578. + sg_init_one(&sg, message, messagelen);
  7579. + crypto_hash_setkey(tfm, key, keylen);
  7580. + crypto_hash_digest(&desc, &sg, messagelen, out);
  7581. + crypto_free_hash(tfm);
  7582. +
  7583. + return 1;
  7584. +}
  7585. +#endif /* DWC_CRYPTOLIB */
  7586. +
  7587. +
  7588. +/* Byte Ordering Conversions */
  7589. +
  7590. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  7591. +{
  7592. +#ifdef __LITTLE_ENDIAN
  7593. + return *p;
  7594. +#else
  7595. + uint8_t *u_p = (uint8_t *)p;
  7596. +
  7597. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7598. +#endif
  7599. +}
  7600. +
  7601. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  7602. +{
  7603. +#ifdef __BIG_ENDIAN
  7604. + return *p;
  7605. +#else
  7606. + uint8_t *u_p = (uint8_t *)p;
  7607. +
  7608. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7609. +#endif
  7610. +}
  7611. +
  7612. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  7613. +{
  7614. +#ifdef __LITTLE_ENDIAN
  7615. + return *p;
  7616. +#else
  7617. + uint8_t *u_p = (uint8_t *)p;
  7618. +
  7619. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7620. +#endif
  7621. +}
  7622. +
  7623. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  7624. +{
  7625. +#ifdef __BIG_ENDIAN
  7626. + return *p;
  7627. +#else
  7628. + uint8_t *u_p = (uint8_t *)p;
  7629. +
  7630. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7631. +#endif
  7632. +}
  7633. +
  7634. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  7635. +{
  7636. +#ifdef __LITTLE_ENDIAN
  7637. + return *p;
  7638. +#else
  7639. + uint8_t *u_p = (uint8_t *)p;
  7640. + return (u_p[1] | (u_p[0] << 8));
  7641. +#endif
  7642. +}
  7643. +
  7644. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  7645. +{
  7646. +#ifdef __BIG_ENDIAN
  7647. + return *p;
  7648. +#else
  7649. + uint8_t *u_p = (uint8_t *)p;
  7650. + return (u_p[1] | (u_p[0] << 8));
  7651. +#endif
  7652. +}
  7653. +
  7654. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  7655. +{
  7656. +#ifdef __LITTLE_ENDIAN
  7657. + return *p;
  7658. +#else
  7659. + uint8_t *u_p = (uint8_t *)p;
  7660. + return (u_p[1] | (u_p[0] << 8));
  7661. +#endif
  7662. +}
  7663. +
  7664. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  7665. +{
  7666. +#ifdef __BIG_ENDIAN
  7667. + return *p;
  7668. +#else
  7669. + uint8_t *u_p = (uint8_t *)p;
  7670. + return (u_p[1] | (u_p[0] << 8));
  7671. +#endif
  7672. +}
  7673. +
  7674. +
  7675. +/* Registers */
  7676. +
  7677. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  7678. +{
  7679. + return readl(reg);
  7680. +}
  7681. +
  7682. +#if 0
  7683. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  7684. +{
  7685. +}
  7686. +#endif
  7687. +
  7688. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  7689. +{
  7690. + writel(value, reg);
  7691. +}
  7692. +
  7693. +#if 0
  7694. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  7695. +{
  7696. +}
  7697. +#endif
  7698. +
  7699. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  7700. +{
  7701. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  7702. +}
  7703. +
  7704. +#if 0
  7705. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  7706. +{
  7707. +}
  7708. +#endif
  7709. +
  7710. +
  7711. +/* Locking */
  7712. +
  7713. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  7714. +{
  7715. + spinlock_t *sl = (spinlock_t *)1;
  7716. +
  7717. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7718. + sl = DWC_ALLOC(sizeof(*sl));
  7719. + if (!sl) {
  7720. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  7721. + return NULL;
  7722. + }
  7723. +
  7724. + spin_lock_init(sl);
  7725. +#endif
  7726. + return (dwc_spinlock_t *)sl;
  7727. +}
  7728. +
  7729. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  7730. +{
  7731. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7732. + DWC_FREE(lock);
  7733. +#endif
  7734. +}
  7735. +
  7736. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  7737. +{
  7738. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7739. + spin_lock((spinlock_t *)lock);
  7740. +#endif
  7741. +}
  7742. +
  7743. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  7744. +{
  7745. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7746. + spin_unlock((spinlock_t *)lock);
  7747. +#endif
  7748. +}
  7749. +
  7750. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  7751. +{
  7752. + dwc_irqflags_t f;
  7753. +
  7754. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7755. + spin_lock_irqsave((spinlock_t *)lock, f);
  7756. +#else
  7757. + local_irq_save(f);
  7758. +#endif
  7759. + *flags = f;
  7760. +}
  7761. +
  7762. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  7763. +{
  7764. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7765. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  7766. +#else
  7767. + local_irq_restore(flags);
  7768. +#endif
  7769. +}
  7770. +
  7771. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  7772. +{
  7773. + struct mutex *m;
  7774. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  7775. +
  7776. + if (!mutex) {
  7777. + DWC_ERROR("Cannot allocate memory for mutex\n");
  7778. + return NULL;
  7779. + }
  7780. +
  7781. + m = (struct mutex *)mutex;
  7782. + mutex_init(m);
  7783. + return mutex;
  7784. +}
  7785. +
  7786. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  7787. +#else
  7788. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  7789. +{
  7790. + mutex_destroy((struct mutex *)mutex);
  7791. + DWC_FREE(mutex);
  7792. +}
  7793. +#endif
  7794. +
  7795. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  7796. +{
  7797. + struct mutex *m = (struct mutex *)mutex;
  7798. + mutex_lock(m);
  7799. +}
  7800. +
  7801. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  7802. +{
  7803. + struct mutex *m = (struct mutex *)mutex;
  7804. + return mutex_trylock(m);
  7805. +}
  7806. +
  7807. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  7808. +{
  7809. + struct mutex *m = (struct mutex *)mutex;
  7810. + mutex_unlock(m);
  7811. +}
  7812. +
  7813. +
  7814. +/* Timing */
  7815. +
  7816. +void DWC_UDELAY(uint32_t usecs)
  7817. +{
  7818. + udelay(usecs);
  7819. +}
  7820. +
  7821. +void DWC_MDELAY(uint32_t msecs)
  7822. +{
  7823. + mdelay(msecs);
  7824. +}
  7825. +
  7826. +void DWC_MSLEEP(uint32_t msecs)
  7827. +{
  7828. + msleep(msecs);
  7829. +}
  7830. +
  7831. +uint32_t DWC_TIME(void)
  7832. +{
  7833. + return jiffies_to_msecs(jiffies);
  7834. +}
  7835. +
  7836. +
  7837. +/* Timers */
  7838. +
  7839. +struct dwc_timer {
  7840. + struct timer_list *t;
  7841. + char *name;
  7842. + dwc_timer_callback_t cb;
  7843. + void *data;
  7844. + uint8_t scheduled;
  7845. + dwc_spinlock_t *lock;
  7846. +};
  7847. +
  7848. +static void timer_callback(unsigned long data)
  7849. +{
  7850. + dwc_timer_t *timer = (dwc_timer_t *)data;
  7851. + dwc_irqflags_t flags;
  7852. +
  7853. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  7854. + timer->scheduled = 0;
  7855. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  7856. + DWC_DEBUGC("Timer %s callback", timer->name);
  7857. + timer->cb(timer->data);
  7858. +}
  7859. +
  7860. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  7861. +{
  7862. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  7863. +
  7864. + if (!t) {
  7865. + DWC_ERROR("Cannot allocate memory for timer");
  7866. + return NULL;
  7867. + }
  7868. +
  7869. + t->t = DWC_ALLOC(sizeof(*t->t));
  7870. + if (!t->t) {
  7871. + DWC_ERROR("Cannot allocate memory for timer->t");
  7872. + goto no_timer;
  7873. + }
  7874. +
  7875. + t->name = DWC_STRDUP(name);
  7876. + if (!t->name) {
  7877. + DWC_ERROR("Cannot allocate memory for timer->name");
  7878. + goto no_name;
  7879. + }
  7880. +
  7881. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  7882. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
  7883. +#else
  7884. + t->lock = DWC_SPINLOCK_ALLOC();
  7885. +#endif
  7886. + if (!t->lock) {
  7887. + DWC_ERROR("Cannot allocate memory for lock");
  7888. + goto no_lock;
  7889. + }
  7890. +
  7891. + t->scheduled = 0;
  7892. + t->t->expires = jiffies;
  7893. + setup_timer(t->t, timer_callback, (unsigned long)t);
  7894. +
  7895. + t->cb = cb;
  7896. + t->data = data;
  7897. +
  7898. + return t;
  7899. +
  7900. + no_lock:
  7901. + DWC_FREE(t->name);
  7902. + no_name:
  7903. + DWC_FREE(t->t);
  7904. + no_timer:
  7905. + DWC_FREE(t);
  7906. + return NULL;
  7907. +}
  7908. +
  7909. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  7910. +{
  7911. + dwc_irqflags_t flags;
  7912. +
  7913. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  7914. +
  7915. + if (timer->scheduled) {
  7916. + del_timer(timer->t);
  7917. + timer->scheduled = 0;
  7918. + }
  7919. +
  7920. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  7921. + DWC_SPINLOCK_FREE(timer->lock);
  7922. + DWC_FREE(timer->t);
  7923. + DWC_FREE(timer->name);
  7924. + DWC_FREE(timer);
  7925. +}
  7926. +
  7927. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  7928. +{
  7929. + dwc_irqflags_t flags;
  7930. +
  7931. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  7932. +
  7933. + if (!timer->scheduled) {
  7934. + timer->scheduled = 1;
  7935. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  7936. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  7937. + add_timer(timer->t);
  7938. + } else {
  7939. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  7940. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  7941. + }
  7942. +
  7943. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  7944. +}
  7945. +
  7946. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  7947. +{
  7948. + del_timer(timer->t);
  7949. +}
  7950. +
  7951. +
  7952. +/* Wait Queues */
  7953. +
  7954. +struct dwc_waitq {
  7955. + wait_queue_head_t queue;
  7956. + int abort;
  7957. +};
  7958. +
  7959. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  7960. +{
  7961. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  7962. +
  7963. + if (!wq) {
  7964. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  7965. + return NULL;
  7966. + }
  7967. +
  7968. + init_waitqueue_head(&wq->queue);
  7969. + wq->abort = 0;
  7970. + return wq;
  7971. +}
  7972. +
  7973. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  7974. +{
  7975. + DWC_FREE(wq);
  7976. +}
  7977. +
  7978. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  7979. +{
  7980. + int result = wait_event_interruptible(wq->queue,
  7981. + cond(data) || wq->abort);
  7982. + if (result == -ERESTARTSYS) {
  7983. + wq->abort = 0;
  7984. + return -DWC_E_RESTART;
  7985. + }
  7986. +
  7987. + if (wq->abort == 1) {
  7988. + wq->abort = 0;
  7989. + return -DWC_E_ABORT;
  7990. + }
  7991. +
  7992. + wq->abort = 0;
  7993. +
  7994. + if (result == 0) {
  7995. + return 0;
  7996. + }
  7997. +
  7998. + return -DWC_E_UNKNOWN;
  7999. +}
  8000. +
  8001. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  8002. + void *data, int32_t msecs)
  8003. +{
  8004. + int32_t tmsecs;
  8005. + int result = wait_event_interruptible_timeout(wq->queue,
  8006. + cond(data) || wq->abort,
  8007. + msecs_to_jiffies(msecs));
  8008. + if (result == -ERESTARTSYS) {
  8009. + wq->abort = 0;
  8010. + return -DWC_E_RESTART;
  8011. + }
  8012. +
  8013. + if (wq->abort == 1) {
  8014. + wq->abort = 0;
  8015. + return -DWC_E_ABORT;
  8016. + }
  8017. +
  8018. + wq->abort = 0;
  8019. +
  8020. + if (result > 0) {
  8021. + tmsecs = jiffies_to_msecs(result);
  8022. + if (!tmsecs) {
  8023. + return 1;
  8024. + }
  8025. +
  8026. + return tmsecs;
  8027. + }
  8028. +
  8029. + if (result == 0) {
  8030. + return -DWC_E_TIMEOUT;
  8031. + }
  8032. +
  8033. + return -DWC_E_UNKNOWN;
  8034. +}
  8035. +
  8036. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  8037. +{
  8038. + wq->abort = 0;
  8039. + wake_up_interruptible(&wq->queue);
  8040. +}
  8041. +
  8042. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  8043. +{
  8044. + wq->abort = 1;
  8045. + wake_up_interruptible(&wq->queue);
  8046. +}
  8047. +
  8048. +
  8049. +/* Threading */
  8050. +
  8051. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  8052. +{
  8053. + struct task_struct *thread = kthread_run(func, data, name);
  8054. +
  8055. + if (thread == ERR_PTR(-ENOMEM)) {
  8056. + return NULL;
  8057. + }
  8058. +
  8059. + return (dwc_thread_t *)thread;
  8060. +}
  8061. +
  8062. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  8063. +{
  8064. + return kthread_stop((struct task_struct *)thread);
  8065. +}
  8066. +
  8067. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  8068. +{
  8069. + return kthread_should_stop();
  8070. +}
  8071. +
  8072. +
  8073. +/* tasklets
  8074. + - run in interrupt context (cannot sleep)
  8075. + - each tasklet runs on a single CPU
  8076. + - different tasklets can be running simultaneously on different CPUs
  8077. + */
  8078. +struct dwc_tasklet {
  8079. + struct tasklet_struct t;
  8080. + dwc_tasklet_callback_t cb;
  8081. + void *data;
  8082. +};
  8083. +
  8084. +static void tasklet_callback(unsigned long data)
  8085. +{
  8086. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  8087. + t->cb(t->data);
  8088. +}
  8089. +
  8090. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  8091. +{
  8092. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  8093. +
  8094. + if (t) {
  8095. + t->cb = cb;
  8096. + t->data = data;
  8097. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  8098. + } else {
  8099. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  8100. + }
  8101. +
  8102. + return t;
  8103. +}
  8104. +
  8105. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  8106. +{
  8107. + DWC_FREE(task);
  8108. +}
  8109. +
  8110. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  8111. +{
  8112. + tasklet_schedule(&task->t);
  8113. +}
  8114. +
  8115. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  8116. +{
  8117. + tasklet_hi_schedule(&task->t);
  8118. +}
  8119. +
  8120. +
  8121. +/* workqueues
  8122. + - run in process context (can sleep)
  8123. + */
  8124. +typedef struct work_container {
  8125. + dwc_work_callback_t cb;
  8126. + void *data;
  8127. + dwc_workq_t *wq;
  8128. + char *name;
  8129. +
  8130. +#ifdef DEBUG
  8131. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  8132. +#endif
  8133. + struct delayed_work work;
  8134. +} work_container_t;
  8135. +
  8136. +#ifdef DEBUG
  8137. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  8138. +#endif
  8139. +
  8140. +struct dwc_workq {
  8141. + struct workqueue_struct *wq;
  8142. + dwc_spinlock_t *lock;
  8143. + dwc_waitq_t *waitq;
  8144. + int pending;
  8145. +
  8146. +#ifdef DEBUG
  8147. + struct work_container_queue entries;
  8148. +#endif
  8149. +};
  8150. +
  8151. +static void do_work(struct work_struct *work)
  8152. +{
  8153. + dwc_irqflags_t flags;
  8154. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  8155. + work_container_t *container = container_of(dw, struct work_container, work);
  8156. + dwc_workq_t *wq = container->wq;
  8157. +
  8158. + container->cb(container->data);
  8159. +
  8160. +#ifdef DEBUG
  8161. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  8162. +#endif
  8163. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  8164. + if (container->name) {
  8165. + DWC_FREE(container->name);
  8166. + }
  8167. + DWC_FREE(container);
  8168. +
  8169. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8170. + wq->pending--;
  8171. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8172. + DWC_WAITQ_TRIGGER(wq->waitq);
  8173. +}
  8174. +
  8175. +static int work_done(void *data)
  8176. +{
  8177. + dwc_workq_t *workq = (dwc_workq_t *)data;
  8178. + return workq->pending == 0;
  8179. +}
  8180. +
  8181. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  8182. +{
  8183. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  8184. +}
  8185. +
  8186. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  8187. +{
  8188. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  8189. +
  8190. + if (!wq) {
  8191. + return NULL;
  8192. + }
  8193. +
  8194. + wq->wq = create_singlethread_workqueue(name);
  8195. + if (!wq->wq) {
  8196. + goto no_wq;
  8197. + }
  8198. +
  8199. + wq->pending = 0;
  8200. +
  8201. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  8202. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
  8203. +#else
  8204. + wq->lock = DWC_SPINLOCK_ALLOC();
  8205. +#endif
  8206. + if (!wq->lock) {
  8207. + goto no_lock;
  8208. + }
  8209. +
  8210. + wq->waitq = DWC_WAITQ_ALLOC();
  8211. + if (!wq->waitq) {
  8212. + goto no_waitq;
  8213. + }
  8214. +
  8215. +#ifdef DEBUG
  8216. + DWC_CIRCLEQ_INIT(&wq->entries);
  8217. +#endif
  8218. + return wq;
  8219. +
  8220. + no_waitq:
  8221. + DWC_SPINLOCK_FREE(wq->lock);
  8222. + no_lock:
  8223. + destroy_workqueue(wq->wq);
  8224. + no_wq:
  8225. + DWC_FREE(wq);
  8226. +
  8227. + return NULL;
  8228. +}
  8229. +
  8230. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  8231. +{
  8232. +#ifdef DEBUG
  8233. + if (wq->pending != 0) {
  8234. + struct work_container *wc;
  8235. + DWC_ERROR("Destroying work queue with pending work");
  8236. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  8237. + DWC_ERROR("Work %s still pending", wc->name);
  8238. + }
  8239. + }
  8240. +#endif
  8241. + destroy_workqueue(wq->wq);
  8242. + DWC_SPINLOCK_FREE(wq->lock);
  8243. + DWC_WAITQ_FREE(wq->waitq);
  8244. + DWC_FREE(wq);
  8245. +}
  8246. +
  8247. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  8248. + char *format, ...)
  8249. +{
  8250. + dwc_irqflags_t flags;
  8251. + work_container_t *container;
  8252. + static char name[128];
  8253. + va_list args;
  8254. +
  8255. + va_start(args, format);
  8256. + DWC_VSNPRINTF(name, 128, format, args);
  8257. + va_end(args);
  8258. +
  8259. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8260. + wq->pending++;
  8261. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8262. + DWC_WAITQ_TRIGGER(wq->waitq);
  8263. +
  8264. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  8265. + if (!container) {
  8266. + DWC_ERROR("Cannot allocate memory for container\n");
  8267. + return;
  8268. + }
  8269. +
  8270. + container->name = DWC_STRDUP(name);
  8271. + if (!container->name) {
  8272. + DWC_ERROR("Cannot allocate memory for container->name\n");
  8273. + DWC_FREE(container);
  8274. + return;
  8275. + }
  8276. +
  8277. + container->cb = cb;
  8278. + container->data = data;
  8279. + container->wq = wq;
  8280. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  8281. + INIT_WORK(&container->work.work, do_work);
  8282. +
  8283. +#ifdef DEBUG
  8284. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  8285. +#endif
  8286. + queue_work(wq->wq, &container->work.work);
  8287. +}
  8288. +
  8289. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  8290. + void *data, uint32_t time, char *format, ...)
  8291. +{
  8292. + dwc_irqflags_t flags;
  8293. + work_container_t *container;
  8294. + static char name[128];
  8295. + va_list args;
  8296. +
  8297. + va_start(args, format);
  8298. + DWC_VSNPRINTF(name, 128, format, args);
  8299. + va_end(args);
  8300. +
  8301. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8302. + wq->pending++;
  8303. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8304. + DWC_WAITQ_TRIGGER(wq->waitq);
  8305. +
  8306. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  8307. + if (!container) {
  8308. + DWC_ERROR("Cannot allocate memory for container\n");
  8309. + return;
  8310. + }
  8311. +
  8312. + container->name = DWC_STRDUP(name);
  8313. + if (!container->name) {
  8314. + DWC_ERROR("Cannot allocate memory for container->name\n");
  8315. + DWC_FREE(container);
  8316. + return;
  8317. + }
  8318. +
  8319. + container->cb = cb;
  8320. + container->data = data;
  8321. + container->wq = wq;
  8322. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  8323. + INIT_DELAYED_WORK(&container->work, do_work);
  8324. +
  8325. +#ifdef DEBUG
  8326. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  8327. +#endif
  8328. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  8329. +}
  8330. +
  8331. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  8332. +{
  8333. + return wq->pending;
  8334. +}
  8335. +
  8336. +
  8337. +#ifdef DWC_LIBMODULE
  8338. +
  8339. +#ifdef DWC_CCLIB
  8340. +/* CC */
  8341. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  8342. +EXPORT_SYMBOL(dwc_cc_if_free);
  8343. +EXPORT_SYMBOL(dwc_cc_clear);
  8344. +EXPORT_SYMBOL(dwc_cc_add);
  8345. +EXPORT_SYMBOL(dwc_cc_remove);
  8346. +EXPORT_SYMBOL(dwc_cc_change);
  8347. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  8348. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  8349. +EXPORT_SYMBOL(dwc_cc_match_chid);
  8350. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  8351. +EXPORT_SYMBOL(dwc_cc_ck);
  8352. +EXPORT_SYMBOL(dwc_cc_chid);
  8353. +EXPORT_SYMBOL(dwc_cc_cdid);
  8354. +EXPORT_SYMBOL(dwc_cc_name);
  8355. +#endif /* DWC_CCLIB */
  8356. +
  8357. +#ifdef DWC_CRYPTOLIB
  8358. +# ifndef CONFIG_MACH_IPMATE
  8359. +/* Modpow */
  8360. +EXPORT_SYMBOL(dwc_modpow);
  8361. +
  8362. +/* DH */
  8363. +EXPORT_SYMBOL(dwc_dh_modpow);
  8364. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  8365. +EXPORT_SYMBOL(dwc_dh_pk);
  8366. +# endif /* CONFIG_MACH_IPMATE */
  8367. +
  8368. +/* Crypto */
  8369. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  8370. +EXPORT_SYMBOL(dwc_wusb_cmf);
  8371. +EXPORT_SYMBOL(dwc_wusb_prf);
  8372. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  8373. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  8374. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  8375. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  8376. +#endif /* DWC_CRYPTOLIB */
  8377. +
  8378. +/* Notification */
  8379. +#ifdef DWC_NOTIFYLIB
  8380. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  8381. +EXPORT_SYMBOL(dwc_free_notification_manager);
  8382. +EXPORT_SYMBOL(dwc_register_notifier);
  8383. +EXPORT_SYMBOL(dwc_unregister_notifier);
  8384. +EXPORT_SYMBOL(dwc_add_observer);
  8385. +EXPORT_SYMBOL(dwc_remove_observer);
  8386. +EXPORT_SYMBOL(dwc_notify);
  8387. +#endif
  8388. +
  8389. +/* Memory Debugging Routines */
  8390. +#ifdef DWC_DEBUG_MEMORY
  8391. +EXPORT_SYMBOL(dwc_alloc_debug);
  8392. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  8393. +EXPORT_SYMBOL(dwc_free_debug);
  8394. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  8395. +EXPORT_SYMBOL(dwc_dma_free_debug);
  8396. +#endif
  8397. +
  8398. +EXPORT_SYMBOL(DWC_MEMSET);
  8399. +EXPORT_SYMBOL(DWC_MEMCPY);
  8400. +EXPORT_SYMBOL(DWC_MEMMOVE);
  8401. +EXPORT_SYMBOL(DWC_MEMCMP);
  8402. +EXPORT_SYMBOL(DWC_STRNCMP);
  8403. +EXPORT_SYMBOL(DWC_STRCMP);
  8404. +EXPORT_SYMBOL(DWC_STRLEN);
  8405. +EXPORT_SYMBOL(DWC_STRCPY);
  8406. +EXPORT_SYMBOL(DWC_STRDUP);
  8407. +EXPORT_SYMBOL(DWC_ATOI);
  8408. +EXPORT_SYMBOL(DWC_ATOUI);
  8409. +
  8410. +#ifdef DWC_UTFLIB
  8411. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  8412. +#endif /* DWC_UTFLIB */
  8413. +
  8414. +EXPORT_SYMBOL(DWC_IN_IRQ);
  8415. +EXPORT_SYMBOL(DWC_IN_BH);
  8416. +EXPORT_SYMBOL(DWC_VPRINTF);
  8417. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  8418. +EXPORT_SYMBOL(DWC_PRINTF);
  8419. +EXPORT_SYMBOL(DWC_SPRINTF);
  8420. +EXPORT_SYMBOL(DWC_SNPRINTF);
  8421. +EXPORT_SYMBOL(__DWC_WARN);
  8422. +EXPORT_SYMBOL(__DWC_ERROR);
  8423. +EXPORT_SYMBOL(DWC_EXCEPTION);
  8424. +
  8425. +#ifdef DEBUG
  8426. +EXPORT_SYMBOL(__DWC_DEBUG);
  8427. +#endif
  8428. +
  8429. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  8430. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  8431. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  8432. +EXPORT_SYMBOL(__DWC_ALLOC);
  8433. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  8434. +EXPORT_SYMBOL(__DWC_FREE);
  8435. +
  8436. +#ifdef DWC_CRYPTOLIB
  8437. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  8438. +EXPORT_SYMBOL(DWC_AES_CBC);
  8439. +EXPORT_SYMBOL(DWC_SHA256);
  8440. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  8441. +#endif
  8442. +
  8443. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  8444. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  8445. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  8446. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  8447. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  8448. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  8449. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  8450. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  8451. +EXPORT_SYMBOL(DWC_READ_REG32);
  8452. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  8453. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  8454. +
  8455. +#if 0
  8456. +EXPORT_SYMBOL(DWC_READ_REG64);
  8457. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  8458. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  8459. +#endif
  8460. +
  8461. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  8462. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  8463. +EXPORT_SYMBOL(DWC_SPINLOCK);
  8464. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  8465. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  8466. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  8467. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  8468. +
  8469. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  8470. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  8471. +#endif
  8472. +
  8473. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  8474. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  8475. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  8476. +EXPORT_SYMBOL(DWC_UDELAY);
  8477. +EXPORT_SYMBOL(DWC_MDELAY);
  8478. +EXPORT_SYMBOL(DWC_MSLEEP);
  8479. +EXPORT_SYMBOL(DWC_TIME);
  8480. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  8481. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  8482. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  8483. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  8484. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  8485. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  8486. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  8487. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  8488. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  8489. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  8490. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  8491. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  8492. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  8493. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  8494. +EXPORT_SYMBOL(DWC_TASK_FREE);
  8495. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  8496. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  8497. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  8498. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  8499. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  8500. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  8501. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  8502. +
  8503. +static int dwc_common_port_init_module(void)
  8504. +{
  8505. + int result = 0;
  8506. +
  8507. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  8508. +
  8509. +#ifdef DWC_DEBUG_MEMORY
  8510. + result = dwc_memory_debug_start(NULL);
  8511. + if (result) {
  8512. + printk(KERN_ERR
  8513. + "dwc_memory_debug_start() failed with error %d\n",
  8514. + result);
  8515. + return result;
  8516. + }
  8517. +#endif
  8518. +
  8519. +#ifdef DWC_NOTIFYLIB
  8520. + result = dwc_alloc_notification_manager(NULL, NULL);
  8521. + if (result) {
  8522. + printk(KERN_ERR
  8523. + "dwc_alloc_notification_manager() failed with error %d\n",
  8524. + result);
  8525. + return result;
  8526. + }
  8527. +#endif
  8528. + return result;
  8529. +}
  8530. +
  8531. +static void dwc_common_port_exit_module(void)
  8532. +{
  8533. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  8534. +
  8535. +#ifdef DWC_NOTIFYLIB
  8536. + dwc_free_notification_manager();
  8537. +#endif
  8538. +
  8539. +#ifdef DWC_DEBUG_MEMORY
  8540. + dwc_memory_debug_stop();
  8541. +#endif
  8542. +}
  8543. +
  8544. +module_init(dwc_common_port_init_module);
  8545. +module_exit(dwc_common_port_exit_module);
  8546. +
  8547. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  8548. +MODULE_AUTHOR("Synopsys Inc.");
  8549. +MODULE_LICENSE ("GPL");
  8550. +
  8551. +#endif /* DWC_LIBMODULE */
  8552. --- /dev/null
  8553. +++ b/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  8554. @@ -0,0 +1,1275 @@
  8555. +#include "dwc_os.h"
  8556. +#include "dwc_list.h"
  8557. +
  8558. +#ifdef DWC_CCLIB
  8559. +# include "dwc_cc.h"
  8560. +#endif
  8561. +
  8562. +#ifdef DWC_CRYPTOLIB
  8563. +# include "dwc_modpow.h"
  8564. +# include "dwc_dh.h"
  8565. +# include "dwc_crypto.h"
  8566. +#endif
  8567. +
  8568. +#ifdef DWC_NOTIFYLIB
  8569. +# include "dwc_notifier.h"
  8570. +#endif
  8571. +
  8572. +/* OS-Level Implementations */
  8573. +
  8574. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  8575. +
  8576. +
  8577. +/* MISC */
  8578. +
  8579. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  8580. +{
  8581. + return memset(dest, byte, size);
  8582. +}
  8583. +
  8584. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  8585. +{
  8586. + return memcpy(dest, src, size);
  8587. +}
  8588. +
  8589. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  8590. +{
  8591. + bcopy(src, dest, size);
  8592. + return dest;
  8593. +}
  8594. +
  8595. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  8596. +{
  8597. + return memcmp(m1, m2, size);
  8598. +}
  8599. +
  8600. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  8601. +{
  8602. + return strncmp(s1, s2, size);
  8603. +}
  8604. +
  8605. +int DWC_STRCMP(void *s1, void *s2)
  8606. +{
  8607. + return strcmp(s1, s2);
  8608. +}
  8609. +
  8610. +int DWC_STRLEN(char const *str)
  8611. +{
  8612. + return strlen(str);
  8613. +}
  8614. +
  8615. +char *DWC_STRCPY(char *to, char const *from)
  8616. +{
  8617. + return strcpy(to, from);
  8618. +}
  8619. +
  8620. +char *DWC_STRDUP(char const *str)
  8621. +{
  8622. + int len = DWC_STRLEN(str) + 1;
  8623. + char *new = DWC_ALLOC_ATOMIC(len);
  8624. +
  8625. + if (!new) {
  8626. + return NULL;
  8627. + }
  8628. +
  8629. + DWC_MEMCPY(new, str, len);
  8630. + return new;
  8631. +}
  8632. +
  8633. +int DWC_ATOI(char *str, int32_t *value)
  8634. +{
  8635. + char *end = NULL;
  8636. +
  8637. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  8638. + * should be equivalent on 2's complement machines
  8639. + */
  8640. + *value = strtoul(str, &end, 0);
  8641. + if (*end == '\0') {
  8642. + return 0;
  8643. + }
  8644. +
  8645. + return -1;
  8646. +}
  8647. +
  8648. +int DWC_ATOUI(char *str, uint32_t *value)
  8649. +{
  8650. + char *end = NULL;
  8651. +
  8652. + *value = strtoul(str, &end, 0);
  8653. + if (*end == '\0') {
  8654. + return 0;
  8655. + }
  8656. +
  8657. + return -1;
  8658. +}
  8659. +
  8660. +
  8661. +#ifdef DWC_UTFLIB
  8662. +/* From usbstring.c */
  8663. +
  8664. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  8665. +{
  8666. + int count = 0;
  8667. + u8 c;
  8668. + u16 uchar;
  8669. +
  8670. + /* this insists on correct encodings, though not minimal ones.
  8671. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  8672. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  8673. + */
  8674. + while (len != 0 && (c = (u8) *s++) != 0) {
  8675. + if (unlikely(c & 0x80)) {
  8676. + // 2-byte sequence:
  8677. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  8678. + if ((c & 0xe0) == 0xc0) {
  8679. + uchar = (c & 0x1f) << 6;
  8680. +
  8681. + c = (u8) *s++;
  8682. + if ((c & 0xc0) != 0xc0)
  8683. + goto fail;
  8684. + c &= 0x3f;
  8685. + uchar |= c;
  8686. +
  8687. + // 3-byte sequence (most CJKV characters):
  8688. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  8689. + } else if ((c & 0xf0) == 0xe0) {
  8690. + uchar = (c & 0x0f) << 12;
  8691. +
  8692. + c = (u8) *s++;
  8693. + if ((c & 0xc0) != 0xc0)
  8694. + goto fail;
  8695. + c &= 0x3f;
  8696. + uchar |= c << 6;
  8697. +
  8698. + c = (u8) *s++;
  8699. + if ((c & 0xc0) != 0xc0)
  8700. + goto fail;
  8701. + c &= 0x3f;
  8702. + uchar |= c;
  8703. +
  8704. + /* no bogus surrogates */
  8705. + if (0xd800 <= uchar && uchar <= 0xdfff)
  8706. + goto fail;
  8707. +
  8708. + // 4-byte sequence (surrogate pairs, currently rare):
  8709. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  8710. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  8711. + // (uuuuu = wwww + 1)
  8712. + // FIXME accept the surrogate code points (only)
  8713. + } else
  8714. + goto fail;
  8715. + } else
  8716. + uchar = c;
  8717. + put_unaligned (cpu_to_le16 (uchar), cp++);
  8718. + count++;
  8719. + len--;
  8720. + }
  8721. + return count;
  8722. +fail:
  8723. + return -1;
  8724. +}
  8725. +
  8726. +#endif /* DWC_UTFLIB */
  8727. +
  8728. +
  8729. +/* dwc_debug.h */
  8730. +
  8731. +dwc_bool_t DWC_IN_IRQ(void)
  8732. +{
  8733. +// return in_irq();
  8734. + return 0;
  8735. +}
  8736. +
  8737. +dwc_bool_t DWC_IN_BH(void)
  8738. +{
  8739. +// return in_softirq();
  8740. + return 0;
  8741. +}
  8742. +
  8743. +void DWC_VPRINTF(char *format, va_list args)
  8744. +{
  8745. + vprintf(format, args);
  8746. +}
  8747. +
  8748. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  8749. +{
  8750. + return vsnprintf(str, size, format, args);
  8751. +}
  8752. +
  8753. +void DWC_PRINTF(char *format, ...)
  8754. +{
  8755. + va_list args;
  8756. +
  8757. + va_start(args, format);
  8758. + DWC_VPRINTF(format, args);
  8759. + va_end(args);
  8760. +}
  8761. +
  8762. +int DWC_SPRINTF(char *buffer, char *format, ...)
  8763. +{
  8764. + int retval;
  8765. + va_list args;
  8766. +
  8767. + va_start(args, format);
  8768. + retval = vsprintf(buffer, format, args);
  8769. + va_end(args);
  8770. + return retval;
  8771. +}
  8772. +
  8773. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  8774. +{
  8775. + int retval;
  8776. + va_list args;
  8777. +
  8778. + va_start(args, format);
  8779. + retval = vsnprintf(buffer, size, format, args);
  8780. + va_end(args);
  8781. + return retval;
  8782. +}
  8783. +
  8784. +void __DWC_WARN(char *format, ...)
  8785. +{
  8786. + va_list args;
  8787. +
  8788. + va_start(args, format);
  8789. + DWC_VPRINTF(format, args);
  8790. + va_end(args);
  8791. +}
  8792. +
  8793. +void __DWC_ERROR(char *format, ...)
  8794. +{
  8795. + va_list args;
  8796. +
  8797. + va_start(args, format);
  8798. + DWC_VPRINTF(format, args);
  8799. + va_end(args);
  8800. +}
  8801. +
  8802. +void DWC_EXCEPTION(char *format, ...)
  8803. +{
  8804. + va_list args;
  8805. +
  8806. + va_start(args, format);
  8807. + DWC_VPRINTF(format, args);
  8808. + va_end(args);
  8809. +// BUG_ON(1); ???
  8810. +}
  8811. +
  8812. +#ifdef DEBUG
  8813. +void __DWC_DEBUG(char *format, ...)
  8814. +{
  8815. + va_list args;
  8816. +
  8817. + va_start(args, format);
  8818. + DWC_VPRINTF(format, args);
  8819. + va_end(args);
  8820. +}
  8821. +#endif
  8822. +
  8823. +
  8824. +/* dwc_mem.h */
  8825. +
  8826. +#if 0
  8827. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  8828. + uint32_t align,
  8829. + uint32_t alloc)
  8830. +{
  8831. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  8832. + size, align, alloc);
  8833. + return (dwc_pool_t *)pool;
  8834. +}
  8835. +
  8836. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  8837. +{
  8838. + dma_pool_destroy((struct dma_pool *)pool);
  8839. +}
  8840. +
  8841. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  8842. +{
  8843. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  8844. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  8845. +}
  8846. +
  8847. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  8848. +{
  8849. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  8850. + memset(..);
  8851. +}
  8852. +
  8853. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  8854. +{
  8855. + dma_pool_free(pool, vaddr, daddr);
  8856. +}
  8857. +#endif
  8858. +
  8859. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  8860. +{
  8861. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  8862. + int error;
  8863. +
  8864. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  8865. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  8866. + &dma->nsegs, BUS_DMA_NOWAIT);
  8867. + if (error) {
  8868. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  8869. + (uintmax_t)size, error);
  8870. + goto fail_0;
  8871. + }
  8872. +
  8873. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  8874. + (caddr_t *)&dma->dma_vaddr,
  8875. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  8876. + if (error) {
  8877. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  8878. + goto fail_1;
  8879. + }
  8880. +
  8881. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  8882. + BUS_DMA_NOWAIT, &dma->dma_map);
  8883. + if (error) {
  8884. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  8885. + goto fail_2;
  8886. + }
  8887. +
  8888. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  8889. + size, NULL, BUS_DMA_NOWAIT);
  8890. + if (error) {
  8891. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  8892. + goto fail_3;
  8893. + }
  8894. +
  8895. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  8896. + *dma_addr = dma->dma_paddr;
  8897. + return dma->dma_vaddr;
  8898. +
  8899. +fail_3:
  8900. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  8901. +fail_2:
  8902. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  8903. +fail_1:
  8904. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  8905. +fail_0:
  8906. + dma->dma_map = NULL;
  8907. + dma->dma_vaddr = NULL;
  8908. + dma->nsegs = 0;
  8909. +
  8910. + return NULL;
  8911. +}
  8912. +
  8913. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  8914. +{
  8915. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  8916. +
  8917. + if (dma->dma_map != NULL) {
  8918. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  8919. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  8920. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  8921. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  8922. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  8923. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  8924. + dma->dma_paddr = 0;
  8925. + dma->dma_map = NULL;
  8926. + dma->dma_vaddr = NULL;
  8927. + dma->nsegs = 0;
  8928. + }
  8929. +}
  8930. +
  8931. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  8932. +{
  8933. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  8934. +}
  8935. +
  8936. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  8937. +{
  8938. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  8939. +}
  8940. +
  8941. +void __DWC_FREE(void *mem_ctx, void *addr)
  8942. +{
  8943. + free(addr, M_DEVBUF);
  8944. +}
  8945. +
  8946. +
  8947. +#ifdef DWC_CRYPTOLIB
  8948. +/* dwc_crypto.h */
  8949. +
  8950. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  8951. +{
  8952. + get_random_bytes(buffer, length);
  8953. +}
  8954. +
  8955. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  8956. +{
  8957. + struct crypto_blkcipher *tfm;
  8958. + struct blkcipher_desc desc;
  8959. + struct scatterlist sgd;
  8960. + struct scatterlist sgs;
  8961. +
  8962. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  8963. + if (tfm == NULL) {
  8964. + printk("failed to load transform for aes CBC\n");
  8965. + return -1;
  8966. + }
  8967. +
  8968. + crypto_blkcipher_setkey(tfm, key, keylen);
  8969. + crypto_blkcipher_set_iv(tfm, iv, 16);
  8970. +
  8971. + sg_init_one(&sgd, out, messagelen);
  8972. + sg_init_one(&sgs, message, messagelen);
  8973. +
  8974. + desc.tfm = tfm;
  8975. + desc.flags = 0;
  8976. +
  8977. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  8978. + crypto_free_blkcipher(tfm);
  8979. + DWC_ERROR("AES CBC encryption failed");
  8980. + return -1;
  8981. + }
  8982. +
  8983. + crypto_free_blkcipher(tfm);
  8984. + return 0;
  8985. +}
  8986. +
  8987. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  8988. +{
  8989. + struct crypto_hash *tfm;
  8990. + struct hash_desc desc;
  8991. + struct scatterlist sg;
  8992. +
  8993. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  8994. + if (IS_ERR(tfm)) {
  8995. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  8996. + return 0;
  8997. + }
  8998. + desc.tfm = tfm;
  8999. + desc.flags = 0;
  9000. +
  9001. + sg_init_one(&sg, message, len);
  9002. + crypto_hash_digest(&desc, &sg, len, out);
  9003. + crypto_free_hash(tfm);
  9004. +
  9005. + return 1;
  9006. +}
  9007. +
  9008. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  9009. + uint8_t *key, uint32_t keylen, uint8_t *out)
  9010. +{
  9011. + struct crypto_hash *tfm;
  9012. + struct hash_desc desc;
  9013. + struct scatterlist sg;
  9014. +
  9015. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  9016. + if (IS_ERR(tfm)) {
  9017. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  9018. + return 0;
  9019. + }
  9020. + desc.tfm = tfm;
  9021. + desc.flags = 0;
  9022. +
  9023. + sg_init_one(&sg, message, messagelen);
  9024. + crypto_hash_setkey(tfm, key, keylen);
  9025. + crypto_hash_digest(&desc, &sg, messagelen, out);
  9026. + crypto_free_hash(tfm);
  9027. +
  9028. + return 1;
  9029. +}
  9030. +
  9031. +#endif /* DWC_CRYPTOLIB */
  9032. +
  9033. +
  9034. +/* Byte Ordering Conversions */
  9035. +
  9036. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  9037. +{
  9038. +#ifdef __LITTLE_ENDIAN
  9039. + return *p;
  9040. +#else
  9041. + uint8_t *u_p = (uint8_t *)p;
  9042. +
  9043. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9044. +#endif
  9045. +}
  9046. +
  9047. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  9048. +{
  9049. +#ifdef __BIG_ENDIAN
  9050. + return *p;
  9051. +#else
  9052. + uint8_t *u_p = (uint8_t *)p;
  9053. +
  9054. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9055. +#endif
  9056. +}
  9057. +
  9058. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  9059. +{
  9060. +#ifdef __LITTLE_ENDIAN
  9061. + return *p;
  9062. +#else
  9063. + uint8_t *u_p = (uint8_t *)p;
  9064. +
  9065. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9066. +#endif
  9067. +}
  9068. +
  9069. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  9070. +{
  9071. +#ifdef __BIG_ENDIAN
  9072. + return *p;
  9073. +#else
  9074. + uint8_t *u_p = (uint8_t *)p;
  9075. +
  9076. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9077. +#endif
  9078. +}
  9079. +
  9080. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  9081. +{
  9082. +#ifdef __LITTLE_ENDIAN
  9083. + return *p;
  9084. +#else
  9085. + uint8_t *u_p = (uint8_t *)p;
  9086. + return (u_p[1] | (u_p[0] << 8));
  9087. +#endif
  9088. +}
  9089. +
  9090. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  9091. +{
  9092. +#ifdef __BIG_ENDIAN
  9093. + return *p;
  9094. +#else
  9095. + uint8_t *u_p = (uint8_t *)p;
  9096. + return (u_p[1] | (u_p[0] << 8));
  9097. +#endif
  9098. +}
  9099. +
  9100. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  9101. +{
  9102. +#ifdef __LITTLE_ENDIAN
  9103. + return *p;
  9104. +#else
  9105. + uint8_t *u_p = (uint8_t *)p;
  9106. + return (u_p[1] | (u_p[0] << 8));
  9107. +#endif
  9108. +}
  9109. +
  9110. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  9111. +{
  9112. +#ifdef __BIG_ENDIAN
  9113. + return *p;
  9114. +#else
  9115. + uint8_t *u_p = (uint8_t *)p;
  9116. + return (u_p[1] | (u_p[0] << 8));
  9117. +#endif
  9118. +}
  9119. +
  9120. +
  9121. +/* Registers */
  9122. +
  9123. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  9124. +{
  9125. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9126. + bus_size_t ior = (bus_size_t)reg;
  9127. +
  9128. + return bus_space_read_4(io->iot, io->ioh, ior);
  9129. +}
  9130. +
  9131. +#if 0
  9132. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  9133. +{
  9134. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9135. + bus_size_t ior = (bus_size_t)reg;
  9136. +
  9137. + return bus_space_read_8(io->iot, io->ioh, ior);
  9138. +}
  9139. +#endif
  9140. +
  9141. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  9142. +{
  9143. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9144. + bus_size_t ior = (bus_size_t)reg;
  9145. +
  9146. + bus_space_write_4(io->iot, io->ioh, ior, value);
  9147. +}
  9148. +
  9149. +#if 0
  9150. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  9151. +{
  9152. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9153. + bus_size_t ior = (bus_size_t)reg;
  9154. +
  9155. + bus_space_write_8(io->iot, io->ioh, ior, value);
  9156. +}
  9157. +#endif
  9158. +
  9159. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  9160. + uint32_t set_mask)
  9161. +{
  9162. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9163. + bus_size_t ior = (bus_size_t)reg;
  9164. +
  9165. + bus_space_write_4(io->iot, io->ioh, ior,
  9166. + (bus_space_read_4(io->iot, io->ioh, ior) &
  9167. + ~clear_mask) | set_mask);
  9168. +}
  9169. +
  9170. +#if 0
  9171. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  9172. + uint64_t set_mask)
  9173. +{
  9174. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9175. + bus_size_t ior = (bus_size_t)reg;
  9176. +
  9177. + bus_space_write_8(io->iot, io->ioh, ior,
  9178. + (bus_space_read_8(io->iot, io->ioh, ior) &
  9179. + ~clear_mask) | set_mask);
  9180. +}
  9181. +#endif
  9182. +
  9183. +
  9184. +/* Locking */
  9185. +
  9186. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  9187. +{
  9188. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  9189. +
  9190. + if (!sl) {
  9191. + DWC_ERROR("Cannot allocate memory for spinlock");
  9192. + return NULL;
  9193. + }
  9194. +
  9195. + simple_lock_init(sl);
  9196. + return (dwc_spinlock_t *)sl;
  9197. +}
  9198. +
  9199. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  9200. +{
  9201. + struct simplelock *sl = (struct simplelock *)lock;
  9202. +
  9203. + DWC_FREE(sl);
  9204. +}
  9205. +
  9206. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  9207. +{
  9208. + simple_lock((struct simplelock *)lock);
  9209. +}
  9210. +
  9211. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  9212. +{
  9213. + simple_unlock((struct simplelock *)lock);
  9214. +}
  9215. +
  9216. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  9217. +{
  9218. + simple_lock((struct simplelock *)lock);
  9219. + *flags = splbio();
  9220. +}
  9221. +
  9222. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  9223. +{
  9224. + splx(flags);
  9225. + simple_unlock((struct simplelock *)lock);
  9226. +}
  9227. +
  9228. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  9229. +{
  9230. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  9231. +
  9232. + if (!mutex) {
  9233. + DWC_ERROR("Cannot allocate memory for mutex");
  9234. + return NULL;
  9235. + }
  9236. +
  9237. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  9238. + return mutex;
  9239. +}
  9240. +
  9241. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  9242. +#else
  9243. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  9244. +{
  9245. + DWC_FREE(mutex);
  9246. +}
  9247. +#endif
  9248. +
  9249. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  9250. +{
  9251. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  9252. +}
  9253. +
  9254. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  9255. +{
  9256. + int status;
  9257. +
  9258. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  9259. + return status == 0;
  9260. +}
  9261. +
  9262. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  9263. +{
  9264. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  9265. +}
  9266. +
  9267. +
  9268. +/* Timing */
  9269. +
  9270. +void DWC_UDELAY(uint32_t usecs)
  9271. +{
  9272. + DELAY(usecs);
  9273. +}
  9274. +
  9275. +void DWC_MDELAY(uint32_t msecs)
  9276. +{
  9277. + do {
  9278. + DELAY(1000);
  9279. + } while (--msecs);
  9280. +}
  9281. +
  9282. +void DWC_MSLEEP(uint32_t msecs)
  9283. +{
  9284. + struct timeval tv;
  9285. +
  9286. + tv.tv_sec = msecs / 1000;
  9287. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  9288. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  9289. +}
  9290. +
  9291. +uint32_t DWC_TIME(void)
  9292. +{
  9293. + struct timeval tv;
  9294. +
  9295. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  9296. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  9297. +}
  9298. +
  9299. +
  9300. +/* Timers */
  9301. +
  9302. +struct dwc_timer {
  9303. + struct callout t;
  9304. + char *name;
  9305. + dwc_spinlock_t *lock;
  9306. + dwc_timer_callback_t cb;
  9307. + void *data;
  9308. +};
  9309. +
  9310. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  9311. +{
  9312. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  9313. +
  9314. + if (!t) {
  9315. + DWC_ERROR("Cannot allocate memory for timer");
  9316. + return NULL;
  9317. + }
  9318. +
  9319. + callout_init(&t->t);
  9320. +
  9321. + t->name = DWC_STRDUP(name);
  9322. + if (!t->name) {
  9323. + DWC_ERROR("Cannot allocate memory for timer->name");
  9324. + goto no_name;
  9325. + }
  9326. +
  9327. + t->lock = DWC_SPINLOCK_ALLOC();
  9328. + if (!t->lock) {
  9329. + DWC_ERROR("Cannot allocate memory for timer->lock");
  9330. + goto no_lock;
  9331. + }
  9332. +
  9333. + t->cb = cb;
  9334. + t->data = data;
  9335. +
  9336. + return t;
  9337. +
  9338. + no_lock:
  9339. + DWC_FREE(t->name);
  9340. + no_name:
  9341. + DWC_FREE(t);
  9342. +
  9343. + return NULL;
  9344. +}
  9345. +
  9346. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  9347. +{
  9348. + callout_stop(&timer->t);
  9349. + DWC_SPINLOCK_FREE(timer->lock);
  9350. + DWC_FREE(timer->name);
  9351. + DWC_FREE(timer);
  9352. +}
  9353. +
  9354. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  9355. +{
  9356. + struct timeval tv;
  9357. +
  9358. + tv.tv_sec = time / 1000;
  9359. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  9360. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  9361. +}
  9362. +
  9363. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  9364. +{
  9365. + callout_stop(&timer->t);
  9366. +}
  9367. +
  9368. +
  9369. +/* Wait Queues */
  9370. +
  9371. +struct dwc_waitq {
  9372. + struct simplelock lock;
  9373. + int abort;
  9374. +};
  9375. +
  9376. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  9377. +{
  9378. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  9379. +
  9380. + if (!wq) {
  9381. + DWC_ERROR("Cannot allocate memory for waitqueue");
  9382. + return NULL;
  9383. + }
  9384. +
  9385. + simple_lock_init(&wq->lock);
  9386. + wq->abort = 0;
  9387. +
  9388. + return wq;
  9389. +}
  9390. +
  9391. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  9392. +{
  9393. + DWC_FREE(wq);
  9394. +}
  9395. +
  9396. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  9397. +{
  9398. + int ipl;
  9399. + int result = 0;
  9400. +
  9401. + simple_lock(&wq->lock);
  9402. + ipl = splbio();
  9403. +
  9404. + /* Skip the sleep if already aborted or triggered */
  9405. + if (!wq->abort && !cond(data)) {
  9406. + splx(ipl);
  9407. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  9408. + ipl = splbio();
  9409. + }
  9410. +
  9411. + if (result == 0) { // awoken
  9412. + if (wq->abort) {
  9413. + wq->abort = 0;
  9414. + result = -DWC_E_ABORT;
  9415. + } else {
  9416. + result = 0;
  9417. + }
  9418. +
  9419. + splx(ipl);
  9420. + simple_unlock(&wq->lock);
  9421. + } else {
  9422. + wq->abort = 0;
  9423. + splx(ipl);
  9424. + simple_unlock(&wq->lock);
  9425. +
  9426. + if (result == ERESTART) { // signaled - restart
  9427. + result = -DWC_E_RESTART;
  9428. + } else { // signaled - must be EINTR
  9429. + result = -DWC_E_ABORT;
  9430. + }
  9431. + }
  9432. +
  9433. + return result;
  9434. +}
  9435. +
  9436. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  9437. + void *data, int32_t msecs)
  9438. +{
  9439. + struct timeval tv, tv1, tv2;
  9440. + int ipl;
  9441. + int result = 0;
  9442. +
  9443. + tv.tv_sec = msecs / 1000;
  9444. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  9445. +
  9446. + simple_lock(&wq->lock);
  9447. + ipl = splbio();
  9448. +
  9449. + /* Skip the sleep if already aborted or triggered */
  9450. + if (!wq->abort && !cond(data)) {
  9451. + splx(ipl);
  9452. + getmicrouptime(&tv1);
  9453. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  9454. + getmicrouptime(&tv2);
  9455. + ipl = splbio();
  9456. + }
  9457. +
  9458. + if (result == 0) { // awoken
  9459. + if (wq->abort) {
  9460. + wq->abort = 0;
  9461. + splx(ipl);
  9462. + simple_unlock(&wq->lock);
  9463. + result = -DWC_E_ABORT;
  9464. + } else {
  9465. + splx(ipl);
  9466. + simple_unlock(&wq->lock);
  9467. +
  9468. + tv2.tv_usec -= tv1.tv_usec;
  9469. + if (tv2.tv_usec < 0) {
  9470. + tv2.tv_usec += 1000000;
  9471. + tv2.tv_sec--;
  9472. + }
  9473. +
  9474. + tv2.tv_sec -= tv1.tv_sec;
  9475. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  9476. + result = msecs - result;
  9477. + if (result <= 0)
  9478. + result = 1;
  9479. + }
  9480. + } else {
  9481. + wq->abort = 0;
  9482. + splx(ipl);
  9483. + simple_unlock(&wq->lock);
  9484. +
  9485. + if (result == ERESTART) { // signaled - restart
  9486. + result = -DWC_E_RESTART;
  9487. +
  9488. + } else if (result == EINTR) { // signaled - interrupt
  9489. + result = -DWC_E_ABORT;
  9490. +
  9491. + } else { // timed out
  9492. + result = -DWC_E_TIMEOUT;
  9493. + }
  9494. + }
  9495. +
  9496. + return result;
  9497. +}
  9498. +
  9499. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  9500. +{
  9501. + wakeup(wq);
  9502. +}
  9503. +
  9504. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  9505. +{
  9506. + int ipl;
  9507. +
  9508. + simple_lock(&wq->lock);
  9509. + ipl = splbio();
  9510. + wq->abort = 1;
  9511. + wakeup(wq);
  9512. + splx(ipl);
  9513. + simple_unlock(&wq->lock);
  9514. +}
  9515. +
  9516. +
  9517. +/* Threading */
  9518. +
  9519. +struct dwc_thread {
  9520. + struct proc *proc;
  9521. + int abort;
  9522. +};
  9523. +
  9524. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  9525. +{
  9526. + int retval;
  9527. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  9528. +
  9529. + if (!thread) {
  9530. + return NULL;
  9531. + }
  9532. +
  9533. + thread->abort = 0;
  9534. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  9535. + "%s", name);
  9536. + if (retval) {
  9537. + DWC_FREE(thread);
  9538. + return NULL;
  9539. + }
  9540. +
  9541. + return thread;
  9542. +}
  9543. +
  9544. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  9545. +{
  9546. + int retval;
  9547. +
  9548. + thread->abort = 1;
  9549. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  9550. +
  9551. + if (retval == 0) {
  9552. + /* DWC_THREAD_EXIT() will free the thread struct */
  9553. + return 0;
  9554. + }
  9555. +
  9556. + /* NOTE: We leak the thread struct if thread doesn't die */
  9557. +
  9558. + if (retval == EWOULDBLOCK) {
  9559. + return -DWC_E_TIMEOUT;
  9560. + }
  9561. +
  9562. + return -DWC_E_UNKNOWN;
  9563. +}
  9564. +
  9565. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  9566. +{
  9567. + return thread->abort;
  9568. +}
  9569. +
  9570. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  9571. +{
  9572. + wakeup(&thread->abort);
  9573. + DWC_FREE(thread);
  9574. + kthread_exit(0);
  9575. +}
  9576. +
  9577. +/* tasklets
  9578. + - Runs in interrupt context (cannot sleep)
  9579. + - Each tasklet runs on a single CPU
  9580. + - Different tasklets can be running simultaneously on different CPUs
  9581. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  9582. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  9583. + */
  9584. +struct dwc_tasklet {
  9585. + dwc_tasklet_callback_t cb;
  9586. + void *data;
  9587. +};
  9588. +
  9589. +static void tasklet_callback(void *data)
  9590. +{
  9591. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  9592. +
  9593. + task->cb(task->data);
  9594. +}
  9595. +
  9596. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  9597. +{
  9598. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  9599. +
  9600. + if (task) {
  9601. + task->cb = cb;
  9602. + task->data = data;
  9603. + } else {
  9604. + DWC_ERROR("Cannot allocate memory for tasklet");
  9605. + }
  9606. +
  9607. + return task;
  9608. +}
  9609. +
  9610. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  9611. +{
  9612. + DWC_FREE(task);
  9613. +}
  9614. +
  9615. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  9616. +{
  9617. + tasklet_callback(task);
  9618. +}
  9619. +
  9620. +
  9621. +/* workqueues
  9622. + - Runs in process context (can sleep)
  9623. + */
  9624. +typedef struct work_container {
  9625. + dwc_work_callback_t cb;
  9626. + void *data;
  9627. + dwc_workq_t *wq;
  9628. + char *name;
  9629. + int hz;
  9630. + struct work task;
  9631. +} work_container_t;
  9632. +
  9633. +struct dwc_workq {
  9634. + struct workqueue *taskq;
  9635. + dwc_spinlock_t *lock;
  9636. + dwc_waitq_t *waitq;
  9637. + int pending;
  9638. + struct work_container *container;
  9639. +};
  9640. +
  9641. +static void do_work(struct work *task, void *data)
  9642. +{
  9643. + dwc_workq_t *wq = (dwc_workq_t *)data;
  9644. + work_container_t *container = wq->container;
  9645. + dwc_irqflags_t flags;
  9646. +
  9647. + if (container->hz) {
  9648. + tsleep(container, 0, "dw3wrk", container->hz);
  9649. + }
  9650. +
  9651. + container->cb(container->data);
  9652. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  9653. +
  9654. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9655. + if (container->name)
  9656. + DWC_FREE(container->name);
  9657. + DWC_FREE(container);
  9658. + wq->pending--;
  9659. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9660. + DWC_WAITQ_TRIGGER(wq->waitq);
  9661. +}
  9662. +
  9663. +static int work_done(void *data)
  9664. +{
  9665. + dwc_workq_t *workq = (dwc_workq_t *)data;
  9666. +
  9667. + return workq->pending == 0;
  9668. +}
  9669. +
  9670. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  9671. +{
  9672. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  9673. +}
  9674. +
  9675. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  9676. +{
  9677. + int result;
  9678. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  9679. +
  9680. + if (!wq) {
  9681. + DWC_ERROR("Cannot allocate memory for workqueue");
  9682. + return NULL;
  9683. + }
  9684. +
  9685. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  9686. + IPL_BIO, 0);
  9687. + if (result) {
  9688. + DWC_ERROR("Cannot create workqueue");
  9689. + goto no_taskq;
  9690. + }
  9691. +
  9692. + wq->pending = 0;
  9693. +
  9694. + wq->lock = DWC_SPINLOCK_ALLOC();
  9695. + if (!wq->lock) {
  9696. + DWC_ERROR("Cannot allocate memory for spinlock");
  9697. + goto no_lock;
  9698. + }
  9699. +
  9700. + wq->waitq = DWC_WAITQ_ALLOC();
  9701. + if (!wq->waitq) {
  9702. + DWC_ERROR("Cannot allocate memory for waitqueue");
  9703. + goto no_waitq;
  9704. + }
  9705. +
  9706. + return wq;
  9707. +
  9708. + no_waitq:
  9709. + DWC_SPINLOCK_FREE(wq->lock);
  9710. + no_lock:
  9711. + workqueue_destroy(wq->taskq);
  9712. + no_taskq:
  9713. + DWC_FREE(wq);
  9714. +
  9715. + return NULL;
  9716. +}
  9717. +
  9718. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  9719. +{
  9720. +#ifdef DEBUG
  9721. + dwc_irqflags_t flags;
  9722. +
  9723. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9724. +
  9725. + if (wq->pending != 0) {
  9726. + struct work_container *container = wq->container;
  9727. +
  9728. + DWC_ERROR("Destroying work queue with pending work");
  9729. +
  9730. + if (container && container->name) {
  9731. + DWC_ERROR("Work %s still pending", container->name);
  9732. + }
  9733. + }
  9734. +
  9735. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9736. +#endif
  9737. + DWC_WAITQ_FREE(wq->waitq);
  9738. + DWC_SPINLOCK_FREE(wq->lock);
  9739. + workqueue_destroy(wq->taskq);
  9740. + DWC_FREE(wq);
  9741. +}
  9742. +
  9743. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  9744. + char *format, ...)
  9745. +{
  9746. + dwc_irqflags_t flags;
  9747. + work_container_t *container;
  9748. + static char name[128];
  9749. + va_list args;
  9750. +
  9751. + va_start(args, format);
  9752. + DWC_VSNPRINTF(name, 128, format, args);
  9753. + va_end(args);
  9754. +
  9755. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9756. + wq->pending++;
  9757. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9758. + DWC_WAITQ_TRIGGER(wq->waitq);
  9759. +
  9760. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  9761. + if (!container) {
  9762. + DWC_ERROR("Cannot allocate memory for container");
  9763. + return;
  9764. + }
  9765. +
  9766. + container->name = DWC_STRDUP(name);
  9767. + if (!container->name) {
  9768. + DWC_ERROR("Cannot allocate memory for container->name");
  9769. + DWC_FREE(container);
  9770. + return;
  9771. + }
  9772. +
  9773. + container->cb = cb;
  9774. + container->data = data;
  9775. + container->wq = wq;
  9776. + container->hz = 0;
  9777. + wq->container = container;
  9778. +
  9779. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  9780. + workqueue_enqueue(wq->taskq, &container->task);
  9781. +}
  9782. +
  9783. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  9784. + void *data, uint32_t time, char *format, ...)
  9785. +{
  9786. + dwc_irqflags_t flags;
  9787. + work_container_t *container;
  9788. + static char name[128];
  9789. + struct timeval tv;
  9790. + va_list args;
  9791. +
  9792. + va_start(args, format);
  9793. + DWC_VSNPRINTF(name, 128, format, args);
  9794. + va_end(args);
  9795. +
  9796. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9797. + wq->pending++;
  9798. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9799. + DWC_WAITQ_TRIGGER(wq->waitq);
  9800. +
  9801. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  9802. + if (!container) {
  9803. + DWC_ERROR("Cannot allocate memory for container");
  9804. + return;
  9805. + }
  9806. +
  9807. + container->name = DWC_STRDUP(name);
  9808. + if (!container->name) {
  9809. + DWC_ERROR("Cannot allocate memory for container->name");
  9810. + DWC_FREE(container);
  9811. + return;
  9812. + }
  9813. +
  9814. + container->cb = cb;
  9815. + container->data = data;
  9816. + container->wq = wq;
  9817. + tv.tv_sec = time / 1000;
  9818. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  9819. + container->hz = tvtohz(&tv);
  9820. + wq->container = container;
  9821. +
  9822. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  9823. + workqueue_enqueue(wq->taskq, &container->task);
  9824. +}
  9825. +
  9826. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  9827. +{
  9828. + return wq->pending;
  9829. +}
  9830. --- /dev/null
  9831. +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.c
  9832. @@ -0,0 +1,308 @@
  9833. +/* =========================================================================
  9834. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  9835. + * $Revision: #5 $
  9836. + * $Date: 2010/09/28 $
  9837. + * $Change: 1596182 $
  9838. + *
  9839. + * Synopsys Portability Library Software and documentation
  9840. + * (hereinafter, "Software") is an Unsupported proprietary work of
  9841. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  9842. + * between Synopsys and you.
  9843. + *
  9844. + * The Software IS NOT an item of Licensed Software or Licensed Product
  9845. + * under any End User Software License Agreement or Agreement for
  9846. + * Licensed Product with Synopsys or any supplement thereto. You are
  9847. + * permitted to use and redistribute this Software in source and binary
  9848. + * forms, with or without modification, provided that redistributions
  9849. + * of source code must retain this notice. You may not view, use,
  9850. + * disclose, copy or distribute this file or any information contained
  9851. + * herein except pursuant to this license grant from Synopsys. If you
  9852. + * do not agree with this notice, including the disclaimer below, then
  9853. + * you are not authorized to use the Software.
  9854. + *
  9855. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  9856. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9857. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  9858. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  9859. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9860. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9861. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9862. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  9863. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  9864. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  9865. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  9866. + * DAMAGE.
  9867. + * ========================================================================= */
  9868. +
  9869. +/** @file
  9870. + * This file contains the WUSB cryptographic routines.
  9871. + */
  9872. +
  9873. +#ifdef DWC_CRYPTOLIB
  9874. +
  9875. +#include "dwc_crypto.h"
  9876. +#include "usb.h"
  9877. +
  9878. +#ifdef DEBUG
  9879. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  9880. +{
  9881. + int i;
  9882. + DWC_PRINTF("%s: ", name);
  9883. + for (i=0; i<len; i++) {
  9884. + DWC_PRINTF("%02x ", bytes[i]);
  9885. + }
  9886. + DWC_PRINTF("\n");
  9887. +}
  9888. +#else
  9889. +#define dump_bytes(x...)
  9890. +#endif
  9891. +
  9892. +/* Display a block */
  9893. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  9894. +{
  9895. +#ifdef DWC_DEBUG_CRYPTO
  9896. + int i, blksize = 16;
  9897. +
  9898. + DWC_DEBUG("%s", prefix);
  9899. +
  9900. + if (suffix == NULL) {
  9901. + suffix = "\n";
  9902. + blksize = a;
  9903. + }
  9904. +
  9905. + for (i = 0; i < blksize; i++)
  9906. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  9907. + DWC_PRINT(suffix);
  9908. +#endif
  9909. +}
  9910. +
  9911. +/**
  9912. + * Encrypts an array of bytes using the AES encryption engine.
  9913. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  9914. + * in-place.
  9915. + *
  9916. + * @return 0 on success, negative error code on error.
  9917. + */
  9918. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  9919. +{
  9920. + u8 block_t[16];
  9921. + DWC_MEMSET(block_t, 0, 16);
  9922. +
  9923. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  9924. +}
  9925. +
  9926. +/**
  9927. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  9928. + * This function takes a data string and returns the encrypted CBC
  9929. + * Counter-mode MIC.
  9930. + *
  9931. + * @param key The 128-bit symmetric key.
  9932. + * @param nonce The CCM nonce.
  9933. + * @param label The unique 14-byte ASCII text label.
  9934. + * @param bytes The byte array to be encrypted.
  9935. + * @param len Length of the byte array.
  9936. + * @param result Byte array to receive the 8-byte encrypted MIC.
  9937. + */
  9938. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  9939. + char *label, u8 *bytes, int len, u8 *result)
  9940. +{
  9941. + u8 block_m[16];
  9942. + u8 block_x[16];
  9943. + u8 block_t[8];
  9944. + int idx, blkNum;
  9945. + u16 la = (u16)(len + 14);
  9946. +
  9947. + /* Set the AES-128 key */
  9948. + //dwc_aes_setkey(tfm, key, 16);
  9949. +
  9950. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  9951. + block_m[0] = 0x59;
  9952. + for (idx = 0; idx < 13; idx++)
  9953. + block_m[idx + 1] = nonce[idx];
  9954. + block_m[14] = 0;
  9955. + block_m[15] = 0;
  9956. +
  9957. + /* Produce the CBC IV */
  9958. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  9959. + show_block(block_m, "CBC IV in: ", "\n", 0);
  9960. + show_block(block_x, "CBC IV out:", "\n", 0);
  9961. +
  9962. + /* Fill block B1 from l(a) = Blen + 14, and A */
  9963. + block_x[0] ^= (u8)(la >> 8);
  9964. + block_x[1] ^= (u8)la;
  9965. + for (idx = 0; idx < 14; idx++)
  9966. + block_x[idx + 2] ^= label[idx];
  9967. + show_block(block_x, "After xor: ", "b1\n", 16);
  9968. +
  9969. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  9970. + show_block(block_x, "After AES: ", "b1\n", 16);
  9971. +
  9972. + idx = 0;
  9973. + blkNum = 0;
  9974. +
  9975. + /* Fill remaining blocks with B */
  9976. + while (len-- > 0) {
  9977. + block_x[idx] ^= *bytes++;
  9978. + if (++idx >= 16) {
  9979. + idx = 0;
  9980. + show_block(block_x, "After xor: ", "\n", blkNum);
  9981. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  9982. + show_block(block_x, "After AES: ", "\n", blkNum);
  9983. + blkNum++;
  9984. + }
  9985. + }
  9986. +
  9987. + /* Handle partial last block */
  9988. + if (idx > 0) {
  9989. + show_block(block_x, "After xor: ", "\n", blkNum);
  9990. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  9991. + show_block(block_x, "After AES: ", "\n", blkNum);
  9992. + }
  9993. +
  9994. + /* Save the MIC tag */
  9995. + DWC_MEMCPY(block_t, block_x, 8);
  9996. + show_block(block_t, "MIC tag : ", NULL, 8);
  9997. +
  9998. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  9999. + block_m[0] = 0x01;
  10000. + block_m[14] = 0;
  10001. + block_m[15] = 0;
  10002. +
  10003. + /* Encrypt the counter */
  10004. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  10005. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  10006. +
  10007. + /* XOR with MIC tag */
  10008. + for (idx = 0; idx < 8; idx++) {
  10009. + block_t[idx] ^= block_x[idx];
  10010. + }
  10011. +
  10012. + /* Return result to caller */
  10013. + DWC_MEMCPY(result, block_t, 8);
  10014. + show_block(result, "CCM-MIC : ", NULL, 8);
  10015. +
  10016. +}
  10017. +
  10018. +/**
  10019. + * The PRF function described in section 6.5 of the WUSB spec. This function
  10020. + * concatenates MIC values returned from dwc_cmf() to create a value of
  10021. + * the requested length.
  10022. + *
  10023. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  10024. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  10025. + * @param result Byte array to receive the result.
  10026. + */
  10027. +void dwc_wusb_prf(int prf_len, u8 *key,
  10028. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  10029. +{
  10030. + int i;
  10031. +
  10032. + nonce[0] = 0;
  10033. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  10034. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  10035. + result += 8;
  10036. + }
  10037. +}
  10038. +
  10039. +/**
  10040. + * Fills in CCM Nonce per the WUSB spec.
  10041. + *
  10042. + * @param[in] haddr Host address.
  10043. + * @param[in] daddr Device address.
  10044. + * @param[in] tkid Session Key(PTK) identifier.
  10045. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  10046. + */
  10047. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  10048. + uint8_t *nonce)
  10049. +{
  10050. +
  10051. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  10052. +
  10053. + DWC_MEMSET(&nonce[0], 0, 16);
  10054. +
  10055. + DWC_MEMCPY(&nonce[6], tkid, 3);
  10056. + nonce[9] = daddr & 0xFF;
  10057. + nonce[10] = (daddr >> 8) & 0xFF;
  10058. + nonce[11] = haddr & 0xFF;
  10059. + nonce[12] = (haddr >> 8) & 0xFF;
  10060. +
  10061. + dump_bytes("CCM nonce", nonce, 16);
  10062. +}
  10063. +
  10064. +/**
  10065. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  10066. + * Nonce.
  10067. + */
  10068. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  10069. +{
  10070. + uint8_t inonce[16];
  10071. + uint32_t temp[4];
  10072. +
  10073. + /* Fill in the Nonce */
  10074. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  10075. + inonce[9] = addr & 0xFF;
  10076. + inonce[10] = (addr >> 8) & 0xFF;
  10077. + inonce[11] = inonce[9];
  10078. + inonce[12] = inonce[10];
  10079. +
  10080. + /* Collect "randomness samples" */
  10081. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  10082. +
  10083. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  10084. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  10085. + nonce);
  10086. +}
  10087. +
  10088. +/**
  10089. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  10090. + * WUSB spec.
  10091. + *
  10092. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  10093. + * @param[in] mk Master Key to derive the session from
  10094. + * @param[in] hnonce Pointer to Host Nonce.
  10095. + * @param[in] dnonce Pointer to Device Nonce.
  10096. + * @param[out] kck Pointer to where the KCK output is to be written.
  10097. + * @param[out] ptk Pointer to where the PTK output is to be written.
  10098. + */
  10099. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  10100. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  10101. +{
  10102. + uint8_t idata[32];
  10103. + uint8_t odata[32];
  10104. +
  10105. + dump_bytes("ck", mk, 16);
  10106. + dump_bytes("hnonce", hnonce, 16);
  10107. + dump_bytes("dnonce", dnonce, 16);
  10108. +
  10109. + /* The data is the HNonce and DNonce concatenated */
  10110. + DWC_MEMCPY(&idata[0], hnonce, 16);
  10111. + DWC_MEMCPY(&idata[16], dnonce, 16);
  10112. +
  10113. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  10114. +
  10115. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  10116. + DWC_MEMCPY(kck, &odata[0], 16);
  10117. + DWC_MEMCPY(ptk, &odata[16], 16);
  10118. +
  10119. + dump_bytes("kck", kck, 16);
  10120. + dump_bytes("ptk", ptk, 16);
  10121. +}
  10122. +
  10123. +/**
  10124. + * Generates the Message Integrity Code over the Handshake data per the
  10125. + * WUSB spec.
  10126. + *
  10127. + * @param ccm_nonce Pointer to CCM Nonce.
  10128. + * @param kck Pointer to Key Confirmation Key.
  10129. + * @param data Pointer to Handshake data to be checked.
  10130. + * @param mic Pointer to where the MIC output is to be written.
  10131. + */
  10132. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  10133. + uint8_t *data, uint8_t *mic)
  10134. +{
  10135. +
  10136. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  10137. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  10138. +}
  10139. +
  10140. +#endif /* DWC_CRYPTOLIB */
  10141. --- /dev/null
  10142. +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.h
  10143. @@ -0,0 +1,111 @@
  10144. +/* =========================================================================
  10145. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  10146. + * $Revision: #3 $
  10147. + * $Date: 2010/09/28 $
  10148. + * $Change: 1596182 $
  10149. + *
  10150. + * Synopsys Portability Library Software and documentation
  10151. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10152. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10153. + * between Synopsys and you.
  10154. + *
  10155. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10156. + * under any End User Software License Agreement or Agreement for
  10157. + * Licensed Product with Synopsys or any supplement thereto. You are
  10158. + * permitted to use and redistribute this Software in source and binary
  10159. + * forms, with or without modification, provided that redistributions
  10160. + * of source code must retain this notice. You may not view, use,
  10161. + * disclose, copy or distribute this file or any information contained
  10162. + * herein except pursuant to this license grant from Synopsys. If you
  10163. + * do not agree with this notice, including the disclaimer below, then
  10164. + * you are not authorized to use the Software.
  10165. + *
  10166. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10167. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10168. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10169. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10170. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10171. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10172. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10173. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10174. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10175. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10176. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10177. + * DAMAGE.
  10178. + * ========================================================================= */
  10179. +
  10180. +#ifndef _DWC_CRYPTO_H_
  10181. +#define _DWC_CRYPTO_H_
  10182. +
  10183. +#ifdef __cplusplus
  10184. +extern "C" {
  10185. +#endif
  10186. +
  10187. +/** @file
  10188. + *
  10189. + * This file contains declarations for the WUSB Cryptographic routines as
  10190. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  10191. + * modules.
  10192. + */
  10193. +
  10194. +#include "dwc_os.h"
  10195. +
  10196. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  10197. +
  10198. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  10199. + char *label, u8 *bytes, int len, u8 *result);
  10200. +void dwc_wusb_prf(int prf_len, u8 *key,
  10201. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  10202. +
  10203. +/**
  10204. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  10205. + *
  10206. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10207. + */
  10208. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  10209. + char *label, u8 *bytes, int len, u8 *result)
  10210. +{
  10211. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  10212. +}
  10213. +
  10214. +/**
  10215. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  10216. + *
  10217. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10218. + */
  10219. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  10220. + char *label, u8 *bytes, int len, u8 *result)
  10221. +{
  10222. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  10223. +}
  10224. +
  10225. +/**
  10226. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  10227. + *
  10228. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10229. + */
  10230. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  10231. + char *label, u8 *bytes, int len, u8 *result)
  10232. +{
  10233. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  10234. +}
  10235. +
  10236. +
  10237. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  10238. + uint8_t *nonce);
  10239. +void dwc_wusb_gen_nonce(uint16_t addr,
  10240. + uint8_t *nonce);
  10241. +
  10242. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  10243. + uint8_t *hnonce, uint8_t *dnonce,
  10244. + uint8_t *kck, uint8_t *ptk);
  10245. +
  10246. +
  10247. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  10248. + *kck, uint8_t *data, uint8_t *mic);
  10249. +
  10250. +#ifdef __cplusplus
  10251. +}
  10252. +#endif
  10253. +
  10254. +#endif /* _DWC_CRYPTO_H_ */
  10255. --- /dev/null
  10256. +++ b/drivers/usb/host/dwc_common_port/dwc_dh.c
  10257. @@ -0,0 +1,291 @@
  10258. +/* =========================================================================
  10259. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  10260. + * $Revision: #3 $
  10261. + * $Date: 2010/09/28 $
  10262. + * $Change: 1596182 $
  10263. + *
  10264. + * Synopsys Portability Library Software and documentation
  10265. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10266. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10267. + * between Synopsys and you.
  10268. + *
  10269. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10270. + * under any End User Software License Agreement or Agreement for
  10271. + * Licensed Product with Synopsys or any supplement thereto. You are
  10272. + * permitted to use and redistribute this Software in source and binary
  10273. + * forms, with or without modification, provided that redistributions
  10274. + * of source code must retain this notice. You may not view, use,
  10275. + * disclose, copy or distribute this file or any information contained
  10276. + * herein except pursuant to this license grant from Synopsys. If you
  10277. + * do not agree with this notice, including the disclaimer below, then
  10278. + * you are not authorized to use the Software.
  10279. + *
  10280. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10281. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10282. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10283. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10284. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10285. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10286. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10287. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10288. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10289. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10290. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10291. + * DAMAGE.
  10292. + * ========================================================================= */
  10293. +#ifdef DWC_CRYPTOLIB
  10294. +
  10295. +#ifndef CONFIG_MACH_IPMATE
  10296. +
  10297. +#include "dwc_dh.h"
  10298. +#include "dwc_modpow.h"
  10299. +
  10300. +#ifdef DEBUG
  10301. +/* This function prints out a buffer in the format described in the Association
  10302. + * Model specification. */
  10303. +static void dh_dump(char *str, void *_num, int len)
  10304. +{
  10305. + uint8_t *num = _num;
  10306. + int i;
  10307. + DWC_PRINTF("%s\n", str);
  10308. + for (i = 0; i < len; i ++) {
  10309. + DWC_PRINTF("%02x", num[i]);
  10310. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  10311. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  10312. + }
  10313. +
  10314. + DWC_PRINTF("\n");
  10315. +}
  10316. +#else
  10317. +#define dh_dump(_x...) do {; } while(0)
  10318. +#endif
  10319. +
  10320. +/* Constant g value */
  10321. +static __u32 dh_g[] = {
  10322. + 0x02000000,
  10323. +};
  10324. +
  10325. +/* Constant p value */
  10326. +static __u32 dh_p[] = {
  10327. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  10328. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  10329. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  10330. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  10331. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  10332. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  10333. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  10334. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  10335. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  10336. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  10337. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  10338. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  10339. +};
  10340. +
  10341. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  10342. +{
  10343. + uint8_t *in = _in;
  10344. + uint8_t *out = _out;
  10345. + int i;
  10346. + for (i=0; i<len; i++) {
  10347. + out[i] = in[len-1-i];
  10348. + }
  10349. +}
  10350. +
  10351. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  10352. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  10353. + * of 4. */
  10354. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  10355. + void *exp, uint32_t exp_len,
  10356. + void *mod, uint32_t mod_len,
  10357. + void *out)
  10358. +{
  10359. + /* modpow() takes little endian numbers. AM uses big-endian. This
  10360. + * function swaps bytes of numbers before passing onto modpow. */
  10361. +
  10362. + int retval = 0;
  10363. + uint32_t *result;
  10364. +
  10365. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  10366. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  10367. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  10368. +
  10369. + dh_swap_bytes(num, &bignum_num[1], num_len);
  10370. + bignum_num[0] = num_len / 4;
  10371. +
  10372. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  10373. + bignum_exp[0] = exp_len / 4;
  10374. +
  10375. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  10376. + bignum_mod[0] = mod_len / 4;
  10377. +
  10378. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  10379. + if (!result) {
  10380. + retval = -1;
  10381. + goto dh_modpow_nomem;
  10382. + }
  10383. +
  10384. + dh_swap_bytes(&result[1], out, result[0] * 4);
  10385. + dwc_free(mem_ctx, result);
  10386. +
  10387. + dh_modpow_nomem:
  10388. + dwc_free(mem_ctx, bignum_num);
  10389. + dwc_free(mem_ctx, bignum_exp);
  10390. + dwc_free(mem_ctx, bignum_mod);
  10391. + return retval;
  10392. +}
  10393. +
  10394. +
  10395. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  10396. +{
  10397. + int retval;
  10398. + uint8_t m3[385];
  10399. +
  10400. +#ifndef DH_TEST_VECTORS
  10401. + DWC_RANDOM_BYTES(exp, 32);
  10402. +#endif
  10403. +
  10404. + /* Compute the pkd */
  10405. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  10406. + exp, 32,
  10407. + dh_p, 384, pk))) {
  10408. + return retval;
  10409. + }
  10410. +
  10411. + m3[384] = nd;
  10412. + DWC_MEMCPY(&m3[0], pk, 384);
  10413. + DWC_SHA256(m3, 385, hash);
  10414. +
  10415. + dh_dump("PK", pk, 384);
  10416. + dh_dump("SHA-256(M3)", hash, 32);
  10417. + return 0;
  10418. +}
  10419. +
  10420. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  10421. + uint8_t *exp, int is_host,
  10422. + char *dd, uint8_t *ck, uint8_t *kdk)
  10423. +{
  10424. + int retval;
  10425. + uint8_t mv[784];
  10426. + uint8_t sha_result[32];
  10427. + uint8_t dhkey[384];
  10428. + uint8_t shared_secret[384];
  10429. + char *message;
  10430. + uint32_t vd;
  10431. +
  10432. + uint8_t *pk;
  10433. +
  10434. + if (is_host) {
  10435. + pk = pkd;
  10436. + }
  10437. + else {
  10438. + pk = pkh;
  10439. + }
  10440. +
  10441. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  10442. + exp, 32,
  10443. + dh_p, 384, shared_secret))) {
  10444. + return retval;
  10445. + }
  10446. + dh_dump("Shared Secret", shared_secret, 384);
  10447. +
  10448. + DWC_SHA256(shared_secret, 384, dhkey);
  10449. + dh_dump("DHKEY", dhkey, 384);
  10450. +
  10451. + DWC_MEMCPY(&mv[0], pkd, 384);
  10452. + DWC_MEMCPY(&mv[384], pkh, 384);
  10453. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  10454. + dh_dump("MV", mv, 784);
  10455. +
  10456. + DWC_SHA256(mv, 784, sha_result);
  10457. + dh_dump("SHA-256(MV)", sha_result, 32);
  10458. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  10459. +
  10460. + dh_swap_bytes(sha_result, &vd, 4);
  10461. +#ifdef DEBUG
  10462. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  10463. +#endif
  10464. +
  10465. + switch (nd) {
  10466. + case 2:
  10467. + vd = vd % 100;
  10468. + DWC_SPRINTF(dd, "%02d", vd);
  10469. + break;
  10470. + case 3:
  10471. + vd = vd % 1000;
  10472. + DWC_SPRINTF(dd, "%03d", vd);
  10473. + break;
  10474. + case 4:
  10475. + vd = vd % 10000;
  10476. + DWC_SPRINTF(dd, "%04d", vd);
  10477. + break;
  10478. + }
  10479. +#ifdef DEBUG
  10480. + DWC_PRINTF("Display Digits: %s\n", dd);
  10481. +#endif
  10482. +
  10483. + message = "connection key";
  10484. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  10485. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  10486. + DWC_MEMCPY(ck, sha_result, 16);
  10487. +
  10488. + message = "key derivation key";
  10489. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  10490. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  10491. + DWC_MEMCPY(kdk, sha_result, 32);
  10492. +
  10493. + return 0;
  10494. +}
  10495. +
  10496. +
  10497. +#ifdef DH_TEST_VECTORS
  10498. +
  10499. +static __u8 dh_a[] = {
  10500. + 0x44, 0x00, 0x51, 0xd6,
  10501. + 0xf0, 0xb5, 0x5e, 0xa9,
  10502. + 0x67, 0xab, 0x31, 0xc6,
  10503. + 0x8a, 0x8b, 0x5e, 0x37,
  10504. + 0xd9, 0x10, 0xda, 0xe0,
  10505. + 0xe2, 0xd4, 0x59, 0xa4,
  10506. + 0x86, 0x45, 0x9c, 0xaa,
  10507. + 0xdf, 0x36, 0x75, 0x16,
  10508. +};
  10509. +
  10510. +static __u8 dh_b[] = {
  10511. + 0x5d, 0xae, 0xc7, 0x86,
  10512. + 0x79, 0x80, 0xa3, 0x24,
  10513. + 0x8c, 0xe3, 0x57, 0x8f,
  10514. + 0xc7, 0x5f, 0x1b, 0x0f,
  10515. + 0x2d, 0xf8, 0x9d, 0x30,
  10516. + 0x6f, 0xa4, 0x52, 0xcd,
  10517. + 0xe0, 0x7a, 0x04, 0x8a,
  10518. + 0xde, 0xd9, 0x26, 0x56,
  10519. +};
  10520. +
  10521. +void dwc_run_dh_test_vectors(void *mem_ctx)
  10522. +{
  10523. + uint8_t pkd[384];
  10524. + uint8_t pkh[384];
  10525. + uint8_t hashd[32];
  10526. + uint8_t hashh[32];
  10527. + uint8_t ck[16];
  10528. + uint8_t kdk[32];
  10529. + char dd[5];
  10530. +
  10531. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  10532. +
  10533. + /* compute the PKd and SHA-256(PKd || Nd) */
  10534. + DWC_PRINTF("Computing PKd\n");
  10535. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  10536. +
  10537. + /* compute the PKd and SHA-256(PKh || Nd) */
  10538. + DWC_PRINTF("Computing PKh\n");
  10539. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  10540. +
  10541. + /* compute the dhkey */
  10542. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  10543. +}
  10544. +#endif /* DH_TEST_VECTORS */
  10545. +
  10546. +#endif /* !CONFIG_MACH_IPMATE */
  10547. +
  10548. +#endif /* DWC_CRYPTOLIB */
  10549. --- /dev/null
  10550. +++ b/drivers/usb/host/dwc_common_port/dwc_dh.h
  10551. @@ -0,0 +1,106 @@
  10552. +/* =========================================================================
  10553. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  10554. + * $Revision: #4 $
  10555. + * $Date: 2010/09/28 $
  10556. + * $Change: 1596182 $
  10557. + *
  10558. + * Synopsys Portability Library Software and documentation
  10559. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10560. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10561. + * between Synopsys and you.
  10562. + *
  10563. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10564. + * under any End User Software License Agreement or Agreement for
  10565. + * Licensed Product with Synopsys or any supplement thereto. You are
  10566. + * permitted to use and redistribute this Software in source and binary
  10567. + * forms, with or without modification, provided that redistributions
  10568. + * of source code must retain this notice. You may not view, use,
  10569. + * disclose, copy or distribute this file or any information contained
  10570. + * herein except pursuant to this license grant from Synopsys. If you
  10571. + * do not agree with this notice, including the disclaimer below, then
  10572. + * you are not authorized to use the Software.
  10573. + *
  10574. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10575. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10576. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10577. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10578. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10579. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10580. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10581. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10582. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10583. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10584. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10585. + * DAMAGE.
  10586. + * ========================================================================= */
  10587. +#ifndef _DWC_DH_H_
  10588. +#define _DWC_DH_H_
  10589. +
  10590. +#ifdef __cplusplus
  10591. +extern "C" {
  10592. +#endif
  10593. +
  10594. +#include "dwc_os.h"
  10595. +
  10596. +/** @file
  10597. + *
  10598. + * This file defines the common functions on device and host for performing
  10599. + * numeric association as defined in the WUSB spec. They are only to be
  10600. + * used internally by the DWC UWB modules. */
  10601. +
  10602. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  10603. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  10604. + uint8_t *key, uint32_t keylen,
  10605. + uint8_t *out);
  10606. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  10607. + void *exp, uint32_t exp_len,
  10608. + void *mod, uint32_t mod_len,
  10609. + void *out);
  10610. +
  10611. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  10612. + *
  10613. + * PK = g^exp mod p.
  10614. + *
  10615. + * Input:
  10616. + * Nd = Number of digits on the device.
  10617. + *
  10618. + * Output:
  10619. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  10620. + * used as either A or B.
  10621. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  10622. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  10623. + */
  10624. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  10625. +
  10626. +/** Computes the DHKEY, and VD.
  10627. + *
  10628. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  10629. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  10630. + *
  10631. + * Input:
  10632. + * pkd = The PKD value.
  10633. + * pkh = The PKH value.
  10634. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  10635. + * is_host = Set to non zero if a WUSB host is calling this function.
  10636. + *
  10637. + * Output:
  10638. +
  10639. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  10640. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  10641. + * null termination character. This buffer can be used directly for display.
  10642. + * ck = A 16-byte buffer to be filled with the CK.
  10643. + * kdk = A 32-byte buffer to be filled with the KDK.
  10644. + */
  10645. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  10646. + uint8_t *exp, int is_host,
  10647. + char *dd, uint8_t *ck, uint8_t *kdk);
  10648. +
  10649. +#ifdef DH_TEST_VECTORS
  10650. +extern void dwc_run_dh_test_vectors(void);
  10651. +#endif
  10652. +
  10653. +#ifdef __cplusplus
  10654. +}
  10655. +#endif
  10656. +
  10657. +#endif /* _DWC_DH_H_ */
  10658. --- /dev/null
  10659. +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
  10660. @@ -0,0 +1,594 @@
  10661. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  10662. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  10663. +
  10664. +/*
  10665. + * Copyright (c) 1991, 1993
  10666. + * The Regents of the University of California. All rights reserved.
  10667. + *
  10668. + * Redistribution and use in source and binary forms, with or without
  10669. + * modification, are permitted provided that the following conditions
  10670. + * are met:
  10671. + * 1. Redistributions of source code must retain the above copyright
  10672. + * notice, this list of conditions and the following disclaimer.
  10673. + * 2. Redistributions in binary form must reproduce the above copyright
  10674. + * notice, this list of conditions and the following disclaimer in the
  10675. + * documentation and/or other materials provided with the distribution.
  10676. + * 3. Neither the name of the University nor the names of its contributors
  10677. + * may be used to endorse or promote products derived from this software
  10678. + * without specific prior written permission.
  10679. + *
  10680. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  10681. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  10682. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  10683. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  10684. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  10685. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  10686. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  10687. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  10688. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  10689. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  10690. + * SUCH DAMAGE.
  10691. + *
  10692. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  10693. + */
  10694. +
  10695. +#ifndef _DWC_LIST_H_
  10696. +#define _DWC_LIST_H_
  10697. +
  10698. +#ifdef __cplusplus
  10699. +extern "C" {
  10700. +#endif
  10701. +
  10702. +/** @file
  10703. + *
  10704. + * This file defines linked list operations. It is derived from BSD with
  10705. + * only the MACRO names being prefixed with DWC_. This is because a few of
  10706. + * these names conflict with those on Linux. For documentation on use, see the
  10707. + * inline comments in the source code. The original license for this source
  10708. + * code applies and is preserved in the dwc_list.h source file.
  10709. + */
  10710. +
  10711. +/*
  10712. + * This file defines five types of data structures: singly-linked lists,
  10713. + * lists, simple queues, tail queues, and circular queues.
  10714. + *
  10715. + *
  10716. + * A singly-linked list is headed by a single forward pointer. The elements
  10717. + * are singly linked for minimum space and pointer manipulation overhead at
  10718. + * the expense of O(n) removal for arbitrary elements. New elements can be
  10719. + * added to the list after an existing element or at the head of the list.
  10720. + * Elements being removed from the head of the list should use the explicit
  10721. + * macro for this purpose for optimum efficiency. A singly-linked list may
  10722. + * only be traversed in the forward direction. Singly-linked lists are ideal
  10723. + * for applications with large datasets and few or no removals or for
  10724. + * implementing a LIFO queue.
  10725. + *
  10726. + * A list is headed by a single forward pointer (or an array of forward
  10727. + * pointers for a hash table header). The elements are doubly linked
  10728. + * so that an arbitrary element can be removed without a need to
  10729. + * traverse the list. New elements can be added to the list before
  10730. + * or after an existing element or at the head of the list. A list
  10731. + * may only be traversed in the forward direction.
  10732. + *
  10733. + * A simple queue is headed by a pair of pointers, one the head of the
  10734. + * list and the other to the tail of the list. The elements are singly
  10735. + * linked to save space, so elements can only be removed from the
  10736. + * head of the list. New elements can be added to the list before or after
  10737. + * an existing element, at the head of the list, or at the end of the
  10738. + * list. A simple queue may only be traversed in the forward direction.
  10739. + *
  10740. + * A tail queue is headed by a pair of pointers, one to the head of the
  10741. + * list and the other to the tail of the list. The elements are doubly
  10742. + * linked so that an arbitrary element can be removed without a need to
  10743. + * traverse the list. New elements can be added to the list before or
  10744. + * after an existing element, at the head of the list, or at the end of
  10745. + * the list. A tail queue may be traversed in either direction.
  10746. + *
  10747. + * A circle queue is headed by a pair of pointers, one to the head of the
  10748. + * list and the other to the tail of the list. The elements are doubly
  10749. + * linked so that an arbitrary element can be removed without a need to
  10750. + * traverse the list. New elements can be added to the list before or after
  10751. + * an existing element, at the head of the list, or at the end of the list.
  10752. + * A circle queue may be traversed in either direction, but has a more
  10753. + * complex end of list detection.
  10754. + *
  10755. + * For details on the use of these macros, see the queue(3) manual page.
  10756. + */
  10757. +
  10758. +/*
  10759. + * Double-linked List.
  10760. + */
  10761. +
  10762. +typedef struct dwc_list_link {
  10763. + struct dwc_list_link *next;
  10764. + struct dwc_list_link *prev;
  10765. +} dwc_list_link_t;
  10766. +
  10767. +#define DWC_LIST_INIT(link) do { \
  10768. + (link)->next = (link); \
  10769. + (link)->prev = (link); \
  10770. +} while (0)
  10771. +
  10772. +#define DWC_LIST_FIRST(link) ((link)->next)
  10773. +#define DWC_LIST_LAST(link) ((link)->prev)
  10774. +#define DWC_LIST_END(link) (link)
  10775. +#define DWC_LIST_NEXT(link) ((link)->next)
  10776. +#define DWC_LIST_PREV(link) ((link)->prev)
  10777. +#define DWC_LIST_EMPTY(link) \
  10778. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  10779. +#define DWC_LIST_ENTRY(link, type, field) \
  10780. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  10781. +
  10782. +#if 0
  10783. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  10784. + (link)->next = (list)->next; \
  10785. + (link)->prev = (list); \
  10786. + (list)->next->prev = (link); \
  10787. + (list)->next = (link); \
  10788. +} while (0)
  10789. +
  10790. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  10791. + (link)->next = (list); \
  10792. + (link)->prev = (list)->prev; \
  10793. + (list)->prev->next = (link); \
  10794. + (list)->prev = (link); \
  10795. +} while (0)
  10796. +#else
  10797. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  10798. + dwc_list_link_t *__next__ = (list)->next; \
  10799. + __next__->prev = (link); \
  10800. + (link)->next = __next__; \
  10801. + (link)->prev = (list); \
  10802. + (list)->next = (link); \
  10803. +} while (0)
  10804. +
  10805. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  10806. + dwc_list_link_t *__prev__ = (list)->prev; \
  10807. + (list)->prev = (link); \
  10808. + (link)->next = (list); \
  10809. + (link)->prev = __prev__; \
  10810. + __prev__->next = (link); \
  10811. +} while (0)
  10812. +#endif
  10813. +
  10814. +#if 0
  10815. +static inline void __list_add(struct list_head *new,
  10816. + struct list_head *prev,
  10817. + struct list_head *next)
  10818. +{
  10819. + next->prev = new;
  10820. + new->next = next;
  10821. + new->prev = prev;
  10822. + prev->next = new;
  10823. +}
  10824. +
  10825. +static inline void list_add(struct list_head *new, struct list_head *head)
  10826. +{
  10827. + __list_add(new, head, head->next);
  10828. +}
  10829. +
  10830. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  10831. +{
  10832. + __list_add(new, head->prev, head);
  10833. +}
  10834. +
  10835. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  10836. +{
  10837. + next->prev = prev;
  10838. + prev->next = next;
  10839. +}
  10840. +
  10841. +static inline void list_del(struct list_head *entry)
  10842. +{
  10843. + __list_del(entry->prev, entry->next);
  10844. + entry->next = LIST_POISON1;
  10845. + entry->prev = LIST_POISON2;
  10846. +}
  10847. +#endif
  10848. +
  10849. +#define DWC_LIST_REMOVE(link) do { \
  10850. + (link)->next->prev = (link)->prev; \
  10851. + (link)->prev->next = (link)->next; \
  10852. +} while (0)
  10853. +
  10854. +#define DWC_LIST_REMOVE_INIT(link) do { \
  10855. + DWC_LIST_REMOVE(link); \
  10856. + DWC_LIST_INIT(link); \
  10857. +} while (0)
  10858. +
  10859. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  10860. + DWC_LIST_REMOVE(link); \
  10861. + DWC_LIST_INSERT_HEAD(list, link); \
  10862. +} while (0)
  10863. +
  10864. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  10865. + DWC_LIST_REMOVE(link); \
  10866. + DWC_LIST_INSERT_TAIL(list, link); \
  10867. +} while (0)
  10868. +
  10869. +#define DWC_LIST_FOREACH(var, list) \
  10870. + for((var) = DWC_LIST_FIRST(list); \
  10871. + (var) != DWC_LIST_END(list); \
  10872. + (var) = DWC_LIST_NEXT(var))
  10873. +
  10874. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  10875. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  10876. + (var) != DWC_LIST_END(list); \
  10877. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  10878. +
  10879. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  10880. + for((var) = DWC_LIST_LAST(list); \
  10881. + (var) != DWC_LIST_END(list); \
  10882. + (var) = DWC_LIST_PREV(var))
  10883. +
  10884. +/*
  10885. + * Singly-linked List definitions.
  10886. + */
  10887. +#define DWC_SLIST_HEAD(name, type) \
  10888. +struct name { \
  10889. + struct type *slh_first; /* first element */ \
  10890. +}
  10891. +
  10892. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  10893. + { NULL }
  10894. +
  10895. +#define DWC_SLIST_ENTRY(type) \
  10896. +struct { \
  10897. + struct type *sle_next; /* next element */ \
  10898. +}
  10899. +
  10900. +/*
  10901. + * Singly-linked List access methods.
  10902. + */
  10903. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  10904. +#define DWC_SLIST_END(head) NULL
  10905. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  10906. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  10907. +
  10908. +#define DWC_SLIST_FOREACH(var, head, field) \
  10909. + for((var) = SLIST_FIRST(head); \
  10910. + (var) != SLIST_END(head); \
  10911. + (var) = SLIST_NEXT(var, field))
  10912. +
  10913. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  10914. + for((varp) = &SLIST_FIRST((head)); \
  10915. + ((var) = *(varp)) != SLIST_END(head); \
  10916. + (varp) = &SLIST_NEXT((var), field))
  10917. +
  10918. +/*
  10919. + * Singly-linked List functions.
  10920. + */
  10921. +#define DWC_SLIST_INIT(head) { \
  10922. + SLIST_FIRST(head) = SLIST_END(head); \
  10923. +}
  10924. +
  10925. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  10926. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  10927. + (slistelm)->field.sle_next = (elm); \
  10928. +} while (0)
  10929. +
  10930. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  10931. + (elm)->field.sle_next = (head)->slh_first; \
  10932. + (head)->slh_first = (elm); \
  10933. +} while (0)
  10934. +
  10935. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  10936. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  10937. +} while (0)
  10938. +
  10939. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  10940. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  10941. +} while (0)
  10942. +
  10943. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  10944. + if ((head)->slh_first == (elm)) { \
  10945. + SLIST_REMOVE_HEAD((head), field); \
  10946. + } \
  10947. + else { \
  10948. + struct type *curelm = (head)->slh_first; \
  10949. + while( curelm->field.sle_next != (elm) ) \
  10950. + curelm = curelm->field.sle_next; \
  10951. + curelm->field.sle_next = \
  10952. + curelm->field.sle_next->field.sle_next; \
  10953. + } \
  10954. +} while (0)
  10955. +
  10956. +/*
  10957. + * Simple queue definitions.
  10958. + */
  10959. +#define DWC_SIMPLEQ_HEAD(name, type) \
  10960. +struct name { \
  10961. + struct type *sqh_first; /* first element */ \
  10962. + struct type **sqh_last; /* addr of last next element */ \
  10963. +}
  10964. +
  10965. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  10966. + { NULL, &(head).sqh_first }
  10967. +
  10968. +#define DWC_SIMPLEQ_ENTRY(type) \
  10969. +struct { \
  10970. + struct type *sqe_next; /* next element */ \
  10971. +}
  10972. +
  10973. +/*
  10974. + * Simple queue access methods.
  10975. + */
  10976. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  10977. +#define DWC_SIMPLEQ_END(head) NULL
  10978. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  10979. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  10980. +
  10981. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  10982. + for((var) = SIMPLEQ_FIRST(head); \
  10983. + (var) != SIMPLEQ_END(head); \
  10984. + (var) = SIMPLEQ_NEXT(var, field))
  10985. +
  10986. +/*
  10987. + * Simple queue functions.
  10988. + */
  10989. +#define DWC_SIMPLEQ_INIT(head) do { \
  10990. + (head)->sqh_first = NULL; \
  10991. + (head)->sqh_last = &(head)->sqh_first; \
  10992. +} while (0)
  10993. +
  10994. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  10995. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  10996. + (head)->sqh_last = &(elm)->field.sqe_next; \
  10997. + (head)->sqh_first = (elm); \
  10998. +} while (0)
  10999. +
  11000. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  11001. + (elm)->field.sqe_next = NULL; \
  11002. + *(head)->sqh_last = (elm); \
  11003. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11004. +} while (0)
  11005. +
  11006. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11007. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  11008. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11009. + (listelm)->field.sqe_next = (elm); \
  11010. +} while (0)
  11011. +
  11012. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  11013. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  11014. + (head)->sqh_last = &(head)->sqh_first; \
  11015. +} while (0)
  11016. +
  11017. +/*
  11018. + * Tail queue definitions.
  11019. + */
  11020. +#define DWC_TAILQ_HEAD(name, type) \
  11021. +struct name { \
  11022. + struct type *tqh_first; /* first element */ \
  11023. + struct type **tqh_last; /* addr of last next element */ \
  11024. +}
  11025. +
  11026. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  11027. + { NULL, &(head).tqh_first }
  11028. +
  11029. +#define DWC_TAILQ_ENTRY(type) \
  11030. +struct { \
  11031. + struct type *tqe_next; /* next element */ \
  11032. + struct type **tqe_prev; /* address of previous next element */ \
  11033. +}
  11034. +
  11035. +/*
  11036. + * tail queue access methods
  11037. + */
  11038. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  11039. +#define DWC_TAILQ_END(head) NULL
  11040. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  11041. +#define DWC_TAILQ_LAST(head, headname) \
  11042. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  11043. +/* XXX */
  11044. +#define DWC_TAILQ_PREV(elm, headname, field) \
  11045. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  11046. +#define DWC_TAILQ_EMPTY(head) \
  11047. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  11048. +
  11049. +#define DWC_TAILQ_FOREACH(var, head, field) \
  11050. + for ((var) = DWC_TAILQ_FIRST(head); \
  11051. + (var) != DWC_TAILQ_END(head); \
  11052. + (var) = DWC_TAILQ_NEXT(var, field))
  11053. +
  11054. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  11055. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  11056. + (var) != DWC_TAILQ_END(head); \
  11057. + (var) = DWC_TAILQ_PREV(var, headname, field))
  11058. +
  11059. +/*
  11060. + * Tail queue functions.
  11061. + */
  11062. +#define DWC_TAILQ_INIT(head) do { \
  11063. + (head)->tqh_first = NULL; \
  11064. + (head)->tqh_last = &(head)->tqh_first; \
  11065. +} while (0)
  11066. +
  11067. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  11068. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  11069. + (head)->tqh_first->field.tqe_prev = \
  11070. + &(elm)->field.tqe_next; \
  11071. + else \
  11072. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11073. + (head)->tqh_first = (elm); \
  11074. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  11075. +} while (0)
  11076. +
  11077. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  11078. + (elm)->field.tqe_next = NULL; \
  11079. + (elm)->field.tqe_prev = (head)->tqh_last; \
  11080. + *(head)->tqh_last = (elm); \
  11081. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11082. +} while (0)
  11083. +
  11084. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11085. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  11086. + (elm)->field.tqe_next->field.tqe_prev = \
  11087. + &(elm)->field.tqe_next; \
  11088. + else \
  11089. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11090. + (listelm)->field.tqe_next = (elm); \
  11091. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  11092. +} while (0)
  11093. +
  11094. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  11095. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  11096. + (elm)->field.tqe_next = (listelm); \
  11097. + *(listelm)->field.tqe_prev = (elm); \
  11098. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  11099. +} while (0)
  11100. +
  11101. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  11102. + if (((elm)->field.tqe_next) != NULL) \
  11103. + (elm)->field.tqe_next->field.tqe_prev = \
  11104. + (elm)->field.tqe_prev; \
  11105. + else \
  11106. + (head)->tqh_last = (elm)->field.tqe_prev; \
  11107. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  11108. +} while (0)
  11109. +
  11110. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  11111. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  11112. + (elm2)->field.tqe_next->field.tqe_prev = \
  11113. + &(elm2)->field.tqe_next; \
  11114. + else \
  11115. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  11116. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  11117. + *(elm2)->field.tqe_prev = (elm2); \
  11118. +} while (0)
  11119. +
  11120. +/*
  11121. + * Circular queue definitions.
  11122. + */
  11123. +#define DWC_CIRCLEQ_HEAD(name, type) \
  11124. +struct name { \
  11125. + struct type *cqh_first; /* first element */ \
  11126. + struct type *cqh_last; /* last element */ \
  11127. +}
  11128. +
  11129. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  11130. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  11131. +
  11132. +#define DWC_CIRCLEQ_ENTRY(type) \
  11133. +struct { \
  11134. + struct type *cqe_next; /* next element */ \
  11135. + struct type *cqe_prev; /* previous element */ \
  11136. +}
  11137. +
  11138. +/*
  11139. + * Circular queue access methods
  11140. + */
  11141. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  11142. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  11143. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  11144. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  11145. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  11146. +#define DWC_CIRCLEQ_EMPTY(head) \
  11147. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  11148. +
  11149. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  11150. +
  11151. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  11152. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  11153. + (var) != DWC_CIRCLEQ_END(head); \
  11154. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  11155. +
  11156. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  11157. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  11158. + (var) != DWC_CIRCLEQ_END(head); \
  11159. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  11160. +
  11161. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  11162. + for((var) = DWC_CIRCLEQ_LAST(head); \
  11163. + (var) != DWC_CIRCLEQ_END(head); \
  11164. + (var) = DWC_CIRCLEQ_PREV(var, field))
  11165. +
  11166. +/*
  11167. + * Circular queue functions.
  11168. + */
  11169. +#define DWC_CIRCLEQ_INIT(head) do { \
  11170. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  11171. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  11172. +} while (0)
  11173. +
  11174. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  11175. + (elm)->field.cqe_next = NULL; \
  11176. + (elm)->field.cqe_prev = NULL; \
  11177. +} while (0)
  11178. +
  11179. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11180. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  11181. + (elm)->field.cqe_prev = (listelm); \
  11182. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  11183. + (head)->cqh_last = (elm); \
  11184. + else \
  11185. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  11186. + (listelm)->field.cqe_next = (elm); \
  11187. +} while (0)
  11188. +
  11189. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  11190. + (elm)->field.cqe_next = (listelm); \
  11191. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  11192. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  11193. + (head)->cqh_first = (elm); \
  11194. + else \
  11195. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  11196. + (listelm)->field.cqe_prev = (elm); \
  11197. +} while (0)
  11198. +
  11199. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  11200. + (elm)->field.cqe_next = (head)->cqh_first; \
  11201. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  11202. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  11203. + (head)->cqh_last = (elm); \
  11204. + else \
  11205. + (head)->cqh_first->field.cqe_prev = (elm); \
  11206. + (head)->cqh_first = (elm); \
  11207. +} while (0)
  11208. +
  11209. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  11210. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  11211. + (elm)->field.cqe_prev = (head)->cqh_last; \
  11212. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  11213. + (head)->cqh_first = (elm); \
  11214. + else \
  11215. + (head)->cqh_last->field.cqe_next = (elm); \
  11216. + (head)->cqh_last = (elm); \
  11217. +} while (0)
  11218. +
  11219. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  11220. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  11221. + (head)->cqh_last = (elm)->field.cqe_prev; \
  11222. + else \
  11223. + (elm)->field.cqe_next->field.cqe_prev = \
  11224. + (elm)->field.cqe_prev; \
  11225. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  11226. + (head)->cqh_first = (elm)->field.cqe_next; \
  11227. + else \
  11228. + (elm)->field.cqe_prev->field.cqe_next = \
  11229. + (elm)->field.cqe_next; \
  11230. +} while (0)
  11231. +
  11232. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  11233. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  11234. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  11235. +} while (0)
  11236. +
  11237. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  11238. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  11239. + DWC_CIRCLEQ_END(head)) \
  11240. + (head).cqh_last = (elm2); \
  11241. + else \
  11242. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  11243. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  11244. + DWC_CIRCLEQ_END(head)) \
  11245. + (head).cqh_first = (elm2); \
  11246. + else \
  11247. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  11248. +} while (0)
  11249. +
  11250. +#ifdef __cplusplus
  11251. +}
  11252. +#endif
  11253. +
  11254. +#endif /* _DWC_LIST_H_ */
  11255. --- /dev/null
  11256. +++ b/drivers/usb/host/dwc_common_port/dwc_mem.c
  11257. @@ -0,0 +1,245 @@
  11258. +/* Memory Debugging */
  11259. +#ifdef DWC_DEBUG_MEMORY
  11260. +
  11261. +#include "dwc_os.h"
  11262. +#include "dwc_list.h"
  11263. +
  11264. +struct allocation {
  11265. + void *addr;
  11266. + void *ctx;
  11267. + char *func;
  11268. + int line;
  11269. + uint32_t size;
  11270. + int dma;
  11271. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  11272. +};
  11273. +
  11274. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  11275. +
  11276. +struct allocation_manager {
  11277. + void *mem_ctx;
  11278. + struct allocation_queue allocations;
  11279. +
  11280. + /* statistics */
  11281. + int num;
  11282. + int num_freed;
  11283. + int num_active;
  11284. + uint32_t total;
  11285. + uint32_t cur;
  11286. + uint32_t max;
  11287. +};
  11288. +
  11289. +static struct allocation_manager *manager = NULL;
  11290. +
  11291. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  11292. + int dma)
  11293. +{
  11294. + struct allocation *a;
  11295. +
  11296. + DWC_ASSERT(manager != NULL, "manager not allocated");
  11297. +
  11298. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  11299. + if (!a) {
  11300. + return -DWC_E_NO_MEMORY;
  11301. + }
  11302. +
  11303. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  11304. + if (!a->func) {
  11305. + __DWC_FREE(manager->mem_ctx, a);
  11306. + return -DWC_E_NO_MEMORY;
  11307. + }
  11308. +
  11309. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  11310. + a->addr = addr;
  11311. + a->ctx = ctx;
  11312. + a->line = line;
  11313. + a->size = size;
  11314. + a->dma = dma;
  11315. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  11316. +
  11317. + /* Update stats */
  11318. + manager->num++;
  11319. + manager->num_active++;
  11320. + manager->total += size;
  11321. + manager->cur += size;
  11322. +
  11323. + if (manager->max < manager->cur) {
  11324. + manager->max = manager->cur;
  11325. + }
  11326. +
  11327. + return 0;
  11328. +}
  11329. +
  11330. +static struct allocation *find_allocation(void *ctx, void *addr)
  11331. +{
  11332. + struct allocation *a;
  11333. +
  11334. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11335. + if (a->ctx == ctx && a->addr == addr) {
  11336. + return a;
  11337. + }
  11338. + }
  11339. +
  11340. + return NULL;
  11341. +}
  11342. +
  11343. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  11344. +{
  11345. + struct allocation *a = find_allocation(ctx, addr);
  11346. +
  11347. + if (!a) {
  11348. + DWC_ASSERT(0,
  11349. + "Free of address %p that was never allocated or already freed %s:%d",
  11350. + addr, func, line);
  11351. + return;
  11352. + }
  11353. +
  11354. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  11355. +
  11356. + manager->num_active--;
  11357. + manager->num_freed++;
  11358. + manager->cur -= a->size;
  11359. + __DWC_FREE(manager->mem_ctx, a->func);
  11360. + __DWC_FREE(manager->mem_ctx, a);
  11361. +}
  11362. +
  11363. +int dwc_memory_debug_start(void *mem_ctx)
  11364. +{
  11365. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  11366. +
  11367. + if (manager) {
  11368. + return -DWC_E_BUSY;
  11369. + }
  11370. +
  11371. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  11372. + if (!manager) {
  11373. + return -DWC_E_NO_MEMORY;
  11374. + }
  11375. +
  11376. + DWC_CIRCLEQ_INIT(&manager->allocations);
  11377. + manager->mem_ctx = mem_ctx;
  11378. + manager->num = 0;
  11379. + manager->num_freed = 0;
  11380. + manager->num_active = 0;
  11381. + manager->total = 0;
  11382. + manager->cur = 0;
  11383. + manager->max = 0;
  11384. +
  11385. + return 0;
  11386. +}
  11387. +
  11388. +void dwc_memory_debug_stop(void)
  11389. +{
  11390. + struct allocation *a;
  11391. +
  11392. + dwc_memory_debug_report();
  11393. +
  11394. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11395. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  11396. + free_allocation(a->ctx, a->addr, NULL, -1);
  11397. + }
  11398. +
  11399. + __DWC_FREE(manager->mem_ctx, manager);
  11400. +}
  11401. +
  11402. +void dwc_memory_debug_report(void)
  11403. +{
  11404. + struct allocation *a;
  11405. +
  11406. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  11407. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  11408. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  11409. + DWC_PRINTF("Active = %d\n", manager->num_active);
  11410. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  11411. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  11412. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  11413. + DWC_PRINTF("Unfreed allocations:\n");
  11414. +
  11415. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11416. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  11417. + a->addr, a->size, a->func, a->line, a->dma);
  11418. + }
  11419. +}
  11420. +
  11421. +/* The replacement functions */
  11422. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  11423. +{
  11424. + void *addr = __DWC_ALLOC(mem_ctx, size);
  11425. +
  11426. + if (!addr) {
  11427. + return NULL;
  11428. + }
  11429. +
  11430. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  11431. + __DWC_FREE(mem_ctx, addr);
  11432. + return NULL;
  11433. + }
  11434. +
  11435. + return addr;
  11436. +}
  11437. +
  11438. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  11439. + int line)
  11440. +{
  11441. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  11442. +
  11443. + if (!addr) {
  11444. + return NULL;
  11445. + }
  11446. +
  11447. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  11448. + __DWC_FREE(mem_ctx, addr);
  11449. + return NULL;
  11450. + }
  11451. +
  11452. + return addr;
  11453. +}
  11454. +
  11455. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  11456. +{
  11457. + free_allocation(mem_ctx, addr, func, line);
  11458. + __DWC_FREE(mem_ctx, addr);
  11459. +}
  11460. +
  11461. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  11462. + char const *func, int line)
  11463. +{
  11464. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  11465. +
  11466. + if (!addr) {
  11467. + return NULL;
  11468. + }
  11469. +
  11470. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  11471. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  11472. + return NULL;
  11473. + }
  11474. +
  11475. + return addr;
  11476. +}
  11477. +
  11478. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  11479. + dwc_dma_t *dma_addr, char const *func, int line)
  11480. +{
  11481. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  11482. +
  11483. + if (!addr) {
  11484. + return NULL;
  11485. + }
  11486. +
  11487. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  11488. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  11489. + return NULL;
  11490. + }
  11491. +
  11492. + return addr;
  11493. +}
  11494. +
  11495. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  11496. + dwc_dma_t dma_addr, char const *func, int line)
  11497. +{
  11498. + free_allocation(dma_ctx, virt_addr, func, line);
  11499. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  11500. +}
  11501. +
  11502. +#endif /* DWC_DEBUG_MEMORY */
  11503. --- /dev/null
  11504. +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.c
  11505. @@ -0,0 +1,636 @@
  11506. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  11507. + *
  11508. + * PuTTY is copyright 1997-2007 Simon Tatham.
  11509. + *
  11510. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  11511. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  11512. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  11513. + * Kuhn, and CORE SDI S.A.
  11514. + *
  11515. + * Permission is hereby granted, free of charge, to any person
  11516. + * obtaining a copy of this software and associated documentation files
  11517. + * (the "Software"), to deal in the Software without restriction,
  11518. + * including without limitation the rights to use, copy, modify, merge,
  11519. + * publish, distribute, sublicense, and/or sell copies of the Software,
  11520. + * and to permit persons to whom the Software is furnished to do so,
  11521. + * subject to the following conditions:
  11522. + *
  11523. + * The above copyright notice and this permission notice shall be
  11524. + * included in all copies or substantial portions of the Software.
  11525. +
  11526. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11527. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11528. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11529. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  11530. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11531. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  11532. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11533. + *
  11534. + */
  11535. +#ifdef DWC_CRYPTOLIB
  11536. +
  11537. +#ifndef CONFIG_MACH_IPMATE
  11538. +
  11539. +#include "dwc_modpow.h"
  11540. +
  11541. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  11542. +#define BIGNUM_TOP_BIT 0x80000000UL
  11543. +#define BIGNUM_INT_BITS 32
  11544. +
  11545. +
  11546. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  11547. +{
  11548. + void *p;
  11549. + size *= n;
  11550. + if (size == 0) size = 1;
  11551. + p = dwc_alloc(mem_ctx, size);
  11552. + return p;
  11553. +}
  11554. +
  11555. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  11556. +#define sfree dwc_free
  11557. +
  11558. +/*
  11559. + * Usage notes:
  11560. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  11561. + * subscripts, as some implementations object to this (see below).
  11562. + * * Note that none of the division methods below will cope if the
  11563. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  11564. + * to avoid this case.
  11565. + * If this condition occurs, in the case of the x86 DIV instruction,
  11566. + * an overflow exception will occur, which (according to a correspondent)
  11567. + * will manifest on Windows as something like
  11568. + * 0xC0000095: Integer overflow
  11569. + * The C variant won't give the right answer, either.
  11570. + */
  11571. +
  11572. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  11573. +
  11574. +#if defined __GNUC__ && defined __i386__
  11575. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  11576. + __asm__("div %2" : \
  11577. + "=d" (r), "=a" (q) : \
  11578. + "r" (w), "d" (hi), "a" (lo))
  11579. +#else
  11580. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  11581. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  11582. + q = n / w; \
  11583. + r = n % w; \
  11584. +} while (0)
  11585. +#endif
  11586. +
  11587. +// q = n / w;
  11588. +// r = n % w;
  11589. +
  11590. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  11591. +
  11592. +#define BIGNUM_INTERNAL
  11593. +
  11594. +static Bignum newbn(void *mem_ctx, int length)
  11595. +{
  11596. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  11597. + //if (!b)
  11598. + //abort(); /* FIXME */
  11599. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  11600. + b[0] = length;
  11601. + return b;
  11602. +}
  11603. +
  11604. +void freebn(void *mem_ctx, Bignum b)
  11605. +{
  11606. + /*
  11607. + * Burn the evidence, just in case.
  11608. + */
  11609. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  11610. + sfree(mem_ctx, b);
  11611. +}
  11612. +
  11613. +/*
  11614. + * Compute c = a * b.
  11615. + * Input is in the first len words of a and b.
  11616. + * Result is returned in the first 2*len words of c.
  11617. + */
  11618. +static void internal_mul(BignumInt *a, BignumInt *b,
  11619. + BignumInt *c, int len)
  11620. +{
  11621. + int i, j;
  11622. + BignumDblInt t;
  11623. +
  11624. + for (j = 0; j < 2 * len; j++)
  11625. + c[j] = 0;
  11626. +
  11627. + for (i = len - 1; i >= 0; i--) {
  11628. + t = 0;
  11629. + for (j = len - 1; j >= 0; j--) {
  11630. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  11631. + t += (BignumDblInt) c[i + j + 1];
  11632. + c[i + j + 1] = (BignumInt) t;
  11633. + t = t >> BIGNUM_INT_BITS;
  11634. + }
  11635. + c[i] = (BignumInt) t;
  11636. + }
  11637. +}
  11638. +
  11639. +static void internal_add_shifted(BignumInt *number,
  11640. + unsigned n, int shift)
  11641. +{
  11642. + int word = 1 + (shift / BIGNUM_INT_BITS);
  11643. + int bshift = shift % BIGNUM_INT_BITS;
  11644. + BignumDblInt addend;
  11645. +
  11646. + addend = (BignumDblInt)n << bshift;
  11647. +
  11648. + while (addend) {
  11649. + addend += number[word];
  11650. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  11651. + addend >>= BIGNUM_INT_BITS;
  11652. + word++;
  11653. + }
  11654. +}
  11655. +
  11656. +/*
  11657. + * Compute a = a % m.
  11658. + * Input in first alen words of a and first mlen words of m.
  11659. + * Output in first alen words of a
  11660. + * (of which first alen-mlen words will be zero).
  11661. + * The MSW of m MUST have its high bit set.
  11662. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  11663. + * rather than the internal bigendian format. Quotient parts are shifted
  11664. + * left by `qshift' before adding into quot.
  11665. + */
  11666. +static void internal_mod(BignumInt *a, int alen,
  11667. + BignumInt *m, int mlen,
  11668. + BignumInt *quot, int qshift)
  11669. +{
  11670. + BignumInt m0, m1;
  11671. + unsigned int h;
  11672. + int i, k;
  11673. +
  11674. + m0 = m[0];
  11675. + if (mlen > 1)
  11676. + m1 = m[1];
  11677. + else
  11678. + m1 = 0;
  11679. +
  11680. + for (i = 0; i <= alen - mlen; i++) {
  11681. + BignumDblInt t;
  11682. + unsigned int q, r, c, ai1;
  11683. +
  11684. + if (i == 0) {
  11685. + h = 0;
  11686. + } else {
  11687. + h = a[i - 1];
  11688. + a[i - 1] = 0;
  11689. + }
  11690. +
  11691. + if (i == alen - 1)
  11692. + ai1 = 0;
  11693. + else
  11694. + ai1 = a[i + 1];
  11695. +
  11696. + /* Find q = h:a[i] / m0 */
  11697. + if (h >= m0) {
  11698. + /*
  11699. + * Special case.
  11700. + *
  11701. + * To illustrate it, suppose a BignumInt is 8 bits, and
  11702. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  11703. + * our initial division will be 0xA123 / 0xA1, which
  11704. + * will give a quotient of 0x100 and a divide overflow.
  11705. + * However, the invariants in this division algorithm
  11706. + * are not violated, since the full number A1:23:... is
  11707. + * _less_ than the quotient prefix A1:B2:... and so the
  11708. + * following correction loop would have sorted it out.
  11709. + *
  11710. + * In this situation we set q to be the largest
  11711. + * quotient we _can_ stomach (0xFF, of course).
  11712. + */
  11713. + q = BIGNUM_INT_MASK;
  11714. + } else {
  11715. + /* Macro doesn't want an array subscript expression passed
  11716. + * into it (see definition), so use a temporary. */
  11717. + BignumInt tmplo = a[i];
  11718. + DIVMOD_WORD(q, r, h, tmplo, m0);
  11719. +
  11720. + /* Refine our estimate of q by looking at
  11721. + h:a[i]:a[i+1] / m0:m1 */
  11722. + t = MUL_WORD(m1, q);
  11723. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  11724. + q--;
  11725. + t -= m1;
  11726. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  11727. + if (r >= (BignumDblInt) m0 &&
  11728. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  11729. + }
  11730. + }
  11731. +
  11732. + /* Subtract q * m from a[i...] */
  11733. + c = 0;
  11734. + for (k = mlen - 1; k >= 0; k--) {
  11735. + t = MUL_WORD(q, m[k]);
  11736. + t += c;
  11737. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  11738. + if ((BignumInt) t > a[i + k])
  11739. + c++;
  11740. + a[i + k] -= (BignumInt) t;
  11741. + }
  11742. +
  11743. + /* Add back m in case of borrow */
  11744. + if (c != h) {
  11745. + t = 0;
  11746. + for (k = mlen - 1; k >= 0; k--) {
  11747. + t += m[k];
  11748. + t += a[i + k];
  11749. + a[i + k] = (BignumInt) t;
  11750. + t = t >> BIGNUM_INT_BITS;
  11751. + }
  11752. + q--;
  11753. + }
  11754. + if (quot)
  11755. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  11756. + }
  11757. +}
  11758. +
  11759. +/*
  11760. + * Compute p % mod.
  11761. + * The most significant word of mod MUST be non-zero.
  11762. + * We assume that the result array is the same size as the mod array.
  11763. + * We optionally write out a quotient if `quotient' is non-NULL.
  11764. + * We can avoid writing out the result if `result' is NULL.
  11765. + */
  11766. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  11767. +{
  11768. + BignumInt *n, *m;
  11769. + int mshift;
  11770. + int plen, mlen, i, j;
  11771. +
  11772. + /* Allocate m of size mlen, copy mod to m */
  11773. + /* We use big endian internally */
  11774. + mlen = mod[0];
  11775. + m = snewn(mem_ctx, mlen, BignumInt);
  11776. + //if (!m)
  11777. + //abort(); /* FIXME */
  11778. + for (j = 0; j < mlen; j++)
  11779. + m[j] = mod[mod[0] - j];
  11780. +
  11781. + /* Shift m left to make msb bit set */
  11782. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  11783. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  11784. + break;
  11785. + if (mshift) {
  11786. + for (i = 0; i < mlen - 1; i++)
  11787. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  11788. + m[mlen - 1] = m[mlen - 1] << mshift;
  11789. + }
  11790. +
  11791. + plen = p[0];
  11792. + /* Ensure plen > mlen */
  11793. + if (plen <= mlen)
  11794. + plen = mlen + 1;
  11795. +
  11796. + /* Allocate n of size plen, copy p to n */
  11797. + n = snewn(mem_ctx, plen, BignumInt);
  11798. + //if (!n)
  11799. + //abort(); /* FIXME */
  11800. + for (j = 0; j < plen; j++)
  11801. + n[j] = 0;
  11802. + for (j = 1; j <= (int)p[0]; j++)
  11803. + n[plen - j] = p[j];
  11804. +
  11805. + /* Main computation */
  11806. + internal_mod(n, plen, m, mlen, quotient, mshift);
  11807. +
  11808. + /* Fixup result in case the modulus was shifted */
  11809. + if (mshift) {
  11810. + for (i = plen - mlen - 1; i < plen - 1; i++)
  11811. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  11812. + n[plen - 1] = n[plen - 1] << mshift;
  11813. + internal_mod(n, plen, m, mlen, quotient, 0);
  11814. + for (i = plen - 1; i >= plen - mlen; i--)
  11815. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  11816. + }
  11817. +
  11818. + /* Copy result to buffer */
  11819. + if (result) {
  11820. + for (i = 1; i <= (int)result[0]; i++) {
  11821. + int j = plen - i;
  11822. + result[i] = j >= 0 ? n[j] : 0;
  11823. + }
  11824. + }
  11825. +
  11826. + /* Free temporary arrays */
  11827. + for (i = 0; i < mlen; i++)
  11828. + m[i] = 0;
  11829. + sfree(mem_ctx, m);
  11830. + for (i = 0; i < plen; i++)
  11831. + n[i] = 0;
  11832. + sfree(mem_ctx, n);
  11833. +}
  11834. +
  11835. +/*
  11836. + * Simple remainder.
  11837. + */
  11838. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  11839. +{
  11840. + Bignum r = newbn(mem_ctx, b[0]);
  11841. + bigdivmod(mem_ctx, a, b, r, NULL);
  11842. + return r;
  11843. +}
  11844. +
  11845. +/*
  11846. + * Compute (base ^ exp) % mod.
  11847. + */
  11848. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  11849. +{
  11850. + BignumInt *a, *b, *n, *m;
  11851. + int mshift;
  11852. + int mlen, i, j;
  11853. + Bignum base, result;
  11854. +
  11855. + /*
  11856. + * The most significant word of mod needs to be non-zero. It
  11857. + * should already be, but let's make sure.
  11858. + */
  11859. + //assert(mod[mod[0]] != 0);
  11860. +
  11861. + /*
  11862. + * Make sure the base is smaller than the modulus, by reducing
  11863. + * it modulo the modulus if not.
  11864. + */
  11865. + base = bigmod(mem_ctx, base_in, mod);
  11866. +
  11867. + /* Allocate m of size mlen, copy mod to m */
  11868. + /* We use big endian internally */
  11869. + mlen = mod[0];
  11870. + m = snewn(mem_ctx, mlen, BignumInt);
  11871. + //if (!m)
  11872. + //abort(); /* FIXME */
  11873. + for (j = 0; j < mlen; j++)
  11874. + m[j] = mod[mod[0] - j];
  11875. +
  11876. + /* Shift m left to make msb bit set */
  11877. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  11878. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  11879. + break;
  11880. + if (mshift) {
  11881. + for (i = 0; i < mlen - 1; i++)
  11882. + m[i] =
  11883. + (m[i] << mshift) | (m[i + 1] >>
  11884. + (BIGNUM_INT_BITS - mshift));
  11885. + m[mlen - 1] = m[mlen - 1] << mshift;
  11886. + }
  11887. +
  11888. + /* Allocate n of size mlen, copy base to n */
  11889. + n = snewn(mem_ctx, mlen, BignumInt);
  11890. + //if (!n)
  11891. + //abort(); /* FIXME */
  11892. + i = mlen - base[0];
  11893. + for (j = 0; j < i; j++)
  11894. + n[j] = 0;
  11895. + for (j = 0; j < base[0]; j++)
  11896. + n[i + j] = base[base[0] - j];
  11897. +
  11898. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  11899. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  11900. + //if (!a)
  11901. + //abort(); /* FIXME */
  11902. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  11903. + //if (!b)
  11904. + //abort(); /* FIXME */
  11905. + for (i = 0; i < 2 * mlen; i++)
  11906. + a[i] = 0;
  11907. + a[2 * mlen - 1] = 1;
  11908. +
  11909. + /* Skip leading zero bits of exp. */
  11910. + i = 0;
  11911. + j = BIGNUM_INT_BITS - 1;
  11912. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  11913. + j--;
  11914. + if (j < 0) {
  11915. + i++;
  11916. + j = BIGNUM_INT_BITS - 1;
  11917. + }
  11918. + }
  11919. +
  11920. + /* Main computation */
  11921. + while (i < exp[0]) {
  11922. + while (j >= 0) {
  11923. + internal_mul(a + mlen, a + mlen, b, mlen);
  11924. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  11925. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  11926. + internal_mul(b + mlen, n, a, mlen);
  11927. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  11928. + } else {
  11929. + BignumInt *t;
  11930. + t = a;
  11931. + a = b;
  11932. + b = t;
  11933. + }
  11934. + j--;
  11935. + }
  11936. + i++;
  11937. + j = BIGNUM_INT_BITS - 1;
  11938. + }
  11939. +
  11940. + /* Fixup result in case the modulus was shifted */
  11941. + if (mshift) {
  11942. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  11943. + a[i] =
  11944. + (a[i] << mshift) | (a[i + 1] >>
  11945. + (BIGNUM_INT_BITS - mshift));
  11946. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  11947. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  11948. + for (i = 2 * mlen - 1; i >= mlen; i--)
  11949. + a[i] =
  11950. + (a[i] >> mshift) | (a[i - 1] <<
  11951. + (BIGNUM_INT_BITS - mshift));
  11952. + }
  11953. +
  11954. + /* Copy result to buffer */
  11955. + result = newbn(mem_ctx, mod[0]);
  11956. + for (i = 0; i < mlen; i++)
  11957. + result[result[0] - i] = a[i + mlen];
  11958. + while (result[0] > 1 && result[result[0]] == 0)
  11959. + result[0]--;
  11960. +
  11961. + /* Free temporary arrays */
  11962. + for (i = 0; i < 2 * mlen; i++)
  11963. + a[i] = 0;
  11964. + sfree(mem_ctx, a);
  11965. + for (i = 0; i < 2 * mlen; i++)
  11966. + b[i] = 0;
  11967. + sfree(mem_ctx, b);
  11968. + for (i = 0; i < mlen; i++)
  11969. + m[i] = 0;
  11970. + sfree(mem_ctx, m);
  11971. + for (i = 0; i < mlen; i++)
  11972. + n[i] = 0;
  11973. + sfree(mem_ctx, n);
  11974. +
  11975. + freebn(mem_ctx, base);
  11976. +
  11977. + return result;
  11978. +}
  11979. +
  11980. +
  11981. +#ifdef UNITTEST
  11982. +
  11983. +static __u32 dh_p[] = {
  11984. + 96,
  11985. + 0xFFFFFFFF,
  11986. + 0xFFFFFFFF,
  11987. + 0xA93AD2CA,
  11988. + 0x4B82D120,
  11989. + 0xE0FD108E,
  11990. + 0x43DB5BFC,
  11991. + 0x74E5AB31,
  11992. + 0x08E24FA0,
  11993. + 0xBAD946E2,
  11994. + 0x770988C0,
  11995. + 0x7A615D6C,
  11996. + 0xBBE11757,
  11997. + 0x177B200C,
  11998. + 0x521F2B18,
  11999. + 0x3EC86A64,
  12000. + 0xD8760273,
  12001. + 0xD98A0864,
  12002. + 0xF12FFA06,
  12003. + 0x1AD2EE6B,
  12004. + 0xCEE3D226,
  12005. + 0x4A25619D,
  12006. + 0x1E8C94E0,
  12007. + 0xDB0933D7,
  12008. + 0xABF5AE8C,
  12009. + 0xA6E1E4C7,
  12010. + 0xB3970F85,
  12011. + 0x5D060C7D,
  12012. + 0x8AEA7157,
  12013. + 0x58DBEF0A,
  12014. + 0xECFB8504,
  12015. + 0xDF1CBA64,
  12016. + 0xA85521AB,
  12017. + 0x04507A33,
  12018. + 0xAD33170D,
  12019. + 0x8AAAC42D,
  12020. + 0x15728E5A,
  12021. + 0x98FA0510,
  12022. + 0x15D22618,
  12023. + 0xEA956AE5,
  12024. + 0x3995497C,
  12025. + 0x95581718,
  12026. + 0xDE2BCBF6,
  12027. + 0x6F4C52C9,
  12028. + 0xB5C55DF0,
  12029. + 0xEC07A28F,
  12030. + 0x9B2783A2,
  12031. + 0x180E8603,
  12032. + 0xE39E772C,
  12033. + 0x2E36CE3B,
  12034. + 0x32905E46,
  12035. + 0xCA18217C,
  12036. + 0xF1746C08,
  12037. + 0x4ABC9804,
  12038. + 0x670C354E,
  12039. + 0x7096966D,
  12040. + 0x9ED52907,
  12041. + 0x208552BB,
  12042. + 0x1C62F356,
  12043. + 0xDCA3AD96,
  12044. + 0x83655D23,
  12045. + 0xFD24CF5F,
  12046. + 0x69163FA8,
  12047. + 0x1C55D39A,
  12048. + 0x98DA4836,
  12049. + 0xA163BF05,
  12050. + 0xC2007CB8,
  12051. + 0xECE45B3D,
  12052. + 0x49286651,
  12053. + 0x7C4B1FE6,
  12054. + 0xAE9F2411,
  12055. + 0x5A899FA5,
  12056. + 0xEE386BFB,
  12057. + 0xF406B7ED,
  12058. + 0x0BFF5CB6,
  12059. + 0xA637ED6B,
  12060. + 0xF44C42E9,
  12061. + 0x625E7EC6,
  12062. + 0xE485B576,
  12063. + 0x6D51C245,
  12064. + 0x4FE1356D,
  12065. + 0xF25F1437,
  12066. + 0x302B0A6D,
  12067. + 0xCD3A431B,
  12068. + 0xEF9519B3,
  12069. + 0x8E3404DD,
  12070. + 0x514A0879,
  12071. + 0x3B139B22,
  12072. + 0x020BBEA6,
  12073. + 0x8A67CC74,
  12074. + 0x29024E08,
  12075. + 0x80DC1CD1,
  12076. + 0xC4C6628B,
  12077. + 0x2168C234,
  12078. + 0xC90FDAA2,
  12079. + 0xFFFFFFFF,
  12080. + 0xFFFFFFFF,
  12081. +};
  12082. +
  12083. +static __u32 dh_a[] = {
  12084. + 8,
  12085. + 0xdf367516,
  12086. + 0x86459caa,
  12087. + 0xe2d459a4,
  12088. + 0xd910dae0,
  12089. + 0x8a8b5e37,
  12090. + 0x67ab31c6,
  12091. + 0xf0b55ea9,
  12092. + 0x440051d6,
  12093. +};
  12094. +
  12095. +static __u32 dh_b[] = {
  12096. + 8,
  12097. + 0xded92656,
  12098. + 0xe07a048a,
  12099. + 0x6fa452cd,
  12100. + 0x2df89d30,
  12101. + 0xc75f1b0f,
  12102. + 0x8ce3578f,
  12103. + 0x7980a324,
  12104. + 0x5daec786,
  12105. +};
  12106. +
  12107. +static __u32 dh_g[] = {
  12108. + 1,
  12109. + 2,
  12110. +};
  12111. +
  12112. +int main(void)
  12113. +{
  12114. + int i;
  12115. + __u32 *k;
  12116. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  12117. +
  12118. + printf("\n\n");
  12119. + for (i=0; i<k[0]; i++) {
  12120. + __u32 word32 = k[k[0] - i];
  12121. + __u16 l = word32 & 0xffff;
  12122. + __u16 m = (word32 & 0xffff0000) >> 16;
  12123. + printf("%04x %04x ", m, l);
  12124. + if (!((i + 1)%13)) printf("\n");
  12125. + }
  12126. + printf("\n\n");
  12127. +
  12128. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  12129. + printf("PASS\n\n");
  12130. + }
  12131. + else {
  12132. + printf("FAIL\n\n");
  12133. + }
  12134. +
  12135. +}
  12136. +
  12137. +#endif /* UNITTEST */
  12138. +
  12139. +#endif /* CONFIG_MACH_IPMATE */
  12140. +
  12141. +#endif /*DWC_CRYPTOLIB */
  12142. --- /dev/null
  12143. +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.h
  12144. @@ -0,0 +1,34 @@
  12145. +/*
  12146. + * dwc_modpow.h
  12147. + * See dwc_modpow.c for license and changes
  12148. + */
  12149. +#ifndef _DWC_MODPOW_H
  12150. +#define _DWC_MODPOW_H
  12151. +
  12152. +#ifdef __cplusplus
  12153. +extern "C" {
  12154. +#endif
  12155. +
  12156. +#include "dwc_os.h"
  12157. +
  12158. +/** @file
  12159. + *
  12160. + * This file defines the module exponentiation function which is only used
  12161. + * internally by the DWC UWB modules for calculation of PKs during numeric
  12162. + * association. The routine is taken from the PUTTY, an open source terminal
  12163. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  12164. + *
  12165. + */
  12166. +
  12167. +typedef uint32_t BignumInt;
  12168. +typedef uint64_t BignumDblInt;
  12169. +typedef BignumInt *Bignum;
  12170. +
  12171. +/* Compute modular exponentiaion */
  12172. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  12173. +
  12174. +#ifdef __cplusplus
  12175. +}
  12176. +#endif
  12177. +
  12178. +#endif /* _LINUX_BIGNUM_H */
  12179. --- /dev/null
  12180. +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.c
  12181. @@ -0,0 +1,319 @@
  12182. +#ifdef DWC_NOTIFYLIB
  12183. +
  12184. +#include "dwc_notifier.h"
  12185. +#include "dwc_list.h"
  12186. +
  12187. +typedef struct dwc_observer {
  12188. + void *observer;
  12189. + dwc_notifier_callback_t callback;
  12190. + void *data;
  12191. + char *notification;
  12192. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  12193. +} observer_t;
  12194. +
  12195. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  12196. +
  12197. +typedef struct dwc_notifier {
  12198. + void *mem_ctx;
  12199. + void *object;
  12200. + struct observer_queue observers;
  12201. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  12202. +} notifier_t;
  12203. +
  12204. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  12205. +
  12206. +typedef struct manager {
  12207. + void *mem_ctx;
  12208. + void *wkq_ctx;
  12209. + dwc_workq_t *wq;
  12210. +// dwc_mutex_t *mutex;
  12211. + struct notifier_queue notifiers;
  12212. +} manager_t;
  12213. +
  12214. +static manager_t *manager = NULL;
  12215. +
  12216. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  12217. +{
  12218. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  12219. + if (!manager) {
  12220. + return -DWC_E_NO_MEMORY;
  12221. + }
  12222. +
  12223. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  12224. +
  12225. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  12226. + if (!manager->wq) {
  12227. + return -DWC_E_NO_MEMORY;
  12228. + }
  12229. +
  12230. + return 0;
  12231. +}
  12232. +
  12233. +static void free_manager(void)
  12234. +{
  12235. + dwc_workq_free(manager->wq);
  12236. +
  12237. + /* All notifiers must have unregistered themselves before this module
  12238. + * can be removed. Hitting this assertion indicates a programmer
  12239. + * error. */
  12240. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  12241. + "Notification manager being freed before all notifiers have been removed");
  12242. + dwc_free(manager->mem_ctx, manager);
  12243. +}
  12244. +
  12245. +#ifdef DEBUG
  12246. +static void dump_manager(void)
  12247. +{
  12248. + notifier_t *n;
  12249. + observer_t *o;
  12250. +
  12251. + DWC_ASSERT(manager, "Notification manager not found");
  12252. +
  12253. + DWC_DEBUG("List of all notifiers and observers:\n");
  12254. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  12255. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  12256. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  12257. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  12258. + }
  12259. + }
  12260. +}
  12261. +#else
  12262. +#define dump_manager(...)
  12263. +#endif
  12264. +
  12265. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  12266. + dwc_notifier_callback_t callback, void *data)
  12267. +{
  12268. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  12269. +
  12270. + if (!new_observer) {
  12271. + return NULL;
  12272. + }
  12273. +
  12274. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  12275. + new_observer->observer = observer;
  12276. + new_observer->notification = notification;
  12277. + new_observer->callback = callback;
  12278. + new_observer->data = data;
  12279. + return new_observer;
  12280. +}
  12281. +
  12282. +static void free_observer(void *mem_ctx, observer_t *observer)
  12283. +{
  12284. + dwc_free(mem_ctx, observer);
  12285. +}
  12286. +
  12287. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  12288. +{
  12289. + notifier_t *notifier;
  12290. +
  12291. + if (!object) {
  12292. + return NULL;
  12293. + }
  12294. +
  12295. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  12296. + if (!notifier) {
  12297. + return NULL;
  12298. + }
  12299. +
  12300. + DWC_CIRCLEQ_INIT(&notifier->observers);
  12301. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  12302. +
  12303. + notifier->mem_ctx = mem_ctx;
  12304. + notifier->object = object;
  12305. + return notifier;
  12306. +}
  12307. +
  12308. +static void free_notifier(notifier_t *notifier)
  12309. +{
  12310. + observer_t *observer;
  12311. +
  12312. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  12313. + free_observer(notifier->mem_ctx, observer);
  12314. + }
  12315. +
  12316. + dwc_free(notifier->mem_ctx, notifier);
  12317. +}
  12318. +
  12319. +static notifier_t *find_notifier(void *object)
  12320. +{
  12321. + notifier_t *notifier;
  12322. +
  12323. + DWC_ASSERT(manager, "Notification manager not found");
  12324. +
  12325. + if (!object) {
  12326. + return NULL;
  12327. + }
  12328. +
  12329. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  12330. + if (notifier->object == object) {
  12331. + return notifier;
  12332. + }
  12333. + }
  12334. +
  12335. + return NULL;
  12336. +}
  12337. +
  12338. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  12339. +{
  12340. + return create_manager(mem_ctx, wkq_ctx);
  12341. +}
  12342. +
  12343. +void dwc_free_notification_manager(void)
  12344. +{
  12345. + free_manager();
  12346. +}
  12347. +
  12348. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  12349. +{
  12350. + notifier_t *notifier;
  12351. +
  12352. + DWC_ASSERT(manager, "Notification manager not found");
  12353. +
  12354. + notifier = find_notifier(object);
  12355. + if (notifier) {
  12356. + DWC_ERROR("Notifier %p is already registered\n", object);
  12357. + return NULL;
  12358. + }
  12359. +
  12360. + notifier = alloc_notifier(mem_ctx, object);
  12361. + if (!notifier) {
  12362. + return NULL;
  12363. + }
  12364. +
  12365. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  12366. +
  12367. + DWC_INFO("Notifier %p registered", object);
  12368. + dump_manager();
  12369. +
  12370. + return notifier;
  12371. +}
  12372. +
  12373. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  12374. +{
  12375. + DWC_ASSERT(manager, "Notification manager not found");
  12376. +
  12377. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  12378. + observer_t *o;
  12379. +
  12380. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  12381. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  12382. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  12383. + }
  12384. +
  12385. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  12386. + "Notifier %p has active observers when removing", notifier);
  12387. + }
  12388. +
  12389. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  12390. + free_notifier(notifier);
  12391. +
  12392. + DWC_INFO("Notifier unregistered");
  12393. + dump_manager();
  12394. +}
  12395. +
  12396. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  12397. +int dwc_add_observer(void *observer, void *object, char *notification,
  12398. + dwc_notifier_callback_t callback, void *data)
  12399. +{
  12400. + notifier_t *notifier = find_notifier(object);
  12401. + observer_t *new_observer;
  12402. +
  12403. + if (!notifier) {
  12404. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  12405. + return -DWC_E_INVALID;
  12406. + }
  12407. +
  12408. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  12409. + if (!new_observer) {
  12410. + return -DWC_E_NO_MEMORY;
  12411. + }
  12412. +
  12413. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  12414. +
  12415. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  12416. + observer, object, notification, callback, data);
  12417. +
  12418. + dump_manager();
  12419. + return 0;
  12420. +}
  12421. +
  12422. +int dwc_remove_observer(void *observer)
  12423. +{
  12424. + notifier_t *n;
  12425. +
  12426. + DWC_ASSERT(manager, "Notification manager not found");
  12427. +
  12428. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  12429. + observer_t *o;
  12430. + observer_t *o2;
  12431. +
  12432. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  12433. + if (o->observer == observer) {
  12434. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  12435. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  12436. + o->observer, n->object, o->notification);
  12437. + free_observer(n->mem_ctx, o);
  12438. + }
  12439. + }
  12440. + }
  12441. +
  12442. + dump_manager();
  12443. + return 0;
  12444. +}
  12445. +
  12446. +typedef struct callback_data {
  12447. + void *mem_ctx;
  12448. + dwc_notifier_callback_t cb;
  12449. + void *observer;
  12450. + void *data;
  12451. + void *object;
  12452. + char *notification;
  12453. + void *notification_data;
  12454. +} cb_data_t;
  12455. +
  12456. +static void cb_task(void *data)
  12457. +{
  12458. + cb_data_t *cb = (cb_data_t *)data;
  12459. +
  12460. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  12461. + dwc_free(cb->mem_ctx, cb);
  12462. +}
  12463. +
  12464. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  12465. +{
  12466. + observer_t *o;
  12467. +
  12468. + DWC_ASSERT(manager, "Notification manager not found");
  12469. +
  12470. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  12471. + int len = DWC_STRLEN(notification);
  12472. +
  12473. + if (DWC_STRLEN(o->notification) != len) {
  12474. + continue;
  12475. + }
  12476. +
  12477. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  12478. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  12479. +
  12480. + if (!cb_data) {
  12481. + DWC_ERROR("Failed to allocate callback data\n");
  12482. + return;
  12483. + }
  12484. +
  12485. + cb_data->mem_ctx = notifier->mem_ctx;
  12486. + cb_data->cb = o->callback;
  12487. + cb_data->observer = o->observer;
  12488. + cb_data->data = o->data;
  12489. + cb_data->object = notifier->object;
  12490. + cb_data->notification = notification;
  12491. + cb_data->notification_data = notification_data;
  12492. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  12493. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  12494. + "Notify callback from %p for Notification %s, to observer %p",
  12495. + cb_data->object, notification, cb_data->observer);
  12496. + }
  12497. + }
  12498. +}
  12499. +
  12500. +#endif /* DWC_NOTIFYLIB */
  12501. --- /dev/null
  12502. +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.h
  12503. @@ -0,0 +1,122 @@
  12504. +
  12505. +#ifndef __DWC_NOTIFIER_H__
  12506. +#define __DWC_NOTIFIER_H__
  12507. +
  12508. +#ifdef __cplusplus
  12509. +extern "C" {
  12510. +#endif
  12511. +
  12512. +#include "dwc_os.h"
  12513. +
  12514. +/** @file
  12515. + *
  12516. + * A simple implementation of the Observer pattern. Any "module" can
  12517. + * register as an observer or notifier. The notion of "module" is abstract and
  12518. + * can mean anything used to identify either an observer or notifier. Usually
  12519. + * it will be a pointer to a data structure which contains some state, ie an
  12520. + * object.
  12521. + *
  12522. + * Before any notifiers can be added, the global notification manager must be
  12523. + * brought up with dwc_alloc_notification_manager().
  12524. + * dwc_free_notification_manager() will bring it down and free all resources.
  12525. + * These would typically be called upon module load and unload. The
  12526. + * notification manager is a single global instance that handles all registered
  12527. + * observable modules and observers so this should be done only once.
  12528. + *
  12529. + * A module can be observable by using Notifications to publicize some general
  12530. + * information about it's state or operation. It does not care who listens, or
  12531. + * even if anyone listens, or what they do with the information. The observable
  12532. + * modules do not need to know any information about it's observers or their
  12533. + * interface, or their state or data.
  12534. + *
  12535. + * Any module can register to emit Notifications. It should publish a list of
  12536. + * notifications that it can emit and their behavior, such as when they will get
  12537. + * triggered, and what information will be provided to the observer. Then it
  12538. + * should register itself as an observable module. See dwc_register_notifier().
  12539. + *
  12540. + * Any module can observe any observable, registered module, provided it has a
  12541. + * handle to the other module and knows what notifications to observe. See
  12542. + * dwc_add_observer().
  12543. + *
  12544. + * A function of type dwc_notifier_callback_t is called whenever a notification
  12545. + * is triggered with one or more observers observing it. This function is
  12546. + * called in it's own process so it may sleep or block if needed. It is
  12547. + * guaranteed to be called sometime after the notification has occurred and will
  12548. + * be called once per each time the notification is triggered. It will NOT be
  12549. + * called in the same process context used to trigger the notification.
  12550. + *
  12551. + * @section Limitiations
  12552. + *
  12553. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  12554. + * schedule too many processes too handle. Be aware of this limitation when
  12555. + * designing to use notifications, and only add notifications for appropriate
  12556. + * observable information.
  12557. + *
  12558. + * Also Notification callbacks are not synchronous. If you need to synchronize
  12559. + * the behavior between module/observer you must use other means. And perhaps
  12560. + * that will mean Notifications are not the proper solution.
  12561. + */
  12562. +
  12563. +struct dwc_notifier;
  12564. +typedef struct dwc_notifier dwc_notifier_t;
  12565. +
  12566. +/** The callback function must be of this type.
  12567. + *
  12568. + * @param object This is the object that is being observed.
  12569. + * @param notification This is the notification that was triggered.
  12570. + * @param observer This is the observer
  12571. + * @param notification_data This is notification-specific data that the notifier
  12572. + * has included in this notification. The value of this should be published in
  12573. + * the documentation of the observable module with the notifications.
  12574. + * @param user_data This is any custom data that the observer provided when
  12575. + * adding itself as an observer to the notification. */
  12576. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  12577. + void *notification_data, void *user_data);
  12578. +
  12579. +/** Brings up the notification manager. */
  12580. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  12581. +/** Brings down the notification manager. */
  12582. +extern void dwc_free_notification_manager(void);
  12583. +
  12584. +/** This function registers an observable module. A dwc_notifier_t object is
  12585. + * returned to the observable module. This is an opaque object that is used by
  12586. + * the observable module to trigger notifications. This object should only be
  12587. + * accessible to functions that are authorized to trigger notifications for this
  12588. + * module. Observers do not need this object. */
  12589. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  12590. +
  12591. +/** This function unregisters an observable module. All observers have to be
  12592. + * removed prior to unregistration. */
  12593. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  12594. +
  12595. +/** Add a module as an observer to the observable module. The observable module
  12596. + * needs to have previously registered with the notification manager.
  12597. + *
  12598. + * @param observer The observer module
  12599. + * @param object The module to observe
  12600. + * @param notification The notification to observe
  12601. + * @param callback The callback function to call
  12602. + * @param user_data Any additional user data to pass into the callback function */
  12603. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  12604. + dwc_notifier_callback_t callback, void *user_data);
  12605. +
  12606. +/** Removes the specified observer from all notifications that it is currently
  12607. + * observing. */
  12608. +extern int dwc_remove_observer(void *observer);
  12609. +
  12610. +/** This function triggers a Notification. It should be called by the
  12611. + * observable module, or any module or library which the observable module
  12612. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  12613. + *
  12614. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  12615. + * their own process context for each trigger. Callbacks can be blocking.
  12616. + * dwc_notify can be called from interrupt context if needed.
  12617. + *
  12618. + */
  12619. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  12620. +
  12621. +#ifdef __cplusplus
  12622. +}
  12623. +#endif
  12624. +
  12625. +#endif /* __DWC_NOTIFIER_H__ */
  12626. --- /dev/null
  12627. +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
  12628. @@ -0,0 +1,1276 @@
  12629. +/* =========================================================================
  12630. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  12631. + * $Revision: #14 $
  12632. + * $Date: 2010/11/04 $
  12633. + * $Change: 1621695 $
  12634. + *
  12635. + * Synopsys Portability Library Software and documentation
  12636. + * (hereinafter, "Software") is an Unsupported proprietary work of
  12637. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  12638. + * between Synopsys and you.
  12639. + *
  12640. + * The Software IS NOT an item of Licensed Software or Licensed Product
  12641. + * under any End User Software License Agreement or Agreement for
  12642. + * Licensed Product with Synopsys or any supplement thereto. You are
  12643. + * permitted to use and redistribute this Software in source and binary
  12644. + * forms, with or without modification, provided that redistributions
  12645. + * of source code must retain this notice. You may not view, use,
  12646. + * disclose, copy or distribute this file or any information contained
  12647. + * herein except pursuant to this license grant from Synopsys. If you
  12648. + * do not agree with this notice, including the disclaimer below, then
  12649. + * you are not authorized to use the Software.
  12650. + *
  12651. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  12652. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  12653. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  12654. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  12655. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12656. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12657. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12658. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  12659. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12660. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  12661. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  12662. + * DAMAGE.
  12663. + * ========================================================================= */
  12664. +#ifndef _DWC_OS_H_
  12665. +#define _DWC_OS_H_
  12666. +
  12667. +#ifdef __cplusplus
  12668. +extern "C" {
  12669. +#endif
  12670. +
  12671. +/** @file
  12672. + *
  12673. + * DWC portability library, low level os-wrapper functions
  12674. + *
  12675. + */
  12676. +
  12677. +/* These basic types need to be defined by some OS header file or custom header
  12678. + * file for your specific target architecture.
  12679. + *
  12680. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  12681. + *
  12682. + * Any custom or alternate header file must be added and enabled here.
  12683. + */
  12684. +
  12685. +#ifdef DWC_LINUX
  12686. +# include <linux/types.h>
  12687. +# ifdef CONFIG_DEBUG_MUTEXES
  12688. +# include <linux/mutex.h>
  12689. +# endif
  12690. +# include <linux/spinlock.h>
  12691. +# include <linux/errno.h>
  12692. +# include <stdarg.h>
  12693. +#endif
  12694. +
  12695. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  12696. +# include <os_dep.h>
  12697. +#endif
  12698. +
  12699. +
  12700. +/** @name Primitive Types and Values */
  12701. +
  12702. +/** We define a boolean type for consistency. Can be either YES or NO */
  12703. +typedef uint8_t dwc_bool_t;
  12704. +#define YES 1
  12705. +#define NO 0
  12706. +
  12707. +#ifdef DWC_LINUX
  12708. +
  12709. +/** @name Error Codes */
  12710. +#define DWC_E_INVALID EINVAL
  12711. +#define DWC_E_NO_MEMORY ENOMEM
  12712. +#define DWC_E_NO_DEVICE ENODEV
  12713. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  12714. +#define DWC_E_TIMEOUT ETIMEDOUT
  12715. +#define DWC_E_BUSY EBUSY
  12716. +#define DWC_E_AGAIN EAGAIN
  12717. +#define DWC_E_RESTART ERESTART
  12718. +#define DWC_E_ABORT ECONNABORTED
  12719. +#define DWC_E_SHUTDOWN ESHUTDOWN
  12720. +#define DWC_E_NO_DATA ENODATA
  12721. +#define DWC_E_DISCONNECT ECONNRESET
  12722. +#define DWC_E_UNKNOWN EINVAL
  12723. +#define DWC_E_NO_STREAM_RES ENOSR
  12724. +#define DWC_E_COMMUNICATION ECOMM
  12725. +#define DWC_E_OVERFLOW EOVERFLOW
  12726. +#define DWC_E_PROTOCOL EPROTO
  12727. +#define DWC_E_IN_PROGRESS EINPROGRESS
  12728. +#define DWC_E_PIPE EPIPE
  12729. +#define DWC_E_IO EIO
  12730. +#define DWC_E_NO_SPACE ENOSPC
  12731. +
  12732. +#else
  12733. +
  12734. +/** @name Error Codes */
  12735. +#define DWC_E_INVALID 1001
  12736. +#define DWC_E_NO_MEMORY 1002
  12737. +#define DWC_E_NO_DEVICE 1003
  12738. +#define DWC_E_NOT_SUPPORTED 1004
  12739. +#define DWC_E_TIMEOUT 1005
  12740. +#define DWC_E_BUSY 1006
  12741. +#define DWC_E_AGAIN 1007
  12742. +#define DWC_E_RESTART 1008
  12743. +#define DWC_E_ABORT 1009
  12744. +#define DWC_E_SHUTDOWN 1010
  12745. +#define DWC_E_NO_DATA 1011
  12746. +#define DWC_E_DISCONNECT 2000
  12747. +#define DWC_E_UNKNOWN 3000
  12748. +#define DWC_E_NO_STREAM_RES 4001
  12749. +#define DWC_E_COMMUNICATION 4002
  12750. +#define DWC_E_OVERFLOW 4003
  12751. +#define DWC_E_PROTOCOL 4004
  12752. +#define DWC_E_IN_PROGRESS 4005
  12753. +#define DWC_E_PIPE 4006
  12754. +#define DWC_E_IO 4007
  12755. +#define DWC_E_NO_SPACE 4008
  12756. +
  12757. +#endif
  12758. +
  12759. +
  12760. +/** @name Tracing/Logging Functions
  12761. + *
  12762. + * These function provide the capability to add tracing, debugging, and error
  12763. + * messages, as well exceptions as assertions. The WUDEV uses these
  12764. + * extensively. These could be logged to the main console, the serial port, an
  12765. + * internal buffer, etc. These functions could also be no-op if they are too
  12766. + * expensive on your system. By default undefining the DEBUG macro already
  12767. + * no-ops some of these functions. */
  12768. +
  12769. +/** Returns non-zero if in interrupt context. */
  12770. +extern dwc_bool_t DWC_IN_IRQ(void);
  12771. +#define dwc_in_irq DWC_IN_IRQ
  12772. +
  12773. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  12774. +static inline char *dwc_irq(void) {
  12775. + return DWC_IN_IRQ() ? "IRQ" : "";
  12776. +}
  12777. +
  12778. +/** Returns non-zero if in bottom-half context. */
  12779. +extern dwc_bool_t DWC_IN_BH(void);
  12780. +#define dwc_in_bh DWC_IN_BH
  12781. +
  12782. +/** Returns "BH" if DWC_IN_BH is true. */
  12783. +static inline char *dwc_bh(void) {
  12784. + return DWC_IN_BH() ? "BH" : "";
  12785. +}
  12786. +
  12787. +/**
  12788. + * A vprintf() clone. Just call vprintf if you've got it.
  12789. + */
  12790. +extern void DWC_VPRINTF(char *format, va_list args);
  12791. +#define dwc_vprintf DWC_VPRINTF
  12792. +
  12793. +/**
  12794. + * A vsnprintf() clone. Just call vprintf if you've got it.
  12795. + */
  12796. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  12797. +#define dwc_vsnprintf DWC_VSNPRINTF
  12798. +
  12799. +/**
  12800. + * printf() clone. Just call printf if you've go it.
  12801. + */
  12802. +extern void DWC_PRINTF(char *format, ...)
  12803. +/* This provides compiler level static checking of the parameters if you're
  12804. + * using GCC. */
  12805. +#ifdef __GNUC__
  12806. + __attribute__ ((format(printf, 1, 2)));
  12807. +#else
  12808. + ;
  12809. +#endif
  12810. +#define dwc_printf DWC_PRINTF
  12811. +
  12812. +/**
  12813. + * sprintf() clone. Just call sprintf if you've got it.
  12814. + */
  12815. +extern int DWC_SPRINTF(char *string, char *format, ...)
  12816. +#ifdef __GNUC__
  12817. + __attribute__ ((format(printf, 2, 3)));
  12818. +#else
  12819. + ;
  12820. +#endif
  12821. +#define dwc_sprintf DWC_SPRINTF
  12822. +
  12823. +/**
  12824. + * snprintf() clone. Just call snprintf if you've got it.
  12825. + */
  12826. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  12827. +#ifdef __GNUC__
  12828. + __attribute__ ((format(printf, 3, 4)));
  12829. +#else
  12830. + ;
  12831. +#endif
  12832. +#define dwc_snprintf DWC_SNPRINTF
  12833. +
  12834. +/**
  12835. + * Prints a WARNING message. On systems that don't differentiate between
  12836. + * warnings and regular log messages, just print it. Indicates that something
  12837. + * may be wrong with the driver. Works like printf().
  12838. + *
  12839. + * Use the DWC_WARN macro to call this function.
  12840. + */
  12841. +extern void __DWC_WARN(char *format, ...)
  12842. +#ifdef __GNUC__
  12843. + __attribute__ ((format(printf, 1, 2)));
  12844. +#else
  12845. + ;
  12846. +#endif
  12847. +
  12848. +/**
  12849. + * Prints an error message. On systems that don't differentiate between errors
  12850. + * and regular log messages, just print it. Indicates that something went wrong
  12851. + * with the driver. Works like printf().
  12852. + *
  12853. + * Use the DWC_ERROR macro to call this function.
  12854. + */
  12855. +extern void __DWC_ERROR(char *format, ...)
  12856. +#ifdef __GNUC__
  12857. + __attribute__ ((format(printf, 1, 2)));
  12858. +#else
  12859. + ;
  12860. +#endif
  12861. +
  12862. +/**
  12863. + * Prints an exception error message and takes some user-defined action such as
  12864. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  12865. + * abnormally wrong with the driver such as programmer error, or other
  12866. + * exceptional condition. It should not be ignored so even on systems without
  12867. + * printing capability, some action should be taken to notify the developer of
  12868. + * it. Works like printf().
  12869. + */
  12870. +extern void DWC_EXCEPTION(char *format, ...)
  12871. +#ifdef __GNUC__
  12872. + __attribute__ ((format(printf, 1, 2)));
  12873. +#else
  12874. + ;
  12875. +#endif
  12876. +#define dwc_exception DWC_EXCEPTION
  12877. +
  12878. +#ifndef DWC_OTG_DEBUG_LEV
  12879. +#define DWC_OTG_DEBUG_LEV 0
  12880. +#endif
  12881. +
  12882. +#ifdef DEBUG
  12883. +/**
  12884. + * Prints out a debug message. Used for logging/trace messages.
  12885. + *
  12886. + * Use the DWC_DEBUG macro to call this function
  12887. + */
  12888. +extern void __DWC_DEBUG(char *format, ...)
  12889. +#ifdef __GNUC__
  12890. + __attribute__ ((format(printf, 1, 2)));
  12891. +#else
  12892. + ;
  12893. +#endif
  12894. +#else
  12895. +#define __DWC_DEBUG printk
  12896. +#endif
  12897. +
  12898. +/**
  12899. + * Prints out a Debug message.
  12900. + */
  12901. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  12902. + __func__, dwc_irq(), ## _args)
  12903. +#define dwc_debug DWC_DEBUG
  12904. +/**
  12905. + * Prints out a Debug message if enabled at compile time.
  12906. + */
  12907. +#if DWC_OTG_DEBUG_LEV > 0
  12908. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  12909. +#else
  12910. +#define DWC_DEBUGC(_format, _args...)
  12911. +#endif
  12912. +#define dwc_debugc DWC_DEBUGC
  12913. +/**
  12914. + * Prints out an informative message.
  12915. + */
  12916. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  12917. + dwc_irq(), ## _args)
  12918. +#define dwc_info DWC_INFO
  12919. +/**
  12920. + * Prints out an informative message if enabled at compile time.
  12921. + */
  12922. +#if DWC_OTG_DEBUG_LEV > 1
  12923. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  12924. +#else
  12925. +#define DWC_INFOC(_format, _args...)
  12926. +#endif
  12927. +#define dwc_infoc DWC_INFOC
  12928. +/**
  12929. + * Prints out a warning message.
  12930. + */
  12931. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  12932. + dwc_irq(), __func__, __LINE__, ## _args)
  12933. +#define dwc_warn DWC_WARN
  12934. +/**
  12935. + * Prints out an error message.
  12936. + */
  12937. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  12938. + dwc_irq(), __func__, __LINE__, ## _args)
  12939. +#define dwc_error DWC_ERROR
  12940. +
  12941. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  12942. + dwc_irq(), __func__, __LINE__, ## _args)
  12943. +#define dwc_proto_error DWC_PROTO_ERROR
  12944. +
  12945. +#ifdef DEBUG
  12946. +/** Prints out a exception error message if the _expr expression fails. Disabled
  12947. + * if DEBUG is not enabled. */
  12948. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  12949. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  12950. + __FILE__, __LINE__, ## _args); } \
  12951. + } while (0)
  12952. +#else
  12953. +#define DWC_ASSERT(_x...)
  12954. +#endif
  12955. +#define dwc_assert DWC_ASSERT
  12956. +
  12957. +
  12958. +/** @name Byte Ordering
  12959. + * The following functions are for conversions between processor's byte ordering
  12960. + * and specific ordering you want.
  12961. + */
  12962. +
  12963. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  12964. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  12965. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  12966. +
  12967. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  12968. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  12969. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  12970. +
  12971. +/** Converts 32 bit little endian data to CPU byte ordering. */
  12972. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  12973. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  12974. +
  12975. +/** Converts 32 bit big endian data to CPU byte ordering. */
  12976. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  12977. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  12978. +
  12979. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  12980. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  12981. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  12982. +
  12983. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  12984. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  12985. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  12986. +
  12987. +/** Converts 16 bit little endian data to CPU byte ordering. */
  12988. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  12989. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  12990. +
  12991. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  12992. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  12993. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  12994. +
  12995. +
  12996. +/** @name Register Read/Write
  12997. + *
  12998. + * The following six functions should be implemented to read/write registers of
  12999. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  13000. + * The reg value is a pointer to the register calculated from the void *base
  13001. + * variable passed into the driver when it is started. */
  13002. +
  13003. +#ifdef DWC_LINUX
  13004. +/* Linux doesn't need any extra parameters for register read/write, so we
  13005. + * just throw away the IO context parameter.
  13006. + */
  13007. +/** Reads the content of a 32-bit register. */
  13008. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  13009. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  13010. +
  13011. +/** Reads the content of a 64-bit register. */
  13012. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  13013. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  13014. +
  13015. +/** Writes to a 32-bit register. */
  13016. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  13017. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  13018. +
  13019. +/** Writes to a 64-bit register. */
  13020. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  13021. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  13022. +
  13023. +/**
  13024. + * Modify bit values in a register. Using the
  13025. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  13026. + */
  13027. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  13028. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  13029. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  13030. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  13031. +
  13032. +#endif /* DWC_LINUX */
  13033. +
  13034. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13035. +typedef struct dwc_ioctx {
  13036. + struct device *dev;
  13037. + bus_space_tag_t iot;
  13038. + bus_space_handle_t ioh;
  13039. +} dwc_ioctx_t;
  13040. +
  13041. +/** BSD needs two extra parameters for register read/write, so we pass
  13042. + * them in using the IO context parameter.
  13043. + */
  13044. +/** Reads the content of a 32-bit register. */
  13045. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  13046. +#define dwc_read_reg32 DWC_READ_REG32
  13047. +
  13048. +/** Reads the content of a 64-bit register. */
  13049. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  13050. +#define dwc_read_reg64 DWC_READ_REG64
  13051. +
  13052. +/** Writes to a 32-bit register. */
  13053. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  13054. +#define dwc_write_reg32 DWC_WRITE_REG32
  13055. +
  13056. +/** Writes to a 64-bit register. */
  13057. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  13058. +#define dwc_write_reg64 DWC_WRITE_REG64
  13059. +
  13060. +/**
  13061. + * Modify bit values in a register. Using the
  13062. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  13063. + */
  13064. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  13065. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  13066. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  13067. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  13068. +
  13069. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  13070. +
  13071. +/** @cond */
  13072. +
  13073. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  13074. + * register writes. */
  13075. +
  13076. +#ifdef DWC_LINUX
  13077. +
  13078. +# ifdef DWC_DEBUG_REGS
  13079. +
  13080. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13081. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  13082. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  13083. +} \
  13084. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  13085. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  13086. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  13087. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  13088. +}
  13089. +
  13090. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13091. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  13092. + return DWC_READ_REG32(&container->regs->_reg); \
  13093. +} \
  13094. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  13095. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  13096. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  13097. +}
  13098. +
  13099. +# else /* DWC_DEBUG_REGS */
  13100. +
  13101. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13102. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  13103. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  13104. +} \
  13105. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  13106. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  13107. +}
  13108. +
  13109. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13110. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  13111. + return DWC_READ_REG32(&container->regs->_reg); \
  13112. +} \
  13113. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  13114. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  13115. +}
  13116. +
  13117. +# endif /* DWC_DEBUG_REGS */
  13118. +
  13119. +#endif /* DWC_LINUX */
  13120. +
  13121. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13122. +
  13123. +# ifdef DWC_DEBUG_REGS
  13124. +
  13125. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13126. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  13127. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  13128. +} \
  13129. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  13130. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  13131. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  13132. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  13133. +}
  13134. +
  13135. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13136. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  13137. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  13138. +} \
  13139. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  13140. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  13141. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  13142. +}
  13143. +
  13144. +# else /* DWC_DEBUG_REGS */
  13145. +
  13146. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13147. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  13148. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  13149. +} \
  13150. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  13151. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  13152. +}
  13153. +
  13154. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13155. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  13156. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  13157. +} \
  13158. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  13159. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  13160. +}
  13161. +
  13162. +# endif /* DWC_DEBUG_REGS */
  13163. +
  13164. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  13165. +
  13166. +/** @endcond */
  13167. +
  13168. +
  13169. +#ifdef DWC_CRYPTOLIB
  13170. +/** @name Crypto Functions
  13171. + *
  13172. + * These are the low-level cryptographic functions used by the driver. */
  13173. +
  13174. +/** Perform AES CBC */
  13175. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  13176. +#define dwc_aes_cbc DWC_AES_CBC
  13177. +
  13178. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  13179. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  13180. +#define dwc_random_bytes DWC_RANDOM_BYTES
  13181. +
  13182. +/** Perform the SHA-256 hash function */
  13183. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  13184. +#define dwc_sha256 DWC_SHA256
  13185. +
  13186. +/** Calculated the HMAC-SHA256 */
  13187. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  13188. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  13189. +
  13190. +#endif /* DWC_CRYPTOLIB */
  13191. +
  13192. +
  13193. +/** @name Memory Allocation
  13194. + *
  13195. + * These function provide access to memory allocation. There are only 2 DMA
  13196. + * functions and 3 Regular memory functions that need to be implemented. None
  13197. + * of the memory debugging routines need to be implemented. The allocation
  13198. + * routines all ZERO the contents of the memory.
  13199. + *
  13200. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  13201. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  13202. + * keeps track of how much memory the driver is using at any given time. */
  13203. +
  13204. +#define DWC_PAGE_SIZE 4096
  13205. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  13206. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  13207. +
  13208. +#define DWC_INVALID_DMA_ADDR 0x0
  13209. +
  13210. +#ifdef DWC_LINUX
  13211. +/** Type for a DMA address */
  13212. +typedef dma_addr_t dwc_dma_t;
  13213. +#endif
  13214. +
  13215. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13216. +typedef bus_addr_t dwc_dma_t;
  13217. +#endif
  13218. +
  13219. +#ifdef DWC_FREEBSD
  13220. +typedef struct dwc_dmactx {
  13221. + struct device *dev;
  13222. + bus_dma_tag_t dma_tag;
  13223. + bus_dmamap_t dma_map;
  13224. + bus_addr_t dma_paddr;
  13225. + void *dma_vaddr;
  13226. +} dwc_dmactx_t;
  13227. +#endif
  13228. +
  13229. +#ifdef DWC_NETBSD
  13230. +typedef struct dwc_dmactx {
  13231. + struct device *dev;
  13232. + bus_dma_tag_t dma_tag;
  13233. + bus_dmamap_t dma_map;
  13234. + bus_dma_segment_t segs[1];
  13235. + int nsegs;
  13236. + bus_addr_t dma_paddr;
  13237. + void *dma_vaddr;
  13238. +} dwc_dmactx_t;
  13239. +#endif
  13240. +
  13241. +/* @todo these functions will be added in the future */
  13242. +#if 0
  13243. +/**
  13244. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  13245. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  13246. + * boundary requirements specified.
  13247. + *
  13248. + * @param[in] size Specifies the size of the buffers that will be allocated from
  13249. + * this pool.
  13250. + * @param[in] align Specifies the byte alignment requirements of the buffers
  13251. + * allocated from this pool. Must be a power of 2.
  13252. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  13253. + * this pool must not cross.
  13254. + *
  13255. + * @returns A pointer to an internal opaque structure which is not to be
  13256. + * accessed outside of these library functions. Use this handle to specify
  13257. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  13258. + * when you are done with it.
  13259. + */
  13260. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  13261. +
  13262. +/**
  13263. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  13264. + */
  13265. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  13266. +
  13267. +/**
  13268. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  13269. + */
  13270. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  13271. +
  13272. +/**
  13273. + * Free a previously allocated buffer from the DMA pool.
  13274. + */
  13275. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  13276. +#endif
  13277. +
  13278. +/** Allocates a DMA capable buffer and zeroes its contents. */
  13279. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  13280. +
  13281. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  13282. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  13283. +
  13284. +/** Frees a previously allocated buffer. */
  13285. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  13286. +
  13287. +/** Allocates a block of memory and zeroes its contents. */
  13288. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  13289. +
  13290. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  13291. + * which can be used inside interrupt context. The size should be sufficiently
  13292. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  13293. + * __DWC_ALLOC if it is atomic. */
  13294. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  13295. +
  13296. +/** Frees a previously allocated buffer. */
  13297. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  13298. +
  13299. +#ifndef DWC_DEBUG_MEMORY
  13300. +
  13301. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  13302. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  13303. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  13304. +
  13305. +# ifdef DWC_LINUX
  13306. +#define DWC_DMA_ALLOC(_size_,_dma_) __DWC_DMA_ALLOC(NULL, _size_, _dma_)
  13307. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) __DWC_DMA_ALLOC_ATOMIC(NULL, _size_,_dma_)
  13308. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) __DWC_DMA_FREE(NULL, _size_, _virt_, _dma_)
  13309. +# endif
  13310. +
  13311. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13312. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  13313. +#define DWC_DMA_FREE __DWC_DMA_FREE
  13314. +# endif
  13315. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  13316. +
  13317. +#else /* DWC_DEBUG_MEMORY */
  13318. +
  13319. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  13320. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  13321. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  13322. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  13323. + char const *func, int line);
  13324. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  13325. + char const *func, int line);
  13326. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  13327. + dwc_dma_t dma_addr, char const *func, int line);
  13328. +
  13329. +extern int dwc_memory_debug_start(void *mem_ctx);
  13330. +extern void dwc_memory_debug_stop(void);
  13331. +extern void dwc_memory_debug_report(void);
  13332. +
  13333. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  13334. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  13335. + __func__, __LINE__)
  13336. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  13337. +
  13338. +# ifdef DWC_LINUX
  13339. +#define DWC_DMA_ALLOC(_size_,_dma_) dwc_dma_alloc_debug(NULL, _size_, \
  13340. + _dma_, __func__, __LINE__)
  13341. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) dwc_dma_alloc_atomic_debug(NULL, _size_, \
  13342. + _dma_, __func__, __LINE__)
  13343. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) dwc_dma_free_debug(NULL, _size_, \
  13344. + _virt_, _dma_, __func__, __LINE__)
  13345. +# endif
  13346. +
  13347. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13348. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  13349. + _dma_, __func__, __LINE__)
  13350. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  13351. + _virt_, _dma_, __func__, __LINE__)
  13352. +# endif
  13353. +
  13354. +#endif /* DWC_DEBUG_MEMORY */
  13355. +
  13356. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  13357. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  13358. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  13359. +
  13360. +#ifdef DWC_LINUX
  13361. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  13362. + * just throw away the DMA context parameter.
  13363. + */
  13364. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  13365. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  13366. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  13367. +#endif
  13368. +
  13369. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13370. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  13371. + * them in using the DMA context parameter.
  13372. + */
  13373. +#define dwc_dma_alloc DWC_DMA_ALLOC
  13374. +#define dwc_dma_free DWC_DMA_FREE
  13375. +#endif
  13376. +
  13377. +
  13378. +/** @name Memory and String Processing */
  13379. +
  13380. +/** memset() clone */
  13381. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  13382. +#define dwc_memset DWC_MEMSET
  13383. +
  13384. +/** memcpy() clone */
  13385. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  13386. +#define dwc_memcpy DWC_MEMCPY
  13387. +
  13388. +/** memmove() clone */
  13389. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  13390. +#define dwc_memmove DWC_MEMMOVE
  13391. +
  13392. +/** memcmp() clone */
  13393. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  13394. +#define dwc_memcmp DWC_MEMCMP
  13395. +
  13396. +/** strcmp() clone */
  13397. +extern int DWC_STRCMP(void *s1, void *s2);
  13398. +#define dwc_strcmp DWC_STRCMP
  13399. +
  13400. +/** strncmp() clone */
  13401. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  13402. +#define dwc_strncmp DWC_STRNCMP
  13403. +
  13404. +/** strlen() clone, for NULL terminated ASCII strings */
  13405. +extern int DWC_STRLEN(char const *str);
  13406. +#define dwc_strlen DWC_STRLEN
  13407. +
  13408. +/** strcpy() clone, for NULL terminated ASCII strings */
  13409. +extern char *DWC_STRCPY(char *to, const char *from);
  13410. +#define dwc_strcpy DWC_STRCPY
  13411. +
  13412. +/** strdup() clone. If you wish to use memory allocation debugging, this
  13413. + * implementation of strdup should use the DWC_* memory routines instead of
  13414. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  13415. + * will not be seen by the debugging routines. */
  13416. +extern char *DWC_STRDUP(char const *str);
  13417. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  13418. +
  13419. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  13420. + * converted from the string str in base 10 unless the string begins with a "0x"
  13421. + * in which case it is base 16. String must be a NULL terminated sequence of
  13422. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  13423. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  13424. + * the number and end with a NULL character. If any invalid characters are
  13425. + * encountered or it returns with a negative error code and the results of the
  13426. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  13427. + * undefined. An example implementation using atoi() can be referenced from the
  13428. + * Linux implementation. */
  13429. +extern int DWC_ATOI(const char *str, int32_t *value);
  13430. +#define dwc_atoi DWC_ATOI
  13431. +
  13432. +/** Same as above but for unsigned. */
  13433. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  13434. +#define dwc_atoui DWC_ATOUI
  13435. +
  13436. +#ifdef DWC_UTFLIB
  13437. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  13438. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  13439. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  13440. +#endif
  13441. +
  13442. +
  13443. +/** @name Wait queues
  13444. + *
  13445. + * Wait queues provide a means of synchronizing between threads or processes. A
  13446. + * process can block on a waitq if some condition is not true, waiting for it to
  13447. + * become true. When the waitq is triggered all waiting process will get
  13448. + * unblocked and the condition will be check again. Waitqs should be triggered
  13449. + * every time a condition can potentially change.*/
  13450. +struct dwc_waitq;
  13451. +
  13452. +/** Type for a waitq */
  13453. +typedef struct dwc_waitq dwc_waitq_t;
  13454. +
  13455. +/** The type of waitq condition callback function. This is called every time
  13456. + * condition is evaluated. */
  13457. +typedef int (*dwc_waitq_condition_t)(void *data);
  13458. +
  13459. +/** Allocate a waitq */
  13460. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  13461. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  13462. +
  13463. +/** Free a waitq */
  13464. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  13465. +#define dwc_waitq_free DWC_WAITQ_FREE
  13466. +
  13467. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  13468. + * condition again. The function returns when the condition becomes true. The return value
  13469. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  13470. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  13471. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  13472. +
  13473. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  13474. + * check the condition again. The function returns when the condition become
  13475. + * true or the timeout has passed. The return value is 0 on condition true or
  13476. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  13477. + * error. */
  13478. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  13479. + void *data, int32_t msecs);
  13480. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  13481. +
  13482. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  13483. + * has potentially changed. */
  13484. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  13485. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  13486. +
  13487. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  13488. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  13489. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  13490. +
  13491. +
  13492. +/** @name Threads
  13493. + *
  13494. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  13495. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  13496. + * returns the value from the thread.
  13497. + */
  13498. +
  13499. +struct dwc_thread;
  13500. +
  13501. +/** Type for a thread */
  13502. +typedef struct dwc_thread dwc_thread_t;
  13503. +
  13504. +/** The thread function */
  13505. +typedef int (*dwc_thread_function_t)(void *data);
  13506. +
  13507. +/** Create a thread and start it running the thread_function. Returns a handle
  13508. + * to the thread */
  13509. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  13510. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  13511. +
  13512. +/** Stops a thread. Return the value returned by the thread. Or will return
  13513. + * DWC_ABORT if the thread never started. */
  13514. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  13515. +#define dwc_thread_stop DWC_THREAD_STOP
  13516. +
  13517. +/** Signifies to the thread that it must stop. */
  13518. +#ifdef DWC_LINUX
  13519. +/* Linux doesn't need any parameters for kthread_should_stop() */
  13520. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  13521. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  13522. +
  13523. +/* No thread_exit function in Linux */
  13524. +#define dwc_thread_exit(_thrd_)
  13525. +#endif
  13526. +
  13527. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13528. +/** BSD needs the thread pointer for kthread_suspend_check() */
  13529. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  13530. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  13531. +
  13532. +/** The thread must call this to exit. */
  13533. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  13534. +#define dwc_thread_exit DWC_THREAD_EXIT
  13535. +#endif
  13536. +
  13537. +
  13538. +/** @name Work queues
  13539. + *
  13540. + * Workqs are used to queue a callback function to be called at some later time,
  13541. + * in another thread. */
  13542. +struct dwc_workq;
  13543. +
  13544. +/** Type for a workq */
  13545. +typedef struct dwc_workq dwc_workq_t;
  13546. +
  13547. +/** The type of the callback function to be called. */
  13548. +typedef void (*dwc_work_callback_t)(void *data);
  13549. +
  13550. +/** Allocate a workq */
  13551. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  13552. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  13553. +
  13554. +/** Free a workq. All work must be completed before being freed. */
  13555. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  13556. +#define dwc_workq_free DWC_WORKQ_FREE
  13557. +
  13558. +/** Schedule a callback on the workq, passing in data. The function will be
  13559. + * scheduled at some later time. */
  13560. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  13561. + void *data, char *format, ...)
  13562. +#ifdef __GNUC__
  13563. + __attribute__ ((format(printf, 4, 5)));
  13564. +#else
  13565. + ;
  13566. +#endif
  13567. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  13568. +
  13569. +/** Schedule a callback on the workq, that will be called until at least
  13570. + * given number miliseconds have passed. */
  13571. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  13572. + void *data, uint32_t time, char *format, ...)
  13573. +#ifdef __GNUC__
  13574. + __attribute__ ((format(printf, 5, 6)));
  13575. +#else
  13576. + ;
  13577. +#endif
  13578. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  13579. +
  13580. +/** The number of processes in the workq */
  13581. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  13582. +#define dwc_workq_pending DWC_WORKQ_PENDING
  13583. +
  13584. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  13585. + * 0 on timeout. */
  13586. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  13587. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  13588. +
  13589. +
  13590. +/** @name Tasklets
  13591. + *
  13592. + */
  13593. +struct dwc_tasklet;
  13594. +
  13595. +/** Type for a tasklet */
  13596. +typedef struct dwc_tasklet dwc_tasklet_t;
  13597. +
  13598. +/** The type of the callback function to be called */
  13599. +typedef void (*dwc_tasklet_callback_t)(void *data);
  13600. +
  13601. +/** Allocates a tasklet */
  13602. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  13603. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  13604. +
  13605. +/** Frees a tasklet */
  13606. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  13607. +#define dwc_task_free DWC_TASK_FREE
  13608. +
  13609. +/** Schedules a tasklet to run */
  13610. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  13611. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  13612. +
  13613. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  13614. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  13615. +
  13616. +/** @name Timer
  13617. + *
  13618. + * Callbacks must be small and atomic.
  13619. + */
  13620. +struct dwc_timer;
  13621. +
  13622. +/** Type for a timer */
  13623. +typedef struct dwc_timer dwc_timer_t;
  13624. +
  13625. +/** The type of the callback function to be called */
  13626. +typedef void (*dwc_timer_callback_t)(void *data);
  13627. +
  13628. +/** Allocates a timer */
  13629. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  13630. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  13631. +
  13632. +/** Frees a timer */
  13633. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  13634. +#define dwc_timer_free DWC_TIMER_FREE
  13635. +
  13636. +/** Schedules the timer to run at time ms from now. And will repeat at every
  13637. + * repeat_interval msec therafter
  13638. + *
  13639. + * Modifies a timer that is still awaiting execution to a new expiration time.
  13640. + * The mod_time is added to the old time. */
  13641. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  13642. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  13643. +
  13644. +/** Disables the timer from execution. */
  13645. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  13646. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  13647. +
  13648. +
  13649. +/** @name Spinlocks
  13650. + *
  13651. + * These locks are used when the work between the lock/unlock is atomic and
  13652. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  13653. + * suitable to lock between interrupt/non-interrupt context. They also lock
  13654. + * between processes if you have multiple CPUs or Preemption. If you don't have
  13655. + * multiple CPUS or Preemption, then the you can simply implement the
  13656. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  13657. + * the work between the lock/unlock is atomic, the process context will never
  13658. + * change, and so you never have to lock between processes. */
  13659. +
  13660. +struct dwc_spinlock;
  13661. +
  13662. +/** Type for a spinlock */
  13663. +typedef struct dwc_spinlock dwc_spinlock_t;
  13664. +
  13665. +/** Type for the 'flags' argument to spinlock funtions */
  13666. +typedef unsigned long dwc_irqflags_t;
  13667. +
  13668. +/** Returns an initialized lock variable. This function should allocate and
  13669. + * initialize the OS-specific data structure used for locking. This data
  13670. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  13671. + * be freed by the DWC_FREE_LOCK when it is no longer used.
  13672. + *
  13673. + * For Linux Spinlock Debugging make it macro because the debugging routines use
  13674. + * the symbol name to determine recursive locking. Using a wrapper function
  13675. + * makes it falsely think recursive locking occurs. */
  13676. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
  13677. +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
  13678. + lock = DWC_ALLOC(sizeof(spinlock_t)); \
  13679. + if (lock) { \
  13680. + spin_lock_init((spinlock_t *)lock); \
  13681. + } \
  13682. +})
  13683. +#else
  13684. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  13685. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  13686. +#endif
  13687. +
  13688. +/** Frees an initialized lock variable. */
  13689. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  13690. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  13691. +
  13692. +/** Disables interrupts and blocks until it acquires the lock.
  13693. + *
  13694. + * @param lock Pointer to the spinlock.
  13695. + * @param flags Unsigned long for irq flags storage.
  13696. + */
  13697. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  13698. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  13699. +
  13700. +/** Re-enables the interrupt and releases the lock.
  13701. + *
  13702. + * @param lock Pointer to the spinlock.
  13703. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  13704. + * passed into DWC_LOCK.
  13705. + */
  13706. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  13707. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  13708. +
  13709. +/** Blocks until it acquires the lock.
  13710. + *
  13711. + * @param lock Pointer to the spinlock.
  13712. + */
  13713. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  13714. +#define dwc_spinlock DWC_SPINLOCK
  13715. +
  13716. +/** Releases the lock.
  13717. + *
  13718. + * @param lock Pointer to the spinlock.
  13719. + */
  13720. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  13721. +#define dwc_spinunlock DWC_SPINUNLOCK
  13722. +
  13723. +
  13724. +/** @name Mutexes
  13725. + *
  13726. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  13727. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  13728. + */
  13729. +
  13730. +struct dwc_mutex;
  13731. +
  13732. +/** Type for a mutex */
  13733. +typedef struct dwc_mutex dwc_mutex_t;
  13734. +
  13735. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  13736. + * the symbol to determine recursive locking. This makes it falsely think
  13737. + * recursive locking occurs. */
  13738. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  13739. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  13740. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  13741. + mutex_init((struct mutex *)__mutexp); \
  13742. +})
  13743. +#endif
  13744. +
  13745. +/** Allocate a mutex */
  13746. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  13747. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  13748. +
  13749. +/* For memory leak debugging when using Linux Mutex Debugging */
  13750. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  13751. +#define DWC_MUTEX_FREE(__mutexp) do { \
  13752. + mutex_destroy((struct mutex *)__mutexp); \
  13753. + DWC_FREE(__mutexp); \
  13754. +} while(0)
  13755. +#else
  13756. +/** Free a mutex */
  13757. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  13758. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  13759. +#endif
  13760. +
  13761. +/** Lock a mutex */
  13762. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  13763. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  13764. +
  13765. +/** Non-blocking lock returns 1 on successful lock. */
  13766. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  13767. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  13768. +
  13769. +/** Unlock a mutex */
  13770. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  13771. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  13772. +
  13773. +
  13774. +/** @name Time */
  13775. +
  13776. +/** Microsecond delay.
  13777. + *
  13778. + * @param usecs Microseconds to delay.
  13779. + */
  13780. +extern void DWC_UDELAY(uint32_t usecs);
  13781. +#define dwc_udelay DWC_UDELAY
  13782. +
  13783. +/** Millisecond delay.
  13784. + *
  13785. + * @param msecs Milliseconds to delay.
  13786. + */
  13787. +extern void DWC_MDELAY(uint32_t msecs);
  13788. +#define dwc_mdelay DWC_MDELAY
  13789. +
  13790. +/** Non-busy waiting.
  13791. + * Sleeps for specified number of milliseconds.
  13792. + *
  13793. + * @param msecs Milliseconds to sleep.
  13794. + */
  13795. +extern void DWC_MSLEEP(uint32_t msecs);
  13796. +#define dwc_msleep DWC_MSLEEP
  13797. +
  13798. +/**
  13799. + * Returns number of milliseconds since boot.
  13800. + */
  13801. +extern uint32_t DWC_TIME(void);
  13802. +#define dwc_time DWC_TIME
  13803. +
  13804. +
  13805. +
  13806. +
  13807. +/* @mainpage DWC Portability and Common Library
  13808. + *
  13809. + * This is the documentation for the DWC Portability and Common Library.
  13810. + *
  13811. + * @section intro Introduction
  13812. + *
  13813. + * The DWC Portability library consists of wrapper calls and data structures to
  13814. + * all low-level functions which are typically provided by the OS. The WUDEV
  13815. + * driver uses only these functions. In order to port the WUDEV driver, only
  13816. + * the functions in this library need to be re-implemented, with the same
  13817. + * behavior as documented here.
  13818. + *
  13819. + * The Common library consists of higher level functions, which rely only on
  13820. + * calling the functions from the DWC Portability library. These common
  13821. + * routines are shared across modules. Some of the common libraries need to be
  13822. + * used directly by the driver programmer when porting WUDEV. Such as the
  13823. + * parameter and notification libraries.
  13824. + *
  13825. + * @section low Portability Library OS Wrapper Functions
  13826. + *
  13827. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  13828. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  13829. + * these functions are included in the dwc_os.h file.
  13830. + *
  13831. + * There are many functions here covering a wide array of OS services. Please
  13832. + * see dwc_os.h for details, and implementation notes for each function.
  13833. + *
  13834. + * @section common Common Library Functions
  13835. + *
  13836. + * Any function starting with dwc and in all lowercase is a common library
  13837. + * routine. These functions have a portable implementation and do not need to
  13838. + * be reimplemented when porting. The common routines can be used by any
  13839. + * driver, and some must be used by the end user to control the drivers. For
  13840. + * example, you must use the Parameter common library in order to set the
  13841. + * parameters in the WUDEV module.
  13842. + *
  13843. + * The common libraries consist of the following:
  13844. + *
  13845. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  13846. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  13847. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  13848. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  13849. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  13850. + * - Modpow - Used internally only. See dwc_modpow.h
  13851. + * - DH - Used internally only. See dwc_dh.h
  13852. + * - Crypto - Used internally only. See dwc_crypto.h
  13853. + *
  13854. + *
  13855. + * @section prereq Prerequistes For dwc_os.h
  13856. + * @subsection types Data Types
  13857. + *
  13858. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  13859. + * compilation environment. These data types are:
  13860. + *
  13861. + * - uint8_t - unsigned 8-bit data type
  13862. + * - int8_t - signed 8-bit data type
  13863. + * - uint16_t - unsigned 16-bit data type
  13864. + * - int16_t - signed 16-bit data type
  13865. + * - uint32_t - unsigned 32-bit data type
  13866. + * - int32_t - signed 32-bit data type
  13867. + * - uint64_t - unsigned 64-bit data type
  13868. + * - int64_t - signed 64-bit data type
  13869. + *
  13870. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  13871. + * that is to modify the top of the file to include the appropriate header.
  13872. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  13873. + * defined, the correct header will be added. A standard header <stdint.h> is
  13874. + * also used for environments where standard C headers are available.
  13875. + *
  13876. + * @subsection stdarg Variable Arguments
  13877. + *
  13878. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  13879. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  13880. + * provided in your enviornment in order to use dwc_os.h with the debug and
  13881. + * tracing message functionality.
  13882. + *
  13883. + * @subsection thread Threading
  13884. + *
  13885. + * WUDEV Core must be run on an operating system that provides for multiple
  13886. + * threads/processes. Threading can be implemented in many ways, even in
  13887. + * embedded systems without an operating system. At the bare minimum, the
  13888. + * system should be able to start any number of processes at any time to handle
  13889. + * special work. It need not be a pre-emptive system. Process context can
  13890. + * change upon a call to a blocking function. The hardware interrupt context
  13891. + * that calls the module's ISR() function must be differentiable from process
  13892. + * context, even if your processes are impemented via a hardware interrupt.
  13893. + * Further locking mechanism between process must exist (or be implemented), and
  13894. + * process context must have a way to disable interrupts for a period of time to
  13895. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  13896. + * threading should be able to be implemented with the defined behavior.
  13897. + *
  13898. + */
  13899. +
  13900. +#ifdef __cplusplus
  13901. +}
  13902. +#endif
  13903. +
  13904. +#endif /* _DWC_OS_H_ */
  13905. --- /dev/null
  13906. +++ b/drivers/usb/host/dwc_common_port/usb.h
  13907. @@ -0,0 +1,946 @@
  13908. +/*
  13909. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  13910. + * All rights reserved.
  13911. + *
  13912. + * This code is derived from software contributed to The NetBSD Foundation
  13913. + * by Lennart Augustsson (lennart@augustsson.net) at
  13914. + * Carlstedt Research & Technology.
  13915. + *
  13916. + * Redistribution and use in source and binary forms, with or without
  13917. + * modification, are permitted provided that the following conditions
  13918. + * are met:
  13919. + * 1. Redistributions of source code must retain the above copyright
  13920. + * notice, this list of conditions and the following disclaimer.
  13921. + * 2. Redistributions in binary form must reproduce the above copyright
  13922. + * notice, this list of conditions and the following disclaimer in the
  13923. + * documentation and/or other materials provided with the distribution.
  13924. + * 3. All advertising materials mentioning features or use of this software
  13925. + * must display the following acknowledgement:
  13926. + * This product includes software developed by the NetBSD
  13927. + * Foundation, Inc. and its contributors.
  13928. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  13929. + * contributors may be used to endorse or promote products derived
  13930. + * from this software without specific prior written permission.
  13931. + *
  13932. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  13933. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  13934. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  13935. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  13936. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  13937. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  13938. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13939. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  13940. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  13941. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  13942. + * POSSIBILITY OF SUCH DAMAGE.
  13943. + */
  13944. +
  13945. +/* Modified by Synopsys, Inc, 12/12/2007 */
  13946. +
  13947. +
  13948. +#ifndef _USB_H_
  13949. +#define _USB_H_
  13950. +
  13951. +#ifdef __cplusplus
  13952. +extern "C" {
  13953. +#endif
  13954. +
  13955. +/*
  13956. + * The USB records contain some unaligned little-endian word
  13957. + * components. The U[SG]ETW macros take care of both the alignment
  13958. + * and endian problem and should always be used to access non-byte
  13959. + * values.
  13960. + */
  13961. +typedef u_int8_t uByte;
  13962. +typedef u_int8_t uWord[2];
  13963. +typedef u_int8_t uDWord[4];
  13964. +
  13965. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  13966. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  13967. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  13968. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  13969. +
  13970. +#if 1
  13971. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  13972. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  13973. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  13974. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  13975. + (w)[1] = (u_int8_t)((v) >> 8), \
  13976. + (w)[2] = (u_int8_t)((v) >> 16), \
  13977. + (w)[3] = (u_int8_t)((v) >> 24))
  13978. +#else
  13979. +/*
  13980. + * On little-endian machines that can handle unanliged accesses
  13981. + * (e.g. i386) these macros can be replaced by the following.
  13982. + */
  13983. +#define UGETW(w) (*(u_int16_t *)(w))
  13984. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  13985. +#define UGETDW(w) (*(u_int32_t *)(w))
  13986. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  13987. +#endif
  13988. +
  13989. +/*
  13990. + * Macros for accessing UAS IU fields, which are big-endian
  13991. + */
  13992. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  13993. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  13994. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  13995. + ((x) >> 8) & 0xff, (x) & 0xff }
  13996. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  13997. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  13998. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  13999. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  14000. + (w)[1] = (u_int8_t)((v) >> 16), \
  14001. + (w)[2] = (u_int8_t)((v) >> 8), \
  14002. + (w)[3] = (u_int8_t)(v))
  14003. +
  14004. +#define UPACKED __attribute__((__packed__))
  14005. +
  14006. +typedef struct {
  14007. + uByte bmRequestType;
  14008. + uByte bRequest;
  14009. + uWord wValue;
  14010. + uWord wIndex;
  14011. + uWord wLength;
  14012. +} UPACKED usb_device_request_t;
  14013. +
  14014. +#define UT_GET_DIR(a) ((a) & 0x80)
  14015. +#define UT_WRITE 0x00
  14016. +#define UT_READ 0x80
  14017. +
  14018. +#define UT_GET_TYPE(a) ((a) & 0x60)
  14019. +#define UT_STANDARD 0x00
  14020. +#define UT_CLASS 0x20
  14021. +#define UT_VENDOR 0x40
  14022. +
  14023. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  14024. +#define UT_DEVICE 0x00
  14025. +#define UT_INTERFACE 0x01
  14026. +#define UT_ENDPOINT 0x02
  14027. +#define UT_OTHER 0x03
  14028. +
  14029. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  14030. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  14031. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  14032. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  14033. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  14034. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  14035. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  14036. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  14037. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  14038. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  14039. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  14040. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  14041. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  14042. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  14043. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  14044. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  14045. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  14046. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  14047. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  14048. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  14049. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  14050. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  14051. +
  14052. +/* Requests */
  14053. +#define UR_GET_STATUS 0x00
  14054. +#define USTAT_STANDARD_STATUS 0x00
  14055. +#define WUSTAT_WUSB_FEATURE 0x01
  14056. +#define WUSTAT_CHANNEL_INFO 0x02
  14057. +#define WUSTAT_RECEIVED_DATA 0x03
  14058. +#define WUSTAT_MAS_AVAILABILITY 0x04
  14059. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  14060. +#define UR_CLEAR_FEATURE 0x01
  14061. +#define UR_SET_FEATURE 0x03
  14062. +#define UR_SET_AND_TEST_FEATURE 0x0c
  14063. +#define UR_SET_ADDRESS 0x05
  14064. +#define UR_GET_DESCRIPTOR 0x06
  14065. +#define UDESC_DEVICE 0x01
  14066. +#define UDESC_CONFIG 0x02
  14067. +#define UDESC_STRING 0x03
  14068. +#define UDESC_INTERFACE 0x04
  14069. +#define UDESC_ENDPOINT 0x05
  14070. +#define UDESC_SS_USB_COMPANION 0x30
  14071. +#define UDESC_DEVICE_QUALIFIER 0x06
  14072. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  14073. +#define UDESC_INTERFACE_POWER 0x08
  14074. +#define UDESC_OTG 0x09
  14075. +#define WUDESC_SECURITY 0x0c
  14076. +#define WUDESC_KEY 0x0d
  14077. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  14078. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  14079. +#define WUD_KEY_TYPE_ASSOC 0x01
  14080. +#define WUD_KEY_TYPE_GTK 0x02
  14081. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  14082. +#define WUD_KEY_ORIGIN_HOST 0x00
  14083. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  14084. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  14085. +#define WUDESC_BOS 0x0f
  14086. +#define WUDESC_DEVICE_CAPABILITY 0x10
  14087. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  14088. +#define UDESC_BOS 0x0f
  14089. +#define UDESC_DEVICE_CAPABILITY 0x10
  14090. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  14091. +#define UDESC_CS_CONFIG 0x22
  14092. +#define UDESC_CS_STRING 0x23
  14093. +#define UDESC_CS_INTERFACE 0x24
  14094. +#define UDESC_CS_ENDPOINT 0x25
  14095. +#define UDESC_HUB 0x29
  14096. +#define UR_SET_DESCRIPTOR 0x07
  14097. +#define UR_GET_CONFIG 0x08
  14098. +#define UR_SET_CONFIG 0x09
  14099. +#define UR_GET_INTERFACE 0x0a
  14100. +#define UR_SET_INTERFACE 0x0b
  14101. +#define UR_SYNCH_FRAME 0x0c
  14102. +#define WUR_SET_ENCRYPTION 0x0d
  14103. +#define WUR_GET_ENCRYPTION 0x0e
  14104. +#define WUR_SET_HANDSHAKE 0x0f
  14105. +#define WUR_GET_HANDSHAKE 0x10
  14106. +#define WUR_SET_CONNECTION 0x11
  14107. +#define WUR_SET_SECURITY_DATA 0x12
  14108. +#define WUR_GET_SECURITY_DATA 0x13
  14109. +#define WUR_SET_WUSB_DATA 0x14
  14110. +#define WUDATA_DRPIE_INFO 0x01
  14111. +#define WUDATA_TRANSMIT_DATA 0x02
  14112. +#define WUDATA_TRANSMIT_PARAMS 0x03
  14113. +#define WUDATA_RECEIVE_PARAMS 0x04
  14114. +#define WUDATA_TRANSMIT_POWER 0x05
  14115. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  14116. +#define WUR_LOOPBACK_DATA_READ 0x16
  14117. +#define WUR_SET_INTERFACE_DS 0x17
  14118. +
  14119. +/* Feature numbers */
  14120. +#define UF_ENDPOINT_HALT 0
  14121. +#define UF_DEVICE_REMOTE_WAKEUP 1
  14122. +#define UF_TEST_MODE 2
  14123. +#define UF_DEVICE_B_HNP_ENABLE 3
  14124. +#define UF_DEVICE_A_HNP_SUPPORT 4
  14125. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  14126. +#define WUF_WUSB 3
  14127. +#define WUF_TX_DRPIE 0x0
  14128. +#define WUF_DEV_XMIT_PACKET 0x1
  14129. +#define WUF_COUNT_PACKETS 0x2
  14130. +#define WUF_CAPTURE_PACKETS 0x3
  14131. +#define UF_FUNCTION_SUSPEND 0
  14132. +#define UF_U1_ENABLE 48
  14133. +#define UF_U2_ENABLE 49
  14134. +#define UF_LTM_ENABLE 50
  14135. +
  14136. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  14137. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  14138. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  14139. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  14140. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  14141. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  14142. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  14143. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  14144. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  14145. +
  14146. +#ifdef _MSC_VER
  14147. +#include <pshpack1.h>
  14148. +#endif
  14149. +
  14150. +typedef struct {
  14151. + uByte bLength;
  14152. + uByte bDescriptorType;
  14153. + uByte bDescriptorSubtype;
  14154. +} UPACKED usb_descriptor_t;
  14155. +
  14156. +typedef struct {
  14157. + uByte bLength;
  14158. + uByte bDescriptorType;
  14159. +} UPACKED usb_descriptor_header_t;
  14160. +
  14161. +typedef struct {
  14162. + uByte bLength;
  14163. + uByte bDescriptorType;
  14164. + uWord bcdUSB;
  14165. +#define UD_USB_2_0 0x0200
  14166. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  14167. + uByte bDeviceClass;
  14168. + uByte bDeviceSubClass;
  14169. + uByte bDeviceProtocol;
  14170. + uByte bMaxPacketSize;
  14171. + /* The fields below are not part of the initial descriptor. */
  14172. + uWord idVendor;
  14173. + uWord idProduct;
  14174. + uWord bcdDevice;
  14175. + uByte iManufacturer;
  14176. + uByte iProduct;
  14177. + uByte iSerialNumber;
  14178. + uByte bNumConfigurations;
  14179. +} UPACKED usb_device_descriptor_t;
  14180. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  14181. +
  14182. +typedef struct {
  14183. + uByte bLength;
  14184. + uByte bDescriptorType;
  14185. + uWord wTotalLength;
  14186. + uByte bNumInterface;
  14187. + uByte bConfigurationValue;
  14188. + uByte iConfiguration;
  14189. +#define UC_ATT_ONE (1 << 7) /* must be set */
  14190. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  14191. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  14192. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  14193. + uByte bmAttributes;
  14194. +#define UC_BUS_POWERED 0x80
  14195. +#define UC_SELF_POWERED 0x40
  14196. +#define UC_REMOTE_WAKEUP 0x20
  14197. + uByte bMaxPower; /* max current in 2 mA units */
  14198. +#define UC_POWER_FACTOR 2
  14199. +} UPACKED usb_config_descriptor_t;
  14200. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  14201. +
  14202. +typedef struct {
  14203. + uByte bLength;
  14204. + uByte bDescriptorType;
  14205. + uByte bInterfaceNumber;
  14206. + uByte bAlternateSetting;
  14207. + uByte bNumEndpoints;
  14208. + uByte bInterfaceClass;
  14209. + uByte bInterfaceSubClass;
  14210. + uByte bInterfaceProtocol;
  14211. + uByte iInterface;
  14212. +} UPACKED usb_interface_descriptor_t;
  14213. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  14214. +
  14215. +typedef struct {
  14216. + uByte bLength;
  14217. + uByte bDescriptorType;
  14218. + uByte bEndpointAddress;
  14219. +#define UE_GET_DIR(a) ((a) & 0x80)
  14220. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  14221. +#define UE_DIR_IN 0x80
  14222. +#define UE_DIR_OUT 0x00
  14223. +#define UE_ADDR 0x0f
  14224. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  14225. + uByte bmAttributes;
  14226. +#define UE_XFERTYPE 0x03
  14227. +#define UE_CONTROL 0x00
  14228. +#define UE_ISOCHRONOUS 0x01
  14229. +#define UE_BULK 0x02
  14230. +#define UE_INTERRUPT 0x03
  14231. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  14232. +#define UE_ISO_TYPE 0x0c
  14233. +#define UE_ISO_ASYNC 0x04
  14234. +#define UE_ISO_ADAPT 0x08
  14235. +#define UE_ISO_SYNC 0x0c
  14236. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  14237. + uWord wMaxPacketSize;
  14238. + uByte bInterval;
  14239. +} UPACKED usb_endpoint_descriptor_t;
  14240. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  14241. +
  14242. +typedef struct ss_endpoint_companion_descriptor {
  14243. + uByte bLength;
  14244. + uByte bDescriptorType;
  14245. + uByte bMaxBurst;
  14246. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  14247. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  14248. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  14249. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  14250. + uByte bmAttributes;
  14251. + uWord wBytesPerInterval;
  14252. +} UPACKED ss_endpoint_companion_descriptor_t;
  14253. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  14254. +
  14255. +typedef struct {
  14256. + uByte bLength;
  14257. + uByte bDescriptorType;
  14258. + uWord bString[127];
  14259. +} UPACKED usb_string_descriptor_t;
  14260. +#define USB_MAX_STRING_LEN 128
  14261. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  14262. +
  14263. +/* Hub specific request */
  14264. +#define UR_GET_BUS_STATE 0x02
  14265. +#define UR_CLEAR_TT_BUFFER 0x08
  14266. +#define UR_RESET_TT 0x09
  14267. +#define UR_GET_TT_STATE 0x0a
  14268. +#define UR_STOP_TT 0x0b
  14269. +
  14270. +/* Hub features */
  14271. +#define UHF_C_HUB_LOCAL_POWER 0
  14272. +#define UHF_C_HUB_OVER_CURRENT 1
  14273. +#define UHF_PORT_CONNECTION 0
  14274. +#define UHF_PORT_ENABLE 1
  14275. +#define UHF_PORT_SUSPEND 2
  14276. +#define UHF_PORT_OVER_CURRENT 3
  14277. +#define UHF_PORT_RESET 4
  14278. +#define UHF_PORT_L1 5
  14279. +#define UHF_PORT_POWER 8
  14280. +#define UHF_PORT_LOW_SPEED 9
  14281. +#define UHF_PORT_HIGH_SPEED 10
  14282. +#define UHF_C_PORT_CONNECTION 16
  14283. +#define UHF_C_PORT_ENABLE 17
  14284. +#define UHF_C_PORT_SUSPEND 18
  14285. +#define UHF_C_PORT_OVER_CURRENT 19
  14286. +#define UHF_C_PORT_RESET 20
  14287. +#define UHF_C_PORT_L1 23
  14288. +#define UHF_PORT_TEST 21
  14289. +#define UHF_PORT_INDICATOR 22
  14290. +
  14291. +typedef struct {
  14292. + uByte bDescLength;
  14293. + uByte bDescriptorType;
  14294. + uByte bNbrPorts;
  14295. + uWord wHubCharacteristics;
  14296. +#define UHD_PWR 0x0003
  14297. +#define UHD_PWR_GANGED 0x0000
  14298. +#define UHD_PWR_INDIVIDUAL 0x0001
  14299. +#define UHD_PWR_NO_SWITCH 0x0002
  14300. +#define UHD_COMPOUND 0x0004
  14301. +#define UHD_OC 0x0018
  14302. +#define UHD_OC_GLOBAL 0x0000
  14303. +#define UHD_OC_INDIVIDUAL 0x0008
  14304. +#define UHD_OC_NONE 0x0010
  14305. +#define UHD_TT_THINK 0x0060
  14306. +#define UHD_TT_THINK_8 0x0000
  14307. +#define UHD_TT_THINK_16 0x0020
  14308. +#define UHD_TT_THINK_24 0x0040
  14309. +#define UHD_TT_THINK_32 0x0060
  14310. +#define UHD_PORT_IND 0x0080
  14311. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  14312. +#define UHD_PWRON_FACTOR 2
  14313. + uByte bHubContrCurrent;
  14314. + uByte DeviceRemovable[32]; /* max 255 ports */
  14315. +#define UHD_NOT_REMOV(desc, i) \
  14316. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  14317. + /* deprecated */ uByte PortPowerCtrlMask[1];
  14318. +} UPACKED usb_hub_descriptor_t;
  14319. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  14320. +
  14321. +typedef struct {
  14322. + uByte bLength;
  14323. + uByte bDescriptorType;
  14324. + uWord bcdUSB;
  14325. + uByte bDeviceClass;
  14326. + uByte bDeviceSubClass;
  14327. + uByte bDeviceProtocol;
  14328. + uByte bMaxPacketSize0;
  14329. + uByte bNumConfigurations;
  14330. + uByte bReserved;
  14331. +} UPACKED usb_device_qualifier_t;
  14332. +#define USB_DEVICE_QUALIFIER_SIZE 10
  14333. +
  14334. +typedef struct {
  14335. + uByte bLength;
  14336. + uByte bDescriptorType;
  14337. + uByte bmAttributes;
  14338. +#define UOTG_SRP 0x01
  14339. +#define UOTG_HNP 0x02
  14340. +} UPACKED usb_otg_descriptor_t;
  14341. +
  14342. +/* OTG feature selectors */
  14343. +#define UOTG_B_HNP_ENABLE 3
  14344. +#define UOTG_A_HNP_SUPPORT 4
  14345. +#define UOTG_A_ALT_HNP_SUPPORT 5
  14346. +
  14347. +typedef struct {
  14348. + uWord wStatus;
  14349. +/* Device status flags */
  14350. +#define UDS_SELF_POWERED 0x0001
  14351. +#define UDS_REMOTE_WAKEUP 0x0002
  14352. +/* Endpoint status flags */
  14353. +#define UES_HALT 0x0001
  14354. +} UPACKED usb_status_t;
  14355. +
  14356. +typedef struct {
  14357. + uWord wHubStatus;
  14358. +#define UHS_LOCAL_POWER 0x0001
  14359. +#define UHS_OVER_CURRENT 0x0002
  14360. + uWord wHubChange;
  14361. +} UPACKED usb_hub_status_t;
  14362. +
  14363. +typedef struct {
  14364. + uWord wPortStatus;
  14365. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  14366. +#define UPS_PORT_ENABLED 0x0002
  14367. +#define UPS_SUSPEND 0x0004
  14368. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  14369. +#define UPS_RESET 0x0010
  14370. +#define UPS_PORT_POWER 0x0100
  14371. +#define UPS_LOW_SPEED 0x0200
  14372. +#define UPS_HIGH_SPEED 0x0400
  14373. +#define UPS_PORT_TEST 0x0800
  14374. +#define UPS_PORT_INDICATOR 0x1000
  14375. + uWord wPortChange;
  14376. +#define UPS_C_CONNECT_STATUS 0x0001
  14377. +#define UPS_C_PORT_ENABLED 0x0002
  14378. +#define UPS_C_SUSPEND 0x0004
  14379. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  14380. +#define UPS_C_PORT_RESET 0x0010
  14381. +} UPACKED usb_port_status_t;
  14382. +
  14383. +#ifdef _MSC_VER
  14384. +#include <poppack.h>
  14385. +#endif
  14386. +
  14387. +/* Device class codes */
  14388. +#define UDCLASS_IN_INTERFACE 0x00
  14389. +#define UDCLASS_COMM 0x02
  14390. +#define UDCLASS_HUB 0x09
  14391. +#define UDSUBCLASS_HUB 0x00
  14392. +#define UDPROTO_FSHUB 0x00
  14393. +#define UDPROTO_HSHUBSTT 0x01
  14394. +#define UDPROTO_HSHUBMTT 0x02
  14395. +#define UDCLASS_DIAGNOSTIC 0xdc
  14396. +#define UDCLASS_WIRELESS 0xe0
  14397. +#define UDSUBCLASS_RF 0x01
  14398. +#define UDPROTO_BLUETOOTH 0x01
  14399. +#define UDCLASS_VENDOR 0xff
  14400. +
  14401. +/* Interface class codes */
  14402. +#define UICLASS_UNSPEC 0x00
  14403. +
  14404. +#define UICLASS_AUDIO 0x01
  14405. +#define UISUBCLASS_AUDIOCONTROL 1
  14406. +#define UISUBCLASS_AUDIOSTREAM 2
  14407. +#define UISUBCLASS_MIDISTREAM 3
  14408. +
  14409. +#define UICLASS_CDC 0x02 /* communication */
  14410. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  14411. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  14412. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  14413. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  14414. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  14415. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  14416. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  14417. +#define UIPROTO_CDC_AT 1
  14418. +
  14419. +#define UICLASS_HID 0x03
  14420. +#define UISUBCLASS_BOOT 1
  14421. +#define UIPROTO_BOOT_KEYBOARD 1
  14422. +
  14423. +#define UICLASS_PHYSICAL 0x05
  14424. +
  14425. +#define UICLASS_IMAGE 0x06
  14426. +
  14427. +#define UICLASS_PRINTER 0x07
  14428. +#define UISUBCLASS_PRINTER 1
  14429. +#define UIPROTO_PRINTER_UNI 1
  14430. +#define UIPROTO_PRINTER_BI 2
  14431. +#define UIPROTO_PRINTER_1284 3
  14432. +
  14433. +#define UICLASS_MASS 0x08
  14434. +#define UISUBCLASS_RBC 1
  14435. +#define UISUBCLASS_SFF8020I 2
  14436. +#define UISUBCLASS_QIC157 3
  14437. +#define UISUBCLASS_UFI 4
  14438. +#define UISUBCLASS_SFF8070I 5
  14439. +#define UISUBCLASS_SCSI 6
  14440. +#define UIPROTO_MASS_CBI_I 0
  14441. +#define UIPROTO_MASS_CBI 1
  14442. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  14443. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  14444. +
  14445. +#define UICLASS_HUB 0x09
  14446. +#define UISUBCLASS_HUB 0
  14447. +#define UIPROTO_FSHUB 0
  14448. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  14449. +#define UIPROTO_HSHUBMTT 1
  14450. +
  14451. +#define UICLASS_CDC_DATA 0x0a
  14452. +#define UISUBCLASS_DATA 0
  14453. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  14454. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  14455. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  14456. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  14457. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  14458. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  14459. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  14460. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  14461. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  14462. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  14463. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  14464. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  14465. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  14466. +
  14467. +#define UICLASS_SMARTCARD 0x0b
  14468. +
  14469. +/*#define UICLASS_FIRM_UPD 0x0c*/
  14470. +
  14471. +#define UICLASS_SECURITY 0x0d
  14472. +
  14473. +#define UICLASS_DIAGNOSTIC 0xdc
  14474. +
  14475. +#define UICLASS_WIRELESS 0xe0
  14476. +#define UISUBCLASS_RF 0x01
  14477. +#define UIPROTO_BLUETOOTH 0x01
  14478. +
  14479. +#define UICLASS_APPL_SPEC 0xfe
  14480. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  14481. +#define UISUBCLASS_IRDA 2
  14482. +#define UIPROTO_IRDA 0
  14483. +
  14484. +#define UICLASS_VENDOR 0xff
  14485. +
  14486. +#define USB_HUB_MAX_DEPTH 5
  14487. +
  14488. +/*
  14489. + * Minimum time a device needs to be powered down to go through
  14490. + * a power cycle. XXX Are these time in the spec?
  14491. + */
  14492. +#define USB_POWER_DOWN_TIME 200 /* ms */
  14493. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  14494. +
  14495. +#if 0
  14496. +/* These are the values from the spec. */
  14497. +#define USB_PORT_RESET_DELAY 10 /* ms */
  14498. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  14499. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  14500. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  14501. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  14502. +#define USB_RESUME_DELAY (20*5) /* ms */
  14503. +#define USB_RESUME_WAIT 10 /* ms */
  14504. +#define USB_RESUME_RECOVERY 10 /* ms */
  14505. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  14506. +#else
  14507. +/* Allow for marginal (i.e. non-conforming) devices. */
  14508. +#define USB_PORT_RESET_DELAY 50 /* ms */
  14509. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  14510. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  14511. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  14512. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  14513. +#define USB_RESUME_DELAY (50*5) /* ms */
  14514. +#define USB_RESUME_WAIT 50 /* ms */
  14515. +#define USB_RESUME_RECOVERY 50 /* ms */
  14516. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  14517. +#endif
  14518. +
  14519. +#define USB_MIN_POWER 100 /* mA */
  14520. +#define USB_MAX_POWER 500 /* mA */
  14521. +
  14522. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  14523. +
  14524. +#define USB_UNCONFIG_NO 0
  14525. +#define USB_UNCONFIG_INDEX (-1)
  14526. +
  14527. +/*** ioctl() related stuff ***/
  14528. +
  14529. +struct usb_ctl_request {
  14530. + int ucr_addr;
  14531. + usb_device_request_t ucr_request;
  14532. + void *ucr_data;
  14533. + int ucr_flags;
  14534. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  14535. + int ucr_actlen; /* actual length transferred */
  14536. +};
  14537. +
  14538. +struct usb_alt_interface {
  14539. + int uai_config_index;
  14540. + int uai_interface_index;
  14541. + int uai_alt_no;
  14542. +};
  14543. +
  14544. +#define USB_CURRENT_CONFIG_INDEX (-1)
  14545. +#define USB_CURRENT_ALT_INDEX (-1)
  14546. +
  14547. +struct usb_config_desc {
  14548. + int ucd_config_index;
  14549. + usb_config_descriptor_t ucd_desc;
  14550. +};
  14551. +
  14552. +struct usb_interface_desc {
  14553. + int uid_config_index;
  14554. + int uid_interface_index;
  14555. + int uid_alt_index;
  14556. + usb_interface_descriptor_t uid_desc;
  14557. +};
  14558. +
  14559. +struct usb_endpoint_desc {
  14560. + int ued_config_index;
  14561. + int ued_interface_index;
  14562. + int ued_alt_index;
  14563. + int ued_endpoint_index;
  14564. + usb_endpoint_descriptor_t ued_desc;
  14565. +};
  14566. +
  14567. +struct usb_full_desc {
  14568. + int ufd_config_index;
  14569. + u_int ufd_size;
  14570. + u_char *ufd_data;
  14571. +};
  14572. +
  14573. +struct usb_string_desc {
  14574. + int usd_string_index;
  14575. + int usd_language_id;
  14576. + usb_string_descriptor_t usd_desc;
  14577. +};
  14578. +
  14579. +struct usb_ctl_report_desc {
  14580. + int ucrd_size;
  14581. + u_char ucrd_data[1024]; /* filled data size will vary */
  14582. +};
  14583. +
  14584. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  14585. +
  14586. +#define USB_MAX_DEVNAMES 4
  14587. +#define USB_MAX_DEVNAMELEN 16
  14588. +struct usb_device_info {
  14589. + u_int8_t udi_bus;
  14590. + u_int8_t udi_addr; /* device address */
  14591. + usb_event_cookie_t udi_cookie;
  14592. + char udi_product[USB_MAX_STRING_LEN];
  14593. + char udi_vendor[USB_MAX_STRING_LEN];
  14594. + char udi_release[8];
  14595. + u_int16_t udi_productNo;
  14596. + u_int16_t udi_vendorNo;
  14597. + u_int16_t udi_releaseNo;
  14598. + u_int8_t udi_class;
  14599. + u_int8_t udi_subclass;
  14600. + u_int8_t udi_protocol;
  14601. + u_int8_t udi_config;
  14602. + u_int8_t udi_speed;
  14603. +#define USB_SPEED_UNKNOWN 0
  14604. +#define USB_SPEED_LOW 1
  14605. +#define USB_SPEED_FULL 2
  14606. +#define USB_SPEED_HIGH 3
  14607. +#define USB_SPEED_VARIABLE 4
  14608. +#define USB_SPEED_SUPER 5
  14609. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  14610. + int udi_nports;
  14611. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  14612. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  14613. +#define USB_PORT_ENABLED 0xff
  14614. +#define USB_PORT_SUSPENDED 0xfe
  14615. +#define USB_PORT_POWERED 0xfd
  14616. +#define USB_PORT_DISABLED 0xfc
  14617. +};
  14618. +
  14619. +struct usb_ctl_report {
  14620. + int ucr_report;
  14621. + u_char ucr_data[1024]; /* filled data size will vary */
  14622. +};
  14623. +
  14624. +struct usb_device_stats {
  14625. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  14626. +};
  14627. +
  14628. +#define WUSB_MIN_IE 0x80
  14629. +#define WUSB_WCTA_IE 0x80
  14630. +#define WUSB_WCONNECTACK_IE 0x81
  14631. +#define WUSB_WHOSTINFO_IE 0x82
  14632. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  14633. +#define WUHI_CA_RECONN 0x00
  14634. +#define WUHI_CA_LIMITED 0x01
  14635. +#define WUHI_CA_ALL 0x03
  14636. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  14637. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  14638. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  14639. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  14640. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  14641. +#define WUSB_WWORK_IE 0x87
  14642. +#define WUSB_WCHANNEL_STOP_IE 0x88
  14643. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  14644. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  14645. +#define WUSB_WRESETDEVICE_IE 0x8B
  14646. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  14647. +#define WUSB_MAX_IE 0x8C
  14648. +
  14649. +/* Device Notification Types */
  14650. +
  14651. +#define WUSB_DN_MIN 0x01
  14652. +#define WUSB_DN_CONNECT 0x01
  14653. +# define WUSB_DA_OLDCONN 0x00
  14654. +# define WUSB_DA_NEWCONN 0x01
  14655. +# define WUSB_DA_SELF_BEACON 0x02
  14656. +# define WUSB_DA_DIR_BEACON 0x04
  14657. +# define WUSB_DA_NO_BEACON 0x06
  14658. +#define WUSB_DN_DISCONNECT 0x02
  14659. +#define WUSB_DN_EPRDY 0x03
  14660. +#define WUSB_DN_MASAVAILCHANGED 0x04
  14661. +#define WUSB_DN_REMOTEWAKEUP 0x05
  14662. +#define WUSB_DN_SLEEP 0x06
  14663. +#define WUSB_DN_ALIVE 0x07
  14664. +#define WUSB_DN_MAX 0x07
  14665. +
  14666. +#ifdef _MSC_VER
  14667. +#include <pshpack1.h>
  14668. +#endif
  14669. +
  14670. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  14671. +typedef struct wusb_hndshk_data {
  14672. + uByte bMessageNumber;
  14673. + uByte bStatus;
  14674. + uByte tTKID[3];
  14675. + uByte bReserved;
  14676. + uByte CDID[16];
  14677. + uByte Nonce[16];
  14678. + uByte MIC[8];
  14679. +} UPACKED wusb_hndshk_data_t;
  14680. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  14681. +
  14682. +/* WUSB Connection Context */
  14683. +typedef struct wusb_conn_context {
  14684. + uByte CHID [16];
  14685. + uByte CDID [16];
  14686. + uByte CK [16];
  14687. +} UPACKED wusb_conn_context_t;
  14688. +
  14689. +/* WUSB Security Descriptor */
  14690. +typedef struct wusb_security_desc {
  14691. + uByte bLength;
  14692. + uByte bDescriptorType;
  14693. + uWord wTotalLength;
  14694. + uByte bNumEncryptionTypes;
  14695. +} UPACKED wusb_security_desc_t;
  14696. +
  14697. +/* WUSB Encryption Type Descriptor */
  14698. +typedef struct wusb_encrypt_type_desc {
  14699. + uByte bLength;
  14700. + uByte bDescriptorType;
  14701. +
  14702. + uByte bEncryptionType;
  14703. +#define WUETD_UNSECURE 0
  14704. +#define WUETD_WIRED 1
  14705. +#define WUETD_CCM_1 2
  14706. +#define WUETD_RSA_1 3
  14707. +
  14708. + uByte bEncryptionValue;
  14709. + uByte bAuthKeyIndex;
  14710. +} UPACKED wusb_encrypt_type_desc_t;
  14711. +
  14712. +/* WUSB Key Descriptor */
  14713. +typedef struct wusb_key_desc {
  14714. + uByte bLength;
  14715. + uByte bDescriptorType;
  14716. + uByte tTKID[3];
  14717. + uByte bReserved;
  14718. + uByte KeyData[1]; /* variable length */
  14719. +} UPACKED wusb_key_desc_t;
  14720. +
  14721. +/* WUSB BOS Descriptor (Binary device Object Store) */
  14722. +typedef struct wusb_bos_desc {
  14723. + uByte bLength;
  14724. + uByte bDescriptorType;
  14725. + uWord wTotalLength;
  14726. + uByte bNumDeviceCaps;
  14727. +} UPACKED wusb_bos_desc_t;
  14728. +
  14729. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  14730. +typedef struct usb_dev_cap_20_ext_desc {
  14731. + uByte bLength;
  14732. + uByte bDescriptorType;
  14733. + uByte bDevCapabilityType;
  14734. +#define USB_20_EXT_LPM 0x02
  14735. + uDWord bmAttributes;
  14736. +} UPACKED usb_dev_cap_20_ext_desc_t;
  14737. +
  14738. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  14739. +typedef struct usb_dev_cap_ss_usb {
  14740. + uByte bLength;
  14741. + uByte bDescriptorType;
  14742. + uByte bDevCapabilityType;
  14743. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  14744. + uByte bmAttributes;
  14745. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  14746. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  14747. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  14748. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  14749. + uWord wSpeedsSupported;
  14750. + uByte bFunctionalitySupport;
  14751. + uByte bU1DevExitLat;
  14752. + uWord wU2DevExitLat;
  14753. +} UPACKED usb_dev_cap_ss_usb_t;
  14754. +
  14755. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  14756. +typedef struct usb_dev_cap_container_id {
  14757. + uByte bLength;
  14758. + uByte bDescriptorType;
  14759. + uByte bDevCapabilityType;
  14760. + uByte bReserved;
  14761. + uByte containerID[16];
  14762. +} UPACKED usb_dev_cap_container_id_t;
  14763. +
  14764. +/* Device Capability Type Codes */
  14765. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  14766. +
  14767. +/* Device Capability Descriptor */
  14768. +typedef struct wusb_dev_cap_desc {
  14769. + uByte bLength;
  14770. + uByte bDescriptorType;
  14771. + uByte bDevCapabilityType;
  14772. + uByte caps[1]; /* Variable length */
  14773. +} UPACKED wusb_dev_cap_desc_t;
  14774. +
  14775. +/* Device Capability Descriptor */
  14776. +typedef struct wusb_dev_cap_uwb_desc {
  14777. + uByte bLength;
  14778. + uByte bDescriptorType;
  14779. + uByte bDevCapabilityType;
  14780. + uByte bmAttributes;
  14781. + uWord wPHYRates; /* Bitmap */
  14782. + uByte bmTFITXPowerInfo;
  14783. + uByte bmFFITXPowerInfo;
  14784. + uWord bmBandGroup;
  14785. + uByte bReserved;
  14786. +} UPACKED wusb_dev_cap_uwb_desc_t;
  14787. +
  14788. +/* Wireless USB Endpoint Companion Descriptor */
  14789. +typedef struct wusb_endpoint_companion_desc {
  14790. + uByte bLength;
  14791. + uByte bDescriptorType;
  14792. + uByte bMaxBurst;
  14793. + uByte bMaxSequence;
  14794. + uWord wMaxStreamDelay;
  14795. + uWord wOverTheAirPacketSize;
  14796. + uByte bOverTheAirInterval;
  14797. + uByte bmCompAttributes;
  14798. +} UPACKED wusb_endpoint_companion_desc_t;
  14799. +
  14800. +/* Wireless USB Numeric Association M1 Data Structure */
  14801. +typedef struct wusb_m1_data {
  14802. + uByte version;
  14803. + uWord langId;
  14804. + uByte deviceFriendlyNameLength;
  14805. + uByte sha_256_m3[32];
  14806. + uByte deviceFriendlyName[256];
  14807. +} UPACKED wusb_m1_data_t;
  14808. +
  14809. +typedef struct wusb_m2_data {
  14810. + uByte version;
  14811. + uWord langId;
  14812. + uByte hostFriendlyNameLength;
  14813. + uByte pkh[384];
  14814. + uByte hostFriendlyName[256];
  14815. +} UPACKED wusb_m2_data_t;
  14816. +
  14817. +typedef struct wusb_m3_data {
  14818. + uByte pkd[384];
  14819. + uByte nd;
  14820. +} UPACKED wusb_m3_data_t;
  14821. +
  14822. +typedef struct wusb_m4_data {
  14823. + uDWord _attributeTypeIdAndLength_1;
  14824. + uWord associationTypeId;
  14825. +
  14826. + uDWord _attributeTypeIdAndLength_2;
  14827. + uWord associationSubTypeId;
  14828. +
  14829. + uDWord _attributeTypeIdAndLength_3;
  14830. + uDWord length;
  14831. +
  14832. + uDWord _attributeTypeIdAndLength_4;
  14833. + uDWord associationStatus;
  14834. +
  14835. + uDWord _attributeTypeIdAndLength_5;
  14836. + uByte chid[16];
  14837. +
  14838. + uDWord _attributeTypeIdAndLength_6;
  14839. + uByte cdid[16];
  14840. +
  14841. + uDWord _attributeTypeIdAndLength_7;
  14842. + uByte bandGroups[2];
  14843. +} UPACKED wusb_m4_data_t;
  14844. +
  14845. +#ifdef _MSC_VER
  14846. +#include <poppack.h>
  14847. +#endif
  14848. +
  14849. +#ifdef __cplusplus
  14850. +}
  14851. +#endif
  14852. +
  14853. +#endif /* _USB_H_ */
  14854. --- /dev/null
  14855. +++ b/drivers/usb/host/dwc_otg/Makefile
  14856. @@ -0,0 +1,82 @@
  14857. +#
  14858. +# Makefile for DWC_otg Highspeed USB controller driver
  14859. +#
  14860. +
  14861. +ifneq ($(KERNELRELEASE),)
  14862. +
  14863. +# Use the BUS_INTERFACE variable to compile the software for either
  14864. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  14865. +ifeq ($(BUS_INTERFACE),)
  14866. +# BUS_INTERFACE = -DPCI_INTERFACE
  14867. +# BUS_INTERFACE = -DLM_INTERFACE
  14868. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  14869. +endif
  14870. +
  14871. +#ccflags-y += -DDEBUG
  14872. +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  14873. +
  14874. +# Use one of the following flags to compile the software in host-only or
  14875. +# device-only mode.
  14876. +#ccflags-y += -DDWC_HOST_ONLY
  14877. +#ccflags-y += -DDWC_DEVICE_ONLY
  14878. +
  14879. +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
  14880. +#ccflags-y += -DDWC_EN_ISOC
  14881. +ccflags-y += -I$(obj)/../dwc_common_port
  14882. +#ccflags-y += -I$(PORTLIB)
  14883. +ccflags-y += -DDWC_LINUX
  14884. +ccflags-y += $(CFI)
  14885. +ccflags-y += $(BUS_INTERFACE)
  14886. +#ccflags-y += -DDWC_DEV_SRPCAP
  14887. +
  14888. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  14889. +
  14890. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  14891. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  14892. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  14893. +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
  14894. +dwc_otg-objs += dwc_otg_adp.o
  14895. +dwc_otg-objs += dwc_otg_fiq_fsm.o
  14896. +dwc_otg-objs += dwc_otg_fiq_stub.o
  14897. +ifneq ($(CFI),)
  14898. +dwc_otg-objs += dwc_otg_cfi.o
  14899. +endif
  14900. +
  14901. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  14902. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  14903. +
  14904. +ifneq ($(kernrel3),2.6.20)
  14905. +ccflags-y += $(CPPFLAGS)
  14906. +endif
  14907. +
  14908. +else
  14909. +
  14910. +PWD := $(shell pwd)
  14911. +PORTLIB := $(PWD)/../dwc_common_port
  14912. +
  14913. +# Command paths
  14914. +CTAGS := $(CTAGS)
  14915. +DOXYGEN := $(DOXYGEN)
  14916. +
  14917. +default: portlib
  14918. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  14919. +
  14920. +install: default
  14921. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  14922. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  14923. +
  14924. +portlib:
  14925. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  14926. + cp $(PORTLIB)/Module.symvers $(PWD)/
  14927. +
  14928. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  14929. + $(DOXYGEN) doc/doxygen.cfg
  14930. +
  14931. +tags: $(wildcard *.[hc])
  14932. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  14933. +
  14934. +
  14935. +clean:
  14936. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  14937. +
  14938. +endif
  14939. --- /dev/null
  14940. +++ b/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  14941. @@ -0,0 +1,224 @@
  14942. +# Doxyfile 1.3.9.1
  14943. +
  14944. +#---------------------------------------------------------------------------
  14945. +# Project related configuration options
  14946. +#---------------------------------------------------------------------------
  14947. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  14948. +PROJECT_NUMBER = v3.00a
  14949. +OUTPUT_DIRECTORY = ./doc/
  14950. +CREATE_SUBDIRS = NO
  14951. +OUTPUT_LANGUAGE = English
  14952. +BRIEF_MEMBER_DESC = YES
  14953. +REPEAT_BRIEF = YES
  14954. +ABBREVIATE_BRIEF = "The $name class" \
  14955. + "The $name widget" \
  14956. + "The $name file" \
  14957. + is \
  14958. + provides \
  14959. + specifies \
  14960. + contains \
  14961. + represents \
  14962. + a \
  14963. + an \
  14964. + the
  14965. +ALWAYS_DETAILED_SEC = NO
  14966. +INLINE_INHERITED_MEMB = NO
  14967. +FULL_PATH_NAMES = NO
  14968. +STRIP_FROM_PATH =
  14969. +STRIP_FROM_INC_PATH =
  14970. +SHORT_NAMES = NO
  14971. +JAVADOC_AUTOBRIEF = YES
  14972. +MULTILINE_CPP_IS_BRIEF = NO
  14973. +INHERIT_DOCS = YES
  14974. +DISTRIBUTE_GROUP_DOC = NO
  14975. +TAB_SIZE = 8
  14976. +ALIASES =
  14977. +OPTIMIZE_OUTPUT_FOR_C = YES
  14978. +OPTIMIZE_OUTPUT_JAVA = NO
  14979. +SUBGROUPING = YES
  14980. +#---------------------------------------------------------------------------
  14981. +# Build related configuration options
  14982. +#---------------------------------------------------------------------------
  14983. +EXTRACT_ALL = NO
  14984. +EXTRACT_PRIVATE = YES
  14985. +EXTRACT_STATIC = YES
  14986. +EXTRACT_LOCAL_CLASSES = YES
  14987. +EXTRACT_LOCAL_METHODS = NO
  14988. +HIDE_UNDOC_MEMBERS = NO
  14989. +HIDE_UNDOC_CLASSES = NO
  14990. +HIDE_FRIEND_COMPOUNDS = NO
  14991. +HIDE_IN_BODY_DOCS = NO
  14992. +INTERNAL_DOCS = NO
  14993. +CASE_SENSE_NAMES = NO
  14994. +HIDE_SCOPE_NAMES = NO
  14995. +SHOW_INCLUDE_FILES = YES
  14996. +INLINE_INFO = YES
  14997. +SORT_MEMBER_DOCS = NO
  14998. +SORT_BRIEF_DOCS = NO
  14999. +SORT_BY_SCOPE_NAME = NO
  15000. +GENERATE_TODOLIST = YES
  15001. +GENERATE_TESTLIST = YES
  15002. +GENERATE_BUGLIST = YES
  15003. +GENERATE_DEPRECATEDLIST= YES
  15004. +ENABLED_SECTIONS =
  15005. +MAX_INITIALIZER_LINES = 30
  15006. +SHOW_USED_FILES = YES
  15007. +SHOW_DIRECTORIES = YES
  15008. +#---------------------------------------------------------------------------
  15009. +# configuration options related to warning and progress messages
  15010. +#---------------------------------------------------------------------------
  15011. +QUIET = YES
  15012. +WARNINGS = YES
  15013. +WARN_IF_UNDOCUMENTED = NO
  15014. +WARN_IF_DOC_ERROR = YES
  15015. +WARN_FORMAT = "$file:$line: $text"
  15016. +WARN_LOGFILE =
  15017. +#---------------------------------------------------------------------------
  15018. +# configuration options related to the input files
  15019. +#---------------------------------------------------------------------------
  15020. +INPUT = .
  15021. +FILE_PATTERNS = *.c \
  15022. + *.h \
  15023. + ./linux/*.c \
  15024. + ./linux/*.h
  15025. +RECURSIVE = NO
  15026. +EXCLUDE = ./test/ \
  15027. + ./dwc_otg/.AppleDouble/
  15028. +EXCLUDE_SYMLINKS = YES
  15029. +EXCLUDE_PATTERNS = *.mod.*
  15030. +EXAMPLE_PATH =
  15031. +EXAMPLE_PATTERNS = *
  15032. +EXAMPLE_RECURSIVE = NO
  15033. +IMAGE_PATH =
  15034. +INPUT_FILTER =
  15035. +FILTER_PATTERNS =
  15036. +FILTER_SOURCE_FILES = NO
  15037. +#---------------------------------------------------------------------------
  15038. +# configuration options related to source browsing
  15039. +#---------------------------------------------------------------------------
  15040. +SOURCE_BROWSER = YES
  15041. +INLINE_SOURCES = NO
  15042. +STRIP_CODE_COMMENTS = YES
  15043. +REFERENCED_BY_RELATION = NO
  15044. +REFERENCES_RELATION = NO
  15045. +VERBATIM_HEADERS = NO
  15046. +#---------------------------------------------------------------------------
  15047. +# configuration options related to the alphabetical class index
  15048. +#---------------------------------------------------------------------------
  15049. +ALPHABETICAL_INDEX = NO
  15050. +COLS_IN_ALPHA_INDEX = 5
  15051. +IGNORE_PREFIX =
  15052. +#---------------------------------------------------------------------------
  15053. +# configuration options related to the HTML output
  15054. +#---------------------------------------------------------------------------
  15055. +GENERATE_HTML = YES
  15056. +HTML_OUTPUT = html
  15057. +HTML_FILE_EXTENSION = .html
  15058. +HTML_HEADER =
  15059. +HTML_FOOTER =
  15060. +HTML_STYLESHEET =
  15061. +HTML_ALIGN_MEMBERS = YES
  15062. +GENERATE_HTMLHELP = NO
  15063. +CHM_FILE =
  15064. +HHC_LOCATION =
  15065. +GENERATE_CHI = NO
  15066. +BINARY_TOC = NO
  15067. +TOC_EXPAND = NO
  15068. +DISABLE_INDEX = NO
  15069. +ENUM_VALUES_PER_LINE = 4
  15070. +GENERATE_TREEVIEW = YES
  15071. +TREEVIEW_WIDTH = 250
  15072. +#---------------------------------------------------------------------------
  15073. +# configuration options related to the LaTeX output
  15074. +#---------------------------------------------------------------------------
  15075. +GENERATE_LATEX = NO
  15076. +LATEX_OUTPUT = latex
  15077. +LATEX_CMD_NAME = latex
  15078. +MAKEINDEX_CMD_NAME = makeindex
  15079. +COMPACT_LATEX = NO
  15080. +PAPER_TYPE = a4wide
  15081. +EXTRA_PACKAGES =
  15082. +LATEX_HEADER =
  15083. +PDF_HYPERLINKS = NO
  15084. +USE_PDFLATEX = NO
  15085. +LATEX_BATCHMODE = NO
  15086. +LATEX_HIDE_INDICES = NO
  15087. +#---------------------------------------------------------------------------
  15088. +# configuration options related to the RTF output
  15089. +#---------------------------------------------------------------------------
  15090. +GENERATE_RTF = NO
  15091. +RTF_OUTPUT = rtf
  15092. +COMPACT_RTF = NO
  15093. +RTF_HYPERLINKS = NO
  15094. +RTF_STYLESHEET_FILE =
  15095. +RTF_EXTENSIONS_FILE =
  15096. +#---------------------------------------------------------------------------
  15097. +# configuration options related to the man page output
  15098. +#---------------------------------------------------------------------------
  15099. +GENERATE_MAN = NO
  15100. +MAN_OUTPUT = man
  15101. +MAN_EXTENSION = .3
  15102. +MAN_LINKS = NO
  15103. +#---------------------------------------------------------------------------
  15104. +# configuration options related to the XML output
  15105. +#---------------------------------------------------------------------------
  15106. +GENERATE_XML = NO
  15107. +XML_OUTPUT = xml
  15108. +XML_SCHEMA =
  15109. +XML_DTD =
  15110. +XML_PROGRAMLISTING = YES
  15111. +#---------------------------------------------------------------------------
  15112. +# configuration options for the AutoGen Definitions output
  15113. +#---------------------------------------------------------------------------
  15114. +GENERATE_AUTOGEN_DEF = NO
  15115. +#---------------------------------------------------------------------------
  15116. +# configuration options related to the Perl module output
  15117. +#---------------------------------------------------------------------------
  15118. +GENERATE_PERLMOD = NO
  15119. +PERLMOD_LATEX = NO
  15120. +PERLMOD_PRETTY = YES
  15121. +PERLMOD_MAKEVAR_PREFIX =
  15122. +#---------------------------------------------------------------------------
  15123. +# Configuration options related to the preprocessor
  15124. +#---------------------------------------------------------------------------
  15125. +ENABLE_PREPROCESSING = YES
  15126. +MACRO_EXPANSION = YES
  15127. +EXPAND_ONLY_PREDEF = YES
  15128. +SEARCH_INCLUDES = YES
  15129. +INCLUDE_PATH =
  15130. +INCLUDE_FILE_PATTERNS =
  15131. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  15132. +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
  15133. +SKIP_FUNCTION_MACROS = NO
  15134. +#---------------------------------------------------------------------------
  15135. +# Configuration::additions related to external references
  15136. +#---------------------------------------------------------------------------
  15137. +TAGFILES =
  15138. +GENERATE_TAGFILE =
  15139. +ALLEXTERNALS = NO
  15140. +EXTERNAL_GROUPS = YES
  15141. +PERL_PATH = /usr/bin/perl
  15142. +#---------------------------------------------------------------------------
  15143. +# Configuration options related to the dot tool
  15144. +#---------------------------------------------------------------------------
  15145. +CLASS_DIAGRAMS = YES
  15146. +HIDE_UNDOC_RELATIONS = YES
  15147. +HAVE_DOT = NO
  15148. +CLASS_GRAPH = YES
  15149. +COLLABORATION_GRAPH = YES
  15150. +UML_LOOK = NO
  15151. +TEMPLATE_RELATIONS = NO
  15152. +INCLUDE_GRAPH = YES
  15153. +INCLUDED_BY_GRAPH = YES
  15154. +CALL_GRAPH = NO
  15155. +GRAPHICAL_HIERARCHY = YES
  15156. +DOT_IMAGE_FORMAT = png
  15157. +DOT_PATH =
  15158. +DOTFILE_DIRS =
  15159. +MAX_DOT_GRAPH_DEPTH = 1000
  15160. +GENERATE_LEGEND = YES
  15161. +DOT_CLEANUP = YES
  15162. +#---------------------------------------------------------------------------
  15163. +# Configuration::additions related to the search engine
  15164. +#---------------------------------------------------------------------------
  15165. +SEARCHENGINE = NO
  15166. --- /dev/null
  15167. +++ b/drivers/usb/host/dwc_otg/dummy_audio.c
  15168. @@ -0,0 +1,1575 @@
  15169. +/*
  15170. + * zero.c -- Gadget Zero, for USB development
  15171. + *
  15172. + * Copyright (C) 2003-2004 David Brownell
  15173. + * All rights reserved.
  15174. + *
  15175. + * Redistribution and use in source and binary forms, with or without
  15176. + * modification, are permitted provided that the following conditions
  15177. + * are met:
  15178. + * 1. Redistributions of source code must retain the above copyright
  15179. + * notice, this list of conditions, and the following disclaimer,
  15180. + * without modification.
  15181. + * 2. Redistributions in binary form must reproduce the above copyright
  15182. + * notice, this list of conditions and the following disclaimer in the
  15183. + * documentation and/or other materials provided with the distribution.
  15184. + * 3. The names of the above-listed copyright holders may not be used
  15185. + * to endorse or promote products derived from this software without
  15186. + * specific prior written permission.
  15187. + *
  15188. + * ALTERNATIVELY, this software may be distributed under the terms of the
  15189. + * GNU General Public License ("GPL") as published by the Free Software
  15190. + * Foundation, either version 2 of that License or (at your option) any
  15191. + * later version.
  15192. + *
  15193. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  15194. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15195. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  15196. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  15197. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  15198. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  15199. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15200. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  15201. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  15202. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  15203. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15204. + */
  15205. +
  15206. +
  15207. +/*
  15208. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  15209. + * can write a hardware-agnostic gadget driver running inside a USB device.
  15210. + *
  15211. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  15212. + * affect most of the driver.
  15213. + *
  15214. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  15215. + * functional test of your device-side usb stack, or with "usb-skeleton".
  15216. + *
  15217. + * It supports two similar configurations. One sinks whatever the usb host
  15218. + * writes, and in return sources zeroes. The other loops whatever the host
  15219. + * writes back, so the host can read it. Module options include:
  15220. + *
  15221. + * buflen=N default N=4096, buffer size used
  15222. + * qlen=N default N=32, how many buffers in the loopback queue
  15223. + * loopdefault default false, list loopback config first
  15224. + *
  15225. + * Many drivers will only have one configuration, letting them be much
  15226. + * simpler if they also don't support high speed operation (like this
  15227. + * driver does).
  15228. + */
  15229. +
  15230. +#include <linux/config.h>
  15231. +#include <linux/module.h>
  15232. +#include <linux/kernel.h>
  15233. +#include <linux/delay.h>
  15234. +#include <linux/ioport.h>
  15235. +#include <linux/sched.h>
  15236. +#include <linux/slab.h>
  15237. +#include <linux/smp_lock.h>
  15238. +#include <linux/errno.h>
  15239. +#include <linux/init.h>
  15240. +#include <linux/timer.h>
  15241. +#include <linux/list.h>
  15242. +#include <linux/interrupt.h>
  15243. +#include <linux/uts.h>
  15244. +#include <linux/version.h>
  15245. +#include <linux/device.h>
  15246. +#include <linux/moduleparam.h>
  15247. +#include <linux/proc_fs.h>
  15248. +
  15249. +#include <asm/byteorder.h>
  15250. +#include <asm/io.h>
  15251. +#include <asm/irq.h>
  15252. +#include <asm/system.h>
  15253. +#include <asm/unaligned.h>
  15254. +
  15255. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  15256. +# include <linux/usb/ch9.h>
  15257. +#else
  15258. +# include <linux/usb_ch9.h>
  15259. +#endif
  15260. +
  15261. +#include <linux/usb_gadget.h>
  15262. +
  15263. +
  15264. +/*-------------------------------------------------------------------------*/
  15265. +/*-------------------------------------------------------------------------*/
  15266. +
  15267. +
  15268. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  15269. +{
  15270. + int count = 0;
  15271. + u8 c;
  15272. + u16 uchar;
  15273. +
  15274. + /* this insists on correct encodings, though not minimal ones.
  15275. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  15276. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  15277. + */
  15278. + while (len != 0 && (c = (u8) *s++) != 0) {
  15279. + if (unlikely(c & 0x80)) {
  15280. + // 2-byte sequence:
  15281. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  15282. + if ((c & 0xe0) == 0xc0) {
  15283. + uchar = (c & 0x1f) << 6;
  15284. +
  15285. + c = (u8) *s++;
  15286. + if ((c & 0xc0) != 0xc0)
  15287. + goto fail;
  15288. + c &= 0x3f;
  15289. + uchar |= c;
  15290. +
  15291. + // 3-byte sequence (most CJKV characters):
  15292. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  15293. + } else if ((c & 0xf0) == 0xe0) {
  15294. + uchar = (c & 0x0f) << 12;
  15295. +
  15296. + c = (u8) *s++;
  15297. + if ((c & 0xc0) != 0xc0)
  15298. + goto fail;
  15299. + c &= 0x3f;
  15300. + uchar |= c << 6;
  15301. +
  15302. + c = (u8) *s++;
  15303. + if ((c & 0xc0) != 0xc0)
  15304. + goto fail;
  15305. + c &= 0x3f;
  15306. + uchar |= c;
  15307. +
  15308. + /* no bogus surrogates */
  15309. + if (0xd800 <= uchar && uchar <= 0xdfff)
  15310. + goto fail;
  15311. +
  15312. + // 4-byte sequence (surrogate pairs, currently rare):
  15313. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  15314. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  15315. + // (uuuuu = wwww + 1)
  15316. + // FIXME accept the surrogate code points (only)
  15317. +
  15318. + } else
  15319. + goto fail;
  15320. + } else
  15321. + uchar = c;
  15322. + put_unaligned (cpu_to_le16 (uchar), cp++);
  15323. + count++;
  15324. + len--;
  15325. + }
  15326. + return count;
  15327. +fail:
  15328. + return -1;
  15329. +}
  15330. +
  15331. +
  15332. +/**
  15333. + * usb_gadget_get_string - fill out a string descriptor
  15334. + * @table: of c strings encoded using UTF-8
  15335. + * @id: string id, from low byte of wValue in get string descriptor
  15336. + * @buf: at least 256 bytes
  15337. + *
  15338. + * Finds the UTF-8 string matching the ID, and converts it into a
  15339. + * string descriptor in utf16-le.
  15340. + * Returns length of descriptor (always even) or negative errno
  15341. + *
  15342. + * If your driver needs stings in multiple languages, you'll probably
  15343. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  15344. + * using this routine after choosing which set of UTF-8 strings to use.
  15345. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  15346. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  15347. + * characters (which are also widely used in C strings).
  15348. + */
  15349. +int
  15350. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  15351. +{
  15352. + struct usb_string *s;
  15353. + int len;
  15354. +
  15355. + /* descriptor 0 has the language id */
  15356. + if (id == 0) {
  15357. + buf [0] = 4;
  15358. + buf [1] = USB_DT_STRING;
  15359. + buf [2] = (u8) table->language;
  15360. + buf [3] = (u8) (table->language >> 8);
  15361. + return 4;
  15362. + }
  15363. + for (s = table->strings; s && s->s; s++)
  15364. + if (s->id == id)
  15365. + break;
  15366. +
  15367. + /* unrecognized: stall. */
  15368. + if (!s || !s->s)
  15369. + return -EINVAL;
  15370. +
  15371. + /* string descriptors have length, tag, then UTF16-LE text */
  15372. + len = min ((size_t) 126, strlen (s->s));
  15373. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  15374. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  15375. + if (len < 0)
  15376. + return -EINVAL;
  15377. + buf [0] = (len + 1) * 2;
  15378. + buf [1] = USB_DT_STRING;
  15379. + return buf [0];
  15380. +}
  15381. +
  15382. +
  15383. +/*-------------------------------------------------------------------------*/
  15384. +/*-------------------------------------------------------------------------*/
  15385. +
  15386. +
  15387. +/**
  15388. + * usb_descriptor_fillbuf - fill buffer with descriptors
  15389. + * @buf: Buffer to be filled
  15390. + * @buflen: Size of buf
  15391. + * @src: Array of descriptor pointers, terminated by null pointer.
  15392. + *
  15393. + * Copies descriptors into the buffer, returning the length or a
  15394. + * negative error code if they can't all be copied. Useful when
  15395. + * assembling descriptors for an associated set of interfaces used
  15396. + * as part of configuring a composite device; or in other cases where
  15397. + * sets of descriptors need to be marshaled.
  15398. + */
  15399. +int
  15400. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  15401. + const struct usb_descriptor_header **src)
  15402. +{
  15403. + u8 *dest = buf;
  15404. +
  15405. + if (!src)
  15406. + return -EINVAL;
  15407. +
  15408. + /* fill buffer from src[] until null descriptor ptr */
  15409. + for (; 0 != *src; src++) {
  15410. + unsigned len = (*src)->bLength;
  15411. +
  15412. + if (len > buflen)
  15413. + return -EINVAL;
  15414. + memcpy(dest, *src, len);
  15415. + buflen -= len;
  15416. + dest += len;
  15417. + }
  15418. + return dest - (u8 *)buf;
  15419. +}
  15420. +
  15421. +
  15422. +/**
  15423. + * usb_gadget_config_buf - builts a complete configuration descriptor
  15424. + * @config: Header for the descriptor, including characteristics such
  15425. + * as power requirements and number of interfaces.
  15426. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  15427. + * endpoint, etc) defining all functions in this device configuration.
  15428. + * @buf: Buffer for the resulting configuration descriptor.
  15429. + * @length: Length of buffer. If this is not big enough to hold the
  15430. + * entire configuration descriptor, an error code will be returned.
  15431. + *
  15432. + * This copies descriptors into the response buffer, building a descriptor
  15433. + * for that configuration. It returns the buffer length or a negative
  15434. + * status code. The config.wTotalLength field is set to match the length
  15435. + * of the result, but other descriptor fields (including power usage and
  15436. + * interface count) must be set by the caller.
  15437. + *
  15438. + * Gadget drivers could use this when constructing a config descriptor
  15439. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  15440. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  15441. + */
  15442. +int usb_gadget_config_buf(
  15443. + const struct usb_config_descriptor *config,
  15444. + void *buf,
  15445. + unsigned length,
  15446. + const struct usb_descriptor_header **desc
  15447. +)
  15448. +{
  15449. + struct usb_config_descriptor *cp = buf;
  15450. + int len;
  15451. +
  15452. + /* config descriptor first */
  15453. + if (length < USB_DT_CONFIG_SIZE || !desc)
  15454. + return -EINVAL;
  15455. + *cp = *config;
  15456. +
  15457. + /* then interface/endpoint/class/vendor/... */
  15458. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  15459. + length - USB_DT_CONFIG_SIZE, desc);
  15460. + if (len < 0)
  15461. + return len;
  15462. + len += USB_DT_CONFIG_SIZE;
  15463. + if (len > 0xffff)
  15464. + return -EINVAL;
  15465. +
  15466. + /* patch up the config descriptor */
  15467. + cp->bLength = USB_DT_CONFIG_SIZE;
  15468. + cp->bDescriptorType = USB_DT_CONFIG;
  15469. + cp->wTotalLength = cpu_to_le16(len);
  15470. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  15471. + return len;
  15472. +}
  15473. +
  15474. +/*-------------------------------------------------------------------------*/
  15475. +/*-------------------------------------------------------------------------*/
  15476. +
  15477. +
  15478. +#define RBUF_LEN (1024*1024)
  15479. +static int rbuf_start;
  15480. +static int rbuf_len;
  15481. +static __u8 rbuf[RBUF_LEN];
  15482. +
  15483. +/*-------------------------------------------------------------------------*/
  15484. +
  15485. +#define DRIVER_VERSION "St Patrick's Day 2004"
  15486. +
  15487. +static const char shortname [] = "zero";
  15488. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  15489. +
  15490. +static const char source_sink [] = "source and sink data";
  15491. +static const char loopback [] = "loop input to output";
  15492. +
  15493. +/*-------------------------------------------------------------------------*/
  15494. +
  15495. +/*
  15496. + * driver assumes self-powered hardware, and
  15497. + * has no way for users to trigger remote wakeup.
  15498. + *
  15499. + * this version autoconfigures as much as possible,
  15500. + * which is reasonable for most "bulk-only" drivers.
  15501. + */
  15502. +static const char *EP_IN_NAME; /* source */
  15503. +static const char *EP_OUT_NAME; /* sink */
  15504. +
  15505. +/*-------------------------------------------------------------------------*/
  15506. +
  15507. +/* big enough to hold our biggest descriptor */
  15508. +#define USB_BUFSIZ 512
  15509. +
  15510. +struct zero_dev {
  15511. + spinlock_t lock;
  15512. + struct usb_gadget *gadget;
  15513. + struct usb_request *req; /* for control responses */
  15514. +
  15515. + /* when configured, we have one of two configs:
  15516. + * - source data (in to host) and sink it (out from host)
  15517. + * - or loop it back (out from host back in to host)
  15518. + */
  15519. + u8 config;
  15520. + struct usb_ep *in_ep, *out_ep;
  15521. +
  15522. + /* autoresume timer */
  15523. + struct timer_list resume;
  15524. +};
  15525. +
  15526. +#define xprintk(d,level,fmt,args...) \
  15527. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  15528. +
  15529. +#ifdef DEBUG
  15530. +#define DBG(dev,fmt,args...) \
  15531. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  15532. +#else
  15533. +#define DBG(dev,fmt,args...) \
  15534. + do { } while (0)
  15535. +#endif /* DEBUG */
  15536. +
  15537. +#ifdef VERBOSE
  15538. +#define VDBG DBG
  15539. +#else
  15540. +#define VDBG(dev,fmt,args...) \
  15541. + do { } while (0)
  15542. +#endif /* VERBOSE */
  15543. +
  15544. +#define ERROR(dev,fmt,args...) \
  15545. + xprintk(dev , KERN_ERR , fmt , ## args)
  15546. +#define WARN(dev,fmt,args...) \
  15547. + xprintk(dev , KERN_WARNING , fmt , ## args)
  15548. +#define INFO(dev,fmt,args...) \
  15549. + xprintk(dev , KERN_INFO , fmt , ## args)
  15550. +
  15551. +/*-------------------------------------------------------------------------*/
  15552. +
  15553. +static unsigned buflen = 4096;
  15554. +static unsigned qlen = 32;
  15555. +static unsigned pattern = 0;
  15556. +
  15557. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  15558. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  15559. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  15560. +
  15561. +/*
  15562. + * if it's nonzero, autoresume says how many seconds to wait
  15563. + * before trying to wake up the host after suspend.
  15564. + */
  15565. +static unsigned autoresume = 0;
  15566. +module_param (autoresume, uint, 0);
  15567. +
  15568. +/*
  15569. + * Normally the "loopback" configuration is second (index 1) so
  15570. + * it's not the default. Here's where to change that order, to
  15571. + * work better with hosts where config changes are problematic.
  15572. + * Or controllers (like superh) that only support one config.
  15573. + */
  15574. +static int loopdefault = 0;
  15575. +
  15576. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  15577. +
  15578. +/*-------------------------------------------------------------------------*/
  15579. +
  15580. +/* Thanks to NetChip Technologies for donating this product ID.
  15581. + *
  15582. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  15583. + * Instead: allocate your own, using normal USB-IF procedures.
  15584. + */
  15585. +#ifndef CONFIG_USB_ZERO_HNPTEST
  15586. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  15587. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  15588. +#else
  15589. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  15590. +#define DRIVER_PRODUCT_NUM 0xbadd
  15591. +#endif
  15592. +
  15593. +/*-------------------------------------------------------------------------*/
  15594. +
  15595. +/*
  15596. + * DESCRIPTORS ... most are static, but strings and (full)
  15597. + * configuration descriptors are built on demand.
  15598. + */
  15599. +
  15600. +/*
  15601. +#define STRING_MANUFACTURER 25
  15602. +#define STRING_PRODUCT 42
  15603. +#define STRING_SERIAL 101
  15604. +*/
  15605. +#define STRING_MANUFACTURER 1
  15606. +#define STRING_PRODUCT 2
  15607. +#define STRING_SERIAL 3
  15608. +
  15609. +#define STRING_SOURCE_SINK 250
  15610. +#define STRING_LOOPBACK 251
  15611. +
  15612. +/*
  15613. + * This device advertises two configurations; these numbers work
  15614. + * on a pxa250 as well as more flexible hardware.
  15615. + */
  15616. +#define CONFIG_SOURCE_SINK 3
  15617. +#define CONFIG_LOOPBACK 2
  15618. +
  15619. +/*
  15620. +static struct usb_device_descriptor
  15621. +device_desc = {
  15622. + .bLength = sizeof device_desc,
  15623. + .bDescriptorType = USB_DT_DEVICE,
  15624. +
  15625. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  15626. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  15627. +
  15628. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  15629. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  15630. + .iManufacturer = STRING_MANUFACTURER,
  15631. + .iProduct = STRING_PRODUCT,
  15632. + .iSerialNumber = STRING_SERIAL,
  15633. + .bNumConfigurations = 2,
  15634. +};
  15635. +*/
  15636. +static struct usb_device_descriptor
  15637. +device_desc = {
  15638. + .bLength = sizeof device_desc,
  15639. + .bDescriptorType = USB_DT_DEVICE,
  15640. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  15641. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  15642. + .bDeviceSubClass = 0,
  15643. + .bDeviceProtocol = 0,
  15644. + .bMaxPacketSize0 = 64,
  15645. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  15646. + .idVendor = __constant_cpu_to_le16 (0x0499),
  15647. + .idProduct = __constant_cpu_to_le16 (0x3002),
  15648. + .iManufacturer = STRING_MANUFACTURER,
  15649. + .iProduct = STRING_PRODUCT,
  15650. + .iSerialNumber = STRING_SERIAL,
  15651. + .bNumConfigurations = 1,
  15652. +};
  15653. +
  15654. +static struct usb_config_descriptor
  15655. +z_config = {
  15656. + .bLength = sizeof z_config,
  15657. + .bDescriptorType = USB_DT_CONFIG,
  15658. +
  15659. + /* compute wTotalLength on the fly */
  15660. + .bNumInterfaces = 2,
  15661. + .bConfigurationValue = 1,
  15662. + .iConfiguration = 0,
  15663. + .bmAttributes = 0x40,
  15664. + .bMaxPower = 0, /* self-powered */
  15665. +};
  15666. +
  15667. +
  15668. +static struct usb_otg_descriptor
  15669. +otg_descriptor = {
  15670. + .bLength = sizeof otg_descriptor,
  15671. + .bDescriptorType = USB_DT_OTG,
  15672. +
  15673. + .bmAttributes = USB_OTG_SRP,
  15674. +};
  15675. +
  15676. +/* one interface in each configuration */
  15677. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  15678. +
  15679. +/*
  15680. + * usb 2.0 devices need to expose both high speed and full speed
  15681. + * descriptors, unless they only run at full speed.
  15682. + *
  15683. + * that means alternate endpoint descriptors (bigger packets)
  15684. + * and a "device qualifier" ... plus more construction options
  15685. + * for the config descriptor.
  15686. + */
  15687. +
  15688. +static struct usb_qualifier_descriptor
  15689. +dev_qualifier = {
  15690. + .bLength = sizeof dev_qualifier,
  15691. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  15692. +
  15693. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  15694. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  15695. +
  15696. + .bNumConfigurations = 2,
  15697. +};
  15698. +
  15699. +
  15700. +struct usb_cs_as_general_descriptor {
  15701. + __u8 bLength;
  15702. + __u8 bDescriptorType;
  15703. +
  15704. + __u8 bDescriptorSubType;
  15705. + __u8 bTerminalLink;
  15706. + __u8 bDelay;
  15707. + __u16 wFormatTag;
  15708. +} __attribute__ ((packed));
  15709. +
  15710. +struct usb_cs_as_format_descriptor {
  15711. + __u8 bLength;
  15712. + __u8 bDescriptorType;
  15713. +
  15714. + __u8 bDescriptorSubType;
  15715. + __u8 bFormatType;
  15716. + __u8 bNrChannels;
  15717. + __u8 bSubframeSize;
  15718. + __u8 bBitResolution;
  15719. + __u8 bSamfreqType;
  15720. + __u8 tLowerSamFreq[3];
  15721. + __u8 tUpperSamFreq[3];
  15722. +} __attribute__ ((packed));
  15723. +
  15724. +static const struct usb_interface_descriptor
  15725. +z_audio_control_if_desc = {
  15726. + .bLength = sizeof z_audio_control_if_desc,
  15727. + .bDescriptorType = USB_DT_INTERFACE,
  15728. + .bInterfaceNumber = 0,
  15729. + .bAlternateSetting = 0,
  15730. + .bNumEndpoints = 0,
  15731. + .bInterfaceClass = USB_CLASS_AUDIO,
  15732. + .bInterfaceSubClass = 0x1,
  15733. + .bInterfaceProtocol = 0,
  15734. + .iInterface = 0,
  15735. +};
  15736. +
  15737. +static const struct usb_interface_descriptor
  15738. +z_audio_if_desc = {
  15739. + .bLength = sizeof z_audio_if_desc,
  15740. + .bDescriptorType = USB_DT_INTERFACE,
  15741. + .bInterfaceNumber = 1,
  15742. + .bAlternateSetting = 0,
  15743. + .bNumEndpoints = 0,
  15744. + .bInterfaceClass = USB_CLASS_AUDIO,
  15745. + .bInterfaceSubClass = 0x2,
  15746. + .bInterfaceProtocol = 0,
  15747. + .iInterface = 0,
  15748. +};
  15749. +
  15750. +static const struct usb_interface_descriptor
  15751. +z_audio_if_desc2 = {
  15752. + .bLength = sizeof z_audio_if_desc,
  15753. + .bDescriptorType = USB_DT_INTERFACE,
  15754. + .bInterfaceNumber = 1,
  15755. + .bAlternateSetting = 1,
  15756. + .bNumEndpoints = 1,
  15757. + .bInterfaceClass = USB_CLASS_AUDIO,
  15758. + .bInterfaceSubClass = 0x2,
  15759. + .bInterfaceProtocol = 0,
  15760. + .iInterface = 0,
  15761. +};
  15762. +
  15763. +static const struct usb_cs_as_general_descriptor
  15764. +z_audio_cs_as_if_desc = {
  15765. + .bLength = 7,
  15766. + .bDescriptorType = 0x24,
  15767. +
  15768. + .bDescriptorSubType = 0x01,
  15769. + .bTerminalLink = 0x01,
  15770. + .bDelay = 0x0,
  15771. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  15772. +};
  15773. +
  15774. +
  15775. +static const struct usb_cs_as_format_descriptor
  15776. +z_audio_cs_as_format_desc = {
  15777. + .bLength = 0xe,
  15778. + .bDescriptorType = 0x24,
  15779. +
  15780. + .bDescriptorSubType = 2,
  15781. + .bFormatType = 1,
  15782. + .bNrChannels = 1,
  15783. + .bSubframeSize = 1,
  15784. + .bBitResolution = 8,
  15785. + .bSamfreqType = 0,
  15786. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  15787. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  15788. +};
  15789. +
  15790. +static const struct usb_endpoint_descriptor
  15791. +z_iso_ep = {
  15792. + .bLength = 0x09,
  15793. + .bDescriptorType = 0x05,
  15794. + .bEndpointAddress = 0x04,
  15795. + .bmAttributes = 0x09,
  15796. + .wMaxPacketSize = 0x0038,
  15797. + .bInterval = 0x01,
  15798. + .bRefresh = 0x00,
  15799. + .bSynchAddress = 0x00,
  15800. +};
  15801. +
  15802. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15803. +
  15804. +// 9 bytes
  15805. +static char z_ac_interface_header_desc[] =
  15806. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  15807. +
  15808. +// 12 bytes
  15809. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  15810. + 0x03, 0x00, 0x00, 0x00};
  15811. +// 13 bytes
  15812. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  15813. + 0x02, 0x00, 0x02, 0x00, 0x00};
  15814. +// 9 bytes
  15815. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  15816. + 0x00};
  15817. +
  15818. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  15819. + 0x00};
  15820. +
  15821. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15822. +
  15823. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  15824. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15825. +
  15826. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  15827. + 0x00};
  15828. +
  15829. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15830. +
  15831. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  15832. + 0x00};
  15833. +
  15834. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15835. +
  15836. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  15837. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15838. +
  15839. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  15840. + 0x00};
  15841. +
  15842. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15843. +
  15844. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  15845. + 0x00};
  15846. +
  15847. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15848. +
  15849. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  15850. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15851. +
  15852. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  15853. + 0x00};
  15854. +
  15855. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15856. +
  15857. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  15858. + 0x00};
  15859. +
  15860. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15861. +
  15862. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  15863. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15864. +
  15865. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  15866. + 0x00};
  15867. +
  15868. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15869. +
  15870. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  15871. + 0x00};
  15872. +
  15873. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15874. +
  15875. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  15876. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15877. +
  15878. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  15879. + 0x00};
  15880. +
  15881. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15882. +
  15883. +
  15884. +
  15885. +static const struct usb_descriptor_header *z_function [] = {
  15886. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  15887. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  15888. + (struct usb_descriptor_header *) &z_0,
  15889. + (struct usb_descriptor_header *) &z_1,
  15890. + (struct usb_descriptor_header *) &z_2,
  15891. + (struct usb_descriptor_header *) &z_audio_if_desc,
  15892. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  15893. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  15894. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  15895. + (struct usb_descriptor_header *) &z_iso_ep,
  15896. + (struct usb_descriptor_header *) &z_iso_ep2,
  15897. + (struct usb_descriptor_header *) &za_0,
  15898. + (struct usb_descriptor_header *) &za_1,
  15899. + (struct usb_descriptor_header *) &za_2,
  15900. + (struct usb_descriptor_header *) &za_3,
  15901. + (struct usb_descriptor_header *) &za_4,
  15902. + (struct usb_descriptor_header *) &za_5,
  15903. + (struct usb_descriptor_header *) &za_6,
  15904. + (struct usb_descriptor_header *) &za_7,
  15905. + (struct usb_descriptor_header *) &za_8,
  15906. + (struct usb_descriptor_header *) &za_9,
  15907. + (struct usb_descriptor_header *) &za_10,
  15908. + (struct usb_descriptor_header *) &za_11,
  15909. + (struct usb_descriptor_header *) &za_12,
  15910. + (struct usb_descriptor_header *) &za_13,
  15911. + (struct usb_descriptor_header *) &za_14,
  15912. + (struct usb_descriptor_header *) &za_15,
  15913. + (struct usb_descriptor_header *) &za_16,
  15914. + (struct usb_descriptor_header *) &za_17,
  15915. + (struct usb_descriptor_header *) &za_18,
  15916. + (struct usb_descriptor_header *) &za_19,
  15917. + (struct usb_descriptor_header *) &za_20,
  15918. + (struct usb_descriptor_header *) &za_21,
  15919. + (struct usb_descriptor_header *) &za_22,
  15920. + (struct usb_descriptor_header *) &za_23,
  15921. + (struct usb_descriptor_header *) &za_24,
  15922. + NULL,
  15923. +};
  15924. +
  15925. +/* maxpacket and other transfer characteristics vary by speed. */
  15926. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  15927. +
  15928. +#else
  15929. +
  15930. +/* if there's no high speed support, maxpacket doesn't change. */
  15931. +#define ep_desc(g,hs,fs) fs
  15932. +
  15933. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  15934. +
  15935. +static char manufacturer [40];
  15936. +//static char serial [40];
  15937. +static char serial [] = "Ser 00 em";
  15938. +
  15939. +/* static strings, in UTF-8 */
  15940. +static struct usb_string strings [] = {
  15941. + { STRING_MANUFACTURER, manufacturer, },
  15942. + { STRING_PRODUCT, longname, },
  15943. + { STRING_SERIAL, serial, },
  15944. + { STRING_LOOPBACK, loopback, },
  15945. + { STRING_SOURCE_SINK, source_sink, },
  15946. + { } /* end of list */
  15947. +};
  15948. +
  15949. +static struct usb_gadget_strings stringtab = {
  15950. + .language = 0x0409, /* en-us */
  15951. + .strings = strings,
  15952. +};
  15953. +
  15954. +/*
  15955. + * config descriptors are also handcrafted. these must agree with code
  15956. + * that sets configurations, and with code managing interfaces and their
  15957. + * altsettings. other complexity may come from:
  15958. + *
  15959. + * - high speed support, including "other speed config" rules
  15960. + * - multiple configurations
  15961. + * - interfaces with alternate settings
  15962. + * - embedded class or vendor-specific descriptors
  15963. + *
  15964. + * this handles high speed, and has a second config that could as easily
  15965. + * have been an alternate interface setting (on most hardware).
  15966. + *
  15967. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  15968. + * should include an altsetting to test interrupt transfers, including
  15969. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  15970. + * device?)
  15971. + */
  15972. +static int
  15973. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  15974. +{
  15975. + int len;
  15976. + const struct usb_descriptor_header **function;
  15977. +
  15978. + function = z_function;
  15979. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  15980. + if (len < 0)
  15981. + return len;
  15982. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  15983. + return len;
  15984. +}
  15985. +
  15986. +/*-------------------------------------------------------------------------*/
  15987. +
  15988. +static struct usb_request *
  15989. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  15990. +{
  15991. + struct usb_request *req;
  15992. +
  15993. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  15994. + if (req) {
  15995. + req->length = length;
  15996. + req->buf = usb_ep_alloc_buffer (ep, length,
  15997. + &req->dma, GFP_ATOMIC);
  15998. + if (!req->buf) {
  15999. + usb_ep_free_request (ep, req);
  16000. + req = NULL;
  16001. + }
  16002. + }
  16003. + return req;
  16004. +}
  16005. +
  16006. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  16007. +{
  16008. + if (req->buf)
  16009. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  16010. + usb_ep_free_request (ep, req);
  16011. +}
  16012. +
  16013. +/*-------------------------------------------------------------------------*/
  16014. +
  16015. +/* optionally require specific source/sink data patterns */
  16016. +
  16017. +static int
  16018. +check_read_data (
  16019. + struct zero_dev *dev,
  16020. + struct usb_ep *ep,
  16021. + struct usb_request *req
  16022. +)
  16023. +{
  16024. + unsigned i;
  16025. + u8 *buf = req->buf;
  16026. +
  16027. + for (i = 0; i < req->actual; i++, buf++) {
  16028. + switch (pattern) {
  16029. + /* all-zeroes has no synchronization issues */
  16030. + case 0:
  16031. + if (*buf == 0)
  16032. + continue;
  16033. + break;
  16034. + /* mod63 stays in sync with short-terminated transfers,
  16035. + * or otherwise when host and gadget agree on how large
  16036. + * each usb transfer request should be. resync is done
  16037. + * with set_interface or set_config.
  16038. + */
  16039. + case 1:
  16040. + if (*buf == (u8)(i % 63))
  16041. + continue;
  16042. + break;
  16043. + }
  16044. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  16045. + usb_ep_set_halt (ep);
  16046. + return -EINVAL;
  16047. + }
  16048. + return 0;
  16049. +}
  16050. +
  16051. +/*-------------------------------------------------------------------------*/
  16052. +
  16053. +static void zero_reset_config (struct zero_dev *dev)
  16054. +{
  16055. + if (dev->config == 0)
  16056. + return;
  16057. +
  16058. + DBG (dev, "reset config\n");
  16059. +
  16060. + /* just disable endpoints, forcing completion of pending i/o.
  16061. + * all our completion handlers free their requests in this case.
  16062. + */
  16063. + if (dev->in_ep) {
  16064. + usb_ep_disable (dev->in_ep);
  16065. + dev->in_ep = NULL;
  16066. + }
  16067. + if (dev->out_ep) {
  16068. + usb_ep_disable (dev->out_ep);
  16069. + dev->out_ep = NULL;
  16070. + }
  16071. + dev->config = 0;
  16072. + del_timer (&dev->resume);
  16073. +}
  16074. +
  16075. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  16076. +
  16077. +static void
  16078. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  16079. +{
  16080. + struct zero_dev *dev = ep->driver_data;
  16081. + int status = req->status;
  16082. + int i, j;
  16083. +
  16084. + switch (status) {
  16085. +
  16086. + case 0: /* normal completion? */
  16087. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  16088. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  16089. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  16090. + rbuf[j] = ((__u8*)req->buf)[i];
  16091. + j++;
  16092. + if (j >= RBUF_LEN) j=0;
  16093. + }
  16094. + rbuf_start = j;
  16095. + //printk ("\n\n");
  16096. +
  16097. + if (rbuf_len < RBUF_LEN) {
  16098. + rbuf_len += req->actual;
  16099. + if (rbuf_len > RBUF_LEN) {
  16100. + rbuf_len = RBUF_LEN;
  16101. + }
  16102. + }
  16103. +
  16104. + break;
  16105. +
  16106. + /* this endpoint is normally active while we're configured */
  16107. + case -ECONNABORTED: /* hardware forced ep reset */
  16108. + case -ECONNRESET: /* request dequeued */
  16109. + case -ESHUTDOWN: /* disconnect from host */
  16110. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  16111. + req->actual, req->length);
  16112. + if (ep == dev->out_ep)
  16113. + check_read_data (dev, ep, req);
  16114. + free_ep_req (ep, req);
  16115. + return;
  16116. +
  16117. + case -EOVERFLOW: /* buffer overrun on read means that
  16118. + * we didn't provide a big enough
  16119. + * buffer.
  16120. + */
  16121. + default:
  16122. +#if 1
  16123. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  16124. + status, req->actual, req->length);
  16125. +#endif
  16126. + case -EREMOTEIO: /* short read */
  16127. + break;
  16128. + }
  16129. +
  16130. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  16131. + if (status) {
  16132. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  16133. + ep->name, req->length, status);
  16134. + usb_ep_set_halt (ep);
  16135. + /* FIXME recover later ... somehow */
  16136. + }
  16137. +}
  16138. +
  16139. +static struct usb_request *
  16140. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  16141. +{
  16142. + struct usb_request *req;
  16143. + int status;
  16144. +
  16145. + req = alloc_ep_req (ep, 512);
  16146. + if (!req)
  16147. + return NULL;
  16148. +
  16149. + req->complete = zero_isoc_complete;
  16150. +
  16151. + status = usb_ep_queue (ep, req, gfp_flags);
  16152. + if (status) {
  16153. + struct zero_dev *dev = ep->driver_data;
  16154. +
  16155. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  16156. + free_ep_req (ep, req);
  16157. + req = NULL;
  16158. + }
  16159. +
  16160. + return req;
  16161. +}
  16162. +
  16163. +/* change our operational config. this code must agree with the code
  16164. + * that returns config descriptors, and altsetting code.
  16165. + *
  16166. + * it's also responsible for power management interactions. some
  16167. + * configurations might not work with our current power sources.
  16168. + *
  16169. + * note that some device controller hardware will constrain what this
  16170. + * code can do, perhaps by disallowing more than one configuration or
  16171. + * by limiting configuration choices (like the pxa2xx).
  16172. + */
  16173. +static int
  16174. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  16175. +{
  16176. + int result = 0;
  16177. + struct usb_gadget *gadget = dev->gadget;
  16178. + const struct usb_endpoint_descriptor *d;
  16179. + struct usb_ep *ep;
  16180. +
  16181. + if (number == dev->config)
  16182. + return 0;
  16183. +
  16184. + zero_reset_config (dev);
  16185. +
  16186. + gadget_for_each_ep (ep, gadget) {
  16187. +
  16188. + if (strcmp (ep->name, "ep4") == 0) {
  16189. +
  16190. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  16191. + result = usb_ep_enable (ep, d);
  16192. +
  16193. + if (result == 0) {
  16194. + ep->driver_data = dev;
  16195. + dev->in_ep = ep;
  16196. +
  16197. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  16198. +
  16199. + dev->in_ep = ep;
  16200. + continue;
  16201. + }
  16202. +
  16203. + usb_ep_disable (ep);
  16204. + result = -EIO;
  16205. + }
  16206. + }
  16207. +
  16208. + }
  16209. +
  16210. + dev->config = number;
  16211. + return result;
  16212. +}
  16213. +
  16214. +/*-------------------------------------------------------------------------*/
  16215. +
  16216. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  16217. +{
  16218. + if (req->status || req->actual != req->length)
  16219. + DBG ((struct zero_dev *) ep->driver_data,
  16220. + "setup complete --> %d, %d/%d\n",
  16221. + req->status, req->actual, req->length);
  16222. +}
  16223. +
  16224. +/*
  16225. + * The setup() callback implements all the ep0 functionality that's
  16226. + * not handled lower down, in hardware or the hardware driver (like
  16227. + * device and endpoint feature flags, and their status). It's all
  16228. + * housekeeping for the gadget function we're implementing. Most of
  16229. + * the work is in config-specific setup.
  16230. + */
  16231. +static int
  16232. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  16233. +{
  16234. + struct zero_dev *dev = get_gadget_data (gadget);
  16235. + struct usb_request *req = dev->req;
  16236. + int value = -EOPNOTSUPP;
  16237. +
  16238. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  16239. + * but config change events will reconfigure hardware.
  16240. + */
  16241. + req->zero = 0;
  16242. + switch (ctrl->bRequest) {
  16243. +
  16244. + case USB_REQ_GET_DESCRIPTOR:
  16245. +
  16246. + switch (ctrl->wValue >> 8) {
  16247. +
  16248. + case USB_DT_DEVICE:
  16249. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  16250. + memcpy (req->buf, &device_desc, value);
  16251. + break;
  16252. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16253. + case USB_DT_DEVICE_QUALIFIER:
  16254. + if (!gadget->is_dualspeed)
  16255. + break;
  16256. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  16257. + memcpy (req->buf, &dev_qualifier, value);
  16258. + break;
  16259. +
  16260. + case USB_DT_OTHER_SPEED_CONFIG:
  16261. + if (!gadget->is_dualspeed)
  16262. + break;
  16263. + // FALLTHROUGH
  16264. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  16265. + case USB_DT_CONFIG:
  16266. + value = config_buf (gadget, req->buf,
  16267. + ctrl->wValue >> 8,
  16268. + ctrl->wValue & 0xff);
  16269. + if (value >= 0)
  16270. + value = min (ctrl->wLength, (u16) value);
  16271. + break;
  16272. +
  16273. + case USB_DT_STRING:
  16274. + /* wIndex == language code.
  16275. + * this driver only handles one language, you can
  16276. + * add string tables for other languages, using
  16277. + * any UTF-8 characters
  16278. + */
  16279. + value = usb_gadget_get_string (&stringtab,
  16280. + ctrl->wValue & 0xff, req->buf);
  16281. + if (value >= 0) {
  16282. + value = min (ctrl->wLength, (u16) value);
  16283. + }
  16284. + break;
  16285. + }
  16286. + break;
  16287. +
  16288. + /* currently two configs, two speeds */
  16289. + case USB_REQ_SET_CONFIGURATION:
  16290. + if (ctrl->bRequestType != 0)
  16291. + goto unknown;
  16292. +
  16293. + spin_lock (&dev->lock);
  16294. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  16295. + spin_unlock (&dev->lock);
  16296. + break;
  16297. + case USB_REQ_GET_CONFIGURATION:
  16298. + if (ctrl->bRequestType != USB_DIR_IN)
  16299. + goto unknown;
  16300. + *(u8 *)req->buf = dev->config;
  16301. + value = min (ctrl->wLength, (u16) 1);
  16302. + break;
  16303. +
  16304. + /* until we add altsetting support, or other interfaces,
  16305. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  16306. + * and already killed pending endpoint I/O.
  16307. + */
  16308. + case USB_REQ_SET_INTERFACE:
  16309. +
  16310. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  16311. + goto unknown;
  16312. + spin_lock (&dev->lock);
  16313. + if (dev->config) {
  16314. + u8 config = dev->config;
  16315. +
  16316. + /* resets interface configuration, forgets about
  16317. + * previous transaction state (queued bufs, etc)
  16318. + * and re-inits endpoint state (toggle etc)
  16319. + * no response queued, just zero status == success.
  16320. + * if we had more than one interface we couldn't
  16321. + * use this "reset the config" shortcut.
  16322. + */
  16323. + zero_reset_config (dev);
  16324. + zero_set_config (dev, config, GFP_ATOMIC);
  16325. + value = 0;
  16326. + }
  16327. + spin_unlock (&dev->lock);
  16328. + break;
  16329. + case USB_REQ_GET_INTERFACE:
  16330. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  16331. + value = ctrl->wLength;
  16332. + break;
  16333. + }
  16334. + else {
  16335. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  16336. + goto unknown;
  16337. + if (!dev->config)
  16338. + break;
  16339. + if (ctrl->wIndex != 0) {
  16340. + value = -EDOM;
  16341. + break;
  16342. + }
  16343. + *(u8 *)req->buf = 0;
  16344. + value = min (ctrl->wLength, (u16) 1);
  16345. + }
  16346. + break;
  16347. +
  16348. + /*
  16349. + * These are the same vendor-specific requests supported by
  16350. + * Intel's USB 2.0 compliance test devices. We exceed that
  16351. + * device spec by allowing multiple-packet requests.
  16352. + */
  16353. + case 0x5b: /* control WRITE test -- fill the buffer */
  16354. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  16355. + goto unknown;
  16356. + if (ctrl->wValue || ctrl->wIndex)
  16357. + break;
  16358. + /* just read that many bytes into the buffer */
  16359. + if (ctrl->wLength > USB_BUFSIZ)
  16360. + break;
  16361. + value = ctrl->wLength;
  16362. + break;
  16363. + case 0x5c: /* control READ test -- return the buffer */
  16364. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  16365. + goto unknown;
  16366. + if (ctrl->wValue || ctrl->wIndex)
  16367. + break;
  16368. + /* expect those bytes are still in the buffer; send back */
  16369. + if (ctrl->wLength > USB_BUFSIZ
  16370. + || ctrl->wLength != req->length)
  16371. + break;
  16372. + value = ctrl->wLength;
  16373. + break;
  16374. +
  16375. + case 0x01: // SET_CUR
  16376. + case 0x02:
  16377. + case 0x03:
  16378. + case 0x04:
  16379. + case 0x05:
  16380. + value = ctrl->wLength;
  16381. + break;
  16382. + case 0x81:
  16383. + switch (ctrl->wValue) {
  16384. + case 0x0201:
  16385. + case 0x0202:
  16386. + ((u8*)req->buf)[0] = 0x00;
  16387. + ((u8*)req->buf)[1] = 0xe3;
  16388. + break;
  16389. + case 0x0300:
  16390. + case 0x0500:
  16391. + ((u8*)req->buf)[0] = 0x00;
  16392. + break;
  16393. + }
  16394. + //((u8*)req->buf)[0] = 0x81;
  16395. + //((u8*)req->buf)[1] = 0x81;
  16396. + value = ctrl->wLength;
  16397. + break;
  16398. + case 0x82:
  16399. + switch (ctrl->wValue) {
  16400. + case 0x0201:
  16401. + case 0x0202:
  16402. + ((u8*)req->buf)[0] = 0x00;
  16403. + ((u8*)req->buf)[1] = 0xc3;
  16404. + break;
  16405. + case 0x0300:
  16406. + case 0x0500:
  16407. + ((u8*)req->buf)[0] = 0x00;
  16408. + break;
  16409. + }
  16410. + //((u8*)req->buf)[0] = 0x82;
  16411. + //((u8*)req->buf)[1] = 0x82;
  16412. + value = ctrl->wLength;
  16413. + break;
  16414. + case 0x83:
  16415. + switch (ctrl->wValue) {
  16416. + case 0x0201:
  16417. + case 0x0202:
  16418. + ((u8*)req->buf)[0] = 0x00;
  16419. + ((u8*)req->buf)[1] = 0x00;
  16420. + break;
  16421. + case 0x0300:
  16422. + ((u8*)req->buf)[0] = 0x60;
  16423. + break;
  16424. + case 0x0500:
  16425. + ((u8*)req->buf)[0] = 0x18;
  16426. + break;
  16427. + }
  16428. + //((u8*)req->buf)[0] = 0x83;
  16429. + //((u8*)req->buf)[1] = 0x83;
  16430. + value = ctrl->wLength;
  16431. + break;
  16432. + case 0x84:
  16433. + switch (ctrl->wValue) {
  16434. + case 0x0201:
  16435. + case 0x0202:
  16436. + ((u8*)req->buf)[0] = 0x00;
  16437. + ((u8*)req->buf)[1] = 0x01;
  16438. + break;
  16439. + case 0x0300:
  16440. + case 0x0500:
  16441. + ((u8*)req->buf)[0] = 0x08;
  16442. + break;
  16443. + }
  16444. + //((u8*)req->buf)[0] = 0x84;
  16445. + //((u8*)req->buf)[1] = 0x84;
  16446. + value = ctrl->wLength;
  16447. + break;
  16448. + case 0x85:
  16449. + ((u8*)req->buf)[0] = 0x85;
  16450. + ((u8*)req->buf)[1] = 0x85;
  16451. + value = ctrl->wLength;
  16452. + break;
  16453. +
  16454. +
  16455. + default:
  16456. +unknown:
  16457. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  16458. + ctrl->bRequestType, ctrl->bRequest,
  16459. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  16460. + }
  16461. +
  16462. + /* respond with data transfer before status phase? */
  16463. + if (value >= 0) {
  16464. + req->length = value;
  16465. + req->zero = value < ctrl->wLength
  16466. + && (value % gadget->ep0->maxpacket) == 0;
  16467. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  16468. + if (value < 0) {
  16469. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  16470. + req->status = 0;
  16471. + zero_setup_complete (gadget->ep0, req);
  16472. + }
  16473. + }
  16474. +
  16475. + /* device either stalls (value < 0) or reports success */
  16476. + return value;
  16477. +}
  16478. +
  16479. +static void
  16480. +zero_disconnect (struct usb_gadget *gadget)
  16481. +{
  16482. + struct zero_dev *dev = get_gadget_data (gadget);
  16483. + unsigned long flags;
  16484. +
  16485. + spin_lock_irqsave (&dev->lock, flags);
  16486. + zero_reset_config (dev);
  16487. +
  16488. + /* a more significant application might have some non-usb
  16489. + * activities to quiesce here, saving resources like power
  16490. + * or pushing the notification up a network stack.
  16491. + */
  16492. + spin_unlock_irqrestore (&dev->lock, flags);
  16493. +
  16494. + /* next we may get setup() calls to enumerate new connections;
  16495. + * or an unbind() during shutdown (including removing module).
  16496. + */
  16497. +}
  16498. +
  16499. +static void
  16500. +zero_autoresume (unsigned long _dev)
  16501. +{
  16502. + struct zero_dev *dev = (struct zero_dev *) _dev;
  16503. + int status;
  16504. +
  16505. + /* normally the host would be woken up for something
  16506. + * more significant than just a timer firing...
  16507. + */
  16508. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  16509. + status = usb_gadget_wakeup (dev->gadget);
  16510. + DBG (dev, "wakeup --> %d\n", status);
  16511. + }
  16512. +}
  16513. +
  16514. +/*-------------------------------------------------------------------------*/
  16515. +
  16516. +static void
  16517. +zero_unbind (struct usb_gadget *gadget)
  16518. +{
  16519. + struct zero_dev *dev = get_gadget_data (gadget);
  16520. +
  16521. + DBG (dev, "unbind\n");
  16522. +
  16523. + /* we've already been disconnected ... no i/o is active */
  16524. + if (dev->req)
  16525. + free_ep_req (gadget->ep0, dev->req);
  16526. + del_timer_sync (&dev->resume);
  16527. + kfree (dev);
  16528. + set_gadget_data (gadget, NULL);
  16529. +}
  16530. +
  16531. +static int
  16532. +zero_bind (struct usb_gadget *gadget)
  16533. +{
  16534. + struct zero_dev *dev;
  16535. + //struct usb_ep *ep;
  16536. +
  16537. + printk("binding\n");
  16538. + /*
  16539. + * DRIVER POLICY CHOICE: you may want to do this differently.
  16540. + * One thing to avoid is reusing a bcdDevice revision code
  16541. + * with different host-visible configurations or behavior
  16542. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  16543. + */
  16544. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  16545. +
  16546. +
  16547. + /* ok, we made sense of the hardware ... */
  16548. + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  16549. + if (!dev)
  16550. + return -ENOMEM;
  16551. + memset (dev, 0, sizeof *dev);
  16552. + spin_lock_init (&dev->lock);
  16553. + dev->gadget = gadget;
  16554. + set_gadget_data (gadget, dev);
  16555. +
  16556. + /* preallocate control response and buffer */
  16557. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  16558. + if (!dev->req)
  16559. + goto enomem;
  16560. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  16561. + &dev->req->dma, GFP_KERNEL);
  16562. + if (!dev->req->buf)
  16563. + goto enomem;
  16564. +
  16565. + dev->req->complete = zero_setup_complete;
  16566. +
  16567. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  16568. +
  16569. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16570. + /* assume ep0 uses the same value for both speeds ... */
  16571. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  16572. +
  16573. + /* and that all endpoints are dual-speed */
  16574. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  16575. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  16576. +#endif
  16577. +
  16578. + usb_gadget_set_selfpowered (gadget);
  16579. +
  16580. + init_timer (&dev->resume);
  16581. + dev->resume.function = zero_autoresume;
  16582. + dev->resume.data = (unsigned long) dev;
  16583. +
  16584. + gadget->ep0->driver_data = dev;
  16585. +
  16586. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  16587. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  16588. + EP_OUT_NAME, EP_IN_NAME);
  16589. +
  16590. + snprintf (manufacturer, sizeof manufacturer,
  16591. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  16592. + gadget->name);
  16593. +
  16594. + return 0;
  16595. +
  16596. +enomem:
  16597. + zero_unbind (gadget);
  16598. + return -ENOMEM;
  16599. +}
  16600. +
  16601. +/*-------------------------------------------------------------------------*/
  16602. +
  16603. +static void
  16604. +zero_suspend (struct usb_gadget *gadget)
  16605. +{
  16606. + struct zero_dev *dev = get_gadget_data (gadget);
  16607. +
  16608. + if (gadget->speed == USB_SPEED_UNKNOWN)
  16609. + return;
  16610. +
  16611. + if (autoresume) {
  16612. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  16613. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  16614. + } else
  16615. + DBG (dev, "suspend\n");
  16616. +}
  16617. +
  16618. +static void
  16619. +zero_resume (struct usb_gadget *gadget)
  16620. +{
  16621. + struct zero_dev *dev = get_gadget_data (gadget);
  16622. +
  16623. + DBG (dev, "resume\n");
  16624. + del_timer (&dev->resume);
  16625. +}
  16626. +
  16627. +
  16628. +/*-------------------------------------------------------------------------*/
  16629. +
  16630. +static struct usb_gadget_driver zero_driver = {
  16631. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16632. + .speed = USB_SPEED_HIGH,
  16633. +#else
  16634. + .speed = USB_SPEED_FULL,
  16635. +#endif
  16636. + .function = (char *) longname,
  16637. + .bind = zero_bind,
  16638. + .unbind = zero_unbind,
  16639. +
  16640. + .setup = zero_setup,
  16641. + .disconnect = zero_disconnect,
  16642. +
  16643. + .suspend = zero_suspend,
  16644. + .resume = zero_resume,
  16645. +
  16646. + .driver = {
  16647. + .name = (char *) shortname,
  16648. + // .shutdown = ...
  16649. + // .suspend = ...
  16650. + // .resume = ...
  16651. + },
  16652. +};
  16653. +
  16654. +MODULE_AUTHOR ("David Brownell");
  16655. +MODULE_LICENSE ("Dual BSD/GPL");
  16656. +
  16657. +static struct proc_dir_entry *pdir, *pfile;
  16658. +
  16659. +static int isoc_read_data (char *page, char **start,
  16660. + off_t off, int count,
  16661. + int *eof, void *data)
  16662. +{
  16663. + int i;
  16664. + static int c = 0;
  16665. + static int done = 0;
  16666. + static int s = 0;
  16667. +
  16668. +/*
  16669. + printk ("\ncount: %d\n", count);
  16670. + printk ("rbuf_start: %d\n", rbuf_start);
  16671. + printk ("rbuf_len: %d\n", rbuf_len);
  16672. + printk ("off: %d\n", off);
  16673. + printk ("start: %p\n\n", *start);
  16674. +*/
  16675. + if (done) {
  16676. + c = 0;
  16677. + done = 0;
  16678. + *eof = 1;
  16679. + return 0;
  16680. + }
  16681. +
  16682. + if (c == 0) {
  16683. + if (rbuf_len == RBUF_LEN)
  16684. + s = rbuf_start;
  16685. + else s = 0;
  16686. + }
  16687. +
  16688. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  16689. + page[i] = rbuf[(c+s) % RBUF_LEN];
  16690. + }
  16691. + *start = page;
  16692. +
  16693. + if (c >= rbuf_len) {
  16694. + *eof = 1;
  16695. + done = 1;
  16696. + }
  16697. +
  16698. +
  16699. + return i;
  16700. +}
  16701. +
  16702. +static int __init init (void)
  16703. +{
  16704. +
  16705. + int retval = 0;
  16706. +
  16707. + pdir = proc_mkdir("isoc_test", NULL);
  16708. + if(pdir == NULL) {
  16709. + retval = -ENOMEM;
  16710. + printk("Error creating dir\n");
  16711. + goto done;
  16712. + }
  16713. + pdir->owner = THIS_MODULE;
  16714. +
  16715. + pfile = create_proc_read_entry("isoc_data",
  16716. + 0444, pdir,
  16717. + isoc_read_data,
  16718. + NULL);
  16719. + if (pfile == NULL) {
  16720. + retval = -ENOMEM;
  16721. + printk("Error creating file\n");
  16722. + goto no_file;
  16723. + }
  16724. + pfile->owner = THIS_MODULE;
  16725. +
  16726. + return usb_gadget_register_driver (&zero_driver);
  16727. +
  16728. + no_file:
  16729. + remove_proc_entry("isoc_data", NULL);
  16730. + done:
  16731. + return retval;
  16732. +}
  16733. +module_init (init);
  16734. +
  16735. +static void __exit cleanup (void)
  16736. +{
  16737. +
  16738. + usb_gadget_unregister_driver (&zero_driver);
  16739. +
  16740. + remove_proc_entry("isoc_data", pdir);
  16741. + remove_proc_entry("isoc_test", NULL);
  16742. +}
  16743. +module_exit (cleanup);
  16744. --- /dev/null
  16745. +++ b/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  16746. @@ -0,0 +1,142 @@
  16747. +/* ==========================================================================
  16748. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  16749. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  16750. + * otherwise expressly agreed to in writing between Synopsys and you.
  16751. + *
  16752. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  16753. + * any End User Software License Agreement or Agreement for Licensed Product
  16754. + * with Synopsys or any supplement thereto. You are permitted to use and
  16755. + * redistribute this Software in source and binary forms, with or without
  16756. + * modification, provided that redistributions of source code must retain this
  16757. + * notice. You may not view, use, disclose, copy or distribute this file or
  16758. + * any information contained herein except pursuant to this license grant from
  16759. + * Synopsys. If you do not agree with this notice, including the disclaimer
  16760. + * below, then you are not authorized to use the Software.
  16761. + *
  16762. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  16763. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16764. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16765. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  16766. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16767. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16768. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  16769. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16770. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16771. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  16772. + * DAMAGE.
  16773. + * ========================================================================== */
  16774. +
  16775. +#if !defined(__DWC_CFI_COMMON_H__)
  16776. +#define __DWC_CFI_COMMON_H__
  16777. +
  16778. +//#include <linux/types.h>
  16779. +
  16780. +/**
  16781. + * @file
  16782. + *
  16783. + * This file contains the CFI specific common constants, interfaces
  16784. + * (functions and macros) and structures for Linux. No PCD specific
  16785. + * data structure or definition is to be included in this file.
  16786. + *
  16787. + */
  16788. +
  16789. +/** This is a request for all Core Features */
  16790. +#define VEN_CORE_GET_FEATURES 0xB1
  16791. +
  16792. +/** This is a request to get the value of a specific Core Feature */
  16793. +#define VEN_CORE_GET_FEATURE 0xB2
  16794. +
  16795. +/** This command allows the host to set the value of a specific Core Feature */
  16796. +#define VEN_CORE_SET_FEATURE 0xB3
  16797. +
  16798. +/** This command allows the host to set the default values of
  16799. + * either all or any specific Core Feature
  16800. + */
  16801. +#define VEN_CORE_RESET_FEATURES 0xB4
  16802. +
  16803. +/** This command forces the PCD to write the deferred values of a Core Features */
  16804. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  16805. +
  16806. +/** This request reads a DWORD value from a register at the specified offset */
  16807. +#define VEN_CORE_READ_REGISTER 0xB6
  16808. +
  16809. +/** This request writes a DWORD value into a register at the specified offset */
  16810. +#define VEN_CORE_WRITE_REGISTER 0xB7
  16811. +
  16812. +/** This structure is the header of the Core Features dataset returned to
  16813. + * the Host
  16814. + */
  16815. +struct cfi_all_features_header {
  16816. +/** The features header structure length is */
  16817. +#define CFI_ALL_FEATURES_HDR_LEN 8
  16818. + /**
  16819. + * The total length of the features dataset returned to the Host
  16820. + */
  16821. + uint16_t wTotalLen;
  16822. +
  16823. + /**
  16824. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  16825. + * This field identifies the version of the CFI Specification with which
  16826. + * the device is compliant.
  16827. + */
  16828. + uint16_t wVersion;
  16829. +
  16830. + /** The ID of the Core */
  16831. + uint16_t wCoreID;
  16832. +#define CFI_CORE_ID_UDC 1
  16833. +#define CFI_CORE_ID_OTG 2
  16834. +#define CFI_CORE_ID_WUDEV 3
  16835. +
  16836. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  16837. + uint16_t wNumFeatures;
  16838. +} UPACKED;
  16839. +
  16840. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  16841. +
  16842. +/** This structure is a header of the Core Feature descriptor dataset returned to
  16843. + * the Host after the VEN_CORE_GET_FEATURES request
  16844. + */
  16845. +struct cfi_feature_desc_header {
  16846. +#define CFI_FEATURE_DESC_HDR_LEN 8
  16847. +
  16848. + /** The feature ID */
  16849. + uint16_t wFeatureID;
  16850. +
  16851. + /** Length of this feature descriptor in bytes - including the
  16852. + * length of the feature name string
  16853. + */
  16854. + uint16_t wLength;
  16855. +
  16856. + /** The data length of this feature in bytes */
  16857. + uint16_t wDataLength;
  16858. +
  16859. + /**
  16860. + * Attributes of this features
  16861. + * D0: Access rights
  16862. + * 0 - Read/Write
  16863. + * 1 - Read only
  16864. + */
  16865. + uint8_t bmAttributes;
  16866. +#define CFI_FEATURE_ATTR_RO 1
  16867. +#define CFI_FEATURE_ATTR_RW 0
  16868. +
  16869. + /** Length of the feature name in bytes */
  16870. + uint8_t bNameLen;
  16871. +
  16872. + /** The feature name buffer */
  16873. + //uint8_t *name;
  16874. +} UPACKED;
  16875. +
  16876. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  16877. +
  16878. +/**
  16879. + * This structure describes a NULL terminated string referenced by its id field.
  16880. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  16881. + */
  16882. +struct cfi_string {
  16883. + uint16_t id;
  16884. + const uint8_t *s;
  16885. +};
  16886. +typedef struct cfi_string cfi_string_t;
  16887. +
  16888. +#endif
  16889. --- /dev/null
  16890. +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  16891. @@ -0,0 +1,854 @@
  16892. +/* ==========================================================================
  16893. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  16894. + * $Revision: #12 $
  16895. + * $Date: 2011/10/26 $
  16896. + * $Change: 1873028 $
  16897. + *
  16898. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  16899. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  16900. + * otherwise expressly agreed to in writing between Synopsys and you.
  16901. + *
  16902. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  16903. + * any End User Software License Agreement or Agreement for Licensed Product
  16904. + * with Synopsys or any supplement thereto. You are permitted to use and
  16905. + * redistribute this Software in source and binary forms, with or without
  16906. + * modification, provided that redistributions of source code must retain this
  16907. + * notice. You may not view, use, disclose, copy or distribute this file or
  16908. + * any information contained herein except pursuant to this license grant from
  16909. + * Synopsys. If you do not agree with this notice, including the disclaimer
  16910. + * below, then you are not authorized to use the Software.
  16911. + *
  16912. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  16913. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16914. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16915. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  16916. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16917. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16918. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  16919. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16920. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16921. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  16922. + * DAMAGE.
  16923. + * ========================================================================== */
  16924. +
  16925. +#include "dwc_os.h"
  16926. +#include "dwc_otg_regs.h"
  16927. +#include "dwc_otg_cil.h"
  16928. +#include "dwc_otg_adp.h"
  16929. +
  16930. +/** @file
  16931. + *
  16932. + * This file contains the most of the Attach Detect Protocol implementation for
  16933. + * the driver to support OTG Rev2.0.
  16934. + *
  16935. + */
  16936. +
  16937. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  16938. +{
  16939. + adpctl_data_t adpctl;
  16940. +
  16941. + adpctl.d32 = value;
  16942. + adpctl.b.ar = 0x2;
  16943. +
  16944. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  16945. +
  16946. + while (adpctl.b.ar) {
  16947. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  16948. + }
  16949. +
  16950. +}
  16951. +
  16952. +/**
  16953. + * Function is called to read ADP registers
  16954. + */
  16955. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  16956. +{
  16957. + adpctl_data_t adpctl;
  16958. +
  16959. + adpctl.d32 = 0;
  16960. + adpctl.b.ar = 0x1;
  16961. +
  16962. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  16963. +
  16964. + while (adpctl.b.ar) {
  16965. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  16966. + }
  16967. +
  16968. + return adpctl.d32;
  16969. +}
  16970. +
  16971. +/**
  16972. + * Function is called to read ADPCTL register and filter Write-clear bits
  16973. + */
  16974. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  16975. +{
  16976. + adpctl_data_t adpctl;
  16977. +
  16978. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  16979. + adpctl.b.adp_tmout_int = 0;
  16980. + adpctl.b.adp_prb_int = 0;
  16981. + adpctl.b.adp_tmout_int = 0;
  16982. +
  16983. + return adpctl.d32;
  16984. +}
  16985. +
  16986. +/**
  16987. + * Function is called to write ADP registers
  16988. + */
  16989. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  16990. + uint32_t set)
  16991. +{
  16992. + dwc_otg_adp_write_reg(core_if,
  16993. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  16994. +}
  16995. +
  16996. +static void adp_sense_timeout(void *ptr)
  16997. +{
  16998. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  16999. + core_if->adp.sense_timer_started = 0;
  17000. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  17001. + if (core_if->adp_enable) {
  17002. + dwc_otg_adp_sense_stop(core_if);
  17003. + dwc_otg_adp_probe_start(core_if);
  17004. + }
  17005. +}
  17006. +
  17007. +/**
  17008. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  17009. + */
  17010. +static void adp_vbuson_timeout(void *ptr)
  17011. +{
  17012. + gpwrdn_data_t gpwrdn;
  17013. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  17014. + hprt0_data_t hprt0 = {.d32 = 0 };
  17015. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  17016. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  17017. + if (core_if) {
  17018. + core_if->adp.vbuson_timer_started = 0;
  17019. + /* Turn off vbus */
  17020. + hprt0.b.prtpwr = 1;
  17021. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  17022. + gpwrdn.d32 = 0;
  17023. +
  17024. + /* Power off the core */
  17025. + if (core_if->power_down == 2) {
  17026. + /* Enable Wakeup Logic */
  17027. +// gpwrdn.b.wkupactiv = 1;
  17028. + gpwrdn.b.pmuactv = 0;
  17029. + gpwrdn.b.pwrdnrstn = 1;
  17030. + gpwrdn.b.pwrdnclmp = 1;
  17031. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17032. + gpwrdn.d32);
  17033. +
  17034. + /* Suspend the Phy Clock */
  17035. + pcgcctl.b.stoppclk = 1;
  17036. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  17037. +
  17038. + /* Switch on VDD */
  17039. +// gpwrdn.b.wkupactiv = 1;
  17040. + gpwrdn.b.pmuactv = 1;
  17041. + gpwrdn.b.pwrdnrstn = 1;
  17042. + gpwrdn.b.pwrdnclmp = 1;
  17043. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17044. + gpwrdn.d32);
  17045. + } else {
  17046. + /* Enable Power Down Logic */
  17047. + gpwrdn.b.pmuintsel = 1;
  17048. + gpwrdn.b.pmuactv = 1;
  17049. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17050. + }
  17051. +
  17052. + /* Power off the core */
  17053. + if (core_if->power_down == 2) {
  17054. + gpwrdn.d32 = 0;
  17055. + gpwrdn.b.pwrdnswtch = 1;
  17056. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  17057. + gpwrdn.d32, 0);
  17058. + }
  17059. +
  17060. + /* Unmask SRP detected interrupt from Power Down Logic */
  17061. + gpwrdn.d32 = 0;
  17062. + gpwrdn.b.srp_det_msk = 1;
  17063. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17064. +
  17065. + dwc_otg_adp_probe_start(core_if);
  17066. + dwc_otg_dump_global_registers(core_if);
  17067. + dwc_otg_dump_host_registers(core_if);
  17068. + }
  17069. +
  17070. +}
  17071. +
  17072. +/**
  17073. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  17074. + * not asserted within 1.1 seconds.
  17075. + *
  17076. + * @param core_if the pointer to core_if strucure.
  17077. + */
  17078. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  17079. +{
  17080. + core_if->adp.vbuson_timer_started = 1;
  17081. + if (core_if->adp.vbuson_timer)
  17082. + {
  17083. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  17084. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  17085. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  17086. + } else {
  17087. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  17088. + }
  17089. +}
  17090. +
  17091. +#if 0
  17092. +/**
  17093. + * Masks all DWC OTG core interrupts
  17094. + *
  17095. + */
  17096. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  17097. +{
  17098. + int i;
  17099. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  17100. +
  17101. + /* Mask Host Interrupts */
  17102. +
  17103. + /* Clear and disable HCINTs */
  17104. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  17105. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  17106. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  17107. +
  17108. + }
  17109. +
  17110. + /* Clear and disable HAINT */
  17111. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  17112. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  17113. +
  17114. + /* Mask Device Interrupts */
  17115. + if (!core_if->multiproc_int_enable) {
  17116. + /* Clear and disable IN Endpoint interrupts */
  17117. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  17118. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  17119. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  17120. + diepint, 0xFFFFFFFF);
  17121. + }
  17122. +
  17123. + /* Clear and disable OUT Endpoint interrupts */
  17124. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  17125. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  17126. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  17127. + doepint, 0xFFFFFFFF);
  17128. + }
  17129. +
  17130. + /* Clear and disable DAINT */
  17131. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  17132. + 0xFFFFFFFF);
  17133. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  17134. + } else {
  17135. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  17136. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  17137. + diepeachintmsk[i], 0);
  17138. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  17139. + diepint, 0xFFFFFFFF);
  17140. + }
  17141. +
  17142. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  17143. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  17144. + doepeachintmsk[i], 0);
  17145. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  17146. + doepint, 0xFFFFFFFF);
  17147. + }
  17148. +
  17149. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  17150. + 0);
  17151. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  17152. + 0xFFFFFFFF);
  17153. +
  17154. + }
  17155. +
  17156. + /* Disable interrupts */
  17157. + ahbcfg.b.glblintrmsk = 1;
  17158. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  17159. +
  17160. + /* Disable all interrupts. */
  17161. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  17162. +
  17163. + /* Clear any pending interrupts */
  17164. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17165. +
  17166. + /* Clear any pending OTG Interrupts */
  17167. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  17168. +}
  17169. +
  17170. +/**
  17171. + * Unmask Port Connection Detected interrupt
  17172. + *
  17173. + */
  17174. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  17175. +{
  17176. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  17177. +
  17178. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  17179. +}
  17180. +#endif
  17181. +
  17182. +/**
  17183. + * Starts the ADP Probing
  17184. + *
  17185. + * @param core_if the pointer to core_if structure.
  17186. + */
  17187. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  17188. +{
  17189. +
  17190. + adpctl_data_t adpctl = {.d32 = 0};
  17191. + gpwrdn_data_t gpwrdn;
  17192. +#if 0
  17193. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  17194. + .b.adp_sns_int = 1, b.adp_tmout_int};
  17195. +#endif
  17196. + dwc_otg_disable_global_interrupts(core_if);
  17197. + DWC_PRINTF("ADP Probe Start\n");
  17198. + core_if->adp.probe_enabled = 1;
  17199. +
  17200. + adpctl.b.adpres = 1;
  17201. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17202. +
  17203. + while (adpctl.b.adpres) {
  17204. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17205. + }
  17206. +
  17207. + adpctl.d32 = 0;
  17208. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17209. +
  17210. + /* In Host mode unmask SRP detected interrupt */
  17211. + gpwrdn.d32 = 0;
  17212. + gpwrdn.b.sts_chngint_msk = 1;
  17213. + if (!gpwrdn.b.idsts) {
  17214. + gpwrdn.b.srp_det_msk = 1;
  17215. + }
  17216. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17217. +
  17218. + adpctl.b.adp_tmout_int_msk = 1;
  17219. + adpctl.b.adp_prb_int_msk = 1;
  17220. + adpctl.b.prb_dschg = 1;
  17221. + adpctl.b.prb_delta = 1;
  17222. + adpctl.b.prb_per = 1;
  17223. + adpctl.b.adpen = 1;
  17224. + adpctl.b.enaprb = 1;
  17225. +
  17226. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17227. + DWC_PRINTF("ADP Probe Finish\n");
  17228. + return 0;
  17229. +}
  17230. +
  17231. +/**
  17232. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  17233. + * within 3 seconds.
  17234. + *
  17235. + * @param core_if the pointer to core_if strucure.
  17236. + */
  17237. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  17238. +{
  17239. + core_if->adp.sense_timer_started = 1;
  17240. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  17241. +}
  17242. +
  17243. +/**
  17244. + * Starts the ADP Sense
  17245. + *
  17246. + * @param core_if the pointer to core_if strucure.
  17247. + */
  17248. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  17249. +{
  17250. + adpctl_data_t adpctl;
  17251. +
  17252. + DWC_PRINTF("ADP Sense Start\n");
  17253. +
  17254. + /* Unmask ADP sense interrupt and mask all other from the core */
  17255. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17256. + adpctl.b.adp_sns_int_msk = 1;
  17257. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17258. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  17259. +
  17260. + /* Set ADP reset bit*/
  17261. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17262. + adpctl.b.adpres = 1;
  17263. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17264. +
  17265. + while (adpctl.b.adpres) {
  17266. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17267. + }
  17268. +
  17269. + adpctl.b.adpres = 0;
  17270. + adpctl.b.adpen = 1;
  17271. + adpctl.b.enasns = 1;
  17272. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17273. +
  17274. + dwc_otg_adp_sense_timer_start(core_if);
  17275. +
  17276. + return 0;
  17277. +}
  17278. +
  17279. +/**
  17280. + * Stops the ADP Probing
  17281. + *
  17282. + * @param core_if the pointer to core_if strucure.
  17283. + */
  17284. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  17285. +{
  17286. +
  17287. + adpctl_data_t adpctl;
  17288. + DWC_PRINTF("Stop ADP probe\n");
  17289. + core_if->adp.probe_enabled = 0;
  17290. + core_if->adp.probe_counter = 0;
  17291. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17292. +
  17293. + adpctl.b.adpen = 0;
  17294. + adpctl.b.adp_prb_int = 1;
  17295. + adpctl.b.adp_tmout_int = 1;
  17296. + adpctl.b.adp_sns_int = 1;
  17297. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17298. +
  17299. + return 0;
  17300. +}
  17301. +
  17302. +/**
  17303. + * Stops the ADP Sensing
  17304. + *
  17305. + * @param core_if the pointer to core_if strucure.
  17306. + */
  17307. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  17308. +{
  17309. + adpctl_data_t adpctl;
  17310. +
  17311. + core_if->adp.sense_enabled = 0;
  17312. +
  17313. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17314. + adpctl.b.enasns = 0;
  17315. + adpctl.b.adp_sns_int = 1;
  17316. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17317. +
  17318. + return 0;
  17319. +}
  17320. +
  17321. +/**
  17322. + * Called to turn on the VBUS after initial ADP probe in host mode.
  17323. + * If port power was already enabled in cil_hcd_start function then
  17324. + * only schedule a timer.
  17325. + *
  17326. + * @param core_if the pointer to core_if structure.
  17327. + */
  17328. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  17329. +{
  17330. + hprt0_data_t hprt0 = {.d32 = 0 };
  17331. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  17332. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  17333. +
  17334. + if (hprt0.b.prtpwr == 0) {
  17335. + hprt0.b.prtpwr = 1;
  17336. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  17337. + }
  17338. +
  17339. + dwc_otg_adp_vbuson_timer_start(core_if);
  17340. +}
  17341. +
  17342. +/**
  17343. + * Called right after driver is loaded
  17344. + * to perform initial actions for ADP
  17345. + *
  17346. + * @param core_if the pointer to core_if structure.
  17347. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  17348. + */
  17349. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  17350. +{
  17351. + gpwrdn_data_t gpwrdn;
  17352. +
  17353. + DWC_PRINTF("ADP Initial Start\n");
  17354. + core_if->adp.adp_started = 1;
  17355. +
  17356. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17357. + dwc_otg_disable_global_interrupts(core_if);
  17358. + if (is_host) {
  17359. + DWC_PRINTF("HOST MODE\n");
  17360. + /* Enable Power Down Logic Interrupt*/
  17361. + gpwrdn.d32 = 0;
  17362. + gpwrdn.b.pmuintsel = 1;
  17363. + gpwrdn.b.pmuactv = 1;
  17364. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17365. + /* Initialize first ADP probe to obtain Ramp Time value */
  17366. + core_if->adp.initial_probe = 1;
  17367. + dwc_otg_adp_probe_start(core_if);
  17368. + } else {
  17369. + gotgctl_data_t gotgctl;
  17370. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  17371. + DWC_PRINTF("DEVICE MODE\n");
  17372. + if (gotgctl.b.bsesvld == 0) {
  17373. + /* Enable Power Down Logic Interrupt*/
  17374. + gpwrdn.d32 = 0;
  17375. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  17376. + gpwrdn.b.pmuintsel = 1;
  17377. + gpwrdn.b.pmuactv = 1;
  17378. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17379. + core_if->adp.initial_probe = 1;
  17380. + dwc_otg_adp_probe_start(core_if);
  17381. + } else {
  17382. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  17383. + core_if->op_state = B_PERIPHERAL;
  17384. + dwc_otg_core_init(core_if);
  17385. + dwc_otg_enable_global_interrupts(core_if);
  17386. + cil_pcd_start(core_if);
  17387. + dwc_otg_dump_global_registers(core_if);
  17388. + dwc_otg_dump_dev_registers(core_if);
  17389. + }
  17390. + }
  17391. +}
  17392. +
  17393. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  17394. +{
  17395. + core_if->adp.adp_started = 0;
  17396. + core_if->adp.initial_probe = 0;
  17397. + core_if->adp.probe_timer_values[0] = -1;
  17398. + core_if->adp.probe_timer_values[1] = -1;
  17399. + core_if->adp.probe_enabled = 0;
  17400. + core_if->adp.sense_enabled = 0;
  17401. + core_if->adp.sense_timer_started = 0;
  17402. + core_if->adp.vbuson_timer_started = 0;
  17403. + core_if->adp.probe_counter = 0;
  17404. + core_if->adp.gpwrdn = 0;
  17405. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  17406. + /* Initialize timers */
  17407. + core_if->adp.sense_timer =
  17408. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  17409. + core_if->adp.vbuson_timer =
  17410. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  17411. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  17412. + {
  17413. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  17414. + }
  17415. +}
  17416. +
  17417. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  17418. +{
  17419. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  17420. + gpwrdn.b.pmuintsel = 1;
  17421. + gpwrdn.b.pmuactv = 1;
  17422. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17423. +
  17424. + if (core_if->adp.probe_enabled)
  17425. + dwc_otg_adp_probe_stop(core_if);
  17426. + if (core_if->adp.sense_enabled)
  17427. + dwc_otg_adp_sense_stop(core_if);
  17428. + if (core_if->adp.sense_timer_started)
  17429. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  17430. + if (core_if->adp.vbuson_timer_started)
  17431. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  17432. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  17433. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  17434. +}
  17435. +
  17436. +/////////////////////////////////////////////////////////////////////
  17437. +////////////// ADP Interrupt Handlers ///////////////////////////////
  17438. +/////////////////////////////////////////////////////////////////////
  17439. +/**
  17440. + * This function sets Ramp Timer values
  17441. + */
  17442. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  17443. +{
  17444. + if (core_if->adp.probe_timer_values[0] == -1) {
  17445. + core_if->adp.probe_timer_values[0] = val;
  17446. + core_if->adp.probe_timer_values[1] = -1;
  17447. + return 1;
  17448. + } else {
  17449. + core_if->adp.probe_timer_values[1] =
  17450. + core_if->adp.probe_timer_values[0];
  17451. + core_if->adp.probe_timer_values[0] = val;
  17452. + return 0;
  17453. + }
  17454. +}
  17455. +
  17456. +/**
  17457. + * This function compares Ramp Timer values
  17458. + */
  17459. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  17460. +{
  17461. + uint32_t diff;
  17462. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  17463. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  17464. + else
  17465. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  17466. + if(diff < 2) {
  17467. + return 0;
  17468. + } else {
  17469. + return 1;
  17470. + }
  17471. +}
  17472. +
  17473. +/**
  17474. + * This function handles ADP Probe Interrupts
  17475. + */
  17476. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  17477. + uint32_t val)
  17478. +{
  17479. + adpctl_data_t adpctl = {.d32 = 0 };
  17480. + gpwrdn_data_t gpwrdn, temp;
  17481. + adpctl.d32 = val;
  17482. +
  17483. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17484. + core_if->adp.probe_counter++;
  17485. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17486. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  17487. + DWC_PRINTF("RTIM value is 0\n");
  17488. + goto exit;
  17489. + }
  17490. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  17491. + core_if->adp.initial_probe) {
  17492. + core_if->adp.initial_probe = 0;
  17493. + dwc_otg_adp_probe_stop(core_if);
  17494. + gpwrdn.d32 = 0;
  17495. + gpwrdn.b.pmuactv = 1;
  17496. + gpwrdn.b.pmuintsel = 1;
  17497. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17498. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17499. +
  17500. + /* check which value is for device mode and which for Host mode */
  17501. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  17502. + /*
  17503. + * Turn on VBUS after initial ADP probe.
  17504. + */
  17505. + core_if->op_state = A_HOST;
  17506. + dwc_otg_enable_global_interrupts(core_if);
  17507. + DWC_SPINUNLOCK(core_if->lock);
  17508. + cil_hcd_start(core_if);
  17509. + dwc_otg_adp_turnon_vbus(core_if);
  17510. + DWC_SPINLOCK(core_if->lock);
  17511. + } else {
  17512. + /*
  17513. + * Initiate SRP after initial ADP probe.
  17514. + */
  17515. + dwc_otg_enable_global_interrupts(core_if);
  17516. + dwc_otg_initiate_srp(core_if);
  17517. + }
  17518. + } else if (core_if->adp.probe_counter > 2){
  17519. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17520. + if (compare_timer_values(core_if)) {
  17521. + DWC_PRINTF("Difference in timer values !!! \n");
  17522. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  17523. + dwc_otg_adp_probe_stop(core_if);
  17524. +
  17525. + /* Power on the core */
  17526. + if (core_if->power_down == 2) {
  17527. + gpwrdn.b.pwrdnswtch = 1;
  17528. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17529. + gpwrdn, 0, gpwrdn.d32);
  17530. + }
  17531. +
  17532. + /* check which value is for device mode and which for Host mode */
  17533. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  17534. + /* Disable Interrupt from Power Down Logic */
  17535. + gpwrdn.d32 = 0;
  17536. + gpwrdn.b.pmuintsel = 1;
  17537. + gpwrdn.b.pmuactv = 1;
  17538. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17539. + gpwrdn, gpwrdn.d32, 0);
  17540. +
  17541. + /*
  17542. + * Initialize the Core for Host mode.
  17543. + */
  17544. + core_if->op_state = A_HOST;
  17545. + dwc_otg_core_init(core_if);
  17546. + dwc_otg_enable_global_interrupts(core_if);
  17547. + cil_hcd_start(core_if);
  17548. + } else {
  17549. + gotgctl_data_t gotgctl;
  17550. + /* Mask SRP detected interrupt from Power Down Logic */
  17551. + gpwrdn.d32 = 0;
  17552. + gpwrdn.b.srp_det_msk = 1;
  17553. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17554. + gpwrdn, gpwrdn.d32, 0);
  17555. +
  17556. + /* Disable Power Down Logic */
  17557. + gpwrdn.d32 = 0;
  17558. + gpwrdn.b.pmuintsel = 1;
  17559. + gpwrdn.b.pmuactv = 1;
  17560. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17561. + gpwrdn, gpwrdn.d32, 0);
  17562. +
  17563. + /*
  17564. + * Initialize the Core for Device mode.
  17565. + */
  17566. + core_if->op_state = B_PERIPHERAL;
  17567. + dwc_otg_core_init(core_if);
  17568. + dwc_otg_enable_global_interrupts(core_if);
  17569. + cil_pcd_start(core_if);
  17570. +
  17571. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  17572. + if (!gotgctl.b.bsesvld) {
  17573. + dwc_otg_initiate_srp(core_if);
  17574. + }
  17575. + }
  17576. + }
  17577. + if (core_if->power_down == 2) {
  17578. + if (gpwrdn.b.bsessvld) {
  17579. + /* Mask SRP detected interrupt from Power Down Logic */
  17580. + gpwrdn.d32 = 0;
  17581. + gpwrdn.b.srp_det_msk = 1;
  17582. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17583. +
  17584. + /* Disable Power Down Logic */
  17585. + gpwrdn.d32 = 0;
  17586. + gpwrdn.b.pmuactv = 1;
  17587. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17588. +
  17589. + /*
  17590. + * Initialize the Core for Device mode.
  17591. + */
  17592. + core_if->op_state = B_PERIPHERAL;
  17593. + dwc_otg_core_init(core_if);
  17594. + dwc_otg_enable_global_interrupts(core_if);
  17595. + cil_pcd_start(core_if);
  17596. + }
  17597. + }
  17598. + }
  17599. +exit:
  17600. + /* Clear interrupt */
  17601. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17602. + adpctl.b.adp_prb_int = 1;
  17603. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17604. +
  17605. + return 0;
  17606. +}
  17607. +
  17608. +/**
  17609. + * This function hadles ADP Sense Interrupt
  17610. + */
  17611. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  17612. +{
  17613. + adpctl_data_t adpctl;
  17614. + /* Stop ADP Sense timer */
  17615. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  17616. +
  17617. + /* Restart ADP Sense timer */
  17618. + dwc_otg_adp_sense_timer_start(core_if);
  17619. +
  17620. + /* Clear interrupt */
  17621. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17622. + adpctl.b.adp_sns_int = 1;
  17623. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17624. +
  17625. + return 0;
  17626. +}
  17627. +
  17628. +/**
  17629. + * This function handles ADP Probe Interrupts
  17630. + */
  17631. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  17632. + uint32_t val)
  17633. +{
  17634. + adpctl_data_t adpctl = {.d32 = 0 };
  17635. + adpctl.d32 = val;
  17636. + set_timer_value(core_if, adpctl.b.rtim);
  17637. +
  17638. + /* Clear interrupt */
  17639. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17640. + adpctl.b.adp_tmout_int = 1;
  17641. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17642. +
  17643. + return 0;
  17644. +}
  17645. +
  17646. +/**
  17647. + * ADP Interrupt handler.
  17648. + *
  17649. + */
  17650. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  17651. +{
  17652. + int retval = 0;
  17653. + adpctl_data_t adpctl = {.d32 = 0};
  17654. +
  17655. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17656. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  17657. +
  17658. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  17659. + DWC_PRINTF("ADP Sense interrupt\n");
  17660. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  17661. + }
  17662. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  17663. + DWC_PRINTF("ADP timeout interrupt\n");
  17664. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  17665. + }
  17666. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  17667. + DWC_PRINTF("ADP Probe interrupt\n");
  17668. + adpctl.b.adp_prb_int = 1;
  17669. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  17670. + }
  17671. +
  17672. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  17673. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17674. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  17675. +
  17676. + return retval;
  17677. +}
  17678. +
  17679. +/**
  17680. + *
  17681. + * @param core_if Programming view of DWC_otg controller.
  17682. + */
  17683. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  17684. +{
  17685. +
  17686. +#ifndef DWC_HOST_ONLY
  17687. + hprt0_data_t hprt0;
  17688. + gpwrdn_data_t gpwrdn;
  17689. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  17690. +
  17691. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17692. + /* check which value is for device mode and which for Host mode */
  17693. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  17694. + DWC_PRINTF("SRP: Host mode\n");
  17695. +
  17696. + if (core_if->adp_enable) {
  17697. + dwc_otg_adp_probe_stop(core_if);
  17698. +
  17699. + /* Power on the core */
  17700. + if (core_if->power_down == 2) {
  17701. + gpwrdn.b.pwrdnswtch = 1;
  17702. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17703. + gpwrdn, 0, gpwrdn.d32);
  17704. + }
  17705. +
  17706. + core_if->op_state = A_HOST;
  17707. + dwc_otg_core_init(core_if);
  17708. + dwc_otg_enable_global_interrupts(core_if);
  17709. + cil_hcd_start(core_if);
  17710. + }
  17711. +
  17712. + /* Turn on the port power bit. */
  17713. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  17714. + hprt0.b.prtpwr = 1;
  17715. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  17716. +
  17717. + /* Start the Connection timer. So a message can be displayed
  17718. + * if connect does not occur within 10 seconds. */
  17719. + cil_hcd_session_start(core_if);
  17720. + } else {
  17721. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  17722. + if (core_if->adp_enable) {
  17723. + dwc_otg_adp_probe_stop(core_if);
  17724. +
  17725. + /* Power on the core */
  17726. + if (core_if->power_down == 2) {
  17727. + gpwrdn.b.pwrdnswtch = 1;
  17728. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17729. + gpwrdn, 0, gpwrdn.d32);
  17730. + }
  17731. +
  17732. + gpwrdn.d32 = 0;
  17733. + gpwrdn.b.pmuactv = 0;
  17734. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17735. + gpwrdn.d32);
  17736. +
  17737. + core_if->op_state = B_PERIPHERAL;
  17738. + dwc_otg_core_init(core_if);
  17739. + dwc_otg_enable_global_interrupts(core_if);
  17740. + cil_pcd_start(core_if);
  17741. + }
  17742. + }
  17743. +#endif
  17744. + return 1;
  17745. +}
  17746. --- /dev/null
  17747. +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  17748. @@ -0,0 +1,80 @@
  17749. +/* ==========================================================================
  17750. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  17751. + * $Revision: #7 $
  17752. + * $Date: 2011/10/24 $
  17753. + * $Change: 1871159 $
  17754. + *
  17755. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  17756. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17757. + * otherwise expressly agreed to in writing between Synopsys and you.
  17758. + *
  17759. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17760. + * any End User Software License Agreement or Agreement for Licensed Product
  17761. + * with Synopsys or any supplement thereto. You are permitted to use and
  17762. + * redistribute this Software in source and binary forms, with or without
  17763. + * modification, provided that redistributions of source code must retain this
  17764. + * notice. You may not view, use, disclose, copy or distribute this file or
  17765. + * any information contained herein except pursuant to this license grant from
  17766. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17767. + * below, then you are not authorized to use the Software.
  17768. + *
  17769. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17770. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17771. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17772. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17773. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17774. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17775. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17776. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17777. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17778. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17779. + * DAMAGE.
  17780. + * ========================================================================== */
  17781. +
  17782. +#ifndef __DWC_OTG_ADP_H__
  17783. +#define __DWC_OTG_ADP_H__
  17784. +
  17785. +/**
  17786. + * @file
  17787. + *
  17788. + * This file contains the Attach Detect Protocol interfaces and defines
  17789. + * (functions) and structures for Linux.
  17790. + *
  17791. + */
  17792. +
  17793. +#define DWC_OTG_ADP_UNATTACHED 0
  17794. +#define DWC_OTG_ADP_ATTACHED 1
  17795. +#define DWC_OTG_ADP_UNKOWN 2
  17796. +
  17797. +typedef struct dwc_otg_adp {
  17798. + uint32_t adp_started;
  17799. + uint32_t initial_probe;
  17800. + int32_t probe_timer_values[2];
  17801. + uint32_t probe_enabled;
  17802. + uint32_t sense_enabled;
  17803. + dwc_timer_t *sense_timer;
  17804. + uint32_t sense_timer_started;
  17805. + dwc_timer_t *vbuson_timer;
  17806. + uint32_t vbuson_timer_started;
  17807. + uint32_t attached;
  17808. + uint32_t probe_counter;
  17809. + uint32_t gpwrdn;
  17810. +} dwc_otg_adp_t;
  17811. +
  17812. +/**
  17813. + * Attach Detect Protocol functions
  17814. + */
  17815. +
  17816. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  17817. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  17818. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  17819. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  17820. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  17821. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  17822. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  17823. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  17824. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  17825. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  17826. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  17827. +
  17828. +#endif //__DWC_OTG_ADP_H__
  17829. --- /dev/null
  17830. +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  17831. @@ -0,0 +1,1210 @@
  17832. +/* ==========================================================================
  17833. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  17834. + * $Revision: #44 $
  17835. + * $Date: 2010/11/29 $
  17836. + * $Change: 1636033 $
  17837. + *
  17838. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  17839. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17840. + * otherwise expressly agreed to in writing between Synopsys and you.
  17841. + *
  17842. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17843. + * any End User Software License Agreement or Agreement for Licensed Product
  17844. + * with Synopsys or any supplement thereto. You are permitted to use and
  17845. + * redistribute this Software in source and binary forms, with or without
  17846. + * modification, provided that redistributions of source code must retain this
  17847. + * notice. You may not view, use, disclose, copy or distribute this file or
  17848. + * any information contained herein except pursuant to this license grant from
  17849. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17850. + * below, then you are not authorized to use the Software.
  17851. + *
  17852. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17853. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17854. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17855. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17856. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17857. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17858. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17859. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17860. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17861. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17862. + * DAMAGE.
  17863. + * ========================================================================== */
  17864. +
  17865. +/** @file
  17866. + *
  17867. + * The diagnostic interface will provide access to the controller for
  17868. + * bringing up the hardware and testing. The Linux driver attributes
  17869. + * feature will be used to provide the Linux Diagnostic
  17870. + * Interface. These attributes are accessed through sysfs.
  17871. + */
  17872. +
  17873. +/** @page "Linux Module Attributes"
  17874. + *
  17875. + * The Linux module attributes feature is used to provide the Linux
  17876. + * Diagnostic Interface. These attributes are accessed through sysfs.
  17877. + * The diagnostic interface will provide access to the controller for
  17878. + * bringing up the hardware and testing.
  17879. +
  17880. + The following table shows the attributes.
  17881. + <table>
  17882. + <tr>
  17883. + <td><b> Name</b></td>
  17884. + <td><b> Description</b></td>
  17885. + <td><b> Access</b></td>
  17886. + </tr>
  17887. +
  17888. + <tr>
  17889. + <td> mode </td>
  17890. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  17891. + <td> Read</td>
  17892. + </tr>
  17893. +
  17894. + <tr>
  17895. + <td> hnpcapable </td>
  17896. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  17897. + Read returns the current value.</td>
  17898. + <td> Read/Write</td>
  17899. + </tr>
  17900. +
  17901. + <tr>
  17902. + <td> srpcapable </td>
  17903. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  17904. + Read returns the current value.</td>
  17905. + <td> Read/Write</td>
  17906. + </tr>
  17907. +
  17908. + <tr>
  17909. + <td> hsic_connect </td>
  17910. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  17911. + Read returns the current value.</td>
  17912. + <td> Read/Write</td>
  17913. + </tr>
  17914. +
  17915. + <tr>
  17916. + <td> inv_sel_hsic </td>
  17917. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  17918. + Read returns the current value.</td>
  17919. + <td> Read/Write</td>
  17920. + </tr>
  17921. +
  17922. + <tr>
  17923. + <td> hnp </td>
  17924. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  17925. + <td> Read/Write</td>
  17926. + </tr>
  17927. +
  17928. + <tr>
  17929. + <td> srp </td>
  17930. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  17931. + <td> Read/Write</td>
  17932. + </tr>
  17933. +
  17934. + <tr>
  17935. + <td> buspower </td>
  17936. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  17937. + <td> Read/Write</td>
  17938. + </tr>
  17939. +
  17940. + <tr>
  17941. + <td> bussuspend </td>
  17942. + <td> Suspends the USB bus.</td>
  17943. + <td> Read/Write</td>
  17944. + </tr>
  17945. +
  17946. + <tr>
  17947. + <td> busconnected </td>
  17948. + <td> Gets the connection status of the bus</td>
  17949. + <td> Read</td>
  17950. + </tr>
  17951. +
  17952. + <tr>
  17953. + <td> gotgctl </td>
  17954. + <td> Gets or sets the Core Control Status Register.</td>
  17955. + <td> Read/Write</td>
  17956. + </tr>
  17957. +
  17958. + <tr>
  17959. + <td> gusbcfg </td>
  17960. + <td> Gets or sets the Core USB Configuration Register</td>
  17961. + <td> Read/Write</td>
  17962. + </tr>
  17963. +
  17964. + <tr>
  17965. + <td> grxfsiz </td>
  17966. + <td> Gets or sets the Receive FIFO Size Register</td>
  17967. + <td> Read/Write</td>
  17968. + </tr>
  17969. +
  17970. + <tr>
  17971. + <td> gnptxfsiz </td>
  17972. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  17973. + <td> Read/Write</td>
  17974. + </tr>
  17975. +
  17976. + <tr>
  17977. + <td> gpvndctl </td>
  17978. + <td> Gets or sets the PHY Vendor Control Register</td>
  17979. + <td> Read/Write</td>
  17980. + </tr>
  17981. +
  17982. + <tr>
  17983. + <td> ggpio </td>
  17984. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  17985. + or sets the upper 16 bits.</td>
  17986. + <td> Read/Write</td>
  17987. + </tr>
  17988. +
  17989. + <tr>
  17990. + <td> guid </td>
  17991. + <td> Gets or sets the value of the User ID Register</td>
  17992. + <td> Read/Write</td>
  17993. + </tr>
  17994. +
  17995. + <tr>
  17996. + <td> gsnpsid </td>
  17997. + <td> Gets the value of the Synopsys ID Regester</td>
  17998. + <td> Read</td>
  17999. + </tr>
  18000. +
  18001. + <tr>
  18002. + <td> devspeed </td>
  18003. + <td> Gets or sets the device speed setting in the DCFG register</td>
  18004. + <td> Read/Write</td>
  18005. + </tr>
  18006. +
  18007. + <tr>
  18008. + <td> enumspeed </td>
  18009. + <td> Gets the device enumeration Speed.</td>
  18010. + <td> Read</td>
  18011. + </tr>
  18012. +
  18013. + <tr>
  18014. + <td> hptxfsiz </td>
  18015. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  18016. + <td> Read</td>
  18017. + </tr>
  18018. +
  18019. + <tr>
  18020. + <td> hprt0 </td>
  18021. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  18022. + <td> Read/Write</td>
  18023. + </tr>
  18024. +
  18025. + <tr>
  18026. + <td> regoffset </td>
  18027. + <td> Sets the register offset for the next Register Access</td>
  18028. + <td> Read/Write</td>
  18029. + </tr>
  18030. +
  18031. + <tr>
  18032. + <td> regvalue </td>
  18033. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  18034. + <td> Read/Write</td>
  18035. + </tr>
  18036. +
  18037. + <tr>
  18038. + <td> remote_wakeup </td>
  18039. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  18040. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  18041. + Wakeup signalling bit in the Device Control Register is set for 1
  18042. + milli-second.</td>
  18043. + <td> Read/Write</td>
  18044. + </tr>
  18045. +
  18046. + <tr>
  18047. + <td> rem_wakeup_pwrdn </td>
  18048. + <td> On read, shows the status core - hibernated or not. On write, initiates
  18049. + a remote wakeup of the device from Hibernation. </td>
  18050. + <td> Read/Write</td>
  18051. + </tr>
  18052. +
  18053. + <tr>
  18054. + <td> mode_ch_tim_en </td>
  18055. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  18056. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  18057. + after Suspend or LPM. </td>
  18058. + <td> Read/Write</td>
  18059. + </tr>
  18060. +
  18061. + <tr>
  18062. + <td> fr_interval </td>
  18063. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  18064. + reload HFIR register during runtime. The application can write a value to this
  18065. + register only after the Port Enable bit of the Host Port Control and Status
  18066. + register (HPRT.PrtEnaPort) has been set </td>
  18067. + <td> Read/Write</td>
  18068. + </tr>
  18069. +
  18070. + <tr>
  18071. + <td> disconnect_us </td>
  18072. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  18073. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  18074. + <td> Read/Write</td>
  18075. + </tr>
  18076. +
  18077. + <tr>
  18078. + <td> regdump </td>
  18079. + <td> Dumps the contents of core registers.</td>
  18080. + <td> Read</td>
  18081. + </tr>
  18082. +
  18083. + <tr>
  18084. + <td> spramdump </td>
  18085. + <td> Dumps the contents of core registers.</td>
  18086. + <td> Read</td>
  18087. + </tr>
  18088. +
  18089. + <tr>
  18090. + <td> hcddump </td>
  18091. + <td> Dumps the current HCD state.</td>
  18092. + <td> Read</td>
  18093. + </tr>
  18094. +
  18095. + <tr>
  18096. + <td> hcd_frrem </td>
  18097. + <td> Shows the average value of the Frame Remaining
  18098. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  18099. + occurs. This can be used to determine the average interrupt latency. Also
  18100. + shows the average Frame Remaining value for start_transfer and the "a" and
  18101. + "b" sample points. The "a" and "b" sample points may be used during debugging
  18102. + bto determine how long it takes to execute a section of the HCD code.</td>
  18103. + <td> Read</td>
  18104. + </tr>
  18105. +
  18106. + <tr>
  18107. + <td> rd_reg_test </td>
  18108. + <td> Displays the time required to read the GNPTXFSIZ register many times
  18109. + (the output shows the number of times the register is read).
  18110. + <td> Read</td>
  18111. + </tr>
  18112. +
  18113. + <tr>
  18114. + <td> wr_reg_test </td>
  18115. + <td> Displays the time required to write the GNPTXFSIZ register many times
  18116. + (the output shows the number of times the register is written).
  18117. + <td> Read</td>
  18118. + </tr>
  18119. +
  18120. + <tr>
  18121. + <td> lpm_response </td>
  18122. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  18123. + <td> Write</td>
  18124. + </tr>
  18125. +
  18126. + <tr>
  18127. + <td> sleep_status </td>
  18128. + <td> Shows sleep status of device.
  18129. + <td> Read</td>
  18130. + </tr>
  18131. +
  18132. + </table>
  18133. +
  18134. + Example usage:
  18135. + To get the current mode:
  18136. + cat /sys/devices/lm0/mode
  18137. +
  18138. + To power down the USB:
  18139. + echo 0 > /sys/devices/lm0/buspower
  18140. + */
  18141. +
  18142. +#include "dwc_otg_os_dep.h"
  18143. +#include "dwc_os.h"
  18144. +#include "dwc_otg_driver.h"
  18145. +#include "dwc_otg_attr.h"
  18146. +#include "dwc_otg_core_if.h"
  18147. +#include "dwc_otg_pcd_if.h"
  18148. +#include "dwc_otg_hcd_if.h"
  18149. +
  18150. +/*
  18151. + * MACROs for defining sysfs attribute
  18152. + */
  18153. +#ifdef LM_INTERFACE
  18154. +
  18155. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18156. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18157. +{ \
  18158. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18159. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18160. + uint32_t val; \
  18161. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18162. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18163. +}
  18164. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18165. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18166. + const char *buf, size_t count) \
  18167. +{ \
  18168. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18169. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18170. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18171. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18172. + return count; \
  18173. +}
  18174. +
  18175. +#elif defined(PCI_INTERFACE)
  18176. +
  18177. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18178. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18179. +{ \
  18180. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18181. + uint32_t val; \
  18182. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18183. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18184. +}
  18185. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18186. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18187. + const char *buf, size_t count) \
  18188. +{ \
  18189. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18190. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18191. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18192. + return count; \
  18193. +}
  18194. +
  18195. +#elif defined(PLATFORM_INTERFACE)
  18196. +
  18197. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18198. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18199. +{ \
  18200. + struct platform_device *platform_dev = \
  18201. + container_of(_dev, struct platform_device, dev); \
  18202. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18203. + uint32_t val; \
  18204. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  18205. + __func__, _dev, platform_dev, otg_dev); \
  18206. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18207. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18208. +}
  18209. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18210. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18211. + const char *buf, size_t count) \
  18212. +{ \
  18213. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18214. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18215. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18216. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18217. + return count; \
  18218. +}
  18219. +#endif
  18220. +
  18221. +/*
  18222. + * MACROs for defining sysfs attribute for 32-bit registers
  18223. + */
  18224. +#ifdef LM_INTERFACE
  18225. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18226. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18227. +{ \
  18228. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18229. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18230. + uint32_t val; \
  18231. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18232. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18233. +}
  18234. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18235. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18236. + const char *buf, size_t count) \
  18237. +{ \
  18238. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18239. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18240. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18241. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18242. + return count; \
  18243. +}
  18244. +#elif defined(PCI_INTERFACE)
  18245. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18246. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18247. +{ \
  18248. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18249. + uint32_t val; \
  18250. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18251. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18252. +}
  18253. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18254. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18255. + const char *buf, size_t count) \
  18256. +{ \
  18257. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18258. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18259. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18260. + return count; \
  18261. +}
  18262. +
  18263. +#elif defined(PLATFORM_INTERFACE)
  18264. +#include "dwc_otg_dbg.h"
  18265. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18266. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18267. +{ \
  18268. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18269. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18270. + uint32_t val; \
  18271. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  18272. + __func__, _dev, platform_dev, otg_dev); \
  18273. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18274. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18275. +}
  18276. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18277. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18278. + const char *buf, size_t count) \
  18279. +{ \
  18280. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18281. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18282. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18283. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18284. + return count; \
  18285. +}
  18286. +
  18287. +#endif
  18288. +
  18289. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  18290. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18291. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18292. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  18293. +
  18294. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  18295. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18296. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  18297. +
  18298. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  18299. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18300. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18301. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  18302. +
  18303. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  18304. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18305. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  18306. +
  18307. +/** @name Functions for Show/Store of Attributes */
  18308. +/**@{*/
  18309. +
  18310. +/**
  18311. + * Helper function returning the otg_device structure of the given device
  18312. + */
  18313. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  18314. +{
  18315. + dwc_otg_device_t *otg_dev;
  18316. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  18317. + return otg_dev;
  18318. +}
  18319. +
  18320. +/**
  18321. + * Show the register offset of the Register Access.
  18322. + */
  18323. +static ssize_t regoffset_show(struct device *_dev,
  18324. + struct device_attribute *attr, char *buf)
  18325. +{
  18326. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18327. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  18328. + otg_dev->os_dep.reg_offset);
  18329. +}
  18330. +
  18331. +/**
  18332. + * Set the register offset for the next Register Access Read/Write
  18333. + */
  18334. +static ssize_t regoffset_store(struct device *_dev,
  18335. + struct device_attribute *attr,
  18336. + const char *buf, size_t count)
  18337. +{
  18338. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18339. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  18340. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  18341. + if (offset < SZ_256K) {
  18342. +#elif defined(PCI_INTERFACE)
  18343. + if (offset < 0x00040000) {
  18344. +#endif
  18345. + otg_dev->os_dep.reg_offset = offset;
  18346. + } else {
  18347. + dev_err(_dev, "invalid offset\n");
  18348. + }
  18349. +
  18350. + return count;
  18351. +}
  18352. +
  18353. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  18354. +
  18355. +/**
  18356. + * Show the value of the register at the offset in the reg_offset
  18357. + * attribute.
  18358. + */
  18359. +static ssize_t regvalue_show(struct device *_dev,
  18360. + struct device_attribute *attr, char *buf)
  18361. +{
  18362. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18363. + uint32_t val;
  18364. + volatile uint32_t *addr;
  18365. +
  18366. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  18367. + /* Calculate the address */
  18368. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  18369. + (uint8_t *) otg_dev->os_dep.base);
  18370. + val = DWC_READ_REG32(addr);
  18371. + return snprintf(buf,
  18372. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  18373. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  18374. + val);
  18375. + } else {
  18376. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  18377. + return sprintf(buf, "invalid offset\n");
  18378. + }
  18379. +}
  18380. +
  18381. +/**
  18382. + * Store the value in the register at the offset in the reg_offset
  18383. + * attribute.
  18384. + *
  18385. + */
  18386. +static ssize_t regvalue_store(struct device *_dev,
  18387. + struct device_attribute *attr,
  18388. + const char *buf, size_t count)
  18389. +{
  18390. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18391. + volatile uint32_t *addr;
  18392. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18393. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  18394. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  18395. + /* Calculate the address */
  18396. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  18397. + (uint8_t *) otg_dev->os_dep.base);
  18398. + DWC_WRITE_REG32(addr, val);
  18399. + } else {
  18400. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  18401. + otg_dev->os_dep.reg_offset);
  18402. + }
  18403. + return count;
  18404. +}
  18405. +
  18406. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  18407. +
  18408. +/*
  18409. + * Attributes
  18410. + */
  18411. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  18412. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  18413. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  18414. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  18415. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  18416. +
  18417. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  18418. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  18419. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  18420. +
  18421. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  18422. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  18423. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  18424. + "GUSBCFG");
  18425. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  18426. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  18427. + "GRXFSIZ");
  18428. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  18429. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  18430. + "GNPTXFSIZ");
  18431. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  18432. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  18433. + "GPVNDCTL");
  18434. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  18435. + &(otg_dev->core_if->core_global_regs->ggpio),
  18436. + "GGPIO");
  18437. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  18438. + "GUID");
  18439. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  18440. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  18441. + "GSNPSID");
  18442. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  18443. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  18444. +
  18445. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  18446. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  18447. + "HPTXFSIZ");
  18448. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  18449. +
  18450. +/**
  18451. + * @todo Add code to initiate the HNP.
  18452. + */
  18453. +/**
  18454. + * Show the HNP status bit
  18455. + */
  18456. +static ssize_t hnp_show(struct device *_dev,
  18457. + struct device_attribute *attr, char *buf)
  18458. +{
  18459. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18460. + return sprintf(buf, "HstNegScs = 0x%x\n",
  18461. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  18462. +}
  18463. +
  18464. +/**
  18465. + * Set the HNP Request bit
  18466. + */
  18467. +static ssize_t hnp_store(struct device *_dev,
  18468. + struct device_attribute *attr,
  18469. + const char *buf, size_t count)
  18470. +{
  18471. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18472. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18473. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  18474. + return count;
  18475. +}
  18476. +
  18477. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  18478. +
  18479. +/**
  18480. + * @todo Add code to initiate the SRP.
  18481. + */
  18482. +/**
  18483. + * Show the SRP status bit
  18484. + */
  18485. +static ssize_t srp_show(struct device *_dev,
  18486. + struct device_attribute *attr, char *buf)
  18487. +{
  18488. +#ifndef DWC_HOST_ONLY
  18489. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18490. + return sprintf(buf, "SesReqScs = 0x%x\n",
  18491. + dwc_otg_get_srpstatus(otg_dev->core_if));
  18492. +#else
  18493. + return sprintf(buf, "Host Only Mode!\n");
  18494. +#endif
  18495. +}
  18496. +
  18497. +/**
  18498. + * Set the SRP Request bit
  18499. + */
  18500. +static ssize_t srp_store(struct device *_dev,
  18501. + struct device_attribute *attr,
  18502. + const char *buf, size_t count)
  18503. +{
  18504. +#ifndef DWC_HOST_ONLY
  18505. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18506. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  18507. +#endif
  18508. + return count;
  18509. +}
  18510. +
  18511. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  18512. +
  18513. +/**
  18514. + * @todo Need to do more for power on/off?
  18515. + */
  18516. +/**
  18517. + * Show the Bus Power status
  18518. + */
  18519. +static ssize_t buspower_show(struct device *_dev,
  18520. + struct device_attribute *attr, char *buf)
  18521. +{
  18522. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18523. + return sprintf(buf, "Bus Power = 0x%x\n",
  18524. + dwc_otg_get_prtpower(otg_dev->core_if));
  18525. +}
  18526. +
  18527. +/**
  18528. + * Set the Bus Power status
  18529. + */
  18530. +static ssize_t buspower_store(struct device *_dev,
  18531. + struct device_attribute *attr,
  18532. + const char *buf, size_t count)
  18533. +{
  18534. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18535. + uint32_t on = simple_strtoul(buf, NULL, 16);
  18536. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  18537. + return count;
  18538. +}
  18539. +
  18540. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  18541. +
  18542. +/**
  18543. + * @todo Need to do more for suspend?
  18544. + */
  18545. +/**
  18546. + * Show the Bus Suspend status
  18547. + */
  18548. +static ssize_t bussuspend_show(struct device *_dev,
  18549. + struct device_attribute *attr, char *buf)
  18550. +{
  18551. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18552. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  18553. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  18554. +}
  18555. +
  18556. +/**
  18557. + * Set the Bus Suspend status
  18558. + */
  18559. +static ssize_t bussuspend_store(struct device *_dev,
  18560. + struct device_attribute *attr,
  18561. + const char *buf, size_t count)
  18562. +{
  18563. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18564. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18565. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  18566. + return count;
  18567. +}
  18568. +
  18569. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  18570. +
  18571. +/**
  18572. + * Show the Mode Change Ready Timer status
  18573. + */
  18574. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  18575. + struct device_attribute *attr, char *buf)
  18576. +{
  18577. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18578. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  18579. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  18580. +}
  18581. +
  18582. +/**
  18583. + * Set the Mode Change Ready Timer status
  18584. + */
  18585. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  18586. + struct device_attribute *attr,
  18587. + const char *buf, size_t count)
  18588. +{
  18589. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18590. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18591. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  18592. + return count;
  18593. +}
  18594. +
  18595. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  18596. +
  18597. +/**
  18598. + * Show the value of HFIR Frame Interval bitfield
  18599. + */
  18600. +static ssize_t fr_interval_show(struct device *_dev,
  18601. + struct device_attribute *attr, char *buf)
  18602. +{
  18603. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18604. + return sprintf(buf, "Frame Interval = 0x%x\n",
  18605. + dwc_otg_get_fr_interval(otg_dev->core_if));
  18606. +}
  18607. +
  18608. +/**
  18609. + * Set the HFIR Frame Interval value
  18610. + */
  18611. +static ssize_t fr_interval_store(struct device *_dev,
  18612. + struct device_attribute *attr,
  18613. + const char *buf, size_t count)
  18614. +{
  18615. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18616. + uint32_t in = simple_strtoul(buf, NULL, 10);
  18617. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  18618. + return count;
  18619. +}
  18620. +
  18621. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  18622. +
  18623. +/**
  18624. + * Show the status of Remote Wakeup.
  18625. + */
  18626. +static ssize_t remote_wakeup_show(struct device *_dev,
  18627. + struct device_attribute *attr, char *buf)
  18628. +{
  18629. +#ifndef DWC_HOST_ONLY
  18630. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18631. +
  18632. + return sprintf(buf,
  18633. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  18634. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  18635. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  18636. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  18637. +#else
  18638. + return sprintf(buf, "Host Only Mode!\n");
  18639. +#endif /* DWC_HOST_ONLY */
  18640. +}
  18641. +
  18642. +/**
  18643. + * Initiate a remote wakeup of the host. The Device control register
  18644. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  18645. + * flag is set.
  18646. + *
  18647. + */
  18648. +static ssize_t remote_wakeup_store(struct device *_dev,
  18649. + struct device_attribute *attr,
  18650. + const char *buf, size_t count)
  18651. +{
  18652. +#ifndef DWC_HOST_ONLY
  18653. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18654. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18655. +
  18656. + if (val & 1) {
  18657. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  18658. + } else {
  18659. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  18660. + }
  18661. +#endif /* DWC_HOST_ONLY */
  18662. + return count;
  18663. +}
  18664. +
  18665. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  18666. + remote_wakeup_store);
  18667. +
  18668. +/**
  18669. + * Show the whether core is hibernated or not.
  18670. + */
  18671. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  18672. + struct device_attribute *attr, char *buf)
  18673. +{
  18674. +#ifndef DWC_HOST_ONLY
  18675. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18676. +
  18677. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  18678. + DWC_PRINTF("Core is in hibernation\n");
  18679. + } else {
  18680. + DWC_PRINTF("Core is not in hibernation\n");
  18681. + }
  18682. +#endif /* DWC_HOST_ONLY */
  18683. + return 0;
  18684. +}
  18685. +
  18686. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  18687. + int rem_wakeup, int reset);
  18688. +
  18689. +/**
  18690. + * Initiate a remote wakeup of the device to exit from hibernation.
  18691. + */
  18692. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  18693. + struct device_attribute *attr,
  18694. + const char *buf, size_t count)
  18695. +{
  18696. +#ifndef DWC_HOST_ONLY
  18697. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18698. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  18699. +#endif
  18700. + return count;
  18701. +}
  18702. +
  18703. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  18704. + rem_wakeup_pwrdn_store);
  18705. +
  18706. +static ssize_t disconnect_us(struct device *_dev,
  18707. + struct device_attribute *attr,
  18708. + const char *buf, size_t count)
  18709. +{
  18710. +
  18711. +#ifndef DWC_HOST_ONLY
  18712. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18713. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18714. + DWC_PRINTF("The Passed value is %04x\n", val);
  18715. +
  18716. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  18717. +
  18718. +#endif /* DWC_HOST_ONLY */
  18719. + return count;
  18720. +}
  18721. +
  18722. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  18723. +
  18724. +/**
  18725. + * Dump global registers and either host or device registers (depending on the
  18726. + * current mode of the core).
  18727. + */
  18728. +static ssize_t regdump_show(struct device *_dev,
  18729. + struct device_attribute *attr, char *buf)
  18730. +{
  18731. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18732. +
  18733. + dwc_otg_dump_global_registers(otg_dev->core_if);
  18734. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  18735. + dwc_otg_dump_host_registers(otg_dev->core_if);
  18736. + } else {
  18737. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  18738. +
  18739. + }
  18740. + return sprintf(buf, "Register Dump\n");
  18741. +}
  18742. +
  18743. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  18744. +
  18745. +/**
  18746. + * Dump global registers and either host or device registers (depending on the
  18747. + * current mode of the core).
  18748. + */
  18749. +static ssize_t spramdump_show(struct device *_dev,
  18750. + struct device_attribute *attr, char *buf)
  18751. +{
  18752. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18753. +
  18754. + //dwc_otg_dump_spram(otg_dev->core_if);
  18755. +
  18756. + return sprintf(buf, "SPRAM Dump\n");
  18757. +}
  18758. +
  18759. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  18760. +
  18761. +/**
  18762. + * Dump the current hcd state.
  18763. + */
  18764. +static ssize_t hcddump_show(struct device *_dev,
  18765. + struct device_attribute *attr, char *buf)
  18766. +{
  18767. +#ifndef DWC_DEVICE_ONLY
  18768. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18769. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  18770. +#endif /* DWC_DEVICE_ONLY */
  18771. + return sprintf(buf, "HCD Dump\n");
  18772. +}
  18773. +
  18774. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  18775. +
  18776. +/**
  18777. + * Dump the average frame remaining at SOF. This can be used to
  18778. + * determine average interrupt latency. Frame remaining is also shown for
  18779. + * start transfer and two additional sample points.
  18780. + */
  18781. +static ssize_t hcd_frrem_show(struct device *_dev,
  18782. + struct device_attribute *attr, char *buf)
  18783. +{
  18784. +#ifndef DWC_DEVICE_ONLY
  18785. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18786. +
  18787. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  18788. +#endif /* DWC_DEVICE_ONLY */
  18789. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  18790. +}
  18791. +
  18792. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  18793. +
  18794. +/**
  18795. + * Displays the time required to read the GNPTXFSIZ register many times (the
  18796. + * output shows the number of times the register is read).
  18797. + */
  18798. +#define RW_REG_COUNT 10000000
  18799. +#define MSEC_PER_JIFFIE 1000/HZ
  18800. +static ssize_t rd_reg_test_show(struct device *_dev,
  18801. + struct device_attribute *attr, char *buf)
  18802. +{
  18803. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18804. + int i;
  18805. + int time;
  18806. + int start_jiffies;
  18807. +
  18808. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  18809. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  18810. + start_jiffies = jiffies;
  18811. + for (i = 0; i < RW_REG_COUNT; i++) {
  18812. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  18813. + }
  18814. + time = jiffies - start_jiffies;
  18815. + return sprintf(buf,
  18816. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  18817. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  18818. +}
  18819. +
  18820. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  18821. +
  18822. +/**
  18823. + * Displays the time required to write the GNPTXFSIZ register many times (the
  18824. + * output shows the number of times the register is written).
  18825. + */
  18826. +static ssize_t wr_reg_test_show(struct device *_dev,
  18827. + struct device_attribute *attr, char *buf)
  18828. +{
  18829. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18830. + uint32_t reg_val;
  18831. + int i;
  18832. + int time;
  18833. + int start_jiffies;
  18834. +
  18835. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  18836. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  18837. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  18838. + start_jiffies = jiffies;
  18839. + for (i = 0; i < RW_REG_COUNT; i++) {
  18840. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  18841. + }
  18842. + time = jiffies - start_jiffies;
  18843. + return sprintf(buf,
  18844. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  18845. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  18846. +}
  18847. +
  18848. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  18849. +
  18850. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18851. +
  18852. +/**
  18853. +* Show the lpm_response attribute.
  18854. +*/
  18855. +static ssize_t lpmresp_show(struct device *_dev,
  18856. + struct device_attribute *attr, char *buf)
  18857. +{
  18858. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18859. +
  18860. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  18861. + return sprintf(buf, "** LPM is DISABLED **\n");
  18862. +
  18863. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  18864. + return sprintf(buf, "** Current mode is not device mode\n");
  18865. + }
  18866. + return sprintf(buf, "lpm_response = %d\n",
  18867. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  18868. +}
  18869. +
  18870. +/**
  18871. +* Store the lpm_response attribute.
  18872. +*/
  18873. +static ssize_t lpmresp_store(struct device *_dev,
  18874. + struct device_attribute *attr,
  18875. + const char *buf, size_t count)
  18876. +{
  18877. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18878. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18879. +
  18880. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  18881. + return 0;
  18882. + }
  18883. +
  18884. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  18885. + return 0;
  18886. + }
  18887. +
  18888. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  18889. + return count;
  18890. +}
  18891. +
  18892. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  18893. +
  18894. +/**
  18895. +* Show the sleep_status attribute.
  18896. +*/
  18897. +static ssize_t sleepstatus_show(struct device *_dev,
  18898. + struct device_attribute *attr, char *buf)
  18899. +{
  18900. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18901. + return sprintf(buf, "Sleep Status = %d\n",
  18902. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  18903. +}
  18904. +
  18905. +/**
  18906. + * Store the sleep_status attribure.
  18907. + */
  18908. +static ssize_t sleepstatus_store(struct device *_dev,
  18909. + struct device_attribute *attr,
  18910. + const char *buf, size_t count)
  18911. +{
  18912. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18913. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  18914. +
  18915. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  18916. + if (dwc_otg_is_host_mode(core_if)) {
  18917. +
  18918. + DWC_PRINTF("Host initiated resume\n");
  18919. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  18920. + }
  18921. + }
  18922. +
  18923. + return count;
  18924. +}
  18925. +
  18926. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  18927. + sleepstatus_store);
  18928. +
  18929. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  18930. +
  18931. +/**@}*/
  18932. +
  18933. +/**
  18934. + * Create the device files
  18935. + */
  18936. +void dwc_otg_attr_create(
  18937. +#ifdef LM_INTERFACE
  18938. + struct lm_device *dev
  18939. +#elif defined(PCI_INTERFACE)
  18940. + struct pci_dev *dev
  18941. +#elif defined(PLATFORM_INTERFACE)
  18942. + struct platform_device *dev
  18943. +#endif
  18944. + )
  18945. +{
  18946. + int error;
  18947. +
  18948. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  18949. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  18950. + error = device_create_file(&dev->dev, &dev_attr_mode);
  18951. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  18952. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  18953. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  18954. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  18955. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  18956. + error = device_create_file(&dev->dev, &dev_attr_srp);
  18957. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  18958. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  18959. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  18960. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  18961. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  18962. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  18963. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  18964. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  18965. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  18966. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  18967. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  18968. + error = device_create_file(&dev->dev, &dev_attr_guid);
  18969. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  18970. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  18971. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  18972. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  18973. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  18974. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  18975. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  18976. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  18977. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  18978. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  18979. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  18980. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  18981. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  18982. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  18983. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18984. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  18985. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  18986. +#endif
  18987. +}
  18988. +
  18989. +/**
  18990. + * Remove the device files
  18991. + */
  18992. +void dwc_otg_attr_remove(
  18993. +#ifdef LM_INTERFACE
  18994. + struct lm_device *dev
  18995. +#elif defined(PCI_INTERFACE)
  18996. + struct pci_dev *dev
  18997. +#elif defined(PLATFORM_INTERFACE)
  18998. + struct platform_device *dev
  18999. +#endif
  19000. + )
  19001. +{
  19002. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  19003. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  19004. + device_remove_file(&dev->dev, &dev_attr_mode);
  19005. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  19006. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  19007. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  19008. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  19009. + device_remove_file(&dev->dev, &dev_attr_hnp);
  19010. + device_remove_file(&dev->dev, &dev_attr_srp);
  19011. + device_remove_file(&dev->dev, &dev_attr_buspower);
  19012. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  19013. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  19014. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  19015. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  19016. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  19017. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  19018. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  19019. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  19020. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  19021. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  19022. + device_remove_file(&dev->dev, &dev_attr_guid);
  19023. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  19024. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  19025. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  19026. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  19027. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  19028. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  19029. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  19030. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  19031. + device_remove_file(&dev->dev, &dev_attr_regdump);
  19032. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  19033. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  19034. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  19035. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  19036. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  19037. +#ifdef CONFIG_USB_DWC_OTG_LPM
  19038. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  19039. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  19040. +#endif
  19041. +}
  19042. --- /dev/null
  19043. +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  19044. @@ -0,0 +1,89 @@
  19045. +/* ==========================================================================
  19046. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  19047. + * $Revision: #13 $
  19048. + * $Date: 2010/06/21 $
  19049. + * $Change: 1532021 $
  19050. + *
  19051. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  19052. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  19053. + * otherwise expressly agreed to in writing between Synopsys and you.
  19054. + *
  19055. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  19056. + * any End User Software License Agreement or Agreement for Licensed Product
  19057. + * with Synopsys or any supplement thereto. You are permitted to use and
  19058. + * redistribute this Software in source and binary forms, with or without
  19059. + * modification, provided that redistributions of source code must retain this
  19060. + * notice. You may not view, use, disclose, copy or distribute this file or
  19061. + * any information contained herein except pursuant to this license grant from
  19062. + * Synopsys. If you do not agree with this notice, including the disclaimer
  19063. + * below, then you are not authorized to use the Software.
  19064. + *
  19065. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  19066. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19067. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19068. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  19069. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19070. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19071. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19072. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19073. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19074. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  19075. + * DAMAGE.
  19076. + * ========================================================================== */
  19077. +
  19078. +#if !defined(__DWC_OTG_ATTR_H__)
  19079. +#define __DWC_OTG_ATTR_H__
  19080. +
  19081. +/** @file
  19082. + * This file contains the interface to the Linux device attributes.
  19083. + */
  19084. +extern struct device_attribute dev_attr_regoffset;
  19085. +extern struct device_attribute dev_attr_regvalue;
  19086. +
  19087. +extern struct device_attribute dev_attr_mode;
  19088. +extern struct device_attribute dev_attr_hnpcapable;
  19089. +extern struct device_attribute dev_attr_srpcapable;
  19090. +extern struct device_attribute dev_attr_hnp;
  19091. +extern struct device_attribute dev_attr_srp;
  19092. +extern struct device_attribute dev_attr_buspower;
  19093. +extern struct device_attribute dev_attr_bussuspend;
  19094. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  19095. +extern struct device_attribute dev_attr_fr_interval;
  19096. +extern struct device_attribute dev_attr_busconnected;
  19097. +extern struct device_attribute dev_attr_gotgctl;
  19098. +extern struct device_attribute dev_attr_gusbcfg;
  19099. +extern struct device_attribute dev_attr_grxfsiz;
  19100. +extern struct device_attribute dev_attr_gnptxfsiz;
  19101. +extern struct device_attribute dev_attr_gpvndctl;
  19102. +extern struct device_attribute dev_attr_ggpio;
  19103. +extern struct device_attribute dev_attr_guid;
  19104. +extern struct device_attribute dev_attr_gsnpsid;
  19105. +extern struct device_attribute dev_attr_devspeed;
  19106. +extern struct device_attribute dev_attr_enumspeed;
  19107. +extern struct device_attribute dev_attr_hptxfsiz;
  19108. +extern struct device_attribute dev_attr_hprt0;
  19109. +#ifdef CONFIG_USB_DWC_OTG_LPM
  19110. +extern struct device_attribute dev_attr_lpm_response;
  19111. +extern struct device_attribute devi_attr_sleep_status;
  19112. +#endif
  19113. +
  19114. +void dwc_otg_attr_create(
  19115. +#ifdef LM_INTERFACE
  19116. + struct lm_device *dev
  19117. +#elif defined(PCI_INTERFACE)
  19118. + struct pci_dev *dev
  19119. +#elif defined(PLATFORM_INTERFACE)
  19120. + struct platform_device *dev
  19121. +#endif
  19122. + );
  19123. +
  19124. +void dwc_otg_attr_remove(
  19125. +#ifdef LM_INTERFACE
  19126. + struct lm_device *dev
  19127. +#elif defined(PCI_INTERFACE)
  19128. + struct pci_dev *dev
  19129. +#elif defined(PLATFORM_INTERFACE)
  19130. + struct platform_device *dev
  19131. +#endif
  19132. + );
  19133. +#endif
  19134. --- /dev/null
  19135. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  19136. @@ -0,0 +1,1876 @@
  19137. +/* ==========================================================================
  19138. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  19139. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  19140. + * otherwise expressly agreed to in writing between Synopsys and you.
  19141. + *
  19142. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  19143. + * any End User Software License Agreement or Agreement for Licensed Product
  19144. + * with Synopsys or any supplement thereto. You are permitted to use and
  19145. + * redistribute this Software in source and binary forms, with or without
  19146. + * modification, provided that redistributions of source code must retain this
  19147. + * notice. You may not view, use, disclose, copy or distribute this file or
  19148. + * any information contained herein except pursuant to this license grant from
  19149. + * Synopsys. If you do not agree with this notice, including the disclaimer
  19150. + * below, then you are not authorized to use the Software.
  19151. + *
  19152. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  19153. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19154. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19155. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  19156. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19157. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19158. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19159. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19160. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19161. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  19162. + * DAMAGE.
  19163. + * ========================================================================== */
  19164. +
  19165. +/** @file
  19166. + *
  19167. + * This file contains the most of the CFI(Core Feature Interface)
  19168. + * implementation for the OTG.
  19169. + */
  19170. +
  19171. +#ifdef DWC_UTE_CFI
  19172. +
  19173. +#include "dwc_otg_pcd.h"
  19174. +#include "dwc_otg_cfi.h"
  19175. +
  19176. +/** This definition should actually migrate to the Portability Library */
  19177. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  19178. +
  19179. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  19180. +
  19181. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  19182. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  19183. + struct dwc_otg_pcd *pcd,
  19184. + struct cfi_usb_ctrlrequest *ctrl_req);
  19185. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  19186. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19187. + struct cfi_usb_ctrlrequest *req);
  19188. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19189. + struct cfi_usb_ctrlrequest *req);
  19190. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19191. + struct cfi_usb_ctrlrequest *req);
  19192. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  19193. + struct cfi_usb_ctrlrequest *req);
  19194. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  19195. +
  19196. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  19197. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  19198. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  19199. +
  19200. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  19201. +
  19202. +/** This is the header of the all features descriptor */
  19203. +static cfi_all_features_header_t all_props_desc_header = {
  19204. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  19205. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  19206. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  19207. +};
  19208. +
  19209. +/** This is an array of statically allocated feature descriptors */
  19210. +static cfi_feature_desc_header_t prop_descs[] = {
  19211. +
  19212. + /* FT_ID_DMA_MODE */
  19213. + {
  19214. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  19215. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19216. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  19217. + },
  19218. +
  19219. + /* FT_ID_DMA_BUFFER_SETUP */
  19220. + {
  19221. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  19222. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19223. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19224. + },
  19225. +
  19226. + /* FT_ID_DMA_BUFF_ALIGN */
  19227. + {
  19228. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  19229. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19230. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19231. + },
  19232. +
  19233. + /* FT_ID_DMA_CONCAT_SETUP */
  19234. + {
  19235. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  19236. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19237. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19238. + },
  19239. +
  19240. + /* FT_ID_DMA_CIRCULAR */
  19241. + {
  19242. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  19243. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19244. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19245. + },
  19246. +
  19247. + /* FT_ID_THRESHOLD_SETUP */
  19248. + {
  19249. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  19250. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19251. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19252. + },
  19253. +
  19254. + /* FT_ID_DFIFO_DEPTH */
  19255. + {
  19256. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  19257. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  19258. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19259. + },
  19260. +
  19261. + /* FT_ID_TX_FIFO_DEPTH */
  19262. + {
  19263. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  19264. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19265. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19266. + },
  19267. +
  19268. + /* FT_ID_RX_FIFO_DEPTH */
  19269. + {
  19270. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  19271. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19272. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19273. + }
  19274. +};
  19275. +
  19276. +/** The table of feature names */
  19277. +cfi_string_t prop_name_table[] = {
  19278. + {FT_ID_DMA_MODE, "dma_mode"},
  19279. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  19280. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  19281. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  19282. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  19283. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  19284. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  19285. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  19286. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  19287. + {}
  19288. +};
  19289. +
  19290. +/************************************************************************/
  19291. +
  19292. +/**
  19293. + * Returns the name of the feature by its ID
  19294. + * or NULL if no featute ID matches.
  19295. + *
  19296. + */
  19297. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  19298. +{
  19299. + cfi_string_t *pstr;
  19300. + *len = 0;
  19301. +
  19302. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  19303. + if (pstr->id == prop_id) {
  19304. + *len = DWC_STRLEN(pstr->s);
  19305. + return pstr->s;
  19306. + }
  19307. + }
  19308. + return NULL;
  19309. +}
  19310. +
  19311. +/**
  19312. + * This function handles all CFI specific control requests.
  19313. + *
  19314. + * Return a negative value to stall the DCE.
  19315. + */
  19316. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  19317. +{
  19318. + int retval = 0;
  19319. + dwc_otg_pcd_ep_t *ep = NULL;
  19320. + cfiobject_t *cfi = pcd->cfi;
  19321. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  19322. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  19323. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  19324. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  19325. + uint32_t regaddr = 0;
  19326. + uint32_t regval = 0;
  19327. +
  19328. + /* Save this Control Request in the CFI object.
  19329. + * The data field will be assigned in the data stage completion CB function.
  19330. + */
  19331. + cfi->ctrl_req = *ctrl;
  19332. + cfi->ctrl_req.data = NULL;
  19333. +
  19334. + cfi->need_gadget_att = 0;
  19335. + cfi->need_status_in_complete = 0;
  19336. +
  19337. + switch (ctrl->bRequest) {
  19338. + case VEN_CORE_GET_FEATURES:
  19339. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  19340. + if (retval >= 0) {
  19341. + //dump_msg(cfi->buf_in.buf, retval);
  19342. + ep = &pcd->ep0;
  19343. +
  19344. + retval = min((uint16_t) retval, wLen);
  19345. + /* Transfer this buffer to the host through the EP0-IN EP */
  19346. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19347. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19348. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19349. + ep->dwc_ep.xfer_len = retval;
  19350. + ep->dwc_ep.xfer_count = 0;
  19351. + ep->dwc_ep.sent_zlp = 0;
  19352. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19353. +
  19354. + pcd->ep0_pending = 1;
  19355. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19356. + }
  19357. + retval = 0;
  19358. + break;
  19359. +
  19360. + case VEN_CORE_GET_FEATURE:
  19361. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  19362. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  19363. + pcd, ctrl);
  19364. + if (retval >= 0) {
  19365. + ep = &pcd->ep0;
  19366. +
  19367. + retval = min((uint16_t) retval, wLen);
  19368. + /* Transfer this buffer to the host through the EP0-IN EP */
  19369. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19370. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19371. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19372. + ep->dwc_ep.xfer_len = retval;
  19373. + ep->dwc_ep.xfer_count = 0;
  19374. + ep->dwc_ep.sent_zlp = 0;
  19375. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19376. +
  19377. + pcd->ep0_pending = 1;
  19378. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19379. + }
  19380. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  19381. + dump_msg(cfi->buf_in.buf, retval);
  19382. + break;
  19383. +
  19384. + case VEN_CORE_SET_FEATURE:
  19385. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  19386. + /* Set up an XFER to get the data stage of the control request,
  19387. + * which is the new value of the feature to be modified.
  19388. + */
  19389. + ep = &pcd->ep0;
  19390. + ep->dwc_ep.is_in = 0;
  19391. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  19392. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  19393. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  19394. + ep->dwc_ep.xfer_len = wLen;
  19395. + ep->dwc_ep.xfer_count = 0;
  19396. + ep->dwc_ep.sent_zlp = 0;
  19397. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19398. +
  19399. + pcd->ep0_pending = 1;
  19400. + /* Read the control write's data stage */
  19401. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19402. + retval = 0;
  19403. + break;
  19404. +
  19405. + case VEN_CORE_RESET_FEATURES:
  19406. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  19407. + cfi->need_gadget_att = 1;
  19408. + cfi->need_status_in_complete = 1;
  19409. + retval = cfi_preproc_reset(pcd, ctrl);
  19410. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  19411. + break;
  19412. +
  19413. + case VEN_CORE_ACTIVATE_FEATURES:
  19414. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  19415. + break;
  19416. +
  19417. + case VEN_CORE_READ_REGISTER:
  19418. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  19419. + /* wValue optionally contains the HI WORD of the register offset and
  19420. + * wIndex contains the LOW WORD of the register offset
  19421. + */
  19422. + if (wValue == 0) {
  19423. + /* @TODO - MAS - fix the access to the base field */
  19424. + regaddr = 0;
  19425. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  19426. + //GET_CORE_IF(pcd)->co
  19427. + regaddr |= wIndex;
  19428. + } else {
  19429. + regaddr = (wValue << 16) | wIndex;
  19430. + }
  19431. +
  19432. + /* Read a 32-bit value of the memory at the regaddr */
  19433. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  19434. +
  19435. + ep = &pcd->ep0;
  19436. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  19437. + ep->dwc_ep.is_in = 1;
  19438. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19439. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19440. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19441. + ep->dwc_ep.xfer_len = wLen;
  19442. + ep->dwc_ep.xfer_count = 0;
  19443. + ep->dwc_ep.sent_zlp = 0;
  19444. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19445. +
  19446. + pcd->ep0_pending = 1;
  19447. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19448. + cfi->need_gadget_att = 0;
  19449. + retval = 0;
  19450. + break;
  19451. +
  19452. + case VEN_CORE_WRITE_REGISTER:
  19453. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  19454. + /* Set up an XFER to get the data stage of the control request,
  19455. + * which is the new value of the register to be modified.
  19456. + */
  19457. + ep = &pcd->ep0;
  19458. + ep->dwc_ep.is_in = 0;
  19459. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  19460. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  19461. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  19462. + ep->dwc_ep.xfer_len = wLen;
  19463. + ep->dwc_ep.xfer_count = 0;
  19464. + ep->dwc_ep.sent_zlp = 0;
  19465. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19466. +
  19467. + pcd->ep0_pending = 1;
  19468. + /* Read the control write's data stage */
  19469. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19470. + retval = 0;
  19471. + break;
  19472. +
  19473. + default:
  19474. + retval = -DWC_E_NOT_SUPPORTED;
  19475. + break;
  19476. + }
  19477. +
  19478. + return retval;
  19479. +}
  19480. +
  19481. +/**
  19482. + * This function prepares the core features descriptors and copies its
  19483. + * raw representation into the buffer <buf>.
  19484. + *
  19485. + * The buffer structure is as follows:
  19486. + * all_features_header (8 bytes)
  19487. + * features_#1 (8 bytes + feature name string length)
  19488. + * features_#2 (8 bytes + feature name string length)
  19489. + * .....
  19490. + * features_#n - where n=the total count of feature descriptors
  19491. + */
  19492. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  19493. +{
  19494. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  19495. + cfi_feature_desc_header_t *prop;
  19496. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  19497. + cfi_all_features_header_t *tmp;
  19498. + uint8_t *tmpbuf = buf;
  19499. + const uint8_t *pname = NULL;
  19500. + int i, j, namelen = 0, totlen;
  19501. +
  19502. + /* Prepare and copy the core features into the buffer */
  19503. + CFI_INFO("%s:\n", __func__);
  19504. +
  19505. + tmp = (cfi_all_features_header_t *) tmpbuf;
  19506. + *tmp = *all_props_hdr;
  19507. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  19508. +
  19509. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  19510. + for (i = 0; i < j; i++, prop_hdr++) {
  19511. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  19512. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  19513. + *prop = *prop_hdr;
  19514. +
  19515. + prop->bNameLen = namelen;
  19516. + prop->wLength =
  19517. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  19518. + namelen);
  19519. +
  19520. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  19521. + dwc_memcpy(tmpbuf, pname, namelen);
  19522. + tmpbuf += namelen;
  19523. + }
  19524. +
  19525. + totlen = tmpbuf - buf;
  19526. +
  19527. + if (totlen > 0) {
  19528. + tmp = (cfi_all_features_header_t *) buf;
  19529. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  19530. + }
  19531. +
  19532. + return totlen;
  19533. +}
  19534. +
  19535. +/**
  19536. + * This function releases all the dynamic memory in the CFI object.
  19537. + */
  19538. +static void cfi_release(cfiobject_t * cfiobj)
  19539. +{
  19540. + cfi_ep_t *cfiep;
  19541. + dwc_list_link_t *tmp;
  19542. +
  19543. + CFI_INFO("%s\n", __func__);
  19544. +
  19545. + if (cfiobj->buf_in.buf) {
  19546. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  19547. + cfiobj->buf_in.addr);
  19548. + cfiobj->buf_in.buf = NULL;
  19549. + }
  19550. +
  19551. + if (cfiobj->buf_out.buf) {
  19552. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  19553. + cfiobj->buf_out.addr);
  19554. + cfiobj->buf_out.buf = NULL;
  19555. + }
  19556. +
  19557. + /* Free the Buffer Setup values for each EP */
  19558. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  19559. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  19560. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  19561. + cfi_free_ep_bs_dyn_data(cfiep);
  19562. + }
  19563. +}
  19564. +
  19565. +/**
  19566. + * This function frees the dynamically allocated EP buffer setup data.
  19567. + */
  19568. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  19569. +{
  19570. + if (cfiep->bm_sg) {
  19571. + DWC_FREE(cfiep->bm_sg);
  19572. + cfiep->bm_sg = NULL;
  19573. + }
  19574. +
  19575. + if (cfiep->bm_align) {
  19576. + DWC_FREE(cfiep->bm_align);
  19577. + cfiep->bm_align = NULL;
  19578. + }
  19579. +
  19580. + if (cfiep->bm_concat) {
  19581. + if (NULL != cfiep->bm_concat->wTxBytes) {
  19582. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  19583. + cfiep->bm_concat->wTxBytes = NULL;
  19584. + }
  19585. + DWC_FREE(cfiep->bm_concat);
  19586. + cfiep->bm_concat = NULL;
  19587. + }
  19588. +}
  19589. +
  19590. +/**
  19591. + * This function initializes the default values of the features
  19592. + * for a specific endpoint and should be called only once when
  19593. + * the EP is enabled first time.
  19594. + */
  19595. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  19596. +{
  19597. + int retval = 0;
  19598. +
  19599. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  19600. + if (NULL == cfiep->bm_sg) {
  19601. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  19602. + return -DWC_E_NO_MEMORY;
  19603. + }
  19604. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  19605. +
  19606. + /* For the Concatenation feature's default value we do not allocate
  19607. + * memory for the wTxBytes field - it will be done in the set_feature_value
  19608. + * request handler.
  19609. + */
  19610. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  19611. + if (NULL == cfiep->bm_concat) {
  19612. + CFI_INFO
  19613. + ("Failed to allocate memory for CONCATENATION feature value\n");
  19614. + DWC_FREE(cfiep->bm_sg);
  19615. + return -DWC_E_NO_MEMORY;
  19616. + }
  19617. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  19618. +
  19619. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  19620. + if (NULL == cfiep->bm_align) {
  19621. + CFI_INFO
  19622. + ("Failed to allocate memory for Alignment feature value\n");
  19623. + DWC_FREE(cfiep->bm_sg);
  19624. + DWC_FREE(cfiep->bm_concat);
  19625. + return -DWC_E_NO_MEMORY;
  19626. + }
  19627. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  19628. +
  19629. + return retval;
  19630. +}
  19631. +
  19632. +/**
  19633. + * The callback function that notifies the CFI on the activation of
  19634. + * an endpoint in the PCD. The following steps are done in this function:
  19635. + *
  19636. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  19637. + * active endpoint)
  19638. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  19639. + * Set the Buffer Mode to standard
  19640. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  19641. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  19642. + */
  19643. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  19644. + struct dwc_otg_pcd_ep *ep)
  19645. +{
  19646. + cfi_ep_t *cfiep;
  19647. + int retval = -DWC_E_NOT_SUPPORTED;
  19648. +
  19649. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  19650. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  19651. + /* MAS - Check whether this endpoint already is in the list */
  19652. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  19653. +
  19654. + if (NULL == cfiep) {
  19655. + /* Allocate a cfi_ep_t object */
  19656. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  19657. + if (NULL == cfiep) {
  19658. + CFI_INFO
  19659. + ("Unable to allocate memory for <cfiep> in function %s\n",
  19660. + __func__);
  19661. + return -DWC_E_NO_MEMORY;
  19662. + }
  19663. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  19664. +
  19665. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  19666. + cfiep->ep = ep;
  19667. +
  19668. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  19669. + ep->dwc_ep.descs =
  19670. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  19671. + sizeof(dwc_otg_dma_desc_t),
  19672. + &ep->dwc_ep.descs_dma_addr);
  19673. +
  19674. + if (NULL == ep->dwc_ep.descs) {
  19675. + DWC_FREE(cfiep);
  19676. + return -DWC_E_NO_MEMORY;
  19677. + }
  19678. +
  19679. + DWC_LIST_INIT(&cfiep->lh);
  19680. +
  19681. + /* Set the buffer mode to BM_STANDARD. It will be modified
  19682. + * when building descriptors for a specific buffer mode */
  19683. + ep->dwc_ep.buff_mode = BM_STANDARD;
  19684. +
  19685. + /* Create and initialize the default values for this EP's Buffer modes */
  19686. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  19687. + return retval;
  19688. +
  19689. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  19690. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  19691. + retval = 0;
  19692. + } else { /* The sought EP already is in the list */
  19693. + CFI_INFO("%s: The sought EP already is in the list\n",
  19694. + __func__);
  19695. + }
  19696. +
  19697. + return retval;
  19698. +}
  19699. +
  19700. +/**
  19701. + * This function is called when the data stage of a 3-stage Control Write request
  19702. + * is complete.
  19703. + *
  19704. + */
  19705. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  19706. + struct dwc_otg_pcd *pcd)
  19707. +{
  19708. + uint32_t addr, reg_value;
  19709. + uint16_t wIndex, wValue;
  19710. + uint8_t bRequest;
  19711. + uint8_t *buf = cfi->buf_out.buf;
  19712. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  19713. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  19714. + int retval = -DWC_E_NOT_SUPPORTED;
  19715. +
  19716. + CFI_INFO("%s\n", __func__);
  19717. +
  19718. + bRequest = ctrl_req->bRequest;
  19719. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  19720. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  19721. +
  19722. + /*
  19723. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  19724. + * The request should be already saved in the command stage by now.
  19725. + */
  19726. + ctrl_req->data = cfi->buf_out.buf;
  19727. + cfi->need_status_in_complete = 0;
  19728. + cfi->need_gadget_att = 0;
  19729. +
  19730. + switch (bRequest) {
  19731. + case VEN_CORE_WRITE_REGISTER:
  19732. + /* The buffer contains raw data of the new value for the register */
  19733. + reg_value = *((uint32_t *) buf);
  19734. + if (wValue == 0) {
  19735. + addr = 0;
  19736. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  19737. + addr += wIndex;
  19738. + } else {
  19739. + addr = (wValue << 16) | wIndex;
  19740. + }
  19741. +
  19742. + //writel(reg_value, addr);
  19743. +
  19744. + retval = 0;
  19745. + cfi->need_status_in_complete = 1;
  19746. + break;
  19747. +
  19748. + case VEN_CORE_SET_FEATURE:
  19749. + /* The buffer contains raw data of the new value of the feature */
  19750. + retval = cfi_set_feature_value(pcd);
  19751. + if (retval < 0)
  19752. + return retval;
  19753. +
  19754. + cfi->need_status_in_complete = 1;
  19755. + break;
  19756. +
  19757. + default:
  19758. + break;
  19759. + }
  19760. +
  19761. + return retval;
  19762. +}
  19763. +
  19764. +/**
  19765. + * This function builds the DMA descriptors for the SG buffer mode.
  19766. + */
  19767. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19768. + dwc_otg_pcd_request_t * req)
  19769. +{
  19770. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19771. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  19772. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19773. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  19774. + dma_addr_t buff_addr = req->dma;
  19775. + int i;
  19776. + uint32_t txsize, off;
  19777. +
  19778. + txsize = sgval->wSize;
  19779. + off = sgval->bOffset;
  19780. +
  19781. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  19782. +// __func__, cfiep->ep->ep.name, txsize, off);
  19783. +
  19784. + for (i = 0; i < sgval->bCount; i++) {
  19785. + desc->status.b.bs = BS_HOST_BUSY;
  19786. + desc->buf = buff_addr;
  19787. + desc->status.b.l = 0;
  19788. + desc->status.b.ioc = 0;
  19789. + desc->status.b.sp = 0;
  19790. + desc->status.b.bytes = txsize;
  19791. + desc->status.b.bs = BS_HOST_READY;
  19792. +
  19793. + /* Set the next address of the buffer */
  19794. + buff_addr += txsize + off;
  19795. + desc_last = desc;
  19796. + desc++;
  19797. + }
  19798. +
  19799. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  19800. + desc_last->status.b.l = 1;
  19801. + desc_last->status.b.ioc = 1;
  19802. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  19803. + /* Save the last DMA descriptor pointer */
  19804. + cfiep->dma_desc_last = desc_last;
  19805. + cfiep->desc_count = sgval->bCount;
  19806. +}
  19807. +
  19808. +/**
  19809. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  19810. + */
  19811. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19812. + dwc_otg_pcd_request_t * req)
  19813. +{
  19814. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19815. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  19816. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19817. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  19818. + dma_addr_t buff_addr = req->dma;
  19819. + int i;
  19820. + uint16_t *txsize;
  19821. +
  19822. + txsize = concatval->wTxBytes;
  19823. +
  19824. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  19825. + desc->buf = buff_addr;
  19826. + desc->status.b.bs = BS_HOST_BUSY;
  19827. + desc->status.b.l = 0;
  19828. + desc->status.b.ioc = 0;
  19829. + desc->status.b.sp = 0;
  19830. + desc->status.b.bytes = *txsize;
  19831. + desc->status.b.bs = BS_HOST_READY;
  19832. +
  19833. + txsize++;
  19834. + /* Set the next address of the buffer */
  19835. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  19836. + desc_last = desc;
  19837. + desc++;
  19838. + }
  19839. +
  19840. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  19841. + desc_last->status.b.l = 1;
  19842. + desc_last->status.b.ioc = 1;
  19843. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  19844. + cfiep->dma_desc_last = desc_last;
  19845. + cfiep->desc_count = concatval->hdr.bDescCount;
  19846. +}
  19847. +
  19848. +/**
  19849. + * This function builds the DMA descriptors for the Circular buffer mode
  19850. + */
  19851. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19852. + dwc_otg_pcd_request_t * req)
  19853. +{
  19854. + /* @todo: MAS - add implementation when this feature needs to be tested */
  19855. +}
  19856. +
  19857. +/**
  19858. + * This function builds the DMA descriptors for the Alignment buffer mode
  19859. + */
  19860. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19861. + dwc_otg_pcd_request_t * req)
  19862. +{
  19863. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19864. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  19865. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19866. + dma_addr_t buff_addr = req->dma;
  19867. +
  19868. + desc->status.b.bs = BS_HOST_BUSY;
  19869. + desc->status.b.l = 1;
  19870. + desc->status.b.ioc = 1;
  19871. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  19872. + desc->status.b.bytes = req->length;
  19873. + /* Adjust the buffer alignment */
  19874. + desc->buf = (buff_addr + alignval->bAlign);
  19875. + desc->status.b.bs = BS_HOST_READY;
  19876. + cfiep->dma_desc_last = desc;
  19877. + cfiep->desc_count = 1;
  19878. +}
  19879. +
  19880. +/**
  19881. + * This function builds the DMA descriptors chain for different modes of the
  19882. + * buffer setup of an endpoint.
  19883. + */
  19884. +static void cfi_build_descriptors(struct cfiobject *cfi,
  19885. + struct dwc_otg_pcd *pcd,
  19886. + struct dwc_otg_pcd_ep *ep,
  19887. + dwc_otg_pcd_request_t * req)
  19888. +{
  19889. + cfi_ep_t *cfiep;
  19890. +
  19891. + /* Get the cfiep by the dwc_otg_pcd_ep */
  19892. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  19893. + if (NULL == cfiep) {
  19894. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  19895. + __func__);
  19896. + return;
  19897. + }
  19898. +
  19899. + cfiep->xfer_len = req->length;
  19900. +
  19901. + /* Iterate through all the DMA descriptors */
  19902. + switch (cfiep->ep->dwc_ep.buff_mode) {
  19903. + case BM_SG:
  19904. + cfi_build_sg_descs(cfi, cfiep, req);
  19905. + break;
  19906. +
  19907. + case BM_CONCAT:
  19908. + cfi_build_concat_descs(cfi, cfiep, req);
  19909. + break;
  19910. +
  19911. + case BM_CIRCULAR:
  19912. + cfi_build_circ_descs(cfi, cfiep, req);
  19913. + break;
  19914. +
  19915. + case BM_ALIGN:
  19916. + cfi_build_align_descs(cfi, cfiep, req);
  19917. + break;
  19918. +
  19919. + default:
  19920. + break;
  19921. + }
  19922. +}
  19923. +
  19924. +/**
  19925. + * Allocate DMA buffer for different Buffer modes.
  19926. + */
  19927. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  19928. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  19929. + unsigned size, gfp_t flags)
  19930. +{
  19931. + return DWC_DMA_ALLOC(size, dma);
  19932. +}
  19933. +
  19934. +/**
  19935. + * This function initializes the CFI object.
  19936. + */
  19937. +int init_cfi(cfiobject_t * cfiobj)
  19938. +{
  19939. + CFI_INFO("%s\n", __func__);
  19940. +
  19941. + /* Allocate a buffer for IN XFERs */
  19942. + cfiobj->buf_in.buf =
  19943. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  19944. + if (NULL == cfiobj->buf_in.buf) {
  19945. + CFI_INFO("Unable to allocate buffer for INs\n");
  19946. + return -DWC_E_NO_MEMORY;
  19947. + }
  19948. +
  19949. + /* Allocate a buffer for OUT XFERs */
  19950. + cfiobj->buf_out.buf =
  19951. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  19952. + if (NULL == cfiobj->buf_out.buf) {
  19953. + CFI_INFO("Unable to allocate buffer for OUT\n");
  19954. + return -DWC_E_NO_MEMORY;
  19955. + }
  19956. +
  19957. + /* Initialize the callback function pointers */
  19958. + cfiobj->ops.release = cfi_release;
  19959. + cfiobj->ops.ep_enable = cfi_ep_enable;
  19960. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  19961. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  19962. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  19963. +
  19964. + /* Initialize the list of active endpoints in the CFI object */
  19965. + DWC_LIST_INIT(&cfiobj->active_eps);
  19966. +
  19967. + return 0;
  19968. +}
  19969. +
  19970. +/**
  19971. + * This function reads the required feature's current value into the buffer
  19972. + *
  19973. + * @retval: Returns negative as error, or the data length of the feature
  19974. + */
  19975. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  19976. + struct dwc_otg_pcd *pcd,
  19977. + struct cfi_usb_ctrlrequest *ctrl_req)
  19978. +{
  19979. + int retval = -DWC_E_NOT_SUPPORTED;
  19980. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  19981. + uint16_t dfifo, rxfifo, txfifo;
  19982. +
  19983. + switch (ctrl_req->wIndex) {
  19984. + /* Whether the DDMA is enabled or not */
  19985. + case FT_ID_DMA_MODE:
  19986. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  19987. + retval = 1;
  19988. + break;
  19989. +
  19990. + case FT_ID_DMA_BUFFER_SETUP:
  19991. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  19992. + break;
  19993. +
  19994. + case FT_ID_DMA_BUFF_ALIGN:
  19995. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  19996. + break;
  19997. +
  19998. + case FT_ID_DMA_CONCAT_SETUP:
  19999. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  20000. + break;
  20001. +
  20002. + case FT_ID_DMA_CIRCULAR:
  20003. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  20004. + break;
  20005. +
  20006. + case FT_ID_THRESHOLD_SETUP:
  20007. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  20008. + break;
  20009. +
  20010. + case FT_ID_DFIFO_DEPTH:
  20011. + dfifo = get_dfifo_size(coreif);
  20012. + *((uint16_t *) buf) = dfifo;
  20013. + retval = sizeof(uint16_t);
  20014. + break;
  20015. +
  20016. + case FT_ID_TX_FIFO_DEPTH:
  20017. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  20018. + if (retval >= 0) {
  20019. + txfifo = retval;
  20020. + *((uint16_t *) buf) = txfifo;
  20021. + retval = sizeof(uint16_t);
  20022. + }
  20023. + break;
  20024. +
  20025. + case FT_ID_RX_FIFO_DEPTH:
  20026. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  20027. + if (retval >= 0) {
  20028. + rxfifo = retval;
  20029. + *((uint16_t *) buf) = rxfifo;
  20030. + retval = sizeof(uint16_t);
  20031. + }
  20032. + break;
  20033. + }
  20034. +
  20035. + return retval;
  20036. +}
  20037. +
  20038. +/**
  20039. + * This function resets the SG for the specified EP to its default value
  20040. + */
  20041. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  20042. +{
  20043. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  20044. + return 0;
  20045. +}
  20046. +
  20047. +/**
  20048. + * This function resets the Alignment for the specified EP to its default value
  20049. + */
  20050. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  20051. +{
  20052. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  20053. + return 0;
  20054. +}
  20055. +
  20056. +/**
  20057. + * This function resets the Concatenation for the specified EP to its default value
  20058. + * This function will also set the value of the wTxBytes field to NULL after
  20059. + * freeing the memory previously allocated for this field.
  20060. + */
  20061. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  20062. +{
  20063. + /* First we need to free the wTxBytes field */
  20064. + if (cfiep->bm_concat->wTxBytes) {
  20065. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  20066. + cfiep->bm_concat->wTxBytes = NULL;
  20067. + }
  20068. +
  20069. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  20070. + return 0;
  20071. +}
  20072. +
  20073. +/**
  20074. + * This function resets all the buffer setups of the specified endpoint
  20075. + */
  20076. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  20077. +{
  20078. + cfi_reset_sg_val(cfiep);
  20079. + cfi_reset_align_val(cfiep);
  20080. + cfi_reset_concat_val(cfiep);
  20081. + return 0;
  20082. +}
  20083. +
  20084. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  20085. + uint8_t rx_rst, uint8_t tx_rst)
  20086. +{
  20087. + int retval = -DWC_E_INVALID;
  20088. + uint16_t tx_siz[15];
  20089. + uint16_t rx_siz = 0;
  20090. + dwc_otg_pcd_ep_t *ep = NULL;
  20091. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  20092. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20093. +
  20094. + if (rx_rst) {
  20095. + rx_siz = params->dev_rx_fifo_size;
  20096. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  20097. + }
  20098. +
  20099. + if (tx_rst) {
  20100. + if (ep_addr == 0) {
  20101. + int i;
  20102. +
  20103. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20104. + tx_siz[i] =
  20105. + core_if->core_params->dev_tx_fifo_size[i];
  20106. + core_if->core_params->dev_tx_fifo_size[i] =
  20107. + core_if->init_txfsiz[i];
  20108. + }
  20109. + } else {
  20110. +
  20111. + ep = get_ep_by_addr(pcd, ep_addr);
  20112. +
  20113. + if (NULL == ep) {
  20114. + CFI_INFO
  20115. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  20116. + __func__, ep_addr);
  20117. + return -DWC_E_INVALID;
  20118. + }
  20119. +
  20120. + tx_siz[0] =
  20121. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  20122. + 1];
  20123. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  20124. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  20125. + dwc_ep.tx_fifo_num -
  20126. + 1];
  20127. + }
  20128. + }
  20129. +
  20130. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20131. + retval = 0;
  20132. + } else {
  20133. + CFI_INFO
  20134. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  20135. + __func__);
  20136. + if (rx_rst) {
  20137. + params->dev_rx_fifo_size = rx_siz;
  20138. + }
  20139. +
  20140. + if (tx_rst) {
  20141. + if (ep_addr == 0) {
  20142. + int i;
  20143. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  20144. + i++) {
  20145. + core_if->
  20146. + core_params->dev_tx_fifo_size[i] =
  20147. + tx_siz[i];
  20148. + }
  20149. + } else {
  20150. + params->dev_tx_fifo_size[ep->
  20151. + dwc_ep.tx_fifo_num -
  20152. + 1] = tx_siz[0];
  20153. + }
  20154. + }
  20155. + retval = -DWC_E_INVALID;
  20156. + }
  20157. + return retval;
  20158. +}
  20159. +
  20160. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  20161. +{
  20162. + int retval = 0;
  20163. + cfi_ep_t *cfiep;
  20164. + cfiobject_t *cfi = pcd->cfi;
  20165. + dwc_list_link_t *tmp;
  20166. +
  20167. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  20168. + if (retval < 0) {
  20169. + return retval;
  20170. + }
  20171. +
  20172. + /* If the EP address is known then reset the features for only that EP */
  20173. + if (addr) {
  20174. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20175. + if (NULL == cfiep) {
  20176. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20177. + __func__, addr);
  20178. + return -DWC_E_INVALID;
  20179. + }
  20180. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  20181. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  20182. + }
  20183. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20184. + else {
  20185. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20186. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20187. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20188. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20189. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  20190. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  20191. + if (retval < 0) {
  20192. + CFI_INFO
  20193. + ("%s: Error resetting the feature Reset All\n",
  20194. + __func__);
  20195. + return retval;
  20196. + }
  20197. + }
  20198. + }
  20199. + return retval;
  20200. +}
  20201. +
  20202. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  20203. + uint8_t addr)
  20204. +{
  20205. + int retval = 0;
  20206. + cfi_ep_t *cfiep;
  20207. + cfiobject_t *cfi = pcd->cfi;
  20208. + dwc_list_link_t *tmp;
  20209. +
  20210. + /* If the EP address is known then reset the features for only that EP */
  20211. + if (addr) {
  20212. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20213. + if (NULL == cfiep) {
  20214. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20215. + __func__, addr);
  20216. + return -DWC_E_INVALID;
  20217. + }
  20218. + retval = cfi_reset_sg_val(cfiep);
  20219. + }
  20220. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20221. + else {
  20222. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20223. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20224. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20225. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20226. + retval = cfi_reset_sg_val(cfiep);
  20227. + if (retval < 0) {
  20228. + CFI_INFO
  20229. + ("%s: Error resetting the feature Buffer Setup\n",
  20230. + __func__);
  20231. + return retval;
  20232. + }
  20233. + }
  20234. + }
  20235. + return retval;
  20236. +}
  20237. +
  20238. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  20239. +{
  20240. + int retval = 0;
  20241. + cfi_ep_t *cfiep;
  20242. + cfiobject_t *cfi = pcd->cfi;
  20243. + dwc_list_link_t *tmp;
  20244. +
  20245. + /* If the EP address is known then reset the features for only that EP */
  20246. + if (addr) {
  20247. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20248. + if (NULL == cfiep) {
  20249. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20250. + __func__, addr);
  20251. + return -DWC_E_INVALID;
  20252. + }
  20253. + retval = cfi_reset_concat_val(cfiep);
  20254. + }
  20255. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20256. + else {
  20257. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20258. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20259. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20260. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20261. + retval = cfi_reset_concat_val(cfiep);
  20262. + if (retval < 0) {
  20263. + CFI_INFO
  20264. + ("%s: Error resetting the feature Concatenation Value\n",
  20265. + __func__);
  20266. + return retval;
  20267. + }
  20268. + }
  20269. + }
  20270. + return retval;
  20271. +}
  20272. +
  20273. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  20274. +{
  20275. + int retval = 0;
  20276. + cfi_ep_t *cfiep;
  20277. + cfiobject_t *cfi = pcd->cfi;
  20278. + dwc_list_link_t *tmp;
  20279. +
  20280. + /* If the EP address is known then reset the features for only that EP */
  20281. + if (addr) {
  20282. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20283. + if (NULL == cfiep) {
  20284. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20285. + __func__, addr);
  20286. + return -DWC_E_INVALID;
  20287. + }
  20288. + retval = cfi_reset_align_val(cfiep);
  20289. + }
  20290. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20291. + else {
  20292. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20293. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20294. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20295. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20296. + retval = cfi_reset_align_val(cfiep);
  20297. + if (retval < 0) {
  20298. + CFI_INFO
  20299. + ("%s: Error resetting the feature Aliignment Value\n",
  20300. + __func__);
  20301. + return retval;
  20302. + }
  20303. + }
  20304. + }
  20305. + return retval;
  20306. +
  20307. +}
  20308. +
  20309. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  20310. + struct cfi_usb_ctrlrequest *req)
  20311. +{
  20312. + int retval = 0;
  20313. +
  20314. + switch (req->wIndex) {
  20315. + case 0:
  20316. + /* Reset all features */
  20317. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  20318. + break;
  20319. +
  20320. + case FT_ID_DMA_BUFFER_SETUP:
  20321. + /* Reset the SG buffer setup */
  20322. + retval =
  20323. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  20324. + break;
  20325. +
  20326. + case FT_ID_DMA_CONCAT_SETUP:
  20327. + /* Reset the Concatenation buffer setup */
  20328. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  20329. + break;
  20330. +
  20331. + case FT_ID_DMA_BUFF_ALIGN:
  20332. + /* Reset the Alignment buffer setup */
  20333. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  20334. + break;
  20335. +
  20336. + case FT_ID_TX_FIFO_DEPTH:
  20337. + retval =
  20338. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  20339. + pcd->cfi->need_gadget_att = 0;
  20340. + break;
  20341. +
  20342. + case FT_ID_RX_FIFO_DEPTH:
  20343. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  20344. + pcd->cfi->need_gadget_att = 0;
  20345. + break;
  20346. + default:
  20347. + break;
  20348. + }
  20349. + return retval;
  20350. +}
  20351. +
  20352. +/**
  20353. + * This function sets a new value for the SG buffer setup.
  20354. + */
  20355. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20356. +{
  20357. + uint8_t inaddr, outaddr;
  20358. + cfi_ep_t *epin, *epout;
  20359. + ddma_sg_buffer_setup_t *psgval;
  20360. + uint32_t desccount, size;
  20361. +
  20362. + CFI_INFO("%s\n", __func__);
  20363. +
  20364. + psgval = (ddma_sg_buffer_setup_t *) buf;
  20365. + desccount = (uint32_t) psgval->bCount;
  20366. + size = (uint32_t) psgval->wSize;
  20367. +
  20368. + /* Check the DMA descriptor count */
  20369. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  20370. + CFI_INFO
  20371. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  20372. + __func__, MAX_DMA_DESCS_PER_EP);
  20373. + return -DWC_E_INVALID;
  20374. + }
  20375. +
  20376. + /* Check the DMA descriptor count */
  20377. +
  20378. + if (size == 0) {
  20379. +
  20380. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  20381. + __func__);
  20382. +
  20383. + return -DWC_E_INVALID;
  20384. +
  20385. + }
  20386. +
  20387. + inaddr = psgval->bInEndpointAddress;
  20388. + outaddr = psgval->bOutEndpointAddress;
  20389. +
  20390. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  20391. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  20392. +
  20393. + if (NULL == epin || NULL == epout) {
  20394. + CFI_INFO
  20395. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  20396. + __func__, inaddr, outaddr);
  20397. + return -DWC_E_INVALID;
  20398. + }
  20399. +
  20400. + epin->ep->dwc_ep.buff_mode = BM_SG;
  20401. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  20402. +
  20403. + epout->ep->dwc_ep.buff_mode = BM_SG;
  20404. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  20405. +
  20406. + return 0;
  20407. +}
  20408. +
  20409. +/**
  20410. + * This function sets a new value for the buffer Alignment setup.
  20411. + */
  20412. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20413. +{
  20414. + cfi_ep_t *ep;
  20415. + uint8_t addr;
  20416. + ddma_align_buffer_setup_t *palignval;
  20417. +
  20418. + palignval = (ddma_align_buffer_setup_t *) buf;
  20419. + addr = palignval->bEndpointAddress;
  20420. +
  20421. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20422. +
  20423. + if (NULL == ep) {
  20424. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20425. + __func__, addr);
  20426. + return -DWC_E_INVALID;
  20427. + }
  20428. +
  20429. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  20430. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  20431. +
  20432. + return 0;
  20433. +}
  20434. +
  20435. +/**
  20436. + * This function sets a new value for the Concatenation buffer setup.
  20437. + */
  20438. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20439. +{
  20440. + uint8_t addr;
  20441. + cfi_ep_t *ep;
  20442. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  20443. + uint16_t *pVals;
  20444. + uint32_t desccount;
  20445. + int i;
  20446. + uint16_t mps;
  20447. +
  20448. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  20449. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  20450. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  20451. +
  20452. + /* Check the DMA descriptor count */
  20453. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  20454. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  20455. + __func__, MAX_DMA_DESCS_PER_EP);
  20456. + return -DWC_E_INVALID;
  20457. + }
  20458. +
  20459. + addr = pConcatValHdr->bEndpointAddress;
  20460. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20461. + if (NULL == ep) {
  20462. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20463. + __func__, addr);
  20464. + return -DWC_E_INVALID;
  20465. + }
  20466. +
  20467. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  20468. +
  20469. +#if 0
  20470. + for (i = 0; i < desccount; i++) {
  20471. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  20472. + }
  20473. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  20474. +#endif
  20475. +
  20476. + /* Check the wTxSizes to be less than or equal to the mps */
  20477. + for (i = 0; i < desccount; i++) {
  20478. + if (pVals[i] > mps) {
  20479. + CFI_INFO
  20480. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  20481. + __func__, i, pVals[i]);
  20482. + return -DWC_E_INVALID;
  20483. + }
  20484. + }
  20485. +
  20486. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  20487. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  20488. +
  20489. + /* Free the previously allocated storage for the wTxBytes */
  20490. + if (ep->bm_concat->wTxBytes) {
  20491. + DWC_FREE(ep->bm_concat->wTxBytes);
  20492. + }
  20493. +
  20494. + /* Allocate a new storage for the wTxBytes field */
  20495. + ep->bm_concat->wTxBytes =
  20496. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  20497. + if (NULL == ep->bm_concat->wTxBytes) {
  20498. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  20499. + return -DWC_E_NO_MEMORY;
  20500. + }
  20501. +
  20502. + /* Copy the new values into the wTxBytes filed */
  20503. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  20504. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  20505. +
  20506. + return 0;
  20507. +}
  20508. +
  20509. +/**
  20510. + * This function calculates the total of all FIFO sizes
  20511. + *
  20512. + * @param core_if Programming view of DWC_otg controller
  20513. + *
  20514. + * @return The total of data FIFO sizes.
  20515. + *
  20516. + */
  20517. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  20518. +{
  20519. + dwc_otg_core_params_t *params = core_if->core_params;
  20520. + uint16_t dfifo_total = 0;
  20521. + int i;
  20522. +
  20523. + /* The shared RxFIFO size */
  20524. + dfifo_total =
  20525. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  20526. +
  20527. + /* Add up each TxFIFO size to the total */
  20528. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20529. + dfifo_total += params->dev_tx_fifo_size[i];
  20530. + }
  20531. +
  20532. + return dfifo_total;
  20533. +}
  20534. +
  20535. +/**
  20536. + * This function returns Rx FIFO size
  20537. + *
  20538. + * @param core_if Programming view of DWC_otg controller
  20539. + *
  20540. + * @return The total of data FIFO sizes.
  20541. + *
  20542. + */
  20543. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  20544. +{
  20545. + switch (wValue >> 8) {
  20546. + case 0:
  20547. + return (core_if->pwron_rxfsiz <
  20548. + 32768) ? core_if->pwron_rxfsiz : 32768;
  20549. + break;
  20550. + case 1:
  20551. + return core_if->core_params->dev_rx_fifo_size;
  20552. + break;
  20553. + default:
  20554. + return -DWC_E_INVALID;
  20555. + break;
  20556. + }
  20557. +}
  20558. +
  20559. +/**
  20560. + * This function returns Tx FIFO size for IN EP
  20561. + *
  20562. + * @param core_if Programming view of DWC_otg controller
  20563. + *
  20564. + * @return The total of data FIFO sizes.
  20565. + *
  20566. + */
  20567. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  20568. +{
  20569. + dwc_otg_pcd_ep_t *ep;
  20570. +
  20571. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  20572. +
  20573. + if (NULL == ep) {
  20574. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20575. + __func__, wValue & 0xff);
  20576. + return -DWC_E_INVALID;
  20577. + }
  20578. +
  20579. + if (!ep->dwc_ep.is_in) {
  20580. + CFI_INFO
  20581. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  20582. + __func__, wValue & 0xff);
  20583. + return -DWC_E_INVALID;
  20584. + }
  20585. +
  20586. + switch (wValue >> 8) {
  20587. + case 0:
  20588. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  20589. + [ep->dwc_ep.tx_fifo_num - 1] <
  20590. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  20591. + dwc_ep.tx_fifo_num
  20592. + - 1] : 32768;
  20593. + break;
  20594. + case 1:
  20595. + return GET_CORE_IF(pcd)->core_params->
  20596. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  20597. + break;
  20598. + default:
  20599. + return -DWC_E_INVALID;
  20600. + break;
  20601. + }
  20602. +}
  20603. +
  20604. +/**
  20605. + * This function checks if the submitted combination of
  20606. + * device mode FIFO sizes is possible or not.
  20607. + *
  20608. + * @param core_if Programming view of DWC_otg controller
  20609. + *
  20610. + * @return 1 if possible, 0 otherwise.
  20611. + *
  20612. + */
  20613. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  20614. +{
  20615. + uint16_t dfifo_actual = 0;
  20616. + dwc_otg_core_params_t *params = core_if->core_params;
  20617. + uint16_t start_addr = 0;
  20618. + int i;
  20619. +
  20620. + dfifo_actual =
  20621. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  20622. +
  20623. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20624. + dfifo_actual += params->dev_tx_fifo_size[i];
  20625. + }
  20626. +
  20627. + if (dfifo_actual > core_if->total_fifo_size) {
  20628. + return 0;
  20629. + }
  20630. +
  20631. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  20632. + return 0;
  20633. +
  20634. + if (params->dev_nperio_tx_fifo_size > 32768
  20635. + || params->dev_nperio_tx_fifo_size < 16)
  20636. + return 0;
  20637. +
  20638. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20639. +
  20640. + if (params->dev_tx_fifo_size[i] > 768
  20641. + || params->dev_tx_fifo_size[i] < 4)
  20642. + return 0;
  20643. + }
  20644. +
  20645. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  20646. + return 0;
  20647. + start_addr = params->dev_rx_fifo_size;
  20648. +
  20649. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  20650. + return 0;
  20651. + start_addr += params->dev_nperio_tx_fifo_size;
  20652. +
  20653. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20654. +
  20655. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  20656. + return 0;
  20657. + start_addr += params->dev_tx_fifo_size[i];
  20658. + }
  20659. +
  20660. + return 1;
  20661. +}
  20662. +
  20663. +/**
  20664. + * This function resizes Device mode FIFOs
  20665. + *
  20666. + * @param core_if Programming view of DWC_otg controller
  20667. + *
  20668. + * @return 1 if successful, 0 otherwise
  20669. + *
  20670. + */
  20671. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  20672. +{
  20673. + int i = 0;
  20674. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  20675. + dwc_otg_core_params_t *params = core_if->core_params;
  20676. + uint32_t rx_fifo_size;
  20677. + fifosize_data_t nptxfifosize;
  20678. + fifosize_data_t txfifosize[15];
  20679. +
  20680. + uint32_t rx_fsz_bak;
  20681. + uint32_t nptxfsz_bak;
  20682. + uint32_t txfsz_bak[15];
  20683. +
  20684. + uint16_t start_address;
  20685. + uint8_t retval = 1;
  20686. +
  20687. + if (!check_fifo_sizes(core_if)) {
  20688. + return 0;
  20689. + }
  20690. +
  20691. + /* Configure data FIFO sizes */
  20692. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  20693. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  20694. + rx_fifo_size = params->dev_rx_fifo_size;
  20695. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  20696. +
  20697. + /*
  20698. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  20699. + * Indexes of the FIFO size module parameters in the
  20700. + * dev_tx_fifo_size array and the FIFO size registers in
  20701. + * the dtxfsiz array run from 0 to 14.
  20702. + */
  20703. +
  20704. + /* Non-periodic Tx FIFO */
  20705. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  20706. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  20707. + start_address = params->dev_rx_fifo_size;
  20708. + nptxfifosize.b.startaddr = start_address;
  20709. +
  20710. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  20711. +
  20712. + start_address += nptxfifosize.b.depth;
  20713. +
  20714. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20715. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  20716. +
  20717. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  20718. + txfifosize[i].b.startaddr = start_address;
  20719. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  20720. + txfifosize[i].d32);
  20721. +
  20722. + start_address += txfifosize[i].b.depth;
  20723. + }
  20724. +
  20725. + /** Check if register values are set correctly */
  20726. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  20727. + retval = 0;
  20728. + }
  20729. +
  20730. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  20731. + retval = 0;
  20732. + }
  20733. +
  20734. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20735. + if (txfifosize[i].d32 !=
  20736. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  20737. + retval = 0;
  20738. + }
  20739. + }
  20740. +
  20741. + /** If register values are not set correctly, reset old values */
  20742. + if (retval == 0) {
  20743. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  20744. +
  20745. + /* Non-periodic Tx FIFO */
  20746. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  20747. +
  20748. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20749. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  20750. + txfsz_bak[i]);
  20751. + }
  20752. + }
  20753. + } else {
  20754. + return 0;
  20755. + }
  20756. +
  20757. + /* Flush the FIFOs */
  20758. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  20759. + dwc_otg_flush_rx_fifo(core_if);
  20760. +
  20761. + return retval;
  20762. +}
  20763. +
  20764. +/**
  20765. + * This function sets a new value for the buffer Alignment setup.
  20766. + */
  20767. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  20768. +{
  20769. + int retval;
  20770. + uint32_t fsiz;
  20771. + uint16_t size;
  20772. + uint16_t ep_addr;
  20773. + dwc_otg_pcd_ep_t *ep;
  20774. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20775. + tx_fifo_size_setup_t *ptxfifoval;
  20776. +
  20777. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  20778. + ep_addr = ptxfifoval->bEndpointAddress;
  20779. + size = ptxfifoval->wDepth;
  20780. +
  20781. + ep = get_ep_by_addr(pcd, ep_addr);
  20782. +
  20783. + CFI_INFO
  20784. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  20785. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  20786. +
  20787. + if (NULL == ep) {
  20788. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20789. + __func__, ep_addr);
  20790. + return -DWC_E_INVALID;
  20791. + }
  20792. +
  20793. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  20794. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  20795. +
  20796. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20797. + retval = 0;
  20798. + } else {
  20799. + CFI_INFO
  20800. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  20801. + __func__, ep_addr);
  20802. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  20803. + retval = -DWC_E_INVALID;
  20804. + }
  20805. +
  20806. + return retval;
  20807. +}
  20808. +
  20809. +/**
  20810. + * This function sets a new value for the buffer Alignment setup.
  20811. + */
  20812. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  20813. +{
  20814. + int retval;
  20815. + uint32_t fsiz;
  20816. + uint16_t size;
  20817. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20818. + rx_fifo_size_setup_t *prxfifoval;
  20819. +
  20820. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  20821. + size = prxfifoval->wDepth;
  20822. +
  20823. + fsiz = params->dev_rx_fifo_size;
  20824. + params->dev_rx_fifo_size = size;
  20825. +
  20826. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20827. + retval = 0;
  20828. + } else {
  20829. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  20830. + __func__);
  20831. + params->dev_rx_fifo_size = fsiz;
  20832. + retval = -DWC_E_INVALID;
  20833. + }
  20834. +
  20835. + return retval;
  20836. +}
  20837. +
  20838. +/**
  20839. + * This function reads the SG of an EP's buffer setup into the buffer buf
  20840. + */
  20841. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20842. + struct cfi_usb_ctrlrequest *req)
  20843. +{
  20844. + int retval = -DWC_E_INVALID;
  20845. + uint8_t addr;
  20846. + cfi_ep_t *ep;
  20847. +
  20848. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20849. + addr = req->wValue & 0xFF;
  20850. + if (addr == 0) /* The address should be non-zero */
  20851. + return retval;
  20852. +
  20853. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20854. + if (NULL == ep) {
  20855. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20856. + __func__, addr);
  20857. + return retval;
  20858. + }
  20859. +
  20860. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  20861. + retval = BS_SG_VAL_DESC_LEN;
  20862. + return retval;
  20863. +}
  20864. +
  20865. +/**
  20866. + * This function reads the Concatenation value of an EP's buffer mode into
  20867. + * the buffer buf
  20868. + */
  20869. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20870. + struct cfi_usb_ctrlrequest *req)
  20871. +{
  20872. + int retval = -DWC_E_INVALID;
  20873. + uint8_t addr;
  20874. + cfi_ep_t *ep;
  20875. + uint8_t desc_count;
  20876. +
  20877. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20878. + addr = req->wValue & 0xFF;
  20879. + if (addr == 0) /* The address should be non-zero */
  20880. + return retval;
  20881. +
  20882. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20883. + if (NULL == ep) {
  20884. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20885. + __func__, addr);
  20886. + return retval;
  20887. + }
  20888. +
  20889. + /* Copy the header to the buffer */
  20890. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  20891. + /* Advance the buffer pointer by the header size */
  20892. + buf += BS_CONCAT_VAL_HDR_LEN;
  20893. +
  20894. + desc_count = ep->bm_concat->hdr.bDescCount;
  20895. + /* Copy alll the wTxBytes to the buffer */
  20896. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  20897. +
  20898. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  20899. + return retval;
  20900. +}
  20901. +
  20902. +/**
  20903. + * This function reads the buffer Alignment value of an EP's buffer mode into
  20904. + * the buffer buf
  20905. + *
  20906. + * @return The total number of bytes copied to the buffer or negative error code.
  20907. + */
  20908. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20909. + struct cfi_usb_ctrlrequest *req)
  20910. +{
  20911. + int retval = -DWC_E_INVALID;
  20912. + uint8_t addr;
  20913. + cfi_ep_t *ep;
  20914. +
  20915. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20916. + addr = req->wValue & 0xFF;
  20917. + if (addr == 0) /* The address should be non-zero */
  20918. + return retval;
  20919. +
  20920. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20921. + if (NULL == ep) {
  20922. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20923. + __func__, addr);
  20924. + return retval;
  20925. + }
  20926. +
  20927. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  20928. + retval = BS_ALIGN_VAL_HDR_LEN;
  20929. +
  20930. + return retval;
  20931. +}
  20932. +
  20933. +/**
  20934. + * This function sets a new value for the specified feature
  20935. + *
  20936. + * @param pcd A pointer to the PCD object
  20937. + *
  20938. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  20939. + */
  20940. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  20941. +{
  20942. + int retval = -DWC_E_NOT_SUPPORTED;
  20943. + uint16_t wIndex, wValue;
  20944. + uint8_t bRequest;
  20945. + struct dwc_otg_core_if *coreif;
  20946. + cfiobject_t *cfi = pcd->cfi;
  20947. + struct cfi_usb_ctrlrequest *ctrl_req;
  20948. + uint8_t *buf;
  20949. + ctrl_req = &cfi->ctrl_req;
  20950. +
  20951. + buf = pcd->cfi->ctrl_req.data;
  20952. +
  20953. + coreif = GET_CORE_IF(pcd);
  20954. + bRequest = ctrl_req->bRequest;
  20955. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  20956. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  20957. +
  20958. + /* See which feature is to be modified */
  20959. + switch (wIndex) {
  20960. + case FT_ID_DMA_BUFFER_SETUP:
  20961. + /* Modify the feature */
  20962. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  20963. + return retval;
  20964. +
  20965. + /* And send this request to the gadget */
  20966. + cfi->need_gadget_att = 1;
  20967. + break;
  20968. +
  20969. + case FT_ID_DMA_BUFF_ALIGN:
  20970. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  20971. + return retval;
  20972. + cfi->need_gadget_att = 1;
  20973. + break;
  20974. +
  20975. + case FT_ID_DMA_CONCAT_SETUP:
  20976. + /* Modify the feature */
  20977. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  20978. + return retval;
  20979. + cfi->need_gadget_att = 1;
  20980. + break;
  20981. +
  20982. + case FT_ID_DMA_CIRCULAR:
  20983. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  20984. + break;
  20985. +
  20986. + case FT_ID_THRESHOLD_SETUP:
  20987. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  20988. + break;
  20989. +
  20990. + case FT_ID_DFIFO_DEPTH:
  20991. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  20992. + break;
  20993. +
  20994. + case FT_ID_TX_FIFO_DEPTH:
  20995. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  20996. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  20997. + return retval;
  20998. + cfi->need_gadget_att = 0;
  20999. + break;
  21000. +
  21001. + case FT_ID_RX_FIFO_DEPTH:
  21002. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  21003. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  21004. + return retval;
  21005. + cfi->need_gadget_att = 0;
  21006. + break;
  21007. + }
  21008. +
  21009. + return retval;
  21010. +}
  21011. +
  21012. +#endif //DWC_UTE_CFI
  21013. --- /dev/null
  21014. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  21015. @@ -0,0 +1,320 @@
  21016. +/* ==========================================================================
  21017. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  21018. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  21019. + * otherwise expressly agreed to in writing between Synopsys and you.
  21020. + *
  21021. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  21022. + * any End User Software License Agreement or Agreement for Licensed Product
  21023. + * with Synopsys or any supplement thereto. You are permitted to use and
  21024. + * redistribute this Software in source and binary forms, with or without
  21025. + * modification, provided that redistributions of source code must retain this
  21026. + * notice. You may not view, use, disclose, copy or distribute this file or
  21027. + * any information contained herein except pursuant to this license grant from
  21028. + * Synopsys. If you do not agree with this notice, including the disclaimer
  21029. + * below, then you are not authorized to use the Software.
  21030. + *
  21031. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  21032. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21033. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21034. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  21035. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21036. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21037. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21038. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21039. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21040. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  21041. + * DAMAGE.
  21042. + * ========================================================================== */
  21043. +
  21044. +#if !defined(__DWC_OTG_CFI_H__)
  21045. +#define __DWC_OTG_CFI_H__
  21046. +
  21047. +#include "dwc_otg_pcd.h"
  21048. +#include "dwc_cfi_common.h"
  21049. +
  21050. +/**
  21051. + * @file
  21052. + * This file contains the CFI related OTG PCD specific common constants,
  21053. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  21054. + * optional interface for internal testing purposes that a DUT may implement to
  21055. + * support testing of configurable features.
  21056. + *
  21057. + */
  21058. +
  21059. +struct dwc_otg_pcd;
  21060. +struct dwc_otg_pcd_ep;
  21061. +
  21062. +/** OTG CFI Features (properties) ID constants */
  21063. +/** This is a request for all Core Features */
  21064. +#define FT_ID_DMA_MODE 0x0001
  21065. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  21066. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  21067. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  21068. +#define FT_ID_DMA_CIRCULAR 0x0005
  21069. +#define FT_ID_THRESHOLD_SETUP 0x0006
  21070. +#define FT_ID_DFIFO_DEPTH 0x0007
  21071. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  21072. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  21073. +
  21074. +/**********************************************************/
  21075. +#define CFI_INFO_DEF
  21076. +
  21077. +#ifdef CFI_INFO_DEF
  21078. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  21079. +#else
  21080. +#define CFI_INFO(fmt...)
  21081. +#endif
  21082. +
  21083. +#define min(x,y) ({ \
  21084. + x < y ? x : y; })
  21085. +
  21086. +#define max(x,y) ({ \
  21087. + x > y ? x : y; })
  21088. +
  21089. +/**
  21090. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  21091. + * also used for setting up a buffer for Circular DDMA.
  21092. + */
  21093. +struct _ddma_sg_buffer_setup {
  21094. +#define BS_SG_VAL_DESC_LEN 6
  21095. + /* The OUT EP address */
  21096. + uint8_t bOutEndpointAddress;
  21097. + /* The IN EP address */
  21098. + uint8_t bInEndpointAddress;
  21099. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  21100. + uint8_t bOffset;
  21101. + /* The number of transfer segments (a DMA descriptors per each segment) */
  21102. + uint8_t bCount;
  21103. + /* Size (in byte) of each transfer segment */
  21104. + uint16_t wSize;
  21105. +} __attribute__ ((packed));
  21106. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  21107. +
  21108. +/** Descriptor DMA Concatenation Buffer setup structure */
  21109. +struct _ddma_concat_buffer_setup_hdr {
  21110. +#define BS_CONCAT_VAL_HDR_LEN 4
  21111. + /* The endpoint for which the buffer is to be set up */
  21112. + uint8_t bEndpointAddress;
  21113. + /* The count of descriptors to be used */
  21114. + uint8_t bDescCount;
  21115. + /* The total size of the transfer */
  21116. + uint16_t wSize;
  21117. +} __attribute__ ((packed));
  21118. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  21119. +
  21120. +/** Descriptor DMA Concatenation Buffer setup structure */
  21121. +struct _ddma_concat_buffer_setup {
  21122. + /* The SG header */
  21123. + ddma_concat_buffer_setup_hdr_t hdr;
  21124. +
  21125. + /* The XFER sizes pointer (allocated dynamically) */
  21126. + uint16_t *wTxBytes;
  21127. +} __attribute__ ((packed));
  21128. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  21129. +
  21130. +/** Descriptor DMA Alignment Buffer setup structure */
  21131. +struct _ddma_align_buffer_setup {
  21132. +#define BS_ALIGN_VAL_HDR_LEN 2
  21133. + uint8_t bEndpointAddress;
  21134. + uint8_t bAlign;
  21135. +} __attribute__ ((packed));
  21136. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  21137. +
  21138. +/** Transmit FIFO Size setup structure */
  21139. +struct _tx_fifo_size_setup {
  21140. + uint8_t bEndpointAddress;
  21141. + uint16_t wDepth;
  21142. +} __attribute__ ((packed));
  21143. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  21144. +
  21145. +/** Transmit FIFO Size setup structure */
  21146. +struct _rx_fifo_size_setup {
  21147. + uint16_t wDepth;
  21148. +} __attribute__ ((packed));
  21149. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  21150. +
  21151. +/**
  21152. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  21153. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  21154. + * to the data returned in the data stage of a 3-stage Control Write requests.
  21155. + */
  21156. +struct cfi_usb_ctrlrequest {
  21157. + uint8_t bRequestType;
  21158. + uint8_t bRequest;
  21159. + uint16_t wValue;
  21160. + uint16_t wIndex;
  21161. + uint16_t wLength;
  21162. + uint8_t *data;
  21163. +} UPACKED;
  21164. +
  21165. +/*---------------------------------------------------------------------------*/
  21166. +
  21167. +/**
  21168. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  21169. + * This structure is used to store the buffer setup data for any
  21170. + * enabled endpoint in the PCD.
  21171. + */
  21172. +struct cfi_ep {
  21173. + /* Entry for the list container */
  21174. + dwc_list_link_t lh;
  21175. + /* Pointer to the active PCD endpoint structure */
  21176. + struct dwc_otg_pcd_ep *ep;
  21177. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  21178. + struct dwc_otg_dma_desc *dma_desc_last;
  21179. + /* The SG feature value */
  21180. + ddma_sg_buffer_setup_t *bm_sg;
  21181. + /* The Circular feature value */
  21182. + ddma_sg_buffer_setup_t *bm_circ;
  21183. + /* The Concatenation feature value */
  21184. + ddma_concat_buffer_setup_t *bm_concat;
  21185. + /* The Alignment feature value */
  21186. + ddma_align_buffer_setup_t *bm_align;
  21187. + /* XFER length */
  21188. + uint32_t xfer_len;
  21189. + /*
  21190. + * Count of DMA descriptors currently used.
  21191. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  21192. + * defined in the dwc_otg_cil.h
  21193. + */
  21194. + uint32_t desc_count;
  21195. +};
  21196. +typedef struct cfi_ep cfi_ep_t;
  21197. +
  21198. +typedef struct cfi_dma_buff {
  21199. +#define CFI_IN_BUF_LEN 1024
  21200. +#define CFI_OUT_BUF_LEN 1024
  21201. + dma_addr_t addr;
  21202. + uint8_t *buf;
  21203. +} cfi_dma_buff_t;
  21204. +
  21205. +struct cfiobject;
  21206. +
  21207. +/**
  21208. + * This is the interface for the CFI operations.
  21209. + *
  21210. + * @param ep_enable Called when any endpoint is enabled and activated.
  21211. + * @param release Called when the CFI object is released and it needs to correctly
  21212. + * deallocate the dynamic memory
  21213. + * @param ctrl_write_complete Called when the data stage of the request is complete
  21214. + */
  21215. +typedef struct cfi_ops {
  21216. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  21217. + struct dwc_otg_pcd_ep * ep);
  21218. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  21219. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  21220. + unsigned size, gfp_t flags);
  21221. + void (*release) (struct cfiobject * cfi);
  21222. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  21223. + struct dwc_otg_pcd * pcd);
  21224. + void (*build_descriptors) (struct cfiobject * cfi,
  21225. + struct dwc_otg_pcd * pcd,
  21226. + struct dwc_otg_pcd_ep * ep,
  21227. + dwc_otg_pcd_request_t * req);
  21228. +} cfi_ops_t;
  21229. +
  21230. +struct cfiobject {
  21231. + cfi_ops_t ops;
  21232. + struct dwc_otg_pcd *pcd;
  21233. + struct usb_gadget *gadget;
  21234. +
  21235. + /* Buffers used to send/receive CFI-related request data */
  21236. + cfi_dma_buff_t buf_in;
  21237. + cfi_dma_buff_t buf_out;
  21238. +
  21239. + /* CFI specific Control request wrapper */
  21240. + struct cfi_usb_ctrlrequest ctrl_req;
  21241. +
  21242. + /* The list of active EP's in the PCD of type cfi_ep_t */
  21243. + dwc_list_link_t active_eps;
  21244. +
  21245. + /* This flag shall control the propagation of a specific request
  21246. + * to the gadget's processing routines.
  21247. + * 0 - no gadget handling
  21248. + * 1 - the gadget needs to know about this request (w/o completing a status
  21249. + * phase - just return a 0 to the _setup callback)
  21250. + */
  21251. + uint8_t need_gadget_att;
  21252. +
  21253. + /* Flag indicating whether the status IN phase needs to be
  21254. + * completed by the PCD
  21255. + */
  21256. + uint8_t need_status_in_complete;
  21257. +};
  21258. +typedef struct cfiobject cfiobject_t;
  21259. +
  21260. +#define DUMP_MSG
  21261. +
  21262. +#if defined(DUMP_MSG)
  21263. +static inline void dump_msg(const u8 * buf, unsigned int length)
  21264. +{
  21265. + unsigned int start, num, i;
  21266. + char line[52], *p;
  21267. +
  21268. + if (length >= 512)
  21269. + return;
  21270. +
  21271. + start = 0;
  21272. + while (length > 0) {
  21273. + num = min(length, 16u);
  21274. + p = line;
  21275. + for (i = 0; i < num; ++i) {
  21276. + if (i == 8)
  21277. + *p++ = ' ';
  21278. + DWC_SPRINTF(p, " %02x", buf[i]);
  21279. + p += 3;
  21280. + }
  21281. + *p = 0;
  21282. + DWC_DEBUG("%6x: %s\n", start, line);
  21283. + buf += num;
  21284. + start += num;
  21285. + length -= num;
  21286. + }
  21287. +}
  21288. +#else
  21289. +static inline void dump_msg(const u8 * buf, unsigned int length)
  21290. +{
  21291. +}
  21292. +#endif
  21293. +
  21294. +/**
  21295. + * This function returns a pointer to cfi_ep_t object with the addr address.
  21296. + */
  21297. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  21298. + uint8_t addr)
  21299. +{
  21300. + struct cfi_ep *pcfiep;
  21301. + dwc_list_link_t *tmp;
  21302. +
  21303. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  21304. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  21305. +
  21306. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  21307. + return pcfiep;
  21308. + }
  21309. + }
  21310. +
  21311. + return NULL;
  21312. +}
  21313. +
  21314. +/**
  21315. + * This function returns a pointer to cfi_ep_t object that matches
  21316. + * the dwc_otg_pcd_ep object.
  21317. + */
  21318. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  21319. + struct dwc_otg_pcd_ep *ep)
  21320. +{
  21321. + struct cfi_ep *pcfiep = NULL;
  21322. + dwc_list_link_t *tmp;
  21323. +
  21324. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  21325. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  21326. + if (pcfiep->ep == ep) {
  21327. + return pcfiep;
  21328. + }
  21329. + }
  21330. + return NULL;
  21331. +}
  21332. +
  21333. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  21334. +
  21335. +#endif /* (__DWC_OTG_CFI_H__) */
  21336. --- /dev/null
  21337. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  21338. @@ -0,0 +1,7141 @@
  21339. +/* ==========================================================================
  21340. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  21341. + * $Revision: #191 $
  21342. + * $Date: 2012/08/10 $
  21343. + * $Change: 2047372 $
  21344. + *
  21345. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  21346. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  21347. + * otherwise expressly agreed to in writing between Synopsys and you.
  21348. + *
  21349. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  21350. + * any End User Software License Agreement or Agreement for Licensed Product
  21351. + * with Synopsys or any supplement thereto. You are permitted to use and
  21352. + * redistribute this Software in source and binary forms, with or without
  21353. + * modification, provided that redistributions of source code must retain this
  21354. + * notice. You may not view, use, disclose, copy or distribute this file or
  21355. + * any information contained herein except pursuant to this license grant from
  21356. + * Synopsys. If you do not agree with this notice, including the disclaimer
  21357. + * below, then you are not authorized to use the Software.
  21358. + *
  21359. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  21360. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21361. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21362. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  21363. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21364. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21365. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21366. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21367. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21368. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  21369. + * DAMAGE.
  21370. + * ========================================================================== */
  21371. +
  21372. +/** @file
  21373. + *
  21374. + * The Core Interface Layer provides basic services for accessing and
  21375. + * managing the DWC_otg hardware. These services are used by both the
  21376. + * Host Controller Driver and the Peripheral Controller Driver.
  21377. + *
  21378. + * The CIL manages the memory map for the core so that the HCD and PCD
  21379. + * don't have to do this separately. It also handles basic tasks like
  21380. + * reading/writing the registers and data FIFOs in the controller.
  21381. + * Some of the data access functions provide encapsulation of several
  21382. + * operations required to perform a task, such as writing multiple
  21383. + * registers to start a transfer. Finally, the CIL performs basic
  21384. + * services that are not specific to either the host or device modes
  21385. + * of operation. These services include management of the OTG Host
  21386. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  21387. + * Diagnostic API is also provided to allow testing of the controller
  21388. + * hardware.
  21389. + *
  21390. + * The Core Interface Layer has the following requirements:
  21391. + * - Provides basic controller operations.
  21392. + * - Minimal use of OS services.
  21393. + * - The OS services used will be abstracted by using inline functions
  21394. + * or macros.
  21395. + *
  21396. + */
  21397. +
  21398. +#include "dwc_os.h"
  21399. +#include "dwc_otg_regs.h"
  21400. +#include "dwc_otg_cil.h"
  21401. +
  21402. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  21403. +
  21404. +/**
  21405. + * This function is called to initialize the DWC_otg CSR data
  21406. + * structures. The register addresses in the device and host
  21407. + * structures are initialized from the base address supplied by the
  21408. + * caller. The calling function must make the OS calls to get the
  21409. + * base address of the DWC_otg controller registers. The core_params
  21410. + * argument holds the parameters that specify how the core should be
  21411. + * configured.
  21412. + *
  21413. + * @param reg_base_addr Base address of DWC_otg core registers
  21414. + *
  21415. + */
  21416. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  21417. +{
  21418. + dwc_otg_core_if_t *core_if = 0;
  21419. + dwc_otg_dev_if_t *dev_if = 0;
  21420. + dwc_otg_host_if_t *host_if = 0;
  21421. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  21422. + int i = 0;
  21423. +
  21424. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  21425. +
  21426. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  21427. +
  21428. + if (core_if == NULL) {
  21429. + DWC_DEBUGPL(DBG_CIL,
  21430. + "Allocation of dwc_otg_core_if_t failed\n");
  21431. + return 0;
  21432. + }
  21433. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  21434. +
  21435. + /*
  21436. + * Allocate the Device Mode structures.
  21437. + */
  21438. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  21439. +
  21440. + if (dev_if == NULL) {
  21441. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  21442. + DWC_FREE(core_if);
  21443. + return 0;
  21444. + }
  21445. +
  21446. + dev_if->dev_global_regs =
  21447. + (dwc_otg_device_global_regs_t *) (reg_base +
  21448. + DWC_DEV_GLOBAL_REG_OFFSET);
  21449. +
  21450. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21451. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  21452. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  21453. + (i * DWC_EP_REG_OFFSET));
  21454. +
  21455. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  21456. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  21457. + (i * DWC_EP_REG_OFFSET));
  21458. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  21459. + i, &dev_if->in_ep_regs[i]->diepctl);
  21460. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  21461. + i, &dev_if->out_ep_regs[i]->doepctl);
  21462. + }
  21463. +
  21464. + dev_if->speed = 0; // unknown
  21465. +
  21466. + core_if->dev_if = dev_if;
  21467. +
  21468. + /*
  21469. + * Allocate the Host Mode structures.
  21470. + */
  21471. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  21472. +
  21473. + if (host_if == NULL) {
  21474. + DWC_DEBUGPL(DBG_CIL,
  21475. + "Allocation of dwc_otg_host_if_t failed\n");
  21476. + DWC_FREE(dev_if);
  21477. + DWC_FREE(core_if);
  21478. + return 0;
  21479. + }
  21480. +
  21481. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  21482. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  21483. +
  21484. + host_if->hprt0 =
  21485. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  21486. +
  21487. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21488. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  21489. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  21490. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  21491. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  21492. + i, &host_if->hc_regs[i]->hcchar);
  21493. + }
  21494. +
  21495. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  21496. + core_if->host_if = host_if;
  21497. +
  21498. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21499. + core_if->data_fifo[i] =
  21500. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  21501. + (i * DWC_OTG_DATA_FIFO_SIZE));
  21502. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  21503. + i, (unsigned long)core_if->data_fifo[i]);
  21504. + }
  21505. +
  21506. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  21507. +
  21508. + /* Initiate lx_state to L3 disconnected state */
  21509. + core_if->lx_state = DWC_OTG_L3;
  21510. + /*
  21511. + * Store the contents of the hardware configuration registers here for
  21512. + * easy access later.
  21513. + */
  21514. + core_if->hwcfg1.d32 =
  21515. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  21516. + core_if->hwcfg2.d32 =
  21517. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  21518. + core_if->hwcfg3.d32 =
  21519. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  21520. + core_if->hwcfg4.d32 =
  21521. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  21522. +
  21523. + /* Force host mode to get HPTXFSIZ exact power on value */
  21524. + {
  21525. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  21526. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21527. + gusbcfg.b.force_host_mode = 1;
  21528. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  21529. + dwc_mdelay(100);
  21530. + core_if->hptxfsiz.d32 =
  21531. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  21532. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21533. + gusbcfg.b.force_host_mode = 1;
  21534. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  21535. + dwc_mdelay(100);
  21536. + }
  21537. +
  21538. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  21539. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  21540. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  21541. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  21542. +
  21543. + core_if->hcfg.d32 =
  21544. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  21545. + core_if->dcfg.d32 =
  21546. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  21547. +
  21548. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  21549. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  21550. +
  21551. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  21552. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  21553. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  21554. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  21555. + core_if->hwcfg2.b.num_host_chan);
  21556. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  21557. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  21558. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  21559. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  21560. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  21561. + core_if->hwcfg2.b.dev_token_q_depth);
  21562. +
  21563. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  21564. + core_if->hwcfg3.b.dfifo_depth);
  21565. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  21566. + core_if->hwcfg3.b.xfer_size_cntr_width);
  21567. +
  21568. + /*
  21569. + * Set the SRP sucess bit for FS-I2c
  21570. + */
  21571. + core_if->srp_success = 0;
  21572. + core_if->srp_timer_started = 0;
  21573. +
  21574. + /*
  21575. + * Create new workqueue and init works
  21576. + */
  21577. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  21578. + if (core_if->wq_otg == 0) {
  21579. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  21580. + DWC_FREE(host_if);
  21581. + DWC_FREE(dev_if);
  21582. + DWC_FREE(core_if);
  21583. + return 0;
  21584. + }
  21585. +
  21586. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  21587. +
  21588. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  21589. + (core_if->snpsid >> 12 & 0xF),
  21590. + (core_if->snpsid >> 8 & 0xF),
  21591. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  21592. +
  21593. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  21594. + w_wakeup_detected, core_if);
  21595. + if (core_if->wkp_timer == 0) {
  21596. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  21597. + DWC_FREE(host_if);
  21598. + DWC_FREE(dev_if);
  21599. + DWC_WORKQ_FREE(core_if->wq_otg);
  21600. + DWC_FREE(core_if);
  21601. + return 0;
  21602. + }
  21603. +
  21604. + if (dwc_otg_setup_params(core_if)) {
  21605. + DWC_WARN("Error while setting core params\n");
  21606. + }
  21607. +
  21608. + core_if->hibernation_suspend = 0;
  21609. +
  21610. + /** ADP initialization */
  21611. + dwc_otg_adp_init(core_if);
  21612. +
  21613. + return core_if;
  21614. +}
  21615. +
  21616. +/**
  21617. + * This function frees the structures allocated by dwc_otg_cil_init().
  21618. + *
  21619. + * @param core_if The core interface pointer returned from
  21620. + * dwc_otg_cil_init().
  21621. + *
  21622. + */
  21623. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  21624. +{
  21625. + dctl_data_t dctl = {.d32 = 0 };
  21626. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  21627. +
  21628. + /* Disable all interrupts */
  21629. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  21630. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  21631. +
  21632. + dctl.b.sftdiscon = 1;
  21633. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  21634. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  21635. + dctl.d32);
  21636. + }
  21637. +
  21638. + if (core_if->wq_otg) {
  21639. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  21640. + DWC_WORKQ_FREE(core_if->wq_otg);
  21641. + }
  21642. + if (core_if->dev_if) {
  21643. + DWC_FREE(core_if->dev_if);
  21644. + }
  21645. + if (core_if->host_if) {
  21646. + DWC_FREE(core_if->host_if);
  21647. + }
  21648. +
  21649. + /** Remove ADP Stuff */
  21650. + dwc_otg_adp_remove(core_if);
  21651. + if (core_if->core_params) {
  21652. + DWC_FREE(core_if->core_params);
  21653. + }
  21654. + if (core_if->wkp_timer) {
  21655. + DWC_TIMER_FREE(core_if->wkp_timer);
  21656. + }
  21657. + if (core_if->srp_timer) {
  21658. + DWC_TIMER_FREE(core_if->srp_timer);
  21659. + }
  21660. + DWC_FREE(core_if);
  21661. +}
  21662. +
  21663. +/**
  21664. + * This function enables the controller's Global Interrupt in the AHB Config
  21665. + * register.
  21666. + *
  21667. + * @param core_if Programming view of DWC_otg controller.
  21668. + */
  21669. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  21670. +{
  21671. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  21672. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  21673. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  21674. +}
  21675. +
  21676. +/**
  21677. + * This function disables the controller's Global Interrupt in the AHB Config
  21678. + * register.
  21679. + *
  21680. + * @param core_if Programming view of DWC_otg controller.
  21681. + */
  21682. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  21683. +{
  21684. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  21685. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  21686. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  21687. +}
  21688. +
  21689. +/**
  21690. + * This function initializes the commmon interrupts, used in both
  21691. + * device and host modes.
  21692. + *
  21693. + * @param core_if Programming view of the DWC_otg controller
  21694. + *
  21695. + */
  21696. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  21697. +{
  21698. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  21699. + gintmsk_data_t intr_mask = {.d32 = 0 };
  21700. +
  21701. + /* Clear any pending OTG Interrupts */
  21702. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  21703. +
  21704. + /* Clear any pending interrupts */
  21705. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  21706. +
  21707. + /*
  21708. + * Enable the interrupts in the GINTMSK.
  21709. + */
  21710. + intr_mask.b.modemismatch = 1;
  21711. + intr_mask.b.otgintr = 1;
  21712. +
  21713. + if (!core_if->dma_enable) {
  21714. + intr_mask.b.rxstsqlvl = 1;
  21715. + }
  21716. +
  21717. + intr_mask.b.conidstschng = 1;
  21718. + intr_mask.b.wkupintr = 1;
  21719. + intr_mask.b.disconnect = 0;
  21720. + intr_mask.b.usbsuspend = 1;
  21721. + intr_mask.b.sessreqintr = 1;
  21722. +#ifdef CONFIG_USB_DWC_OTG_LPM
  21723. + if (core_if->core_params->lpm_enable) {
  21724. + intr_mask.b.lpmtranrcvd = 1;
  21725. + }
  21726. +#endif
  21727. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  21728. +}
  21729. +
  21730. +/*
  21731. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  21732. + * Hibernation. This function is for exiting from Device mode hibernation by
  21733. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  21734. + * @param core_if Programming view of DWC_otg controller.
  21735. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  21736. + * @param reset - indicates whether resume is initiated by Reset.
  21737. + */
  21738. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  21739. + int rem_wakeup, int reset)
  21740. +{
  21741. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  21742. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  21743. + dctl_data_t dctl = {.d32 = 0 };
  21744. +
  21745. + int timeout = 2000;
  21746. +
  21747. + if (!core_if->hibernation_suspend) {
  21748. + DWC_PRINTF("Already exited from Hibernation\n");
  21749. + return 1;
  21750. + }
  21751. +
  21752. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  21753. + /* Switch-on voltage to the core */
  21754. + gpwrdn.b.pwrdnswtch = 1;
  21755. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21756. + dwc_udelay(10);
  21757. +
  21758. + /* Reset core */
  21759. + gpwrdn.d32 = 0;
  21760. + gpwrdn.b.pwrdnrstn = 1;
  21761. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21762. + dwc_udelay(10);
  21763. +
  21764. + /* Assert Restore signal */
  21765. + gpwrdn.d32 = 0;
  21766. + gpwrdn.b.restore = 1;
  21767. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21768. + dwc_udelay(10);
  21769. +
  21770. + /* Disable power clamps */
  21771. + gpwrdn.d32 = 0;
  21772. + gpwrdn.b.pwrdnclmp = 1;
  21773. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21774. +
  21775. + if (rem_wakeup) {
  21776. + dwc_udelay(70);
  21777. + }
  21778. +
  21779. + /* Deassert Reset core */
  21780. + gpwrdn.d32 = 0;
  21781. + gpwrdn.b.pwrdnrstn = 1;
  21782. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21783. + dwc_udelay(10);
  21784. +
  21785. + /* Disable PMU interrupt */
  21786. + gpwrdn.d32 = 0;
  21787. + gpwrdn.b.pmuintsel = 1;
  21788. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21789. +
  21790. + /* Mask interrupts from gpwrdn */
  21791. + gpwrdn.d32 = 0;
  21792. + gpwrdn.b.connect_det_msk = 1;
  21793. + gpwrdn.b.srp_det_msk = 1;
  21794. + gpwrdn.b.disconn_det_msk = 1;
  21795. + gpwrdn.b.rst_det_msk = 1;
  21796. + gpwrdn.b.lnstchng_msk = 1;
  21797. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21798. +
  21799. + /* Indicates that we are going out from hibernation */
  21800. + core_if->hibernation_suspend = 0;
  21801. +
  21802. + /*
  21803. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  21804. + * indicates restore from remote_wakeup
  21805. + */
  21806. + restore_essential_regs(core_if, rem_wakeup, 0);
  21807. +
  21808. + /*
  21809. + * Wait a little for seeing new value of variable hibernation_suspend if
  21810. + * Restore done interrupt received before polling
  21811. + */
  21812. + dwc_udelay(10);
  21813. +
  21814. + if (core_if->hibernation_suspend == 0) {
  21815. + /*
  21816. + * Wait For Restore_done Interrupt. This mechanism of polling the
  21817. + * interrupt is introduced to avoid any possible race conditions
  21818. + */
  21819. + do {
  21820. + gintsts_data_t gintsts;
  21821. + gintsts.d32 =
  21822. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  21823. + if (gintsts.b.restoredone) {
  21824. + gintsts.d32 = 0;
  21825. + gintsts.b.restoredone = 1;
  21826. + DWC_WRITE_REG32(&core_if->core_global_regs->
  21827. + gintsts, gintsts.d32);
  21828. + DWC_PRINTF("Restore Done Interrupt seen\n");
  21829. + break;
  21830. + }
  21831. + dwc_udelay(10);
  21832. + } while (--timeout);
  21833. + if (!timeout) {
  21834. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  21835. + }
  21836. + }
  21837. + /* Clear all pending interupts */
  21838. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21839. +
  21840. + /* De-assert Restore */
  21841. + gpwrdn.d32 = 0;
  21842. + gpwrdn.b.restore = 1;
  21843. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21844. + dwc_udelay(10);
  21845. +
  21846. + if (!rem_wakeup) {
  21847. + pcgcctl.d32 = 0;
  21848. + pcgcctl.b.rstpdwnmodule = 1;
  21849. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  21850. + }
  21851. +
  21852. + /* Restore GUSBCFG and DCFG */
  21853. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  21854. + core_if->gr_backup->gusbcfg_local);
  21855. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  21856. + core_if->dr_backup->dcfg);
  21857. +
  21858. + /* De-assert Wakeup Logic */
  21859. + gpwrdn.d32 = 0;
  21860. + gpwrdn.b.pmuactv = 1;
  21861. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21862. + dwc_udelay(10);
  21863. +
  21864. + if (!rem_wakeup) {
  21865. + /* Set Device programming done bit */
  21866. + dctl.b.pwronprgdone = 1;
  21867. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  21868. + } else {
  21869. + /* Start Remote Wakeup Signaling */
  21870. + dctl.d32 = core_if->dr_backup->dctl;
  21871. + dctl.b.rmtwkupsig = 1;
  21872. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  21873. + }
  21874. +
  21875. + dwc_mdelay(2);
  21876. + /* Clear all pending interupts */
  21877. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21878. +
  21879. + /* Restore global registers */
  21880. + dwc_otg_restore_global_regs(core_if);
  21881. + /* Restore device global registers */
  21882. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  21883. +
  21884. + if (rem_wakeup) {
  21885. + dwc_mdelay(7);
  21886. + dctl.d32 = 0;
  21887. + dctl.b.rmtwkupsig = 1;
  21888. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  21889. + }
  21890. +
  21891. + core_if->hibernation_suspend = 0;
  21892. + /* The core will be in ON STATE */
  21893. + core_if->lx_state = DWC_OTG_L0;
  21894. + DWC_PRINTF("Hibernation recovery completes here\n");
  21895. +
  21896. + return 1;
  21897. +}
  21898. +
  21899. +/*
  21900. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  21901. + * Hibernation. This function is for exiting from Host mode hibernation by
  21902. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  21903. + * @param core_if Programming view of DWC_otg controller.
  21904. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  21905. + * @param reset - indicates whether resume is initiated by Reset.
  21906. + */
  21907. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  21908. + int rem_wakeup, int reset)
  21909. +{
  21910. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  21911. + hprt0_data_t hprt0 = {.d32 = 0 };
  21912. +
  21913. + int timeout = 2000;
  21914. +
  21915. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  21916. + /* Switch-on voltage to the core */
  21917. + gpwrdn.b.pwrdnswtch = 1;
  21918. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21919. + dwc_udelay(10);
  21920. +
  21921. + /* Reset core */
  21922. + gpwrdn.d32 = 0;
  21923. + gpwrdn.b.pwrdnrstn = 1;
  21924. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21925. + dwc_udelay(10);
  21926. +
  21927. + /* Assert Restore signal */
  21928. + gpwrdn.d32 = 0;
  21929. + gpwrdn.b.restore = 1;
  21930. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21931. + dwc_udelay(10);
  21932. +
  21933. + /* Disable power clamps */
  21934. + gpwrdn.d32 = 0;
  21935. + gpwrdn.b.pwrdnclmp = 1;
  21936. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21937. +
  21938. + if (!rem_wakeup) {
  21939. + dwc_udelay(50);
  21940. + }
  21941. +
  21942. + /* Deassert Reset core */
  21943. + gpwrdn.d32 = 0;
  21944. + gpwrdn.b.pwrdnrstn = 1;
  21945. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21946. + dwc_udelay(10);
  21947. +
  21948. + /* Disable PMU interrupt */
  21949. + gpwrdn.d32 = 0;
  21950. + gpwrdn.b.pmuintsel = 1;
  21951. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21952. +
  21953. + gpwrdn.d32 = 0;
  21954. + gpwrdn.b.connect_det_msk = 1;
  21955. + gpwrdn.b.srp_det_msk = 1;
  21956. + gpwrdn.b.disconn_det_msk = 1;
  21957. + gpwrdn.b.rst_det_msk = 1;
  21958. + gpwrdn.b.lnstchng_msk = 1;
  21959. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21960. +
  21961. + /* Indicates that we are going out from hibernation */
  21962. + core_if->hibernation_suspend = 0;
  21963. +
  21964. + /* Set Restore Essential Regs bit in PCGCCTL register */
  21965. + restore_essential_regs(core_if, rem_wakeup, 1);
  21966. +
  21967. + /* Wait a little for seeing new value of variable hibernation_suspend if
  21968. + * Restore done interrupt received before polling */
  21969. + dwc_udelay(10);
  21970. +
  21971. + if (core_if->hibernation_suspend == 0) {
  21972. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  21973. + * interrupt is introduced to avoid any possible race conditions
  21974. + */
  21975. + do {
  21976. + gintsts_data_t gintsts;
  21977. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  21978. + if (gintsts.b.restoredone) {
  21979. + gintsts.d32 = 0;
  21980. + gintsts.b.restoredone = 1;
  21981. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  21982. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  21983. + break;
  21984. + }
  21985. + dwc_udelay(10);
  21986. + } while (--timeout);
  21987. + if (!timeout) {
  21988. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  21989. + }
  21990. + }
  21991. +
  21992. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  21993. + core_if->hibernation_suspend = 0;
  21994. +
  21995. + /* This step is not described in functional spec but if not wait for this
  21996. + * delay, mismatch interrupts occurred because just after restore core is
  21997. + * in Device mode(gintsts.curmode == 0) */
  21998. + dwc_mdelay(100);
  21999. +
  22000. + /* Clear all pending interrupts */
  22001. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22002. +
  22003. + /* De-assert Restore */
  22004. + gpwrdn.d32 = 0;
  22005. + gpwrdn.b.restore = 1;
  22006. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22007. + dwc_udelay(10);
  22008. +
  22009. + /* Restore GUSBCFG and HCFG */
  22010. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  22011. + core_if->gr_backup->gusbcfg_local);
  22012. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  22013. + core_if->hr_backup->hcfg_local);
  22014. +
  22015. + /* De-assert Wakeup Logic */
  22016. + gpwrdn.d32 = 0;
  22017. + gpwrdn.b.pmuactv = 1;
  22018. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22019. + dwc_udelay(10);
  22020. +
  22021. + /* Start the Resume operation by programming HPRT0 */
  22022. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22023. + hprt0.b.prtpwr = 1;
  22024. + hprt0.b.prtena = 0;
  22025. + hprt0.b.prtsusp = 0;
  22026. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22027. +
  22028. + DWC_PRINTF("Resume Starts Now\n");
  22029. + if (!reset) { // Indicates it is Resume Operation
  22030. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22031. + hprt0.b.prtres = 1;
  22032. + hprt0.b.prtpwr = 1;
  22033. + hprt0.b.prtena = 0;
  22034. + hprt0.b.prtsusp = 0;
  22035. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22036. +
  22037. + if (!rem_wakeup)
  22038. + hprt0.b.prtres = 0;
  22039. + /* Wait for Resume time and then program HPRT again */
  22040. + dwc_mdelay(100);
  22041. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22042. +
  22043. + } else { // Indicates it is Reset Operation
  22044. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22045. + hprt0.b.prtrst = 1;
  22046. + hprt0.b.prtpwr = 1;
  22047. + hprt0.b.prtena = 0;
  22048. + hprt0.b.prtsusp = 0;
  22049. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22050. + /* Wait for Reset time and then program HPRT again */
  22051. + dwc_mdelay(60);
  22052. + hprt0.b.prtrst = 0;
  22053. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22054. + }
  22055. + /* Clear all interrupt status */
  22056. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  22057. + hprt0.b.prtconndet = 1;
  22058. + hprt0.b.prtenchng = 1;
  22059. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22060. +
  22061. + /* Clear all pending interupts */
  22062. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22063. +
  22064. + /* Restore global registers */
  22065. + dwc_otg_restore_global_regs(core_if);
  22066. + /* Restore host global registers */
  22067. + dwc_otg_restore_host_regs(core_if, reset);
  22068. +
  22069. + /* The core will be in ON STATE */
  22070. + core_if->lx_state = DWC_OTG_L0;
  22071. + DWC_PRINTF("Hibernation recovery is complete here\n");
  22072. + return 0;
  22073. +}
  22074. +
  22075. +/** Saves some register values into system memory. */
  22076. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  22077. +{
  22078. + struct dwc_otg_global_regs_backup *gr;
  22079. + int i;
  22080. +
  22081. + gr = core_if->gr_backup;
  22082. + if (!gr) {
  22083. + gr = DWC_ALLOC(sizeof(*gr));
  22084. + if (!gr) {
  22085. + return -DWC_E_NO_MEMORY;
  22086. + }
  22087. + core_if->gr_backup = gr;
  22088. + }
  22089. +
  22090. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  22091. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  22092. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  22093. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  22094. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  22095. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  22096. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  22097. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22098. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  22099. +#endif
  22100. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  22101. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  22102. + gr->gdfifocfg_local =
  22103. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  22104. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  22105. + gr->dtxfsiz_local[i] =
  22106. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  22107. + }
  22108. +
  22109. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  22110. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  22111. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  22112. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  22113. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  22114. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  22115. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  22116. + gr->gnptxfsiz_local);
  22117. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  22118. + gr->hptxfsiz_local);
  22119. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22120. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  22121. +#endif
  22122. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  22123. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  22124. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  22125. +
  22126. + return 0;
  22127. +}
  22128. +
  22129. +/** Saves GINTMSK register before setting the msk bits. */
  22130. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  22131. +{
  22132. + struct dwc_otg_global_regs_backup *gr;
  22133. +
  22134. + gr = core_if->gr_backup;
  22135. + if (!gr) {
  22136. + gr = DWC_ALLOC(sizeof(*gr));
  22137. + if (!gr) {
  22138. + return -DWC_E_NO_MEMORY;
  22139. + }
  22140. + core_if->gr_backup = gr;
  22141. + }
  22142. +
  22143. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  22144. +
  22145. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  22146. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  22147. +
  22148. + return 0;
  22149. +}
  22150. +
  22151. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  22152. +{
  22153. + struct dwc_otg_dev_regs_backup *dr;
  22154. + int i;
  22155. +
  22156. + dr = core_if->dr_backup;
  22157. + if (!dr) {
  22158. + dr = DWC_ALLOC(sizeof(*dr));
  22159. + if (!dr) {
  22160. + return -DWC_E_NO_MEMORY;
  22161. + }
  22162. + core_if->dr_backup = dr;
  22163. + }
  22164. +
  22165. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  22166. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  22167. + dr->daintmsk =
  22168. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  22169. + dr->diepmsk =
  22170. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  22171. + dr->doepmsk =
  22172. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  22173. +
  22174. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22175. + dr->diepctl[i] =
  22176. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  22177. + dr->dieptsiz[i] =
  22178. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  22179. + dr->diepdma[i] =
  22180. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  22181. + }
  22182. +
  22183. + DWC_DEBUGPL(DBG_ANY,
  22184. + "=============Backing Host registers==============\n");
  22185. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  22186. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  22187. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  22188. + dr->daintmsk);
  22189. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  22190. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  22191. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22192. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  22193. + dr->diepctl[i]);
  22194. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  22195. + i, dr->dieptsiz[i]);
  22196. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  22197. + dr->diepdma[i]);
  22198. + }
  22199. +
  22200. + return 0;
  22201. +}
  22202. +
  22203. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  22204. +{
  22205. + struct dwc_otg_host_regs_backup *hr;
  22206. + int i;
  22207. +
  22208. + hr = core_if->hr_backup;
  22209. + if (!hr) {
  22210. + hr = DWC_ALLOC(sizeof(*hr));
  22211. + if (!hr) {
  22212. + return -DWC_E_NO_MEMORY;
  22213. + }
  22214. + core_if->hr_backup = hr;
  22215. + }
  22216. +
  22217. + hr->hcfg_local =
  22218. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  22219. + hr->haintmsk_local =
  22220. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  22221. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22222. + hr->hcintmsk_local[i] =
  22223. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  22224. + }
  22225. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  22226. + hr->hfir_local =
  22227. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  22228. +
  22229. + DWC_DEBUGPL(DBG_ANY,
  22230. + "=============Backing Host registers===============\n");
  22231. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  22232. + hr->hcfg_local);
  22233. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  22234. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22235. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  22236. + hr->hcintmsk_local[i]);
  22237. + }
  22238. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  22239. + hr->hprt0_local);
  22240. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  22241. + hr->hfir_local);
  22242. +
  22243. + return 0;
  22244. +}
  22245. +
  22246. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  22247. +{
  22248. + struct dwc_otg_global_regs_backup *gr;
  22249. + int i;
  22250. +
  22251. + gr = core_if->gr_backup;
  22252. + if (!gr) {
  22253. + return -DWC_E_INVALID;
  22254. + }
  22255. +
  22256. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  22257. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  22258. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  22259. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  22260. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  22261. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  22262. + gr->gnptxfsiz_local);
  22263. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  22264. + gr->hptxfsiz_local);
  22265. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  22266. + gr->gdfifocfg_local);
  22267. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  22268. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  22269. + gr->dtxfsiz_local[i]);
  22270. + }
  22271. +
  22272. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22273. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  22274. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  22275. + (gr->gahbcfg_local));
  22276. + return 0;
  22277. +}
  22278. +
  22279. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  22280. +{
  22281. + struct dwc_otg_dev_regs_backup *dr;
  22282. + int i;
  22283. +
  22284. + dr = core_if->dr_backup;
  22285. +
  22286. + if (!dr) {
  22287. + return -DWC_E_INVALID;
  22288. + }
  22289. +
  22290. + if (!rem_wakeup) {
  22291. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  22292. + dr->dctl);
  22293. + }
  22294. +
  22295. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  22296. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  22297. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  22298. +
  22299. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22300. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  22301. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  22302. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  22303. + }
  22304. +
  22305. + return 0;
  22306. +}
  22307. +
  22308. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  22309. +{
  22310. + struct dwc_otg_host_regs_backup *hr;
  22311. + int i;
  22312. + hr = core_if->hr_backup;
  22313. +
  22314. + if (!hr) {
  22315. + return -DWC_E_INVALID;
  22316. + }
  22317. +
  22318. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  22319. + //if (!reset)
  22320. + //{
  22321. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  22322. + //}
  22323. +
  22324. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  22325. + hr->haintmsk_local);
  22326. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22327. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  22328. + hr->hcintmsk_local[i]);
  22329. + }
  22330. +
  22331. + return 0;
  22332. +}
  22333. +
  22334. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  22335. +{
  22336. + struct dwc_otg_global_regs_backup *gr;
  22337. +
  22338. + gr = core_if->gr_backup;
  22339. +
  22340. + /* Restore values for LPM and I2C */
  22341. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22342. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  22343. +#endif
  22344. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  22345. +
  22346. + return 0;
  22347. +}
  22348. +
  22349. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  22350. +{
  22351. + struct dwc_otg_global_regs_backup *gr;
  22352. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  22353. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  22354. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  22355. + gintmsk_data_t gintmsk = {.d32 = 0 };
  22356. +
  22357. + /* Restore LPM and I2C registers */
  22358. + restore_lpm_i2c_regs(core_if);
  22359. +
  22360. + /* Set PCGCCTL to 0 */
  22361. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  22362. +
  22363. + gr = core_if->gr_backup;
  22364. + /* Load restore values for [31:14] bits */
  22365. + DWC_WRITE_REG32(core_if->pcgcctl,
  22366. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  22367. +
  22368. + /* Umnask global Interrupt in GAHBCFG and restore it */
  22369. + gahbcfg.d32 = gr->gahbcfg_local;
  22370. + gahbcfg.b.glblintrmsk = 1;
  22371. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  22372. +
  22373. + /* Clear all pending interupts */
  22374. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22375. +
  22376. + /* Unmask restore done interrupt */
  22377. + gintmsk.b.restoredone = 1;
  22378. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  22379. +
  22380. + /* Restore GUSBCFG and HCFG/DCFG */
  22381. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  22382. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  22383. +
  22384. + if (is_host) {
  22385. + hcfg_data_t hcfg = {.d32 = 0 };
  22386. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  22387. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  22388. + hcfg.d32);
  22389. +
  22390. + /* Load restore values for [31:14] bits */
  22391. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22392. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22393. +
  22394. + if (rmode)
  22395. + pcgcctl.b.restoremode = 1;
  22396. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22397. + dwc_udelay(10);
  22398. +
  22399. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  22400. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  22401. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22402. + pcgcctl.b.ess_reg_restored = 1;
  22403. + if (rmode)
  22404. + pcgcctl.b.restoremode = 1;
  22405. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22406. + } else {
  22407. + dcfg_data_t dcfg = {.d32 = 0 };
  22408. + dcfg.d32 = core_if->dr_backup->dcfg;
  22409. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  22410. +
  22411. + /* Load restore values for [31:14] bits */
  22412. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22413. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22414. + if (!rmode) {
  22415. + pcgcctl.d32 |= 0x208;
  22416. + }
  22417. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22418. + dwc_udelay(10);
  22419. +
  22420. + /* Load restore values for [31:14] bits */
  22421. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22422. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22423. + pcgcctl.b.ess_reg_restored = 1;
  22424. + if (!rmode)
  22425. + pcgcctl.d32 |= 0x208;
  22426. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22427. + }
  22428. +
  22429. + return 0;
  22430. +}
  22431. +
  22432. +/**
  22433. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  22434. + * type.
  22435. + */
  22436. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  22437. +{
  22438. + uint32_t val;
  22439. + hcfg_data_t hcfg;
  22440. +
  22441. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22442. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22443. + (core_if->core_params->ulpi_fs_ls)) ||
  22444. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22445. + /* Full speed PHY */
  22446. + val = DWC_HCFG_48_MHZ;
  22447. + } else {
  22448. + /* High speed PHY running at full speed or high speed */
  22449. + val = DWC_HCFG_30_60_MHZ;
  22450. + }
  22451. +
  22452. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  22453. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  22454. + hcfg.b.fslspclksel = val;
  22455. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  22456. +}
  22457. +
  22458. +/**
  22459. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  22460. + * and the enumeration speed of the device.
  22461. + */
  22462. +static void init_devspd(dwc_otg_core_if_t * core_if)
  22463. +{
  22464. + uint32_t val;
  22465. + dcfg_data_t dcfg;
  22466. +
  22467. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22468. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22469. + (core_if->core_params->ulpi_fs_ls)) ||
  22470. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22471. + /* Full speed PHY */
  22472. + val = 0x3;
  22473. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  22474. + /* High speed PHY running at full speed */
  22475. + val = 0x1;
  22476. + } else {
  22477. + /* High speed PHY running at high speed */
  22478. + val = 0x0;
  22479. + }
  22480. +
  22481. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  22482. +
  22483. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  22484. + dcfg.b.devspd = val;
  22485. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  22486. +}
  22487. +
  22488. +/**
  22489. + * This function calculates the number of IN EPS
  22490. + * using GHWCFG1 and GHWCFG2 registers values
  22491. + *
  22492. + * @param core_if Programming view of the DWC_otg controller
  22493. + */
  22494. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  22495. +{
  22496. + uint32_t num_in_eps = 0;
  22497. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  22498. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  22499. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  22500. + int i;
  22501. +
  22502. + for (i = 0; i < num_eps; ++i) {
  22503. + if (!(hwcfg1 & 0x1))
  22504. + num_in_eps++;
  22505. +
  22506. + hwcfg1 >>= 2;
  22507. + }
  22508. +
  22509. + if (core_if->hwcfg4.b.ded_fifo_en) {
  22510. + num_in_eps =
  22511. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  22512. + }
  22513. +
  22514. + return num_in_eps;
  22515. +}
  22516. +
  22517. +/**
  22518. + * This function calculates the number of OUT EPS
  22519. + * using GHWCFG1 and GHWCFG2 registers values
  22520. + *
  22521. + * @param core_if Programming view of the DWC_otg controller
  22522. + */
  22523. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  22524. +{
  22525. + uint32_t num_out_eps = 0;
  22526. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  22527. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  22528. + int i;
  22529. +
  22530. + for (i = 0; i < num_eps; ++i) {
  22531. + if (!(hwcfg1 & 0x1))
  22532. + num_out_eps++;
  22533. +
  22534. + hwcfg1 >>= 2;
  22535. + }
  22536. + return num_out_eps;
  22537. +}
  22538. +
  22539. +/**
  22540. + * This function initializes the DWC_otg controller registers and
  22541. + * prepares the core for device mode or host mode operation.
  22542. + *
  22543. + * @param core_if Programming view of the DWC_otg controller
  22544. + *
  22545. + */
  22546. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  22547. +{
  22548. + int i = 0;
  22549. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22550. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  22551. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  22552. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  22553. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  22554. +
  22555. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  22556. + core_if, global_regs);
  22557. +
  22558. + /* Common Initialization */
  22559. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22560. +
  22561. + /* Program the ULPI External VBUS bit if needed */
  22562. + usbcfg.b.ulpi_ext_vbus_drv =
  22563. + (core_if->core_params->phy_ulpi_ext_vbus ==
  22564. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  22565. +
  22566. + /* Set external TS Dline pulsing */
  22567. + usbcfg.b.term_sel_dl_pulse =
  22568. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  22569. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22570. +
  22571. + /* Reset the Controller */
  22572. + dwc_otg_core_reset(core_if);
  22573. +
  22574. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  22575. + core_if->power_down = core_if->core_params->power_down;
  22576. + core_if->otg_sts = 0;
  22577. +
  22578. + /* Initialize parameters from Hardware configuration registers. */
  22579. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  22580. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  22581. +
  22582. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  22583. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  22584. +
  22585. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  22586. + dev_if->perio_tx_fifo_size[i] =
  22587. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  22588. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  22589. + i, dev_if->perio_tx_fifo_size[i]);
  22590. + }
  22591. +
  22592. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  22593. + dev_if->tx_fifo_size[i] =
  22594. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  22595. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  22596. + i, dev_if->tx_fifo_size[i]);
  22597. + }
  22598. +
  22599. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  22600. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  22601. + core_if->nperio_tx_fifo_size =
  22602. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  22603. +
  22604. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  22605. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  22606. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  22607. + core_if->nperio_tx_fifo_size);
  22608. +
  22609. + /* This programming sequence needs to happen in FS mode before any other
  22610. + * programming occurs */
  22611. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  22612. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22613. + /* If FS mode with FS PHY */
  22614. +
  22615. + /* core_init() is now called on every switch so only call the
  22616. + * following for the first time through. */
  22617. + if (!core_if->phy_init_done) {
  22618. + core_if->phy_init_done = 1;
  22619. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  22620. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22621. + usbcfg.b.physel = 1;
  22622. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22623. +
  22624. + /* Reset after a PHY select */
  22625. + dwc_otg_core_reset(core_if);
  22626. + }
  22627. +
  22628. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  22629. + * do this on HNP Dev/Host mode switches (done in dev_init and
  22630. + * host_init). */
  22631. + if (dwc_otg_is_host_mode(core_if)) {
  22632. + init_fslspclksel(core_if);
  22633. + } else {
  22634. + init_devspd(core_if);
  22635. + }
  22636. +
  22637. + if (core_if->core_params->i2c_enable) {
  22638. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  22639. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  22640. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22641. + usbcfg.b.otgutmifssel = 1;
  22642. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22643. +
  22644. + /* Program GI2CCTL.I2CEn */
  22645. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  22646. + i2cctl.b.i2cdevaddr = 1;
  22647. + i2cctl.b.i2cen = 0;
  22648. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  22649. + i2cctl.b.i2cen = 1;
  22650. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  22651. + }
  22652. +
  22653. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  22654. + else {
  22655. + /* High speed PHY. */
  22656. + if (!core_if->phy_init_done) {
  22657. + core_if->phy_init_done = 1;
  22658. + /* HS PHY parameters. These parameters are preserved
  22659. + * during soft reset so only program the first time. Do
  22660. + * a soft reset immediately after setting phyif. */
  22661. +
  22662. + if (core_if->core_params->phy_type == 2) {
  22663. + /* ULPI interface */
  22664. + usbcfg.b.ulpi_utmi_sel = 1;
  22665. + usbcfg.b.phyif = 0;
  22666. + usbcfg.b.ddrsel =
  22667. + core_if->core_params->phy_ulpi_ddr;
  22668. + } else if (core_if->core_params->phy_type == 1) {
  22669. + /* UTMI+ interface */
  22670. + usbcfg.b.ulpi_utmi_sel = 0;
  22671. + if (core_if->core_params->phy_utmi_width == 16) {
  22672. + usbcfg.b.phyif = 1;
  22673. +
  22674. + } else {
  22675. + usbcfg.b.phyif = 0;
  22676. + }
  22677. + } else {
  22678. + DWC_ERROR("FS PHY TYPE\n");
  22679. + }
  22680. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22681. + /* Reset after setting the PHY parameters */
  22682. + dwc_otg_core_reset(core_if);
  22683. + }
  22684. + }
  22685. +
  22686. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22687. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22688. + (core_if->core_params->ulpi_fs_ls)) {
  22689. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  22690. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22691. + usbcfg.b.ulpi_fsls = 1;
  22692. + usbcfg.b.ulpi_clk_sus_m = 1;
  22693. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22694. + } else {
  22695. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22696. + usbcfg.b.ulpi_fsls = 0;
  22697. + usbcfg.b.ulpi_clk_sus_m = 0;
  22698. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22699. + }
  22700. +
  22701. + /* Program the GAHBCFG Register. */
  22702. + switch (core_if->hwcfg2.b.architecture) {
  22703. +
  22704. + case DWC_SLAVE_ONLY_ARCH:
  22705. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  22706. + ahbcfg.b.nptxfemplvl_txfemplvl =
  22707. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  22708. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  22709. + core_if->dma_enable = 0;
  22710. + core_if->dma_desc_enable = 0;
  22711. + break;
  22712. +
  22713. + case DWC_EXT_DMA_ARCH:
  22714. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  22715. + {
  22716. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  22717. + ahbcfg.b.hburstlen = 0;
  22718. + while (brst_sz > 1) {
  22719. + ahbcfg.b.hburstlen++;
  22720. + brst_sz >>= 1;
  22721. + }
  22722. + }
  22723. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  22724. + core_if->dma_desc_enable =
  22725. + (core_if->core_params->dma_desc_enable != 0);
  22726. + break;
  22727. +
  22728. + case DWC_INT_DMA_ARCH:
  22729. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  22730. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  22731. + Host mode ISOC in issue fix - vahrama */
  22732. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  22733. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  22734. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  22735. + core_if->dma_desc_enable =
  22736. + (core_if->core_params->dma_desc_enable != 0);
  22737. + break;
  22738. +
  22739. + }
  22740. + if (core_if->dma_enable) {
  22741. + if (core_if->dma_desc_enable) {
  22742. + DWC_PRINTF("Using Descriptor DMA mode\n");
  22743. + } else {
  22744. + DWC_PRINTF("Using Buffer DMA mode\n");
  22745. +
  22746. + }
  22747. + } else {
  22748. + DWC_PRINTF("Using Slave mode\n");
  22749. + core_if->dma_desc_enable = 0;
  22750. + }
  22751. +
  22752. + if (core_if->core_params->ahb_single) {
  22753. + ahbcfg.b.ahbsingle = 1;
  22754. + }
  22755. +
  22756. + ahbcfg.b.dmaenable = core_if->dma_enable;
  22757. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  22758. +
  22759. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  22760. +
  22761. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  22762. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  22763. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  22764. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  22765. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  22766. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  22767. +
  22768. + /*
  22769. + * Program the GUSBCFG register.
  22770. + */
  22771. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22772. +
  22773. + switch (core_if->hwcfg2.b.op_mode) {
  22774. + case DWC_MODE_HNP_SRP_CAPABLE:
  22775. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  22776. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  22777. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22778. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22779. + break;
  22780. +
  22781. + case DWC_MODE_SRP_ONLY_CAPABLE:
  22782. + usbcfg.b.hnpcap = 0;
  22783. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22784. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22785. + break;
  22786. +
  22787. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  22788. + usbcfg.b.hnpcap = 0;
  22789. + usbcfg.b.srpcap = 0;
  22790. + break;
  22791. +
  22792. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  22793. + usbcfg.b.hnpcap = 0;
  22794. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22795. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22796. + break;
  22797. +
  22798. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  22799. + usbcfg.b.hnpcap = 0;
  22800. + usbcfg.b.srpcap = 0;
  22801. + break;
  22802. +
  22803. + case DWC_MODE_SRP_CAPABLE_HOST:
  22804. + usbcfg.b.hnpcap = 0;
  22805. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22806. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22807. + break;
  22808. +
  22809. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  22810. + usbcfg.b.hnpcap = 0;
  22811. + usbcfg.b.srpcap = 0;
  22812. + break;
  22813. + }
  22814. +
  22815. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22816. +
  22817. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22818. + if (core_if->core_params->lpm_enable) {
  22819. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  22820. +
  22821. + /* To enable LPM support set lpm_cap_en bit */
  22822. + lpmcfg.b.lpm_cap_en = 1;
  22823. +
  22824. + /* Make AppL1Res ACK */
  22825. + lpmcfg.b.appl_resp = 1;
  22826. +
  22827. + /* Retry 3 times */
  22828. + lpmcfg.b.retry_count = 3;
  22829. +
  22830. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  22831. + 0, lpmcfg.d32);
  22832. +
  22833. + }
  22834. +#endif
  22835. + if (core_if->core_params->ic_usb_cap) {
  22836. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  22837. + gusbcfg.b.ic_usb_cap = 1;
  22838. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  22839. + 0, gusbcfg.d32);
  22840. + }
  22841. + {
  22842. + gotgctl_data_t gotgctl = {.d32 = 0 };
  22843. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  22844. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  22845. + gotgctl.d32);
  22846. + /* Set OTG version supported */
  22847. + core_if->otg_ver = core_if->core_params->otg_ver;
  22848. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  22849. + core_if->core_params->otg_ver, core_if->otg_ver);
  22850. + }
  22851. +
  22852. +
  22853. + /* Enable common interrupts */
  22854. + dwc_otg_enable_common_interrupts(core_if);
  22855. +
  22856. + /* Do device or host intialization based on mode during PCD
  22857. + * and HCD initialization */
  22858. + if (dwc_otg_is_host_mode(core_if)) {
  22859. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  22860. + core_if->op_state = A_HOST;
  22861. + } else {
  22862. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  22863. + core_if->op_state = B_PERIPHERAL;
  22864. +#ifdef DWC_DEVICE_ONLY
  22865. + dwc_otg_core_dev_init(core_if);
  22866. +#endif
  22867. + }
  22868. +}
  22869. +
  22870. +/**
  22871. + * This function enables the Device mode interrupts.
  22872. + *
  22873. + * @param core_if Programming view of DWC_otg controller
  22874. + */
  22875. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  22876. +{
  22877. + gintmsk_data_t intr_mask = {.d32 = 0 };
  22878. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22879. +
  22880. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  22881. +
  22882. + /* Disable all interrupts. */
  22883. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  22884. +
  22885. + /* Clear any pending interrupts */
  22886. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  22887. +
  22888. + /* Enable the common interrupts */
  22889. + dwc_otg_enable_common_interrupts(core_if);
  22890. +
  22891. + /* Enable interrupts */
  22892. + intr_mask.b.usbreset = 1;
  22893. + intr_mask.b.enumdone = 1;
  22894. + /* Disable Disconnect interrupt in Device mode */
  22895. + intr_mask.b.disconnect = 0;
  22896. +
  22897. + if (!core_if->multiproc_int_enable) {
  22898. + intr_mask.b.inepintr = 1;
  22899. + intr_mask.b.outepintr = 1;
  22900. + }
  22901. +
  22902. + intr_mask.b.erlysuspend = 1;
  22903. +
  22904. + if (core_if->en_multiple_tx_fifo == 0) {
  22905. + intr_mask.b.epmismatch = 1;
  22906. + }
  22907. +
  22908. + //intr_mask.b.incomplisoout = 1;
  22909. + intr_mask.b.incomplisoin = 1;
  22910. +
  22911. +/* Enable the ignore frame number for ISOC xfers - MAS */
  22912. +/* Disable to support high bandwith ISOC transfers - manukz */
  22913. +#if 0
  22914. +#ifdef DWC_UTE_PER_IO
  22915. + if (core_if->dma_enable) {
  22916. + if (core_if->dma_desc_enable) {
  22917. + dctl_data_t dctl1 = {.d32 = 0 };
  22918. + dctl1.b.ifrmnum = 1;
  22919. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  22920. + dctl, 0, dctl1.d32);
  22921. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  22922. + DWC_READ_REG32(&core_if->dev_if->
  22923. + dev_global_regs->dctl));
  22924. + }
  22925. + }
  22926. +#endif
  22927. +#endif
  22928. +#ifdef DWC_EN_ISOC
  22929. + if (core_if->dma_enable) {
  22930. + if (core_if->dma_desc_enable == 0) {
  22931. + if (core_if->pti_enh_enable) {
  22932. + dctl_data_t dctl = {.d32 = 0 };
  22933. + dctl.b.ifrmnum = 1;
  22934. + DWC_MODIFY_REG32(&core_if->
  22935. + dev_if->dev_global_regs->dctl,
  22936. + 0, dctl.d32);
  22937. + } else {
  22938. + intr_mask.b.incomplisoin = 1;
  22939. + intr_mask.b.incomplisoout = 1;
  22940. + }
  22941. + }
  22942. + } else {
  22943. + intr_mask.b.incomplisoin = 1;
  22944. + intr_mask.b.incomplisoout = 1;
  22945. + }
  22946. +#endif /* DWC_EN_ISOC */
  22947. +
  22948. + /** @todo NGS: Should this be a module parameter? */
  22949. +#ifdef USE_PERIODIC_EP
  22950. + intr_mask.b.isooutdrop = 1;
  22951. + intr_mask.b.eopframe = 1;
  22952. + intr_mask.b.incomplisoin = 1;
  22953. + intr_mask.b.incomplisoout = 1;
  22954. +#endif
  22955. +
  22956. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  22957. +
  22958. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  22959. + DWC_READ_REG32(&global_regs->gintmsk));
  22960. +}
  22961. +
  22962. +/**
  22963. + * This function initializes the DWC_otg controller registers for
  22964. + * device mode.
  22965. + *
  22966. + * @param core_if Programming view of DWC_otg controller
  22967. + *
  22968. + */
  22969. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  22970. +{
  22971. + int i;
  22972. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22973. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  22974. + dwc_otg_core_params_t *params = core_if->core_params;
  22975. + dcfg_data_t dcfg = {.d32 = 0 };
  22976. + depctl_data_t diepctl = {.d32 = 0 };
  22977. + grstctl_t resetctl = {.d32 = 0 };
  22978. + uint32_t rx_fifo_size;
  22979. + fifosize_data_t nptxfifosize;
  22980. + fifosize_data_t txfifosize;
  22981. + dthrctl_data_t dthrctl;
  22982. + fifosize_data_t ptxfifosize;
  22983. + uint16_t rxfsiz, nptxfsiz;
  22984. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  22985. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  22986. +
  22987. + /* Restart the Phy Clock */
  22988. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  22989. +
  22990. + /* Device configuration register */
  22991. + init_devspd(core_if);
  22992. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  22993. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  22994. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  22995. + /* Enable Device OUT NAK in case of DDMA mode*/
  22996. + if (core_if->core_params->dev_out_nak) {
  22997. + dcfg.b.endevoutnak = 1;
  22998. + }
  22999. +
  23000. + if (core_if->core_params->cont_on_bna) {
  23001. + dctl_data_t dctl = {.d32 = 0 };
  23002. + dctl.b.encontonbna = 1;
  23003. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23004. + }
  23005. +
  23006. +
  23007. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  23008. +
  23009. + /* Configure data FIFO sizes */
  23010. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  23011. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  23012. + core_if->total_fifo_size);
  23013. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  23014. + params->dev_rx_fifo_size);
  23015. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  23016. + params->dev_nperio_tx_fifo_size);
  23017. +
  23018. + /* Rx FIFO */
  23019. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  23020. + DWC_READ_REG32(&global_regs->grxfsiz));
  23021. +
  23022. +#ifdef DWC_UTE_CFI
  23023. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  23024. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  23025. +#endif
  23026. + rx_fifo_size = params->dev_rx_fifo_size;
  23027. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  23028. +
  23029. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  23030. + DWC_READ_REG32(&global_regs->grxfsiz));
  23031. +
  23032. + /** Set Periodic Tx FIFO Mask all bits 0 */
  23033. + core_if->p_tx_msk = 0;
  23034. +
  23035. + /** Set Tx FIFO Mask all bits 0 */
  23036. + core_if->tx_msk = 0;
  23037. +
  23038. + if (core_if->en_multiple_tx_fifo == 0) {
  23039. + /* Non-periodic Tx FIFO */
  23040. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23041. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23042. +
  23043. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  23044. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  23045. +
  23046. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  23047. + nptxfifosize.d32);
  23048. +
  23049. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23050. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23051. +
  23052. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  23053. + /*
  23054. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  23055. + * Indexes of the FIFO size module parameters in the
  23056. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  23057. + * the dptxfsiz array run from 0 to 14.
  23058. + */
  23059. + /** @todo Finish debug of this */
  23060. + ptxfifosize.b.startaddr =
  23061. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23062. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  23063. + ptxfifosize.b.depth =
  23064. + params->dev_perio_tx_fifo_size[i];
  23065. + DWC_DEBUGPL(DBG_CIL,
  23066. + "initial dtxfsiz[%d]=%08x\n", i,
  23067. + DWC_READ_REG32(&global_regs->dtxfsiz
  23068. + [i]));
  23069. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  23070. + ptxfifosize.d32);
  23071. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  23072. + i,
  23073. + DWC_READ_REG32(&global_regs->dtxfsiz
  23074. + [i]));
  23075. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  23076. + }
  23077. + } else {
  23078. + /*
  23079. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  23080. + * Indexes of the FIFO size module parameters in the
  23081. + * dev_tx_fifo_size array and the FIFO size registers in
  23082. + * the dtxfsiz array run from 0 to 14.
  23083. + */
  23084. +
  23085. + /* Non-periodic Tx FIFO */
  23086. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23087. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23088. +
  23089. +#ifdef DWC_UTE_CFI
  23090. + core_if->pwron_gnptxfsiz =
  23091. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23092. + core_if->init_gnptxfsiz =
  23093. + params->dev_nperio_tx_fifo_size;
  23094. +#endif
  23095. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  23096. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  23097. +
  23098. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  23099. + nptxfifosize.d32);
  23100. +
  23101. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23102. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23103. +
  23104. + txfifosize.b.startaddr =
  23105. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23106. +
  23107. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  23108. +
  23109. + txfifosize.b.depth =
  23110. + params->dev_tx_fifo_size[i];
  23111. +
  23112. + DWC_DEBUGPL(DBG_CIL,
  23113. + "initial dtxfsiz[%d]=%08x\n",
  23114. + i,
  23115. + DWC_READ_REG32(&global_regs->dtxfsiz
  23116. + [i]));
  23117. +
  23118. +#ifdef DWC_UTE_CFI
  23119. + core_if->pwron_txfsiz[i] =
  23120. + (DWC_READ_REG32
  23121. + (&global_regs->dtxfsiz[i]) >> 16);
  23122. + core_if->init_txfsiz[i] =
  23123. + params->dev_tx_fifo_size[i];
  23124. +#endif
  23125. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  23126. + txfifosize.d32);
  23127. +
  23128. + DWC_DEBUGPL(DBG_CIL,
  23129. + "new dtxfsiz[%d]=%08x\n",
  23130. + i,
  23131. + DWC_READ_REG32(&global_regs->dtxfsiz
  23132. + [i]));
  23133. +
  23134. + txfifosize.b.startaddr += txfifosize.b.depth;
  23135. + }
  23136. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  23137. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  23138. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  23139. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  23140. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  23141. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23142. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  23143. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23144. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  23145. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23146. + }
  23147. + }
  23148. +
  23149. + /* Flush the FIFOs */
  23150. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  23151. + dwc_otg_flush_rx_fifo(core_if);
  23152. +
  23153. + /* Flush the Learning Queue. */
  23154. + resetctl.b.intknqflsh = 1;
  23155. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  23156. +
  23157. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  23158. + core_if->start_predict = 0;
  23159. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  23160. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  23161. + }
  23162. + core_if->nextep_seq[0] = 0;
  23163. + core_if->first_in_nextep_seq = 0;
  23164. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  23165. + diepctl.b.nextep = 0;
  23166. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  23167. +
  23168. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  23169. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  23170. + dcfg.b.epmscnt = 2;
  23171. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  23172. +
  23173. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  23174. + __func__, core_if->first_in_nextep_seq);
  23175. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  23176. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  23177. + }
  23178. + DWC_DEBUGPL(DBG_CILV,"\n");
  23179. + }
  23180. +
  23181. + /* Clear all pending Device Interrupts */
  23182. + /** @todo - if the condition needed to be checked
  23183. + * or in any case all pending interrutps should be cleared?
  23184. + */
  23185. + if (core_if->multiproc_int_enable) {
  23186. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  23187. + DWC_WRITE_REG32(&dev_if->
  23188. + dev_global_regs->diepeachintmsk[i], 0);
  23189. + }
  23190. + }
  23191. +
  23192. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  23193. + DWC_WRITE_REG32(&dev_if->
  23194. + dev_global_regs->doepeachintmsk[i], 0);
  23195. + }
  23196. +
  23197. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  23198. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  23199. + } else {
  23200. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  23201. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  23202. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  23203. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  23204. + }
  23205. +
  23206. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  23207. + depctl_data_t depctl;
  23208. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  23209. + if (depctl.b.epena) {
  23210. + depctl.d32 = 0;
  23211. + depctl.b.epdis = 1;
  23212. + depctl.b.snak = 1;
  23213. + } else {
  23214. + depctl.d32 = 0;
  23215. + }
  23216. +
  23217. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  23218. +
  23219. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  23220. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  23221. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  23222. + }
  23223. +
  23224. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  23225. + depctl_data_t depctl;
  23226. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  23227. + if (depctl.b.epena) {
  23228. + dctl_data_t dctl = {.d32 = 0 };
  23229. + gintmsk_data_t gintsts = {.d32 = 0 };
  23230. + doepint_data_t doepint = {.d32 = 0 };
  23231. + dctl.b.sgoutnak = 1;
  23232. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23233. + do {
  23234. + dwc_udelay(10);
  23235. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  23236. + } while (!gintsts.b.goutnakeff);
  23237. + gintsts.d32 = 0;
  23238. + gintsts.b.goutnakeff = 1;
  23239. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  23240. +
  23241. + depctl.d32 = 0;
  23242. + depctl.b.epdis = 1;
  23243. + depctl.b.snak = 1;
  23244. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  23245. + do {
  23246. + dwc_udelay(10);
  23247. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  23248. + out_ep_regs[i]->doepint);
  23249. + } while (!doepint.b.epdisabled);
  23250. +
  23251. + doepint.b.epdisabled = 1;
  23252. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  23253. +
  23254. + dctl.d32 = 0;
  23255. + dctl.b.cgoutnak = 1;
  23256. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23257. + } else {
  23258. + depctl.d32 = 0;
  23259. + }
  23260. +
  23261. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  23262. +
  23263. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  23264. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  23265. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  23266. + }
  23267. +
  23268. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  23269. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  23270. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  23271. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  23272. +
  23273. + dev_if->rx_thr_length = params->rx_thr_length;
  23274. + dev_if->tx_thr_length = params->tx_thr_length;
  23275. +
  23276. + dev_if->setup_desc_index = 0;
  23277. +
  23278. + dthrctl.d32 = 0;
  23279. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  23280. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  23281. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  23282. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  23283. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  23284. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  23285. +
  23286. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  23287. + dthrctl.d32);
  23288. +
  23289. + DWC_DEBUGPL(DBG_CIL,
  23290. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  23291. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  23292. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  23293. + dthrctl.b.rx_thr_len);
  23294. +
  23295. + }
  23296. +
  23297. + dwc_otg_enable_device_interrupts(core_if);
  23298. +
  23299. + {
  23300. + diepmsk_data_t msk = {.d32 = 0 };
  23301. + msk.b.txfifoundrn = 1;
  23302. + if (core_if->multiproc_int_enable) {
  23303. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  23304. + diepeachintmsk[0], msk.d32, msk.d32);
  23305. + } else {
  23306. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  23307. + msk.d32, msk.d32);
  23308. + }
  23309. + }
  23310. +
  23311. + if (core_if->multiproc_int_enable) {
  23312. + /* Set NAK on Babble */
  23313. + dctl_data_t dctl = {.d32 = 0 };
  23314. + dctl.b.nakonbble = 1;
  23315. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23316. + }
  23317. +
  23318. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  23319. + dctl_data_t dctl = {.d32 = 0 };
  23320. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  23321. + dctl.b.sftdiscon = 0;
  23322. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  23323. + }
  23324. +}
  23325. +
  23326. +/**
  23327. + * This function enables the Host mode interrupts.
  23328. + *
  23329. + * @param core_if Programming view of DWC_otg controller
  23330. + */
  23331. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  23332. +{
  23333. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23334. + gintmsk_data_t intr_mask = {.d32 = 0 };
  23335. +
  23336. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  23337. +
  23338. + /* Disable all interrupts. */
  23339. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  23340. +
  23341. + /* Clear any pending interrupts. */
  23342. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  23343. +
  23344. + /* Enable the common interrupts */
  23345. + dwc_otg_enable_common_interrupts(core_if);
  23346. +
  23347. + /*
  23348. + * Enable host mode interrupts without disturbing common
  23349. + * interrupts.
  23350. + */
  23351. +
  23352. + intr_mask.b.disconnect = 1;
  23353. + intr_mask.b.portintr = 1;
  23354. + intr_mask.b.hcintr = 1;
  23355. +
  23356. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  23357. +}
  23358. +
  23359. +/**
  23360. + * This function disables the Host Mode interrupts.
  23361. + *
  23362. + * @param core_if Programming view of DWC_otg controller
  23363. + */
  23364. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  23365. +{
  23366. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23367. + gintmsk_data_t intr_mask = {.d32 = 0 };
  23368. +
  23369. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  23370. +
  23371. + /*
  23372. + * Disable host mode interrupts without disturbing common
  23373. + * interrupts.
  23374. + */
  23375. + intr_mask.b.sofintr = 1;
  23376. + intr_mask.b.portintr = 1;
  23377. + intr_mask.b.hcintr = 1;
  23378. + intr_mask.b.ptxfempty = 1;
  23379. + intr_mask.b.nptxfempty = 1;
  23380. +
  23381. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  23382. +}
  23383. +
  23384. +/**
  23385. + * This function initializes the DWC_otg controller registers for
  23386. + * host mode.
  23387. + *
  23388. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  23389. + * request queues. Host channels are reset to ensure that they are ready for
  23390. + * performing transfers.
  23391. + *
  23392. + * @param core_if Programming view of DWC_otg controller
  23393. + *
  23394. + */
  23395. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  23396. +{
  23397. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23398. + dwc_otg_host_if_t *host_if = core_if->host_if;
  23399. + dwc_otg_core_params_t *params = core_if->core_params;
  23400. + hprt0_data_t hprt0 = {.d32 = 0 };
  23401. + fifosize_data_t nptxfifosize;
  23402. + fifosize_data_t ptxfifosize;
  23403. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  23404. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  23405. + int i;
  23406. + hcchar_data_t hcchar;
  23407. + hcfg_data_t hcfg;
  23408. + hfir_data_t hfir;
  23409. + dwc_otg_hc_regs_t *hc_regs;
  23410. + int num_channels;
  23411. + gotgctl_data_t gotgctl = {.d32 = 0 };
  23412. +
  23413. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  23414. +
  23415. + /* Restart the Phy Clock */
  23416. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  23417. +
  23418. + /* Initialize Host Configuration Register */
  23419. + init_fslspclksel(core_if);
  23420. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  23421. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  23422. + hcfg.b.fslssupp = 1;
  23423. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  23424. +
  23425. + }
  23426. +
  23427. + /* This bit allows dynamic reloading of the HFIR register
  23428. + * during runtime. This bit needs to be programmed during
  23429. + * initial configuration and its value must not be changed
  23430. + * during runtime.*/
  23431. + if (core_if->core_params->reload_ctl == 1) {
  23432. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  23433. + hfir.b.hfirrldctrl = 1;
  23434. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  23435. + }
  23436. +
  23437. + if (core_if->core_params->dma_desc_enable) {
  23438. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  23439. + if (!
  23440. + (core_if->hwcfg4.b.desc_dma
  23441. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  23442. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  23443. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  23444. + || (op_mode ==
  23445. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  23446. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  23447. + || (op_mode ==
  23448. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  23449. +
  23450. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  23451. + "Either core version is below 2.90a or "
  23452. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  23453. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  23454. + "module parameter to 0.\n");
  23455. + return;
  23456. + }
  23457. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  23458. + hcfg.b.descdma = 1;
  23459. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  23460. + }
  23461. +
  23462. + /* Configure data FIFO sizes */
  23463. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  23464. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  23465. + core_if->total_fifo_size);
  23466. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  23467. + params->host_rx_fifo_size);
  23468. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  23469. + params->host_nperio_tx_fifo_size);
  23470. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  23471. + params->host_perio_tx_fifo_size);
  23472. +
  23473. + /* Rx FIFO */
  23474. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  23475. + DWC_READ_REG32(&global_regs->grxfsiz));
  23476. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  23477. + params->host_rx_fifo_size);
  23478. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  23479. + DWC_READ_REG32(&global_regs->grxfsiz));
  23480. +
  23481. + /* Non-periodic Tx FIFO */
  23482. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23483. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23484. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  23485. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  23486. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  23487. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23488. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23489. +
  23490. + /* Periodic Tx FIFO */
  23491. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  23492. + DWC_READ_REG32(&global_regs->hptxfsiz));
  23493. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  23494. + ptxfifosize.b.startaddr =
  23495. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23496. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  23497. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  23498. + DWC_READ_REG32(&global_regs->hptxfsiz));
  23499. +
  23500. + if (core_if->en_multiple_tx_fifo
  23501. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  23502. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  23503. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  23504. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  23505. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23506. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  23507. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  23508. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23509. + }
  23510. + }
  23511. +
  23512. + /* TODO - check this */
  23513. + /* Clear Host Set HNP Enable in the OTG Control Register */
  23514. + gotgctl.b.hstsethnpen = 1;
  23515. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  23516. + /* Make sure the FIFOs are flushed. */
  23517. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  23518. + dwc_otg_flush_rx_fifo(core_if);
  23519. +
  23520. + /* Clear Host Set HNP Enable in the OTG Control Register */
  23521. + gotgctl.b.hstsethnpen = 1;
  23522. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  23523. +
  23524. + if (!core_if->core_params->dma_desc_enable) {
  23525. + /* Flush out any leftover queued requests. */
  23526. + num_channels = core_if->core_params->host_channels;
  23527. +
  23528. + for (i = 0; i < num_channels; i++) {
  23529. + hc_regs = core_if->host_if->hc_regs[i];
  23530. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23531. + hcchar.b.chen = 0;
  23532. + hcchar.b.chdis = 1;
  23533. + hcchar.b.epdir = 0;
  23534. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23535. + }
  23536. +
  23537. + /* Halt all channels to put them into a known state. */
  23538. + for (i = 0; i < num_channels; i++) {
  23539. + int count = 0;
  23540. + hc_regs = core_if->host_if->hc_regs[i];
  23541. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23542. + hcchar.b.chen = 1;
  23543. + hcchar.b.chdis = 1;
  23544. + hcchar.b.epdir = 0;
  23545. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23546. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  23547. + do {
  23548. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23549. + if (++count > 1000) {
  23550. + DWC_ERROR
  23551. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  23552. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  23553. + break;
  23554. + }
  23555. + dwc_udelay(1);
  23556. + } while (hcchar.b.chen);
  23557. + }
  23558. + }
  23559. +
  23560. + /* Turn on the vbus power. */
  23561. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  23562. + if (core_if->op_state == A_HOST) {
  23563. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  23564. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  23565. + if (hprt0.b.prtpwr == 0) {
  23566. + hprt0.b.prtpwr = 1;
  23567. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  23568. + }
  23569. + }
  23570. +
  23571. + dwc_otg_enable_host_interrupts(core_if);
  23572. +}
  23573. +
  23574. +/**
  23575. + * Prepares a host channel for transferring packets to/from a specific
  23576. + * endpoint. The HCCHARn register is set up with the characteristics specified
  23577. + * in _hc. Host channel interrupts that may need to be serviced while this
  23578. + * transfer is in progress are enabled.
  23579. + *
  23580. + * @param core_if Programming view of DWC_otg controller
  23581. + * @param hc Information needed to initialize the host channel
  23582. + */
  23583. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23584. +{
  23585. + hcintmsk_data_t hc_intr_mask;
  23586. + hcchar_data_t hcchar;
  23587. + hcsplt_data_t hcsplt;
  23588. +
  23589. + uint8_t hc_num = hc->hc_num;
  23590. + dwc_otg_host_if_t *host_if = core_if->host_if;
  23591. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  23592. +
  23593. + /* Clear old interrupt conditions for this host channel. */
  23594. + hc_intr_mask.d32 = 0xFFFFFFFF;
  23595. + hc_intr_mask.b.reserved14_31 = 0;
  23596. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  23597. +
  23598. + /* Enable channel interrupts required for this transfer. */
  23599. + hc_intr_mask.d32 = 0;
  23600. + hc_intr_mask.b.chhltd = 1;
  23601. + if (core_if->dma_enable) {
  23602. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  23603. + if (!core_if->dma_desc_enable)
  23604. + hc_intr_mask.b.ahberr = 1;
  23605. + else {
  23606. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  23607. + hc_intr_mask.b.xfercompl = 1;
  23608. + }
  23609. +
  23610. + if (hc->error_state && !hc->do_split &&
  23611. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  23612. + hc_intr_mask.b.ack = 1;
  23613. + if (hc->ep_is_in) {
  23614. + hc_intr_mask.b.datatglerr = 1;
  23615. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  23616. + hc_intr_mask.b.nak = 1;
  23617. + }
  23618. + }
  23619. + }
  23620. + } else {
  23621. + switch (hc->ep_type) {
  23622. + case DWC_OTG_EP_TYPE_CONTROL:
  23623. + case DWC_OTG_EP_TYPE_BULK:
  23624. + hc_intr_mask.b.xfercompl = 1;
  23625. + hc_intr_mask.b.stall = 1;
  23626. + hc_intr_mask.b.xacterr = 1;
  23627. + hc_intr_mask.b.datatglerr = 1;
  23628. + if (hc->ep_is_in) {
  23629. + hc_intr_mask.b.bblerr = 1;
  23630. + } else {
  23631. + hc_intr_mask.b.nak = 1;
  23632. + hc_intr_mask.b.nyet = 1;
  23633. + if (hc->do_ping) {
  23634. + hc_intr_mask.b.ack = 1;
  23635. + }
  23636. + }
  23637. +
  23638. + if (hc->do_split) {
  23639. + hc_intr_mask.b.nak = 1;
  23640. + if (hc->complete_split) {
  23641. + hc_intr_mask.b.nyet = 1;
  23642. + } else {
  23643. + hc_intr_mask.b.ack = 1;
  23644. + }
  23645. + }
  23646. +
  23647. + if (hc->error_state) {
  23648. + hc_intr_mask.b.ack = 1;
  23649. + }
  23650. + break;
  23651. + case DWC_OTG_EP_TYPE_INTR:
  23652. + hc_intr_mask.b.xfercompl = 1;
  23653. + hc_intr_mask.b.nak = 1;
  23654. + hc_intr_mask.b.stall = 1;
  23655. + hc_intr_mask.b.xacterr = 1;
  23656. + hc_intr_mask.b.datatglerr = 1;
  23657. + hc_intr_mask.b.frmovrun = 1;
  23658. +
  23659. + if (hc->ep_is_in) {
  23660. + hc_intr_mask.b.bblerr = 1;
  23661. + }
  23662. + if (hc->error_state) {
  23663. + hc_intr_mask.b.ack = 1;
  23664. + }
  23665. + if (hc->do_split) {
  23666. + if (hc->complete_split) {
  23667. + hc_intr_mask.b.nyet = 1;
  23668. + } else {
  23669. + hc_intr_mask.b.ack = 1;
  23670. + }
  23671. + }
  23672. + break;
  23673. + case DWC_OTG_EP_TYPE_ISOC:
  23674. + hc_intr_mask.b.xfercompl = 1;
  23675. + hc_intr_mask.b.frmovrun = 1;
  23676. + hc_intr_mask.b.ack = 1;
  23677. +
  23678. + if (hc->ep_is_in) {
  23679. + hc_intr_mask.b.xacterr = 1;
  23680. + hc_intr_mask.b.bblerr = 1;
  23681. + }
  23682. + break;
  23683. + }
  23684. + }
  23685. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  23686. +
  23687. + /*
  23688. + * Program the HCCHARn register with the endpoint characteristics for
  23689. + * the current transfer.
  23690. + */
  23691. + hcchar.d32 = 0;
  23692. + hcchar.b.devaddr = hc->dev_addr;
  23693. + hcchar.b.epnum = hc->ep_num;
  23694. + hcchar.b.epdir = hc->ep_is_in;
  23695. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  23696. + hcchar.b.eptype = hc->ep_type;
  23697. + hcchar.b.mps = hc->max_packet;
  23698. +
  23699. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  23700. +
  23701. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  23702. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  23703. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  23704. + "Max Pkt %d, Multi Cnt %d\n",
  23705. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  23706. + hcchar.b.mps, hcchar.b.multicnt);
  23707. +
  23708. + /*
  23709. + * Program the HCSPLIT register for SPLITs
  23710. + */
  23711. + hcsplt.d32 = 0;
  23712. + if (hc->do_split) {
  23713. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  23714. + hc->hc_num,
  23715. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  23716. + hcsplt.b.compsplt = hc->complete_split;
  23717. + hcsplt.b.xactpos = hc->xact_pos;
  23718. + hcsplt.b.hubaddr = hc->hub_addr;
  23719. + hcsplt.b.prtaddr = hc->port_addr;
  23720. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  23721. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  23722. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  23723. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  23724. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  23725. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  23726. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  23727. + }
  23728. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  23729. +
  23730. +}
  23731. +
  23732. +/**
  23733. + * Attempts to halt a host channel. This function should only be called in
  23734. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  23735. + * normal circumstances in DMA mode, the controller halts the channel when the
  23736. + * transfer is complete or a condition occurs that requires application
  23737. + * intervention.
  23738. + *
  23739. + * In slave mode, checks for a free request queue entry, then sets the Channel
  23740. + * Enable and Channel Disable bits of the Host Channel Characteristics
  23741. + * register of the specified channel to intiate the halt. If there is no free
  23742. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  23743. + * register to flush requests for this channel. In the latter case, sets a
  23744. + * flag to indicate that the host channel needs to be halted when a request
  23745. + * queue slot is open.
  23746. + *
  23747. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  23748. + * HCCHARn register. The controller ensures there is space in the request
  23749. + * queue before submitting the halt request.
  23750. + *
  23751. + * Some time may elapse before the core flushes any posted requests for this
  23752. + * host channel and halts. The Channel Halted interrupt handler completes the
  23753. + * deactivation of the host channel.
  23754. + *
  23755. + * @param core_if Controller register interface.
  23756. + * @param hc Host channel to halt.
  23757. + * @param halt_status Reason for halting the channel.
  23758. + */
  23759. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  23760. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  23761. +{
  23762. + gnptxsts_data_t nptxsts;
  23763. + hptxsts_data_t hptxsts;
  23764. + hcchar_data_t hcchar;
  23765. + dwc_otg_hc_regs_t *hc_regs;
  23766. + dwc_otg_core_global_regs_t *global_regs;
  23767. + dwc_otg_host_global_regs_t *host_global_regs;
  23768. +
  23769. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23770. + global_regs = core_if->core_global_regs;
  23771. + host_global_regs = core_if->host_if->host_global_regs;
  23772. +
  23773. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  23774. + "halt_status = %d\n", halt_status);
  23775. +
  23776. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  23777. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  23778. + /*
  23779. + * Disable all channel interrupts except Ch Halted. The QTD
  23780. + * and QH state associated with this transfer has been cleared
  23781. + * (in the case of URB_DEQUEUE), so the channel needs to be
  23782. + * shut down carefully to prevent crashes.
  23783. + */
  23784. + hcintmsk_data_t hcintmsk;
  23785. + hcintmsk.d32 = 0;
  23786. + hcintmsk.b.chhltd = 1;
  23787. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  23788. +
  23789. + /*
  23790. + * Make sure no other interrupts besides halt are currently
  23791. + * pending. Handling another interrupt could cause a crash due
  23792. + * to the QTD and QH state.
  23793. + */
  23794. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  23795. +
  23796. + /*
  23797. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  23798. + * even if the channel was already halted for some other
  23799. + * reason.
  23800. + */
  23801. + hc->halt_status = halt_status;
  23802. +
  23803. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23804. + if (hcchar.b.chen == 0) {
  23805. + /*
  23806. + * The channel is either already halted or it hasn't
  23807. + * started yet. In DMA mode, the transfer may halt if
  23808. + * it finishes normally or a condition occurs that
  23809. + * requires driver intervention. Don't want to halt
  23810. + * the channel again. In either Slave or DMA mode,
  23811. + * it's possible that the transfer has been assigned
  23812. + * to a channel, but not started yet when an URB is
  23813. + * dequeued. Don't want to halt a channel that hasn't
  23814. + * started yet.
  23815. + */
  23816. + return;
  23817. + }
  23818. + }
  23819. + if (hc->halt_pending) {
  23820. + /*
  23821. + * A halt has already been issued for this channel. This might
  23822. + * happen when a transfer is aborted by a higher level in
  23823. + * the stack.
  23824. + */
  23825. +#ifdef DEBUG
  23826. + DWC_PRINTF
  23827. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  23828. + __func__, hc->hc_num);
  23829. +
  23830. +#endif
  23831. + return;
  23832. + }
  23833. +
  23834. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23835. +
  23836. + /* No need to set the bit in DDMA for disabling the channel */
  23837. + //TODO check it everywhere channel is disabled
  23838. + if (!core_if->core_params->dma_desc_enable)
  23839. + hcchar.b.chen = 1;
  23840. + hcchar.b.chdis = 1;
  23841. +
  23842. + if (!core_if->dma_enable) {
  23843. + /* Check for space in the request queue to issue the halt. */
  23844. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  23845. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  23846. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  23847. + if (nptxsts.b.nptxqspcavail == 0) {
  23848. + hcchar.b.chen = 0;
  23849. + }
  23850. + } else {
  23851. + hptxsts.d32 =
  23852. + DWC_READ_REG32(&host_global_regs->hptxsts);
  23853. + if ((hptxsts.b.ptxqspcavail == 0)
  23854. + || (core_if->queuing_high_bandwidth)) {
  23855. + hcchar.b.chen = 0;
  23856. + }
  23857. + }
  23858. + }
  23859. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23860. +
  23861. + hc->halt_status = halt_status;
  23862. +
  23863. + if (hcchar.b.chen) {
  23864. + hc->halt_pending = 1;
  23865. + hc->halt_on_queue = 0;
  23866. + } else {
  23867. + hc->halt_on_queue = 1;
  23868. + }
  23869. +
  23870. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  23871. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  23872. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  23873. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  23874. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  23875. +
  23876. + return;
  23877. +}
  23878. +
  23879. +/**
  23880. + * Clears the transfer state for a host channel. This function is normally
  23881. + * called after a transfer is done and the host channel is being released.
  23882. + *
  23883. + * @param core_if Programming view of DWC_otg controller.
  23884. + * @param hc Identifies the host channel to clean up.
  23885. + */
  23886. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23887. +{
  23888. + dwc_otg_hc_regs_t *hc_regs;
  23889. +
  23890. + hc->xfer_started = 0;
  23891. +
  23892. + /*
  23893. + * Clear channel interrupt enables and any unhandled channel interrupt
  23894. + * conditions.
  23895. + */
  23896. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23897. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  23898. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  23899. +#ifdef DEBUG
  23900. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  23901. +#endif
  23902. +}
  23903. +
  23904. +/**
  23905. + * Sets the channel property that indicates in which frame a periodic transfer
  23906. + * should occur. This is always set to the _next_ frame. This function has no
  23907. + * effect on non-periodic transfers.
  23908. + *
  23909. + * @param core_if Programming view of DWC_otg controller.
  23910. + * @param hc Identifies the host channel to set up and its properties.
  23911. + * @param hcchar Current value of the HCCHAR register for the specified host
  23912. + * channel.
  23913. + */
  23914. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  23915. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  23916. +{
  23917. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  23918. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  23919. + hfnum_data_t hfnum;
  23920. + hfnum.d32 =
  23921. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  23922. +
  23923. + /* 1 if _next_ frame is odd, 0 if it's even */
  23924. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  23925. +#ifdef DEBUG
  23926. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  23927. + && !hc->complete_split) {
  23928. + switch (hfnum.b.frnum & 0x7) {
  23929. + case 7:
  23930. + core_if->hfnum_7_samples++;
  23931. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  23932. + break;
  23933. + case 0:
  23934. + core_if->hfnum_0_samples++;
  23935. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  23936. + break;
  23937. + default:
  23938. + core_if->hfnum_other_samples++;
  23939. + core_if->hfnum_other_frrem_accum +=
  23940. + hfnum.b.frrem;
  23941. + break;
  23942. + }
  23943. + }
  23944. +#endif
  23945. + }
  23946. +}
  23947. +
  23948. +#ifdef DEBUG
  23949. +void hc_xfer_timeout(void *ptr)
  23950. +{
  23951. + hc_xfer_info_t *xfer_info = NULL;
  23952. + int hc_num = 0;
  23953. +
  23954. + if (ptr)
  23955. + xfer_info = (hc_xfer_info_t *) ptr;
  23956. +
  23957. + if (!xfer_info->hc) {
  23958. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  23959. + return;
  23960. + }
  23961. +
  23962. + hc_num = xfer_info->hc->hc_num;
  23963. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  23964. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  23965. + xfer_info->core_if->start_hcchar_val[hc_num]);
  23966. +}
  23967. +#endif
  23968. +
  23969. +void ep_xfer_timeout(void *ptr)
  23970. +{
  23971. + ep_xfer_info_t *xfer_info = NULL;
  23972. + int ep_num = 0;
  23973. + dctl_data_t dctl = {.d32 = 0 };
  23974. + gintsts_data_t gintsts = {.d32 = 0 };
  23975. + gintmsk_data_t gintmsk = {.d32 = 0 };
  23976. +
  23977. + if (ptr)
  23978. + xfer_info = (ep_xfer_info_t *) ptr;
  23979. +
  23980. + if (!xfer_info->ep) {
  23981. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  23982. + return;
  23983. + }
  23984. +
  23985. + ep_num = xfer_info->ep->num;
  23986. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  23987. + /* Put the sate to 2 as it was time outed */
  23988. + xfer_info->state = 2;
  23989. +
  23990. + dctl.d32 =
  23991. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  23992. + gintsts.d32 =
  23993. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  23994. + gintmsk.d32 =
  23995. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  23996. +
  23997. + if (!gintmsk.b.goutnakeff) {
  23998. + /* Unmask it */
  23999. + gintmsk.b.goutnakeff = 1;
  24000. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  24001. + gintmsk.d32);
  24002. +
  24003. + }
  24004. +
  24005. + if (!gintsts.b.goutnakeff) {
  24006. + dctl.b.sgoutnak = 1;
  24007. + }
  24008. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  24009. + dctl.d32);
  24010. +
  24011. +}
  24012. +
  24013. +void set_pid_isoc(dwc_hc_t * hc)
  24014. +{
  24015. + /* Set up the initial PID for the transfer. */
  24016. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  24017. + if (hc->ep_is_in) {
  24018. + if (hc->multi_count == 1) {
  24019. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24020. + } else if (hc->multi_count == 2) {
  24021. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  24022. + } else {
  24023. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  24024. + }
  24025. + } else {
  24026. + if (hc->multi_count == 1) {
  24027. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24028. + } else {
  24029. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  24030. + }
  24031. + }
  24032. + } else {
  24033. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24034. + }
  24035. +}
  24036. +
  24037. +/**
  24038. + * This function does the setup for a data transfer for a host channel and
  24039. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  24040. + * Slave mode, the caller must ensure that there is sufficient space in the
  24041. + * request queue and Tx Data FIFO.
  24042. + *
  24043. + * For an OUT transfer in Slave mode, it loads a data packet into the
  24044. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  24045. + * the Host ISR.
  24046. + *
  24047. + * For an IN transfer in Slave mode, a data packet is requested. The data
  24048. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  24049. + * additional data packets are requested in the Host ISR.
  24050. + *
  24051. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  24052. + * register along with a packet count of 1 and the channel is enabled. This
  24053. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  24054. + * simply set to 0 since no data transfer occurs in this case.
  24055. + *
  24056. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  24057. + * all the information required to perform the subsequent data transfer. In
  24058. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  24059. + * controller performs the entire PING protocol, then starts the data
  24060. + * transfer.
  24061. + *
  24062. + * @param core_if Programming view of DWC_otg controller.
  24063. + * @param hc Information needed to initialize the host channel. The xfer_len
  24064. + * value may be reduced to accommodate the max widths of the XferSize and
  24065. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  24066. + * to reflect the final xfer_len value.
  24067. + */
  24068. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24069. +{
  24070. + hcchar_data_t hcchar;
  24071. + hctsiz_data_t hctsiz;
  24072. + uint16_t num_packets;
  24073. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  24074. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  24075. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24076. +
  24077. + hctsiz.d32 = 0;
  24078. +
  24079. + if (hc->do_ping) {
  24080. + if (!core_if->dma_enable) {
  24081. + dwc_otg_hc_do_ping(core_if, hc);
  24082. + hc->xfer_started = 1;
  24083. + return;
  24084. + } else {
  24085. + hctsiz.b.dopng = 1;
  24086. + }
  24087. + }
  24088. +
  24089. + if (hc->do_split) {
  24090. + num_packets = 1;
  24091. +
  24092. + if (hc->complete_split && !hc->ep_is_in) {
  24093. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  24094. + * core doesn't expect any data written to the FIFO */
  24095. + hc->xfer_len = 0;
  24096. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  24097. + hc->xfer_len = hc->max_packet;
  24098. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  24099. + hc->xfer_len = 188;
  24100. + }
  24101. +
  24102. + hctsiz.b.xfersize = hc->xfer_len;
  24103. + } else {
  24104. + /*
  24105. + * Ensure that the transfer length and packet count will fit
  24106. + * in the widths allocated for them in the HCTSIZn register.
  24107. + */
  24108. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24109. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24110. + /*
  24111. + * Make sure the transfer size is no larger than one
  24112. + * (micro)frame's worth of data. (A check was done
  24113. + * when the periodic transfer was accepted to ensure
  24114. + * that a (micro)frame's worth of data can be
  24115. + * programmed into a channel.)
  24116. + */
  24117. + uint32_t max_periodic_len =
  24118. + hc->multi_count * hc->max_packet;
  24119. + if (hc->xfer_len > max_periodic_len) {
  24120. + hc->xfer_len = max_periodic_len;
  24121. + } else {
  24122. + }
  24123. + } else if (hc->xfer_len > max_hc_xfer_size) {
  24124. + /* Make sure that xfer_len is a multiple of max packet size. */
  24125. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  24126. + }
  24127. +
  24128. + if (hc->xfer_len > 0) {
  24129. + num_packets =
  24130. + (hc->xfer_len + hc->max_packet -
  24131. + 1) / hc->max_packet;
  24132. + if (num_packets > max_hc_pkt_count) {
  24133. + num_packets = max_hc_pkt_count;
  24134. + hc->xfer_len = num_packets * hc->max_packet;
  24135. + }
  24136. + } else {
  24137. + /* Need 1 packet for transfer length of 0. */
  24138. + num_packets = 1;
  24139. + }
  24140. +
  24141. + if (hc->ep_is_in) {
  24142. + /* Always program an integral # of max packets for IN transfers. */
  24143. + hc->xfer_len = num_packets * hc->max_packet;
  24144. + }
  24145. +
  24146. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24147. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24148. + /*
  24149. + * Make sure that the multi_count field matches the
  24150. + * actual transfer length.
  24151. + */
  24152. + hc->multi_count = num_packets;
  24153. + }
  24154. +
  24155. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  24156. + set_pid_isoc(hc);
  24157. +
  24158. + hctsiz.b.xfersize = hc->xfer_len;
  24159. + }
  24160. +
  24161. + hc->start_pkt_count = num_packets;
  24162. + hctsiz.b.pktcnt = num_packets;
  24163. + hctsiz.b.pid = hc->data_pid_start;
  24164. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24165. +
  24166. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24167. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  24168. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  24169. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  24170. +
  24171. + if (core_if->dma_enable) {
  24172. + dwc_dma_t dma_addr;
  24173. + if (hc->align_buff) {
  24174. + dma_addr = hc->align_buff;
  24175. + } else {
  24176. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  24177. + }
  24178. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  24179. + }
  24180. +
  24181. + /* Start the split */
  24182. + if (hc->do_split) {
  24183. + hcsplt_data_t hcsplt;
  24184. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  24185. + hcsplt.b.spltena = 1;
  24186. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  24187. + }
  24188. +
  24189. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24190. + hcchar.b.multicnt = hc->multi_count;
  24191. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24192. +#ifdef DEBUG
  24193. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  24194. + if (hcchar.b.chdis) {
  24195. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  24196. + __func__, hc->hc_num, hcchar.d32);
  24197. + }
  24198. +#endif
  24199. +
  24200. + /* Set host channel enable after all other setup is complete. */
  24201. + hcchar.b.chen = 1;
  24202. + hcchar.b.chdis = 0;
  24203. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24204. +
  24205. + hc->xfer_started = 1;
  24206. + hc->requests++;
  24207. +
  24208. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  24209. + /* Load OUT packet into the appropriate Tx FIFO. */
  24210. + dwc_otg_hc_write_packet(core_if, hc);
  24211. + }
  24212. +#ifdef DEBUG
  24213. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  24214. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  24215. + hc->hc_num, core_if);//GRAYG
  24216. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  24217. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  24218. +
  24219. + /* Start a timer for this transfer. */
  24220. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  24221. + }
  24222. +#endif
  24223. +}
  24224. +
  24225. +/**
  24226. + * This function does the setup for a data transfer for a host channel
  24227. + * and starts the transfer in Descriptor DMA mode.
  24228. + *
  24229. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  24230. + * Sets PID and NTD values. For periodic transfers
  24231. + * initializes SCHED_INFO field with micro-frame bitmap.
  24232. + *
  24233. + * Initializes HCDMA register with descriptor list address and CTD value
  24234. + * then starts the transfer via enabling the channel.
  24235. + *
  24236. + * @param core_if Programming view of DWC_otg controller.
  24237. + * @param hc Information needed to initialize the host channel.
  24238. + */
  24239. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24240. +{
  24241. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24242. + hcchar_data_t hcchar;
  24243. + hctsiz_data_t hctsiz;
  24244. + hcdma_data_t hcdma;
  24245. +
  24246. + hctsiz.d32 = 0;
  24247. +
  24248. + if (hc->do_ping)
  24249. + hctsiz.b_ddma.dopng = 1;
  24250. +
  24251. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  24252. + set_pid_isoc(hc);
  24253. +
  24254. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  24255. + hctsiz.b_ddma.pid = hc->data_pid_start;
  24256. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  24257. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  24258. +
  24259. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24260. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  24261. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  24262. +
  24263. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24264. +
  24265. + hcdma.d32 = 0;
  24266. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  24267. +
  24268. + /* Always start from first descriptor. */
  24269. + hcdma.b.ctd = 0;
  24270. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  24271. +
  24272. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24273. + hcchar.b.multicnt = hc->multi_count;
  24274. +
  24275. +#ifdef DEBUG
  24276. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  24277. + if (hcchar.b.chdis) {
  24278. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  24279. + __func__, hc->hc_num, hcchar.d32);
  24280. + }
  24281. +#endif
  24282. +
  24283. + /* Set host channel enable after all other setup is complete. */
  24284. + hcchar.b.chen = 1;
  24285. + hcchar.b.chdis = 0;
  24286. +
  24287. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24288. +
  24289. + hc->xfer_started = 1;
  24290. + hc->requests++;
  24291. +
  24292. +#ifdef DEBUG
  24293. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  24294. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  24295. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  24296. + hc->hc_num, core_if);//GRAYG
  24297. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  24298. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  24299. + /* Start a timer for this transfer. */
  24300. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  24301. + }
  24302. +#endif
  24303. +
  24304. +}
  24305. +
  24306. +/**
  24307. + * This function continues a data transfer that was started by previous call
  24308. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  24309. + * sufficient space in the request queue and Tx Data FIFO. This function
  24310. + * should only be called in Slave mode. In DMA mode, the controller acts
  24311. + * autonomously to complete transfers programmed to a host channel.
  24312. + *
  24313. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  24314. + * if there is any data remaining to be queued. For an IN transfer, another
  24315. + * data packet is always requested. For the SETUP phase of a control transfer,
  24316. + * this function does nothing.
  24317. + *
  24318. + * @return 1 if a new request is queued, 0 if no more requests are required
  24319. + * for this transfer.
  24320. + */
  24321. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24322. +{
  24323. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24324. +
  24325. + if (hc->do_split) {
  24326. + /* SPLITs always queue just once per channel */
  24327. + return 0;
  24328. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  24329. + /* SETUPs are queued only once since they can't be NAKed. */
  24330. + return 0;
  24331. + } else if (hc->ep_is_in) {
  24332. + /*
  24333. + * Always queue another request for other IN transfers. If
  24334. + * back-to-back INs are issued and NAKs are received for both,
  24335. + * the driver may still be processing the first NAK when the
  24336. + * second NAK is received. When the interrupt handler clears
  24337. + * the NAK interrupt for the first NAK, the second NAK will
  24338. + * not be seen. So we can't depend on the NAK interrupt
  24339. + * handler to requeue a NAKed request. Instead, IN requests
  24340. + * are issued each time this function is called. When the
  24341. + * transfer completes, the extra requests for the channel will
  24342. + * be flushed.
  24343. + */
  24344. + hcchar_data_t hcchar;
  24345. + dwc_otg_hc_regs_t *hc_regs =
  24346. + core_if->host_if->hc_regs[hc->hc_num];
  24347. +
  24348. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24349. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24350. + hcchar.b.chen = 1;
  24351. + hcchar.b.chdis = 0;
  24352. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  24353. + hcchar.d32);
  24354. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24355. + hc->requests++;
  24356. + return 1;
  24357. + } else {
  24358. + /* OUT transfers. */
  24359. + if (hc->xfer_count < hc->xfer_len) {
  24360. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24361. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24362. + hcchar_data_t hcchar;
  24363. + dwc_otg_hc_regs_t *hc_regs;
  24364. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24365. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24366. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24367. + }
  24368. +
  24369. + /* Load OUT packet into the appropriate Tx FIFO. */
  24370. + dwc_otg_hc_write_packet(core_if, hc);
  24371. + hc->requests++;
  24372. + return 1;
  24373. + } else {
  24374. + return 0;
  24375. + }
  24376. + }
  24377. +}
  24378. +
  24379. +/**
  24380. + * Starts a PING transfer. This function should only be called in Slave mode.
  24381. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  24382. + */
  24383. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24384. +{
  24385. + hcchar_data_t hcchar;
  24386. + hctsiz_data_t hctsiz;
  24387. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24388. +
  24389. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24390. +
  24391. + hctsiz.d32 = 0;
  24392. + hctsiz.b.dopng = 1;
  24393. + hctsiz.b.pktcnt = 1;
  24394. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24395. +
  24396. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24397. + hcchar.b.chen = 1;
  24398. + hcchar.b.chdis = 0;
  24399. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24400. +}
  24401. +
  24402. +/*
  24403. + * This function writes a packet into the Tx FIFO associated with the Host
  24404. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  24405. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  24406. + * periodic Tx FIFO is written. This function should only be called in Slave
  24407. + * mode.
  24408. + *
  24409. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  24410. + * then number of bytes written to the Tx FIFO.
  24411. + */
  24412. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24413. +{
  24414. + uint32_t i;
  24415. + uint32_t remaining_count;
  24416. + uint32_t byte_count;
  24417. + uint32_t dword_count;
  24418. +
  24419. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  24420. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  24421. +
  24422. + remaining_count = hc->xfer_len - hc->xfer_count;
  24423. + if (remaining_count > hc->max_packet) {
  24424. + byte_count = hc->max_packet;
  24425. + } else {
  24426. + byte_count = remaining_count;
  24427. + }
  24428. +
  24429. + dword_count = (byte_count + 3) / 4;
  24430. +
  24431. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  24432. + /* xfer_buff is DWORD aligned. */
  24433. + for (i = 0; i < dword_count; i++, data_buff++) {
  24434. + DWC_WRITE_REG32(data_fifo, *data_buff);
  24435. + }
  24436. + } else {
  24437. + /* xfer_buff is not DWORD aligned. */
  24438. + for (i = 0; i < dword_count; i++, data_buff++) {
  24439. + uint32_t data;
  24440. + data =
  24441. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  24442. + 16 | data_buff[3] << 24);
  24443. + DWC_WRITE_REG32(data_fifo, data);
  24444. + }
  24445. + }
  24446. +
  24447. + hc->xfer_count += byte_count;
  24448. + hc->xfer_buff += byte_count;
  24449. +}
  24450. +
  24451. +/**
  24452. + * Gets the current USB frame number. This is the frame number from the last
  24453. + * SOF packet.
  24454. + */
  24455. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  24456. +{
  24457. + dsts_data_t dsts;
  24458. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  24459. +
  24460. + /* read current frame/microframe number from DSTS register */
  24461. + return dsts.b.soffn;
  24462. +}
  24463. +
  24464. +/**
  24465. + * Calculates and gets the frame Interval value of HFIR register according PHY
  24466. + * type and speed.The application can modify a value of HFIR register only after
  24467. + * the Port Enable bit of the Host Port Control and Status register
  24468. + * (HPRT.PrtEnaPort) has been set.
  24469. +*/
  24470. +
  24471. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  24472. +{
  24473. + gusbcfg_data_t usbcfg;
  24474. + hwcfg2_data_t hwcfg2;
  24475. + hprt0_data_t hprt0;
  24476. + int clock = 60; // default value
  24477. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  24478. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  24479. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  24480. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  24481. + clock = 60;
  24482. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  24483. + clock = 48;
  24484. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24485. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  24486. + clock = 30;
  24487. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24488. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  24489. + clock = 60;
  24490. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24491. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  24492. + clock = 48;
  24493. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  24494. + clock = 48;
  24495. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  24496. + clock = 48;
  24497. + if (hprt0.b.prtspd == 0)
  24498. + /* High speed case */
  24499. + return 125 * clock;
  24500. + else
  24501. + /* FS/LS case */
  24502. + return 1000 * clock;
  24503. +}
  24504. +
  24505. +/**
  24506. + * This function reads a setup packet from the Rx FIFO into the destination
  24507. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  24508. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  24509. + *
  24510. + * @param core_if Programming view of DWC_otg controller.
  24511. + * @param dest Destination buffer for packet data.
  24512. + */
  24513. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  24514. +{
  24515. + device_grxsts_data_t status;
  24516. + /* Get the 8 bytes of a setup transaction data */
  24517. +
  24518. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  24519. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  24520. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  24521. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  24522. + status.d32 =
  24523. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  24524. + DWC_DEBUGPL(DBG_ANY,
  24525. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  24526. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  24527. + status.b.fn, status.b.fn);
  24528. + }
  24529. +}
  24530. +
  24531. +/**
  24532. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  24533. + * IN for transmitting packets. It is normally called when the
  24534. + * "Enumeration Done" interrupt occurs.
  24535. + *
  24536. + * @param core_if Programming view of DWC_otg controller.
  24537. + * @param ep The EP0 data.
  24538. + */
  24539. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24540. +{
  24541. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24542. + dsts_data_t dsts;
  24543. + depctl_data_t diepctl;
  24544. + depctl_data_t doepctl;
  24545. + dctl_data_t dctl = {.d32 = 0 };
  24546. +
  24547. + ep->stp_rollover = 0;
  24548. + /* Read the Device Status and Endpoint 0 Control registers */
  24549. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  24550. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  24551. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  24552. +
  24553. + /* Set the MPS of the IN EP based on the enumeration speed */
  24554. + switch (dsts.b.enumspd) {
  24555. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  24556. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  24557. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  24558. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  24559. + break;
  24560. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  24561. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  24562. + break;
  24563. + }
  24564. +
  24565. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  24566. +
  24567. + /* Enable OUT EP for receive */
  24568. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  24569. + doepctl.b.epena = 1;
  24570. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  24571. + }
  24572. +#ifdef VERBOSE
  24573. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  24574. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  24575. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  24576. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  24577. +#endif
  24578. + dctl.b.cgnpinnak = 1;
  24579. +
  24580. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  24581. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  24582. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  24583. +
  24584. +}
  24585. +
  24586. +/**
  24587. + * This function activates an EP. The Device EP control register for
  24588. + * the EP is configured as defined in the ep structure. Note: This
  24589. + * function is not used for EP0.
  24590. + *
  24591. + * @param core_if Programming view of DWC_otg controller.
  24592. + * @param ep The EP to activate.
  24593. + */
  24594. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24595. +{
  24596. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24597. + depctl_data_t depctl;
  24598. + volatile uint32_t *addr;
  24599. + daint_data_t daintmsk = {.d32 = 0 };
  24600. + dcfg_data_t dcfg;
  24601. + uint8_t i;
  24602. +
  24603. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  24604. + (ep->is_in ? "IN" : "OUT"));
  24605. +
  24606. +#ifdef DWC_UTE_PER_IO
  24607. + ep->xiso_frame_num = 0xFFFFFFFF;
  24608. + ep->xiso_active_xfers = 0;
  24609. + ep->xiso_queued_xfers = 0;
  24610. +#endif
  24611. + /* Read DEPCTLn register */
  24612. + if (ep->is_in == 1) {
  24613. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  24614. + daintmsk.ep.in = 1 << ep->num;
  24615. + } else {
  24616. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  24617. + daintmsk.ep.out = 1 << ep->num;
  24618. + }
  24619. +
  24620. + /* If the EP is already active don't change the EP Control
  24621. + * register. */
  24622. + depctl.d32 = DWC_READ_REG32(addr);
  24623. + if (!depctl.b.usbactep) {
  24624. + depctl.b.mps = ep->maxpacket;
  24625. + depctl.b.eptype = ep->type;
  24626. + depctl.b.txfnum = ep->tx_fifo_num;
  24627. +
  24628. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24629. + depctl.b.setd0pid = 1; // ???
  24630. + } else {
  24631. + depctl.b.setd0pid = 1;
  24632. + }
  24633. + depctl.b.usbactep = 1;
  24634. +
  24635. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  24636. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  24637. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  24638. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  24639. + break;
  24640. + }
  24641. + core_if->nextep_seq[i] = ep->num;
  24642. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  24643. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  24644. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  24645. + dcfg.b.epmscnt++;
  24646. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  24647. +
  24648. + DWC_DEBUGPL(DBG_PCDV,
  24649. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  24650. + __func__, core_if->first_in_nextep_seq);
  24651. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  24652. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  24653. + core_if->nextep_seq[i]);
  24654. + }
  24655. +
  24656. + }
  24657. +
  24658. +
  24659. + DWC_WRITE_REG32(addr, depctl.d32);
  24660. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  24661. + }
  24662. +
  24663. + /* Enable the Interrupt for this EP */
  24664. + if (core_if->multiproc_int_enable) {
  24665. + if (ep->is_in == 1) {
  24666. + diepmsk_data_t diepmsk = {.d32 = 0 };
  24667. + diepmsk.b.xfercompl = 1;
  24668. + diepmsk.b.timeout = 1;
  24669. + diepmsk.b.epdisabled = 1;
  24670. + diepmsk.b.ahberr = 1;
  24671. + diepmsk.b.intknepmis = 1;
  24672. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  24673. + diepmsk.b.intknepmis = 0;
  24674. + diepmsk.b.txfifoundrn = 1; //?????
  24675. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24676. + diepmsk.b.nak = 1;
  24677. + }
  24678. +
  24679. +
  24680. +
  24681. +/*
  24682. + if (core_if->dma_desc_enable) {
  24683. + diepmsk.b.bna = 1;
  24684. + }
  24685. +*/
  24686. +/*
  24687. + if (core_if->dma_enable) {
  24688. + doepmsk.b.nak = 1;
  24689. + }
  24690. +*/
  24691. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  24692. + diepeachintmsk[ep->num], diepmsk.d32);
  24693. +
  24694. + } else {
  24695. + doepmsk_data_t doepmsk = {.d32 = 0 };
  24696. + doepmsk.b.xfercompl = 1;
  24697. + doepmsk.b.ahberr = 1;
  24698. + doepmsk.b.epdisabled = 1;
  24699. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  24700. + doepmsk.b.outtknepdis = 1;
  24701. +
  24702. +/*
  24703. +
  24704. + if (core_if->dma_desc_enable) {
  24705. + doepmsk.b.bna = 1;
  24706. + }
  24707. +*/
  24708. +/*
  24709. + doepmsk.b.babble = 1;
  24710. + doepmsk.b.nyet = 1;
  24711. + doepmsk.b.nak = 1;
  24712. +*/
  24713. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  24714. + doepeachintmsk[ep->num], doepmsk.d32);
  24715. + }
  24716. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  24717. + 0, daintmsk.d32);
  24718. + } else {
  24719. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24720. + if (ep->is_in) {
  24721. + diepmsk_data_t diepmsk = {.d32 = 0 };
  24722. + diepmsk.b.nak = 1;
  24723. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  24724. + } else {
  24725. + doepmsk_data_t doepmsk = {.d32 = 0 };
  24726. + doepmsk.b.outtknepdis = 1;
  24727. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  24728. + }
  24729. + }
  24730. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  24731. + 0, daintmsk.d32);
  24732. + }
  24733. +
  24734. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  24735. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  24736. +
  24737. + ep->stall_clear_flag = 0;
  24738. +
  24739. + return;
  24740. +}
  24741. +
  24742. +/**
  24743. + * This function deactivates an EP. This is done by clearing the USB Active
  24744. + * EP bit in the Device EP control register. Note: This function is not used
  24745. + * for EP0. EP0 cannot be deactivated.
  24746. + *
  24747. + * @param core_if Programming view of DWC_otg controller.
  24748. + * @param ep The EP to deactivate.
  24749. + */
  24750. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24751. +{
  24752. + depctl_data_t depctl = {.d32 = 0 };
  24753. + volatile uint32_t *addr;
  24754. + daint_data_t daintmsk = {.d32 = 0 };
  24755. + dcfg_data_t dcfg;
  24756. + uint8_t i = 0;
  24757. +
  24758. +#ifdef DWC_UTE_PER_IO
  24759. + ep->xiso_frame_num = 0xFFFFFFFF;
  24760. + ep->xiso_active_xfers = 0;
  24761. + ep->xiso_queued_xfers = 0;
  24762. +#endif
  24763. +
  24764. + /* Read DEPCTLn register */
  24765. + if (ep->is_in == 1) {
  24766. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  24767. + daintmsk.ep.in = 1 << ep->num;
  24768. + } else {
  24769. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  24770. + daintmsk.ep.out = 1 << ep->num;
  24771. + }
  24772. +
  24773. + depctl.d32 = DWC_READ_REG32(addr);
  24774. +
  24775. + depctl.b.usbactep = 0;
  24776. +
  24777. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  24778. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  24779. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  24780. + if (core_if->nextep_seq[i] == ep->num)
  24781. + break;
  24782. + }
  24783. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  24784. + if (core_if->first_in_nextep_seq == ep->num)
  24785. + core_if->first_in_nextep_seq = i;
  24786. + core_if->nextep_seq[ep->num] = 0xff;
  24787. + depctl.b.nextep = 0;
  24788. + dcfg.d32 =
  24789. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  24790. + dcfg.b.epmscnt--;
  24791. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  24792. + dcfg.d32);
  24793. +
  24794. + DWC_DEBUGPL(DBG_PCDV,
  24795. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  24796. + __func__, core_if->first_in_nextep_seq);
  24797. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  24798. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  24799. + }
  24800. + }
  24801. +
  24802. + if (ep->is_in == 1)
  24803. + depctl.b.txfnum = 0;
  24804. +
  24805. + if (core_if->dma_desc_enable)
  24806. + depctl.b.epdis = 1;
  24807. +
  24808. + DWC_WRITE_REG32(addr, depctl.d32);
  24809. + depctl.d32 = DWC_READ_REG32(addr);
  24810. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  24811. + && depctl.b.epena) {
  24812. + depctl_data_t depctl = {.d32 = 0};
  24813. + if (ep->is_in) {
  24814. + diepint_data_t diepint = {.d32 = 0};
  24815. +
  24816. + depctl.b.snak = 1;
  24817. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24818. + diepctl, depctl.d32);
  24819. + do {
  24820. + dwc_udelay(10);
  24821. + diepint.d32 =
  24822. + DWC_READ_REG32(&core_if->
  24823. + dev_if->in_ep_regs[ep->num]->
  24824. + diepint);
  24825. + } while (!diepint.b.inepnakeff);
  24826. + diepint.b.inepnakeff = 1;
  24827. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24828. + diepint, diepint.d32);
  24829. + depctl.d32 = 0;
  24830. + depctl.b.epdis = 1;
  24831. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24832. + diepctl, depctl.d32);
  24833. + do {
  24834. + dwc_udelay(10);
  24835. + diepint.d32 =
  24836. + DWC_READ_REG32(&core_if->
  24837. + dev_if->in_ep_regs[ep->num]->
  24838. + diepint);
  24839. + } while (!diepint.b.epdisabled);
  24840. + diepint.b.epdisabled = 1;
  24841. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24842. + diepint, diepint.d32);
  24843. + } else {
  24844. + dctl_data_t dctl = {.d32 = 0};
  24845. + gintmsk_data_t gintsts = {.d32 = 0};
  24846. + doepint_data_t doepint = {.d32 = 0};
  24847. + dctl.b.sgoutnak = 1;
  24848. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  24849. + dctl, 0, dctl.d32);
  24850. + do {
  24851. + dwc_udelay(10);
  24852. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  24853. + } while (!gintsts.b.goutnakeff);
  24854. + gintsts.d32 = 0;
  24855. + gintsts.b.goutnakeff = 1;
  24856. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  24857. +
  24858. + depctl.d32 = 0;
  24859. + depctl.b.epdis = 1;
  24860. + depctl.b.snak = 1;
  24861. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  24862. + do
  24863. + {
  24864. + dwc_udelay(10);
  24865. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  24866. + out_ep_regs[ep->num]->doepint);
  24867. + } while (!doepint.b.epdisabled);
  24868. +
  24869. + doepint.b.epdisabled = 1;
  24870. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  24871. +
  24872. + dctl.d32 = 0;
  24873. + dctl.b.cgoutnak = 1;
  24874. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  24875. + }
  24876. + }
  24877. +
  24878. + /* Disable the Interrupt for this EP */
  24879. + if (core_if->multiproc_int_enable) {
  24880. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  24881. + daintmsk.d32, 0);
  24882. +
  24883. + if (ep->is_in == 1) {
  24884. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  24885. + diepeachintmsk[ep->num], 0);
  24886. + } else {
  24887. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  24888. + doepeachintmsk[ep->num], 0);
  24889. + }
  24890. + } else {
  24891. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  24892. + daintmsk.d32, 0);
  24893. + }
  24894. +
  24895. +}
  24896. +
  24897. +/**
  24898. + * This function initializes dma descriptor chain.
  24899. + *
  24900. + * @param core_if Programming view of DWC_otg controller.
  24901. + * @param ep The EP to start the transfer on.
  24902. + */
  24903. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24904. +{
  24905. + dwc_otg_dev_dma_desc_t *dma_desc;
  24906. + uint32_t offset;
  24907. + uint32_t xfer_est;
  24908. + int i;
  24909. + unsigned maxxfer_local, total_len;
  24910. +
  24911. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  24912. + (ep->maxpacket%4)) {
  24913. + maxxfer_local = ep->maxpacket;
  24914. + total_len = ep->xfer_len;
  24915. + } else {
  24916. + maxxfer_local = ep->maxxfer;
  24917. + total_len = ep->total_len;
  24918. + }
  24919. +
  24920. + ep->desc_cnt = (total_len / maxxfer_local) +
  24921. + ((total_len % maxxfer_local) ? 1 : 0);
  24922. +
  24923. + if (!ep->desc_cnt)
  24924. + ep->desc_cnt = 1;
  24925. +
  24926. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  24927. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  24928. +
  24929. + dma_desc = ep->desc_addr;
  24930. + if (maxxfer_local == ep->maxpacket) {
  24931. + if ((total_len % maxxfer_local) &&
  24932. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  24933. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  24934. + (total_len % maxxfer_local);
  24935. + } else
  24936. + xfer_est = ep->desc_cnt * maxxfer_local;
  24937. + } else
  24938. + xfer_est = total_len;
  24939. + offset = 0;
  24940. + for (i = 0; i < ep->desc_cnt; ++i) {
  24941. + /** DMA Descriptor Setup */
  24942. + if (xfer_est > maxxfer_local) {
  24943. + dma_desc->status.b.bs = BS_HOST_BUSY;
  24944. + dma_desc->status.b.l = 0;
  24945. + dma_desc->status.b.ioc = 0;
  24946. + dma_desc->status.b.sp = 0;
  24947. + dma_desc->status.b.bytes = maxxfer_local;
  24948. + dma_desc->buf = ep->dma_addr + offset;
  24949. + dma_desc->status.b.sts = 0;
  24950. + dma_desc->status.b.bs = BS_HOST_READY;
  24951. +
  24952. + xfer_est -= maxxfer_local;
  24953. + offset += maxxfer_local;
  24954. + } else {
  24955. + dma_desc->status.b.bs = BS_HOST_BUSY;
  24956. + dma_desc->status.b.l = 1;
  24957. + dma_desc->status.b.ioc = 1;
  24958. + if (ep->is_in) {
  24959. + dma_desc->status.b.sp =
  24960. + (xfer_est %
  24961. + ep->maxpacket) ? 1 : ((ep->
  24962. + sent_zlp) ? 1 : 0);
  24963. + dma_desc->status.b.bytes = xfer_est;
  24964. + } else {
  24965. + if (maxxfer_local == ep->maxpacket)
  24966. + dma_desc->status.b.bytes = xfer_est;
  24967. + else
  24968. + dma_desc->status.b.bytes =
  24969. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  24970. + }
  24971. +
  24972. + dma_desc->buf = ep->dma_addr + offset;
  24973. + dma_desc->status.b.sts = 0;
  24974. + dma_desc->status.b.bs = BS_HOST_READY;
  24975. + }
  24976. + dma_desc++;
  24977. + }
  24978. +}
  24979. +/**
  24980. + * This function is called when to write ISOC data into appropriate dedicated
  24981. + * periodic FIFO.
  24982. + */
  24983. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  24984. +{
  24985. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24986. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  24987. + dtxfsts_data_t txstatus = {.d32 = 0 };
  24988. + uint32_t len = 0;
  24989. + int epnum = dwc_ep->num;
  24990. + int dwords;
  24991. +
  24992. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  24993. +
  24994. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  24995. +
  24996. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  24997. +
  24998. + if (len > dwc_ep->maxpacket) {
  24999. + len = dwc_ep->maxpacket;
  25000. + }
  25001. +
  25002. + dwords = (len + 3) / 4;
  25003. +
  25004. + /* While there is space in the queue and space in the FIFO and
  25005. + * More data to tranfer, Write packets to the Tx FIFO */
  25006. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  25007. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  25008. +
  25009. + while (txstatus.b.txfspcavail > dwords &&
  25010. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  25011. + /* Write the FIFO */
  25012. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  25013. +
  25014. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  25015. + if (len > dwc_ep->maxpacket) {
  25016. + len = dwc_ep->maxpacket;
  25017. + }
  25018. +
  25019. + dwords = (len + 3) / 4;
  25020. + txstatus.d32 =
  25021. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  25022. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  25023. + txstatus.d32);
  25024. + }
  25025. +
  25026. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  25027. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  25028. +
  25029. + return 1;
  25030. +}
  25031. +/**
  25032. + * This function does the setup for a data transfer for an EP and
  25033. + * starts the transfer. For an IN transfer, the packets will be
  25034. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  25035. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  25036. + *
  25037. + * @param core_if Programming view of DWC_otg controller.
  25038. + * @param ep The EP to start the transfer on.
  25039. + */
  25040. +
  25041. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25042. +{
  25043. + depctl_data_t depctl;
  25044. + deptsiz_data_t deptsiz;
  25045. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25046. +
  25047. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  25048. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  25049. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  25050. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  25051. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  25052. + ep->total_len);
  25053. + /* IN endpoint */
  25054. + if (ep->is_in == 1) {
  25055. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25056. + core_if->dev_if->in_ep_regs[ep->num];
  25057. +
  25058. + gnptxsts_data_t gtxstatus;
  25059. +
  25060. + gtxstatus.d32 =
  25061. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25062. +
  25063. + if (core_if->en_multiple_tx_fifo == 0
  25064. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  25065. +#ifdef DEBUG
  25066. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  25067. +#endif
  25068. + return;
  25069. + }
  25070. +
  25071. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  25072. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  25073. +
  25074. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  25075. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  25076. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  25077. + else
  25078. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  25079. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  25080. +
  25081. +
  25082. + /* Zero Length Packet? */
  25083. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  25084. + deptsiz.b.xfersize = 0;
  25085. + deptsiz.b.pktcnt = 1;
  25086. + } else {
  25087. + /* Program the transfer size and packet count
  25088. + * as follows: xfersize = N * maxpacket +
  25089. + * short_packet pktcnt = N + (short_packet
  25090. + * exist ? 1 : 0)
  25091. + */
  25092. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  25093. + deptsiz.b.pktcnt =
  25094. + (ep->xfer_len - ep->xfer_count - 1 +
  25095. + ep->maxpacket) / ep->maxpacket;
  25096. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  25097. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  25098. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  25099. + }
  25100. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  25101. + deptsiz.b.mc = deptsiz.b.pktcnt;
  25102. + }
  25103. +
  25104. + /* Write the DMA register */
  25105. + if (core_if->dma_enable) {
  25106. + if (core_if->dma_desc_enable == 0) {
  25107. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  25108. + deptsiz.b.mc = 1;
  25109. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25110. + deptsiz.d32);
  25111. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25112. + (uint32_t) ep->dma_addr);
  25113. + } else {
  25114. +#ifdef DWC_UTE_CFI
  25115. + /* The descriptor chain should be already initialized by now */
  25116. + if (ep->buff_mode != BM_STANDARD) {
  25117. + DWC_WRITE_REG32(&in_regs->diepdma,
  25118. + ep->descs_dma_addr);
  25119. + } else {
  25120. +#endif
  25121. + init_dma_desc_chain(core_if, ep);
  25122. + /** DIEPDMAn Register write */
  25123. + DWC_WRITE_REG32(&in_regs->diepdma,
  25124. + ep->dma_desc_addr);
  25125. +#ifdef DWC_UTE_CFI
  25126. + }
  25127. +#endif
  25128. + }
  25129. + } else {
  25130. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25131. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  25132. + /**
  25133. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  25134. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  25135. + * the data will be written into the fifo by the ISR.
  25136. + */
  25137. + if (core_if->en_multiple_tx_fifo == 0) {
  25138. + intr_mask.b.nptxfempty = 1;
  25139. + DWC_MODIFY_REG32
  25140. + (&core_if->core_global_regs->gintmsk,
  25141. + intr_mask.d32, intr_mask.d32);
  25142. + } else {
  25143. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25144. + if (ep->xfer_len > 0) {
  25145. + uint32_t fifoemptymsk = 0;
  25146. + fifoemptymsk = 1 << ep->num;
  25147. + DWC_MODIFY_REG32
  25148. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25149. + 0, fifoemptymsk);
  25150. +
  25151. + }
  25152. + }
  25153. + } else {
  25154. + write_isoc_tx_fifo(core_if, ep);
  25155. + }
  25156. + }
  25157. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25158. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25159. +
  25160. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  25161. + dsts_data_t dsts = {.d32 = 0};
  25162. + if (ep->bInterval == 1) {
  25163. + dsts.d32 =
  25164. + DWC_READ_REG32(&core_if->dev_if->
  25165. + dev_global_regs->dsts);
  25166. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  25167. + if (ep->frame_num > 0x3FFF) {
  25168. + ep->frm_overrun = 1;
  25169. + ep->frame_num &= 0x3FFF;
  25170. + } else
  25171. + ep->frm_overrun = 0;
  25172. + if (ep->frame_num & 0x1) {
  25173. + depctl.b.setd1pid = 1;
  25174. + } else {
  25175. + depctl.b.setd0pid = 1;
  25176. + }
  25177. + }
  25178. + }
  25179. + /* EP enable, IN data in FIFO */
  25180. + depctl.b.cnak = 1;
  25181. + depctl.b.epena = 1;
  25182. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25183. +
  25184. + } else {
  25185. + /* OUT endpoint */
  25186. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25187. + core_if->dev_if->out_ep_regs[ep->num];
  25188. +
  25189. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  25190. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  25191. +
  25192. + if (!core_if->dma_desc_enable) {
  25193. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  25194. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  25195. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  25196. + else
  25197. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  25198. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  25199. + }
  25200. +
  25201. + /* Program the transfer size and packet count as follows:
  25202. + *
  25203. + * pktcnt = N
  25204. + * xfersize = N * maxpacket
  25205. + */
  25206. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  25207. + /* Zero Length Packet */
  25208. + deptsiz.b.xfersize = ep->maxpacket;
  25209. + deptsiz.b.pktcnt = 1;
  25210. + } else {
  25211. + deptsiz.b.pktcnt =
  25212. + (ep->xfer_len - ep->xfer_count +
  25213. + (ep->maxpacket - 1)) / ep->maxpacket;
  25214. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  25215. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  25216. + }
  25217. + if (!core_if->dma_desc_enable) {
  25218. + ep->xfer_len =
  25219. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  25220. + }
  25221. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  25222. + }
  25223. +
  25224. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  25225. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25226. +
  25227. + if (core_if->dma_enable) {
  25228. + if (!core_if->dma_desc_enable) {
  25229. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25230. + deptsiz.d32);
  25231. +
  25232. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25233. + (uint32_t) ep->dma_addr);
  25234. + } else {
  25235. +#ifdef DWC_UTE_CFI
  25236. + /* The descriptor chain should be already initialized by now */
  25237. + if (ep->buff_mode != BM_STANDARD) {
  25238. + DWC_WRITE_REG32(&out_regs->doepdma,
  25239. + ep->descs_dma_addr);
  25240. + } else {
  25241. +#endif
  25242. + /** This is used for interrupt out transfers*/
  25243. + if (!ep->xfer_len)
  25244. + ep->xfer_len = ep->total_len;
  25245. + init_dma_desc_chain(core_if, ep);
  25246. +
  25247. + if (core_if->core_params->dev_out_nak) {
  25248. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  25249. + deptsiz.b.pktcnt = (ep->total_len +
  25250. + (ep->maxpacket - 1)) / ep->maxpacket;
  25251. + deptsiz.b.xfersize = ep->total_len;
  25252. + /* Remember initial value of doeptsiz */
  25253. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  25254. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25255. + deptsiz.d32);
  25256. + }
  25257. + }
  25258. + /** DOEPDMAn Register write */
  25259. + DWC_WRITE_REG32(&out_regs->doepdma,
  25260. + ep->dma_desc_addr);
  25261. +#ifdef DWC_UTE_CFI
  25262. + }
  25263. +#endif
  25264. + }
  25265. + } else {
  25266. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25267. + }
  25268. +
  25269. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  25270. + dsts_data_t dsts = {.d32 = 0};
  25271. + if (ep->bInterval == 1) {
  25272. + dsts.d32 =
  25273. + DWC_READ_REG32(&core_if->dev_if->
  25274. + dev_global_regs->dsts);
  25275. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  25276. + if (ep->frame_num > 0x3FFF) {
  25277. + ep->frm_overrun = 1;
  25278. + ep->frame_num &= 0x3FFF;
  25279. + } else
  25280. + ep->frm_overrun = 0;
  25281. +
  25282. + if (ep->frame_num & 0x1) {
  25283. + depctl.b.setd1pid = 1;
  25284. + } else {
  25285. + depctl.b.setd0pid = 1;
  25286. + }
  25287. + }
  25288. + }
  25289. +
  25290. + /* EP enable */
  25291. + depctl.b.cnak = 1;
  25292. + depctl.b.epena = 1;
  25293. +
  25294. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25295. +
  25296. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  25297. + DWC_READ_REG32(&out_regs->doepctl),
  25298. + DWC_READ_REG32(&out_regs->doeptsiz));
  25299. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  25300. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  25301. + daintmsk),
  25302. + DWC_READ_REG32(&core_if->core_global_regs->
  25303. + gintmsk));
  25304. +
  25305. + /* Timer is scheduling only for out bulk transfers for
  25306. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  25307. + * about received data payload in case of timeout
  25308. + */
  25309. + if (core_if->core_params->dev_out_nak) {
  25310. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  25311. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  25312. + core_if->ep_xfer_info[ep->num].ep = ep;
  25313. + core_if->ep_xfer_info[ep->num].state = 1;
  25314. +
  25315. + /* Start a timer for this transfer. */
  25316. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  25317. + }
  25318. + }
  25319. + }
  25320. +}
  25321. +
  25322. +/**
  25323. + * This function setup a zero length transfer in Buffer DMA and
  25324. + * Slave modes for usb requests with zero field set
  25325. + *
  25326. + * @param core_if Programming view of DWC_otg controller.
  25327. + * @param ep The EP to start the transfer on.
  25328. + *
  25329. + */
  25330. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25331. +{
  25332. +
  25333. + depctl_data_t depctl;
  25334. + deptsiz_data_t deptsiz;
  25335. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25336. +
  25337. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  25338. + DWC_PRINTF("zero length transfer is called\n");
  25339. +
  25340. + /* IN endpoint */
  25341. + if (ep->is_in == 1) {
  25342. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25343. + core_if->dev_if->in_ep_regs[ep->num];
  25344. +
  25345. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  25346. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  25347. +
  25348. + deptsiz.b.xfersize = 0;
  25349. + deptsiz.b.pktcnt = 1;
  25350. +
  25351. + /* Write the DMA register */
  25352. + if (core_if->dma_enable) {
  25353. + if (core_if->dma_desc_enable == 0) {
  25354. + deptsiz.b.mc = 1;
  25355. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25356. + deptsiz.d32);
  25357. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25358. + (uint32_t) ep->dma_addr);
  25359. + }
  25360. + } else {
  25361. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25362. + /**
  25363. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  25364. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  25365. + * the data will be written into the fifo by the ISR.
  25366. + */
  25367. + if (core_if->en_multiple_tx_fifo == 0) {
  25368. + intr_mask.b.nptxfempty = 1;
  25369. + DWC_MODIFY_REG32(&core_if->
  25370. + core_global_regs->gintmsk,
  25371. + intr_mask.d32, intr_mask.d32);
  25372. + } else {
  25373. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25374. + if (ep->xfer_len > 0) {
  25375. + uint32_t fifoemptymsk = 0;
  25376. + fifoemptymsk = 1 << ep->num;
  25377. + DWC_MODIFY_REG32(&core_if->
  25378. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25379. + 0, fifoemptymsk);
  25380. + }
  25381. + }
  25382. + }
  25383. +
  25384. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25385. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25386. + /* EP enable, IN data in FIFO */
  25387. + depctl.b.cnak = 1;
  25388. + depctl.b.epena = 1;
  25389. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25390. +
  25391. + } else {
  25392. + /* OUT endpoint */
  25393. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25394. + core_if->dev_if->out_ep_regs[ep->num];
  25395. +
  25396. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  25397. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  25398. +
  25399. + /* Zero Length Packet */
  25400. + deptsiz.b.xfersize = ep->maxpacket;
  25401. + deptsiz.b.pktcnt = 1;
  25402. +
  25403. + if (core_if->dma_enable) {
  25404. + if (!core_if->dma_desc_enable) {
  25405. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25406. + deptsiz.d32);
  25407. +
  25408. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25409. + (uint32_t) ep->dma_addr);
  25410. + }
  25411. + } else {
  25412. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25413. + }
  25414. +
  25415. + /* EP enable */
  25416. + depctl.b.cnak = 1;
  25417. + depctl.b.epena = 1;
  25418. +
  25419. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25420. +
  25421. + }
  25422. +}
  25423. +
  25424. +/**
  25425. + * This function does the setup for a data transfer for EP0 and starts
  25426. + * the transfer. For an IN transfer, the packets will be loaded into
  25427. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  25428. + * unloaded from the Rx FIFO in the ISR.
  25429. + *
  25430. + * @param core_if Programming view of DWC_otg controller.
  25431. + * @param ep The EP0 data.
  25432. + */
  25433. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25434. +{
  25435. + depctl_data_t depctl;
  25436. + deptsiz0_data_t deptsiz;
  25437. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25438. + dwc_otg_dev_dma_desc_t *dma_desc;
  25439. +
  25440. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  25441. + "xfer_buff=%p start_xfer_buff=%p \n",
  25442. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  25443. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  25444. +
  25445. + ep->total_len = ep->xfer_len;
  25446. +
  25447. + /* IN endpoint */
  25448. + if (ep->is_in == 1) {
  25449. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25450. + core_if->dev_if->in_ep_regs[0];
  25451. +
  25452. + gnptxsts_data_t gtxstatus;
  25453. +
  25454. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  25455. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25456. + if (depctl.b.epena)
  25457. + return;
  25458. + }
  25459. +
  25460. + gtxstatus.d32 =
  25461. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25462. +
  25463. + /* If dedicated FIFO every time flush fifo before enable ep*/
  25464. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  25465. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  25466. +
  25467. + if (core_if->en_multiple_tx_fifo == 0
  25468. + && gtxstatus.b.nptxqspcavail == 0
  25469. + && !core_if->dma_enable) {
  25470. +#ifdef DEBUG
  25471. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25472. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  25473. + DWC_READ_REG32(&in_regs->diepctl));
  25474. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  25475. + deptsiz.d32,
  25476. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25477. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  25478. + gtxstatus.d32);
  25479. +#endif
  25480. + return;
  25481. + }
  25482. +
  25483. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25484. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25485. +
  25486. + /* Zero Length Packet? */
  25487. + if (ep->xfer_len == 0) {
  25488. + deptsiz.b.xfersize = 0;
  25489. + deptsiz.b.pktcnt = 1;
  25490. + } else {
  25491. + /* Program the transfer size and packet count
  25492. + * as follows: xfersize = N * maxpacket +
  25493. + * short_packet pktcnt = N + (short_packet
  25494. + * exist ? 1 : 0)
  25495. + */
  25496. + if (ep->xfer_len > ep->maxpacket) {
  25497. + ep->xfer_len = ep->maxpacket;
  25498. + deptsiz.b.xfersize = ep->maxpacket;
  25499. + } else {
  25500. + deptsiz.b.xfersize = ep->xfer_len;
  25501. + }
  25502. + deptsiz.b.pktcnt = 1;
  25503. +
  25504. + }
  25505. + DWC_DEBUGPL(DBG_PCDV,
  25506. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25507. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25508. + deptsiz.d32);
  25509. +
  25510. + /* Write the DMA register */
  25511. + if (core_if->dma_enable) {
  25512. + if (core_if->dma_desc_enable == 0) {
  25513. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25514. + deptsiz.d32);
  25515. +
  25516. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25517. + (uint32_t) ep->dma_addr);
  25518. + } else {
  25519. + dma_desc = core_if->dev_if->in_desc_addr;
  25520. +
  25521. + /** DMA Descriptor Setup */
  25522. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25523. + dma_desc->status.b.l = 1;
  25524. + dma_desc->status.b.ioc = 1;
  25525. + dma_desc->status.b.sp =
  25526. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  25527. + dma_desc->status.b.bytes = ep->xfer_len;
  25528. + dma_desc->buf = ep->dma_addr;
  25529. + dma_desc->status.b.sts = 0;
  25530. + dma_desc->status.b.bs = BS_HOST_READY;
  25531. +
  25532. + /** DIEPDMA0 Register write */
  25533. + DWC_WRITE_REG32(&in_regs->diepdma,
  25534. + core_if->
  25535. + dev_if->dma_in_desc_addr);
  25536. + }
  25537. + } else {
  25538. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25539. + }
  25540. +
  25541. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25542. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25543. + /* EP enable, IN data in FIFO */
  25544. + depctl.b.cnak = 1;
  25545. + depctl.b.epena = 1;
  25546. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25547. +
  25548. + /**
  25549. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  25550. + * data will be written into the fifo by the ISR.
  25551. + */
  25552. + if (!core_if->dma_enable) {
  25553. + if (core_if->en_multiple_tx_fifo == 0) {
  25554. + intr_mask.b.nptxfempty = 1;
  25555. + DWC_MODIFY_REG32(&core_if->
  25556. + core_global_regs->gintmsk,
  25557. + intr_mask.d32, intr_mask.d32);
  25558. + } else {
  25559. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25560. + if (ep->xfer_len > 0) {
  25561. + uint32_t fifoemptymsk = 0;
  25562. + fifoemptymsk |= 1 << ep->num;
  25563. + DWC_MODIFY_REG32(&core_if->
  25564. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25565. + 0, fifoemptymsk);
  25566. + }
  25567. + }
  25568. + }
  25569. + } else {
  25570. + /* OUT endpoint */
  25571. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25572. + core_if->dev_if->out_ep_regs[0];
  25573. +
  25574. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  25575. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  25576. +
  25577. + /* Program the transfer size and packet count as follows:
  25578. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  25579. + * pktcnt = N */
  25580. + /* Zero Length Packet */
  25581. + deptsiz.b.xfersize = ep->maxpacket;
  25582. + deptsiz.b.pktcnt = 1;
  25583. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  25584. + deptsiz.b.supcnt = 3;
  25585. +
  25586. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  25587. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25588. +
  25589. + if (core_if->dma_enable) {
  25590. + if (!core_if->dma_desc_enable) {
  25591. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25592. + deptsiz.d32);
  25593. +
  25594. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25595. + (uint32_t) ep->dma_addr);
  25596. + } else {
  25597. + dma_desc = core_if->dev_if->out_desc_addr;
  25598. +
  25599. + /** DMA Descriptor Setup */
  25600. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25601. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  25602. + dma_desc->status.b.mtrf = 0;
  25603. + dma_desc->status.b.sr = 0;
  25604. + }
  25605. + dma_desc->status.b.l = 1;
  25606. + dma_desc->status.b.ioc = 1;
  25607. + dma_desc->status.b.bytes = ep->maxpacket;
  25608. + dma_desc->buf = ep->dma_addr;
  25609. + dma_desc->status.b.sts = 0;
  25610. + dma_desc->status.b.bs = BS_HOST_READY;
  25611. +
  25612. + /** DOEPDMA0 Register write */
  25613. + DWC_WRITE_REG32(&out_regs->doepdma,
  25614. + core_if->dev_if->
  25615. + dma_out_desc_addr);
  25616. + }
  25617. + } else {
  25618. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25619. + }
  25620. +
  25621. + /* EP enable */
  25622. + depctl.b.cnak = 1;
  25623. + depctl.b.epena = 1;
  25624. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  25625. + }
  25626. +}
  25627. +
  25628. +/**
  25629. + * This function continues control IN transfers started by
  25630. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  25631. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  25632. + * bit for the packet count.
  25633. + *
  25634. + * @param core_if Programming view of DWC_otg controller.
  25635. + * @param ep The EP0 data.
  25636. + */
  25637. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25638. +{
  25639. + depctl_data_t depctl;
  25640. + deptsiz0_data_t deptsiz;
  25641. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25642. + dwc_otg_dev_dma_desc_t *dma_desc;
  25643. +
  25644. + if (ep->is_in == 1) {
  25645. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25646. + core_if->dev_if->in_ep_regs[0];
  25647. + gnptxsts_data_t tx_status = {.d32 = 0 };
  25648. +
  25649. + tx_status.d32 =
  25650. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25651. + /** @todo Should there be check for room in the Tx
  25652. + * Status Queue. If not remove the code above this comment. */
  25653. +
  25654. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25655. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25656. +
  25657. + /* Program the transfer size and packet count
  25658. + * as follows: xfersize = N * maxpacket +
  25659. + * short_packet pktcnt = N + (short_packet
  25660. + * exist ? 1 : 0)
  25661. + */
  25662. +
  25663. + if (core_if->dma_desc_enable == 0) {
  25664. + deptsiz.b.xfersize =
  25665. + (ep->total_len - ep->xfer_count) >
  25666. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  25667. + ep->xfer_count);
  25668. + deptsiz.b.pktcnt = 1;
  25669. + if (core_if->dma_enable == 0) {
  25670. + ep->xfer_len += deptsiz.b.xfersize;
  25671. + } else {
  25672. + ep->xfer_len = deptsiz.b.xfersize;
  25673. + }
  25674. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25675. + } else {
  25676. + ep->xfer_len =
  25677. + (ep->total_len - ep->xfer_count) >
  25678. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  25679. + ep->xfer_count);
  25680. +
  25681. + dma_desc = core_if->dev_if->in_desc_addr;
  25682. +
  25683. + /** DMA Descriptor Setup */
  25684. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25685. + dma_desc->status.b.l = 1;
  25686. + dma_desc->status.b.ioc = 1;
  25687. + dma_desc->status.b.sp =
  25688. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  25689. + dma_desc->status.b.bytes = ep->xfer_len;
  25690. + dma_desc->buf = ep->dma_addr;
  25691. + dma_desc->status.b.sts = 0;
  25692. + dma_desc->status.b.bs = BS_HOST_READY;
  25693. +
  25694. + /** DIEPDMA0 Register write */
  25695. + DWC_WRITE_REG32(&in_regs->diepdma,
  25696. + core_if->dev_if->dma_in_desc_addr);
  25697. + }
  25698. +
  25699. + DWC_DEBUGPL(DBG_PCDV,
  25700. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25701. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25702. + deptsiz.d32);
  25703. +
  25704. + /* Write the DMA register */
  25705. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  25706. + if (core_if->dma_desc_enable == 0)
  25707. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25708. + (uint32_t) ep->dma_addr);
  25709. + }
  25710. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25711. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25712. + /* EP enable, IN data in FIFO */
  25713. + depctl.b.cnak = 1;
  25714. + depctl.b.epena = 1;
  25715. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25716. +
  25717. + /**
  25718. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  25719. + * data will be written into the fifo by the ISR.
  25720. + */
  25721. + if (!core_if->dma_enable) {
  25722. + if (core_if->en_multiple_tx_fifo == 0) {
  25723. + /* First clear it from GINTSTS */
  25724. + intr_mask.b.nptxfempty = 1;
  25725. + DWC_MODIFY_REG32(&core_if->
  25726. + core_global_regs->gintmsk,
  25727. + intr_mask.d32, intr_mask.d32);
  25728. +
  25729. + } else {
  25730. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25731. + if (ep->xfer_len > 0) {
  25732. + uint32_t fifoemptymsk = 0;
  25733. + fifoemptymsk |= 1 << ep->num;
  25734. + DWC_MODIFY_REG32(&core_if->
  25735. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25736. + 0, fifoemptymsk);
  25737. + }
  25738. + }
  25739. + }
  25740. + } else {
  25741. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25742. + core_if->dev_if->out_ep_regs[0];
  25743. +
  25744. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  25745. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  25746. +
  25747. + /* Program the transfer size and packet count
  25748. + * as follows: xfersize = N * maxpacket +
  25749. + * short_packet pktcnt = N + (short_packet
  25750. + * exist ? 1 : 0)
  25751. + */
  25752. + deptsiz.b.xfersize = ep->maxpacket;
  25753. + deptsiz.b.pktcnt = 1;
  25754. +
  25755. + if (core_if->dma_desc_enable == 0) {
  25756. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25757. + } else {
  25758. + dma_desc = core_if->dev_if->out_desc_addr;
  25759. +
  25760. + /** DMA Descriptor Setup */
  25761. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25762. + dma_desc->status.b.l = 1;
  25763. + dma_desc->status.b.ioc = 1;
  25764. + dma_desc->status.b.bytes = ep->maxpacket;
  25765. + dma_desc->buf = ep->dma_addr;
  25766. + dma_desc->status.b.sts = 0;
  25767. + dma_desc->status.b.bs = BS_HOST_READY;
  25768. +
  25769. + /** DOEPDMA0 Register write */
  25770. + DWC_WRITE_REG32(&out_regs->doepdma,
  25771. + core_if->dev_if->dma_out_desc_addr);
  25772. + }
  25773. +
  25774. + DWC_DEBUGPL(DBG_PCDV,
  25775. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25776. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25777. + deptsiz.d32);
  25778. +
  25779. + /* Write the DMA register */
  25780. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  25781. + if (core_if->dma_desc_enable == 0)
  25782. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25783. + (uint32_t) ep->dma_addr);
  25784. +
  25785. + }
  25786. +
  25787. + /* EP enable, IN data in FIFO */
  25788. + depctl.b.cnak = 1;
  25789. + depctl.b.epena = 1;
  25790. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25791. +
  25792. + }
  25793. +}
  25794. +
  25795. +#ifdef DEBUG
  25796. +void dump_msg(const u8 * buf, unsigned int length)
  25797. +{
  25798. + unsigned int start, num, i;
  25799. + char line[52], *p;
  25800. +
  25801. + if (length >= 512)
  25802. + return;
  25803. + start = 0;
  25804. + while (length > 0) {
  25805. + num = length < 16u ? length : 16u;
  25806. + p = line;
  25807. + for (i = 0; i < num; ++i) {
  25808. + if (i == 8)
  25809. + *p++ = ' ';
  25810. + DWC_SPRINTF(p, " %02x", buf[i]);
  25811. + p += 3;
  25812. + }
  25813. + *p = 0;
  25814. + DWC_PRINTF("%6x: %s\n", start, line);
  25815. + buf += num;
  25816. + start += num;
  25817. + length -= num;
  25818. + }
  25819. +}
  25820. +#else
  25821. +static inline void dump_msg(const u8 * buf, unsigned int length)
  25822. +{
  25823. +}
  25824. +#endif
  25825. +
  25826. +/**
  25827. + * This function writes a packet into the Tx FIFO associated with the
  25828. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  25829. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  25830. + * with all packets for the next micro-frame.
  25831. + *
  25832. + * @param core_if Programming view of DWC_otg controller.
  25833. + * @param ep The EP to write packet for.
  25834. + * @param dma Indicates if DMA is being used.
  25835. + */
  25836. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  25837. + int dma)
  25838. +{
  25839. + /**
  25840. + * The buffer is padded to DWORD on a per packet basis in
  25841. + * slave/dma mode if the MPS is not DWORD aligned. The last
  25842. + * packet, if short, is also padded to a multiple of DWORD.
  25843. + *
  25844. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  25845. + * multiple of DWORD in length
  25846. + *
  25847. + * ep->xfer_len can be any number of bytes
  25848. + *
  25849. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  25850. + * packet
  25851. + *
  25852. + * FIFO access is DWORD */
  25853. +
  25854. + uint32_t i;
  25855. + uint32_t byte_count;
  25856. + uint32_t dword_count;
  25857. + uint32_t *fifo;
  25858. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  25859. +
  25860. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  25861. + ep);
  25862. + if (ep->xfer_count >= ep->xfer_len) {
  25863. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  25864. + return;
  25865. + }
  25866. +
  25867. + /* Find the byte length of the packet either short packet or MPS */
  25868. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  25869. + byte_count = ep->xfer_len - ep->xfer_count;
  25870. + } else {
  25871. + byte_count = ep->maxpacket;
  25872. + }
  25873. +
  25874. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  25875. + * is not a multiple of DWORD */
  25876. + dword_count = (byte_count + 3) / 4;
  25877. +
  25878. +#ifdef VERBOSE
  25879. + dump_msg(ep->xfer_buff, byte_count);
  25880. +#endif
  25881. +
  25882. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  25883. + * intialized? What should this be? */
  25884. +
  25885. + fifo = core_if->data_fifo[ep->num];
  25886. +
  25887. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  25888. + fifo, data_buff, *data_buff, byte_count);
  25889. +
  25890. + if (!dma) {
  25891. + for (i = 0; i < dword_count; i++, data_buff++) {
  25892. + DWC_WRITE_REG32(fifo, *data_buff);
  25893. + }
  25894. + }
  25895. +
  25896. + ep->xfer_count += byte_count;
  25897. + ep->xfer_buff += byte_count;
  25898. + ep->dma_addr += byte_count;
  25899. +}
  25900. +
  25901. +/**
  25902. + * Set the EP STALL.
  25903. + *
  25904. + * @param core_if Programming view of DWC_otg controller.
  25905. + * @param ep The EP to set the stall on.
  25906. + */
  25907. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25908. +{
  25909. + depctl_data_t depctl;
  25910. + volatile uint32_t *depctl_addr;
  25911. +
  25912. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  25913. + (ep->is_in ? "IN" : "OUT"));
  25914. +
  25915. + if (ep->is_in == 1) {
  25916. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  25917. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25918. +
  25919. + /* set the disable and stall bits */
  25920. + if (depctl.b.epena) {
  25921. + depctl.b.epdis = 1;
  25922. + }
  25923. + depctl.b.stall = 1;
  25924. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25925. + } else {
  25926. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  25927. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25928. +
  25929. + /* set the stall bit */
  25930. + depctl.b.stall = 1;
  25931. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25932. + }
  25933. +
  25934. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  25935. +
  25936. + return;
  25937. +}
  25938. +
  25939. +/**
  25940. + * Clear the EP STALL.
  25941. + *
  25942. + * @param core_if Programming view of DWC_otg controller.
  25943. + * @param ep The EP to clear stall from.
  25944. + */
  25945. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25946. +{
  25947. + depctl_data_t depctl;
  25948. + volatile uint32_t *depctl_addr;
  25949. +
  25950. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  25951. + (ep->is_in ? "IN" : "OUT"));
  25952. +
  25953. + if (ep->is_in == 1) {
  25954. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  25955. + } else {
  25956. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  25957. + }
  25958. +
  25959. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25960. +
  25961. + /* clear the stall bits */
  25962. + depctl.b.stall = 0;
  25963. +
  25964. + /*
  25965. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  25966. + * of whether an endpoint has the Halt feature set, a
  25967. + * ClearFeature(ENDPOINT_HALT) request always results in the
  25968. + * data toggle being reinitialized to DATA0.
  25969. + */
  25970. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  25971. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  25972. + depctl.b.setd0pid = 1; /* DATA0 */
  25973. + }
  25974. +
  25975. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25976. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  25977. + return;
  25978. +}
  25979. +
  25980. +/**
  25981. + * This function reads a packet from the Rx FIFO into the destination
  25982. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  25983. + *
  25984. + * @param core_if Programming view of DWC_otg controller.
  25985. + * @param dest Destination buffer for the packet.
  25986. + * @param bytes Number of bytes to copy to the destination.
  25987. + */
  25988. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  25989. + uint8_t * dest, uint16_t bytes)
  25990. +{
  25991. + int i;
  25992. + int word_count = (bytes + 3) / 4;
  25993. +
  25994. + volatile uint32_t *fifo = core_if->data_fifo[0];
  25995. + uint32_t *data_buff = (uint32_t *) dest;
  25996. +
  25997. + /**
  25998. + * @todo Account for the case where _dest is not dword aligned. This
  25999. + * requires reading data from the FIFO into a uint32_t temp buffer,
  26000. + * then moving it into the data buffer.
  26001. + */
  26002. +
  26003. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  26004. + core_if, dest, bytes);
  26005. +
  26006. + for (i = 0; i < word_count; i++, data_buff++) {
  26007. + *data_buff = DWC_READ_REG32(fifo);
  26008. + }
  26009. +
  26010. + return;
  26011. +}
  26012. +
  26013. +/**
  26014. + * This functions reads the device registers and prints them
  26015. + *
  26016. + * @param core_if Programming view of DWC_otg controller.
  26017. + */
  26018. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  26019. +{
  26020. + int i;
  26021. + volatile uint32_t *addr;
  26022. +
  26023. + DWC_PRINTF("Device Global Registers\n");
  26024. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  26025. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  26026. + (unsigned long)addr, DWC_READ_REG32(addr));
  26027. + addr = &core_if->dev_if->dev_global_regs->dctl;
  26028. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  26029. + (unsigned long)addr, DWC_READ_REG32(addr));
  26030. + addr = &core_if->dev_if->dev_global_regs->dsts;
  26031. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  26032. + (unsigned long)addr, DWC_READ_REG32(addr));
  26033. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  26034. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26035. + DWC_READ_REG32(addr));
  26036. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  26037. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26038. + DWC_READ_REG32(addr));
  26039. + addr = &core_if->dev_if->dev_global_regs->daint;
  26040. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26041. + DWC_READ_REG32(addr));
  26042. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  26043. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26044. + DWC_READ_REG32(addr));
  26045. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  26046. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26047. + DWC_READ_REG32(addr));
  26048. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  26049. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  26050. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  26051. + (unsigned long)addr, DWC_READ_REG32(addr));
  26052. + }
  26053. +
  26054. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  26055. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26056. + DWC_READ_REG32(addr));
  26057. +
  26058. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  26059. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  26060. + (unsigned long)addr, DWC_READ_REG32(addr));
  26061. +
  26062. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  26063. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  26064. + (unsigned long)addr, DWC_READ_REG32(addr));
  26065. +
  26066. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  26067. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  26068. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  26069. + (unsigned long)addr, DWC_READ_REG32(addr));
  26070. + }
  26071. +
  26072. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  26073. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26074. + DWC_READ_REG32(addr));
  26075. +
  26076. + if (core_if->hwcfg2.b.multi_proc_int) {
  26077. +
  26078. + addr = &core_if->dev_if->dev_global_regs->deachint;
  26079. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  26080. + (unsigned long)addr, DWC_READ_REG32(addr));
  26081. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  26082. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  26083. + (unsigned long)addr, DWC_READ_REG32(addr));
  26084. +
  26085. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  26086. + addr =
  26087. + &core_if->dev_if->
  26088. + dev_global_regs->diepeachintmsk[i];
  26089. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  26090. + i, (unsigned long)addr,
  26091. + DWC_READ_REG32(addr));
  26092. + }
  26093. +
  26094. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  26095. + addr =
  26096. + &core_if->dev_if->
  26097. + dev_global_regs->doepeachintmsk[i];
  26098. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  26099. + i, (unsigned long)addr,
  26100. + DWC_READ_REG32(addr));
  26101. + }
  26102. + }
  26103. +
  26104. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  26105. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  26106. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  26107. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  26108. + (unsigned long)addr, DWC_READ_REG32(addr));
  26109. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  26110. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  26111. + (unsigned long)addr, DWC_READ_REG32(addr));
  26112. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  26113. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  26114. + (unsigned long)addr, DWC_READ_REG32(addr));
  26115. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  26116. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  26117. + (unsigned long)addr, DWC_READ_REG32(addr));
  26118. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  26119. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  26120. + (unsigned long)addr, DWC_READ_REG32(addr));
  26121. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  26122. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  26123. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  26124. + }
  26125. +
  26126. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  26127. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  26128. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  26129. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  26130. + (unsigned long)addr, DWC_READ_REG32(addr));
  26131. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  26132. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  26133. + (unsigned long)addr, DWC_READ_REG32(addr));
  26134. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  26135. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  26136. + (unsigned long)addr, DWC_READ_REG32(addr));
  26137. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  26138. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  26139. + (unsigned long)addr, DWC_READ_REG32(addr));
  26140. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  26141. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  26142. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  26143. + (unsigned long)addr, DWC_READ_REG32(addr));
  26144. + }
  26145. +
  26146. + }
  26147. +}
  26148. +
  26149. +/**
  26150. + * This functions reads the SPRAM and prints its content
  26151. + *
  26152. + * @param core_if Programming view of DWC_otg controller.
  26153. + */
  26154. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  26155. +{
  26156. + volatile uint8_t *addr, *start_addr, *end_addr;
  26157. +
  26158. + DWC_PRINTF("SPRAM Data:\n");
  26159. + start_addr = (void *)core_if->core_global_regs;
  26160. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  26161. + start_addr += 0x00028000;
  26162. + end_addr = (void *)core_if->core_global_regs;
  26163. + end_addr += 0x000280e0;
  26164. +
  26165. + for (addr = start_addr; addr < end_addr; addr += 16) {
  26166. + DWC_PRINTF
  26167. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  26168. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  26169. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  26170. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  26171. + );
  26172. + }
  26173. +
  26174. + return;
  26175. +}
  26176. +
  26177. +/**
  26178. + * This function reads the host registers and prints them
  26179. + *
  26180. + * @param core_if Programming view of DWC_otg controller.
  26181. + */
  26182. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  26183. +{
  26184. + int i;
  26185. + volatile uint32_t *addr;
  26186. +
  26187. + DWC_PRINTF("Host Global Registers\n");
  26188. + addr = &core_if->host_if->host_global_regs->hcfg;
  26189. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  26190. + (unsigned long)addr, DWC_READ_REG32(addr));
  26191. + addr = &core_if->host_if->host_global_regs->hfir;
  26192. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  26193. + (unsigned long)addr, DWC_READ_REG32(addr));
  26194. + addr = &core_if->host_if->host_global_regs->hfnum;
  26195. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26196. + DWC_READ_REG32(addr));
  26197. + addr = &core_if->host_if->host_global_regs->hptxsts;
  26198. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26199. + DWC_READ_REG32(addr));
  26200. + addr = &core_if->host_if->host_global_regs->haint;
  26201. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26202. + DWC_READ_REG32(addr));
  26203. + addr = &core_if->host_if->host_global_regs->haintmsk;
  26204. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26205. + DWC_READ_REG32(addr));
  26206. + if (core_if->dma_desc_enable) {
  26207. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  26208. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  26209. + (unsigned long)addr, DWC_READ_REG32(addr));
  26210. + }
  26211. +
  26212. + addr = core_if->host_if->hprt0;
  26213. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26214. + DWC_READ_REG32(addr));
  26215. +
  26216. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  26217. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  26218. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  26219. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  26220. + (unsigned long)addr, DWC_READ_REG32(addr));
  26221. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  26222. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  26223. + (unsigned long)addr, DWC_READ_REG32(addr));
  26224. + addr = &core_if->host_if->hc_regs[i]->hcint;
  26225. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  26226. + (unsigned long)addr, DWC_READ_REG32(addr));
  26227. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  26228. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  26229. + (unsigned long)addr, DWC_READ_REG32(addr));
  26230. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  26231. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  26232. + (unsigned long)addr, DWC_READ_REG32(addr));
  26233. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  26234. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  26235. + (unsigned long)addr, DWC_READ_REG32(addr));
  26236. + if (core_if->dma_desc_enable) {
  26237. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  26238. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  26239. + (unsigned long)addr, DWC_READ_REG32(addr));
  26240. + }
  26241. +
  26242. + }
  26243. + return;
  26244. +}
  26245. +
  26246. +/**
  26247. + * This function reads the core global registers and prints them
  26248. + *
  26249. + * @param core_if Programming view of DWC_otg controller.
  26250. + */
  26251. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  26252. +{
  26253. + int i, ep_num;
  26254. + volatile uint32_t *addr;
  26255. + char *txfsiz;
  26256. +
  26257. + DWC_PRINTF("Core Global Registers\n");
  26258. + addr = &core_if->core_global_regs->gotgctl;
  26259. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26260. + DWC_READ_REG32(addr));
  26261. + addr = &core_if->core_global_regs->gotgint;
  26262. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26263. + DWC_READ_REG32(addr));
  26264. + addr = &core_if->core_global_regs->gahbcfg;
  26265. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26266. + DWC_READ_REG32(addr));
  26267. + addr = &core_if->core_global_regs->gusbcfg;
  26268. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26269. + DWC_READ_REG32(addr));
  26270. + addr = &core_if->core_global_regs->grstctl;
  26271. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26272. + DWC_READ_REG32(addr));
  26273. + addr = &core_if->core_global_regs->gintsts;
  26274. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26275. + DWC_READ_REG32(addr));
  26276. + addr = &core_if->core_global_regs->gintmsk;
  26277. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26278. + DWC_READ_REG32(addr));
  26279. + addr = &core_if->core_global_regs->grxstsr;
  26280. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26281. + DWC_READ_REG32(addr));
  26282. + addr = &core_if->core_global_regs->grxfsiz;
  26283. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26284. + DWC_READ_REG32(addr));
  26285. + addr = &core_if->core_global_regs->gnptxfsiz;
  26286. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26287. + DWC_READ_REG32(addr));
  26288. + addr = &core_if->core_global_regs->gnptxsts;
  26289. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26290. + DWC_READ_REG32(addr));
  26291. + addr = &core_if->core_global_regs->gi2cctl;
  26292. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26293. + DWC_READ_REG32(addr));
  26294. + addr = &core_if->core_global_regs->gpvndctl;
  26295. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26296. + DWC_READ_REG32(addr));
  26297. + addr = &core_if->core_global_regs->ggpio;
  26298. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26299. + DWC_READ_REG32(addr));
  26300. + addr = &core_if->core_global_regs->guid;
  26301. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  26302. + (unsigned long)addr, DWC_READ_REG32(addr));
  26303. + addr = &core_if->core_global_regs->gsnpsid;
  26304. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26305. + DWC_READ_REG32(addr));
  26306. + addr = &core_if->core_global_regs->ghwcfg1;
  26307. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26308. + DWC_READ_REG32(addr));
  26309. + addr = &core_if->core_global_regs->ghwcfg2;
  26310. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26311. + DWC_READ_REG32(addr));
  26312. + addr = &core_if->core_global_regs->ghwcfg3;
  26313. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26314. + DWC_READ_REG32(addr));
  26315. + addr = &core_if->core_global_regs->ghwcfg4;
  26316. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26317. + DWC_READ_REG32(addr));
  26318. + addr = &core_if->core_global_regs->glpmcfg;
  26319. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26320. + DWC_READ_REG32(addr));
  26321. + addr = &core_if->core_global_regs->gpwrdn;
  26322. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26323. + DWC_READ_REG32(addr));
  26324. + addr = &core_if->core_global_regs->gdfifocfg;
  26325. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26326. + DWC_READ_REG32(addr));
  26327. + addr = &core_if->core_global_regs->adpctl;
  26328. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26329. + dwc_otg_adp_read_reg(core_if));
  26330. + addr = &core_if->core_global_regs->hptxfsiz;
  26331. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26332. + DWC_READ_REG32(addr));
  26333. +
  26334. + if (core_if->en_multiple_tx_fifo == 0) {
  26335. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  26336. + txfsiz = "DPTXFSIZ";
  26337. + } else {
  26338. + ep_num = core_if->hwcfg4.b.num_in_eps;
  26339. + txfsiz = "DIENPTXF";
  26340. + }
  26341. + for (i = 0; i < ep_num; i++) {
  26342. + addr = &core_if->core_global_regs->dtxfsiz[i];
  26343. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  26344. + (unsigned long)addr, DWC_READ_REG32(addr));
  26345. + }
  26346. + addr = core_if->pcgcctl;
  26347. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26348. + DWC_READ_REG32(addr));
  26349. +}
  26350. +
  26351. +/**
  26352. + * Flush a Tx FIFO.
  26353. + *
  26354. + * @param core_if Programming view of DWC_otg controller.
  26355. + * @param num Tx FIFO to flush.
  26356. + */
  26357. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  26358. +{
  26359. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26360. + volatile grstctl_t greset = {.d32 = 0 };
  26361. + int count = 0;
  26362. +
  26363. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  26364. +
  26365. + greset.b.txfflsh = 1;
  26366. + greset.b.txfnum = num;
  26367. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26368. +
  26369. + do {
  26370. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26371. + if (++count > 10000) {
  26372. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  26373. + __func__, greset.d32,
  26374. + DWC_READ_REG32(&global_regs->gnptxsts));
  26375. + break;
  26376. + }
  26377. + dwc_udelay(1);
  26378. + } while (greset.b.txfflsh == 1);
  26379. +
  26380. + /* Wait for 3 PHY Clocks */
  26381. + dwc_udelay(1);
  26382. +}
  26383. +
  26384. +/**
  26385. + * Flush Rx FIFO.
  26386. + *
  26387. + * @param core_if Programming view of DWC_otg controller.
  26388. + */
  26389. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  26390. +{
  26391. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26392. + volatile grstctl_t greset = {.d32 = 0 };
  26393. + int count = 0;
  26394. +
  26395. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  26396. + /*
  26397. + *
  26398. + */
  26399. + greset.b.rxfflsh = 1;
  26400. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26401. +
  26402. + do {
  26403. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26404. + if (++count > 10000) {
  26405. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  26406. + greset.d32);
  26407. + break;
  26408. + }
  26409. + dwc_udelay(1);
  26410. + } while (greset.b.rxfflsh == 1);
  26411. +
  26412. + /* Wait for 3 PHY Clocks */
  26413. + dwc_udelay(1);
  26414. +}
  26415. +
  26416. +/**
  26417. + * Do core a soft reset of the core. Be careful with this because it
  26418. + * resets all the internal state machines of the core.
  26419. + */
  26420. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  26421. +{
  26422. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26423. + volatile grstctl_t greset = {.d32 = 0 };
  26424. + int count = 0;
  26425. +
  26426. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  26427. + /* Wait for AHB master IDLE state. */
  26428. + do {
  26429. + dwc_udelay(10);
  26430. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26431. + if (++count > 100000) {
  26432. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  26433. + greset.d32);
  26434. + return;
  26435. + }
  26436. + }
  26437. + while (greset.b.ahbidle == 0);
  26438. +
  26439. + /* Core Soft Reset */
  26440. + count = 0;
  26441. + greset.b.csftrst = 1;
  26442. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26443. + do {
  26444. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26445. + if (++count > 10000) {
  26446. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  26447. + __func__, greset.d32);
  26448. + break;
  26449. + }
  26450. + dwc_udelay(1);
  26451. + }
  26452. + while (greset.b.csftrst == 1);
  26453. +
  26454. + /* Wait for 3 PHY Clocks */
  26455. + dwc_mdelay(100);
  26456. +}
  26457. +
  26458. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  26459. +{
  26460. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  26461. +}
  26462. +
  26463. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  26464. +{
  26465. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  26466. +}
  26467. +
  26468. +/**
  26469. + * Register HCD callbacks. The callbacks are used to start and stop
  26470. + * the HCD for interrupt processing.
  26471. + *
  26472. + * @param core_if Programming view of DWC_otg controller.
  26473. + * @param cb the HCD callback structure.
  26474. + * @param p pointer to be passed to callback function (usb_hcd*).
  26475. + */
  26476. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  26477. + dwc_otg_cil_callbacks_t * cb, void *p)
  26478. +{
  26479. + core_if->hcd_cb = cb;
  26480. + cb->p = p;
  26481. +}
  26482. +
  26483. +/**
  26484. + * Register PCD callbacks. The callbacks are used to start and stop
  26485. + * the PCD for interrupt processing.
  26486. + *
  26487. + * @param core_if Programming view of DWC_otg controller.
  26488. + * @param cb the PCD callback structure.
  26489. + * @param p pointer to be passed to callback function (pcd*).
  26490. + */
  26491. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  26492. + dwc_otg_cil_callbacks_t * cb, void *p)
  26493. +{
  26494. + core_if->pcd_cb = cb;
  26495. + cb->p = p;
  26496. +}
  26497. +
  26498. +#ifdef DWC_EN_ISOC
  26499. +
  26500. +/**
  26501. + * This function writes isoc data per 1 (micro)frame into tx fifo
  26502. + *
  26503. + * @param core_if Programming view of DWC_otg controller.
  26504. + * @param ep The EP to start the transfer on.
  26505. + *
  26506. + */
  26507. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  26508. +{
  26509. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  26510. + dtxfsts_data_t txstatus = {.d32 = 0 };
  26511. + uint32_t len = 0;
  26512. + uint32_t dwords;
  26513. +
  26514. + ep->xfer_len = ep->data_per_frame;
  26515. + ep->xfer_count = 0;
  26516. +
  26517. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  26518. +
  26519. + len = ep->xfer_len - ep->xfer_count;
  26520. +
  26521. + if (len > ep->maxpacket) {
  26522. + len = ep->maxpacket;
  26523. + }
  26524. +
  26525. + dwords = (len + 3) / 4;
  26526. +
  26527. + /* While there is space in the queue and space in the FIFO and
  26528. + * More data to tranfer, Write packets to the Tx FIFO */
  26529. + txstatus.d32 =
  26530. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  26531. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  26532. +
  26533. + while (txstatus.b.txfspcavail > dwords &&
  26534. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  26535. + /* Write the FIFO */
  26536. + dwc_otg_ep_write_packet(core_if, ep, 0);
  26537. +
  26538. + len = ep->xfer_len - ep->xfer_count;
  26539. + if (len > ep->maxpacket) {
  26540. + len = ep->maxpacket;
  26541. + }
  26542. +
  26543. + dwords = (len + 3) / 4;
  26544. + txstatus.d32 =
  26545. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  26546. + dtxfsts);
  26547. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  26548. + txstatus.d32);
  26549. + }
  26550. +}
  26551. +
  26552. +/**
  26553. + * This function initializes a descriptor chain for Isochronous transfer
  26554. + *
  26555. + * @param core_if Programming view of DWC_otg controller.
  26556. + * @param ep The EP to start the transfer on.
  26557. + *
  26558. + */
  26559. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  26560. + dwc_ep_t * ep)
  26561. +{
  26562. + deptsiz_data_t deptsiz = {.d32 = 0 };
  26563. + depctl_data_t depctl = {.d32 = 0 };
  26564. + dsts_data_t dsts = {.d32 = 0 };
  26565. + volatile uint32_t *addr;
  26566. +
  26567. + if (ep->is_in) {
  26568. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  26569. + } else {
  26570. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  26571. + }
  26572. +
  26573. + ep->xfer_len = ep->data_per_frame;
  26574. + ep->xfer_count = 0;
  26575. + ep->xfer_buff = ep->cur_pkt_addr;
  26576. + ep->dma_addr = ep->cur_pkt_dma_addr;
  26577. +
  26578. + if (ep->is_in) {
  26579. + /* Program the transfer size and packet count
  26580. + * as follows: xfersize = N * maxpacket +
  26581. + * short_packet pktcnt = N + (short_packet
  26582. + * exist ? 1 : 0)
  26583. + */
  26584. + deptsiz.b.xfersize = ep->xfer_len;
  26585. + deptsiz.b.pktcnt =
  26586. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  26587. + deptsiz.b.mc = deptsiz.b.pktcnt;
  26588. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  26589. + deptsiz.d32);
  26590. +
  26591. + /* Write the DMA register */
  26592. + if (core_if->dma_enable) {
  26593. + DWC_WRITE_REG32(&
  26594. + (core_if->dev_if->in_ep_regs[ep->num]->
  26595. + diepdma), (uint32_t) ep->dma_addr);
  26596. + }
  26597. + } else {
  26598. + deptsiz.b.pktcnt =
  26599. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  26600. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  26601. +
  26602. + DWC_WRITE_REG32(&core_if->dev_if->
  26603. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  26604. +
  26605. + if (core_if->dma_enable) {
  26606. + DWC_WRITE_REG32(&
  26607. + (core_if->dev_if->
  26608. + out_ep_regs[ep->num]->doepdma),
  26609. + (uint32_t) ep->dma_addr);
  26610. + }
  26611. + }
  26612. +
  26613. + /** Enable endpoint, clear nak */
  26614. +
  26615. + depctl.d32 = 0;
  26616. + if (ep->bInterval == 1) {
  26617. + dsts.d32 =
  26618. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  26619. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  26620. +
  26621. + if (ep->next_frame & 0x1) {
  26622. + depctl.b.setd1pid = 1;
  26623. + } else {
  26624. + depctl.b.setd0pid = 1;
  26625. + }
  26626. + } else {
  26627. + ep->next_frame += ep->bInterval;
  26628. +
  26629. + if (ep->next_frame & 0x1) {
  26630. + depctl.b.setd1pid = 1;
  26631. + } else {
  26632. + depctl.b.setd0pid = 1;
  26633. + }
  26634. + }
  26635. + depctl.b.epena = 1;
  26636. + depctl.b.cnak = 1;
  26637. +
  26638. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  26639. + depctl.d32 = DWC_READ_REG32(addr);
  26640. +
  26641. + if (ep->is_in && core_if->dma_enable == 0) {
  26642. + write_isoc_frame_data(core_if, ep);
  26643. + }
  26644. +
  26645. +}
  26646. +#endif /* DWC_EN_ISOC */
  26647. +
  26648. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  26649. +{
  26650. + int i;
  26651. + for (i = 0; i < size; i++) {
  26652. + p[i] = -1;
  26653. + }
  26654. +}
  26655. +
  26656. +static int dwc_otg_param_initialized(int32_t val)
  26657. +{
  26658. + return val != -1;
  26659. +}
  26660. +
  26661. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  26662. +{
  26663. + int i;
  26664. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  26665. + if (!core_if->core_params) {
  26666. + return -DWC_E_NO_MEMORY;
  26667. + }
  26668. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  26669. + sizeof(*core_if->core_params) /
  26670. + sizeof(int32_t));
  26671. + DWC_PRINTF("Setting default values for core params\n");
  26672. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  26673. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  26674. + dwc_otg_set_param_dma_desc_enable(core_if,
  26675. + dwc_param_dma_desc_enable_default);
  26676. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  26677. + dwc_otg_set_param_dma_burst_size(core_if,
  26678. + dwc_param_dma_burst_size_default);
  26679. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  26680. + dwc_param_host_support_fs_ls_low_power_default);
  26681. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  26682. + dwc_param_enable_dynamic_fifo_default);
  26683. + dwc_otg_set_param_data_fifo_size(core_if,
  26684. + dwc_param_data_fifo_size_default);
  26685. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  26686. + dwc_param_dev_rx_fifo_size_default);
  26687. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  26688. + dwc_param_dev_nperio_tx_fifo_size_default);
  26689. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  26690. + dwc_param_host_rx_fifo_size_default);
  26691. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  26692. + dwc_param_host_nperio_tx_fifo_size_default);
  26693. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  26694. + dwc_param_host_perio_tx_fifo_size_default);
  26695. + dwc_otg_set_param_max_transfer_size(core_if,
  26696. + dwc_param_max_transfer_size_default);
  26697. + dwc_otg_set_param_max_packet_count(core_if,
  26698. + dwc_param_max_packet_count_default);
  26699. + dwc_otg_set_param_host_channels(core_if,
  26700. + dwc_param_host_channels_default);
  26701. + dwc_otg_set_param_dev_endpoints(core_if,
  26702. + dwc_param_dev_endpoints_default);
  26703. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  26704. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  26705. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  26706. + dwc_param_host_ls_low_power_phy_clk_default);
  26707. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  26708. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  26709. + dwc_param_phy_ulpi_ext_vbus_default);
  26710. + dwc_otg_set_param_phy_utmi_width(core_if,
  26711. + dwc_param_phy_utmi_width_default);
  26712. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  26713. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  26714. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  26715. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  26716. + dwc_param_en_multiple_tx_fifo_default);
  26717. + for (i = 0; i < 15; i++) {
  26718. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  26719. + dwc_param_dev_perio_tx_fifo_size_default,
  26720. + i);
  26721. + }
  26722. +
  26723. + for (i = 0; i < 15; i++) {
  26724. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  26725. + dwc_param_dev_tx_fifo_size_default,
  26726. + i);
  26727. + }
  26728. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  26729. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  26730. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  26731. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  26732. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  26733. + dwc_otg_set_param_tx_thr_length(core_if,
  26734. + dwc_param_tx_thr_length_default);
  26735. + dwc_otg_set_param_rx_thr_length(core_if,
  26736. + dwc_param_rx_thr_length_default);
  26737. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  26738. + dwc_param_ahb_thr_ratio_default);
  26739. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  26740. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  26741. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  26742. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  26743. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  26744. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  26745. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  26746. + DWC_PRINTF("Finished setting default values for core params\n");
  26747. +
  26748. + return 0;
  26749. +}
  26750. +
  26751. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  26752. +{
  26753. + return core_if->dma_enable;
  26754. +}
  26755. +
  26756. +/* Checks if the parameter is outside of its valid range of values */
  26757. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  26758. + (((_param_) < (_low_)) || \
  26759. + ((_param_) > (_high_)))
  26760. +
  26761. +/* Parameter access functions */
  26762. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  26763. +{
  26764. + int valid;
  26765. + int retval = 0;
  26766. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  26767. + DWC_WARN("Wrong value for otg_cap parameter\n");
  26768. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  26769. + retval = -DWC_E_INVALID;
  26770. + goto out;
  26771. + }
  26772. +
  26773. + valid = 1;
  26774. + switch (val) {
  26775. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  26776. + if (core_if->hwcfg2.b.op_mode !=
  26777. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26778. + valid = 0;
  26779. + break;
  26780. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  26781. + if ((core_if->hwcfg2.b.op_mode !=
  26782. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26783. + && (core_if->hwcfg2.b.op_mode !=
  26784. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  26785. + && (core_if->hwcfg2.b.op_mode !=
  26786. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  26787. + && (core_if->hwcfg2.b.op_mode !=
  26788. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  26789. + valid = 0;
  26790. + }
  26791. + break;
  26792. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  26793. + /* always valid */
  26794. + break;
  26795. + }
  26796. + if (!valid) {
  26797. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  26798. + DWC_ERROR
  26799. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  26800. + val);
  26801. + }
  26802. + val =
  26803. + (((core_if->hwcfg2.b.op_mode ==
  26804. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26805. + || (core_if->hwcfg2.b.op_mode ==
  26806. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  26807. + || (core_if->hwcfg2.b.op_mode ==
  26808. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  26809. + || (core_if->hwcfg2.b.op_mode ==
  26810. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  26811. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  26812. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  26813. + retval = -DWC_E_INVALID;
  26814. + }
  26815. +
  26816. + core_if->core_params->otg_cap = val;
  26817. +out:
  26818. + return retval;
  26819. +}
  26820. +
  26821. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  26822. +{
  26823. + return core_if->core_params->otg_cap;
  26824. +}
  26825. +
  26826. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  26827. +{
  26828. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26829. + DWC_WARN("Wrong value for opt parameter\n");
  26830. + return -DWC_E_INVALID;
  26831. + }
  26832. + core_if->core_params->opt = val;
  26833. + return 0;
  26834. +}
  26835. +
  26836. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  26837. +{
  26838. + return core_if->core_params->opt;
  26839. +}
  26840. +
  26841. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  26842. +{
  26843. + int retval = 0;
  26844. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26845. + DWC_WARN("Wrong value for dma enable\n");
  26846. + return -DWC_E_INVALID;
  26847. + }
  26848. +
  26849. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  26850. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  26851. + DWC_ERROR
  26852. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  26853. + val);
  26854. + }
  26855. + val = 0;
  26856. + retval = -DWC_E_INVALID;
  26857. + }
  26858. +
  26859. + core_if->core_params->dma_enable = val;
  26860. + if (val == 0) {
  26861. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  26862. + }
  26863. + return retval;
  26864. +}
  26865. +
  26866. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  26867. +{
  26868. + return core_if->core_params->dma_enable;
  26869. +}
  26870. +
  26871. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  26872. +{
  26873. + int retval = 0;
  26874. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26875. + DWC_WARN("Wrong value for dma_enable\n");
  26876. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  26877. + return -DWC_E_INVALID;
  26878. + }
  26879. +
  26880. + if ((val == 1)
  26881. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  26882. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  26883. + if (dwc_otg_param_initialized
  26884. + (core_if->core_params->dma_desc_enable)) {
  26885. + DWC_ERROR
  26886. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  26887. + val);
  26888. + }
  26889. + val = 0;
  26890. + retval = -DWC_E_INVALID;
  26891. + }
  26892. + core_if->core_params->dma_desc_enable = val;
  26893. + return retval;
  26894. +}
  26895. +
  26896. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  26897. +{
  26898. + return core_if->core_params->dma_desc_enable;
  26899. +}
  26900. +
  26901. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  26902. + int32_t val)
  26903. +{
  26904. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26905. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  26906. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  26907. + return -DWC_E_INVALID;
  26908. + }
  26909. + core_if->core_params->host_support_fs_ls_low_power = val;
  26910. + return 0;
  26911. +}
  26912. +
  26913. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  26914. + core_if)
  26915. +{
  26916. + return core_if->core_params->host_support_fs_ls_low_power;
  26917. +}
  26918. +
  26919. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  26920. + int32_t val)
  26921. +{
  26922. + int retval = 0;
  26923. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26924. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  26925. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  26926. + return -DWC_E_INVALID;
  26927. + }
  26928. +
  26929. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  26930. + if (dwc_otg_param_initialized
  26931. + (core_if->core_params->enable_dynamic_fifo)) {
  26932. + DWC_ERROR
  26933. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  26934. + val);
  26935. + }
  26936. + val = 0;
  26937. + retval = -DWC_E_INVALID;
  26938. + }
  26939. + core_if->core_params->enable_dynamic_fifo = val;
  26940. + return retval;
  26941. +}
  26942. +
  26943. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  26944. +{
  26945. + return core_if->core_params->enable_dynamic_fifo;
  26946. +}
  26947. +
  26948. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  26949. +{
  26950. + int retval = 0;
  26951. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  26952. + DWC_WARN("Wrong value for data_fifo_size\n");
  26953. + DWC_WARN("data_fifo_size must be 32-32768\n");
  26954. + return -DWC_E_INVALID;
  26955. + }
  26956. +
  26957. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  26958. + if (dwc_otg_param_initialized
  26959. + (core_if->core_params->data_fifo_size)) {
  26960. + DWC_ERROR
  26961. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  26962. + val);
  26963. + }
  26964. + val = core_if->hwcfg3.b.dfifo_depth;
  26965. + retval = -DWC_E_INVALID;
  26966. + }
  26967. +
  26968. + core_if->core_params->data_fifo_size = val;
  26969. + return retval;
  26970. +}
  26971. +
  26972. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  26973. +{
  26974. + return core_if->core_params->data_fifo_size;
  26975. +}
  26976. +
  26977. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  26978. +{
  26979. + int retval = 0;
  26980. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  26981. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  26982. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  26983. + return -DWC_E_INVALID;
  26984. + }
  26985. +
  26986. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  26987. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  26988. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  26989. + }
  26990. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  26991. + retval = -DWC_E_INVALID;
  26992. + }
  26993. +
  26994. + core_if->core_params->dev_rx_fifo_size = val;
  26995. + return retval;
  26996. +}
  26997. +
  26998. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  26999. +{
  27000. + return core_if->core_params->dev_rx_fifo_size;
  27001. +}
  27002. +
  27003. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27004. + int32_t val)
  27005. +{
  27006. + int retval = 0;
  27007. +
  27008. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27009. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  27010. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  27011. + return -DWC_E_INVALID;
  27012. + }
  27013. +
  27014. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  27015. + if (dwc_otg_param_initialized
  27016. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  27017. + DWC_ERROR
  27018. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  27019. + val);
  27020. + }
  27021. + val =
  27022. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  27023. + 16);
  27024. + retval = -DWC_E_INVALID;
  27025. + }
  27026. +
  27027. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  27028. + return retval;
  27029. +}
  27030. +
  27031. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27032. +{
  27033. + return core_if->core_params->dev_nperio_tx_fifo_size;
  27034. +}
  27035. +
  27036. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  27037. + int32_t val)
  27038. +{
  27039. + int retval = 0;
  27040. +
  27041. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27042. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  27043. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  27044. + return -DWC_E_INVALID;
  27045. + }
  27046. +
  27047. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  27048. + if (dwc_otg_param_initialized
  27049. + (core_if->core_params->host_rx_fifo_size)) {
  27050. + DWC_ERROR
  27051. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  27052. + val);
  27053. + }
  27054. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  27055. + retval = -DWC_E_INVALID;
  27056. + }
  27057. +
  27058. + core_if->core_params->host_rx_fifo_size = val;
  27059. + return retval;
  27060. +
  27061. +}
  27062. +
  27063. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  27064. +{
  27065. + return core_if->core_params->host_rx_fifo_size;
  27066. +}
  27067. +
  27068. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27069. + int32_t val)
  27070. +{
  27071. + int retval = 0;
  27072. +
  27073. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27074. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  27075. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  27076. + return -DWC_E_INVALID;
  27077. + }
  27078. +
  27079. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  27080. + if (dwc_otg_param_initialized
  27081. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  27082. + DWC_ERROR
  27083. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  27084. + val);
  27085. + }
  27086. + val =
  27087. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  27088. + 16);
  27089. + retval = -DWC_E_INVALID;
  27090. + }
  27091. +
  27092. + core_if->core_params->host_nperio_tx_fifo_size = val;
  27093. + return retval;
  27094. +}
  27095. +
  27096. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27097. +{
  27098. + return core_if->core_params->host_nperio_tx_fifo_size;
  27099. +}
  27100. +
  27101. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27102. + int32_t val)
  27103. +{
  27104. + int retval = 0;
  27105. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27106. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  27107. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  27108. + return -DWC_E_INVALID;
  27109. + }
  27110. +
  27111. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  27112. + if (dwc_otg_param_initialized
  27113. + (core_if->core_params->host_perio_tx_fifo_size)) {
  27114. + DWC_ERROR
  27115. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  27116. + val);
  27117. + }
  27118. + val = (core_if->hptxfsiz.d32) >> 16;
  27119. + retval = -DWC_E_INVALID;
  27120. + }
  27121. +
  27122. + core_if->core_params->host_perio_tx_fifo_size = val;
  27123. + return retval;
  27124. +}
  27125. +
  27126. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27127. +{
  27128. + return core_if->core_params->host_perio_tx_fifo_size;
  27129. +}
  27130. +
  27131. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  27132. + int32_t val)
  27133. +{
  27134. + int retval = 0;
  27135. +
  27136. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  27137. + DWC_WARN("Wrong value for max_transfer_size\n");
  27138. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  27139. + return -DWC_E_INVALID;
  27140. + }
  27141. +
  27142. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  27143. + if (dwc_otg_param_initialized
  27144. + (core_if->core_params->max_transfer_size)) {
  27145. + DWC_ERROR
  27146. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  27147. + val);
  27148. + }
  27149. + val =
  27150. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  27151. + 1);
  27152. + retval = -DWC_E_INVALID;
  27153. + }
  27154. +
  27155. + core_if->core_params->max_transfer_size = val;
  27156. + return retval;
  27157. +}
  27158. +
  27159. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  27160. +{
  27161. + return core_if->core_params->max_transfer_size;
  27162. +}
  27163. +
  27164. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  27165. +{
  27166. + int retval = 0;
  27167. +
  27168. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  27169. + DWC_WARN("Wrong value for max_packet_count\n");
  27170. + DWC_WARN("max_packet_count must be 15-511\n");
  27171. + return -DWC_E_INVALID;
  27172. + }
  27173. +
  27174. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  27175. + if (dwc_otg_param_initialized
  27176. + (core_if->core_params->max_packet_count)) {
  27177. + DWC_ERROR
  27178. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  27179. + val);
  27180. + }
  27181. + val =
  27182. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  27183. + retval = -DWC_E_INVALID;
  27184. + }
  27185. +
  27186. + core_if->core_params->max_packet_count = val;
  27187. + return retval;
  27188. +}
  27189. +
  27190. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  27191. +{
  27192. + return core_if->core_params->max_packet_count;
  27193. +}
  27194. +
  27195. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  27196. +{
  27197. + int retval = 0;
  27198. +
  27199. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  27200. + DWC_WARN("Wrong value for host_channels\n");
  27201. + DWC_WARN("host_channels must be 1-16\n");
  27202. + return -DWC_E_INVALID;
  27203. + }
  27204. +
  27205. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  27206. + if (dwc_otg_param_initialized
  27207. + (core_if->core_params->host_channels)) {
  27208. + DWC_ERROR
  27209. + ("%d invalid for host_channels. Check HW configurations.\n",
  27210. + val);
  27211. + }
  27212. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  27213. + retval = -DWC_E_INVALID;
  27214. + }
  27215. +
  27216. + core_if->core_params->host_channels = val;
  27217. + return retval;
  27218. +}
  27219. +
  27220. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  27221. +{
  27222. + return core_if->core_params->host_channels;
  27223. +}
  27224. +
  27225. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  27226. +{
  27227. + int retval = 0;
  27228. +
  27229. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  27230. + DWC_WARN("Wrong value for dev_endpoints\n");
  27231. + DWC_WARN("dev_endpoints must be 1-15\n");
  27232. + return -DWC_E_INVALID;
  27233. + }
  27234. +
  27235. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  27236. + if (dwc_otg_param_initialized
  27237. + (core_if->core_params->dev_endpoints)) {
  27238. + DWC_ERROR
  27239. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  27240. + val);
  27241. + }
  27242. + val = core_if->hwcfg2.b.num_dev_ep;
  27243. + retval = -DWC_E_INVALID;
  27244. + }
  27245. +
  27246. + core_if->core_params->dev_endpoints = val;
  27247. + return retval;
  27248. +}
  27249. +
  27250. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  27251. +{
  27252. + return core_if->core_params->dev_endpoints;
  27253. +}
  27254. +
  27255. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  27256. +{
  27257. + int retval = 0;
  27258. + int valid = 0;
  27259. +
  27260. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  27261. + DWC_WARN("Wrong value for phy_type\n");
  27262. + DWC_WARN("phy_type must be 0,1 or 2\n");
  27263. + return -DWC_E_INVALID;
  27264. + }
  27265. +#ifndef NO_FS_PHY_HW_CHECKS
  27266. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  27267. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  27268. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  27269. + valid = 1;
  27270. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  27271. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  27272. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  27273. + valid = 1;
  27274. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  27275. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  27276. + valid = 1;
  27277. + }
  27278. + if (!valid) {
  27279. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  27280. + DWC_ERROR
  27281. + ("%d invalid for phy_type. Check HW configurations.\n",
  27282. + val);
  27283. + }
  27284. + if (core_if->hwcfg2.b.hs_phy_type) {
  27285. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  27286. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  27287. + val = DWC_PHY_TYPE_PARAM_UTMI;
  27288. + } else {
  27289. + val = DWC_PHY_TYPE_PARAM_ULPI;
  27290. + }
  27291. + }
  27292. + retval = -DWC_E_INVALID;
  27293. + }
  27294. +#endif
  27295. + core_if->core_params->phy_type = val;
  27296. + return retval;
  27297. +}
  27298. +
  27299. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  27300. +{
  27301. + return core_if->core_params->phy_type;
  27302. +}
  27303. +
  27304. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  27305. +{
  27306. + int retval = 0;
  27307. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27308. + DWC_WARN("Wrong value for speed parameter\n");
  27309. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  27310. + return -DWC_E_INVALID;
  27311. + }
  27312. + if ((val == 0)
  27313. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  27314. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  27315. + DWC_ERROR
  27316. + ("%d invalid for speed paremter. Check HW configuration.\n",
  27317. + val);
  27318. + }
  27319. + val =
  27320. + (dwc_otg_get_param_phy_type(core_if) ==
  27321. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  27322. + retval = -DWC_E_INVALID;
  27323. + }
  27324. + core_if->core_params->speed = val;
  27325. + return retval;
  27326. +}
  27327. +
  27328. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  27329. +{
  27330. + return core_if->core_params->speed;
  27331. +}
  27332. +
  27333. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  27334. + int32_t val)
  27335. +{
  27336. + int retval = 0;
  27337. +
  27338. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27339. + DWC_WARN
  27340. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  27341. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  27342. + return -DWC_E_INVALID;
  27343. + }
  27344. +
  27345. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  27346. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  27347. + if (dwc_otg_param_initialized
  27348. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  27349. + DWC_ERROR
  27350. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  27351. + val);
  27352. + }
  27353. + val =
  27354. + (dwc_otg_get_param_phy_type(core_if) ==
  27355. + DWC_PHY_TYPE_PARAM_FS) ?
  27356. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  27357. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  27358. + retval = -DWC_E_INVALID;
  27359. + }
  27360. +
  27361. + core_if->core_params->host_ls_low_power_phy_clk = val;
  27362. + return retval;
  27363. +}
  27364. +
  27365. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  27366. +{
  27367. + return core_if->core_params->host_ls_low_power_phy_clk;
  27368. +}
  27369. +
  27370. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  27371. +{
  27372. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27373. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  27374. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  27375. + return -DWC_E_INVALID;
  27376. + }
  27377. +
  27378. + core_if->core_params->phy_ulpi_ddr = val;
  27379. + return 0;
  27380. +}
  27381. +
  27382. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  27383. +{
  27384. + return core_if->core_params->phy_ulpi_ddr;
  27385. +}
  27386. +
  27387. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  27388. + int32_t val)
  27389. +{
  27390. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27391. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  27392. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  27393. + return -DWC_E_INVALID;
  27394. + }
  27395. +
  27396. + core_if->core_params->phy_ulpi_ext_vbus = val;
  27397. + return 0;
  27398. +}
  27399. +
  27400. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  27401. +{
  27402. + return core_if->core_params->phy_ulpi_ext_vbus;
  27403. +}
  27404. +
  27405. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  27406. +{
  27407. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  27408. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  27409. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  27410. + return -DWC_E_INVALID;
  27411. + }
  27412. +
  27413. + core_if->core_params->phy_utmi_width = val;
  27414. + return 0;
  27415. +}
  27416. +
  27417. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  27418. +{
  27419. + return core_if->core_params->phy_utmi_width;
  27420. +}
  27421. +
  27422. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  27423. +{
  27424. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27425. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  27426. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  27427. + return -DWC_E_INVALID;
  27428. + }
  27429. +
  27430. + core_if->core_params->ulpi_fs_ls = val;
  27431. + return 0;
  27432. +}
  27433. +
  27434. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  27435. +{
  27436. + return core_if->core_params->ulpi_fs_ls;
  27437. +}
  27438. +
  27439. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  27440. +{
  27441. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27442. + DWC_WARN("Wrong valaue for ts_dline\n");
  27443. + DWC_WARN("ts_dline must be 0 or 1\n");
  27444. + return -DWC_E_INVALID;
  27445. + }
  27446. +
  27447. + core_if->core_params->ts_dline = val;
  27448. + return 0;
  27449. +}
  27450. +
  27451. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  27452. +{
  27453. + return core_if->core_params->ts_dline;
  27454. +}
  27455. +
  27456. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27457. +{
  27458. + int retval = 0;
  27459. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27460. + DWC_WARN("Wrong valaue for i2c_enable\n");
  27461. + DWC_WARN("i2c_enable must be 0 or 1\n");
  27462. + return -DWC_E_INVALID;
  27463. + }
  27464. +#ifndef NO_FS_PHY_HW_CHECK
  27465. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  27466. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  27467. + DWC_ERROR
  27468. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  27469. + val);
  27470. + }
  27471. + val = 0;
  27472. + retval = -DWC_E_INVALID;
  27473. + }
  27474. +#endif
  27475. +
  27476. + core_if->core_params->i2c_enable = val;
  27477. + return retval;
  27478. +}
  27479. +
  27480. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  27481. +{
  27482. + return core_if->core_params->i2c_enable;
  27483. +}
  27484. +
  27485. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27486. + int32_t val, int fifo_num)
  27487. +{
  27488. + int retval = 0;
  27489. +
  27490. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  27491. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  27492. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  27493. + return -DWC_E_INVALID;
  27494. + }
  27495. +
  27496. + if (val >
  27497. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  27498. + if (dwc_otg_param_initialized
  27499. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  27500. + DWC_ERROR
  27501. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  27502. + val, fifo_num);
  27503. + }
  27504. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  27505. + retval = -DWC_E_INVALID;
  27506. + }
  27507. +
  27508. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  27509. + return retval;
  27510. +}
  27511. +
  27512. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27513. + int fifo_num)
  27514. +{
  27515. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  27516. +}
  27517. +
  27518. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  27519. + int32_t val)
  27520. +{
  27521. + int retval = 0;
  27522. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27523. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  27524. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  27525. + return -DWC_E_INVALID;
  27526. + }
  27527. +
  27528. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  27529. + if (dwc_otg_param_initialized
  27530. + (core_if->core_params->en_multiple_tx_fifo)) {
  27531. + DWC_ERROR
  27532. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  27533. + val);
  27534. + }
  27535. + val = 0;
  27536. + retval = -DWC_E_INVALID;
  27537. + }
  27538. +
  27539. + core_if->core_params->en_multiple_tx_fifo = val;
  27540. + return retval;
  27541. +}
  27542. +
  27543. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  27544. +{
  27545. + return core_if->core_params->en_multiple_tx_fifo;
  27546. +}
  27547. +
  27548. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  27549. + int fifo_num)
  27550. +{
  27551. + int retval = 0;
  27552. +
  27553. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  27554. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  27555. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  27556. + return -DWC_E_INVALID;
  27557. + }
  27558. +
  27559. + if (val >
  27560. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  27561. + if (dwc_otg_param_initialized
  27562. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  27563. + DWC_ERROR
  27564. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  27565. + val, fifo_num);
  27566. + }
  27567. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  27568. + retval = -DWC_E_INVALID;
  27569. + }
  27570. +
  27571. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  27572. + return retval;
  27573. +}
  27574. +
  27575. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27576. + int fifo_num)
  27577. +{
  27578. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  27579. +}
  27580. +
  27581. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  27582. +{
  27583. + int retval = 0;
  27584. +
  27585. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  27586. + DWC_WARN("Wrong value for thr_ctl\n");
  27587. + DWC_WARN("thr_ctl must be 0-7\n");
  27588. + return -DWC_E_INVALID;
  27589. + }
  27590. +
  27591. + if ((val != 0) &&
  27592. + (!dwc_otg_get_param_dma_enable(core_if) ||
  27593. + !core_if->hwcfg4.b.ded_fifo_en)) {
  27594. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  27595. + DWC_ERROR
  27596. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  27597. + val);
  27598. + }
  27599. + val = 0;
  27600. + retval = -DWC_E_INVALID;
  27601. + }
  27602. +
  27603. + core_if->core_params->thr_ctl = val;
  27604. + return retval;
  27605. +}
  27606. +
  27607. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  27608. +{
  27609. + return core_if->core_params->thr_ctl;
  27610. +}
  27611. +
  27612. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27613. +{
  27614. + int retval = 0;
  27615. +
  27616. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27617. + DWC_WARN("Wrong value for lpm_enable\n");
  27618. + DWC_WARN("lpm_enable must be 0 or 1\n");
  27619. + return -DWC_E_INVALID;
  27620. + }
  27621. +
  27622. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  27623. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  27624. + DWC_ERROR
  27625. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  27626. + val);
  27627. + }
  27628. + val = 0;
  27629. + retval = -DWC_E_INVALID;
  27630. + }
  27631. +
  27632. + core_if->core_params->lpm_enable = val;
  27633. + return retval;
  27634. +}
  27635. +
  27636. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  27637. +{
  27638. + return core_if->core_params->lpm_enable;
  27639. +}
  27640. +
  27641. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  27642. +{
  27643. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  27644. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  27645. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  27646. + return -DWC_E_INVALID;
  27647. + }
  27648. +
  27649. + core_if->core_params->tx_thr_length = val;
  27650. + return 0;
  27651. +}
  27652. +
  27653. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  27654. +{
  27655. + return core_if->core_params->tx_thr_length;
  27656. +}
  27657. +
  27658. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  27659. +{
  27660. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  27661. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  27662. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  27663. + return -DWC_E_INVALID;
  27664. + }
  27665. +
  27666. + core_if->core_params->rx_thr_length = val;
  27667. + return 0;
  27668. +}
  27669. +
  27670. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  27671. +{
  27672. + return core_if->core_params->rx_thr_length;
  27673. +}
  27674. +
  27675. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  27676. +{
  27677. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  27678. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  27679. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  27680. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  27681. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  27682. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  27683. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  27684. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  27685. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  27686. + return -DWC_E_INVALID;
  27687. + }
  27688. + core_if->core_params->dma_burst_size = val;
  27689. + return 0;
  27690. +}
  27691. +
  27692. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  27693. +{
  27694. + return core_if->core_params->dma_burst_size;
  27695. +}
  27696. +
  27697. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27698. +{
  27699. + int retval = 0;
  27700. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27701. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  27702. + return -DWC_E_INVALID;
  27703. + }
  27704. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  27705. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  27706. + DWC_ERROR
  27707. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  27708. + val);
  27709. + }
  27710. + retval = -DWC_E_INVALID;
  27711. + val = 0;
  27712. + }
  27713. + core_if->core_params->pti_enable = val;
  27714. + return retval;
  27715. +}
  27716. +
  27717. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  27718. +{
  27719. + return core_if->core_params->pti_enable;
  27720. +}
  27721. +
  27722. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27723. +{
  27724. + int retval = 0;
  27725. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27726. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  27727. + return -DWC_E_INVALID;
  27728. + }
  27729. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  27730. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  27731. + DWC_ERROR
  27732. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  27733. + val);
  27734. + }
  27735. + retval = -DWC_E_INVALID;
  27736. + val = 0;
  27737. + }
  27738. + core_if->core_params->mpi_enable = val;
  27739. + return retval;
  27740. +}
  27741. +
  27742. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  27743. +{
  27744. + return core_if->core_params->mpi_enable;
  27745. +}
  27746. +
  27747. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27748. +{
  27749. + int retval = 0;
  27750. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27751. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  27752. + return -DWC_E_INVALID;
  27753. + }
  27754. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  27755. + if (dwc_otg_param_initialized
  27756. + (core_if->core_params->adp_supp_enable)) {
  27757. + DWC_ERROR
  27758. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  27759. + val);
  27760. + }
  27761. + retval = -DWC_E_INVALID;
  27762. + val = 0;
  27763. + }
  27764. + core_if->core_params->adp_supp_enable = val;
  27765. + /*Set OTG version 2.0 in case of enabling ADP*/
  27766. + if (val)
  27767. + dwc_otg_set_param_otg_ver(core_if, 1);
  27768. +
  27769. + return retval;
  27770. +}
  27771. +
  27772. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  27773. +{
  27774. + return core_if->core_params->adp_supp_enable;
  27775. +}
  27776. +
  27777. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  27778. +{
  27779. + int retval = 0;
  27780. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27781. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  27782. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  27783. + return -DWC_E_INVALID;
  27784. + }
  27785. +
  27786. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  27787. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  27788. + DWC_ERROR
  27789. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  27790. + val);
  27791. + }
  27792. + retval = -DWC_E_INVALID;
  27793. + val = 0;
  27794. + }
  27795. + core_if->core_params->ic_usb_cap = val;
  27796. + return retval;
  27797. +}
  27798. +
  27799. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  27800. +{
  27801. + return core_if->core_params->ic_usb_cap;
  27802. +}
  27803. +
  27804. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  27805. +{
  27806. + int retval = 0;
  27807. + int valid = 1;
  27808. +
  27809. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  27810. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  27811. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  27812. + return -DWC_E_INVALID;
  27813. + }
  27814. +
  27815. + if (val
  27816. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  27817. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  27818. + valid = 0;
  27819. + } else if (val
  27820. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  27821. + 4)) {
  27822. + valid = 0;
  27823. + }
  27824. + if (valid == 0) {
  27825. + if (dwc_otg_param_initialized
  27826. + (core_if->core_params->ahb_thr_ratio)) {
  27827. + DWC_ERROR
  27828. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  27829. + val);
  27830. + }
  27831. + retval = -DWC_E_INVALID;
  27832. + val = 0;
  27833. + }
  27834. +
  27835. + core_if->core_params->ahb_thr_ratio = val;
  27836. + return retval;
  27837. +}
  27838. +
  27839. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  27840. +{
  27841. + return core_if->core_params->ahb_thr_ratio;
  27842. +}
  27843. +
  27844. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  27845. +{
  27846. + int retval = 0;
  27847. + int valid = 1;
  27848. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  27849. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  27850. +
  27851. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  27852. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  27853. + DWC_WARN("power_down must be 0 - 2\n");
  27854. + return -DWC_E_INVALID;
  27855. + }
  27856. +
  27857. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  27858. + valid = 0;
  27859. + }
  27860. + if ((val == 3)
  27861. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  27862. + || (hwcfg4.b.xhiber == 0))) {
  27863. + valid = 0;
  27864. + }
  27865. + if (valid == 0) {
  27866. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  27867. + DWC_ERROR
  27868. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  27869. + val);
  27870. + }
  27871. + retval = -DWC_E_INVALID;
  27872. + val = 0;
  27873. + }
  27874. + core_if->core_params->power_down = val;
  27875. + return retval;
  27876. +}
  27877. +
  27878. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  27879. +{
  27880. + return core_if->core_params->power_down;
  27881. +}
  27882. +
  27883. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  27884. +{
  27885. + int retval = 0;
  27886. + int valid = 1;
  27887. +
  27888. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27889. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  27890. + DWC_WARN("reload_ctl must be 0 or 1\n");
  27891. + return -DWC_E_INVALID;
  27892. + }
  27893. +
  27894. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  27895. + valid = 0;
  27896. + }
  27897. + if (valid == 0) {
  27898. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  27899. + DWC_ERROR("%d invalid for parameter reload_ctl."
  27900. + "Check HW configuration.\n", val);
  27901. + }
  27902. + retval = -DWC_E_INVALID;
  27903. + val = 0;
  27904. + }
  27905. + core_if->core_params->reload_ctl = val;
  27906. + return retval;
  27907. +}
  27908. +
  27909. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  27910. +{
  27911. + return core_if->core_params->reload_ctl;
  27912. +}
  27913. +
  27914. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  27915. +{
  27916. + int retval = 0;
  27917. + int valid = 1;
  27918. +
  27919. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27920. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  27921. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  27922. + return -DWC_E_INVALID;
  27923. + }
  27924. +
  27925. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  27926. + !(core_if->core_params->dma_desc_enable))) {
  27927. + valid = 0;
  27928. + }
  27929. + if (valid == 0) {
  27930. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  27931. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  27932. + "Check HW configuration.\n", val);
  27933. + }
  27934. + retval = -DWC_E_INVALID;
  27935. + val = 0;
  27936. + }
  27937. + core_if->core_params->dev_out_nak = val;
  27938. + return retval;
  27939. +}
  27940. +
  27941. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  27942. +{
  27943. + return core_if->core_params->dev_out_nak;
  27944. +}
  27945. +
  27946. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  27947. +{
  27948. + int retval = 0;
  27949. + int valid = 1;
  27950. +
  27951. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27952. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  27953. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  27954. + return -DWC_E_INVALID;
  27955. + }
  27956. +
  27957. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  27958. + !(core_if->core_params->dma_desc_enable))) {
  27959. + valid = 0;
  27960. + }
  27961. + if (valid == 0) {
  27962. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  27963. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  27964. + "Check HW configuration.\n", val);
  27965. + }
  27966. + retval = -DWC_E_INVALID;
  27967. + val = 0;
  27968. + }
  27969. + core_if->core_params->cont_on_bna = val;
  27970. + return retval;
  27971. +}
  27972. +
  27973. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  27974. +{
  27975. + return core_if->core_params->cont_on_bna;
  27976. +}
  27977. +
  27978. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  27979. +{
  27980. + int retval = 0;
  27981. + int valid = 1;
  27982. +
  27983. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27984. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  27985. + DWC_WARN("ahb_single must be 0 or 1\n");
  27986. + return -DWC_E_INVALID;
  27987. + }
  27988. +
  27989. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  27990. + valid = 0;
  27991. + }
  27992. + if (valid == 0) {
  27993. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  27994. + DWC_ERROR("%d invalid for parameter ahb_single."
  27995. + "Check HW configuration.\n", val);
  27996. + }
  27997. + retval = -DWC_E_INVALID;
  27998. + val = 0;
  27999. + }
  28000. + core_if->core_params->ahb_single = val;
  28001. + return retval;
  28002. +}
  28003. +
  28004. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  28005. +{
  28006. + return core_if->core_params->ahb_single;
  28007. +}
  28008. +
  28009. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  28010. +{
  28011. + int retval = 0;
  28012. +
  28013. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  28014. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  28015. + DWC_WARN
  28016. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  28017. + return -DWC_E_INVALID;
  28018. + }
  28019. +
  28020. + core_if->core_params->otg_ver = val;
  28021. + return retval;
  28022. +}
  28023. +
  28024. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  28025. +{
  28026. + return core_if->core_params->otg_ver;
  28027. +}
  28028. +
  28029. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  28030. +{
  28031. + gotgctl_data_t otgctl;
  28032. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28033. + return otgctl.b.hstnegscs;
  28034. +}
  28035. +
  28036. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  28037. +{
  28038. + gotgctl_data_t otgctl;
  28039. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28040. + return otgctl.b.sesreqscs;
  28041. +}
  28042. +
  28043. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  28044. +{
  28045. + if(core_if->otg_ver == 0) {
  28046. + gotgctl_data_t otgctl;
  28047. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28048. + otgctl.b.hnpreq = val;
  28049. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  28050. + } else {
  28051. + core_if->otg_sts = val;
  28052. + }
  28053. +}
  28054. +
  28055. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  28056. +{
  28057. + return core_if->snpsid;
  28058. +}
  28059. +
  28060. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  28061. +{
  28062. + gintsts_data_t gintsts;
  28063. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  28064. + return gintsts.b.curmode;
  28065. +}
  28066. +
  28067. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  28068. +{
  28069. + gusbcfg_data_t usbcfg;
  28070. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28071. + return usbcfg.b.hnpcap;
  28072. +}
  28073. +
  28074. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  28075. +{
  28076. + gusbcfg_data_t usbcfg;
  28077. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28078. + usbcfg.b.hnpcap = val;
  28079. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  28080. +}
  28081. +
  28082. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  28083. +{
  28084. + gusbcfg_data_t usbcfg;
  28085. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28086. + return usbcfg.b.srpcap;
  28087. +}
  28088. +
  28089. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  28090. +{
  28091. + gusbcfg_data_t usbcfg;
  28092. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28093. + usbcfg.b.srpcap = val;
  28094. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  28095. +}
  28096. +
  28097. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  28098. +{
  28099. + dcfg_data_t dcfg;
  28100. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  28101. +
  28102. + dcfg.d32 = -1; //GRAYG
  28103. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  28104. + if (NULL == core_if)
  28105. + DWC_ERROR("reg request with NULL core_if\n");
  28106. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  28107. + core_if, core_if->dev_if);
  28108. + if (NULL == core_if->dev_if)
  28109. + DWC_ERROR("reg request with NULL dev_if\n");
  28110. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  28111. + "dev_global_regs(%p)\n", __func__,
  28112. + core_if, core_if->dev_if,
  28113. + core_if->dev_if->dev_global_regs);
  28114. + if (NULL == core_if->dev_if->dev_global_regs)
  28115. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  28116. + else {
  28117. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  28118. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  28119. + core_if, core_if->dev_if,
  28120. + core_if->dev_if->dev_global_regs,
  28121. + &core_if->dev_if->dev_global_regs->dcfg);
  28122. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  28123. + }
  28124. + return dcfg.b.devspd;
  28125. +}
  28126. +
  28127. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  28128. +{
  28129. + dcfg_data_t dcfg;
  28130. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  28131. + dcfg.b.devspd = val;
  28132. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  28133. +}
  28134. +
  28135. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  28136. +{
  28137. + hprt0_data_t hprt0;
  28138. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28139. + return hprt0.b.prtconnsts;
  28140. +}
  28141. +
  28142. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  28143. +{
  28144. + dsts_data_t dsts;
  28145. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  28146. + return dsts.b.enumspd;
  28147. +}
  28148. +
  28149. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  28150. +{
  28151. + hprt0_data_t hprt0;
  28152. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28153. + return hprt0.b.prtpwr;
  28154. +
  28155. +}
  28156. +
  28157. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  28158. +{
  28159. + return core_if->hibernation_suspend;
  28160. +}
  28161. +
  28162. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  28163. +{
  28164. + hprt0_data_t hprt0;
  28165. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28166. + hprt0.b.prtpwr = val;
  28167. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28168. +}
  28169. +
  28170. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  28171. +{
  28172. + hprt0_data_t hprt0;
  28173. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28174. + return hprt0.b.prtsusp;
  28175. +
  28176. +}
  28177. +
  28178. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  28179. +{
  28180. + hprt0_data_t hprt0;
  28181. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28182. + hprt0.b.prtsusp = val;
  28183. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28184. +}
  28185. +
  28186. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  28187. +{
  28188. + hfir_data_t hfir;
  28189. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  28190. + return hfir.b.frint;
  28191. +
  28192. +}
  28193. +
  28194. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  28195. +{
  28196. + hfir_data_t hfir;
  28197. + uint32_t fram_int;
  28198. + fram_int = calc_frame_interval(core_if);
  28199. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  28200. + if (!core_if->core_params->reload_ctl) {
  28201. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  28202. + "not set to 1.\nShould load driver with reload_ctl=1"
  28203. + " module parameter\n");
  28204. + return;
  28205. + }
  28206. + switch (fram_int) {
  28207. + case 3750:
  28208. + if ((val < 3350) || (val > 4150)) {
  28209. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  28210. + "clock freq should be from 3350 to 4150\n");
  28211. + return;
  28212. + }
  28213. + break;
  28214. + case 30000:
  28215. + if ((val < 26820) || (val > 33180)) {
  28216. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  28217. + "clock freq should be from 26820 to 33180\n");
  28218. + return;
  28219. + }
  28220. + break;
  28221. + case 6000:
  28222. + if ((val < 5360) || (val > 6640)) {
  28223. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  28224. + "clock freq should be from 5360 to 6640\n");
  28225. + return;
  28226. + }
  28227. + break;
  28228. + case 48000:
  28229. + if ((val < 42912) || (val > 53088)) {
  28230. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  28231. + "clock freq should be from 42912 to 53088\n");
  28232. + return;
  28233. + }
  28234. + break;
  28235. + case 7500:
  28236. + if ((val < 6700) || (val > 8300)) {
  28237. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  28238. + "clock freq should be from 6700 to 8300\n");
  28239. + return;
  28240. + }
  28241. + break;
  28242. + case 60000:
  28243. + if ((val < 53640) || (val > 65536)) {
  28244. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  28245. + "clock freq should be from 53640 to 65536\n");
  28246. + return;
  28247. + }
  28248. + break;
  28249. + default:
  28250. + DWC_WARN("Unknown frame interval\n");
  28251. + return;
  28252. + break;
  28253. +
  28254. + }
  28255. + hfir.b.frint = val;
  28256. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  28257. +}
  28258. +
  28259. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  28260. +{
  28261. + hcfg_data_t hcfg;
  28262. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  28263. + return hcfg.b.modechtimen;
  28264. +
  28265. +}
  28266. +
  28267. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  28268. +{
  28269. + hcfg_data_t hcfg;
  28270. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  28271. + hcfg.b.modechtimen = val;
  28272. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  28273. +}
  28274. +
  28275. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  28276. +{
  28277. + hprt0_data_t hprt0;
  28278. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28279. + hprt0.b.prtres = val;
  28280. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28281. +}
  28282. +
  28283. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  28284. +{
  28285. + dctl_data_t dctl;
  28286. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  28287. + return dctl.b.rmtwkupsig;
  28288. +}
  28289. +
  28290. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  28291. +{
  28292. + glpmcfg_data_t lpmcfg;
  28293. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28294. +
  28295. + DWC_ASSERT(!
  28296. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  28297. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  28298. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  28299. +
  28300. + return lpmcfg.b.prt_sleep_sts;
  28301. +}
  28302. +
  28303. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  28304. +{
  28305. + glpmcfg_data_t lpmcfg;
  28306. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28307. + return lpmcfg.b.rem_wkup_en;
  28308. +}
  28309. +
  28310. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  28311. +{
  28312. + glpmcfg_data_t lpmcfg;
  28313. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28314. + return lpmcfg.b.appl_resp;
  28315. +}
  28316. +
  28317. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  28318. +{
  28319. + glpmcfg_data_t lpmcfg;
  28320. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28321. + lpmcfg.b.appl_resp = val;
  28322. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28323. +}
  28324. +
  28325. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  28326. +{
  28327. + glpmcfg_data_t lpmcfg;
  28328. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28329. + return lpmcfg.b.hsic_connect;
  28330. +}
  28331. +
  28332. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  28333. +{
  28334. + glpmcfg_data_t lpmcfg;
  28335. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28336. + lpmcfg.b.hsic_connect = val;
  28337. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28338. +}
  28339. +
  28340. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  28341. +{
  28342. + glpmcfg_data_t lpmcfg;
  28343. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28344. + return lpmcfg.b.inv_sel_hsic;
  28345. +
  28346. +}
  28347. +
  28348. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  28349. +{
  28350. + glpmcfg_data_t lpmcfg;
  28351. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28352. + lpmcfg.b.inv_sel_hsic = val;
  28353. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28354. +}
  28355. +
  28356. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  28357. +{
  28358. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28359. +}
  28360. +
  28361. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  28362. +{
  28363. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  28364. +}
  28365. +
  28366. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  28367. +{
  28368. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28369. +}
  28370. +
  28371. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  28372. +{
  28373. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  28374. +}
  28375. +
  28376. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  28377. +{
  28378. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  28379. +}
  28380. +
  28381. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  28382. +{
  28383. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  28384. +}
  28385. +
  28386. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  28387. +{
  28388. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  28389. +}
  28390. +
  28391. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  28392. +{
  28393. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  28394. +}
  28395. +
  28396. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  28397. +{
  28398. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  28399. +}
  28400. +
  28401. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  28402. +{
  28403. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  28404. +}
  28405. +
  28406. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  28407. +{
  28408. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  28409. +}
  28410. +
  28411. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  28412. +{
  28413. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  28414. +}
  28415. +
  28416. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  28417. +{
  28418. + return DWC_READ_REG32(core_if->host_if->hprt0);
  28419. +
  28420. +}
  28421. +
  28422. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  28423. +{
  28424. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  28425. +}
  28426. +
  28427. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  28428. +{
  28429. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  28430. +}
  28431. +
  28432. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  28433. +{
  28434. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  28435. +}
  28436. +
  28437. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  28438. +{
  28439. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  28440. +}
  28441. +
  28442. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  28443. +{
  28444. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  28445. +}
  28446. +
  28447. +/**
  28448. + * Start the SRP timer to detect when the SRP does not complete within
  28449. + * 6 seconds.
  28450. + *
  28451. + * @param core_if the pointer to core_if strucure.
  28452. + */
  28453. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  28454. +{
  28455. + core_if->srp_timer_started = 1;
  28456. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  28457. +}
  28458. +
  28459. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  28460. +{
  28461. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  28462. + gotgctl_data_t mem;
  28463. + gotgctl_data_t val;
  28464. +
  28465. + val.d32 = DWC_READ_REG32(addr);
  28466. + if (val.b.sesreq) {
  28467. + DWC_ERROR("Session Request Already active!\n");
  28468. + return;
  28469. + }
  28470. +
  28471. + DWC_INFO("Session Request Initated\n"); //NOTICE
  28472. + mem.d32 = DWC_READ_REG32(addr);
  28473. + mem.b.sesreq = 1;
  28474. + DWC_WRITE_REG32(addr, mem.d32);
  28475. +
  28476. + /* Start the SRP timer */
  28477. + dwc_otg_pcd_start_srp_timer(core_if);
  28478. + return;
  28479. +}
  28480. --- /dev/null
  28481. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  28482. @@ -0,0 +1,1464 @@
  28483. +/* ==========================================================================
  28484. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  28485. + * $Revision: #123 $
  28486. + * $Date: 2012/08/10 $
  28487. + * $Change: 2047372 $
  28488. + *
  28489. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  28490. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  28491. + * otherwise expressly agreed to in writing between Synopsys and you.
  28492. + *
  28493. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  28494. + * any End User Software License Agreement or Agreement for Licensed Product
  28495. + * with Synopsys or any supplement thereto. You are permitted to use and
  28496. + * redistribute this Software in source and binary forms, with or without
  28497. + * modification, provided that redistributions of source code must retain this
  28498. + * notice. You may not view, use, disclose, copy or distribute this file or
  28499. + * any information contained herein except pursuant to this license grant from
  28500. + * Synopsys. If you do not agree with this notice, including the disclaimer
  28501. + * below, then you are not authorized to use the Software.
  28502. + *
  28503. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  28504. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28505. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28506. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  28507. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28508. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28509. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28510. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28511. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28512. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  28513. + * DAMAGE.
  28514. + * ========================================================================== */
  28515. +
  28516. +#if !defined(__DWC_CIL_H__)
  28517. +#define __DWC_CIL_H__
  28518. +
  28519. +#include "dwc_list.h"
  28520. +#include "dwc_otg_dbg.h"
  28521. +#include "dwc_otg_regs.h"
  28522. +
  28523. +#include "dwc_otg_core_if.h"
  28524. +#include "dwc_otg_adp.h"
  28525. +
  28526. +/**
  28527. + * @file
  28528. + * This file contains the interface to the Core Interface Layer.
  28529. + */
  28530. +
  28531. +#ifdef DWC_UTE_CFI
  28532. +
  28533. +#define MAX_DMA_DESCS_PER_EP 256
  28534. +
  28535. +/**
  28536. + * Enumeration for the data buffer mode
  28537. + */
  28538. +typedef enum _data_buffer_mode {
  28539. + BM_STANDARD = 0, /* data buffer is in normal mode */
  28540. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  28541. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  28542. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  28543. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  28544. +} data_buffer_mode_e;
  28545. +#endif //DWC_UTE_CFI
  28546. +
  28547. +/** Macros defined for DWC OTG HW Release version */
  28548. +
  28549. +#define OTG_CORE_REV_2_60a 0x4F54260A
  28550. +#define OTG_CORE_REV_2_71a 0x4F54271A
  28551. +#define OTG_CORE_REV_2_72a 0x4F54272A
  28552. +#define OTG_CORE_REV_2_80a 0x4F54280A
  28553. +#define OTG_CORE_REV_2_81a 0x4F54281A
  28554. +#define OTG_CORE_REV_2_90a 0x4F54290A
  28555. +#define OTG_CORE_REV_2_91a 0x4F54291A
  28556. +#define OTG_CORE_REV_2_92a 0x4F54292A
  28557. +#define OTG_CORE_REV_2_93a 0x4F54293A
  28558. +#define OTG_CORE_REV_2_94a 0x4F54294A
  28559. +#define OTG_CORE_REV_3_00a 0x4F54300A
  28560. +
  28561. +/**
  28562. + * Information for each ISOC packet.
  28563. + */
  28564. +typedef struct iso_pkt_info {
  28565. + uint32_t offset;
  28566. + uint32_t length;
  28567. + int32_t status;
  28568. +} iso_pkt_info_t;
  28569. +
  28570. +/**
  28571. + * The <code>dwc_ep</code> structure represents the state of a single
  28572. + * endpoint when acting in device mode. It contains the data items
  28573. + * needed for an endpoint to be activated and transfer packets.
  28574. + */
  28575. +typedef struct dwc_ep {
  28576. + /** EP number used for register address lookup */
  28577. + uint8_t num;
  28578. + /** EP direction 0 = OUT */
  28579. + unsigned is_in:1;
  28580. + /** EP active. */
  28581. + unsigned active:1;
  28582. +
  28583. + /**
  28584. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  28585. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  28586. + unsigned tx_fifo_num:4;
  28587. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  28588. + unsigned type:2;
  28589. +#define DWC_OTG_EP_TYPE_CONTROL 0
  28590. +#define DWC_OTG_EP_TYPE_ISOC 1
  28591. +#define DWC_OTG_EP_TYPE_BULK 2
  28592. +#define DWC_OTG_EP_TYPE_INTR 3
  28593. +
  28594. + /** DATA start PID for INTR and BULK EP */
  28595. + unsigned data_pid_start:1;
  28596. + /** Frame (even/odd) for ISOC EP */
  28597. + unsigned even_odd_frame:1;
  28598. + /** Max Packet bytes */
  28599. + unsigned maxpacket:11;
  28600. +
  28601. + /** Max Transfer size */
  28602. + uint32_t maxxfer;
  28603. +
  28604. + /** @name Transfer state */
  28605. + /** @{ */
  28606. +
  28607. + /**
  28608. + * Pointer to the beginning of the transfer buffer -- do not modify
  28609. + * during transfer.
  28610. + */
  28611. +
  28612. + dwc_dma_t dma_addr;
  28613. +
  28614. + dwc_dma_t dma_desc_addr;
  28615. + dwc_otg_dev_dma_desc_t *desc_addr;
  28616. +
  28617. + uint8_t *start_xfer_buff;
  28618. + /** pointer to the transfer buffer */
  28619. + uint8_t *xfer_buff;
  28620. + /** Number of bytes to transfer */
  28621. + unsigned xfer_len:19;
  28622. + /** Number of bytes transferred. */
  28623. + unsigned xfer_count:19;
  28624. + /** Sent ZLP */
  28625. + unsigned sent_zlp:1;
  28626. + /** Total len for control transfer */
  28627. + unsigned total_len:19;
  28628. +
  28629. + /** stall clear flag */
  28630. + unsigned stall_clear_flag:1;
  28631. +
  28632. + /** SETUP pkt cnt rollover flag for EP0 out*/
  28633. + unsigned stp_rollover;
  28634. +
  28635. +#ifdef DWC_UTE_CFI
  28636. + /* The buffer mode */
  28637. + data_buffer_mode_e buff_mode;
  28638. +
  28639. + /* The chain of DMA descriptors.
  28640. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  28641. + */
  28642. + dwc_otg_dma_desc_t *descs;
  28643. +
  28644. + /* The DMA address of the descriptors chain start */
  28645. + dma_addr_t descs_dma_addr;
  28646. + /** This variable stores the length of the last enqueued request */
  28647. + uint32_t cfi_req_len;
  28648. +#endif //DWC_UTE_CFI
  28649. +
  28650. +/** Max DMA Descriptor count for any EP */
  28651. +#define MAX_DMA_DESC_CNT 256
  28652. + /** Allocated DMA Desc count */
  28653. + uint32_t desc_cnt;
  28654. +
  28655. + /** bInterval */
  28656. + uint32_t bInterval;
  28657. + /** Next frame num to setup next ISOC transfer */
  28658. + uint32_t frame_num;
  28659. + /** Indicates SOF number overrun in DSTS */
  28660. + uint8_t frm_overrun;
  28661. +
  28662. +#ifdef DWC_UTE_PER_IO
  28663. + /** Next frame num for which will be setup DMA Desc */
  28664. + uint32_t xiso_frame_num;
  28665. + /** bInterval */
  28666. + uint32_t xiso_bInterval;
  28667. + /** Count of currently active transfers - shall be either 0 or 1 */
  28668. + int xiso_active_xfers;
  28669. + int xiso_queued_xfers;
  28670. +#endif
  28671. +#ifdef DWC_EN_ISOC
  28672. + /**
  28673. + * Variables specific for ISOC EPs
  28674. + *
  28675. + */
  28676. + /** DMA addresses of ISOC buffers */
  28677. + dwc_dma_t dma_addr0;
  28678. + dwc_dma_t dma_addr1;
  28679. +
  28680. + dwc_dma_t iso_dma_desc_addr;
  28681. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  28682. +
  28683. + /** pointer to the transfer buffers */
  28684. + uint8_t *xfer_buff0;
  28685. + uint8_t *xfer_buff1;
  28686. +
  28687. + /** number of ISOC Buffer is processing */
  28688. + uint32_t proc_buf_num;
  28689. + /** Interval of ISOC Buffer processing */
  28690. + uint32_t buf_proc_intrvl;
  28691. + /** Data size for regular frame */
  28692. + uint32_t data_per_frame;
  28693. +
  28694. + /* todo - pattern data support is to be implemented in the future */
  28695. + /** Data size for pattern frame */
  28696. + uint32_t data_pattern_frame;
  28697. + /** Frame number of pattern data */
  28698. + uint32_t sync_frame;
  28699. +
  28700. + /** bInterval */
  28701. + uint32_t bInterval;
  28702. + /** ISO Packet number per frame */
  28703. + uint32_t pkt_per_frm;
  28704. + /** Next frame num for which will be setup DMA Desc */
  28705. + uint32_t next_frame;
  28706. + /** Number of packets per buffer processing */
  28707. + uint32_t pkt_cnt;
  28708. + /** Info for all isoc packets */
  28709. + iso_pkt_info_t *pkt_info;
  28710. + /** current pkt number */
  28711. + uint32_t cur_pkt;
  28712. + /** current pkt number */
  28713. + uint8_t *cur_pkt_addr;
  28714. + /** current pkt number */
  28715. + uint32_t cur_pkt_dma_addr;
  28716. +#endif /* DWC_EN_ISOC */
  28717. +
  28718. +/** @} */
  28719. +} dwc_ep_t;
  28720. +
  28721. +/*
  28722. + * Reasons for halting a host channel.
  28723. + */
  28724. +typedef enum dwc_otg_halt_status {
  28725. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  28726. + DWC_OTG_HC_XFER_COMPLETE,
  28727. + DWC_OTG_HC_XFER_URB_COMPLETE,
  28728. + DWC_OTG_HC_XFER_ACK,
  28729. + DWC_OTG_HC_XFER_NAK,
  28730. + DWC_OTG_HC_XFER_NYET,
  28731. + DWC_OTG_HC_XFER_STALL,
  28732. + DWC_OTG_HC_XFER_XACT_ERR,
  28733. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  28734. + DWC_OTG_HC_XFER_BABBLE_ERR,
  28735. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  28736. + DWC_OTG_HC_XFER_AHB_ERR,
  28737. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  28738. + DWC_OTG_HC_XFER_URB_DEQUEUE
  28739. +} dwc_otg_halt_status_e;
  28740. +
  28741. +/**
  28742. + * Host channel descriptor. This structure represents the state of a single
  28743. + * host channel when acting in host mode. It contains the data items needed to
  28744. + * transfer packets to an endpoint via a host channel.
  28745. + */
  28746. +typedef struct dwc_hc {
  28747. + /** Host channel number used for register address lookup */
  28748. + uint8_t hc_num;
  28749. +
  28750. + /** Device to access */
  28751. + unsigned dev_addr:7;
  28752. +
  28753. + /** EP to access */
  28754. + unsigned ep_num:4;
  28755. +
  28756. + /** EP direction. 0: OUT, 1: IN */
  28757. + unsigned ep_is_in:1;
  28758. +
  28759. + /**
  28760. + * EP speed.
  28761. + * One of the following values:
  28762. + * - DWC_OTG_EP_SPEED_LOW
  28763. + * - DWC_OTG_EP_SPEED_FULL
  28764. + * - DWC_OTG_EP_SPEED_HIGH
  28765. + */
  28766. + unsigned speed:2;
  28767. +#define DWC_OTG_EP_SPEED_LOW 0
  28768. +#define DWC_OTG_EP_SPEED_FULL 1
  28769. +#define DWC_OTG_EP_SPEED_HIGH 2
  28770. +
  28771. + /**
  28772. + * Endpoint type.
  28773. + * One of the following values:
  28774. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  28775. + * - DWC_OTG_EP_TYPE_ISOC: 1
  28776. + * - DWC_OTG_EP_TYPE_BULK: 2
  28777. + * - DWC_OTG_EP_TYPE_INTR: 3
  28778. + */
  28779. + unsigned ep_type:2;
  28780. +
  28781. + /** Max packet size in bytes */
  28782. + unsigned max_packet:11;
  28783. +
  28784. + /**
  28785. + * PID for initial transaction.
  28786. + * 0: DATA0,<br>
  28787. + * 1: DATA2,<br>
  28788. + * 2: DATA1,<br>
  28789. + * 3: MDATA (non-Control EP),
  28790. + * SETUP (Control EP)
  28791. + */
  28792. + unsigned data_pid_start:2;
  28793. +#define DWC_OTG_HC_PID_DATA0 0
  28794. +#define DWC_OTG_HC_PID_DATA2 1
  28795. +#define DWC_OTG_HC_PID_DATA1 2
  28796. +#define DWC_OTG_HC_PID_MDATA 3
  28797. +#define DWC_OTG_HC_PID_SETUP 3
  28798. +
  28799. + /** Number of periodic transactions per (micro)frame */
  28800. + unsigned multi_count:2;
  28801. +
  28802. + /** @name Transfer State */
  28803. + /** @{ */
  28804. +
  28805. + /** Pointer to the current transfer buffer position. */
  28806. + uint8_t *xfer_buff;
  28807. + /**
  28808. + * In Buffer DMA mode this buffer will be used
  28809. + * if xfer_buff is not DWORD aligned.
  28810. + */
  28811. + dwc_dma_t align_buff;
  28812. + /** Total number of bytes to transfer. */
  28813. + uint32_t xfer_len;
  28814. + /** Number of bytes transferred so far. */
  28815. + uint32_t xfer_count;
  28816. + /** Packet count at start of transfer.*/
  28817. + uint16_t start_pkt_count;
  28818. +
  28819. + /**
  28820. + * Flag to indicate whether the transfer has been started. Set to 1 if
  28821. + * it has been started, 0 otherwise.
  28822. + */
  28823. + uint8_t xfer_started;
  28824. +
  28825. + /**
  28826. + * Set to 1 to indicate that a PING request should be issued on this
  28827. + * channel. If 0, process normally.
  28828. + */
  28829. + uint8_t do_ping;
  28830. +
  28831. + /**
  28832. + * Set to 1 to indicate that the error count for this transaction is
  28833. + * non-zero. Set to 0 if the error count is 0.
  28834. + */
  28835. + uint8_t error_state;
  28836. +
  28837. + /**
  28838. + * Set to 1 to indicate that this channel should be halted the next
  28839. + * time a request is queued for the channel. This is necessary in
  28840. + * slave mode if no request queue space is available when an attempt
  28841. + * is made to halt the channel.
  28842. + */
  28843. + uint8_t halt_on_queue;
  28844. +
  28845. + /**
  28846. + * Set to 1 if the host channel has been halted, but the core is not
  28847. + * finished flushing queued requests. Otherwise 0.
  28848. + */
  28849. + uint8_t halt_pending;
  28850. +
  28851. + /**
  28852. + * Reason for halting the host channel.
  28853. + */
  28854. + dwc_otg_halt_status_e halt_status;
  28855. +
  28856. + /*
  28857. + * Split settings for the host channel
  28858. + */
  28859. + uint8_t do_split; /**< Enable split for the channel */
  28860. + uint8_t complete_split; /**< Enable complete split */
  28861. + uint8_t hub_addr; /**< Address of high speed hub */
  28862. +
  28863. + uint8_t port_addr; /**< Port of the low/full speed device */
  28864. + /** Split transaction position
  28865. + * One of the following values:
  28866. + * - DWC_HCSPLIT_XACTPOS_MID
  28867. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  28868. + * - DWC_HCSPLIT_XACTPOS_END
  28869. + * - DWC_HCSPLIT_XACTPOS_ALL */
  28870. + uint8_t xact_pos;
  28871. +
  28872. + /** Set when the host channel does a short read. */
  28873. + uint8_t short_read;
  28874. +
  28875. + /**
  28876. + * Number of requests issued for this channel since it was assigned to
  28877. + * the current transfer (not counting PINGs).
  28878. + */
  28879. + uint8_t requests;
  28880. +
  28881. + /**
  28882. + * Queue Head for the transfer being processed by this channel.
  28883. + */
  28884. + struct dwc_otg_qh *qh;
  28885. +
  28886. + /** @} */
  28887. +
  28888. + /** Entry in list of host channels. */
  28889. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  28890. +
  28891. + /** @name Descriptor DMA support */
  28892. + /** @{ */
  28893. +
  28894. + /** Number of Transfer Descriptors */
  28895. + uint16_t ntd;
  28896. +
  28897. + /** Descriptor List DMA address */
  28898. + dwc_dma_t desc_list_addr;
  28899. +
  28900. + /** Scheduling micro-frame bitmap. */
  28901. + uint8_t schinfo;
  28902. +
  28903. + /** @} */
  28904. +} dwc_hc_t;
  28905. +
  28906. +/**
  28907. + * The following parameters may be specified when starting the module. These
  28908. + * parameters define how the DWC_otg controller should be configured.
  28909. + */
  28910. +typedef struct dwc_otg_core_params {
  28911. + int32_t opt;
  28912. +
  28913. + /**
  28914. + * Specifies the OTG capabilities. The driver will automatically
  28915. + * detect the value for this parameter if none is specified.
  28916. + * 0 - HNP and SRP capable (default)
  28917. + * 1 - SRP Only capable
  28918. + * 2 - No HNP/SRP capable
  28919. + */
  28920. + int32_t otg_cap;
  28921. +
  28922. + /**
  28923. + * Specifies whether to use slave or DMA mode for accessing the data
  28924. + * FIFOs. The driver will automatically detect the value for this
  28925. + * parameter if none is specified.
  28926. + * 0 - Slave
  28927. + * 1 - DMA (default, if available)
  28928. + */
  28929. + int32_t dma_enable;
  28930. +
  28931. + /**
  28932. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  28933. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  28934. + * will automatically detect the value for this if none is specified.
  28935. + * 0 - address DMA
  28936. + * 1 - DMA Descriptor(default, if available)
  28937. + */
  28938. + int32_t dma_desc_enable;
  28939. + /** The DMA Burst size (applicable only for External DMA
  28940. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  28941. + */
  28942. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  28943. +
  28944. + /**
  28945. + * Specifies the maximum speed of operation in host and device mode.
  28946. + * The actual speed depends on the speed of the attached device and
  28947. + * the value of phy_type. The actual speed depends on the speed of the
  28948. + * attached device.
  28949. + * 0 - High Speed (default)
  28950. + * 1 - Full Speed
  28951. + */
  28952. + int32_t speed;
  28953. + /** Specifies whether low power mode is supported when attached
  28954. + * to a Full Speed or Low Speed device in host mode.
  28955. + * 0 - Don't support low power mode (default)
  28956. + * 1 - Support low power mode
  28957. + */
  28958. + int32_t host_support_fs_ls_low_power;
  28959. +
  28960. + /** Specifies the PHY clock rate in low power mode when connected to a
  28961. + * Low Speed device in host mode. This parameter is applicable only if
  28962. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  28963. + * then defaults to 6 MHZ otherwise 48 MHZ.
  28964. + *
  28965. + * 0 - 48 MHz
  28966. + * 1 - 6 MHz
  28967. + */
  28968. + int32_t host_ls_low_power_phy_clk;
  28969. +
  28970. + /**
  28971. + * 0 - Use cC FIFO size parameters
  28972. + * 1 - Allow dynamic FIFO sizing (default)
  28973. + */
  28974. + int32_t enable_dynamic_fifo;
  28975. +
  28976. + /** Total number of 4-byte words in the data FIFO memory. This
  28977. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  28978. + * Tx FIFOs.
  28979. + * 32 to 32768 (default 8192)
  28980. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  28981. + */
  28982. + int32_t data_fifo_size;
  28983. +
  28984. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  28985. + * FIFO sizing is enabled.
  28986. + * 16 to 32768 (default 1064)
  28987. + */
  28988. + int32_t dev_rx_fifo_size;
  28989. +
  28990. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  28991. + * when dynamic FIFO sizing is enabled.
  28992. + * 16 to 32768 (default 1024)
  28993. + */
  28994. + int32_t dev_nperio_tx_fifo_size;
  28995. +
  28996. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  28997. + * mode when dynamic FIFO sizing is enabled.
  28998. + * 4 to 768 (default 256)
  28999. + */
  29000. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  29001. +
  29002. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  29003. + * FIFO sizing is enabled.
  29004. + * 16 to 32768 (default 1024)
  29005. + */
  29006. + int32_t host_rx_fifo_size;
  29007. +
  29008. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  29009. + * when Dynamic FIFO sizing is enabled in the core.
  29010. + * 16 to 32768 (default 1024)
  29011. + */
  29012. + int32_t host_nperio_tx_fifo_size;
  29013. +
  29014. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  29015. + * FIFO sizing is enabled.
  29016. + * 16 to 32768 (default 1024)
  29017. + */
  29018. + int32_t host_perio_tx_fifo_size;
  29019. +
  29020. + /** The maximum transfer size supported in bytes.
  29021. + * 2047 to 65,535 (default 65,535)
  29022. + */
  29023. + int32_t max_transfer_size;
  29024. +
  29025. + /** The maximum number of packets in a transfer.
  29026. + * 15 to 511 (default 511)
  29027. + */
  29028. + int32_t max_packet_count;
  29029. +
  29030. + /** The number of host channel registers to use.
  29031. + * 1 to 16 (default 12)
  29032. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  29033. + */
  29034. + int32_t host_channels;
  29035. +
  29036. + /** The number of endpoints in addition to EP0 available for device
  29037. + * mode operations.
  29038. + * 1 to 15 (default 6 IN and OUT)
  29039. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  29040. + * endpoints in addition to EP0.
  29041. + */
  29042. + int32_t dev_endpoints;
  29043. +
  29044. + /**
  29045. + * Specifies the type of PHY interface to use. By default, the driver
  29046. + * will automatically detect the phy_type.
  29047. + *
  29048. + * 0 - Full Speed PHY
  29049. + * 1 - UTMI+ (default)
  29050. + * 2 - ULPI
  29051. + */
  29052. + int32_t phy_type;
  29053. +
  29054. + /**
  29055. + * Specifies the UTMI+ Data Width. This parameter is
  29056. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  29057. + * PHY_TYPE, this parameter indicates the data width between
  29058. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  29059. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  29060. + * to "8 and 16 bits", meaning that the core has been
  29061. + * configured to work at either data path width.
  29062. + *
  29063. + * 8 or 16 bits (default 16)
  29064. + */
  29065. + int32_t phy_utmi_width;
  29066. +
  29067. + /**
  29068. + * Specifies whether the ULPI operates at double or single
  29069. + * data rate. This parameter is only applicable if PHY_TYPE is
  29070. + * ULPI.
  29071. + *
  29072. + * 0 - single data rate ULPI interface with 8 bit wide data
  29073. + * bus (default)
  29074. + * 1 - double data rate ULPI interface with 4 bit wide data
  29075. + * bus
  29076. + */
  29077. + int32_t phy_ulpi_ddr;
  29078. +
  29079. + /**
  29080. + * Specifies whether to use the internal or external supply to
  29081. + * drive the vbus with a ULPI phy.
  29082. + */
  29083. + int32_t phy_ulpi_ext_vbus;
  29084. +
  29085. + /**
  29086. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  29087. + * parameter is only applicable if PHY_TYPE is FS.
  29088. + * 0 - No (default)
  29089. + * 1 - Yes
  29090. + */
  29091. + int32_t i2c_enable;
  29092. +
  29093. + int32_t ulpi_fs_ls;
  29094. +
  29095. + int32_t ts_dline;
  29096. +
  29097. + /**
  29098. + * Specifies whether dedicated transmit FIFOs are
  29099. + * enabled for non periodic IN endpoints in device mode
  29100. + * 0 - No
  29101. + * 1 - Yes
  29102. + */
  29103. + int32_t en_multiple_tx_fifo;
  29104. +
  29105. + /** Number of 4-byte words in each of the Tx FIFOs in device
  29106. + * mode when dynamic FIFO sizing is enabled.
  29107. + * 4 to 768 (default 256)
  29108. + */
  29109. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  29110. +
  29111. + /** Thresholding enable flag-
  29112. + * bit 0 - enable non-ISO Tx thresholding
  29113. + * bit 1 - enable ISO Tx thresholding
  29114. + * bit 2 - enable Rx thresholding
  29115. + */
  29116. + uint32_t thr_ctl;
  29117. +
  29118. + /** Thresholding length for Tx
  29119. + * FIFOs in 32 bit DWORDs
  29120. + */
  29121. + uint32_t tx_thr_length;
  29122. +
  29123. + /** Thresholding length for Rx
  29124. + * FIFOs in 32 bit DWORDs
  29125. + */
  29126. + uint32_t rx_thr_length;
  29127. +
  29128. + /**
  29129. + * Specifies whether LPM (Link Power Management) support is enabled
  29130. + */
  29131. + int32_t lpm_enable;
  29132. +
  29133. + /** Per Transfer Interrupt
  29134. + * mode enable flag
  29135. + * 1 - Enabled
  29136. + * 0 - Disabled
  29137. + */
  29138. + int32_t pti_enable;
  29139. +
  29140. + /** Multi Processor Interrupt
  29141. + * mode enable flag
  29142. + * 1 - Enabled
  29143. + * 0 - Disabled
  29144. + */
  29145. + int32_t mpi_enable;
  29146. +
  29147. + /** IS_USB Capability
  29148. + * 1 - Enabled
  29149. + * 0 - Disabled
  29150. + */
  29151. + int32_t ic_usb_cap;
  29152. +
  29153. + /** AHB Threshold Ratio
  29154. + * 2'b00 AHB Threshold = MAC Threshold
  29155. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  29156. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  29157. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  29158. + */
  29159. + int32_t ahb_thr_ratio;
  29160. +
  29161. + /** ADP Support
  29162. + * 1 - Enabled
  29163. + * 0 - Disabled
  29164. + */
  29165. + int32_t adp_supp_enable;
  29166. +
  29167. + /** HFIR Reload Control
  29168. + * 0 - The HFIR cannot be reloaded dynamically.
  29169. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  29170. + */
  29171. + int32_t reload_ctl;
  29172. +
  29173. + /** DCFG: Enable device Out NAK
  29174. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  29175. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  29176. + */
  29177. + int32_t dev_out_nak;
  29178. +
  29179. + /** DCFG: Enable Continue on BNA
  29180. + * After receiving BNA interrupt the core disables the endpoint,when the
  29181. + * endpoint is re-enabled by the application the core starts processing
  29182. + * 0 - from the DOEPDMA descriptor
  29183. + * 1 - from the descriptor which received the BNA.
  29184. + */
  29185. + int32_t cont_on_bna;
  29186. +
  29187. + /** GAHBCFG: AHB Single Support
  29188. + * This bit when programmed supports SINGLE transfers for remainder
  29189. + * data in a transfer for DMA mode of operation.
  29190. + * 0 - in this case the remainder data will be sent using INCR burst size.
  29191. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  29192. + */
  29193. + int32_t ahb_single;
  29194. +
  29195. + /** Core Power down mode
  29196. + * 0 - No Power Down is enabled
  29197. + * 1 - Reserved
  29198. + * 2 - Complete Power Down (Hibernation)
  29199. + */
  29200. + int32_t power_down;
  29201. +
  29202. + /** OTG revision supported
  29203. + * 0 - OTG 1.3 revision
  29204. + * 1 - OTG 2.0 revision
  29205. + */
  29206. + int32_t otg_ver;
  29207. +
  29208. +} dwc_otg_core_params_t;
  29209. +
  29210. +#ifdef DEBUG
  29211. +struct dwc_otg_core_if;
  29212. +typedef struct hc_xfer_info {
  29213. + struct dwc_otg_core_if *core_if;
  29214. + dwc_hc_t *hc;
  29215. +} hc_xfer_info_t;
  29216. +#endif
  29217. +
  29218. +typedef struct ep_xfer_info {
  29219. + struct dwc_otg_core_if *core_if;
  29220. + dwc_ep_t *ep;
  29221. + uint8_t state;
  29222. +} ep_xfer_info_t;
  29223. +/*
  29224. + * Device States
  29225. + */
  29226. +typedef enum dwc_otg_lx_state {
  29227. + /** On state */
  29228. + DWC_OTG_L0,
  29229. + /** LPM sleep state*/
  29230. + DWC_OTG_L1,
  29231. + /** USB suspend state*/
  29232. + DWC_OTG_L2,
  29233. + /** Off state*/
  29234. + DWC_OTG_L3
  29235. +} dwc_otg_lx_state_e;
  29236. +
  29237. +struct dwc_otg_global_regs_backup {
  29238. + uint32_t gotgctl_local;
  29239. + uint32_t gintmsk_local;
  29240. + uint32_t gahbcfg_local;
  29241. + uint32_t gusbcfg_local;
  29242. + uint32_t grxfsiz_local;
  29243. + uint32_t gnptxfsiz_local;
  29244. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29245. + uint32_t glpmcfg_local;
  29246. +#endif
  29247. + uint32_t gi2cctl_local;
  29248. + uint32_t hptxfsiz_local;
  29249. + uint32_t pcgcctl_local;
  29250. + uint32_t gdfifocfg_local;
  29251. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  29252. + uint32_t gpwrdn_local;
  29253. + uint32_t xhib_pcgcctl;
  29254. + uint32_t xhib_gpwrdn;
  29255. +};
  29256. +
  29257. +struct dwc_otg_host_regs_backup {
  29258. + uint32_t hcfg_local;
  29259. + uint32_t haintmsk_local;
  29260. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  29261. + uint32_t hprt0_local;
  29262. + uint32_t hfir_local;
  29263. +};
  29264. +
  29265. +struct dwc_otg_dev_regs_backup {
  29266. + uint32_t dcfg;
  29267. + uint32_t dctl;
  29268. + uint32_t daintmsk;
  29269. + uint32_t diepmsk;
  29270. + uint32_t doepmsk;
  29271. + uint32_t diepctl[MAX_EPS_CHANNELS];
  29272. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  29273. + uint32_t diepdma[MAX_EPS_CHANNELS];
  29274. +};
  29275. +/**
  29276. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  29277. + * the DWC_otg controller acting in either host or device mode. It
  29278. + * represents the programming view of the controller as a whole.
  29279. + */
  29280. +struct dwc_otg_core_if {
  29281. + /** Parameters that define how the core should be configured.*/
  29282. + dwc_otg_core_params_t *core_params;
  29283. +
  29284. + /** Core Global registers starting at offset 000h. */
  29285. + dwc_otg_core_global_regs_t *core_global_regs;
  29286. +
  29287. + /** Device-specific information */
  29288. + dwc_otg_dev_if_t *dev_if;
  29289. + /** Host-specific information */
  29290. + dwc_otg_host_if_t *host_if;
  29291. +
  29292. + /** Value from SNPSID register */
  29293. + uint32_t snpsid;
  29294. +
  29295. + /*
  29296. + * Set to 1 if the core PHY interface bits in USBCFG have been
  29297. + * initialized.
  29298. + */
  29299. + uint8_t phy_init_done;
  29300. +
  29301. + /*
  29302. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  29303. + */
  29304. + uint8_t srp_success;
  29305. + uint8_t srp_timer_started;
  29306. + /** Timer for SRP. If it expires before SRP is successful
  29307. + * clear the SRP. */
  29308. + dwc_timer_t *srp_timer;
  29309. +
  29310. +#ifdef DWC_DEV_SRPCAP
  29311. + /* This timer is needed to power on the hibernated host core if SRP is not
  29312. + * initiated on connected SRP capable device for limited period of time
  29313. + */
  29314. + uint8_t pwron_timer_started;
  29315. + dwc_timer_t *pwron_timer;
  29316. +#endif
  29317. + /* Common configuration information */
  29318. + /** Power and Clock Gating Control Register */
  29319. + volatile uint32_t *pcgcctl;
  29320. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  29321. +
  29322. + /** Push/pop addresses for endpoints or host channels.*/
  29323. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  29324. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  29325. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  29326. +
  29327. + /** Total RAM for FIFOs (Bytes) */
  29328. + uint16_t total_fifo_size;
  29329. + /** Size of Rx FIFO (Bytes) */
  29330. + uint16_t rx_fifo_size;
  29331. + /** Size of Non-periodic Tx FIFO (Bytes) */
  29332. + uint16_t nperio_tx_fifo_size;
  29333. +
  29334. + /** 1 if DMA is enabled, 0 otherwise. */
  29335. + uint8_t dma_enable;
  29336. +
  29337. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  29338. + uint8_t dma_desc_enable;
  29339. +
  29340. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  29341. + uint8_t pti_enh_enable;
  29342. +
  29343. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  29344. + uint8_t multiproc_int_enable;
  29345. +
  29346. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  29347. + uint8_t en_multiple_tx_fifo;
  29348. +
  29349. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  29350. + * process of being queued */
  29351. + uint8_t queuing_high_bandwidth;
  29352. +
  29353. + /** Hardware Configuration -- stored here for convenience.*/
  29354. + hwcfg1_data_t hwcfg1;
  29355. + hwcfg2_data_t hwcfg2;
  29356. + hwcfg3_data_t hwcfg3;
  29357. + hwcfg4_data_t hwcfg4;
  29358. + fifosize_data_t hptxfsiz;
  29359. +
  29360. + /** Host and Device Configuration -- stored here for convenience.*/
  29361. + hcfg_data_t hcfg;
  29362. + dcfg_data_t dcfg;
  29363. +
  29364. + /** The operational State, during transations
  29365. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  29366. + * match the core but allows the software to determine
  29367. + * transitions.
  29368. + */
  29369. + uint8_t op_state;
  29370. +
  29371. + /**
  29372. + * Set to 1 if the HCD needs to be restarted on a session request
  29373. + * interrupt. This is required if no connector ID status change has
  29374. + * occurred since the HCD was last disconnected.
  29375. + */
  29376. + uint8_t restart_hcd_on_session_req;
  29377. +
  29378. + /** HCD callbacks */
  29379. + /** A-Device is a_host */
  29380. +#define A_HOST (1)
  29381. + /** A-Device is a_suspend */
  29382. +#define A_SUSPEND (2)
  29383. + /** A-Device is a_peripherial */
  29384. +#define A_PERIPHERAL (3)
  29385. + /** B-Device is operating as a Peripheral. */
  29386. +#define B_PERIPHERAL (4)
  29387. + /** B-Device is operating as a Host. */
  29388. +#define B_HOST (5)
  29389. +
  29390. + /** HCD callbacks */
  29391. + struct dwc_otg_cil_callbacks *hcd_cb;
  29392. + /** PCD callbacks */
  29393. + struct dwc_otg_cil_callbacks *pcd_cb;
  29394. +
  29395. + /** Device mode Periodic Tx FIFO Mask */
  29396. + uint32_t p_tx_msk;
  29397. + /** Device mode Periodic Tx FIFO Mask */
  29398. + uint32_t tx_msk;
  29399. +
  29400. + /** Workqueue object used for handling several interrupts */
  29401. + dwc_workq_t *wq_otg;
  29402. +
  29403. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  29404. + dwc_timer_t *wkp_timer;
  29405. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  29406. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  29407. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  29408. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  29409. +#ifdef DEBUG
  29410. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  29411. +
  29412. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  29413. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  29414. +
  29415. + uint32_t hfnum_7_samples;
  29416. + uint64_t hfnum_7_frrem_accum;
  29417. + uint32_t hfnum_0_samples;
  29418. + uint64_t hfnum_0_frrem_accum;
  29419. + uint32_t hfnum_other_samples;
  29420. + uint64_t hfnum_other_frrem_accum;
  29421. +#endif
  29422. +
  29423. +#ifdef DWC_UTE_CFI
  29424. + uint16_t pwron_rxfsiz;
  29425. + uint16_t pwron_gnptxfsiz;
  29426. + uint16_t pwron_txfsiz[15];
  29427. +
  29428. + uint16_t init_rxfsiz;
  29429. + uint16_t init_gnptxfsiz;
  29430. + uint16_t init_txfsiz[15];
  29431. +#endif
  29432. +
  29433. + /** Lx state of device */
  29434. + dwc_otg_lx_state_e lx_state;
  29435. +
  29436. + /** Saved Core Global registers */
  29437. + struct dwc_otg_global_regs_backup *gr_backup;
  29438. + /** Saved Host registers */
  29439. + struct dwc_otg_host_regs_backup *hr_backup;
  29440. + /** Saved Device registers */
  29441. + struct dwc_otg_dev_regs_backup *dr_backup;
  29442. +
  29443. + /** Power Down Enable */
  29444. + uint32_t power_down;
  29445. +
  29446. + /** ADP support Enable */
  29447. + uint32_t adp_enable;
  29448. +
  29449. + /** ADP structure object */
  29450. + dwc_otg_adp_t adp;
  29451. +
  29452. + /** hibernation/suspend flag */
  29453. + int hibernation_suspend;
  29454. +
  29455. + /** Device mode extended hibernation flag */
  29456. + int xhib;
  29457. +
  29458. + /** OTG revision supported */
  29459. + uint32_t otg_ver;
  29460. +
  29461. + /** OTG status flag used for HNP polling */
  29462. + uint8_t otg_sts;
  29463. +
  29464. + /** Pointer to either hcd->lock or pcd->lock */
  29465. + dwc_spinlock_t *lock;
  29466. +
  29467. + /** Start predict NextEP based on Learning Queue if equal 1,
  29468. + * also used as counter of disabled NP IN EP's */
  29469. + uint8_t start_predict;
  29470. +
  29471. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  29472. + * active, 0xff otherwise */
  29473. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  29474. +
  29475. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  29476. + uint8_t first_in_nextep_seq;
  29477. +
  29478. + /** Frame number while entering to ISR - needed for ISOCs **/
  29479. + uint32_t frame_num;
  29480. +
  29481. +};
  29482. +
  29483. +#ifdef DEBUG
  29484. +/*
  29485. + * This function is called when transfer is timed out.
  29486. + */
  29487. +extern void hc_xfer_timeout(void *ptr);
  29488. +#endif
  29489. +
  29490. +/*
  29491. + * This function is called when transfer is timed out on endpoint.
  29492. + */
  29493. +extern void ep_xfer_timeout(void *ptr);
  29494. +
  29495. +/*
  29496. + * The following functions are functions for works
  29497. + * using during handling some interrupts
  29498. + */
  29499. +extern void w_conn_id_status_change(void *p);
  29500. +
  29501. +extern void w_wakeup_detected(void *p);
  29502. +
  29503. +/** Saves global register values into system memory. */
  29504. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  29505. +/** Saves device register values into system memory. */
  29506. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  29507. +/** Saves host register values into system memory. */
  29508. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  29509. +/** Restore global register values. */
  29510. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  29511. +/** Restore host register values. */
  29512. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  29513. +/** Restore device register values. */
  29514. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  29515. + int rem_wakeup);
  29516. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  29517. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  29518. + int is_host);
  29519. +
  29520. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  29521. + int restore_mode, int reset);
  29522. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  29523. + int rem_wakeup, int reset);
  29524. +
  29525. +/*
  29526. + * The following functions support initialization of the CIL driver component
  29527. + * and the DWC_otg controller.
  29528. + */
  29529. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  29530. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  29531. +
  29532. +/** @name Device CIL Functions
  29533. + * The following functions support managing the DWC_otg controller in device
  29534. + * mode.
  29535. + */
  29536. +/**@{*/
  29537. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  29538. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  29539. + uint32_t * _dest);
  29540. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  29541. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29542. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29543. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29544. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  29545. + dwc_ep_t * _ep);
  29546. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  29547. + dwc_ep_t * _ep);
  29548. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  29549. + dwc_ep_t * _ep);
  29550. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  29551. + dwc_ep_t * _ep);
  29552. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  29553. + dwc_ep_t * _ep, int _dma);
  29554. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29555. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  29556. + dwc_ep_t * _ep);
  29557. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  29558. +
  29559. +#ifdef DWC_EN_ISOC
  29560. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  29561. + dwc_ep_t * ep);
  29562. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  29563. + dwc_ep_t * ep);
  29564. +#endif /* DWC_EN_ISOC */
  29565. +/**@}*/
  29566. +
  29567. +/** @name Host CIL Functions
  29568. + * The following functions support managing the DWC_otg controller in host
  29569. + * mode.
  29570. + */
  29571. +/**@{*/
  29572. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29573. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  29574. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  29575. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29576. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  29577. + dwc_hc_t * _hc);
  29578. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  29579. + dwc_hc_t * _hc);
  29580. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29581. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  29582. + dwc_hc_t * _hc);
  29583. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  29584. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  29585. +
  29586. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  29587. + dwc_hc_t * hc);
  29588. +
  29589. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  29590. +
  29591. +/* Macro used to clear one channel interrupt */
  29592. +#define clear_hc_int(_hc_regs_, _intr_) \
  29593. +do { \
  29594. + hcint_data_t hcint_clear = {.d32 = 0}; \
  29595. + hcint_clear.b._intr_ = 1; \
  29596. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  29597. +} while (0)
  29598. +
  29599. +/*
  29600. + * Macro used to disable one channel interrupt. Channel interrupts are
  29601. + * disabled when the channel is halted or released by the interrupt handler.
  29602. + * There is no need to handle further interrupts of that type until the
  29603. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  29604. + * because the channel structures are cleaned up when the channel is released.
  29605. + */
  29606. +#define disable_hc_int(_hc_regs_, _intr_) \
  29607. +do { \
  29608. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  29609. + hcintmsk.b._intr_ = 1; \
  29610. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  29611. +} while (0)
  29612. +
  29613. +/**
  29614. + * This function Reads HPRT0 in preparation to modify. It keeps the
  29615. + * WC bits 0 so that if they are read as 1, they won't clear when you
  29616. + * write it back
  29617. + */
  29618. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  29619. +{
  29620. + hprt0_data_t hprt0;
  29621. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  29622. + hprt0.b.prtena = 0;
  29623. + hprt0.b.prtconndet = 0;
  29624. + hprt0.b.prtenchng = 0;
  29625. + hprt0.b.prtovrcurrchng = 0;
  29626. + return hprt0.d32;
  29627. +}
  29628. +
  29629. +/**@}*/
  29630. +
  29631. +/** @name Common CIL Functions
  29632. + * The following functions support managing the DWC_otg controller in either
  29633. + * device or host mode.
  29634. + */
  29635. +/**@{*/
  29636. +
  29637. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  29638. + uint8_t * dest, uint16_t bytes);
  29639. +
  29640. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  29641. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  29642. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  29643. +
  29644. +/**
  29645. + * This function returns the Core Interrupt register.
  29646. + */
  29647. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  29648. +{
  29649. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  29650. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  29651. +}
  29652. +
  29653. +/**
  29654. + * This function returns the OTG Interrupt register.
  29655. + */
  29656. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  29657. +{
  29658. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  29659. +}
  29660. +
  29661. +/**
  29662. + * This function reads the Device All Endpoints Interrupt register and
  29663. + * returns the IN endpoint interrupt bits.
  29664. + */
  29665. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  29666. + core_if)
  29667. +{
  29668. +
  29669. + uint32_t v;
  29670. +
  29671. + if (core_if->multiproc_int_enable) {
  29672. + v = DWC_READ_REG32(&core_if->dev_if->
  29673. + dev_global_regs->deachint) &
  29674. + DWC_READ_REG32(&core_if->
  29675. + dev_if->dev_global_regs->deachintmsk);
  29676. + } else {
  29677. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  29678. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  29679. + }
  29680. + return (v & 0xffff);
  29681. +}
  29682. +
  29683. +/**
  29684. + * This function reads the Device All Endpoints Interrupt register and
  29685. + * returns the OUT endpoint interrupt bits.
  29686. + */
  29687. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  29688. + core_if)
  29689. +{
  29690. + uint32_t v;
  29691. +
  29692. + if (core_if->multiproc_int_enable) {
  29693. + v = DWC_READ_REG32(&core_if->dev_if->
  29694. + dev_global_regs->deachint) &
  29695. + DWC_READ_REG32(&core_if->
  29696. + dev_if->dev_global_regs->deachintmsk);
  29697. + } else {
  29698. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  29699. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  29700. + }
  29701. +
  29702. + return ((v & 0xffff0000) >> 16);
  29703. +}
  29704. +
  29705. +/**
  29706. + * This function returns the Device IN EP Interrupt register
  29707. + */
  29708. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  29709. + dwc_ep_t * ep)
  29710. +{
  29711. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  29712. + uint32_t v, msk, emp;
  29713. +
  29714. + if (core_if->multiproc_int_enable) {
  29715. + msk =
  29716. + DWC_READ_REG32(&dev_if->
  29717. + dev_global_regs->diepeachintmsk[ep->num]);
  29718. + emp =
  29719. + DWC_READ_REG32(&dev_if->
  29720. + dev_global_regs->dtknqr4_fifoemptymsk);
  29721. + msk |= ((emp >> ep->num) & 0x1) << 7;
  29722. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  29723. + } else {
  29724. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  29725. + emp =
  29726. + DWC_READ_REG32(&dev_if->
  29727. + dev_global_regs->dtknqr4_fifoemptymsk);
  29728. + msk |= ((emp >> ep->num) & 0x1) << 7;
  29729. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  29730. + }
  29731. +
  29732. + return v;
  29733. +}
  29734. +
  29735. +/**
  29736. + * This function returns the Device OUT EP Interrupt register
  29737. + */
  29738. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  29739. + _core_if, dwc_ep_t * _ep)
  29740. +{
  29741. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  29742. + uint32_t v;
  29743. + doepmsk_data_t msk = {.d32 = 0 };
  29744. +
  29745. + if (_core_if->multiproc_int_enable) {
  29746. + msk.d32 =
  29747. + DWC_READ_REG32(&dev_if->
  29748. + dev_global_regs->doepeachintmsk[_ep->num]);
  29749. + if (_core_if->pti_enh_enable) {
  29750. + msk.b.pktdrpsts = 1;
  29751. + }
  29752. + v = DWC_READ_REG32(&dev_if->
  29753. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  29754. + } else {
  29755. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  29756. + if (_core_if->pti_enh_enable) {
  29757. + msk.b.pktdrpsts = 1;
  29758. + }
  29759. + v = DWC_READ_REG32(&dev_if->
  29760. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  29761. + }
  29762. + return v;
  29763. +}
  29764. +
  29765. +/**
  29766. + * This function returns the Host All Channel Interrupt register
  29767. + */
  29768. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  29769. + _core_if)
  29770. +{
  29771. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  29772. +}
  29773. +
  29774. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  29775. + _core_if, dwc_hc_t * _hc)
  29776. +{
  29777. + return (DWC_READ_REG32
  29778. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  29779. +}
  29780. +
  29781. +/**
  29782. + * This function returns the mode of the operation, host or device.
  29783. + *
  29784. + * @return 0 - Device Mode, 1 - Host Mode
  29785. + */
  29786. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  29787. +{
  29788. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  29789. +}
  29790. +
  29791. +/**@}*/
  29792. +
  29793. +/**
  29794. + * DWC_otg CIL callback structure. This structure allows the HCD and
  29795. + * PCD to register functions used for starting and stopping the PCD
  29796. + * and HCD for role change on for a DRD.
  29797. + */
  29798. +typedef struct dwc_otg_cil_callbacks {
  29799. + /** Start function for role change */
  29800. + int (*start) (void *_p);
  29801. + /** Stop Function for role change */
  29802. + int (*stop) (void *_p);
  29803. + /** Disconnect Function for role change */
  29804. + int (*disconnect) (void *_p);
  29805. + /** Resume/Remote wakeup Function */
  29806. + int (*resume_wakeup) (void *_p);
  29807. + /** Suspend function */
  29808. + int (*suspend) (void *_p);
  29809. + /** Session Start (SRP) */
  29810. + int (*session_start) (void *_p);
  29811. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29812. + /** Sleep (switch to L0 state) */
  29813. + int (*sleep) (void *_p);
  29814. +#endif
  29815. + /** Pointer passed to start() and stop() */
  29816. + void *p;
  29817. +} dwc_otg_cil_callbacks_t;
  29818. +
  29819. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  29820. + dwc_otg_cil_callbacks_t * _cb,
  29821. + void *_p);
  29822. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  29823. + dwc_otg_cil_callbacks_t * _cb,
  29824. + void *_p);
  29825. +
  29826. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  29827. +
  29828. +//////////////////////////////////////////////////////////////////////
  29829. +/** Start the HCD. Helper function for using the HCD callbacks.
  29830. + *
  29831. + * @param core_if Programming view of DWC_otg controller.
  29832. + */
  29833. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  29834. +{
  29835. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  29836. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  29837. + }
  29838. +}
  29839. +
  29840. +/** Stop the HCD. Helper function for using the HCD callbacks.
  29841. + *
  29842. + * @param core_if Programming view of DWC_otg controller.
  29843. + */
  29844. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  29845. +{
  29846. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  29847. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  29848. + }
  29849. +}
  29850. +
  29851. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  29852. + *
  29853. + * @param core_if Programming view of DWC_otg controller.
  29854. + */
  29855. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  29856. +{
  29857. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  29858. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  29859. + }
  29860. +}
  29861. +
  29862. +/** Inform the HCD the a New Session has begun. Helper function for
  29863. + * using the HCD callbacks.
  29864. + *
  29865. + * @param core_if Programming view of DWC_otg controller.
  29866. + */
  29867. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  29868. +{
  29869. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  29870. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  29871. + }
  29872. +}
  29873. +
  29874. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29875. +/**
  29876. + * Inform the HCD about LPM sleep.
  29877. + * Helper function for using the HCD callbacks.
  29878. + *
  29879. + * @param core_if Programming view of DWC_otg controller.
  29880. + */
  29881. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  29882. +{
  29883. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  29884. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  29885. + }
  29886. +}
  29887. +#endif
  29888. +
  29889. +/** Resume the HCD. Helper function for using the HCD callbacks.
  29890. + *
  29891. + * @param core_if Programming view of DWC_otg controller.
  29892. + */
  29893. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  29894. +{
  29895. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  29896. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  29897. + }
  29898. +}
  29899. +
  29900. +/** Start the PCD. Helper function for using the PCD callbacks.
  29901. + *
  29902. + * @param core_if Programming view of DWC_otg controller.
  29903. + */
  29904. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  29905. +{
  29906. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  29907. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  29908. + }
  29909. +}
  29910. +
  29911. +/** Stop the PCD. Helper function for using the PCD callbacks.
  29912. + *
  29913. + * @param core_if Programming view of DWC_otg controller.
  29914. + */
  29915. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  29916. +{
  29917. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  29918. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  29919. + }
  29920. +}
  29921. +
  29922. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  29923. + *
  29924. + * @param core_if Programming view of DWC_otg controller.
  29925. + */
  29926. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  29927. +{
  29928. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  29929. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  29930. + }
  29931. +}
  29932. +
  29933. +/** Resume the PCD. Helper function for using the PCD callbacks.
  29934. + *
  29935. + * @param core_if Programming view of DWC_otg controller.
  29936. + */
  29937. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  29938. +{
  29939. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  29940. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  29941. + }
  29942. +}
  29943. +
  29944. +//////////////////////////////////////////////////////////////////////
  29945. +
  29946. +#endif
  29947. --- /dev/null
  29948. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  29949. @@ -0,0 +1,1594 @@
  29950. +/* ==========================================================================
  29951. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  29952. + * $Revision: #32 $
  29953. + * $Date: 2012/08/10 $
  29954. + * $Change: 2047372 $
  29955. + *
  29956. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  29957. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  29958. + * otherwise expressly agreed to in writing between Synopsys and you.
  29959. + *
  29960. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  29961. + * any End User Software License Agreement or Agreement for Licensed Product
  29962. + * with Synopsys or any supplement thereto. You are permitted to use and
  29963. + * redistribute this Software in source and binary forms, with or without
  29964. + * modification, provided that redistributions of source code must retain this
  29965. + * notice. You may not view, use, disclose, copy or distribute this file or
  29966. + * any information contained herein except pursuant to this license grant from
  29967. + * Synopsys. If you do not agree with this notice, including the disclaimer
  29968. + * below, then you are not authorized to use the Software.
  29969. + *
  29970. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  29971. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29972. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29973. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  29974. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29975. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29976. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29977. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29978. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29979. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  29980. + * DAMAGE.
  29981. + * ========================================================================== */
  29982. +
  29983. +/** @file
  29984. + *
  29985. + * The Core Interface Layer provides basic services for accessing and
  29986. + * managing the DWC_otg hardware. These services are used by both the
  29987. + * Host Controller Driver and the Peripheral Controller Driver.
  29988. + *
  29989. + * This file contains the Common Interrupt handlers.
  29990. + */
  29991. +#include "dwc_os.h"
  29992. +#include "dwc_otg_regs.h"
  29993. +#include "dwc_otg_cil.h"
  29994. +#include "dwc_otg_driver.h"
  29995. +#include "dwc_otg_pcd.h"
  29996. +#include "dwc_otg_hcd.h"
  29997. +
  29998. +#ifdef DEBUG
  29999. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  30000. +{
  30001. + return (core_if->op_state == A_HOST ? "a_host" :
  30002. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  30003. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  30004. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  30005. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  30006. +}
  30007. +#endif
  30008. +
  30009. +/** This function will log a debug message
  30010. + *
  30011. + * @param core_if Programming view of DWC_otg controller.
  30012. + */
  30013. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  30014. +{
  30015. + gintsts_data_t gintsts;
  30016. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  30017. + dwc_otg_mode(core_if) ? "Host" : "Device");
  30018. +
  30019. + /* Clear interrupt */
  30020. + gintsts.d32 = 0;
  30021. + gintsts.b.modemismatch = 1;
  30022. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30023. + return 1;
  30024. +}
  30025. +
  30026. +/**
  30027. + * This function handles the OTG Interrupts. It reads the OTG
  30028. + * Interrupt Register (GOTGINT) to determine what interrupt has
  30029. + * occurred.
  30030. + *
  30031. + * @param core_if Programming view of DWC_otg controller.
  30032. + */
  30033. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  30034. +{
  30035. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  30036. + gotgint_data_t gotgint;
  30037. + gotgctl_data_t gotgctl;
  30038. + gintmsk_data_t gintmsk;
  30039. + gpwrdn_data_t gpwrdn;
  30040. +
  30041. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  30042. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30043. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  30044. + op_state_str(core_if));
  30045. +
  30046. + if (gotgint.b.sesenddet) {
  30047. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30048. + "Session End Detected++ (%s)\n",
  30049. + op_state_str(core_if));
  30050. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30051. +
  30052. + if (core_if->op_state == B_HOST) {
  30053. + cil_pcd_start(core_if);
  30054. + core_if->op_state = B_PERIPHERAL;
  30055. + } else {
  30056. + /* If not B_HOST and Device HNP still set. HNP
  30057. + * Did not succeed!*/
  30058. + if (gotgctl.b.devhnpen) {
  30059. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  30060. + __DWC_ERROR("Device Not Connected/Responding!\n");
  30061. + }
  30062. +
  30063. + /* If Session End Detected the B-Cable has
  30064. + * been disconnected. */
  30065. + /* Reset PCD and Gadget driver to a
  30066. + * clean state. */
  30067. + core_if->lx_state = DWC_OTG_L0;
  30068. + DWC_SPINUNLOCK(core_if->lock);
  30069. + cil_pcd_stop(core_if);
  30070. + DWC_SPINLOCK(core_if->lock);
  30071. +
  30072. + if (core_if->adp_enable) {
  30073. + if (core_if->power_down == 2) {
  30074. + gpwrdn.d32 = 0;
  30075. + gpwrdn.b.pwrdnswtch = 1;
  30076. + DWC_MODIFY_REG32(&core_if->
  30077. + core_global_regs->
  30078. + gpwrdn, gpwrdn.d32, 0);
  30079. + }
  30080. +
  30081. + gpwrdn.d32 = 0;
  30082. + gpwrdn.b.pmuintsel = 1;
  30083. + gpwrdn.b.pmuactv = 1;
  30084. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30085. + gpwrdn, 0, gpwrdn.d32);
  30086. +
  30087. + dwc_otg_adp_sense_start(core_if);
  30088. + }
  30089. + }
  30090. +
  30091. + gotgctl.d32 = 0;
  30092. + gotgctl.b.devhnpen = 1;
  30093. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  30094. + }
  30095. + if (gotgint.b.sesreqsucstschng) {
  30096. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30097. + "Session Reqeust Success Status Change++\n");
  30098. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30099. + if (gotgctl.b.sesreqscs) {
  30100. +
  30101. + if ((core_if->core_params->phy_type ==
  30102. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  30103. + core_if->srp_success = 1;
  30104. + } else {
  30105. + DWC_SPINUNLOCK(core_if->lock);
  30106. + cil_pcd_resume(core_if);
  30107. + DWC_SPINLOCK(core_if->lock);
  30108. + /* Clear Session Request */
  30109. + gotgctl.d32 = 0;
  30110. + gotgctl.b.sesreq = 1;
  30111. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  30112. + gotgctl.d32, 0);
  30113. + }
  30114. + }
  30115. + }
  30116. + if (gotgint.b.hstnegsucstschng) {
  30117. + /* Print statements during the HNP interrupt handling
  30118. + * can cause it to fail.*/
  30119. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30120. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  30121. + * this does not help*/
  30122. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  30123. + dwc_udelay(100);
  30124. + if (gotgctl.b.hstnegscs) {
  30125. + if (dwc_otg_is_host_mode(core_if)) {
  30126. + core_if->op_state = B_HOST;
  30127. + /*
  30128. + * Need to disable SOF interrupt immediately.
  30129. + * When switching from device to host, the PCD
  30130. + * interrupt handler won't handle the
  30131. + * interrupt if host mode is already set. The
  30132. + * HCD interrupt handler won't get called if
  30133. + * the HCD state is HALT. This means that the
  30134. + * interrupt does not get handled and Linux
  30135. + * complains loudly.
  30136. + */
  30137. + gintmsk.d32 = 0;
  30138. + gintmsk.b.sofintr = 1;
  30139. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  30140. + gintmsk.d32, 0);
  30141. + /* Call callback function with spin lock released */
  30142. + DWC_SPINUNLOCK(core_if->lock);
  30143. + cil_pcd_stop(core_if);
  30144. + /*
  30145. + * Initialize the Core for Host mode.
  30146. + */
  30147. + cil_hcd_start(core_if);
  30148. + DWC_SPINLOCK(core_if->lock);
  30149. + core_if->op_state = B_HOST;
  30150. + }
  30151. + } else {
  30152. + gotgctl.d32 = 0;
  30153. + gotgctl.b.hnpreq = 1;
  30154. + gotgctl.b.devhnpen = 1;
  30155. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  30156. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  30157. + __DWC_ERROR("Device Not Connected/Responding\n");
  30158. + }
  30159. + }
  30160. + if (gotgint.b.hstnegdet) {
  30161. + /* The disconnect interrupt is set at the same time as
  30162. + * Host Negotiation Detected. During the mode
  30163. + * switch all interrupts are cleared so the disconnect
  30164. + * interrupt handler will not get executed.
  30165. + */
  30166. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30167. + "Host Negotiation Detected++ (%s)\n",
  30168. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30169. + "Device"));
  30170. + if (dwc_otg_is_device_mode(core_if)) {
  30171. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  30172. + core_if->op_state);
  30173. + DWC_SPINUNLOCK(core_if->lock);
  30174. + cil_hcd_disconnect(core_if);
  30175. + cil_pcd_start(core_if);
  30176. + DWC_SPINLOCK(core_if->lock);
  30177. + core_if->op_state = A_PERIPHERAL;
  30178. + } else {
  30179. + /*
  30180. + * Need to disable SOF interrupt immediately. When
  30181. + * switching from device to host, the PCD interrupt
  30182. + * handler won't handle the interrupt if host mode is
  30183. + * already set. The HCD interrupt handler won't get
  30184. + * called if the HCD state is HALT. This means that
  30185. + * the interrupt does not get handled and Linux
  30186. + * complains loudly.
  30187. + */
  30188. + gintmsk.d32 = 0;
  30189. + gintmsk.b.sofintr = 1;
  30190. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  30191. + DWC_SPINUNLOCK(core_if->lock);
  30192. + cil_pcd_stop(core_if);
  30193. + cil_hcd_start(core_if);
  30194. + DWC_SPINLOCK(core_if->lock);
  30195. + core_if->op_state = A_HOST;
  30196. + }
  30197. + }
  30198. + if (gotgint.b.adevtoutchng) {
  30199. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30200. + "A-Device Timeout Change++\n");
  30201. + }
  30202. + if (gotgint.b.debdone) {
  30203. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  30204. + }
  30205. +
  30206. + /* Clear GOTGINT */
  30207. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  30208. +
  30209. + return 1;
  30210. +}
  30211. +
  30212. +void w_conn_id_status_change(void *p)
  30213. +{
  30214. + dwc_otg_core_if_t *core_if = p;
  30215. + uint32_t count = 0;
  30216. + gotgctl_data_t gotgctl = {.d32 = 0 };
  30217. +
  30218. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  30219. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  30220. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  30221. +
  30222. + /* B-Device connector (Device Mode) */
  30223. + if (gotgctl.b.conidsts) {
  30224. + /* Wait for switch to device mode. */
  30225. + while (!dwc_otg_is_device_mode(core_if)) {
  30226. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  30227. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30228. + "Peripheral"));
  30229. + dwc_mdelay(100);
  30230. + if (++count > 10000)
  30231. + break;
  30232. + }
  30233. + DWC_ASSERT(++count < 10000,
  30234. + "Connection id status change timed out");
  30235. + core_if->op_state = B_PERIPHERAL;
  30236. + dwc_otg_core_init(core_if);
  30237. + dwc_otg_enable_global_interrupts(core_if);
  30238. + cil_pcd_start(core_if);
  30239. + } else {
  30240. + /* A-Device connector (Host Mode) */
  30241. + while (!dwc_otg_is_host_mode(core_if)) {
  30242. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  30243. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30244. + "Peripheral"));
  30245. + dwc_mdelay(100);
  30246. + if (++count > 10000)
  30247. + break;
  30248. + }
  30249. + DWC_ASSERT(++count < 10000,
  30250. + "Connection id status change timed out");
  30251. + core_if->op_state = A_HOST;
  30252. + /*
  30253. + * Initialize the Core for Host mode.
  30254. + */
  30255. + dwc_otg_core_init(core_if);
  30256. + dwc_otg_enable_global_interrupts(core_if);
  30257. + cil_hcd_start(core_if);
  30258. + }
  30259. +}
  30260. +
  30261. +/**
  30262. + * This function handles the Connector ID Status Change Interrupt. It
  30263. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  30264. + * is a Device to Host Mode transition or a Host Mode to Device
  30265. + * Transition.
  30266. + *
  30267. + * This only occurs when the cable is connected/removed from the PHY
  30268. + * connector.
  30269. + *
  30270. + * @param core_if Programming view of DWC_otg controller.
  30271. + */
  30272. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  30273. +{
  30274. +
  30275. + /*
  30276. + * Need to disable SOF interrupt immediately. If switching from device
  30277. + * to host, the PCD interrupt handler won't handle the interrupt if
  30278. + * host mode is already set. The HCD interrupt handler won't get
  30279. + * called if the HCD state is HALT. This means that the interrupt does
  30280. + * not get handled and Linux complains loudly.
  30281. + */
  30282. + gintmsk_data_t gintmsk = {.d32 = 0 };
  30283. + gintsts_data_t gintsts = {.d32 = 0 };
  30284. +
  30285. + gintmsk.b.sofintr = 1;
  30286. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  30287. +
  30288. + DWC_DEBUGPL(DBG_CIL,
  30289. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  30290. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  30291. +
  30292. + DWC_SPINUNLOCK(core_if->lock);
  30293. +
  30294. + /*
  30295. + * Need to schedule a work, as there are possible DELAY function calls
  30296. + * Release lock before scheduling workq as it holds spinlock during scheduling
  30297. + */
  30298. +
  30299. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  30300. + core_if, "connection id status change");
  30301. + DWC_SPINLOCK(core_if->lock);
  30302. +
  30303. + /* Set flag and clear interrupt */
  30304. + gintsts.b.conidstschng = 1;
  30305. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30306. +
  30307. + return 1;
  30308. +}
  30309. +
  30310. +/**
  30311. + * This interrupt indicates that a device is initiating the Session
  30312. + * Request Protocol to request the host to turn on bus power so a new
  30313. + * session can begin. The handler responds by turning on bus power. If
  30314. + * the DWC_otg controller is in low power mode, the handler brings the
  30315. + * controller out of low power mode before turning on bus power.
  30316. + *
  30317. + * @param core_if Programming view of DWC_otg controller.
  30318. + */
  30319. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  30320. +{
  30321. + gintsts_data_t gintsts;
  30322. +
  30323. +#ifndef DWC_HOST_ONLY
  30324. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  30325. +
  30326. + if (dwc_otg_is_device_mode(core_if)) {
  30327. + DWC_PRINTF("SRP: Device mode\n");
  30328. + } else {
  30329. + hprt0_data_t hprt0;
  30330. + DWC_PRINTF("SRP: Host mode\n");
  30331. +
  30332. + /* Turn on the port power bit. */
  30333. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  30334. + hprt0.b.prtpwr = 1;
  30335. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  30336. +
  30337. + /* Start the Connection timer. So a message can be displayed
  30338. + * if connect does not occur within 10 seconds. */
  30339. + cil_hcd_session_start(core_if);
  30340. + }
  30341. +#endif
  30342. +
  30343. + /* Clear interrupt */
  30344. + gintsts.d32 = 0;
  30345. + gintsts.b.sessreqintr = 1;
  30346. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30347. +
  30348. + return 1;
  30349. +}
  30350. +
  30351. +void w_wakeup_detected(void *p)
  30352. +{
  30353. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  30354. + /*
  30355. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  30356. + * so that OPT tests pass with all PHYs).
  30357. + */
  30358. + hprt0_data_t hprt0 = {.d32 = 0 };
  30359. +#if 0
  30360. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30361. + /* Restart the Phy Clock */
  30362. + pcgcctl.b.stoppclk = 1;
  30363. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  30364. + dwc_udelay(10);
  30365. +#endif //0
  30366. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  30367. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  30368. +// dwc_mdelay(70);
  30369. + hprt0.b.prtres = 0; /* Resume */
  30370. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  30371. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  30372. + DWC_READ_REG32(core_if->host_if->hprt0));
  30373. +
  30374. + cil_hcd_resume(core_if);
  30375. +
  30376. + /** Change to L0 state*/
  30377. + core_if->lx_state = DWC_OTG_L0;
  30378. +}
  30379. +
  30380. +/**
  30381. + * This interrupt indicates that the DWC_otg controller has detected a
  30382. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  30383. + * low power mode, the handler must brings the controller out of low
  30384. + * power mode. The controller automatically begins resume
  30385. + * signaling. The handler schedules a time to stop resume signaling.
  30386. + */
  30387. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  30388. +{
  30389. + gintsts_data_t gintsts;
  30390. +
  30391. + DWC_DEBUGPL(DBG_ANY,
  30392. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  30393. +
  30394. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  30395. +
  30396. + if (dwc_otg_is_device_mode(core_if)) {
  30397. + dctl_data_t dctl = {.d32 = 0 };
  30398. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  30399. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  30400. + dsts));
  30401. + if (core_if->lx_state == DWC_OTG_L2) {
  30402. +#ifdef PARTIAL_POWER_DOWN
  30403. + if (core_if->hwcfg4.b.power_optimiz) {
  30404. + pcgcctl_data_t power = {.d32 = 0 };
  30405. +
  30406. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  30407. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  30408. + power.d32);
  30409. +
  30410. + power.b.stoppclk = 0;
  30411. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30412. +
  30413. + power.b.pwrclmp = 0;
  30414. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30415. +
  30416. + power.b.rstpdwnmodule = 0;
  30417. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30418. + }
  30419. +#endif
  30420. + /* Clear the Remote Wakeup Signaling */
  30421. + dctl.b.rmtwkupsig = 1;
  30422. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  30423. + dctl, dctl.d32, 0);
  30424. +
  30425. + DWC_SPINUNLOCK(core_if->lock);
  30426. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  30427. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  30428. + }
  30429. + DWC_SPINLOCK(core_if->lock);
  30430. + } else {
  30431. + glpmcfg_data_t lpmcfg;
  30432. + lpmcfg.d32 =
  30433. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  30434. + lpmcfg.b.hird_thres &= (~(1 << 4));
  30435. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  30436. + lpmcfg.d32);
  30437. + }
  30438. + /** Change to L0 state*/
  30439. + core_if->lx_state = DWC_OTG_L0;
  30440. + } else {
  30441. + if (core_if->lx_state != DWC_OTG_L1) {
  30442. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30443. +
  30444. + /* Restart the Phy Clock */
  30445. + pcgcctl.b.stoppclk = 1;
  30446. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  30447. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  30448. + } else {
  30449. + /** Change to L0 state*/
  30450. + core_if->lx_state = DWC_OTG_L0;
  30451. + }
  30452. + }
  30453. +
  30454. + /* Clear interrupt */
  30455. + gintsts.d32 = 0;
  30456. + gintsts.b.wkupintr = 1;
  30457. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30458. +
  30459. + return 1;
  30460. +}
  30461. +
  30462. +/**
  30463. + * This interrupt indicates that the Wakeup Logic has detected a
  30464. + * Device disconnect.
  30465. + */
  30466. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  30467. +{
  30468. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  30469. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  30470. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30471. +
  30472. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30473. +
  30474. + if (!core_if->hibernation_suspend) {
  30475. + DWC_PRINTF("Already exited from Hibernation\n");
  30476. + return 1;
  30477. + }
  30478. +
  30479. + /* Switch on the voltage to the core */
  30480. + gpwrdn.b.pwrdnswtch = 1;
  30481. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30482. + dwc_udelay(10);
  30483. +
  30484. + /* Reset the core */
  30485. + gpwrdn.d32 = 0;
  30486. + gpwrdn.b.pwrdnrstn = 1;
  30487. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30488. + dwc_udelay(10);
  30489. +
  30490. + /* Disable power clamps*/
  30491. + gpwrdn.d32 = 0;
  30492. + gpwrdn.b.pwrdnclmp = 1;
  30493. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30494. +
  30495. + /* Remove reset the core signal */
  30496. + gpwrdn.d32 = 0;
  30497. + gpwrdn.b.pwrdnrstn = 1;
  30498. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30499. + dwc_udelay(10);
  30500. +
  30501. + /* Disable PMU interrupt */
  30502. + gpwrdn.d32 = 0;
  30503. + gpwrdn.b.pmuintsel = 1;
  30504. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30505. +
  30506. + core_if->hibernation_suspend = 0;
  30507. +
  30508. + /* Disable PMU */
  30509. + gpwrdn.d32 = 0;
  30510. + gpwrdn.b.pmuactv = 1;
  30511. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30512. + dwc_udelay(10);
  30513. +
  30514. + if (gpwrdn_temp.b.idsts) {
  30515. + core_if->op_state = B_PERIPHERAL;
  30516. + dwc_otg_core_init(core_if);
  30517. + dwc_otg_enable_global_interrupts(core_if);
  30518. + cil_pcd_start(core_if);
  30519. + } else {
  30520. + core_if->op_state = A_HOST;
  30521. + dwc_otg_core_init(core_if);
  30522. + dwc_otg_enable_global_interrupts(core_if);
  30523. + cil_hcd_start(core_if);
  30524. + }
  30525. +
  30526. + return 1;
  30527. +}
  30528. +
  30529. +/**
  30530. + * This interrupt indicates that the Wakeup Logic has detected a
  30531. + * remote wakeup sequence.
  30532. + */
  30533. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  30534. +{
  30535. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30536. + DWC_DEBUGPL(DBG_ANY,
  30537. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  30538. +
  30539. + if (!core_if->hibernation_suspend) {
  30540. + DWC_PRINTF("Already exited from Hibernation\n");
  30541. + return 1;
  30542. + }
  30543. +
  30544. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30545. + if (gpwrdn.b.idsts) { // Device Mode
  30546. + if ((core_if->power_down == 2)
  30547. + && (core_if->hibernation_suspend == 1)) {
  30548. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  30549. + }
  30550. + } else {
  30551. + if ((core_if->power_down == 2)
  30552. + && (core_if->hibernation_suspend == 1)) {
  30553. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  30554. + }
  30555. + }
  30556. + return 1;
  30557. +}
  30558. +
  30559. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  30560. +{
  30561. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30562. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  30563. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  30564. +
  30565. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30566. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30567. + if (core_if->power_down == 2) {
  30568. + if (!core_if->hibernation_suspend) {
  30569. + DWC_PRINTF("Already exited from Hibernation\n");
  30570. + return 1;
  30571. + }
  30572. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  30573. + /* Switch on the voltage to the core */
  30574. + gpwrdn.b.pwrdnswtch = 1;
  30575. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30576. + dwc_udelay(10);
  30577. +
  30578. + /* Reset the core */
  30579. + gpwrdn.d32 = 0;
  30580. + gpwrdn.b.pwrdnrstn = 1;
  30581. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30582. + dwc_udelay(10);
  30583. +
  30584. + /* Disable power clamps */
  30585. + gpwrdn.d32 = 0;
  30586. + gpwrdn.b.pwrdnclmp = 1;
  30587. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30588. +
  30589. + /* Remove reset the core signal */
  30590. + gpwrdn.d32 = 0;
  30591. + gpwrdn.b.pwrdnrstn = 1;
  30592. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30593. + dwc_udelay(10);
  30594. +
  30595. + /* Disable PMU interrupt */
  30596. + gpwrdn.d32 = 0;
  30597. + gpwrdn.b.pmuintsel = 1;
  30598. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30599. +
  30600. + /*Indicates that we are exiting from hibernation */
  30601. + core_if->hibernation_suspend = 0;
  30602. +
  30603. + /* Disable PMU */
  30604. + gpwrdn.d32 = 0;
  30605. + gpwrdn.b.pmuactv = 1;
  30606. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30607. + dwc_udelay(10);
  30608. +
  30609. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  30610. + if (gpwrdn.b.dis_vbus == 1) {
  30611. + gpwrdn.d32 = 0;
  30612. + gpwrdn.b.dis_vbus = 1;
  30613. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30614. + }
  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. +
  30629. + if (core_if->adp_enable) {
  30630. + uint8_t is_host = 0;
  30631. + DWC_SPINUNLOCK(core_if->lock);
  30632. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  30633. +#ifndef DWC_HOST_ONLY
  30634. + if (gpwrdn_temp.b.idsts)
  30635. + core_if->lock = otg_dev->pcd->lock;
  30636. +#endif
  30637. +#ifndef DWC_DEVICE_ONLY
  30638. + if (!gpwrdn_temp.b.idsts) {
  30639. + core_if->lock = otg_dev->hcd->lock;
  30640. + is_host = 1;
  30641. + }
  30642. +#endif
  30643. + DWC_PRINTF("RESTART ADP\n");
  30644. + if (core_if->adp.probe_enabled)
  30645. + dwc_otg_adp_probe_stop(core_if);
  30646. + if (core_if->adp.sense_enabled)
  30647. + dwc_otg_adp_sense_stop(core_if);
  30648. + if (core_if->adp.sense_timer_started)
  30649. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  30650. + if (core_if->adp.vbuson_timer_started)
  30651. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  30652. + core_if->adp.probe_timer_values[0] = -1;
  30653. + core_if->adp.probe_timer_values[1] = -1;
  30654. + core_if->adp.sense_timer_started = 0;
  30655. + core_if->adp.vbuson_timer_started = 0;
  30656. + core_if->adp.probe_counter = 0;
  30657. + core_if->adp.gpwrdn = 0;
  30658. +
  30659. + /* Disable PMU and restart ADP */
  30660. + gpwrdn_temp.d32 = 0;
  30661. + gpwrdn_temp.b.pmuactv = 1;
  30662. + gpwrdn_temp.b.pmuintsel = 1;
  30663. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30664. + DWC_PRINTF("Check point 1\n");
  30665. + dwc_mdelay(110);
  30666. + dwc_otg_adp_start(core_if, is_host);
  30667. + DWC_SPINLOCK(core_if->lock);
  30668. + }
  30669. +
  30670. +
  30671. + return 1;
  30672. +}
  30673. +
  30674. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  30675. +{
  30676. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30677. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  30678. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30679. +
  30680. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30681. + if (core_if->power_down == 2) {
  30682. + if (!core_if->hibernation_suspend) {
  30683. + DWC_PRINTF("Already exited from Hibernation\n");
  30684. + return 1;
  30685. + }
  30686. +
  30687. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  30688. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  30689. + gpwrdn.b.bsessvld == 0) {
  30690. + /* Save gpwrdn register for further usage if stschng interrupt */
  30691. + core_if->gr_backup->gpwrdn_local =
  30692. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30693. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  30694. + return 1;
  30695. + }
  30696. +
  30697. + /* Switch on the voltage to the core */
  30698. + gpwrdn.d32 = 0;
  30699. + gpwrdn.b.pwrdnswtch = 1;
  30700. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30701. + dwc_udelay(10);
  30702. +
  30703. + /* Reset the core */
  30704. + gpwrdn.d32 = 0;
  30705. + gpwrdn.b.pwrdnrstn = 1;
  30706. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30707. + dwc_udelay(10);
  30708. +
  30709. + /* Disable power clamps */
  30710. + gpwrdn.d32 = 0;
  30711. + gpwrdn.b.pwrdnclmp = 1;
  30712. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30713. +
  30714. + /* Remove reset the core signal */
  30715. + gpwrdn.d32 = 0;
  30716. + gpwrdn.b.pwrdnrstn = 1;
  30717. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30718. + dwc_udelay(10);
  30719. +
  30720. + /* Disable PMU interrupt */
  30721. + gpwrdn.d32 = 0;
  30722. + gpwrdn.b.pmuintsel = 1;
  30723. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30724. + dwc_udelay(10);
  30725. +
  30726. + /*Indicates that we are exiting from hibernation */
  30727. + core_if->hibernation_suspend = 0;
  30728. +
  30729. + /* Disable PMU */
  30730. + gpwrdn.d32 = 0;
  30731. + gpwrdn.b.pmuactv = 1;
  30732. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30733. + dwc_udelay(10);
  30734. +
  30735. + core_if->op_state = B_PERIPHERAL;
  30736. + dwc_otg_core_init(core_if);
  30737. + dwc_otg_enable_global_interrupts(core_if);
  30738. + cil_pcd_start(core_if);
  30739. +
  30740. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  30741. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  30742. + /*
  30743. + * Initiate SRP after initial ADP probe.
  30744. + */
  30745. + dwc_otg_initiate_srp(core_if);
  30746. + }
  30747. + }
  30748. +
  30749. + return 1;
  30750. +}
  30751. +/**
  30752. + * This interrupt indicates that the Wakeup Logic has detected a
  30753. + * status change either on IDDIG or BSessVld.
  30754. + */
  30755. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  30756. +{
  30757. + int retval;
  30758. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30759. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  30760. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  30761. +
  30762. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30763. +
  30764. + if (core_if->power_down == 2) {
  30765. + if (core_if->hibernation_suspend <= 0) {
  30766. + DWC_PRINTF("Already exited from Hibernation\n");
  30767. + return 1;
  30768. + } else
  30769. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  30770. +
  30771. + } else {
  30772. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  30773. + }
  30774. +
  30775. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30776. +
  30777. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  30778. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  30779. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  30780. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  30781. + }
  30782. +
  30783. + return retval;
  30784. +}
  30785. +
  30786. +/**
  30787. + * This interrupt indicates that the Wakeup Logic has detected a
  30788. + * SRP.
  30789. + */
  30790. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  30791. +{
  30792. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30793. +
  30794. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30795. +
  30796. + if (!core_if->hibernation_suspend) {
  30797. + DWC_PRINTF("Already exited from Hibernation\n");
  30798. + return 1;
  30799. + }
  30800. +#ifdef DWC_DEV_SRPCAP
  30801. + if (core_if->pwron_timer_started) {
  30802. + core_if->pwron_timer_started = 0;
  30803. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  30804. + }
  30805. +#endif
  30806. +
  30807. + /* Switch on the voltage to the core */
  30808. + gpwrdn.b.pwrdnswtch = 1;
  30809. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30810. + dwc_udelay(10);
  30811. +
  30812. + /* Reset the core */
  30813. + gpwrdn.d32 = 0;
  30814. + gpwrdn.b.pwrdnrstn = 1;
  30815. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30816. + dwc_udelay(10);
  30817. +
  30818. + /* Disable power clamps */
  30819. + gpwrdn.d32 = 0;
  30820. + gpwrdn.b.pwrdnclmp = 1;
  30821. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30822. +
  30823. + /* Remove reset the core signal */
  30824. + gpwrdn.d32 = 0;
  30825. + gpwrdn.b.pwrdnrstn = 1;
  30826. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30827. + dwc_udelay(10);
  30828. +
  30829. + /* Disable PMU interrupt */
  30830. + gpwrdn.d32 = 0;
  30831. + gpwrdn.b.pmuintsel = 1;
  30832. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30833. +
  30834. + /* Indicates that we are exiting from hibernation */
  30835. + core_if->hibernation_suspend = 0;
  30836. +
  30837. + /* Disable PMU */
  30838. + gpwrdn.d32 = 0;
  30839. + gpwrdn.b.pmuactv = 1;
  30840. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30841. + dwc_udelay(10);
  30842. +
  30843. + /* Programm Disable VBUS to 0 */
  30844. + gpwrdn.d32 = 0;
  30845. + gpwrdn.b.dis_vbus = 1;
  30846. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30847. +
  30848. + /*Initialize the core as Host */
  30849. + core_if->op_state = A_HOST;
  30850. + dwc_otg_core_init(core_if);
  30851. + dwc_otg_enable_global_interrupts(core_if);
  30852. + cil_hcd_start(core_if);
  30853. +
  30854. + return 1;
  30855. +}
  30856. +
  30857. +/** This interrupt indicates that restore command after Hibernation
  30858. + * was completed by the core. */
  30859. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  30860. +{
  30861. + pcgcctl_data_t pcgcctl;
  30862. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  30863. +
  30864. + //TODO De-assert restore signal. 8.a
  30865. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  30866. + if (pcgcctl.b.restoremode == 1) {
  30867. + gintmsk_data_t gintmsk = {.d32 = 0 };
  30868. + /*
  30869. + * If restore mode is Remote Wakeup,
  30870. + * unmask Remote Wakeup interrupt.
  30871. + */
  30872. + gintmsk.b.wkupintr = 1;
  30873. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  30874. + 0, gintmsk.d32);
  30875. + }
  30876. +
  30877. + return 1;
  30878. +}
  30879. +
  30880. +/**
  30881. + * This interrupt indicates that a device has been disconnected from
  30882. + * the root port.
  30883. + */
  30884. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  30885. +{
  30886. + gintsts_data_t gintsts;
  30887. +
  30888. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  30889. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  30890. + op_state_str(core_if));
  30891. +
  30892. +/** @todo Consolidate this if statement. */
  30893. +#ifndef DWC_HOST_ONLY
  30894. + if (core_if->op_state == B_HOST) {
  30895. + /* If in device mode Disconnect and stop the HCD, then
  30896. + * start the PCD. */
  30897. + DWC_SPINUNLOCK(core_if->lock);
  30898. + cil_hcd_disconnect(core_if);
  30899. + cil_pcd_start(core_if);
  30900. + DWC_SPINLOCK(core_if->lock);
  30901. + core_if->op_state = B_PERIPHERAL;
  30902. + } else if (dwc_otg_is_device_mode(core_if)) {
  30903. + gotgctl_data_t gotgctl = {.d32 = 0 };
  30904. + gotgctl.d32 =
  30905. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  30906. + if (gotgctl.b.hstsethnpen == 1) {
  30907. + /* Do nothing, if HNP in process the OTG
  30908. + * interrupt "Host Negotiation Detected"
  30909. + * interrupt will do the mode switch.
  30910. + */
  30911. + } else if (gotgctl.b.devhnpen == 0) {
  30912. + /* If in device mode Disconnect and stop the HCD, then
  30913. + * start the PCD. */
  30914. + DWC_SPINUNLOCK(core_if->lock);
  30915. + cil_hcd_disconnect(core_if);
  30916. + cil_pcd_start(core_if);
  30917. + DWC_SPINLOCK(core_if->lock);
  30918. + core_if->op_state = B_PERIPHERAL;
  30919. + } else {
  30920. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  30921. + }
  30922. + } else {
  30923. + if (core_if->op_state == A_HOST) {
  30924. + /* A-Cable still connected but device disconnected. */
  30925. + cil_hcd_disconnect(core_if);
  30926. + if (core_if->adp_enable) {
  30927. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  30928. + cil_hcd_stop(core_if);
  30929. + /* Enable Power Down Logic */
  30930. + gpwrdn.b.pmuintsel = 1;
  30931. + gpwrdn.b.pmuactv = 1;
  30932. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30933. + gpwrdn, 0, gpwrdn.d32);
  30934. + dwc_otg_adp_probe_start(core_if);
  30935. +
  30936. + /* Power off the core */
  30937. + if (core_if->power_down == 2) {
  30938. + gpwrdn.d32 = 0;
  30939. + gpwrdn.b.pwrdnswtch = 1;
  30940. + DWC_MODIFY_REG32
  30941. + (&core_if->core_global_regs->gpwrdn,
  30942. + gpwrdn.d32, 0);
  30943. + }
  30944. + }
  30945. + }
  30946. + }
  30947. +#endif
  30948. + /* Change to L3(OFF) state */
  30949. + core_if->lx_state = DWC_OTG_L3;
  30950. +
  30951. + gintsts.d32 = 0;
  30952. + gintsts.b.disconnect = 1;
  30953. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30954. + return 1;
  30955. +}
  30956. +
  30957. +/**
  30958. + * This interrupt indicates that SUSPEND state has been detected on
  30959. + * the USB.
  30960. + *
  30961. + * For HNP the USB Suspend interrupt signals the change from
  30962. + * "a_peripheral" to "a_host".
  30963. + *
  30964. + * When power management is enabled the core will be put in low power
  30965. + * mode.
  30966. + */
  30967. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  30968. +{
  30969. + dsts_data_t dsts;
  30970. + gintsts_data_t gintsts;
  30971. + dcfg_data_t dcfg;
  30972. +
  30973. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  30974. +
  30975. + if (dwc_otg_is_device_mode(core_if)) {
  30976. + /* Check the Device status register to determine if the Suspend
  30977. + * state is active. */
  30978. + dsts.d32 =
  30979. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  30980. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  30981. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  30982. + "HWCFG4.power Optimize=%d\n",
  30983. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  30984. +
  30985. +#ifdef PARTIAL_POWER_DOWN
  30986. +/** @todo Add a module parameter for power management. */
  30987. +
  30988. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  30989. + pcgcctl_data_t power = {.d32 = 0 };
  30990. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  30991. +
  30992. + power.b.pwrclmp = 1;
  30993. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30994. +
  30995. + power.b.rstpdwnmodule = 1;
  30996. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  30997. +
  30998. + power.b.stoppclk = 1;
  30999. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  31000. +
  31001. + } else {
  31002. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  31003. + }
  31004. +#endif
  31005. + /* PCD callback for suspend. Release the lock inside of callback function */
  31006. + cil_pcd_suspend(core_if);
  31007. + if (core_if->power_down == 2)
  31008. + {
  31009. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  31010. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  31011. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  31012. +
  31013. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  31014. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31015. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31016. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  31017. +
  31018. + /* Change to L2(suspend) state */
  31019. + core_if->lx_state = DWC_OTG_L2;
  31020. +
  31021. + /* Clear interrupt in gintsts */
  31022. + gintsts.d32 = 0;
  31023. + gintsts.b.usbsuspend = 1;
  31024. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31025. + gintsts, gintsts.d32);
  31026. + DWC_PRINTF("Start of hibernation completed\n");
  31027. + dwc_otg_save_global_regs(core_if);
  31028. + dwc_otg_save_dev_regs(core_if);
  31029. +
  31030. + gusbcfg.d32 =
  31031. + DWC_READ_REG32(&core_if->core_global_regs->
  31032. + gusbcfg);
  31033. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  31034. + /* ULPI interface */
  31035. + /* Suspend the Phy Clock */
  31036. + pcgcctl.d32 = 0;
  31037. + pcgcctl.b.stoppclk = 1;
  31038. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  31039. + pcgcctl.d32);
  31040. + dwc_udelay(10);
  31041. + gpwrdn.b.pmuactv = 1;
  31042. + DWC_MODIFY_REG32(&core_if->
  31043. + core_global_regs->
  31044. + gpwrdn, 0, gpwrdn.d32);
  31045. + } else {
  31046. + /* UTMI+ Interface */
  31047. + gpwrdn.b.pmuactv = 1;
  31048. + DWC_MODIFY_REG32(&core_if->
  31049. + core_global_regs->
  31050. + gpwrdn, 0, gpwrdn.d32);
  31051. + dwc_udelay(10);
  31052. + pcgcctl.b.stoppclk = 1;
  31053. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  31054. + pcgcctl.d32);
  31055. + dwc_udelay(10);
  31056. + }
  31057. +
  31058. + /* Set flag to indicate that we are in hibernation */
  31059. + core_if->hibernation_suspend = 1;
  31060. + /* Enable interrupts from wake up logic */
  31061. + gpwrdn.d32 = 0;
  31062. + gpwrdn.b.pmuintsel = 1;
  31063. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31064. + gpwrdn, 0, gpwrdn.d32);
  31065. + dwc_udelay(10);
  31066. +
  31067. + /* Unmask device mode interrupts in GPWRDN */
  31068. + gpwrdn.d32 = 0;
  31069. + gpwrdn.b.rst_det_msk = 1;
  31070. + gpwrdn.b.lnstchng_msk = 1;
  31071. + gpwrdn.b.sts_chngint_msk = 1;
  31072. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31073. + gpwrdn, 0, gpwrdn.d32);
  31074. + dwc_udelay(10);
  31075. +
  31076. + /* Enable Power Down Clamp */
  31077. + gpwrdn.d32 = 0;
  31078. + gpwrdn.b.pwrdnclmp = 1;
  31079. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31080. + gpwrdn, 0, gpwrdn.d32);
  31081. + dwc_udelay(10);
  31082. +
  31083. + /* Switch off VDD */
  31084. + gpwrdn.d32 = 0;
  31085. + gpwrdn.b.pwrdnswtch = 1;
  31086. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31087. + gpwrdn, 0, gpwrdn.d32);
  31088. +
  31089. + /* Save gpwrdn register for further usage if stschng interrupt */
  31090. + core_if->gr_backup->gpwrdn_local =
  31091. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31092. + DWC_PRINTF("Hibernation completed\n");
  31093. +
  31094. + return 1;
  31095. + }
  31096. + } else if (core_if->power_down == 3) {
  31097. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31098. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  31099. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  31100. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  31101. +
  31102. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  31103. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  31104. + core_if->xhib = 1;
  31105. +
  31106. + /* Clear interrupt in gintsts */
  31107. + gintsts.d32 = 0;
  31108. + gintsts.b.usbsuspend = 1;
  31109. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31110. + gintsts, gintsts.d32);
  31111. +
  31112. + dwc_otg_save_global_regs(core_if);
  31113. + dwc_otg_save_dev_regs(core_if);
  31114. +
  31115. + /* Wait for 10 PHY clocks */
  31116. + dwc_udelay(10);
  31117. +
  31118. + /* Program GPIO register while entering to xHib */
  31119. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  31120. +
  31121. + pcgcctl.b.enbl_extnd_hiber = 1;
  31122. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31123. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31124. +
  31125. + pcgcctl.d32 = 0;
  31126. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  31127. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31128. +
  31129. + pcgcctl.d32 = 0;
  31130. + pcgcctl.b.extnd_hiber_switch = 1;
  31131. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31132. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  31133. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31134. +
  31135. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  31136. +
  31137. + return 1;
  31138. + }
  31139. + }
  31140. + } else {
  31141. + if (core_if->op_state == A_PERIPHERAL) {
  31142. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  31143. + /* Clear the a_peripheral flag, back to a_host. */
  31144. + DWC_SPINUNLOCK(core_if->lock);
  31145. + cil_pcd_stop(core_if);
  31146. + cil_hcd_start(core_if);
  31147. + DWC_SPINLOCK(core_if->lock);
  31148. + core_if->op_state = A_HOST;
  31149. + }
  31150. + }
  31151. +
  31152. + /* Change to L2(suspend) state */
  31153. + core_if->lx_state = DWC_OTG_L2;
  31154. +
  31155. + /* Clear interrupt */
  31156. + gintsts.d32 = 0;
  31157. + gintsts.b.usbsuspend = 1;
  31158. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  31159. +
  31160. + return 1;
  31161. +}
  31162. +
  31163. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  31164. +{
  31165. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31166. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31167. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  31168. +
  31169. + dwc_udelay(10);
  31170. +
  31171. + /* Program GPIO register while entering to xHib */
  31172. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  31173. +
  31174. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  31175. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  31176. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31177. + dwc_udelay(10);
  31178. +
  31179. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  31180. + gpwrdn.b.restore = 1;
  31181. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  31182. + dwc_udelay(10);
  31183. +
  31184. + restore_lpm_i2c_regs(core_if);
  31185. +
  31186. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31187. + pcgcctl.b.max_xcvrselect = 1;
  31188. + pcgcctl.b.ess_reg_restored = 0;
  31189. + pcgcctl.b.extnd_hiber_switch = 0;
  31190. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  31191. + pcgcctl.b.enbl_extnd_hiber = 1;
  31192. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31193. +
  31194. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  31195. + gahbcfg.b.glblintrmsk = 1;
  31196. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  31197. +
  31198. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  31199. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  31200. +
  31201. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  31202. + core_if->gr_backup->gusbcfg_local);
  31203. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  31204. + core_if->dr_backup->dcfg);
  31205. +
  31206. + pcgcctl.d32 = 0;
  31207. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31208. + pcgcctl.b.max_xcvrselect = 1;
  31209. + pcgcctl.d32 |= 0x608;
  31210. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31211. + dwc_udelay(10);
  31212. +
  31213. + pcgcctl.d32 = 0;
  31214. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31215. + pcgcctl.b.max_xcvrselect = 1;
  31216. + pcgcctl.b.ess_reg_restored = 1;
  31217. + pcgcctl.b.enbl_extnd_hiber = 1;
  31218. + pcgcctl.b.rstpdwnmodule = 1;
  31219. + pcgcctl.b.restoremode = 1;
  31220. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31221. +
  31222. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  31223. +
  31224. + return 1;
  31225. +}
  31226. +
  31227. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31228. +/**
  31229. + * This function hadles LPM transaction received interrupt.
  31230. + */
  31231. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  31232. +{
  31233. + glpmcfg_data_t lpmcfg;
  31234. + gintsts_data_t gintsts;
  31235. +
  31236. + if (!core_if->core_params->lpm_enable) {
  31237. + DWC_PRINTF("Unexpected LPM interrupt\n");
  31238. + }
  31239. +
  31240. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  31241. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  31242. +
  31243. + if (dwc_otg_is_host_mode(core_if)) {
  31244. + cil_hcd_sleep(core_if);
  31245. + } else {
  31246. + lpmcfg.b.hird_thres |= (1 << 4);
  31247. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  31248. + lpmcfg.d32);
  31249. + }
  31250. +
  31251. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  31252. + dwc_udelay(10);
  31253. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  31254. + if (lpmcfg.b.prt_sleep_sts) {
  31255. + /* Save the current state */
  31256. + core_if->lx_state = DWC_OTG_L1;
  31257. + }
  31258. +
  31259. + /* Clear interrupt */
  31260. + gintsts.d32 = 0;
  31261. + gintsts.b.lpmtranrcvd = 1;
  31262. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  31263. + return 1;
  31264. +}
  31265. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  31266. +
  31267. +/**
  31268. + * This function returns the Core Interrupt register.
  31269. + */
  31270. +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)
  31271. +{
  31272. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  31273. + gintsts_data_t gintsts;
  31274. + gintmsk_data_t gintmsk;
  31275. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  31276. + gintmsk_common.b.wkupintr = 1;
  31277. + gintmsk_common.b.sessreqintr = 1;
  31278. + gintmsk_common.b.conidstschng = 1;
  31279. + gintmsk_common.b.otgintr = 1;
  31280. + gintmsk_common.b.modemismatch = 1;
  31281. + gintmsk_common.b.disconnect = 1;
  31282. + gintmsk_common.b.usbsuspend = 1;
  31283. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31284. + gintmsk_common.b.lpmtranrcvd = 1;
  31285. +#endif
  31286. + gintmsk_common.b.restoredone = 1;
  31287. + if(dwc_otg_is_device_mode(core_if))
  31288. + {
  31289. + /** @todo: The port interrupt occurs while in device
  31290. + * mode. Added code to CIL to clear the interrupt for now!
  31291. + */
  31292. + gintmsk_common.b.portintr = 1;
  31293. + }
  31294. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  31295. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  31296. + if(fiq_enable) {
  31297. + local_fiq_disable();
  31298. + /* Pull in the interrupts that the FIQ has masked */
  31299. + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
  31300. + gintmsk.d32 |= gintmsk_common.d32;
  31301. + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
  31302. + reenable_gintmsk->d32 = gintmsk.d32;
  31303. + local_fiq_enable();
  31304. + }
  31305. +
  31306. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  31307. +
  31308. +#ifdef DEBUG
  31309. + /* if any common interrupts set */
  31310. + if (gintsts.d32 & gintmsk_common.d32) {
  31311. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  31312. + gintsts.d32, gintmsk.d32);
  31313. + }
  31314. +#endif
  31315. + if (!fiq_enable){
  31316. + if (gahbcfg.b.glblintrmsk)
  31317. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  31318. + else
  31319. + return 0;
  31320. + } else {
  31321. + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
  31322. + * Can't trust the global interrupt mask bit in this case.
  31323. + */
  31324. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  31325. + }
  31326. +
  31327. +}
  31328. +
  31329. +/* MACRO for clearing interupt bits in GPWRDN register */
  31330. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  31331. +do { \
  31332. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  31333. + gpwrdn.b.__intr = 1; \
  31334. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  31335. + 0, gpwrdn.d32); \
  31336. +} while (0)
  31337. +
  31338. +/**
  31339. + * Common interrupt handler.
  31340. + *
  31341. + * The common interrupts are those that occur in both Host and Device mode.
  31342. + * This handler handles the following interrupts:
  31343. + * - Mode Mismatch Interrupt
  31344. + * - Disconnect Interrupt
  31345. + * - OTG Interrupt
  31346. + * - Connector ID Status Change Interrupt
  31347. + * - Session Request Interrupt.
  31348. + * - Resume / Remote Wakeup Detected Interrupt.
  31349. + * - LPM Transaction Received Interrupt
  31350. + * - ADP Transaction Received Interrupt
  31351. + *
  31352. + */
  31353. +int32_t dwc_otg_handle_common_intr(void *dev)
  31354. +{
  31355. + int retval = 0;
  31356. + gintsts_data_t gintsts;
  31357. + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
  31358. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31359. + dwc_otg_device_t *otg_dev = dev;
  31360. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  31361. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31362. + if (dwc_otg_is_device_mode(core_if))
  31363. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  31364. +
  31365. + if (core_if->lock)
  31366. + DWC_SPINLOCK(core_if->lock);
  31367. +
  31368. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  31369. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  31370. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  31371. + core_if->xhib = 2;
  31372. + if (core_if->lock)
  31373. + DWC_SPINUNLOCK(core_if->lock);
  31374. +
  31375. + return retval;
  31376. + }
  31377. +
  31378. + if (core_if->hibernation_suspend <= 0) {
  31379. + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
  31380. + * of this handler - god only knows why it's done like this
  31381. + */
  31382. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
  31383. +
  31384. + if (gintsts.b.modemismatch) {
  31385. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  31386. + }
  31387. + if (gintsts.b.otgintr) {
  31388. + retval |= dwc_otg_handle_otg_intr(core_if);
  31389. + }
  31390. + if (gintsts.b.conidstschng) {
  31391. + retval |=
  31392. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  31393. + }
  31394. + if (gintsts.b.disconnect) {
  31395. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  31396. + }
  31397. + if (gintsts.b.sessreqintr) {
  31398. + retval |= dwc_otg_handle_session_req_intr(core_if);
  31399. + }
  31400. + if (gintsts.b.wkupintr) {
  31401. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  31402. + }
  31403. + if (gintsts.b.usbsuspend) {
  31404. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  31405. + }
  31406. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31407. + if (gintsts.b.lpmtranrcvd) {
  31408. + retval |= dwc_otg_handle_lpm_intr(core_if);
  31409. + }
  31410. +#endif
  31411. + if (gintsts.b.restoredone) {
  31412. + gintsts.d32 = 0;
  31413. + if (core_if->power_down == 2)
  31414. + core_if->hibernation_suspend = -1;
  31415. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  31416. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31417. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31418. + dctl_data_t dctl = {.d32 = 0 };
  31419. +
  31420. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31421. + gintsts, 0xFFFFFFFF);
  31422. +
  31423. + DWC_DEBUGPL(DBG_ANY,
  31424. + "RESTORE DONE generated\n");
  31425. +
  31426. + gpwrdn.b.restore = 1;
  31427. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  31428. + dwc_udelay(10);
  31429. +
  31430. + pcgcctl.b.rstpdwnmodule = 1;
  31431. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  31432. +
  31433. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  31434. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  31435. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  31436. + dwc_udelay(50);
  31437. +
  31438. + dctl.b.pwronprgdone = 1;
  31439. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  31440. + dwc_udelay(10);
  31441. +
  31442. + dwc_otg_restore_global_regs(core_if);
  31443. + dwc_otg_restore_dev_regs(core_if, 0);
  31444. +
  31445. + dctl.d32 = 0;
  31446. + dctl.b.pwronprgdone = 1;
  31447. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  31448. + dwc_udelay(10);
  31449. +
  31450. + pcgcctl.d32 = 0;
  31451. + pcgcctl.b.enbl_extnd_hiber = 1;
  31452. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  31453. +
  31454. + /* The core will be in ON STATE */
  31455. + core_if->lx_state = DWC_OTG_L0;
  31456. + core_if->xhib = 0;
  31457. +
  31458. + DWC_SPINUNLOCK(core_if->lock);
  31459. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  31460. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  31461. + }
  31462. + DWC_SPINLOCK(core_if->lock);
  31463. +
  31464. + }
  31465. +
  31466. + gintsts.b.restoredone = 1;
  31467. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  31468. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  31469. + retval |= 1;
  31470. + }
  31471. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  31472. + /* The port interrupt occurs while in device mode with HPRT0
  31473. + * Port Enable/Disable.
  31474. + */
  31475. + gintsts.d32 = 0;
  31476. + gintsts.b.portintr = 1;
  31477. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  31478. + retval |= 1;
  31479. + gintmsk_reenable.b.portintr = 1;
  31480. +
  31481. + }
  31482. + /* Did we actually handle anything? if so, unmask the interrupt */
  31483. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
  31484. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
  31485. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
  31486. + if (retval && fiq_enable) {
  31487. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
  31488. + }
  31489. +
  31490. + } else {
  31491. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  31492. +
  31493. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  31494. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  31495. + if (gpwrdn.b.linestate == 0) {
  31496. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  31497. + } else {
  31498. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  31499. + }
  31500. +
  31501. + retval |= 1;
  31502. + }
  31503. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  31504. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  31505. + /* remote wakeup from hibernation */
  31506. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  31507. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  31508. + } else {
  31509. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  31510. + }
  31511. + retval |= 1;
  31512. + }
  31513. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  31514. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  31515. + if (gpwrdn.b.linestate == 0) {
  31516. + DWC_PRINTF("Reset detected\n");
  31517. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  31518. + }
  31519. + }
  31520. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  31521. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  31522. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  31523. + retval |= 1;
  31524. + }
  31525. + }
  31526. + /* Handle ADP interrupt here */
  31527. + if (gpwrdn.b.adp_int) {
  31528. + DWC_PRINTF("ADP interrupt\n");
  31529. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  31530. + dwc_otg_adp_handle_intr(core_if);
  31531. + retval |= 1;
  31532. + }
  31533. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  31534. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  31535. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  31536. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  31537. +
  31538. + retval |= 1;
  31539. + }
  31540. + if (core_if->lock)
  31541. + DWC_SPINUNLOCK(core_if->lock);
  31542. + return retval;
  31543. +}
  31544. --- /dev/null
  31545. +++ b/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  31546. @@ -0,0 +1,705 @@
  31547. +/* ==========================================================================
  31548. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  31549. + * $Revision: #13 $
  31550. + * $Date: 2012/08/10 $
  31551. + * $Change: 2047372 $
  31552. + *
  31553. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  31554. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  31555. + * otherwise expressly agreed to in writing between Synopsys and you.
  31556. + *
  31557. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  31558. + * any End User Software License Agreement or Agreement for Licensed Product
  31559. + * with Synopsys or any supplement thereto. You are permitted to use and
  31560. + * redistribute this Software in source and binary forms, with or without
  31561. + * modification, provided that redistributions of source code must retain this
  31562. + * notice. You may not view, use, disclose, copy or distribute this file or
  31563. + * any information contained herein except pursuant to this license grant from
  31564. + * Synopsys. If you do not agree with this notice, including the disclaimer
  31565. + * below, then you are not authorized to use the Software.
  31566. + *
  31567. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  31568. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31569. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31570. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  31571. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31572. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31573. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31574. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31575. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31576. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31577. + * DAMAGE.
  31578. + * ========================================================================== */
  31579. +#if !defined(__DWC_CORE_IF_H__)
  31580. +#define __DWC_CORE_IF_H__
  31581. +
  31582. +#include "dwc_os.h"
  31583. +
  31584. +/** @file
  31585. + * This file defines DWC_OTG Core API
  31586. + */
  31587. +
  31588. +struct dwc_otg_core_if;
  31589. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  31590. +
  31591. +/** Maximum number of Periodic FIFOs */
  31592. +#define MAX_PERIO_FIFOS 15
  31593. +/** Maximum number of Periodic FIFOs */
  31594. +#define MAX_TX_FIFOS 15
  31595. +
  31596. +/** Maximum number of Endpoints/HostChannels */
  31597. +#define MAX_EPS_CHANNELS 16
  31598. +
  31599. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  31600. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  31601. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  31602. +
  31603. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  31604. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  31605. +
  31606. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  31607. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  31608. +
  31609. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  31610. +
  31611. +/** This function should be called on every hardware interrupt. */
  31612. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  31613. +
  31614. +/** @name OTG Core Parameters */
  31615. +/** @{ */
  31616. +
  31617. +/**
  31618. + * Specifies the OTG capabilities. The driver will automatically
  31619. + * detect the value for this parameter if none is specified.
  31620. + * 0 - HNP and SRP capable (default)
  31621. + * 1 - SRP Only capable
  31622. + * 2 - No HNP/SRP capable
  31623. + */
  31624. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  31625. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  31626. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  31627. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  31628. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  31629. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  31630. +
  31631. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  31632. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  31633. +#define dwc_param_opt_default 1
  31634. +
  31635. +/**
  31636. + * Specifies whether to use slave or DMA mode for accessing the data
  31637. + * FIFOs. The driver will automatically detect the value for this
  31638. + * parameter if none is specified.
  31639. + * 0 - Slave
  31640. + * 1 - DMA (default, if available)
  31641. + */
  31642. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  31643. + int32_t val);
  31644. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  31645. +#define dwc_param_dma_enable_default 1
  31646. +
  31647. +/**
  31648. + * When DMA mode is enabled specifies whether to use
  31649. + * address DMA or DMA Descritor mode for accessing the data
  31650. + * FIFOs in device mode. The driver will automatically detect
  31651. + * the value for this parameter if none is specified.
  31652. + * 0 - address DMA
  31653. + * 1 - DMA Descriptor(default, if available)
  31654. + */
  31655. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  31656. + int32_t val);
  31657. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  31658. +//#define dwc_param_dma_desc_enable_default 1
  31659. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  31660. +
  31661. +/** The DMA Burst size (applicable only for External DMA
  31662. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  31663. + */
  31664. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  31665. + int32_t val);
  31666. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  31667. +#define dwc_param_dma_burst_size_default 32
  31668. +
  31669. +/**
  31670. + * Specifies the maximum speed of operation in host and device mode.
  31671. + * The actual speed depends on the speed of the attached device and
  31672. + * the value of phy_type. The actual speed depends on the speed of the
  31673. + * attached device.
  31674. + * 0 - High Speed (default)
  31675. + * 1 - Full Speed
  31676. + */
  31677. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  31678. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  31679. +#define dwc_param_speed_default 0
  31680. +#define DWC_SPEED_PARAM_HIGH 0
  31681. +#define DWC_SPEED_PARAM_FULL 1
  31682. +
  31683. +/** Specifies whether low power mode is supported when attached
  31684. + * to a Full Speed or Low Speed device in host mode.
  31685. + * 0 - Don't support low power mode (default)
  31686. + * 1 - Support low power mode
  31687. + */
  31688. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  31689. + core_if, int32_t val);
  31690. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  31691. + * core_if);
  31692. +#define dwc_param_host_support_fs_ls_low_power_default 0
  31693. +
  31694. +/** Specifies the PHY clock rate in low power mode when connected to a
  31695. + * Low Speed device in host mode. This parameter is applicable only if
  31696. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  31697. + * then defaults to 6 MHZ otherwise 48 MHZ.
  31698. + *
  31699. + * 0 - 48 MHz
  31700. + * 1 - 6 MHz
  31701. + */
  31702. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  31703. + core_if, int32_t val);
  31704. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  31705. + core_if);
  31706. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  31707. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  31708. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  31709. +
  31710. +/**
  31711. + * 0 - Use cC FIFO size parameters
  31712. + * 1 - Allow dynamic FIFO sizing (default)
  31713. + */
  31714. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  31715. + int32_t val);
  31716. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  31717. + core_if);
  31718. +#define dwc_param_enable_dynamic_fifo_default 1
  31719. +
  31720. +/** Total number of 4-byte words in the data FIFO memory. This
  31721. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  31722. + * Tx FIFOs.
  31723. + * 32 to 32768 (default 8192)
  31724. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  31725. + */
  31726. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  31727. + int32_t val);
  31728. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  31729. +//#define dwc_param_data_fifo_size_default 8192
  31730. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  31731. +
  31732. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  31733. + * FIFO sizing is enabled.
  31734. + * 16 to 32768 (default 1064)
  31735. + */
  31736. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  31737. + int32_t val);
  31738. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  31739. +#define dwc_param_dev_rx_fifo_size_default 1064
  31740. +
  31741. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  31742. + * when dynamic FIFO sizing is enabled.
  31743. + * 16 to 32768 (default 1024)
  31744. + */
  31745. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31746. + core_if, int32_t val);
  31747. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31748. + core_if);
  31749. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  31750. +
  31751. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  31752. + * mode when dynamic FIFO sizing is enabled.
  31753. + * 4 to 768 (default 256)
  31754. + */
  31755. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31756. + int32_t val, int fifo_num);
  31757. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  31758. + core_if, int fifo_num);
  31759. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  31760. +
  31761. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  31762. + * FIFO sizing is enabled.
  31763. + * 16 to 32768 (default 1024)
  31764. + */
  31765. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  31766. + int32_t val);
  31767. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  31768. +//#define dwc_param_host_rx_fifo_size_default 1024
  31769. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  31770. +
  31771. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  31772. + * when Dynamic FIFO sizing is enabled in the core.
  31773. + * 16 to 32768 (default 1024)
  31774. + */
  31775. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31776. + core_if, int32_t val);
  31777. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31778. + core_if);
  31779. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  31780. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  31781. +
  31782. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  31783. + * FIFO sizing is enabled.
  31784. + * 16 to 32768 (default 1024)
  31785. + */
  31786. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  31787. + core_if, int32_t val);
  31788. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  31789. + core_if);
  31790. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  31791. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  31792. +
  31793. +/** The maximum transfer size supported in bytes.
  31794. + * 2047 to 65,535 (default 65,535)
  31795. + */
  31796. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  31797. + int32_t val);
  31798. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  31799. +#define dwc_param_max_transfer_size_default 65535
  31800. +
  31801. +/** The maximum number of packets in a transfer.
  31802. + * 15 to 511 (default 511)
  31803. + */
  31804. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  31805. + int32_t val);
  31806. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  31807. +#define dwc_param_max_packet_count_default 511
  31808. +
  31809. +/** The number of host channel registers to use.
  31810. + * 1 to 16 (default 12)
  31811. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  31812. + */
  31813. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  31814. + int32_t val);
  31815. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  31816. +//#define dwc_param_host_channels_default 12
  31817. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  31818. +
  31819. +/** The number of endpoints in addition to EP0 available for device
  31820. + * mode operations.
  31821. + * 1 to 15 (default 6 IN and OUT)
  31822. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  31823. + * endpoints in addition to EP0.
  31824. + */
  31825. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  31826. + int32_t val);
  31827. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  31828. +#define dwc_param_dev_endpoints_default 6
  31829. +
  31830. +/**
  31831. + * Specifies the type of PHY interface to use. By default, the driver
  31832. + * will automatically detect the phy_type.
  31833. + *
  31834. + * 0 - Full Speed PHY
  31835. + * 1 - UTMI+ (default)
  31836. + * 2 - ULPI
  31837. + */
  31838. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  31839. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  31840. +#define DWC_PHY_TYPE_PARAM_FS 0
  31841. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  31842. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  31843. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  31844. +
  31845. +/**
  31846. + * Specifies the UTMI+ Data Width. This parameter is
  31847. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  31848. + * PHY_TYPE, this parameter indicates the data width between
  31849. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  31850. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  31851. + * to "8 and 16 bits", meaning that the core has been
  31852. + * configured to work at either data path width.
  31853. + *
  31854. + * 8 or 16 bits (default 16)
  31855. + */
  31856. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  31857. + int32_t val);
  31858. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  31859. +//#define dwc_param_phy_utmi_width_default 16
  31860. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  31861. +
  31862. +/**
  31863. + * Specifies whether the ULPI operates at double or single
  31864. + * data rate. This parameter is only applicable if PHY_TYPE is
  31865. + * ULPI.
  31866. + *
  31867. + * 0 - single data rate ULPI interface with 8 bit wide data
  31868. + * bus (default)
  31869. + * 1 - double data rate ULPI interface with 4 bit wide data
  31870. + * bus
  31871. + */
  31872. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  31873. + int32_t val);
  31874. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  31875. +#define dwc_param_phy_ulpi_ddr_default 0
  31876. +
  31877. +/**
  31878. + * Specifies whether to use the internal or external supply to
  31879. + * drive the vbus with a ULPI phy.
  31880. + */
  31881. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  31882. + int32_t val);
  31883. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  31884. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  31885. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  31886. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  31887. +
  31888. +/**
  31889. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  31890. + * parameter is only applicable if PHY_TYPE is FS.
  31891. + * 0 - No (default)
  31892. + * 1 - Yes
  31893. + */
  31894. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  31895. + int32_t val);
  31896. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  31897. +#define dwc_param_i2c_enable_default 0
  31898. +
  31899. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  31900. + int32_t val);
  31901. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  31902. +#define dwc_param_ulpi_fs_ls_default 0
  31903. +
  31904. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  31905. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  31906. +#define dwc_param_ts_dline_default 0
  31907. +
  31908. +/**
  31909. + * Specifies whether dedicated transmit FIFOs are
  31910. + * enabled for non periodic IN endpoints in device mode
  31911. + * 0 - No
  31912. + * 1 - Yes
  31913. + */
  31914. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  31915. + int32_t val);
  31916. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  31917. + core_if);
  31918. +#define dwc_param_en_multiple_tx_fifo_default 1
  31919. +
  31920. +/** Number of 4-byte words in each of the Tx FIFOs in device
  31921. + * mode when dynamic FIFO sizing is enabled.
  31922. + * 4 to 768 (default 256)
  31923. + */
  31924. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31925. + int fifo_num, int32_t val);
  31926. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31927. + int fifo_num);
  31928. +#define dwc_param_dev_tx_fifo_size_default 768
  31929. +
  31930. +/** Thresholding enable flag-
  31931. + * bit 0 - enable non-ISO Tx thresholding
  31932. + * bit 1 - enable ISO Tx thresholding
  31933. + * bit 2 - enable Rx thresholding
  31934. + */
  31935. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  31936. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  31937. +#define dwc_param_thr_ctl_default 0
  31938. +
  31939. +/** Thresholding length for Tx
  31940. + * FIFOs in 32 bit DWORDs
  31941. + */
  31942. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  31943. + int32_t val);
  31944. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  31945. +#define dwc_param_tx_thr_length_default 64
  31946. +
  31947. +/** Thresholding length for Rx
  31948. + * FIFOs in 32 bit DWORDs
  31949. + */
  31950. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  31951. + int32_t val);
  31952. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  31953. +#define dwc_param_rx_thr_length_default 64
  31954. +
  31955. +/**
  31956. + * Specifies whether LPM (Link Power Management) support is enabled
  31957. + */
  31958. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  31959. + int32_t val);
  31960. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  31961. +#define dwc_param_lpm_enable_default 1
  31962. +
  31963. +/**
  31964. + * Specifies whether PTI enhancement is enabled
  31965. + */
  31966. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  31967. + int32_t val);
  31968. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  31969. +#define dwc_param_pti_enable_default 0
  31970. +
  31971. +/**
  31972. + * Specifies whether MPI enhancement is enabled
  31973. + */
  31974. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  31975. + int32_t val);
  31976. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  31977. +#define dwc_param_mpi_enable_default 0
  31978. +
  31979. +/**
  31980. + * Specifies whether ADP capability is enabled
  31981. + */
  31982. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  31983. + int32_t val);
  31984. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  31985. +#define dwc_param_adp_enable_default 0
  31986. +
  31987. +/**
  31988. + * Specifies whether IC_USB capability is enabled
  31989. + */
  31990. +
  31991. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  31992. + int32_t val);
  31993. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  31994. +#define dwc_param_ic_usb_cap_default 0
  31995. +
  31996. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  31997. + int32_t val);
  31998. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  31999. +#define dwc_param_ahb_thr_ratio_default 0
  32000. +
  32001. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  32002. + int32_t val);
  32003. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  32004. +#define dwc_param_power_down_default 0
  32005. +
  32006. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  32007. + int32_t val);
  32008. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  32009. +#define dwc_param_reload_ctl_default 0
  32010. +
  32011. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  32012. + int32_t val);
  32013. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  32014. +#define dwc_param_dev_out_nak_default 0
  32015. +
  32016. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  32017. + int32_t val);
  32018. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  32019. +#define dwc_param_cont_on_bna_default 0
  32020. +
  32021. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  32022. + int32_t val);
  32023. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  32024. +#define dwc_param_ahb_single_default 0
  32025. +
  32026. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  32027. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  32028. +#define dwc_param_otg_ver_default 0
  32029. +
  32030. +/** @} */
  32031. +
  32032. +/** @name Access to registers and bit-fields */
  32033. +
  32034. +/**
  32035. + * Dump core registers and SPRAM
  32036. + */
  32037. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  32038. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  32039. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  32040. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  32041. +
  32042. +/**
  32043. + * Get host negotiation status.
  32044. + */
  32045. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  32046. +
  32047. +/**
  32048. + * Get srp status
  32049. + */
  32050. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  32051. +
  32052. +/**
  32053. + * Set hnpreq bit in the GOTGCTL register.
  32054. + */
  32055. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  32056. +
  32057. +/**
  32058. + * Get Content of SNPSID register.
  32059. + */
  32060. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  32061. +
  32062. +/**
  32063. + * Get current mode.
  32064. + * Returns 0 if in device mode, and 1 if in host mode.
  32065. + */
  32066. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  32067. +
  32068. +/**
  32069. + * Get value of hnpcapable field in the GUSBCFG register
  32070. + */
  32071. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  32072. +/**
  32073. + * Set value of hnpcapable field in the GUSBCFG register
  32074. + */
  32075. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  32076. +
  32077. +/**
  32078. + * Get value of srpcapable field in the GUSBCFG register
  32079. + */
  32080. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  32081. +/**
  32082. + * Set value of srpcapable field in the GUSBCFG register
  32083. + */
  32084. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  32085. +
  32086. +/**
  32087. + * Get value of devspeed field in the DCFG register
  32088. + */
  32089. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  32090. +/**
  32091. + * Set value of devspeed field in the DCFG register
  32092. + */
  32093. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  32094. +
  32095. +/**
  32096. + * Get the value of busconnected field from the HPRT0 register
  32097. + */
  32098. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  32099. +
  32100. +/**
  32101. + * Gets the device enumeration Speed.
  32102. + */
  32103. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  32104. +
  32105. +/**
  32106. + * Get value of prtpwr field from the HPRT0 register
  32107. + */
  32108. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  32109. +
  32110. +/**
  32111. + * Get value of flag indicating core state - hibernated or not
  32112. + */
  32113. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  32114. +
  32115. +/**
  32116. + * Set value of prtpwr field from the HPRT0 register
  32117. + */
  32118. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  32119. +
  32120. +/**
  32121. + * Get value of prtsusp field from the HPRT0 regsiter
  32122. + */
  32123. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  32124. +/**
  32125. + * Set value of prtpwr field from the HPRT0 register
  32126. + */
  32127. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  32128. +
  32129. +/**
  32130. + * Get value of ModeChTimEn field from the HCFG regsiter
  32131. + */
  32132. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  32133. +/**
  32134. + * Set value of ModeChTimEn field from the HCFG regsiter
  32135. + */
  32136. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  32137. +
  32138. +/**
  32139. + * Get value of Fram Interval field from the HFIR regsiter
  32140. + */
  32141. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  32142. +/**
  32143. + * Set value of Frame Interval field from the HFIR regsiter
  32144. + */
  32145. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  32146. +
  32147. +/**
  32148. + * Set value of prtres field from the HPRT0 register
  32149. + *FIXME Remove?
  32150. + */
  32151. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  32152. +
  32153. +/**
  32154. + * Get value of rmtwkupsig bit in DCTL register
  32155. + */
  32156. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  32157. +
  32158. +/**
  32159. + * Get value of prt_sleep_sts field from the GLPMCFG register
  32160. + */
  32161. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  32162. +
  32163. +/**
  32164. + * Get value of rem_wkup_en field from the GLPMCFG register
  32165. + */
  32166. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  32167. +
  32168. +/**
  32169. + * Get value of appl_resp field from the GLPMCFG register
  32170. + */
  32171. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  32172. +/**
  32173. + * Set value of appl_resp field from the GLPMCFG register
  32174. + */
  32175. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  32176. +
  32177. +/**
  32178. + * Get value of hsic_connect field from the GLPMCFG register
  32179. + */
  32180. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  32181. +/**
  32182. + * Set value of hsic_connect field from the GLPMCFG register
  32183. + */
  32184. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  32185. +
  32186. +/**
  32187. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  32188. + */
  32189. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  32190. +/**
  32191. + * Set value of inv_sel_hsic field from the GLPMFG register.
  32192. + */
  32193. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  32194. +
  32195. +/*
  32196. + * Some functions for accessing registers
  32197. + */
  32198. +
  32199. +/**
  32200. + * GOTGCTL register
  32201. + */
  32202. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  32203. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  32204. +
  32205. +/**
  32206. + * GUSBCFG register
  32207. + */
  32208. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  32209. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  32210. +
  32211. +/**
  32212. + * GRXFSIZ register
  32213. + */
  32214. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  32215. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  32216. +
  32217. +/**
  32218. + * GNPTXFSIZ register
  32219. + */
  32220. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  32221. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  32222. +
  32223. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  32224. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  32225. +
  32226. +/**
  32227. + * GGPIO register
  32228. + */
  32229. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  32230. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  32231. +
  32232. +/**
  32233. + * GUID register
  32234. + */
  32235. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  32236. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  32237. +
  32238. +/**
  32239. + * HPRT0 register
  32240. + */
  32241. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  32242. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  32243. +
  32244. +/**
  32245. + * GHPTXFSIZE
  32246. + */
  32247. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  32248. +
  32249. +/** @} */
  32250. +
  32251. +#endif /* __DWC_CORE_IF_H__ */
  32252. --- /dev/null
  32253. +++ b/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  32254. @@ -0,0 +1,117 @@
  32255. +/* ==========================================================================
  32256. + *
  32257. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  32258. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  32259. + * otherwise expressly agreed to in writing between Synopsys and you.
  32260. + *
  32261. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  32262. + * any End User Software License Agreement or Agreement for Licensed Product
  32263. + * with Synopsys or any supplement thereto. You are permitted to use and
  32264. + * redistribute this Software in source and binary forms, with or without
  32265. + * modification, provided that redistributions of source code must retain this
  32266. + * notice. You may not view, use, disclose, copy or distribute this file or
  32267. + * any information contained herein except pursuant to this license grant from
  32268. + * Synopsys. If you do not agree with this notice, including the disclaimer
  32269. + * below, then you are not authorized to use the Software.
  32270. + *
  32271. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  32272. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32273. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32274. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  32275. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32276. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32277. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32278. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32279. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32280. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32281. + * DAMAGE.
  32282. + * ========================================================================== */
  32283. +
  32284. +#ifndef __DWC_OTG_DBG_H__
  32285. +#define __DWC_OTG_DBG_H__
  32286. +
  32287. +/** @file
  32288. + * This file defines debug levels.
  32289. + * Debugging support vanishes in non-debug builds.
  32290. + */
  32291. +
  32292. +/**
  32293. + * The Debug Level bit-mask variable.
  32294. + */
  32295. +extern uint32_t g_dbg_lvl;
  32296. +/**
  32297. + * Set the Debug Level variable.
  32298. + */
  32299. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  32300. +{
  32301. + uint32_t old = g_dbg_lvl;
  32302. + g_dbg_lvl = new;
  32303. + return old;
  32304. +}
  32305. +
  32306. +#define DBG_USER (0x1)
  32307. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  32308. +#define DBG_CIL (0x2)
  32309. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  32310. + * messages */
  32311. +#define DBG_CILV (0x20)
  32312. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  32313. + * messages */
  32314. +#define DBG_PCD (0x4)
  32315. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  32316. + * messages */
  32317. +#define DBG_PCDV (0x40)
  32318. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  32319. +#define DBG_HCD (0x8)
  32320. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  32321. + * messages */
  32322. +#define DBG_HCDV (0x80)
  32323. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  32324. + * mode. */
  32325. +#define DBG_HCD_URB (0x800)
  32326. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  32327. + * messages. */
  32328. +#define DBG_HCDI (0x1000)
  32329. +
  32330. +/** When debug level has any bit set, display debug messages */
  32331. +#define DBG_ANY (0xFF)
  32332. +
  32333. +/** All debug messages off */
  32334. +#define DBG_OFF 0
  32335. +
  32336. +/** Prefix string for DWC_DEBUG print macros. */
  32337. +#define USB_DWC "DWC_otg: "
  32338. +
  32339. +/**
  32340. + * Print a debug message when the Global debug level variable contains
  32341. + * the bit defined in <code>lvl</code>.
  32342. + *
  32343. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  32344. + * @param[in] x - like printf
  32345. + *
  32346. + * Example:<p>
  32347. + * <code>
  32348. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  32349. + * </code>
  32350. + * <br>
  32351. + * results in:<br>
  32352. + * <code>
  32353. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  32354. + * </code>
  32355. + */
  32356. +#ifdef DEBUG
  32357. +
  32358. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  32359. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  32360. +
  32361. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  32362. +
  32363. +#else
  32364. +
  32365. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  32366. +# define DWC_DEBUGP(x...)
  32367. +
  32368. +# define CHK_DEBUG_LEVEL(level) (0)
  32369. +
  32370. +#endif /*DEBUG*/
  32371. +#endif
  32372. --- /dev/null
  32373. +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  32374. @@ -0,0 +1,1757 @@
  32375. +/* ==========================================================================
  32376. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  32377. + * $Revision: #92 $
  32378. + * $Date: 2012/08/10 $
  32379. + * $Change: 2047372 $
  32380. + *
  32381. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  32382. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  32383. + * otherwise expressly agreed to in writing between Synopsys and you.
  32384. + *
  32385. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  32386. + * any End User Software License Agreement or Agreement for Licensed Product
  32387. + * with Synopsys or any supplement thereto. You are permitted to use and
  32388. + * redistribute this Software in source and binary forms, with or without
  32389. + * modification, provided that redistributions of source code must retain this
  32390. + * notice. You may not view, use, disclose, copy or distribute this file or
  32391. + * any information contained herein except pursuant to this license grant from
  32392. + * Synopsys. If you do not agree with this notice, including the disclaimer
  32393. + * below, then you are not authorized to use the Software.
  32394. + *
  32395. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  32396. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32397. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32398. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  32399. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32400. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32401. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32402. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32403. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32404. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32405. + * DAMAGE.
  32406. + * ========================================================================== */
  32407. +
  32408. +/** @file
  32409. + * The dwc_otg_driver module provides the initialization and cleanup entry
  32410. + * points for the DWC_otg driver. This module will be dynamically installed
  32411. + * after Linux is booted using the insmod command. When the module is
  32412. + * installed, the dwc_otg_driver_init function is called. When the module is
  32413. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  32414. + *
  32415. + * This module also defines a data structure for the dwc_otg_driver, which is
  32416. + * used in conjunction with the standard ARM lm_device structure. These
  32417. + * structures allow the OTG driver to comply with the standard Linux driver
  32418. + * model in which devices and drivers are registered with a bus driver. This
  32419. + * has the benefit that Linux can expose attributes of the driver and device
  32420. + * in its special sysfs file system. Users can then read or write files in
  32421. + * this file system to perform diagnostics on the driver components or the
  32422. + * device.
  32423. + */
  32424. +
  32425. +#include "dwc_otg_os_dep.h"
  32426. +#include "dwc_os.h"
  32427. +#include "dwc_otg_dbg.h"
  32428. +#include "dwc_otg_driver.h"
  32429. +#include "dwc_otg_attr.h"
  32430. +#include "dwc_otg_core_if.h"
  32431. +#include "dwc_otg_pcd_if.h"
  32432. +#include "dwc_otg_hcd_if.h"
  32433. +#include "dwc_otg_fiq_fsm.h"
  32434. +
  32435. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  32436. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  32437. +
  32438. +bool microframe_schedule=true;
  32439. +
  32440. +static const char dwc_driver_name[] = "dwc_otg";
  32441. +
  32442. +
  32443. +extern int pcd_init(
  32444. +#ifdef LM_INTERFACE
  32445. + struct lm_device *_dev
  32446. +#elif defined(PCI_INTERFACE)
  32447. + struct pci_dev *_dev
  32448. +#elif defined(PLATFORM_INTERFACE)
  32449. + struct platform_device *dev
  32450. +#endif
  32451. + );
  32452. +extern int hcd_init(
  32453. +#ifdef LM_INTERFACE
  32454. + struct lm_device *_dev
  32455. +#elif defined(PCI_INTERFACE)
  32456. + struct pci_dev *_dev
  32457. +#elif defined(PLATFORM_INTERFACE)
  32458. + struct platform_device *dev
  32459. +#endif
  32460. + );
  32461. +
  32462. +extern int pcd_remove(
  32463. +#ifdef LM_INTERFACE
  32464. + struct lm_device *_dev
  32465. +#elif defined(PCI_INTERFACE)
  32466. + struct pci_dev *_dev
  32467. +#elif defined(PLATFORM_INTERFACE)
  32468. + struct platform_device *_dev
  32469. +#endif
  32470. + );
  32471. +
  32472. +extern void hcd_remove(
  32473. +#ifdef LM_INTERFACE
  32474. + struct lm_device *_dev
  32475. +#elif defined(PCI_INTERFACE)
  32476. + struct pci_dev *_dev
  32477. +#elif defined(PLATFORM_INTERFACE)
  32478. + struct platform_device *_dev
  32479. +#endif
  32480. + );
  32481. +
  32482. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  32483. +
  32484. +/*-------------------------------------------------------------------------*/
  32485. +/* Encapsulate the module parameter settings */
  32486. +
  32487. +struct dwc_otg_driver_module_params {
  32488. + int32_t opt;
  32489. + int32_t otg_cap;
  32490. + int32_t dma_enable;
  32491. + int32_t dma_desc_enable;
  32492. + int32_t dma_burst_size;
  32493. + int32_t speed;
  32494. + int32_t host_support_fs_ls_low_power;
  32495. + int32_t host_ls_low_power_phy_clk;
  32496. + int32_t enable_dynamic_fifo;
  32497. + int32_t data_fifo_size;
  32498. + int32_t dev_rx_fifo_size;
  32499. + int32_t dev_nperio_tx_fifo_size;
  32500. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  32501. + int32_t host_rx_fifo_size;
  32502. + int32_t host_nperio_tx_fifo_size;
  32503. + int32_t host_perio_tx_fifo_size;
  32504. + int32_t max_transfer_size;
  32505. + int32_t max_packet_count;
  32506. + int32_t host_channels;
  32507. + int32_t dev_endpoints;
  32508. + int32_t phy_type;
  32509. + int32_t phy_utmi_width;
  32510. + int32_t phy_ulpi_ddr;
  32511. + int32_t phy_ulpi_ext_vbus;
  32512. + int32_t i2c_enable;
  32513. + int32_t ulpi_fs_ls;
  32514. + int32_t ts_dline;
  32515. + int32_t en_multiple_tx_fifo;
  32516. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  32517. + uint32_t thr_ctl;
  32518. + uint32_t tx_thr_length;
  32519. + uint32_t rx_thr_length;
  32520. + int32_t pti_enable;
  32521. + int32_t mpi_enable;
  32522. + int32_t lpm_enable;
  32523. + int32_t ic_usb_cap;
  32524. + int32_t ahb_thr_ratio;
  32525. + int32_t power_down;
  32526. + int32_t reload_ctl;
  32527. + int32_t dev_out_nak;
  32528. + int32_t cont_on_bna;
  32529. + int32_t ahb_single;
  32530. + int32_t otg_ver;
  32531. + int32_t adp_enable;
  32532. +};
  32533. +
  32534. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  32535. + .opt = -1,
  32536. + .otg_cap = -1,
  32537. + .dma_enable = -1,
  32538. + .dma_desc_enable = -1,
  32539. + .dma_burst_size = -1,
  32540. + .speed = -1,
  32541. + .host_support_fs_ls_low_power = -1,
  32542. + .host_ls_low_power_phy_clk = -1,
  32543. + .enable_dynamic_fifo = -1,
  32544. + .data_fifo_size = -1,
  32545. + .dev_rx_fifo_size = -1,
  32546. + .dev_nperio_tx_fifo_size = -1,
  32547. + .dev_perio_tx_fifo_size = {
  32548. + /* dev_perio_tx_fifo_size_1 */
  32549. + -1,
  32550. + -1,
  32551. + -1,
  32552. + -1,
  32553. + -1,
  32554. + -1,
  32555. + -1,
  32556. + -1,
  32557. + -1,
  32558. + -1,
  32559. + -1,
  32560. + -1,
  32561. + -1,
  32562. + -1,
  32563. + -1
  32564. + /* 15 */
  32565. + },
  32566. + .host_rx_fifo_size = -1,
  32567. + .host_nperio_tx_fifo_size = -1,
  32568. + .host_perio_tx_fifo_size = -1,
  32569. + .max_transfer_size = -1,
  32570. + .max_packet_count = -1,
  32571. + .host_channels = -1,
  32572. + .dev_endpoints = -1,
  32573. + .phy_type = -1,
  32574. + .phy_utmi_width = -1,
  32575. + .phy_ulpi_ddr = -1,
  32576. + .phy_ulpi_ext_vbus = -1,
  32577. + .i2c_enable = -1,
  32578. + .ulpi_fs_ls = -1,
  32579. + .ts_dline = -1,
  32580. + .en_multiple_tx_fifo = -1,
  32581. + .dev_tx_fifo_size = {
  32582. + /* dev_tx_fifo_size */
  32583. + -1,
  32584. + -1,
  32585. + -1,
  32586. + -1,
  32587. + -1,
  32588. + -1,
  32589. + -1,
  32590. + -1,
  32591. + -1,
  32592. + -1,
  32593. + -1,
  32594. + -1,
  32595. + -1,
  32596. + -1,
  32597. + -1
  32598. + /* 15 */
  32599. + },
  32600. + .thr_ctl = -1,
  32601. + .tx_thr_length = -1,
  32602. + .rx_thr_length = -1,
  32603. + .pti_enable = -1,
  32604. + .mpi_enable = -1,
  32605. + .lpm_enable = 0,
  32606. + .ic_usb_cap = -1,
  32607. + .ahb_thr_ratio = -1,
  32608. + .power_down = -1,
  32609. + .reload_ctl = -1,
  32610. + .dev_out_nak = -1,
  32611. + .cont_on_bna = -1,
  32612. + .ahb_single = -1,
  32613. + .otg_ver = -1,
  32614. + .adp_enable = -1,
  32615. +};
  32616. +
  32617. +//Global variable to switch the fiq fix on or off
  32618. +bool fiq_enable = 1;
  32619. +// Global variable to enable the split transaction fix
  32620. +bool fiq_fsm_enable = true;
  32621. +//Bulk split-transaction NAK holdoff in microframes
  32622. +uint16_t nak_holdoff = 8;
  32623. +
  32624. +unsigned short fiq_fsm_mask = 0x07;
  32625. +
  32626. +/**
  32627. + * This function shows the Driver Version.
  32628. + */
  32629. +static ssize_t version_show(struct device_driver *dev, char *buf)
  32630. +{
  32631. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  32632. + DWC_DRIVER_VERSION);
  32633. +}
  32634. +
  32635. +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
  32636. +
  32637. +/**
  32638. + * Global Debug Level Mask.
  32639. + */
  32640. +uint32_t g_dbg_lvl = 0; /* OFF */
  32641. +
  32642. +/**
  32643. + * This function shows the driver Debug Level.
  32644. + */
  32645. +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
  32646. +{
  32647. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  32648. +}
  32649. +
  32650. +/**
  32651. + * This function stores the driver Debug Level.
  32652. + */
  32653. +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
  32654. + size_t count)
  32655. +{
  32656. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  32657. + return count;
  32658. +}
  32659. +
  32660. +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
  32661. + dbg_level_store);
  32662. +
  32663. +/**
  32664. + * This function is called during module intialization
  32665. + * to pass module parameters to the DWC_OTG CORE.
  32666. + */
  32667. +static int set_parameters(dwc_otg_core_if_t * core_if)
  32668. +{
  32669. + int retval = 0;
  32670. + int i;
  32671. +
  32672. + if (dwc_otg_module_params.otg_cap != -1) {
  32673. + retval +=
  32674. + dwc_otg_set_param_otg_cap(core_if,
  32675. + dwc_otg_module_params.otg_cap);
  32676. + }
  32677. + if (dwc_otg_module_params.dma_enable != -1) {
  32678. + retval +=
  32679. + dwc_otg_set_param_dma_enable(core_if,
  32680. + dwc_otg_module_params.
  32681. + dma_enable);
  32682. + }
  32683. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  32684. + retval +=
  32685. + dwc_otg_set_param_dma_desc_enable(core_if,
  32686. + dwc_otg_module_params.
  32687. + dma_desc_enable);
  32688. + }
  32689. + if (dwc_otg_module_params.opt != -1) {
  32690. + retval +=
  32691. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  32692. + }
  32693. + if (dwc_otg_module_params.dma_burst_size != -1) {
  32694. + retval +=
  32695. + dwc_otg_set_param_dma_burst_size(core_if,
  32696. + dwc_otg_module_params.
  32697. + dma_burst_size);
  32698. + }
  32699. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  32700. + retval +=
  32701. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  32702. + dwc_otg_module_params.
  32703. + host_support_fs_ls_low_power);
  32704. + }
  32705. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  32706. + retval +=
  32707. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  32708. + dwc_otg_module_params.
  32709. + enable_dynamic_fifo);
  32710. + }
  32711. + if (dwc_otg_module_params.data_fifo_size != -1) {
  32712. + retval +=
  32713. + dwc_otg_set_param_data_fifo_size(core_if,
  32714. + dwc_otg_module_params.
  32715. + data_fifo_size);
  32716. + }
  32717. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  32718. + retval +=
  32719. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  32720. + dwc_otg_module_params.
  32721. + dev_rx_fifo_size);
  32722. + }
  32723. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  32724. + retval +=
  32725. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  32726. + dwc_otg_module_params.
  32727. + dev_nperio_tx_fifo_size);
  32728. + }
  32729. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  32730. + retval +=
  32731. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  32732. + dwc_otg_module_params.host_rx_fifo_size);
  32733. + }
  32734. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  32735. + retval +=
  32736. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  32737. + dwc_otg_module_params.
  32738. + host_nperio_tx_fifo_size);
  32739. + }
  32740. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  32741. + retval +=
  32742. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  32743. + dwc_otg_module_params.
  32744. + host_perio_tx_fifo_size);
  32745. + }
  32746. + if (dwc_otg_module_params.max_transfer_size != -1) {
  32747. + retval +=
  32748. + dwc_otg_set_param_max_transfer_size(core_if,
  32749. + dwc_otg_module_params.
  32750. + max_transfer_size);
  32751. + }
  32752. + if (dwc_otg_module_params.max_packet_count != -1) {
  32753. + retval +=
  32754. + dwc_otg_set_param_max_packet_count(core_if,
  32755. + dwc_otg_module_params.
  32756. + max_packet_count);
  32757. + }
  32758. + if (dwc_otg_module_params.host_channels != -1) {
  32759. + retval +=
  32760. + dwc_otg_set_param_host_channels(core_if,
  32761. + dwc_otg_module_params.
  32762. + host_channels);
  32763. + }
  32764. + if (dwc_otg_module_params.dev_endpoints != -1) {
  32765. + retval +=
  32766. + dwc_otg_set_param_dev_endpoints(core_if,
  32767. + dwc_otg_module_params.
  32768. + dev_endpoints);
  32769. + }
  32770. + if (dwc_otg_module_params.phy_type != -1) {
  32771. + retval +=
  32772. + dwc_otg_set_param_phy_type(core_if,
  32773. + dwc_otg_module_params.phy_type);
  32774. + }
  32775. + if (dwc_otg_module_params.speed != -1) {
  32776. + retval +=
  32777. + dwc_otg_set_param_speed(core_if,
  32778. + dwc_otg_module_params.speed);
  32779. + }
  32780. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  32781. + retval +=
  32782. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  32783. + dwc_otg_module_params.
  32784. + host_ls_low_power_phy_clk);
  32785. + }
  32786. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  32787. + retval +=
  32788. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  32789. + dwc_otg_module_params.
  32790. + phy_ulpi_ddr);
  32791. + }
  32792. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  32793. + retval +=
  32794. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  32795. + dwc_otg_module_params.
  32796. + phy_ulpi_ext_vbus);
  32797. + }
  32798. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  32799. + retval +=
  32800. + dwc_otg_set_param_phy_utmi_width(core_if,
  32801. + dwc_otg_module_params.
  32802. + phy_utmi_width);
  32803. + }
  32804. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  32805. + retval +=
  32806. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  32807. + dwc_otg_module_params.ulpi_fs_ls);
  32808. + }
  32809. + if (dwc_otg_module_params.ts_dline != -1) {
  32810. + retval +=
  32811. + dwc_otg_set_param_ts_dline(core_if,
  32812. + dwc_otg_module_params.ts_dline);
  32813. + }
  32814. + if (dwc_otg_module_params.i2c_enable != -1) {
  32815. + retval +=
  32816. + dwc_otg_set_param_i2c_enable(core_if,
  32817. + dwc_otg_module_params.
  32818. + i2c_enable);
  32819. + }
  32820. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  32821. + retval +=
  32822. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  32823. + dwc_otg_module_params.
  32824. + en_multiple_tx_fifo);
  32825. + }
  32826. + for (i = 0; i < 15; i++) {
  32827. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  32828. + retval +=
  32829. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  32830. + dwc_otg_module_params.
  32831. + dev_perio_tx_fifo_size
  32832. + [i], i);
  32833. + }
  32834. + }
  32835. +
  32836. + for (i = 0; i < 15; i++) {
  32837. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  32838. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  32839. + dwc_otg_module_params.
  32840. + dev_tx_fifo_size
  32841. + [i], i);
  32842. + }
  32843. + }
  32844. + if (dwc_otg_module_params.thr_ctl != -1) {
  32845. + retval +=
  32846. + dwc_otg_set_param_thr_ctl(core_if,
  32847. + dwc_otg_module_params.thr_ctl);
  32848. + }
  32849. + if (dwc_otg_module_params.mpi_enable != -1) {
  32850. + retval +=
  32851. + dwc_otg_set_param_mpi_enable(core_if,
  32852. + dwc_otg_module_params.
  32853. + mpi_enable);
  32854. + }
  32855. + if (dwc_otg_module_params.pti_enable != -1) {
  32856. + retval +=
  32857. + dwc_otg_set_param_pti_enable(core_if,
  32858. + dwc_otg_module_params.
  32859. + pti_enable);
  32860. + }
  32861. + if (dwc_otg_module_params.lpm_enable != -1) {
  32862. + retval +=
  32863. + dwc_otg_set_param_lpm_enable(core_if,
  32864. + dwc_otg_module_params.
  32865. + lpm_enable);
  32866. + }
  32867. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  32868. + retval +=
  32869. + dwc_otg_set_param_ic_usb_cap(core_if,
  32870. + dwc_otg_module_params.
  32871. + ic_usb_cap);
  32872. + }
  32873. + if (dwc_otg_module_params.tx_thr_length != -1) {
  32874. + retval +=
  32875. + dwc_otg_set_param_tx_thr_length(core_if,
  32876. + dwc_otg_module_params.tx_thr_length);
  32877. + }
  32878. + if (dwc_otg_module_params.rx_thr_length != -1) {
  32879. + retval +=
  32880. + dwc_otg_set_param_rx_thr_length(core_if,
  32881. + dwc_otg_module_params.
  32882. + rx_thr_length);
  32883. + }
  32884. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  32885. + retval +=
  32886. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  32887. + dwc_otg_module_params.ahb_thr_ratio);
  32888. + }
  32889. + if (dwc_otg_module_params.power_down != -1) {
  32890. + retval +=
  32891. + dwc_otg_set_param_power_down(core_if,
  32892. + dwc_otg_module_params.power_down);
  32893. + }
  32894. + if (dwc_otg_module_params.reload_ctl != -1) {
  32895. + retval +=
  32896. + dwc_otg_set_param_reload_ctl(core_if,
  32897. + dwc_otg_module_params.reload_ctl);
  32898. + }
  32899. +
  32900. + if (dwc_otg_module_params.dev_out_nak != -1) {
  32901. + retval +=
  32902. + dwc_otg_set_param_dev_out_nak(core_if,
  32903. + dwc_otg_module_params.dev_out_nak);
  32904. + }
  32905. +
  32906. + if (dwc_otg_module_params.cont_on_bna != -1) {
  32907. + retval +=
  32908. + dwc_otg_set_param_cont_on_bna(core_if,
  32909. + dwc_otg_module_params.cont_on_bna);
  32910. + }
  32911. +
  32912. + if (dwc_otg_module_params.ahb_single != -1) {
  32913. + retval +=
  32914. + dwc_otg_set_param_ahb_single(core_if,
  32915. + dwc_otg_module_params.ahb_single);
  32916. + }
  32917. +
  32918. + if (dwc_otg_module_params.otg_ver != -1) {
  32919. + retval +=
  32920. + dwc_otg_set_param_otg_ver(core_if,
  32921. + dwc_otg_module_params.otg_ver);
  32922. + }
  32923. + if (dwc_otg_module_params.adp_enable != -1) {
  32924. + retval +=
  32925. + dwc_otg_set_param_adp_enable(core_if,
  32926. + dwc_otg_module_params.
  32927. + adp_enable);
  32928. + }
  32929. + return retval;
  32930. +}
  32931. +
  32932. +/**
  32933. + * This function is the top level interrupt handler for the Common
  32934. + * (Device and host modes) interrupts.
  32935. + */
  32936. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  32937. +{
  32938. + int32_t retval = IRQ_NONE;
  32939. +
  32940. + retval = dwc_otg_handle_common_intr(dev);
  32941. + if (retval != 0) {
  32942. + S3C2410X_CLEAR_EINTPEND();
  32943. + }
  32944. + return IRQ_RETVAL(retval);
  32945. +}
  32946. +
  32947. +/**
  32948. + * This function is called when a lm_device is unregistered with the
  32949. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  32950. + * executed. The device may or may not be electrically present. If it is
  32951. + * present, the driver stops device processing. Any resources used on behalf
  32952. + * of this device are freed.
  32953. + *
  32954. + * @param _dev
  32955. + */
  32956. +#ifdef LM_INTERFACE
  32957. +#define REM_RETVAL(n)
  32958. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  32959. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  32960. +#elif defined(PCI_INTERFACE)
  32961. +#define REM_RETVAL(n)
  32962. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  32963. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  32964. +#elif defined(PLATFORM_INTERFACE)
  32965. +#define REM_RETVAL(n) n
  32966. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  32967. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  32968. +#endif
  32969. +
  32970. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  32971. +
  32972. + if (!otg_dev) {
  32973. + /* Memory allocation for the dwc_otg_device failed. */
  32974. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  32975. + return REM_RETVAL(-ENOMEM);
  32976. + }
  32977. +#ifndef DWC_DEVICE_ONLY
  32978. + if (otg_dev->hcd) {
  32979. + hcd_remove(_dev);
  32980. + } else {
  32981. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  32982. + return REM_RETVAL(-EINVAL);
  32983. + }
  32984. +#endif
  32985. +
  32986. +#ifndef DWC_HOST_ONLY
  32987. + if (otg_dev->pcd) {
  32988. + pcd_remove(_dev);
  32989. + } else {
  32990. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  32991. + return REM_RETVAL(-EINVAL);
  32992. + }
  32993. +#endif
  32994. + /*
  32995. + * Free the IRQ
  32996. + */
  32997. + if (otg_dev->common_irq_installed) {
  32998. +#ifdef PLATFORM_INTERFACE
  32999. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  33000. +#else
  33001. + free_irq(_dev->irq, otg_dev);
  33002. +#endif
  33003. + } else {
  33004. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  33005. + return REM_RETVAL(-ENXIO);
  33006. + }
  33007. +
  33008. + if (otg_dev->core_if) {
  33009. + dwc_otg_cil_remove(otg_dev->core_if);
  33010. + } else {
  33011. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  33012. + return REM_RETVAL(-ENXIO);
  33013. + }
  33014. +
  33015. + /*
  33016. + * Remove the device attributes
  33017. + */
  33018. + dwc_otg_attr_remove(_dev);
  33019. +
  33020. + /*
  33021. + * Return the memory.
  33022. + */
  33023. + if (otg_dev->os_dep.base) {
  33024. + iounmap(otg_dev->os_dep.base);
  33025. + }
  33026. + DWC_FREE(otg_dev);
  33027. +
  33028. + /*
  33029. + * Clear the drvdata pointer.
  33030. + */
  33031. +#ifdef LM_INTERFACE
  33032. + lm_set_drvdata(_dev, 0);
  33033. +#elif defined(PCI_INTERFACE)
  33034. + release_mem_region(otg_dev->os_dep.rsrc_start,
  33035. + otg_dev->os_dep.rsrc_len);
  33036. + pci_set_drvdata(_dev, 0);
  33037. +#elif defined(PLATFORM_INTERFACE)
  33038. + platform_set_drvdata(_dev, 0);
  33039. +#endif
  33040. + return REM_RETVAL(0);
  33041. +}
  33042. +
  33043. +/**
  33044. + * This function is called when an lm_device is bound to a
  33045. + * dwc_otg_driver. It creates the driver components required to
  33046. + * control the device (CIL, HCD, and PCD) and it initializes the
  33047. + * device. The driver components are stored in a dwc_otg_device
  33048. + * structure. A reference to the dwc_otg_device is saved in the
  33049. + * lm_device. This allows the driver to access the dwc_otg_device
  33050. + * structure on subsequent calls to driver methods for this device.
  33051. + *
  33052. + * @param _dev Bus device
  33053. + */
  33054. +static int dwc_otg_driver_probe(
  33055. +#ifdef LM_INTERFACE
  33056. + struct lm_device *_dev
  33057. +#elif defined(PCI_INTERFACE)
  33058. + struct pci_dev *_dev,
  33059. + const struct pci_device_id *id
  33060. +#elif defined(PLATFORM_INTERFACE)
  33061. + struct platform_device *_dev
  33062. +#endif
  33063. + )
  33064. +{
  33065. + int retval = 0;
  33066. + dwc_otg_device_t *dwc_otg_device;
  33067. + int devirq;
  33068. +
  33069. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  33070. +#ifdef LM_INTERFACE
  33071. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  33072. +#elif defined(PCI_INTERFACE)
  33073. + if (!id) {
  33074. + DWC_ERROR("Invalid pci_device_id %p", id);
  33075. + return -EINVAL;
  33076. + }
  33077. +
  33078. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  33079. + DWC_ERROR("Invalid pci_device %p", _dev);
  33080. + return -ENODEV;
  33081. + }
  33082. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  33083. + /* other stuff needed as well? */
  33084. +
  33085. +#elif defined(PLATFORM_INTERFACE)
  33086. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  33087. + (unsigned)_dev->resource->start,
  33088. + (unsigned)(_dev->resource->end - _dev->resource->start));
  33089. +#endif
  33090. +
  33091. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  33092. +
  33093. + if (!dwc_otg_device) {
  33094. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  33095. + return -ENOMEM;
  33096. + }
  33097. +
  33098. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  33099. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  33100. + dwc_otg_device->os_dep.platformdev = _dev;
  33101. +
  33102. + /*
  33103. + * Map the DWC_otg Core memory into virtual address space.
  33104. + */
  33105. +#ifdef LM_INTERFACE
  33106. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  33107. +
  33108. + if (!dwc_otg_device->os_dep.base) {
  33109. + dev_err(&_dev->dev, "ioremap() failed\n");
  33110. + DWC_FREE(dwc_otg_device);
  33111. + return -ENOMEM;
  33112. + }
  33113. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  33114. + (unsigned)dwc_otg_device->os_dep.base);
  33115. +#elif defined(PCI_INTERFACE)
  33116. + _dev->current_state = PCI_D0;
  33117. + _dev->dev.power.power_state = PMSG_ON;
  33118. +
  33119. + if (!_dev->irq) {
  33120. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  33121. + pci_name(_dev));
  33122. + iounmap(dwc_otg_device->os_dep.base);
  33123. + DWC_FREE(dwc_otg_device);
  33124. + return -ENODEV;
  33125. + }
  33126. +
  33127. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  33128. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  33129. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  33130. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  33131. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  33132. + if (!request_mem_region
  33133. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  33134. + "dwc_otg")) {
  33135. + dev_dbg(&_dev->dev, "error requesting memory\n");
  33136. + iounmap(dwc_otg_device->os_dep.base);
  33137. + DWC_FREE(dwc_otg_device);
  33138. + return -EFAULT;
  33139. + }
  33140. +
  33141. + dwc_otg_device->os_dep.base =
  33142. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  33143. + dwc_otg_device->os_dep.rsrc_len);
  33144. + if (dwc_otg_device->os_dep.base == NULL) {
  33145. + dev_dbg(&_dev->dev, "error mapping memory\n");
  33146. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  33147. + dwc_otg_device->os_dep.rsrc_len);
  33148. + iounmap(dwc_otg_device->os_dep.base);
  33149. + DWC_FREE(dwc_otg_device);
  33150. + return -EFAULT;
  33151. + }
  33152. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  33153. + dwc_otg_device->os_dep.base);
  33154. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  33155. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  33156. + dwc_otg_device->os_dep.base);
  33157. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  33158. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  33159. + dwc_otg_device->os_dep.base);
  33160. +
  33161. + pci_set_master(_dev);
  33162. + pci_set_drvdata(_dev, dwc_otg_device);
  33163. +#elif defined(PLATFORM_INTERFACE)
  33164. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  33165. + _dev->resource->start,
  33166. + _dev->resource->end - _dev->resource->start + 1);
  33167. +#if 1
  33168. + if (!request_mem_region(_dev->resource[0].start,
  33169. + _dev->resource[0].end - _dev->resource[0].start + 1,
  33170. + "dwc_otg")) {
  33171. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  33172. + retval = -EFAULT;
  33173. + goto fail;
  33174. + }
  33175. +
  33176. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  33177. + _dev->resource[0].end -
  33178. + _dev->resource[0].start+1);
  33179. + if (fiq_enable)
  33180. + {
  33181. + if (!request_mem_region(_dev->resource[1].start,
  33182. + _dev->resource[1].end - _dev->resource[1].start + 1,
  33183. + "dwc_otg")) {
  33184. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  33185. + retval = -EFAULT;
  33186. + goto fail;
  33187. + }
  33188. +
  33189. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  33190. + _dev->resource[1].end -
  33191. + _dev->resource[1].start + 1);
  33192. + }
  33193. +
  33194. +#else
  33195. + {
  33196. + struct map_desc desc = {
  33197. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  33198. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  33199. + .length = SZ_128K,
  33200. + .type = MT_DEVICE
  33201. + };
  33202. + iotable_init(&desc, 1);
  33203. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  33204. + }
  33205. +#endif
  33206. + if (!dwc_otg_device->os_dep.base) {
  33207. + dev_err(&_dev->dev, "ioremap() failed\n");
  33208. + retval = -ENOMEM;
  33209. + goto fail;
  33210. + }
  33211. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  33212. + (unsigned)dwc_otg_device->os_dep.base);
  33213. +#endif
  33214. +
  33215. + /*
  33216. + * Initialize driver data to point to the global DWC_otg
  33217. + * Device structure.
  33218. + */
  33219. +#ifdef LM_INTERFACE
  33220. + lm_set_drvdata(_dev, dwc_otg_device);
  33221. +#elif defined(PLATFORM_INTERFACE)
  33222. + platform_set_drvdata(_dev, dwc_otg_device);
  33223. +#endif
  33224. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  33225. +
  33226. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  33227. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  33228. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  33229. +
  33230. + if (!dwc_otg_device->core_if) {
  33231. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  33232. + retval = -ENOMEM;
  33233. + goto fail;
  33234. + }
  33235. +
  33236. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  33237. + /*
  33238. + * Attempt to ensure this device is really a DWC_otg Controller.
  33239. + * Read and verify the SNPSID register contents. The value should be
  33240. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  33241. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  33242. + */
  33243. +
  33244. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  33245. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  33246. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  33247. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  33248. + retval = -EINVAL;
  33249. + goto fail;
  33250. + }
  33251. +
  33252. + /*
  33253. + * Validate parameter values.
  33254. + */
  33255. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  33256. + if (set_parameters(dwc_otg_device->core_if)) {
  33257. + retval = -EINVAL;
  33258. + goto fail;
  33259. + }
  33260. +
  33261. + /*
  33262. + * Create Device Attributes in sysfs
  33263. + */
  33264. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  33265. + dwc_otg_attr_create(_dev);
  33266. +
  33267. + /*
  33268. + * Disable the global interrupt until all the interrupt
  33269. + * handlers are installed.
  33270. + */
  33271. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  33272. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  33273. +
  33274. + /*
  33275. + * Install the interrupt handler for the common interrupts before
  33276. + * enabling common interrupts in core_init below.
  33277. + */
  33278. +
  33279. +#if defined(PLATFORM_INTERFACE)
  33280. + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
  33281. +#else
  33282. + devirq = _dev->irq;
  33283. +#endif
  33284. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  33285. + devirq);
  33286. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  33287. + retval = request_irq(devirq, dwc_otg_common_irq,
  33288. + IRQF_SHARED,
  33289. + "dwc_otg", dwc_otg_device);
  33290. + if (retval) {
  33291. + DWC_ERROR("request of irq%d failed\n", devirq);
  33292. + retval = -EBUSY;
  33293. + goto fail;
  33294. + } else {
  33295. + dwc_otg_device->common_irq_installed = 1;
  33296. + }
  33297. +
  33298. +#ifndef IRQF_TRIGGER_LOW
  33299. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  33300. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  33301. + set_irq_type(devirq,
  33302. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  33303. + IRQT_LOW
  33304. +#else
  33305. + IRQ_TYPE_LEVEL_LOW
  33306. +#endif
  33307. + );
  33308. +#endif
  33309. +#endif /*IRQF_TRIGGER_LOW*/
  33310. +
  33311. + /*
  33312. + * Initialize the DWC_otg core.
  33313. + */
  33314. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  33315. + dwc_otg_core_init(dwc_otg_device->core_if);
  33316. +
  33317. +#ifndef DWC_HOST_ONLY
  33318. + /*
  33319. + * Initialize the PCD
  33320. + */
  33321. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  33322. + retval = pcd_init(_dev);
  33323. + if (retval != 0) {
  33324. + DWC_ERROR("pcd_init failed\n");
  33325. + dwc_otg_device->pcd = NULL;
  33326. + goto fail;
  33327. + }
  33328. +#endif
  33329. +#ifndef DWC_DEVICE_ONLY
  33330. + /*
  33331. + * Initialize the HCD
  33332. + */
  33333. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  33334. + retval = hcd_init(_dev);
  33335. + if (retval != 0) {
  33336. + DWC_ERROR("hcd_init failed\n");
  33337. + dwc_otg_device->hcd = NULL;
  33338. + goto fail;
  33339. + }
  33340. +#endif
  33341. + /* Recover from drvdata having been overwritten by hcd_init() */
  33342. +#ifdef LM_INTERFACE
  33343. + lm_set_drvdata(_dev, dwc_otg_device);
  33344. +#elif defined(PLATFORM_INTERFACE)
  33345. + platform_set_drvdata(_dev, dwc_otg_device);
  33346. +#elif defined(PCI_INTERFACE)
  33347. + pci_set_drvdata(_dev, dwc_otg_device);
  33348. + dwc_otg_device->os_dep.pcidev = _dev;
  33349. +#endif
  33350. +
  33351. + /*
  33352. + * Enable the global interrupt after all the interrupt
  33353. + * handlers are installed if there is no ADP support else
  33354. + * perform initial actions required for Internal ADP logic.
  33355. + */
  33356. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  33357. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  33358. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  33359. + dev_dbg(&_dev->dev, "Done\n");
  33360. + } else
  33361. + dwc_otg_adp_start(dwc_otg_device->core_if,
  33362. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  33363. +
  33364. + return 0;
  33365. +
  33366. +fail:
  33367. + dwc_otg_driver_remove(_dev);
  33368. + return retval;
  33369. +}
  33370. +
  33371. +/**
  33372. + * This structure defines the methods to be called by a bus driver
  33373. + * during the lifecycle of a device on that bus. Both drivers and
  33374. + * devices are registered with a bus driver. The bus driver matches
  33375. + * devices to drivers based on information in the device and driver
  33376. + * structures.
  33377. + *
  33378. + * The probe function is called when the bus driver matches a device
  33379. + * to this driver. The remove function is called when a device is
  33380. + * unregistered with the bus driver.
  33381. + */
  33382. +#ifdef LM_INTERFACE
  33383. +static struct lm_driver dwc_otg_driver = {
  33384. + .drv = {.name = (char *)dwc_driver_name,},
  33385. + .probe = dwc_otg_driver_probe,
  33386. + .remove = dwc_otg_driver_remove,
  33387. + // 'suspend' and 'resume' absent
  33388. +};
  33389. +#elif defined(PCI_INTERFACE)
  33390. +static const struct pci_device_id pci_ids[] = { {
  33391. + PCI_DEVICE(0x16c3, 0xabcd),
  33392. + .driver_data =
  33393. + (unsigned long)0xdeadbeef,
  33394. + }, { /* end: all zeroes */ }
  33395. +};
  33396. +
  33397. +MODULE_DEVICE_TABLE(pci, pci_ids);
  33398. +
  33399. +/* pci driver glue; this is a "new style" PCI driver module */
  33400. +static struct pci_driver dwc_otg_driver = {
  33401. + .name = "dwc_otg",
  33402. + .id_table = pci_ids,
  33403. +
  33404. + .probe = dwc_otg_driver_probe,
  33405. + .remove = dwc_otg_driver_remove,
  33406. +
  33407. + .driver = {
  33408. + .name = (char *)dwc_driver_name,
  33409. + },
  33410. +};
  33411. +#elif defined(PLATFORM_INTERFACE)
  33412. +static struct platform_device_id platform_ids[] = {
  33413. + {
  33414. + .name = "bcm2708_usb",
  33415. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  33416. + },
  33417. + { /* end: all zeroes */ }
  33418. +};
  33419. +MODULE_DEVICE_TABLE(platform, platform_ids);
  33420. +
  33421. +static const struct of_device_id dwc_otg_of_match_table[] = {
  33422. + { .compatible = "brcm,bcm2708-usb", },
  33423. + {},
  33424. +};
  33425. +MODULE_DEVICE_TABLE(of, dwc_otg_of_match_table);
  33426. +
  33427. +static struct platform_driver dwc_otg_driver = {
  33428. + .driver = {
  33429. + .name = (char *)dwc_driver_name,
  33430. + .of_match_table = dwc_otg_of_match_table,
  33431. + },
  33432. + .id_table = platform_ids,
  33433. +
  33434. + .probe = dwc_otg_driver_probe,
  33435. + .remove = dwc_otg_driver_remove,
  33436. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  33437. +};
  33438. +#endif
  33439. +
  33440. +/**
  33441. + * This function is called when the dwc_otg_driver is installed with the
  33442. + * insmod command. It registers the dwc_otg_driver structure with the
  33443. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  33444. + * to be called. In addition, the bus driver will automatically expose
  33445. + * attributes defined for the device and driver in the special sysfs file
  33446. + * system.
  33447. + *
  33448. + * @return
  33449. + */
  33450. +static int __init dwc_otg_driver_init(void)
  33451. +{
  33452. + int retval = 0;
  33453. + int error;
  33454. + struct device_driver *drv;
  33455. +
  33456. + if(fiq_fsm_enable && !fiq_enable) {
  33457. + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
  33458. + fiq_enable = 1;
  33459. + }
  33460. +
  33461. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  33462. + DWC_DRIVER_VERSION,
  33463. +#ifdef LM_INTERFACE
  33464. + "logicmodule");
  33465. + retval = lm_driver_register(&dwc_otg_driver);
  33466. + drv = &dwc_otg_driver.drv;
  33467. +#elif defined(PCI_INTERFACE)
  33468. + "pci");
  33469. + retval = pci_register_driver(&dwc_otg_driver);
  33470. + drv = &dwc_otg_driver.driver;
  33471. +#elif defined(PLATFORM_INTERFACE)
  33472. + "platform");
  33473. + retval = platform_driver_register(&dwc_otg_driver);
  33474. + drv = &dwc_otg_driver.driver;
  33475. +#endif
  33476. + if (retval < 0) {
  33477. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  33478. + return retval;
  33479. + }
  33480. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
  33481. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
  33482. + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
  33483. +
  33484. + error = driver_create_file(drv, &driver_attr_version);
  33485. +#ifdef DEBUG
  33486. + error = driver_create_file(drv, &driver_attr_debuglevel);
  33487. +#endif
  33488. + return retval;
  33489. +}
  33490. +
  33491. +module_init(dwc_otg_driver_init);
  33492. +
  33493. +/**
  33494. + * This function is called when the driver is removed from the kernel
  33495. + * with the rmmod command. The driver unregisters itself with its bus
  33496. + * driver.
  33497. + *
  33498. + */
  33499. +static void __exit dwc_otg_driver_cleanup(void)
  33500. +{
  33501. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  33502. +
  33503. +#ifdef LM_INTERFACE
  33504. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  33505. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  33506. + lm_driver_unregister(&dwc_otg_driver);
  33507. +#elif defined(PCI_INTERFACE)
  33508. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  33509. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  33510. + pci_unregister_driver(&dwc_otg_driver);
  33511. +#elif defined(PLATFORM_INTERFACE)
  33512. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  33513. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  33514. + platform_driver_unregister(&dwc_otg_driver);
  33515. +#endif
  33516. +
  33517. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  33518. +}
  33519. +
  33520. +module_exit(dwc_otg_driver_cleanup);
  33521. +
  33522. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  33523. +MODULE_AUTHOR("Synopsys Inc.");
  33524. +MODULE_LICENSE("GPL");
  33525. +
  33526. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  33527. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  33528. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  33529. +MODULE_PARM_DESC(opt, "OPT Mode");
  33530. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  33531. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  33532. +
  33533. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  33534. + 0444);
  33535. +MODULE_PARM_DESC(dma_desc_enable,
  33536. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  33537. +
  33538. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  33539. + 0444);
  33540. +MODULE_PARM_DESC(dma_burst_size,
  33541. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  33542. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  33543. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  33544. +module_param_named(host_support_fs_ls_low_power,
  33545. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  33546. + 0444);
  33547. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  33548. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  33549. +module_param_named(host_ls_low_power_phy_clk,
  33550. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  33551. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  33552. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  33553. +module_param_named(enable_dynamic_fifo,
  33554. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  33555. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  33556. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  33557. + 0444);
  33558. +MODULE_PARM_DESC(data_fifo_size,
  33559. + "Total number of words in the data FIFO memory 32-32768");
  33560. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  33561. + int, 0444);
  33562. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  33563. +module_param_named(dev_nperio_tx_fifo_size,
  33564. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  33565. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  33566. + "Number of words in the non-periodic Tx FIFO 16-32768");
  33567. +module_param_named(dev_perio_tx_fifo_size_1,
  33568. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  33569. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  33570. + "Number of words in the periodic Tx FIFO 4-768");
  33571. +module_param_named(dev_perio_tx_fifo_size_2,
  33572. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  33573. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  33574. + "Number of words in the periodic Tx FIFO 4-768");
  33575. +module_param_named(dev_perio_tx_fifo_size_3,
  33576. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  33577. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  33578. + "Number of words in the periodic Tx FIFO 4-768");
  33579. +module_param_named(dev_perio_tx_fifo_size_4,
  33580. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  33581. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  33582. + "Number of words in the periodic Tx FIFO 4-768");
  33583. +module_param_named(dev_perio_tx_fifo_size_5,
  33584. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  33585. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  33586. + "Number of words in the periodic Tx FIFO 4-768");
  33587. +module_param_named(dev_perio_tx_fifo_size_6,
  33588. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  33589. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  33590. + "Number of words in the periodic Tx FIFO 4-768");
  33591. +module_param_named(dev_perio_tx_fifo_size_7,
  33592. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  33593. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  33594. + "Number of words in the periodic Tx FIFO 4-768");
  33595. +module_param_named(dev_perio_tx_fifo_size_8,
  33596. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  33597. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  33598. + "Number of words in the periodic Tx FIFO 4-768");
  33599. +module_param_named(dev_perio_tx_fifo_size_9,
  33600. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  33601. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  33602. + "Number of words in the periodic Tx FIFO 4-768");
  33603. +module_param_named(dev_perio_tx_fifo_size_10,
  33604. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  33605. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  33606. + "Number of words in the periodic Tx FIFO 4-768");
  33607. +module_param_named(dev_perio_tx_fifo_size_11,
  33608. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  33609. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  33610. + "Number of words in the periodic Tx FIFO 4-768");
  33611. +module_param_named(dev_perio_tx_fifo_size_12,
  33612. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  33613. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  33614. + "Number of words in the periodic Tx FIFO 4-768");
  33615. +module_param_named(dev_perio_tx_fifo_size_13,
  33616. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  33617. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  33618. + "Number of words in the periodic Tx FIFO 4-768");
  33619. +module_param_named(dev_perio_tx_fifo_size_14,
  33620. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  33621. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  33622. + "Number of words in the periodic Tx FIFO 4-768");
  33623. +module_param_named(dev_perio_tx_fifo_size_15,
  33624. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  33625. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  33626. + "Number of words in the periodic Tx FIFO 4-768");
  33627. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  33628. + int, 0444);
  33629. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  33630. +module_param_named(host_nperio_tx_fifo_size,
  33631. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  33632. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  33633. + "Number of words in the non-periodic Tx FIFO 16-32768");
  33634. +module_param_named(host_perio_tx_fifo_size,
  33635. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  33636. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  33637. + "Number of words in the host periodic Tx FIFO 16-32768");
  33638. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  33639. + int, 0444);
  33640. +/** @todo Set the max to 512K, modify checks */
  33641. +MODULE_PARM_DESC(max_transfer_size,
  33642. + "The maximum transfer size supported in bytes 2047-65535");
  33643. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  33644. + int, 0444);
  33645. +MODULE_PARM_DESC(max_packet_count,
  33646. + "The maximum number of packets in a transfer 15-511");
  33647. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  33648. + 0444);
  33649. +MODULE_PARM_DESC(host_channels,
  33650. + "The number of host channel registers to use 1-16");
  33651. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  33652. + 0444);
  33653. +MODULE_PARM_DESC(dev_endpoints,
  33654. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  33655. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  33656. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  33657. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  33658. + 0444);
  33659. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  33660. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  33661. +MODULE_PARM_DESC(phy_ulpi_ddr,
  33662. + "ULPI at double or single data rate 0=Single 1=Double");
  33663. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  33664. + int, 0444);
  33665. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  33666. + "ULPI PHY using internal or external vbus 0=Internal");
  33667. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  33668. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  33669. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  33670. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  33671. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  33672. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  33673. +module_param_named(debug, g_dbg_lvl, int, 0444);
  33674. +MODULE_PARM_DESC(debug, "");
  33675. +
  33676. +module_param_named(en_multiple_tx_fifo,
  33677. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  33678. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  33679. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  33680. +module_param_named(dev_tx_fifo_size_1,
  33681. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  33682. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  33683. +module_param_named(dev_tx_fifo_size_2,
  33684. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  33685. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  33686. +module_param_named(dev_tx_fifo_size_3,
  33687. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  33688. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  33689. +module_param_named(dev_tx_fifo_size_4,
  33690. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  33691. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  33692. +module_param_named(dev_tx_fifo_size_5,
  33693. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  33694. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  33695. +module_param_named(dev_tx_fifo_size_6,
  33696. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  33697. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  33698. +module_param_named(dev_tx_fifo_size_7,
  33699. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  33700. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  33701. +module_param_named(dev_tx_fifo_size_8,
  33702. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  33703. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  33704. +module_param_named(dev_tx_fifo_size_9,
  33705. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  33706. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  33707. +module_param_named(dev_tx_fifo_size_10,
  33708. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  33709. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  33710. +module_param_named(dev_tx_fifo_size_11,
  33711. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  33712. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  33713. +module_param_named(dev_tx_fifo_size_12,
  33714. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  33715. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  33716. +module_param_named(dev_tx_fifo_size_13,
  33717. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  33718. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  33719. +module_param_named(dev_tx_fifo_size_14,
  33720. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  33721. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  33722. +module_param_named(dev_tx_fifo_size_15,
  33723. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  33724. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  33725. +
  33726. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  33727. +MODULE_PARM_DESC(thr_ctl,
  33728. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  33729. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  33730. + 0444);
  33731. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  33732. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  33733. + 0444);
  33734. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  33735. +
  33736. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  33737. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  33738. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  33739. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  33740. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  33741. +MODULE_PARM_DESC(ic_usb_cap,
  33742. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  33743. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  33744. + 0444);
  33745. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  33746. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  33747. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  33748. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  33749. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  33750. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  33751. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  33752. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  33753. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  33754. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  33755. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  33756. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  33757. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  33758. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  33759. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  33760. +module_param(microframe_schedule, bool, 0444);
  33761. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  33762. +
  33763. +module_param(fiq_enable, bool, 0444);
  33764. +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
  33765. +module_param(nak_holdoff, ushort, 0644);
  33766. +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
  33767. +module_param(fiq_fsm_enable, bool, 0444);
  33768. +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
  33769. +module_param(fiq_fsm_mask, ushort, 0444);
  33770. +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
  33771. + "Bit 0 : Non-periodic split transactions\n"
  33772. + "Bit 1 : Periodic split transactions\n"
  33773. + "Bit 2 : High-speed multi-transfer isochronous\n"
  33774. + "All other bits should be set 0.");
  33775. +
  33776. +
  33777. +/** @page "Module Parameters"
  33778. + *
  33779. + * The following parameters may be specified when starting the module.
  33780. + * These parameters define how the DWC_otg controller should be
  33781. + * configured. Parameter values are passed to the CIL initialization
  33782. + * function dwc_otg_cil_init
  33783. + *
  33784. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  33785. + *
  33786. +
  33787. + <table>
  33788. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  33789. +
  33790. + <tr>
  33791. + <td>otg_cap</td>
  33792. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  33793. + value for this parameter if none is specified.
  33794. + - 0: HNP and SRP capable (default, if available)
  33795. + - 1: SRP Only capable
  33796. + - 2: No HNP/SRP capable
  33797. + </td></tr>
  33798. +
  33799. + <tr>
  33800. + <td>dma_enable</td>
  33801. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  33802. + The driver will automatically detect the value for this parameter if none is
  33803. + specified.
  33804. + - 0: Slave
  33805. + - 1: DMA (default, if available)
  33806. + </td></tr>
  33807. +
  33808. + <tr>
  33809. + <td>dma_burst_size</td>
  33810. + <td>The DMA Burst size (applicable only for External DMA Mode).
  33811. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  33812. + </td></tr>
  33813. +
  33814. + <tr>
  33815. + <td>speed</td>
  33816. + <td>Specifies the maximum speed of operation in host and device mode. The
  33817. + actual speed depends on the speed of the attached device and the value of
  33818. + phy_type.
  33819. + - 0: High Speed (default)
  33820. + - 1: Full Speed
  33821. + </td></tr>
  33822. +
  33823. + <tr>
  33824. + <td>host_support_fs_ls_low_power</td>
  33825. + <td>Specifies whether low power mode is supported when attached to a Full
  33826. + Speed or Low Speed device in host mode.
  33827. + - 0: Don't support low power mode (default)
  33828. + - 1: Support low power mode
  33829. + </td></tr>
  33830. +
  33831. + <tr>
  33832. + <td>host_ls_low_power_phy_clk</td>
  33833. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  33834. + Speed device in host mode. This parameter is applicable only if
  33835. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  33836. + - 0: 48 MHz (default)
  33837. + - 1: 6 MHz
  33838. + </td></tr>
  33839. +
  33840. + <tr>
  33841. + <td>enable_dynamic_fifo</td>
  33842. + <td> Specifies whether FIFOs may be resized by the driver software.
  33843. + - 0: Use cC FIFO size parameters
  33844. + - 1: Allow dynamic FIFO sizing (default)
  33845. + </td></tr>
  33846. +
  33847. + <tr>
  33848. + <td>data_fifo_size</td>
  33849. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  33850. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  33851. + - Values: 32 to 32768 (default 8192)
  33852. +
  33853. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  33854. + </td></tr>
  33855. +
  33856. + <tr>
  33857. + <td>dev_rx_fifo_size</td>
  33858. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  33859. + FIFO sizing is enabled.
  33860. + - Values: 16 to 32768 (default 1064)
  33861. + </td></tr>
  33862. +
  33863. + <tr>
  33864. + <td>dev_nperio_tx_fifo_size</td>
  33865. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  33866. + dynamic FIFO sizing is enabled.
  33867. + - Values: 16 to 32768 (default 1024)
  33868. + </td></tr>
  33869. +
  33870. + <tr>
  33871. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  33872. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  33873. + when dynamic FIFO sizing is enabled.
  33874. + - Values: 4 to 768 (default 256)
  33875. + </td></tr>
  33876. +
  33877. + <tr>
  33878. + <td>host_rx_fifo_size</td>
  33879. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  33880. + sizing is enabled.
  33881. + - Values: 16 to 32768 (default 1024)
  33882. + </td></tr>
  33883. +
  33884. + <tr>
  33885. + <td>host_nperio_tx_fifo_size</td>
  33886. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  33887. + dynamic FIFO sizing is enabled in the core.
  33888. + - Values: 16 to 32768 (default 1024)
  33889. + </td></tr>
  33890. +
  33891. + <tr>
  33892. + <td>host_perio_tx_fifo_size</td>
  33893. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  33894. + sizing is enabled.
  33895. + - Values: 16 to 32768 (default 1024)
  33896. + </td></tr>
  33897. +
  33898. + <tr>
  33899. + <td>max_transfer_size</td>
  33900. + <td>The maximum transfer size supported in bytes.
  33901. + - Values: 2047 to 65,535 (default 65,535)
  33902. + </td></tr>
  33903. +
  33904. + <tr>
  33905. + <td>max_packet_count</td>
  33906. + <td>The maximum number of packets in a transfer.
  33907. + - Values: 15 to 511 (default 511)
  33908. + </td></tr>
  33909. +
  33910. + <tr>
  33911. + <td>host_channels</td>
  33912. + <td>The number of host channel registers to use.
  33913. + - Values: 1 to 16 (default 12)
  33914. +
  33915. + Note: The FPGA configuration supports a maximum of 12 host channels.
  33916. + </td></tr>
  33917. +
  33918. + <tr>
  33919. + <td>dev_endpoints</td>
  33920. + <td>The number of endpoints in addition to EP0 available for device mode
  33921. + operations.
  33922. + - Values: 1 to 15 (default 6 IN and OUT)
  33923. +
  33924. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  33925. + addition to EP0.
  33926. + </td></tr>
  33927. +
  33928. + <tr>
  33929. + <td>phy_type</td>
  33930. + <td>Specifies the type of PHY interface to use. By default, the driver will
  33931. + automatically detect the phy_type.
  33932. + - 0: Full Speed
  33933. + - 1: UTMI+ (default, if available)
  33934. + - 2: ULPI
  33935. + </td></tr>
  33936. +
  33937. + <tr>
  33938. + <td>phy_utmi_width</td>
  33939. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  33940. + phy_type of UTMI+. Also, this parameter is applicable only if the
  33941. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  33942. + core has been configured to work at either data path width.
  33943. + - Values: 8 or 16 bits (default 16)
  33944. + </td></tr>
  33945. +
  33946. + <tr>
  33947. + <td>phy_ulpi_ddr</td>
  33948. + <td>Specifies whether the ULPI operates at double or single data rate. This
  33949. + parameter is only applicable if phy_type is ULPI.
  33950. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  33951. + - 1: double data rate ULPI interface with 4 bit wide data bus
  33952. + </td></tr>
  33953. +
  33954. + <tr>
  33955. + <td>i2c_enable</td>
  33956. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  33957. + parameter is only applicable if PHY_TYPE is FS.
  33958. + - 0: Disabled (default)
  33959. + - 1: Enabled
  33960. + </td></tr>
  33961. +
  33962. + <tr>
  33963. + <td>ulpi_fs_ls</td>
  33964. + <td>Specifies whether to use ULPI FS/LS mode only.
  33965. + - 0: Disabled (default)
  33966. + - 1: Enabled
  33967. + </td></tr>
  33968. +
  33969. + <tr>
  33970. + <td>ts_dline</td>
  33971. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  33972. + - 0: Disabled (default)
  33973. + - 1: Enabled
  33974. + </td></tr>
  33975. +
  33976. + <tr>
  33977. + <td>en_multiple_tx_fifo</td>
  33978. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  33979. + The driver will automatically detect the value for this parameter if none is
  33980. + specified.
  33981. + - 0: Disabled
  33982. + - 1: Enabled (default, if available)
  33983. + </td></tr>
  33984. +
  33985. + <tr>
  33986. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  33987. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  33988. + when dynamic FIFO sizing is enabled.
  33989. + - Values: 4 to 768 (default 256)
  33990. + </td></tr>
  33991. +
  33992. + <tr>
  33993. + <td>tx_thr_length</td>
  33994. + <td>Transmit Threshold length in 32 bit double words
  33995. + - Values: 8 to 128 (default 64)
  33996. + </td></tr>
  33997. +
  33998. + <tr>
  33999. + <td>rx_thr_length</td>
  34000. + <td>Receive Threshold length in 32 bit double words
  34001. + - Values: 8 to 128 (default 64)
  34002. + </td></tr>
  34003. +
  34004. +<tr>
  34005. + <td>thr_ctl</td>
  34006. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  34007. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  34008. + Rx transfers accordingly.
  34009. + The driver will automatically detect the value for this parameter if none is
  34010. + specified.
  34011. + - Values: 0 to 7 (default 0)
  34012. + Bit values indicate:
  34013. + - 0: Thresholding disabled
  34014. + - 1: Thresholding enabled
  34015. + </td></tr>
  34016. +
  34017. +<tr>
  34018. + <td>dma_desc_enable</td>
  34019. + <td>Specifies whether to enable Descriptor DMA mode.
  34020. + The driver will automatically detect the value for this parameter if none is
  34021. + specified.
  34022. + - 0: Descriptor DMA disabled
  34023. + - 1: Descriptor DMA (default, if available)
  34024. + </td></tr>
  34025. +
  34026. +<tr>
  34027. + <td>mpi_enable</td>
  34028. + <td>Specifies whether to enable MPI enhancement mode.
  34029. + The driver will automatically detect the value for this parameter if none is
  34030. + specified.
  34031. + - 0: MPI disabled (default)
  34032. + - 1: MPI enable
  34033. + </td></tr>
  34034. +
  34035. +<tr>
  34036. + <td>pti_enable</td>
  34037. + <td>Specifies whether to enable PTI enhancement support.
  34038. + The driver will automatically detect the value for this parameter if none is
  34039. + specified.
  34040. + - 0: PTI disabled (default)
  34041. + - 1: PTI enable
  34042. + </td></tr>
  34043. +
  34044. +<tr>
  34045. + <td>lpm_enable</td>
  34046. + <td>Specifies whether to enable LPM support.
  34047. + The driver will automatically detect the value for this parameter if none is
  34048. + specified.
  34049. + - 0: LPM disabled
  34050. + - 1: LPM enable (default, if available)
  34051. + </td></tr>
  34052. +
  34053. +<tr>
  34054. + <td>ic_usb_cap</td>
  34055. + <td>Specifies whether to enable IC_USB capability.
  34056. + The driver will automatically detect the value for this parameter if none is
  34057. + specified.
  34058. + - 0: IC_USB disabled (default, if available)
  34059. + - 1: IC_USB enable
  34060. + </td></tr>
  34061. +
  34062. +<tr>
  34063. + <td>ahb_thr_ratio</td>
  34064. + <td>Specifies AHB Threshold ratio.
  34065. + - Values: 0 to 3 (default 0)
  34066. + </td></tr>
  34067. +
  34068. +<tr>
  34069. + <td>power_down</td>
  34070. + <td>Specifies Power Down(Hibernation) Mode.
  34071. + The driver will automatically detect the value for this parameter if none is
  34072. + specified.
  34073. + - 0: Power Down disabled (default)
  34074. + - 2: Power Down enabled
  34075. + </td></tr>
  34076. +
  34077. + <tr>
  34078. + <td>reload_ctl</td>
  34079. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  34080. + run time. The driver will automatically detect the value for this parameter if
  34081. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  34082. + the core might misbehave.
  34083. + - 0: Reload Control disabled (default)
  34084. + - 1: Reload Control enabled
  34085. + </td></tr>
  34086. +
  34087. + <tr>
  34088. + <td>dev_out_nak</td>
  34089. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  34090. + The driver will automatically detect the value for this parameter if
  34091. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  34092. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  34093. + - 1: The core sets NAK after Bulk OUT transfer complete
  34094. + </td></tr>
  34095. +
  34096. + <tr>
  34097. + <td>cont_on_bna</td>
  34098. + <td>Specifies whether Enable Continue on BNA enabled or no.
  34099. + After receiving BNA interrupt the core disables the endpoint,when the
  34100. + endpoint is re-enabled by the application the
  34101. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  34102. + - 1: Core starts processing from the descriptor which received the BNA.
  34103. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  34104. + </td></tr>
  34105. +
  34106. + <tr>
  34107. + <td>ahb_single</td>
  34108. + <td>This bit when programmed supports SINGLE transfers for remainder data
  34109. + in a transfer for DMA mode of operation.
  34110. + - 0: The remainder data will be sent using INCR burst size (default)
  34111. + - 1: The remainder data will be sent using SINGLE burst size.
  34112. + </td></tr>
  34113. +
  34114. +<tr>
  34115. + <td>adp_enable</td>
  34116. + <td>Specifies whether ADP feature is enabled.
  34117. + The driver will automatically detect the value for this parameter if none is
  34118. + specified.
  34119. + - 0: ADP feature disabled (default)
  34120. + - 1: ADP feature enabled
  34121. + </td></tr>
  34122. +
  34123. + <tr>
  34124. + <td>otg_ver</td>
  34125. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  34126. + USB OTG device.
  34127. + - 0: OTG 2.0 support disabled (default)
  34128. + - 1: OTG 2.0 support enabled
  34129. + </td></tr>
  34130. +
  34131. +*/
  34132. --- /dev/null
  34133. +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  34134. @@ -0,0 +1,86 @@
  34135. +/* ==========================================================================
  34136. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  34137. + * $Revision: #19 $
  34138. + * $Date: 2010/11/15 $
  34139. + * $Change: 1627671 $
  34140. + *
  34141. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  34142. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  34143. + * otherwise expressly agreed to in writing between Synopsys and you.
  34144. + *
  34145. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  34146. + * any End User Software License Agreement or Agreement for Licensed Product
  34147. + * with Synopsys or any supplement thereto. You are permitted to use and
  34148. + * redistribute this Software in source and binary forms, with or without
  34149. + * modification, provided that redistributions of source code must retain this
  34150. + * notice. You may not view, use, disclose, copy or distribute this file or
  34151. + * any information contained herein except pursuant to this license grant from
  34152. + * Synopsys. If you do not agree with this notice, including the disclaimer
  34153. + * below, then you are not authorized to use the Software.
  34154. + *
  34155. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  34156. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34157. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34158. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  34159. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  34160. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34161. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34162. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34163. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34164. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  34165. + * DAMAGE.
  34166. + * ========================================================================== */
  34167. +
  34168. +#ifndef __DWC_OTG_DRIVER_H__
  34169. +#define __DWC_OTG_DRIVER_H__
  34170. +
  34171. +/** @file
  34172. + * This file contains the interface to the Linux driver.
  34173. + */
  34174. +#include "dwc_otg_os_dep.h"
  34175. +#include "dwc_otg_core_if.h"
  34176. +
  34177. +/* Type declarations */
  34178. +struct dwc_otg_pcd;
  34179. +struct dwc_otg_hcd;
  34180. +
  34181. +/**
  34182. + * This structure is a wrapper that encapsulates the driver components used to
  34183. + * manage a single DWC_otg controller.
  34184. + */
  34185. +typedef struct dwc_otg_device {
  34186. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  34187. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  34188. + * require this. */
  34189. + struct os_dependent os_dep;
  34190. +
  34191. + /** Pointer to the core interface structure. */
  34192. + dwc_otg_core_if_t *core_if;
  34193. +
  34194. + /** Pointer to the PCD structure. */
  34195. + struct dwc_otg_pcd *pcd;
  34196. +
  34197. + /** Pointer to the HCD structure. */
  34198. + struct dwc_otg_hcd *hcd;
  34199. +
  34200. + /** Flag to indicate whether the common IRQ handler is installed. */
  34201. + uint8_t common_irq_installed;
  34202. +
  34203. +} dwc_otg_device_t;
  34204. +
  34205. +/*We must clear S3C24XX_EINTPEND external interrupt register
  34206. + * because after clearing in this register trigerred IRQ from
  34207. + * H/W core in kernel interrupt can be occured again before OTG
  34208. + * handlers clear all IRQ sources of Core registers because of
  34209. + * timing latencies and Low Level IRQ Type.
  34210. + */
  34211. +#ifdef CONFIG_MACH_IPMATE
  34212. +#define S3C2410X_CLEAR_EINTPEND() \
  34213. +do { \
  34214. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  34215. +} while (0)
  34216. +#else
  34217. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  34218. +#endif
  34219. +
  34220. +#endif
  34221. --- /dev/null
  34222. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  34223. @@ -0,0 +1,1355 @@
  34224. +/*
  34225. + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
  34226. + *
  34227. + * Copyright (c) 2013 Raspberry Pi Foundation
  34228. + *
  34229. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  34230. + * All rights reserved.
  34231. + *
  34232. + * Redistribution and use in source and binary forms, with or without
  34233. + * modification, are permitted provided that the following conditions are met:
  34234. + * * Redistributions of source code must retain the above copyright
  34235. + * notice, this list of conditions and the following disclaimer.
  34236. + * * Redistributions in binary form must reproduce the above copyright
  34237. + * notice, this list of conditions and the following disclaimer in the
  34238. + * documentation and/or other materials provided with the distribution.
  34239. + * * Neither the name of Raspberry Pi nor the
  34240. + * names of its contributors may be used to endorse or promote products
  34241. + * derived from this software without specific prior written permission.
  34242. + *
  34243. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  34244. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  34245. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  34246. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  34247. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  34248. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34249. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  34250. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34251. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34252. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34253. + *
  34254. + * This FIQ implements functionality that performs split transactions on
  34255. + * the dwc_otg hardware without any outside intervention. A split transaction
  34256. + * is "queued" by nominating a specific host channel to perform the entirety
  34257. + * of a split transaction. This FIQ will then perform the microframe-precise
  34258. + * scheduling required in each phase of the transaction until completion.
  34259. + *
  34260. + * The FIQ functionality is glued into the Synopsys driver via the entry point
  34261. + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
  34262. + * for a FSM-enabled channel.
  34263. + *
  34264. + * NB: Large parts of this implementation have architecture-specific code.
  34265. + * For porting this functionality to other ARM machines, the minimum is required:
  34266. + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
  34267. + * to the FIQ
  34268. + * - A method of forcing a software generated interrupt from FIQ mode that then
  34269. + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
  34270. + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
  34271. + * processor core - there is no locking between the FIQ and IRQ (aside from
  34272. + * local_fiq_disable)
  34273. + *
  34274. + */
  34275. +
  34276. +#include "dwc_otg_fiq_fsm.h"
  34277. +
  34278. +
  34279. +char buffer[1000*16];
  34280. +int wptr;
  34281. +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
  34282. +{
  34283. + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
  34284. + va_list args;
  34285. + char text[17];
  34286. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
  34287. +
  34288. + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
  34289. + {
  34290. + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
  34291. + va_start(args, fmt);
  34292. + vsnprintf(text+8, 9, fmt, args);
  34293. + va_end(args);
  34294. +
  34295. + memcpy(buffer + wptr, text, 16);
  34296. + wptr = (wptr + 16) % sizeof(buffer);
  34297. + }
  34298. +}
  34299. +
  34300. +/**
  34301. + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
  34302. + * Must be called with local interrupts and FIQ disabled.
  34303. + */
  34304. +#if defined(CONFIG_ARCH_BCM2709) && defined(CONFIG_SMP)
  34305. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
  34306. +{
  34307. + unsigned long tmp;
  34308. + uint32_t newval;
  34309. + fiq_lock_t lockval;
  34310. + smp_mb__before_spinlock();
  34311. + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
  34312. + * will be sufficient. If we are on a different CPU, then the lock protects us. */
  34313. + prefetchw(&lock->slock);
  34314. + asm volatile (
  34315. + "1: ldrex %0, [%3]\n"
  34316. + " add %1, %0, %4\n"
  34317. + " strex %2, %1, [%3]\n"
  34318. + " teq %2, #0\n"
  34319. + " bne 1b"
  34320. + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
  34321. + : "r" (&lock->slock), "I" (1 << 16)
  34322. + : "cc");
  34323. +
  34324. + while (lockval.tickets.next != lockval.tickets.owner) {
  34325. + wfe();
  34326. + lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
  34327. + }
  34328. + smp_mb();
  34329. +}
  34330. +#else
  34331. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock) { }
  34332. +#endif
  34333. +
  34334. +/**
  34335. + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
  34336. + */
  34337. +#if defined(CONFIG_ARCH_BCM2709) && defined(CONFIG_SMP)
  34338. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
  34339. +{
  34340. + smp_mb();
  34341. + lock->tickets.owner++;
  34342. + dsb_sev();
  34343. +}
  34344. +#else
  34345. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock) { }
  34346. +#endif
  34347. +
  34348. +/**
  34349. + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
  34350. + * @channel: channel to re-enable
  34351. + */
  34352. +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
  34353. +{
  34354. + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
  34355. +
  34356. + hcchar.b.chen = 0;
  34357. + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
  34358. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34359. + /* Hardware bug workaround: update the ssplit index */
  34360. + if (st->channel[n].hcsplt_copy.b.spltena)
  34361. + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  34362. +
  34363. + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  34364. + }
  34365. +
  34366. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  34367. + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  34368. + hcchar.b.chen = 1;
  34369. +
  34370. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  34371. + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
  34372. +}
  34373. +
  34374. +/**
  34375. + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
  34376. + * @st: Pointer to the channel's state
  34377. + * @n : channel number
  34378. + *
  34379. + * Change host channel registers to perform a complete-split transaction. Being mindful of the
  34380. + * endpoint direction, set control regs up correctly.
  34381. + */
  34382. +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
  34383. +{
  34384. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
  34385. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34386. +
  34387. + hcsplt.b.compsplt = 1;
  34388. + if (st->channel[n].hcchar_copy.b.epdir == 1) {
  34389. + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
  34390. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  34391. + } else {
  34392. + // If OUT, the CSPLIT result contains handshake only.
  34393. + hctsiz.b.xfersize = 0;
  34394. + }
  34395. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  34396. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34397. + mb();
  34398. +}
  34399. +
  34400. +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
  34401. +{
  34402. + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
  34403. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34404. +
  34405. + if (st->channel[n].hcchar_copy.b.epdir == 0) {
  34406. + return st->channel[n].hctsiz_copy.b.xfersize;
  34407. + } else {
  34408. + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
  34409. + }
  34410. +
  34411. +}
  34412. +
  34413. +
  34414. +/**
  34415. + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
  34416. + *
  34417. + * Of use only for IN periodic transfers.
  34418. + */
  34419. +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
  34420. +{
  34421. + hcdma_data_t hcdma;
  34422. + int i = st->channel[n].dma_info.index;
  34423. + int len;
  34424. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  34425. +
  34426. + len = fiq_get_xfer_len(st, n);
  34427. + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
  34428. + st->channel[n].dma_info.slot_len[i] = len;
  34429. + i++;
  34430. + if (i > 6)
  34431. + BUG();
  34432. +
  34433. + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
  34434. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34435. + st->channel[n].dma_info.index = i;
  34436. + return 0;
  34437. +}
  34438. +
  34439. +/**
  34440. + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
  34441. + */
  34442. +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
  34443. +{
  34444. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34445. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  34446. + hctsiz.b.pktcnt = 1;
  34447. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34448. +}
  34449. +
  34450. +/**
  34451. + * fiq_iso_out_advance() - update DMA address and split position bits
  34452. + * for isochronous OUT transactions.
  34453. + *
  34454. + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
  34455. + * Split-BEGIN states are not handled - this is done when the transaction was queued.
  34456. + *
  34457. + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
  34458. + */
  34459. +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
  34460. +{
  34461. + hcsplt_data_t hcsplt;
  34462. + hctsiz_data_t hctsiz;
  34463. + hcdma_data_t hcdma;
  34464. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  34465. + int last = 0;
  34466. + int i = st->channel[n].dma_info.index;
  34467. +
  34468. + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
  34469. + i++;
  34470. + if (i == 4)
  34471. + last = 1;
  34472. + if (st->channel[n].dma_info.slot_len[i+1] == 255)
  34473. + last = 1;
  34474. +
  34475. + /* New DMA address - address of bounce buffer referred to in index */
  34476. + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
  34477. + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n));
  34478. + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
  34479. + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
  34480. + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
  34481. + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
  34482. + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
  34483. + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
  34484. + /* Set up new packet length */
  34485. + hctsiz.b.pktcnt = 1;
  34486. + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
  34487. + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
  34488. +
  34489. + st->channel[n].dma_info.index++;
  34490. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  34491. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34492. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34493. + return last;
  34494. +}
  34495. +
  34496. +/**
  34497. + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
  34498. + *
  34499. + * Despite the limitations of the DWC core, we can force a microframe pipeline of
  34500. + * isochronous OUT start-split transactions while waiting for a corresponding other-type
  34501. + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
  34502. + * is very unlikely that filling the start-split FIFO will cause data loss.
  34503. + * This allows much better interleaving of transactions in an order-independent way-
  34504. + * there is no requirement to prioritise isochronous, just a state-space search has
  34505. + * to be performed on each periodic start-split complete interrupt.
  34506. + */
  34507. +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
  34508. +{
  34509. + int hub_addr = st->channel[n].hub_addr;
  34510. + int port_addr = st->channel[n].port_addr;
  34511. + int i, poked = 0;
  34512. + for (i = 0; i < num_channels; i++) {
  34513. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  34514. + continue;
  34515. + if (st->channel[i].hub_addr == hub_addr &&
  34516. + st->channel[i].port_addr == port_addr) {
  34517. + switch (st->channel[i].fsm) {
  34518. + case FIQ_PER_ISO_OUT_PENDING:
  34519. + if (st->channel[i].nrpackets == 1) {
  34520. + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
  34521. + } else {
  34522. + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34523. + }
  34524. + fiq_fsm_restart_channel(st, i, 0);
  34525. + poked = 1;
  34526. + break;
  34527. +
  34528. + default:
  34529. + break;
  34530. + }
  34531. + }
  34532. + if (poked)
  34533. + break;
  34534. + }
  34535. + return poked;
  34536. +}
  34537. +
  34538. +/**
  34539. + * fiq_fsm_tt_in_use() - search for host channels using this TT
  34540. + * @n: Channel to use as reference
  34541. + *
  34542. + */
  34543. +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
  34544. +{
  34545. + int hub_addr = st->channel[n].hub_addr;
  34546. + int port_addr = st->channel[n].port_addr;
  34547. + int i, in_use = 0;
  34548. + for (i = 0; i < num_channels; i++) {
  34549. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  34550. + continue;
  34551. + switch (st->channel[i].fsm) {
  34552. + /* TT is reserved for channels that are in the middle of a periodic
  34553. + * split transaction.
  34554. + */
  34555. + case FIQ_PER_SSPLIT_STARTED:
  34556. + case FIQ_PER_CSPLIT_WAIT:
  34557. + case FIQ_PER_CSPLIT_NYET1:
  34558. + //case FIQ_PER_CSPLIT_POLL:
  34559. + case FIQ_PER_ISO_OUT_ACTIVE:
  34560. + case FIQ_PER_ISO_OUT_LAST:
  34561. + if (st->channel[i].hub_addr == hub_addr &&
  34562. + st->channel[i].port_addr == port_addr) {
  34563. + in_use = 1;
  34564. + }
  34565. + break;
  34566. + default:
  34567. + break;
  34568. + }
  34569. + if (in_use)
  34570. + break;
  34571. + }
  34572. + return in_use;
  34573. +}
  34574. +
  34575. +/**
  34576. + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
  34577. + * to be issued for this IN transaction.
  34578. + *
  34579. + * We cannot tell the inbound PID of a data packet due to hardware limitations.
  34580. + * we need to make an educated guess as to whether we need to queue another CSPLIT
  34581. + * or not. A no-brainer is when we have received enough data to fill the endpoint
  34582. + * size, but for endpoints that give variable-length data then we have to resort
  34583. + * to heuristics.
  34584. + *
  34585. + * We also return whether this is the last CSPLIT to be queued, again based on
  34586. + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
  34587. + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
  34588. + */
  34589. +
  34590. +/*
  34591. + * We need some way of guaranteeing if a returned periodic packet of size X
  34592. + * has a DATA0 PID.
  34593. + * The heuristic value of 144 bytes assumes that the received data has maximal
  34594. + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
  34595. + * permissible limit. If the transfer length results in a final packet size
  34596. + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
  34597. + * Also used to ensure that an endpoint will nominally only return a single
  34598. + * complete-split worth of data.
  34599. + */
  34600. +#define DATA0_PID_HEURISTIC 144
  34601. +
  34602. +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
  34603. +{
  34604. +
  34605. + int i;
  34606. + int total_len = 0;
  34607. + int more_needed = 1;
  34608. + struct fiq_channel_state *st = &state->channel[n];
  34609. +
  34610. + for (i = 0; i < st->dma_info.index; i++) {
  34611. + total_len += st->dma_info.slot_len[i];
  34612. + }
  34613. +
  34614. + *probably_last = 0;
  34615. +
  34616. + if (st->hcchar_copy.b.eptype == 0x3) {
  34617. + /*
  34618. + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
  34619. + * then this is definitely the last CSPLIT.
  34620. + */
  34621. + *probably_last = 1;
  34622. + } else {
  34623. + /* Isoc IN. This is a bit risky if we are the first transaction:
  34624. + * we may have been held off slightly. */
  34625. + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
  34626. + more_needed = 0;
  34627. + }
  34628. + /* If in the next uframe we will receive enough data to fill the endpoint,
  34629. + * then only issue 1 more csplit.
  34630. + */
  34631. + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
  34632. + *probably_last = 1;
  34633. + }
  34634. +
  34635. + if (total_len >= st->hctsiz_copy.b.xfersize ||
  34636. + i == 6 || total_len == 0)
  34637. + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
  34638. + * a single endpoint. Accepting more would completely break our scheduling mechanism though
  34639. + * - in these extreme cases we will pass through a truncated packet.
  34640. + */
  34641. + more_needed = 0;
  34642. +
  34643. + return more_needed;
  34644. +}
  34645. +
  34646. +/**
  34647. + * fiq_fsm_too_late() - Test transaction for lateness
  34648. + *
  34649. + * If a SSPLIT for a large IN transaction is issued too late in a frame,
  34650. + * the hub will disable the port to the device and respond with ERR handshakes.
  34651. + * The hub status endpoint will not reflect this change.
  34652. + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
  34653. + */
  34654. +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
  34655. +{
  34656. + int uframe;
  34657. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34658. + uframe = hfnum.b.frnum & 0x7;
  34659. + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
  34660. + return 1;
  34661. + } else {
  34662. + return 0;
  34663. + }
  34664. +}
  34665. +
  34666. +
  34667. +/**
  34668. + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
  34669. + *
  34670. + * Search pending transactions in the start-split pending state and queue them.
  34671. + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
  34672. + * Note: we specifically don't do isochronous OUT transactions first because better
  34673. + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
  34674. + */
  34675. +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
  34676. +{
  34677. + int n;
  34678. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34679. + if ((hfnum.b.frnum & 0x7) == 5)
  34680. + return;
  34681. + for (n = 0; n < num_channels; n++) {
  34682. + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
  34683. + /* Check to see if any other transactions are using this TT */
  34684. + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
  34685. + if (!fiq_fsm_too_late(st, n)) {
  34686. + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  34687. + fiq_print(FIQDBG_INT, st, "NEXTPER ");
  34688. + fiq_fsm_restart_channel(st, n, 0);
  34689. + } else {
  34690. + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34691. + }
  34692. + break;
  34693. + }
  34694. + }
  34695. + }
  34696. + for (n = 0; n < num_channels; n++) {
  34697. + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
  34698. + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
  34699. + fiq_print(FIQDBG_INT, st, "NEXTISO ");
  34700. + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34701. + fiq_fsm_restart_channel(st, n, 0);
  34702. + break;
  34703. + }
  34704. + }
  34705. + }
  34706. +}
  34707. +
  34708. +/**
  34709. + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
  34710. + * @state: Pointer to fiq_state
  34711. + * @n: Channel transaction is active on
  34712. + * @hcint: Copy of host channel interrupt register
  34713. + *
  34714. + * Returns 0 if there are no more transactions for this HC to do, 1
  34715. + * otherwise.
  34716. + */
  34717. +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
  34718. +{
  34719. + struct fiq_channel_state *st = &state->channel[n];
  34720. + int xfer_len = 0, nrpackets = 0;
  34721. + hcdma_data_t hcdma;
  34722. + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
  34723. +
  34724. + xfer_len = fiq_get_xfer_len(state, n);
  34725. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
  34726. +
  34727. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
  34728. +
  34729. + st->hs_isoc_info.index++;
  34730. + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
  34731. + return 0;
  34732. + }
  34733. +
  34734. + /* grab the next DMA address offset from the array */
  34735. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
  34736. + FIQ_WRITE(state->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34737. +
  34738. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  34739. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  34740. + * this is always set to the maximum size of the endpoint. */
  34741. + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
  34742. + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
  34743. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  34744. + if (nrpackets == 0)
  34745. + nrpackets = 1;
  34746. + st->hcchar_copy.b.multicnt = nrpackets;
  34747. + st->hctsiz_copy.b.pktcnt = nrpackets;
  34748. +
  34749. + /* Initial PID also needs to be set */
  34750. + if (st->hcchar_copy.b.epdir == 0) {
  34751. + st->hctsiz_copy.b.xfersize = xfer_len;
  34752. + switch (st->hcchar_copy.b.multicnt) {
  34753. + case 1:
  34754. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  34755. + break;
  34756. + case 2:
  34757. + case 3:
  34758. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  34759. + break;
  34760. + }
  34761. +
  34762. + } else {
  34763. + switch (st->hcchar_copy.b.multicnt) {
  34764. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  34765. + case 1:
  34766. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  34767. + break;
  34768. + case 2:
  34769. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  34770. + break;
  34771. + case 3:
  34772. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  34773. + break;
  34774. + }
  34775. + }
  34776. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
  34777. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
  34778. + /* Channel is enabled on hcint handler exit */
  34779. + fiq_print(FIQDBG_INT, state, "HSISOOUT");
  34780. + return 1;
  34781. +}
  34782. +
  34783. +
  34784. +/**
  34785. + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
  34786. + * @state: Pointer to the state struct passed from banked FIQ mode registers.
  34787. + * @num_channels: set according to the DWC hardware configuration
  34788. + *
  34789. + * The SOF handler in FSM mode has two functions
  34790. + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
  34791. + * nothing to do
  34792. + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
  34793. + * of holdoff.
  34794. + *
  34795. + * The second part is architecture-specific to mach-bcm2835 -
  34796. + * a sane interrupt controller would have a mask register for ARM interrupt sources
  34797. + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
  34798. + * number (USB) can be enabled. This means that certain parts of the USB specification
  34799. + * that require "wait a little while, then issue another packet" cannot be fulfilled with
  34800. + * the timing granularity required to achieve optimal throughout. The workaround is to use
  34801. + * the SOF "timer" (125uS) to perform this task.
  34802. + */
  34803. +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
  34804. +{
  34805. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
  34806. + int n;
  34807. + int kick_irq = 0;
  34808. +
  34809. + if ((hfnum.b.frnum & 0x7) == 1) {
  34810. + /* We cannot issue csplits for transactions in the last frame past (n+1).1
  34811. + * Check to see if there are any transactions that are stale.
  34812. + * Boot them out.
  34813. + */
  34814. + for (n = 0; n < num_channels; n++) {
  34815. + switch (state->channel[n].fsm) {
  34816. + case FIQ_PER_CSPLIT_WAIT:
  34817. + case FIQ_PER_CSPLIT_NYET1:
  34818. + case FIQ_PER_CSPLIT_POLL:
  34819. + case FIQ_PER_CSPLIT_LAST:
  34820. + /* Check if we are no longer in the same full-speed frame. */
  34821. + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
  34822. + (hfnum.b.frnum & ~0x7))
  34823. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34824. + break;
  34825. + default:
  34826. + break;
  34827. + }
  34828. + }
  34829. + }
  34830. +
  34831. + for (n = 0; n < num_channels; n++) {
  34832. + switch (state->channel[n].fsm) {
  34833. +
  34834. + case FIQ_NP_SSPLIT_RETRY:
  34835. + case FIQ_NP_IN_CSPLIT_RETRY:
  34836. + case FIQ_NP_OUT_CSPLIT_RETRY:
  34837. + fiq_fsm_restart_channel(state, n, 0);
  34838. + break;
  34839. +
  34840. + case FIQ_HS_ISOC_SLEEPING:
  34841. + /* Is it time to wake this channel yet? */
  34842. + if (--state->channel[n].uframe_sleeps == 0) {
  34843. + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
  34844. + fiq_fsm_restart_channel(state, n, 0);
  34845. + }
  34846. + break;
  34847. +
  34848. + case FIQ_PER_SSPLIT_QUEUED:
  34849. + if ((hfnum.b.frnum & 0x7) == 5)
  34850. + break;
  34851. + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
  34852. + if (!fiq_fsm_too_late(state, n)) {
  34853. + fiq_print(FIQDBG_INT, state, "SOF GO %01d", n);
  34854. + fiq_fsm_restart_channel(state, n, 0);
  34855. + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  34856. + } else {
  34857. + /* Transaction cannot be started without risking a device babble error */
  34858. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34859. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  34860. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  34861. + kick_irq |= 1;
  34862. + }
  34863. + }
  34864. + break;
  34865. +
  34866. + case FIQ_PER_ISO_OUT_PENDING:
  34867. + /* Ordinarily, this should be poked after the SSPLIT
  34868. + * complete interrupt for a competing transfer on the same
  34869. + * TT. Doesn't happen for aborted transactions though.
  34870. + */
  34871. + if ((hfnum.b.frnum & 0x7) >= 5)
  34872. + break;
  34873. + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
  34874. + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
  34875. + * that caused this.
  34876. + */
  34877. + fiq_fsm_restart_channel(state, n, 0);
  34878. + fiq_print(FIQDBG_INT, state, "SOF ISOC");
  34879. + if (state->channel[n].nrpackets == 1) {
  34880. + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  34881. + } else {
  34882. + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34883. + }
  34884. + }
  34885. + break;
  34886. +
  34887. + case FIQ_PER_CSPLIT_WAIT:
  34888. + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
  34889. + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
  34890. + * will utterly bugger this up though.
  34891. + */
  34892. + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
  34893. + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
  34894. + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
  34895. + fiq_fsm_restart_channel(state, n, 0);
  34896. + fiq_fsm_start_next_periodic(state, num_channels);
  34897. +
  34898. + }
  34899. + break;
  34900. +
  34901. + case FIQ_PER_SPLIT_TIMEOUT:
  34902. + case FIQ_DEQUEUE_ISSUED:
  34903. + /* Ugly: we have to force a HCD interrupt.
  34904. + * Poke the mask for the channel in question.
  34905. + * We will take a fake SOF because of this, but
  34906. + * that's OK.
  34907. + */
  34908. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  34909. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  34910. + kick_irq |= 1;
  34911. + break;
  34912. +
  34913. + default:
  34914. + break;
  34915. + }
  34916. + }
  34917. +
  34918. + if (state->kick_np_queues ||
  34919. + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
  34920. + kick_irq |= 1;
  34921. +
  34922. + return !kick_irq;
  34923. +}
  34924. +
  34925. +
  34926. +/**
  34927. + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
  34928. + * @state: Pointer to the FIQ state struct
  34929. + * @num_channels: Number of channels as per hardware config
  34930. + * @n: channel for which HAINT(i) was raised
  34931. + *
  34932. + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
  34933. + */
  34934. +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
  34935. +{
  34936. + hcint_data_t hcint;
  34937. + hcintmsk_data_t hcintmsk;
  34938. + hcint_data_t hcint_probe;
  34939. + hcchar_data_t hcchar;
  34940. + int handled = 0;
  34941. + int restart = 0;
  34942. + int last_csplit = 0;
  34943. + int start_next_periodic = 0;
  34944. + struct fiq_channel_state *st = &state->channel[n];
  34945. + hfnum_data_t hfnum;
  34946. +
  34947. + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
  34948. + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
  34949. + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
  34950. +
  34951. + if (st->fsm != FIQ_PASSTHROUGH) {
  34952. + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
  34953. + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
  34954. + }
  34955. +
  34956. + switch (st->fsm) {
  34957. +
  34958. + case FIQ_PASSTHROUGH:
  34959. + case FIQ_DEQUEUE_ISSUED:
  34960. + /* doesn't belong to us, kick it upstairs */
  34961. + break;
  34962. +
  34963. + case FIQ_PASSTHROUGH_ERRORSTATE:
  34964. + /* We are here to emulate the error recovery mechanism of the dwc HCD.
  34965. + * Several interrupts are unmasked if a previous transaction failed - it's
  34966. + * death for the FIQ to attempt to handle them as the channel isn't halted.
  34967. + * Emulate what the HCD does in this situation: mask and continue.
  34968. + * The FSM has no other state setup so this has to be handled out-of-band.
  34969. + */
  34970. + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
  34971. + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
  34972. + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
  34973. + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
  34974. + * interrupt, after the device has disappeared.
  34975. + */
  34976. + if (!hcint.b.xacterr)
  34977. + st->nr_errors = 0;
  34978. + hcintmsk.b.nak = 0;
  34979. + hcintmsk.b.ack = 0;
  34980. + hcintmsk.b.datatglerr = 0;
  34981. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
  34982. + return 1;
  34983. + }
  34984. + if (hcint_probe.b.chhltd) {
  34985. + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
  34986. + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
  34987. + return 0;
  34988. + }
  34989. + break;
  34990. +
  34991. + /* Non-periodic state groups */
  34992. + case FIQ_NP_SSPLIT_STARTED:
  34993. + case FIQ_NP_SSPLIT_RETRY:
  34994. + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
  34995. + if (hcint.b.ack) {
  34996. + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
  34997. + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
  34998. + */
  34999. + if(st->hcchar_copy.b.epdir == 1)
  35000. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  35001. + else
  35002. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35003. + st->nr_errors = 0;
  35004. + handled = 1;
  35005. + fiq_fsm_setup_csplit(state, n);
  35006. + } else if (hcint.b.nak) {
  35007. + // No buffer space in TT. Retry on a uframe boundary.
  35008. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  35009. + handled = 1;
  35010. + } else if (hcint.b.xacterr) {
  35011. + // The only other one we care about is xacterr. This implies HS bus error - retry.
  35012. + st->nr_errors++;
  35013. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  35014. + if (st->nr_errors >= 3) {
  35015. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35016. + } else {
  35017. + handled = 1;
  35018. + restart = 1;
  35019. + }
  35020. + } else {
  35021. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35022. + handled = 0;
  35023. + restart = 0;
  35024. + }
  35025. + break;
  35026. +
  35027. + case FIQ_NP_IN_CSPLIT_RETRY:
  35028. + /* Received a CSPLIT done interrupt.
  35029. + * Expected Data/NAK/STALL/NYET for IN.
  35030. + */
  35031. + if (hcint.b.xfercomp) {
  35032. + /* For IN, data is present. */
  35033. + st->fsm = FIQ_NP_SPLIT_DONE;
  35034. + } else if (hcint.b.nak) {
  35035. + /* no endpoint data. Punt it upstairs */
  35036. + st->fsm = FIQ_NP_SPLIT_DONE;
  35037. + } else if (hcint.b.nyet) {
  35038. + /* CSPLIT NYET - retry on a uframe boundary. */
  35039. + handled = 1;
  35040. + st->nr_errors = 0;
  35041. + } else if (hcint.b.datatglerr) {
  35042. + /* data toggle errors do not set the xfercomp bit. */
  35043. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35044. + } else if (hcint.b.xacterr) {
  35045. + /* HS error. Retry immediate */
  35046. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  35047. + st->nr_errors++;
  35048. + if (st->nr_errors >= 3) {
  35049. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35050. + } else {
  35051. + handled = 1;
  35052. + restart = 1;
  35053. + }
  35054. + } else if (hcint.b.stall || hcint.b.bblerr) {
  35055. + /* A STALL implies either a LS bus error or a genuine STALL. */
  35056. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35057. + } else {
  35058. + /* Hardware bug. It's possible in some cases to
  35059. + * get a channel halt with nothing else set when
  35060. + * the response was a NYET. Treat as local 3-strikes retry.
  35061. + */
  35062. + hcint_data_t hcint_test = hcint;
  35063. + hcint_test.b.chhltd = 0;
  35064. + if (!hcint_test.d32) {
  35065. + st->nr_errors++;
  35066. + if (st->nr_errors >= 3) {
  35067. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35068. + } else {
  35069. + handled = 1;
  35070. + }
  35071. + } else {
  35072. + /* Bail out if something unexpected happened */
  35073. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35074. + }
  35075. + }
  35076. + break;
  35077. +
  35078. + case FIQ_NP_OUT_CSPLIT_RETRY:
  35079. + /* Received a CSPLIT done interrupt.
  35080. + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
  35081. + if (hcint.b.xfercomp) {
  35082. + st->fsm = FIQ_NP_SPLIT_DONE;
  35083. + } else if (hcint.b.nak) {
  35084. + // The HCD will implement the holdoff on frame boundaries.
  35085. + st->fsm = FIQ_NP_SPLIT_DONE;
  35086. + } else if (hcint.b.nyet) {
  35087. + // Hub still processing.
  35088. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35089. + handled = 1;
  35090. + st->nr_errors = 0;
  35091. + //restart = 1;
  35092. + } else if (hcint.b.xacterr) {
  35093. + /* HS error. retry immediate */
  35094. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35095. + st->nr_errors++;
  35096. + if (st->nr_errors >= 3) {
  35097. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35098. + } else {
  35099. + handled = 1;
  35100. + restart = 1;
  35101. + }
  35102. + } else if (hcint.b.stall) {
  35103. + /* LS bus error or genuine stall */
  35104. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35105. + } else {
  35106. + /*
  35107. + * Hardware bug. It's possible in some cases to get a
  35108. + * channel halt with nothing else set when the response was a NYET.
  35109. + * Treat as local 3-strikes retry.
  35110. + */
  35111. + hcint_data_t hcint_test = hcint;
  35112. + hcint_test.b.chhltd = 0;
  35113. + if (!hcint_test.d32) {
  35114. + st->nr_errors++;
  35115. + if (st->nr_errors >= 3) {
  35116. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35117. + } else {
  35118. + handled = 1;
  35119. + }
  35120. + } else {
  35121. + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
  35122. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35123. + }
  35124. + }
  35125. + break;
  35126. +
  35127. + /* Periodic split states (except isoc out) */
  35128. + case FIQ_PER_SSPLIT_STARTED:
  35129. + /* Expect an ACK or failure for SSPLIT */
  35130. + if (hcint.b.ack) {
  35131. + /*
  35132. + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
  35133. + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
  35134. + * point for microframe n-3, the packet will not appear on the bus until microframe n.
  35135. + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
  35136. + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
  35137. + * coincident with SOF for n+1.
  35138. + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
  35139. + * These appear to be caused by timing/clock crossing bugs within the core itself.
  35140. + * State machine workaround.
  35141. + */
  35142. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35143. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35144. + fiq_fsm_setup_csplit(state, n);
  35145. + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
  35146. + * time. If not, then we're in the next SOF.
  35147. + */
  35148. + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
  35149. + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
  35150. + st->expected_uframe = hfnum.b.frnum;
  35151. + st->fsm = FIQ_PER_CSPLIT_WAIT;
  35152. + } else {
  35153. + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
  35154. + /* For isochronous IN endpoints,
  35155. + * we need to hold off if we are expecting a lot of data */
  35156. + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
  35157. + start_next_periodic = 1;
  35158. + }
  35159. + /* Danger will robinson: we are in a broken state. If our first interrupt after
  35160. + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
  35161. + * lag. Unmask the NYET interrupt.
  35162. + */
  35163. + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  35164. + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
  35165. + restart = 1;
  35166. + }
  35167. + handled = 1;
  35168. + } else if (hcint.b.xacterr) {
  35169. + /* 3-strikes retry is enabled, we have hit our max nr_errors */
  35170. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35171. + start_next_periodic = 1;
  35172. + } else {
  35173. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35174. + start_next_periodic = 1;
  35175. + }
  35176. + /* We can now queue the next isochronous OUT transaction, if one is pending. */
  35177. + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
  35178. + fiq_print(FIQDBG_INT, state, "NEXTISO ");
  35179. + }
  35180. + break;
  35181. +
  35182. + case FIQ_PER_CSPLIT_NYET1:
  35183. + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
  35184. + * we are too late and the TT has dropped its CSPLIT fifo.
  35185. + */
  35186. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35187. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35188. + start_next_periodic = 1;
  35189. + if (hcint.b.nak) {
  35190. + st->fsm = FIQ_PER_SPLIT_DONE;
  35191. + } else if (hcint.b.xfercomp) {
  35192. + fiq_increment_dma_buf(state, num_channels, n);
  35193. + st->fsm = FIQ_PER_CSPLIT_POLL;
  35194. + st->nr_errors = 0;
  35195. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35196. + handled = 1;
  35197. + restart = 1;
  35198. + if (!last_csplit)
  35199. + start_next_periodic = 0;
  35200. + } else {
  35201. + st->fsm = FIQ_PER_SPLIT_DONE;
  35202. + }
  35203. + } else if (hcint.b.nyet) {
  35204. + /* Doh. Data lost. */
  35205. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35206. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35207. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35208. + } else {
  35209. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35210. + }
  35211. + break;
  35212. +
  35213. + case FIQ_PER_CSPLIT_BROKEN_NYET1:
  35214. + /*
  35215. + * we got here because our host channel is in the delayed-interrupt
  35216. + * state and we cannot take a NYET interrupt any later than when it
  35217. + * occurred. Disable then re-enable the channel if this happens to force
  35218. + * CSPLITs to occur at the right time.
  35219. + */
  35220. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35221. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35222. + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
  35223. + if (hcint.b.nak) {
  35224. + st->fsm = FIQ_PER_SPLIT_DONE;
  35225. + start_next_periodic = 1;
  35226. + } else if (hcint.b.xfercomp) {
  35227. + fiq_increment_dma_buf(state, num_channels, n);
  35228. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35229. + st->fsm = FIQ_PER_CSPLIT_POLL;
  35230. + handled = 1;
  35231. + restart = 1;
  35232. + start_next_periodic = 1;
  35233. + /* Reload HCTSIZ for the next transfer */
  35234. + fiq_fsm_reload_hctsiz(state, n);
  35235. + if (!last_csplit)
  35236. + start_next_periodic = 0;
  35237. + } else {
  35238. + st->fsm = FIQ_PER_SPLIT_DONE;
  35239. + }
  35240. + } else if (hcint.b.nyet) {
  35241. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35242. + start_next_periodic = 1;
  35243. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35244. + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
  35245. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35246. + } else {
  35247. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35248. + }
  35249. + break;
  35250. +
  35251. + case FIQ_PER_CSPLIT_POLL:
  35252. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35253. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35254. + start_next_periodic = 1;
  35255. + if (hcint.b.nak) {
  35256. + st->fsm = FIQ_PER_SPLIT_DONE;
  35257. + } else if (hcint.b.xfercomp) {
  35258. + fiq_increment_dma_buf(state, num_channels, n);
  35259. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35260. + handled = 1;
  35261. + restart = 1;
  35262. + /* Reload HCTSIZ for the next transfer */
  35263. + fiq_fsm_reload_hctsiz(state, n);
  35264. + if (!last_csplit)
  35265. + start_next_periodic = 0;
  35266. + } else {
  35267. + st->fsm = FIQ_PER_SPLIT_DONE;
  35268. + }
  35269. + } else if (hcint.b.nyet) {
  35270. + /* Are we a NYET after the first data packet? */
  35271. + if (st->nrpackets == 0) {
  35272. + st->fsm = FIQ_PER_CSPLIT_NYET1;
  35273. + handled = 1;
  35274. + restart = 1;
  35275. + } else {
  35276. + /* We got a NYET when polling CSPLITs. Can happen
  35277. + * if our heuristic fails, or if someone disables us
  35278. + * for any significant length of time.
  35279. + */
  35280. + if (st->nr_errors >= 3) {
  35281. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35282. + } else {
  35283. + st->fsm = FIQ_PER_SPLIT_DONE;
  35284. + }
  35285. + }
  35286. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35287. + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
  35288. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35289. + } else {
  35290. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35291. + }
  35292. + break;
  35293. +
  35294. + case FIQ_HS_ISOC_TURBO:
  35295. + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
  35296. + /* more transactions to come */
  35297. + handled = 1;
  35298. + fiq_print(FIQDBG_INT, state, "HSISO M ");
  35299. + /* For strided transfers, put ourselves to sleep */
  35300. + if (st->hs_isoc_info.stride > 1) {
  35301. + st->uframe_sleeps = st->hs_isoc_info.stride - 1;
  35302. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  35303. + } else {
  35304. + restart = 1;
  35305. + }
  35306. + } else {
  35307. + st->fsm = FIQ_HS_ISOC_DONE;
  35308. + fiq_print(FIQDBG_INT, state, "HSISO F ");
  35309. + }
  35310. + break;
  35311. +
  35312. + case FIQ_HS_ISOC_ABORTED:
  35313. + /* This abort is called by the driver rewriting the state mid-transaction
  35314. + * which allows the dequeue mechanism to work more effectively.
  35315. + */
  35316. + break;
  35317. +
  35318. + case FIQ_PER_ISO_OUT_ACTIVE:
  35319. + if (hcint.b.ack) {
  35320. + if(fiq_iso_out_advance(state, num_channels, n)) {
  35321. + /* last OUT transfer */
  35322. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  35323. + /*
  35324. + * Assuming the periodic FIFO in the dwc core
  35325. + * actually does its job properly, we can queue
  35326. + * the next ssplit now and in theory, the wire
  35327. + * transactions will be in-order.
  35328. + */
  35329. + // No it doesn't. It appears to process requests in host channel order.
  35330. + //start_next_periodic = 1;
  35331. + }
  35332. + handled = 1;
  35333. + restart = 1;
  35334. + } else {
  35335. + /*
  35336. + * Isochronous transactions carry on regardless. Log the error
  35337. + * and continue.
  35338. + */
  35339. + //explode += 1;
  35340. + st->nr_errors++;
  35341. + if(fiq_iso_out_advance(state, num_channels, n)) {
  35342. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  35343. + //start_next_periodic = 1;
  35344. + }
  35345. + handled = 1;
  35346. + restart = 1;
  35347. + }
  35348. + break;
  35349. +
  35350. + case FIQ_PER_ISO_OUT_LAST:
  35351. + if (hcint.b.ack) {
  35352. + /* All done here */
  35353. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  35354. + } else {
  35355. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  35356. + st->nr_errors++;
  35357. + }
  35358. + start_next_periodic = 1;
  35359. + break;
  35360. +
  35361. + case FIQ_PER_SPLIT_TIMEOUT:
  35362. + /* SOF kicked us because we overran. */
  35363. + start_next_periodic = 1;
  35364. + break;
  35365. +
  35366. + default:
  35367. + break;
  35368. + }
  35369. +
  35370. + if (handled) {
  35371. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
  35372. + } else {
  35373. + /* Copy the regs into the state so the IRQ knows what to do */
  35374. + st->hcint_copy.d32 = hcint.d32;
  35375. + }
  35376. +
  35377. + if (restart) {
  35378. + /* Restart always implies handled. */
  35379. + if (restart == 2) {
  35380. + /* For complete-split INs, the show must go on.
  35381. + * Force a channel restart */
  35382. + fiq_fsm_restart_channel(state, n, 1);
  35383. + } else {
  35384. + fiq_fsm_restart_channel(state, n, 0);
  35385. + }
  35386. + }
  35387. + if (start_next_periodic) {
  35388. + fiq_fsm_start_next_periodic(state, num_channels);
  35389. + }
  35390. + if (st->fsm != FIQ_PASSTHROUGH)
  35391. + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
  35392. +
  35393. + return handled;
  35394. +}
  35395. +
  35396. +
  35397. +/**
  35398. + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
  35399. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  35400. + * @num_channels: set according to the DWC hardware configuration
  35401. + * @dma: pointer to DMA bounce buffers for split transaction slots
  35402. + *
  35403. + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
  35404. + * inside an EHCI or similar host controller regarding split transactions. The DWC core
  35405. + * interrupts each and every time a split transaction packet is received or sent successfully.
  35406. + * This results in either an interrupt storm when everything is working "properly", or
  35407. + * the interrupt latency of the system in general breaks time-sensitive periodic split
  35408. + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
  35409. + * solves these problems.
  35410. + *
  35411. + * Return: void
  35412. + */
  35413. +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
  35414. +{
  35415. + gintsts_data_t gintsts, gintsts_handled;
  35416. + gintmsk_data_t gintmsk;
  35417. + //hfnum_data_t hfnum;
  35418. + haint_data_t haint, haint_handled;
  35419. + haintmsk_data_t haintmsk;
  35420. + int kick_irq = 0;
  35421. +
  35422. + gintsts_handled.d32 = 0;
  35423. + haint_handled.d32 = 0;
  35424. +
  35425. + fiq_fsm_spin_lock(&state->lock);
  35426. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  35427. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  35428. + gintsts.d32 &= gintmsk.d32;
  35429. +
  35430. + if (gintsts.b.sofintr) {
  35431. + /* For FSM mode, SOF is required to keep the state machine advance for
  35432. + * certain stages of the periodic pipeline. It's death to mask this
  35433. + * interrupt in that case.
  35434. + */
  35435. +
  35436. + if (!fiq_fsm_do_sof(state, num_channels)) {
  35437. + /* Kick IRQ once. Queue advancement means that all pending transactions
  35438. + * will get serviced when the IRQ finally executes.
  35439. + */
  35440. + if (state->gintmsk_saved.b.sofintr == 1)
  35441. + kick_irq |= 1;
  35442. + state->gintmsk_saved.b.sofintr = 0;
  35443. + }
  35444. + gintsts_handled.b.sofintr = 1;
  35445. + }
  35446. +
  35447. + if (gintsts.b.hcintr) {
  35448. + int i;
  35449. + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
  35450. + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
  35451. + haint.d32 &= haintmsk.d32;
  35452. + haint_handled.d32 = 0;
  35453. + for (i=0; i<num_channels; i++) {
  35454. + if (haint.b2.chint & (1 << i)) {
  35455. + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
  35456. + /* HCINT was not handled in FIQ
  35457. + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
  35458. + * Mask HAINT(i) but keep top-level hcint unmasked.
  35459. + */
  35460. + state->haintmsk_saved.b2.chint &= ~(1 << i);
  35461. + } else {
  35462. + /* do_hcintr cleaned up after itself, but clear haint */
  35463. + haint_handled.b2.chint |= (1 << i);
  35464. + }
  35465. + }
  35466. + }
  35467. +
  35468. + if (haint_handled.b2.chint) {
  35469. + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
  35470. + }
  35471. +
  35472. + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
  35473. + /*
  35474. + * This is necessary to avoid multiple retriggers of the MPHI in the case
  35475. + * where interrupts are held off and HCINTs start to pile up.
  35476. + * Only wake up the IRQ if a new interrupt came in, was not handled and was
  35477. + * masked.
  35478. + */
  35479. + haintmsk.d32 &= state->haintmsk_saved.d32;
  35480. + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
  35481. + kick_irq |= 1;
  35482. + }
  35483. + /* Top-Level interrupt - always handled because it's level-sensitive */
  35484. + gintsts_handled.b.hcintr = 1;
  35485. + }
  35486. +
  35487. +
  35488. + /* Clear the bits in the saved register that were not handled but were triggered. */
  35489. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  35490. +
  35491. + /* FIQ didn't handle something - mask has changed - write new mask */
  35492. + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
  35493. + gintmsk.d32 &= state->gintmsk_saved.d32;
  35494. + gintmsk.b.sofintr = 1;
  35495. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  35496. +// fiq_print(FIQDBG_INT, state, "KICKGINT");
  35497. +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
  35498. +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
  35499. + kick_irq |= 1;
  35500. + }
  35501. +
  35502. + if (gintsts_handled.d32) {
  35503. + /* Only applies to edge-sensitive bits in GINTSTS */
  35504. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  35505. + }
  35506. +
  35507. + /* We got an interrupt, didn't handle it. */
  35508. + if (kick_irq) {
  35509. + state->mphi_int_count++;
  35510. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  35511. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  35512. +
  35513. + }
  35514. + state->fiq_done++;
  35515. + mb();
  35516. + fiq_fsm_spin_unlock(&state->lock);
  35517. +}
  35518. +
  35519. +
  35520. +/**
  35521. + * dwc_otg_fiq_nop() - FIQ "lite"
  35522. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  35523. + *
  35524. + * The "nop" handler does not intervene on any interrupts other than SOF.
  35525. + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
  35526. + * with non-periodic/periodic queues) needs to be kicked.
  35527. + *
  35528. + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
  35529. + *
  35530. + * Return: void
  35531. + */
  35532. +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
  35533. +{
  35534. + gintsts_data_t gintsts, gintsts_handled;
  35535. + gintmsk_data_t gintmsk;
  35536. + hfnum_data_t hfnum;
  35537. +
  35538. + fiq_fsm_spin_lock(&state->lock);
  35539. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35540. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  35541. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  35542. + gintsts.d32 &= gintmsk.d32;
  35543. + gintsts_handled.d32 = 0;
  35544. +
  35545. + if (gintsts.b.sofintr) {
  35546. + if (!state->kick_np_queues &&
  35547. + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
  35548. + /* SOF handled, no work to do, just ACK interrupt */
  35549. + gintsts_handled.b.sofintr = 1;
  35550. + } else {
  35551. + /* Kick IRQ */
  35552. + state->gintmsk_saved.b.sofintr = 0;
  35553. + }
  35554. + }
  35555. +
  35556. + /* Reset handled interrupts */
  35557. + if(gintsts_handled.d32) {
  35558. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  35559. + }
  35560. +
  35561. + /* Clear the bits in the saved register that were not handled but were triggered. */
  35562. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  35563. +
  35564. + /* We got an interrupt, didn't handle it and want to mask it */
  35565. + if (~(state->gintmsk_saved.d32)) {
  35566. + state->mphi_int_count++;
  35567. + gintmsk.d32 &= state->gintmsk_saved.d32;
  35568. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  35569. + /* Force a clear before another dummy send */
  35570. + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
  35571. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  35572. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  35573. +
  35574. + }
  35575. + state->fiq_done++;
  35576. + mb();
  35577. + fiq_fsm_spin_unlock(&state->lock);
  35578. +}
  35579. --- /dev/null
  35580. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  35581. @@ -0,0 +1,370 @@
  35582. +/*
  35583. + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
  35584. + *
  35585. + * Copyright (c) 2013 Raspberry Pi Foundation
  35586. + *
  35587. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  35588. + * All rights reserved.
  35589. + *
  35590. + * Redistribution and use in source and binary forms, with or without
  35591. + * modification, are permitted provided that the following conditions are met:
  35592. + * * Redistributions of source code must retain the above copyright
  35593. + * notice, this list of conditions and the following disclaimer.
  35594. + * * Redistributions in binary form must reproduce the above copyright
  35595. + * notice, this list of conditions and the following disclaimer in the
  35596. + * documentation and/or other materials provided with the distribution.
  35597. + * * Neither the name of Raspberry Pi nor the
  35598. + * names of its contributors may be used to endorse or promote products
  35599. + * derived from this software without specific prior written permission.
  35600. + *
  35601. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  35602. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  35603. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35604. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  35605. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35606. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35607. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35608. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35609. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35610. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35611. + *
  35612. + * This FIQ implements functionality that performs split transactions on
  35613. + * the dwc_otg hardware without any outside intervention. A split transaction
  35614. + * is "queued" by nominating a specific host channel to perform the entirety
  35615. + * of a split transaction. This FIQ will then perform the microframe-precise
  35616. + * scheduling required in each phase of the transaction until completion.
  35617. + *
  35618. + * The FIQ functionality has been surgically implanted into the Synopsys
  35619. + * vendor-provided driver.
  35620. + *
  35621. + */
  35622. +
  35623. +#ifndef DWC_OTG_FIQ_FSM_H_
  35624. +#define DWC_OTG_FIQ_FSM_H_
  35625. +
  35626. +#include "dwc_otg_regs.h"
  35627. +#include "dwc_otg_cil.h"
  35628. +#include "dwc_otg_hcd.h"
  35629. +#include <linux/kernel.h>
  35630. +#include <linux/irqflags.h>
  35631. +#include <linux/string.h>
  35632. +#include <asm/barrier.h>
  35633. +
  35634. +#if 0
  35635. +#define FLAME_ON(x) \
  35636. +do { \
  35637. + int gpioreg; \
  35638. + \
  35639. + gpioreg = readl(__io_address(0x20200000+0x8)); \
  35640. + gpioreg &= ~(7 << (x-20)*3); \
  35641. + gpioreg |= 0x1 << (x-20)*3; \
  35642. + writel(gpioreg, __io_address(0x20200000+0x8)); \
  35643. + \
  35644. + writel(1<<x, __io_address(0x20200000+(0x1C))); \
  35645. +} while (0)
  35646. +
  35647. +#define FLAME_OFF(x) \
  35648. +do { \
  35649. + writel(1<<x, __io_address(0x20200000+(0x28))); \
  35650. +} while (0)
  35651. +#else
  35652. +#define FLAME_ON(x) do { } while (0)
  35653. +#define FLAME_OFF(X) do { } while (0)
  35654. +#endif
  35655. +
  35656. +/* This is a quick-and-dirty arch-specific register read/write. We know that
  35657. + * writes to a peripheral on BCM2835 will always arrive in-order, also that
  35658. + * reads and writes are executed in-order therefore the need for memory barriers
  35659. + * is obviated if we're only talking to USB.
  35660. + */
  35661. +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
  35662. +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
  35663. +
  35664. +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
  35665. +#define GINTSTS 0x014
  35666. +#define GINTMSK 0x018
  35667. +/* Debug register. Poll the top of the received packets FIFO. */
  35668. +#define GRXSTSR 0x01C
  35669. +#define HFNUM 0x408
  35670. +#define HAINT 0x414
  35671. +#define HAINTMSK 0x418
  35672. +#define HPRT0 0x440
  35673. +
  35674. +/* HC_regs start from an offset of 0x500 */
  35675. +#define HC_START 0x500
  35676. +#define HC_OFFSET 0x020
  35677. +
  35678. +#define HC_DMA 0x514
  35679. +
  35680. +#define HCCHAR 0x00
  35681. +#define HCSPLT 0x04
  35682. +#define HCINT 0x08
  35683. +#define HCINTMSK 0x0C
  35684. +#define HCTSIZ 0x10
  35685. +
  35686. +#define ISOC_XACTPOS_ALL 0b11
  35687. +#define ISOC_XACTPOS_BEGIN 0b10
  35688. +#define ISOC_XACTPOS_MID 0b00
  35689. +#define ISOC_XACTPOS_END 0b01
  35690. +
  35691. +#define DWC_PID_DATA2 0b01
  35692. +#define DWC_PID_MDATA 0b11
  35693. +#define DWC_PID_DATA1 0b10
  35694. +#define DWC_PID_DATA0 0b00
  35695. +
  35696. +typedef struct {
  35697. + volatile void* base;
  35698. + volatile void* ctrl;
  35699. + volatile void* outdda;
  35700. + volatile void* outddb;
  35701. + volatile void* intstat;
  35702. +} mphi_regs_t;
  35703. +
  35704. +enum fiq_debug_level {
  35705. + FIQDBG_SCHED = (1 << 0),
  35706. + FIQDBG_INT = (1 << 1),
  35707. + FIQDBG_ERR = (1 << 2),
  35708. + FIQDBG_PORTHUB = (1 << 3),
  35709. +};
  35710. +
  35711. +typedef struct {
  35712. + union {
  35713. + uint32_t slock;
  35714. + struct _tickets {
  35715. + uint16_t owner;
  35716. + uint16_t next;
  35717. + } tickets;
  35718. + };
  35719. +} fiq_lock_t;
  35720. +
  35721. +struct fiq_state;
  35722. +
  35723. +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
  35724. +#if 0
  35725. +#define fiq_print _fiq_print
  35726. +#else
  35727. +#define fiq_print(x, y, ...)
  35728. +#endif
  35729. +
  35730. +extern bool fiq_enable, fiq_fsm_enable;
  35731. +extern ushort nak_holdoff;
  35732. +
  35733. +/**
  35734. + * enum fiq_fsm_state - The FIQ FSM states.
  35735. + *
  35736. + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
  35737. + * USB2.0 specification for host responses to various transaction states.
  35738. + * There are modifications to this host state machine because of a variety of
  35739. + * quirks and limitations in the dwc_otg hardware.
  35740. + *
  35741. + * The fsm state is also used to communicate back to the driver on completion of
  35742. + * a split transaction. The end states are used in conjunction with the interrupts
  35743. + * raised by the final transaction.
  35744. + */
  35745. +enum fiq_fsm_state {
  35746. + /* FIQ isn't enabled for this host channel */
  35747. + FIQ_PASSTHROUGH = 0,
  35748. + /* For the first interrupt received for this channel,
  35749. + * the FIQ has to ack any interrupts indicating success. */
  35750. + FIQ_PASSTHROUGH_ERRORSTATE = 31,
  35751. + /* Nonperiodic state groups */
  35752. + FIQ_NP_SSPLIT_STARTED = 1,
  35753. + FIQ_NP_SSPLIT_RETRY = 2,
  35754. + FIQ_NP_OUT_CSPLIT_RETRY = 3,
  35755. + FIQ_NP_IN_CSPLIT_RETRY = 4,
  35756. + FIQ_NP_SPLIT_DONE = 5,
  35757. + FIQ_NP_SPLIT_LS_ABORTED = 6,
  35758. + /* This differentiates a HS transaction error from a LS one
  35759. + * (handling the hub state is different) */
  35760. + FIQ_NP_SPLIT_HS_ABORTED = 7,
  35761. +
  35762. + /* Periodic state groups */
  35763. + /* Periodic transactions are either started directly by the IRQ handler
  35764. + * or deferred if the TT is already in use.
  35765. + */
  35766. + FIQ_PER_SSPLIT_QUEUED = 8,
  35767. + FIQ_PER_SSPLIT_STARTED = 9,
  35768. + FIQ_PER_SSPLIT_LAST = 10,
  35769. +
  35770. +
  35771. + FIQ_PER_ISO_OUT_PENDING = 11,
  35772. + FIQ_PER_ISO_OUT_ACTIVE = 12,
  35773. + FIQ_PER_ISO_OUT_LAST = 13,
  35774. + FIQ_PER_ISO_OUT_DONE = 27,
  35775. +
  35776. + FIQ_PER_CSPLIT_WAIT = 14,
  35777. + FIQ_PER_CSPLIT_NYET1 = 15,
  35778. + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
  35779. + FIQ_PER_CSPLIT_NYET_FAFF = 29,
  35780. + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
  35781. + FIQ_PER_CSPLIT_POLL = 16,
  35782. + /* The last CSPLIT for a transaction has been issued, differentiates
  35783. + * for the state machine to queue the next packet.
  35784. + */
  35785. + FIQ_PER_CSPLIT_LAST = 17,
  35786. +
  35787. + FIQ_PER_SPLIT_DONE = 18,
  35788. + FIQ_PER_SPLIT_LS_ABORTED = 19,
  35789. + FIQ_PER_SPLIT_HS_ABORTED = 20,
  35790. + FIQ_PER_SPLIT_NYET_ABORTED = 21,
  35791. + /* Frame rollover has occurred without the transaction finishing. */
  35792. + FIQ_PER_SPLIT_TIMEOUT = 22,
  35793. +
  35794. + /* FIQ-accelerated HS Isochronous state groups */
  35795. + FIQ_HS_ISOC_TURBO = 23,
  35796. + /* For interval > 1, SOF wakes up the isochronous FSM */
  35797. + FIQ_HS_ISOC_SLEEPING = 24,
  35798. + FIQ_HS_ISOC_DONE = 25,
  35799. + FIQ_HS_ISOC_ABORTED = 26,
  35800. + FIQ_DEQUEUE_ISSUED = 30,
  35801. + FIQ_TEST = 32,
  35802. +};
  35803. +
  35804. +struct fiq_stack {
  35805. + int magic1;
  35806. + uint8_t stack[2048];
  35807. + int magic2;
  35808. +};
  35809. +
  35810. +
  35811. +/**
  35812. + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
  35813. + * @index: Number of slots reported used for IN transactions / number of slots
  35814. + * transmitted for an OUT transaction
  35815. + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
  35816. + *
  35817. + * Split transaction transfers can have variable length depending on other bus
  35818. + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
  35819. + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
  35820. + * can happen per-frame.
  35821. + */
  35822. +struct fiq_dma_info {
  35823. + u8 index;
  35824. + u8 slot_len[6];
  35825. +};
  35826. +
  35827. +struct __attribute__((packed)) fiq_split_dma_slot {
  35828. + u8 buf[188];
  35829. +};
  35830. +
  35831. +struct fiq_dma_channel {
  35832. + struct __attribute__((packed)) fiq_split_dma_slot index[6];
  35833. +};
  35834. +
  35835. +struct fiq_dma_blob {
  35836. + struct __attribute__((packed)) fiq_dma_channel channel[0];
  35837. +};
  35838. +
  35839. +/**
  35840. + * struct fiq_hs_isoc_info - USB2.0 isochronous data
  35841. + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
  35842. + * @nrframes: Total length of iso_frame_desc array
  35843. + * @index: Current index (FIQ-maintained)
  35844. + * @stride: Interval in uframes between HS isoc transactions
  35845. + */
  35846. +struct fiq_hs_isoc_info {
  35847. + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
  35848. + unsigned int nrframes;
  35849. + unsigned int index;
  35850. + unsigned int stride;
  35851. +};
  35852. +
  35853. +/**
  35854. + * struct fiq_channel_state - FIQ state machine storage
  35855. + * @fsm: Current state of the channel as understood by the FIQ
  35856. + * @nr_errors: Number of transaction errors on this split-transaction
  35857. + * @hub_addr: SSPLIT/CSPLIT destination hub
  35858. + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
  35859. + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
  35860. + * split-IN, number of CSPLIT data packets that were received.
  35861. + * @hcchar_copy:
  35862. + * @hcsplt_copy:
  35863. + * @hcintmsk_copy:
  35864. + * @hctsiz_copy: Copies of the host channel registers.
  35865. + * For use as scratch, or for returning state.
  35866. + *
  35867. + * The fiq_channel_state is state storage between interrupts for a host channel. The
  35868. + * FSM state is stored here. Members of this structure must only be set up by the
  35869. + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
  35870. + * has updated the state to either a COMPLETE state group or ABORT state group.
  35871. + */
  35872. +
  35873. +struct fiq_channel_state {
  35874. + enum fiq_fsm_state fsm;
  35875. + unsigned int nr_errors;
  35876. + unsigned int hub_addr;
  35877. + unsigned int port_addr;
  35878. + /* Hardware bug workaround: sometimes channel halt interrupts are
  35879. + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
  35880. + unsigned int expected_uframe;
  35881. + /* number of uframes remaining (for interval > 1 HS isoc transfers) before next transfer */
  35882. + unsigned int uframe_sleeps;
  35883. + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
  35884. + unsigned int nrpackets;
  35885. + struct fiq_dma_info dma_info;
  35886. + struct fiq_hs_isoc_info hs_isoc_info;
  35887. + /* Copies of HC registers - in/out communication from/to IRQ handler
  35888. + * and for ease of channel setup. A bit of mungeing is performed - for
  35889. + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
  35890. + */
  35891. + hcchar_data_t hcchar_copy;
  35892. + hcsplt_data_t hcsplt_copy;
  35893. + hcint_data_t hcint_copy;
  35894. + hcintmsk_data_t hcintmsk_copy;
  35895. + hctsiz_data_t hctsiz_copy;
  35896. + hcdma_data_t hcdma_copy;
  35897. +};
  35898. +
  35899. +/**
  35900. + * struct fiq_state - top-level FIQ state machine storage
  35901. + * @mphi_regs: virtual address of the MPHI peripheral register file
  35902. + * @dwc_regs_base: virtual address of the base of the DWC core register file
  35903. + * @dma_base: physical address for the base of the DMA bounce buffers
  35904. + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
  35905. + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
  35906. + * Used for determining which interrupts fired to set off the IRQ handler.
  35907. + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
  35908. + * @np_count: Non-periodic transactions in the active queue
  35909. + * @np_sent: Count of non-periodic transactions that have completed
  35910. + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
  35911. + * this is the next frame on which a SOF interrupt is required. Used to hold off
  35912. + * passing SOF through to the driver until necessary.
  35913. + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
  35914. + * channels configured into the core logic.
  35915. + *
  35916. + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
  35917. + * It contains top-level state information.
  35918. + */
  35919. +struct fiq_state {
  35920. + fiq_lock_t lock;
  35921. + mphi_regs_t mphi_regs;
  35922. + void *dwc_regs_base;
  35923. + dma_addr_t dma_base;
  35924. + struct fiq_dma_blob *fiq_dmab;
  35925. + void *dummy_send;
  35926. + gintmsk_data_t gintmsk_saved;
  35927. + haintmsk_data_t haintmsk_saved;
  35928. + int mphi_int_count;
  35929. + unsigned int fiq_done;
  35930. + unsigned int kick_np_queues;
  35931. + unsigned int next_sched_frame;
  35932. +#ifdef FIQ_DEBUG
  35933. + char * buffer;
  35934. + unsigned int bufsiz;
  35935. +#endif
  35936. + struct fiq_channel_state channel[0];
  35937. +};
  35938. +
  35939. +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
  35940. +
  35941. +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
  35942. +
  35943. +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
  35944. +
  35945. +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
  35946. +
  35947. +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
  35948. +
  35949. +extern void dwc_otg_fiq_nop(struct fiq_state *state);
  35950. +
  35951. +#endif /* DWC_OTG_FIQ_FSM_H_ */
  35952. --- /dev/null
  35953. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  35954. @@ -0,0 +1,80 @@
  35955. +/*
  35956. + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
  35957. + *
  35958. + * Copyright (c) 2013 Raspberry Pi Foundation
  35959. + *
  35960. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  35961. + * All rights reserved.
  35962. + *
  35963. + * Redistribution and use in source and binary forms, with or without
  35964. + * modification, are permitted provided that the following conditions are met:
  35965. + * * Redistributions of source code must retain the above copyright
  35966. + * notice, this list of conditions and the following disclaimer.
  35967. + * * Redistributions in binary form must reproduce the above copyright
  35968. + * notice, this list of conditions and the following disclaimer in the
  35969. + * documentation and/or other materials provided with the distribution.
  35970. + * * Neither the name of Raspberry Pi nor the
  35971. + * names of its contributors may be used to endorse or promote products
  35972. + * derived from this software without specific prior written permission.
  35973. + *
  35974. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  35975. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  35976. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35977. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  35978. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35979. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35980. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35981. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35982. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35983. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35984. + */
  35985. +
  35986. +
  35987. +#include <asm/assembler.h>
  35988. +#include <linux/linkage.h>
  35989. +
  35990. +
  35991. +.text
  35992. +
  35993. +.global _dwc_otg_fiq_stub_end;
  35994. +
  35995. +/**
  35996. + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
  35997. + * a C-style function call with arguments from the FIQ banked registers.
  35998. + * r0 = &hcd->fiq_state
  35999. + * r1 = &hcd->num_channels
  36000. + * r2 = &hcd->dma_buffers
  36001. + * Tramples: r0, r1, r2, r4, fp, ip
  36002. + */
  36003. +
  36004. +ENTRY(_dwc_otg_fiq_stub)
  36005. + /* Stash unbanked regs - SP will have been set up for us */
  36006. + mov ip, sp;
  36007. + stmdb sp!, {r0-r12, lr};
  36008. +#ifdef FIQ_DEBUG
  36009. + // Cycle profiling - read cycle counter at start
  36010. + mrc p15, 0, r5, c15, c12, 1;
  36011. +#endif
  36012. + /* r11 = fp, don't trample it */
  36013. + mov r4, fp;
  36014. + /* set EABI frame size */
  36015. + sub fp, ip, #512;
  36016. +
  36017. + /* for fiq NOP mode - just need state */
  36018. + mov r0, r8;
  36019. + /* r9 = num_channels */
  36020. + mov r1, r9;
  36021. + /* r10 = struct *dma_bufs */
  36022. +// mov r2, r10;
  36023. +
  36024. + /* r4 = &fiq_c_function */
  36025. + blx r4;
  36026. +#ifdef FIQ_DEBUG
  36027. + mrc p15, 0, r4, c15, c12, 1;
  36028. + subs r5, r5, r4;
  36029. + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
  36030. +#endif
  36031. + ldmia sp!, {r0-r12, lr};
  36032. + subs pc, lr, #4;
  36033. +_dwc_otg_fiq_stub_end:
  36034. +END(_dwc_otg_fiq_stub)
  36035. --- /dev/null
  36036. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  36037. @@ -0,0 +1,4257 @@
  36038. +
  36039. +/* ==========================================================================
  36040. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  36041. + * $Revision: #104 $
  36042. + * $Date: 2011/10/24 $
  36043. + * $Change: 1871159 $
  36044. + *
  36045. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  36046. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  36047. + * otherwise expressly agreed to in writing between Synopsys and you.
  36048. + *
  36049. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  36050. + * any End User Software License Agreement or Agreement for Licensed Product
  36051. + * with Synopsys or any supplement thereto. You are permitted to use and
  36052. + * redistribute this Software in source and binary forms, with or without
  36053. + * modification, provided that redistributions of source code must retain this
  36054. + * notice. You may not view, use, disclose, copy or distribute this file or
  36055. + * any information contained herein except pursuant to this license grant from
  36056. + * Synopsys. If you do not agree with this notice, including the disclaimer
  36057. + * below, then you are not authorized to use the Software.
  36058. + *
  36059. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  36060. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36061. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36062. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  36063. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36064. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36065. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36066. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36067. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36068. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  36069. + * DAMAGE.
  36070. + * ========================================================================== */
  36071. +#ifndef DWC_DEVICE_ONLY
  36072. +
  36073. +/** @file
  36074. + * This file implements HCD Core. All code in this file is portable and doesn't
  36075. + * use any OS specific functions.
  36076. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  36077. + * header file.
  36078. + */
  36079. +
  36080. +#include <linux/usb.h>
  36081. +#include <linux/usb/hcd.h>
  36082. +
  36083. +#include "dwc_otg_hcd.h"
  36084. +#include "dwc_otg_regs.h"
  36085. +#include "dwc_otg_fiq_fsm.h"
  36086. +
  36087. +extern bool microframe_schedule;
  36088. +extern uint16_t fiq_fsm_mask, nak_holdoff;
  36089. +
  36090. +//#define DEBUG_HOST_CHANNELS
  36091. +#ifdef DEBUG_HOST_CHANNELS
  36092. +static int last_sel_trans_num_per_scheduled = 0;
  36093. +static int last_sel_trans_num_nonper_scheduled = 0;
  36094. +static int last_sel_trans_num_avail_hc_at_start = 0;
  36095. +static int last_sel_trans_num_avail_hc_at_end = 0;
  36096. +#endif /* DEBUG_HOST_CHANNELS */
  36097. +
  36098. +
  36099. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  36100. +{
  36101. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  36102. +}
  36103. +
  36104. +/**
  36105. + * Connection timeout function. An OTG host is required to display a
  36106. + * message if the device does not connect within 10 seconds.
  36107. + */
  36108. +void dwc_otg_hcd_connect_timeout(void *ptr)
  36109. +{
  36110. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  36111. + DWC_PRINTF("Connect Timeout\n");
  36112. + __DWC_ERROR("Device Not Connected/Responding\n");
  36113. +}
  36114. +
  36115. +#if defined(DEBUG)
  36116. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  36117. +{
  36118. + if (qh->channel != NULL) {
  36119. + dwc_hc_t *hc = qh->channel;
  36120. + dwc_list_link_t *item;
  36121. + dwc_otg_qh_t *qh_item;
  36122. + int num_channels = hcd->core_if->core_params->host_channels;
  36123. + int i;
  36124. +
  36125. + dwc_otg_hc_regs_t *hc_regs;
  36126. + hcchar_data_t hcchar;
  36127. + hcsplt_data_t hcsplt;
  36128. + hctsiz_data_t hctsiz;
  36129. + uint32_t hcdma;
  36130. +
  36131. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  36132. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  36133. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  36134. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  36135. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  36136. +
  36137. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  36138. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  36139. + hcsplt.d32);
  36140. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  36141. + hcdma);
  36142. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  36143. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  36144. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  36145. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  36146. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  36147. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  36148. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  36149. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  36150. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  36151. + DWC_PRINTF(" qh: %p\n", hc->qh);
  36152. + DWC_PRINTF(" NP inactive sched:\n");
  36153. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  36154. + qh_item =
  36155. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36156. + DWC_PRINTF(" %p\n", qh_item);
  36157. + }
  36158. + DWC_PRINTF(" NP active sched:\n");
  36159. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  36160. + qh_item =
  36161. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36162. + DWC_PRINTF(" %p\n", qh_item);
  36163. + }
  36164. + DWC_PRINTF(" Channels: \n");
  36165. + for (i = 0; i < num_channels; i++) {
  36166. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  36167. + DWC_PRINTF(" %2d: %p\n", i, hc);
  36168. + }
  36169. + }
  36170. +}
  36171. +#else
  36172. +#define dump_channel_info(hcd, qh)
  36173. +#endif /* DEBUG */
  36174. +
  36175. +/**
  36176. + * Work queue function for starting the HCD when A-Cable is connected.
  36177. + * The hcd_start() must be called in a process context.
  36178. + */
  36179. +static void hcd_start_func(void *_vp)
  36180. +{
  36181. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  36182. +
  36183. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  36184. + if (hcd) {
  36185. + hcd->fops->start(hcd);
  36186. + }
  36187. +}
  36188. +
  36189. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  36190. +{
  36191. +#ifdef DEBUG
  36192. + int i;
  36193. + int num_channels = hcd->core_if->core_params->host_channels;
  36194. + for (i = 0; i < num_channels; i++) {
  36195. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  36196. + }
  36197. +#endif
  36198. +}
  36199. +
  36200. +static void del_timers(dwc_otg_hcd_t * hcd)
  36201. +{
  36202. + del_xfer_timers(hcd);
  36203. + DWC_TIMER_CANCEL(hcd->conn_timer);
  36204. +}
  36205. +
  36206. +/**
  36207. + * Processes all the URBs in a single list of QHs. Completes them with
  36208. + * -ESHUTDOWN and frees the QTD.
  36209. + */
  36210. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  36211. +{
  36212. + dwc_list_link_t *qh_item, *qh_tmp;
  36213. + dwc_otg_qh_t *qh;
  36214. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  36215. +
  36216. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  36217. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  36218. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  36219. + &qh->qtd_list, qtd_list_entry) {
  36220. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  36221. + if (qtd->urb != NULL) {
  36222. + hcd->fops->complete(hcd, qtd->urb->priv,
  36223. + qtd->urb, -DWC_E_SHUTDOWN);
  36224. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  36225. + }
  36226. +
  36227. + }
  36228. + if(qh->channel) {
  36229. + /* Using hcchar.chen == 1 is not a reliable test.
  36230. + * It is possible that the channel has already halted
  36231. + * but not yet been through the IRQ handler.
  36232. + */
  36233. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  36234. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  36235. + if(microframe_schedule)
  36236. + hcd->available_host_channels++;
  36237. + qh->channel = NULL;
  36238. + }
  36239. + dwc_otg_hcd_qh_remove(hcd, qh);
  36240. + }
  36241. +}
  36242. +
  36243. +/**
  36244. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  36245. + * and periodic schedules. The QTD associated with each URB is removed from
  36246. + * the schedule and freed. This function may be called when a disconnect is
  36247. + * detected or when the HCD is being stopped.
  36248. + */
  36249. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  36250. +{
  36251. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  36252. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  36253. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  36254. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  36255. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  36256. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  36257. +}
  36258. +
  36259. +/**
  36260. + * Start the connection timer. An OTG host is required to display a
  36261. + * message if the device does not connect within 10 seconds. The
  36262. + * timer is deleted if a port connect interrupt occurs before the
  36263. + * timer expires.
  36264. + */
  36265. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  36266. +{
  36267. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  36268. +}
  36269. +
  36270. +/**
  36271. + * HCD Callback function for disconnect of the HCD.
  36272. + *
  36273. + * @param p void pointer to the <code>struct usb_hcd</code>
  36274. + */
  36275. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  36276. +{
  36277. + dwc_otg_hcd_t *dwc_otg_hcd;
  36278. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  36279. + dwc_otg_hcd = p;
  36280. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  36281. + return 1;
  36282. +}
  36283. +
  36284. +/**
  36285. + * HCD Callback function for starting the HCD when A-Cable is
  36286. + * connected.
  36287. + *
  36288. + * @param p void pointer to the <code>struct usb_hcd</code>
  36289. + */
  36290. +static int32_t dwc_otg_hcd_start_cb(void *p)
  36291. +{
  36292. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36293. + dwc_otg_core_if_t *core_if;
  36294. + hprt0_data_t hprt0;
  36295. +
  36296. + core_if = dwc_otg_hcd->core_if;
  36297. +
  36298. + if (core_if->op_state == B_HOST) {
  36299. + /*
  36300. + * Reset the port. During a HNP mode switch the reset
  36301. + * needs to occur within 1ms and have a duration of at
  36302. + * least 50ms.
  36303. + */
  36304. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  36305. + hprt0.b.prtrst = 1;
  36306. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36307. + }
  36308. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  36309. + hcd_start_func, dwc_otg_hcd, 50,
  36310. + "start hcd");
  36311. +
  36312. + return 1;
  36313. +}
  36314. +
  36315. +/**
  36316. + * HCD Callback function for disconnect of the HCD.
  36317. + *
  36318. + * @param p void pointer to the <code>struct usb_hcd</code>
  36319. + */
  36320. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  36321. +{
  36322. + gintsts_data_t intr;
  36323. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36324. +
  36325. + /*
  36326. + * Set status flags for the hub driver.
  36327. + */
  36328. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  36329. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  36330. + if(fiq_enable)
  36331. + local_fiq_disable();
  36332. + /*
  36333. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  36334. + * interrupt mask and status bits and disabling subsequent host
  36335. + * channel interrupts.
  36336. + */
  36337. + intr.d32 = 0;
  36338. + intr.b.nptxfempty = 1;
  36339. + intr.b.ptxfempty = 1;
  36340. + intr.b.hcintr = 1;
  36341. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  36342. + intr.d32, 0);
  36343. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  36344. + intr.d32, 0);
  36345. +
  36346. + del_timers(dwc_otg_hcd);
  36347. +
  36348. + /*
  36349. + * Turn off the vbus power only if the core has transitioned to device
  36350. + * mode. If still in host mode, need to keep power on to detect a
  36351. + * reconnection.
  36352. + */
  36353. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  36354. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  36355. + hprt0_data_t hprt0 = {.d32 = 0 };
  36356. + DWC_PRINTF("Disconnect: PortPower off\n");
  36357. + hprt0.b.prtpwr = 0;
  36358. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  36359. + hprt0.d32);
  36360. + }
  36361. +
  36362. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  36363. + }
  36364. +
  36365. + /* Respond with an error status to all URBs in the schedule. */
  36366. + kill_all_urbs(dwc_otg_hcd);
  36367. +
  36368. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  36369. + /* Clean up any host channels that were in use. */
  36370. + int num_channels;
  36371. + int i;
  36372. + dwc_hc_t *channel;
  36373. + dwc_otg_hc_regs_t *hc_regs;
  36374. + hcchar_data_t hcchar;
  36375. +
  36376. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  36377. +
  36378. + if (!dwc_otg_hcd->core_if->dma_enable) {
  36379. + /* Flush out any channel requests in slave mode. */
  36380. + for (i = 0; i < num_channels; i++) {
  36381. + channel = dwc_otg_hcd->hc_ptr_array[i];
  36382. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  36383. + (channel, hc_list_entry)) {
  36384. + hc_regs =
  36385. + dwc_otg_hcd->core_if->
  36386. + host_if->hc_regs[i];
  36387. + hcchar.d32 =
  36388. + DWC_READ_REG32(&hc_regs->hcchar);
  36389. + if (hcchar.b.chen) {
  36390. + hcchar.b.chen = 0;
  36391. + hcchar.b.chdis = 1;
  36392. + hcchar.b.epdir = 0;
  36393. + DWC_WRITE_REG32
  36394. + (&hc_regs->hcchar,
  36395. + hcchar.d32);
  36396. + }
  36397. + }
  36398. + }
  36399. + }
  36400. +
  36401. + for (i = 0; i < num_channels; i++) {
  36402. + channel = dwc_otg_hcd->hc_ptr_array[i];
  36403. + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
  36404. + hc_regs =
  36405. + dwc_otg_hcd->core_if->host_if->hc_regs[i];
  36406. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  36407. + if (hcchar.b.chen) {
  36408. + /* Halt the channel. */
  36409. + hcchar.b.chdis = 1;
  36410. + DWC_WRITE_REG32(&hc_regs->hcchar,
  36411. + hcchar.d32);
  36412. + }
  36413. +
  36414. + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
  36415. + channel);
  36416. + DWC_CIRCLEQ_INSERT_TAIL
  36417. + (&dwc_otg_hcd->free_hc_list, channel,
  36418. + hc_list_entry);
  36419. + /*
  36420. + * Added for Descriptor DMA to prevent channel double cleanup
  36421. + * in release_channel_ddma(). Which called from ep_disable
  36422. + * when device disconnect.
  36423. + */
  36424. + channel->qh = NULL;
  36425. + }
  36426. + }
  36427. + if(fiq_fsm_enable) {
  36428. + for(i=0; i < 128; i++) {
  36429. + dwc_otg_hcd->hub_port[i] = 0;
  36430. + }
  36431. + }
  36432. +
  36433. + }
  36434. +
  36435. + if(fiq_enable)
  36436. + local_fiq_enable();
  36437. +
  36438. + if (dwc_otg_hcd->fops->disconnect) {
  36439. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  36440. + }
  36441. +
  36442. + return 1;
  36443. +}
  36444. +
  36445. +/**
  36446. + * HCD Callback function for stopping the HCD.
  36447. + *
  36448. + * @param p void pointer to the <code>struct usb_hcd</code>
  36449. + */
  36450. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  36451. +{
  36452. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36453. +
  36454. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  36455. + dwc_otg_hcd_stop(dwc_otg_hcd);
  36456. + return 1;
  36457. +}
  36458. +
  36459. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36460. +/**
  36461. + * HCD Callback function for sleep of HCD.
  36462. + *
  36463. + * @param p void pointer to the <code>struct usb_hcd</code>
  36464. + */
  36465. +static int dwc_otg_hcd_sleep_cb(void *p)
  36466. +{
  36467. + dwc_otg_hcd_t *hcd = p;
  36468. +
  36469. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  36470. +
  36471. + return 0;
  36472. +}
  36473. +#endif
  36474. +
  36475. +
  36476. +/**
  36477. + * HCD Callback function for Remote Wakeup.
  36478. + *
  36479. + * @param p void pointer to the <code>struct usb_hcd</code>
  36480. + */
  36481. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  36482. +{
  36483. + dwc_otg_hcd_t *hcd = p;
  36484. +
  36485. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  36486. + hcd->flags.b.port_suspend_change = 1;
  36487. + }
  36488. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36489. + else {
  36490. + hcd->flags.b.port_l1_change = 1;
  36491. + }
  36492. +#endif
  36493. + return 0;
  36494. +}
  36495. +
  36496. +/**
  36497. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  36498. + * stopped.
  36499. + */
  36500. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  36501. +{
  36502. + hprt0_data_t hprt0 = {.d32 = 0 };
  36503. +
  36504. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  36505. +
  36506. + /*
  36507. + * The root hub should be disconnected before this function is called.
  36508. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  36509. + * and the QH lists (via ..._hcd_endpoint_disable).
  36510. + */
  36511. +
  36512. + /* Turn off all host-specific interrupts. */
  36513. + dwc_otg_disable_host_interrupts(hcd->core_if);
  36514. +
  36515. + /* Turn off the vbus power */
  36516. + DWC_PRINTF("PortPower off\n");
  36517. + hprt0.b.prtpwr = 0;
  36518. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  36519. + dwc_mdelay(1);
  36520. +}
  36521. +
  36522. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  36523. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  36524. + int atomic_alloc)
  36525. +{
  36526. + int retval = 0;
  36527. + uint8_t needs_scheduling = 0;
  36528. + dwc_otg_transaction_type_e tr_type;
  36529. + dwc_otg_qtd_t *qtd;
  36530. + gintmsk_data_t intr_mask = {.d32 = 0 };
  36531. + hprt0_data_t hprt0 = { .d32 = 0 };
  36532. +
  36533. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36534. + if (NULL == hcd->core_if) {
  36535. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  36536. + /* No longer connected. */
  36537. + return -DWC_E_INVALID;
  36538. + }
  36539. +#endif
  36540. + if (!hcd->flags.b.port_connect_status) {
  36541. + /* No longer connected. */
  36542. + DWC_ERROR("Not connected\n");
  36543. + return -DWC_E_NO_DEVICE;
  36544. + }
  36545. +
  36546. + /* Some core configurations cannot support LS traffic on a FS root port */
  36547. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  36548. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  36549. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  36550. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  36551. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  36552. + return -DWC_E_NO_DEVICE;
  36553. + }
  36554. + }
  36555. +
  36556. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  36557. + if (qtd == NULL) {
  36558. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  36559. + return -DWC_E_NO_MEMORY;
  36560. + }
  36561. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36562. + if (qtd->urb == NULL) {
  36563. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  36564. + return -DWC_E_NO_MEMORY;
  36565. + }
  36566. + if (qtd->urb->priv == NULL) {
  36567. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  36568. + return -DWC_E_NO_MEMORY;
  36569. + }
  36570. +#endif
  36571. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  36572. + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
  36573. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  36574. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  36575. + needs_scheduling = 0;
  36576. +
  36577. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  36578. + // creates a new queue in ep_handle if it doesn't exist already
  36579. + if (retval < 0) {
  36580. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  36581. + "Error status %d\n", retval);
  36582. + dwc_otg_hcd_qtd_free(qtd);
  36583. + return retval;
  36584. + }
  36585. +
  36586. + if(needs_scheduling) {
  36587. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  36588. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  36589. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  36590. + }
  36591. + }
  36592. + return retval;
  36593. +}
  36594. +
  36595. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  36596. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  36597. +{
  36598. + dwc_otg_qh_t *qh;
  36599. + dwc_otg_qtd_t *urb_qtd;
  36600. + BUG_ON(!hcd);
  36601. + BUG_ON(!dwc_otg_urb);
  36602. +
  36603. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36604. +
  36605. + if (hcd == NULL) {
  36606. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  36607. + return -DWC_E_INVALID;
  36608. + }
  36609. + if (dwc_otg_urb == NULL) {
  36610. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  36611. + return -DWC_E_INVALID;
  36612. + }
  36613. + if (dwc_otg_urb->qtd == NULL) {
  36614. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  36615. + return -DWC_E_INVALID;
  36616. + }
  36617. + urb_qtd = dwc_otg_urb->qtd;
  36618. + BUG_ON(!urb_qtd);
  36619. + if (urb_qtd->qh == NULL) {
  36620. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  36621. + return -DWC_E_INVALID;
  36622. + }
  36623. +#else
  36624. + urb_qtd = dwc_otg_urb->qtd;
  36625. + BUG_ON(!urb_qtd);
  36626. +#endif
  36627. + qh = urb_qtd->qh;
  36628. + BUG_ON(!qh);
  36629. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  36630. + if (urb_qtd->in_process) {
  36631. + dump_channel_info(hcd, qh);
  36632. + }
  36633. + }
  36634. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36635. + if (hcd->core_if == NULL) {
  36636. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  36637. + return -DWC_E_INVALID;
  36638. + }
  36639. +#endif
  36640. + if (urb_qtd->in_process && qh->channel) {
  36641. + /* The QTD is in process (it has been assigned to a channel). */
  36642. + if (hcd->flags.b.port_connect_status) {
  36643. + int n = qh->channel->hc_num;
  36644. + /*
  36645. + * If still connected (i.e. in host mode), halt the
  36646. + * channel so it can be used for other transfers. If
  36647. + * no longer connected, the host registers can't be
  36648. + * written to halt the channel since the core is in
  36649. + * device mode.
  36650. + */
  36651. + /* In FIQ FSM mode, we need to shut down carefully.
  36652. + * The FIQ may attempt to restart a disabled channel */
  36653. + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
  36654. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  36655. + qh->channel->halt_pending = 1;
  36656. + hcd->fiq_state->channel[n].fsm = FIQ_DEQUEUE_ISSUED;
  36657. + } else {
  36658. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  36659. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  36660. + }
  36661. + }
  36662. + }
  36663. +
  36664. + /*
  36665. + * Free the QTD and clean up the associated QH. Leave the QH in the
  36666. + * schedule if it has any remaining QTDs.
  36667. + */
  36668. +
  36669. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  36670. + "delete %sQueue handler\n",
  36671. + hcd->core_if->dma_desc_enable?"DMA ":"");
  36672. + if (!hcd->core_if->dma_desc_enable) {
  36673. + uint8_t b = urb_qtd->in_process;
  36674. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  36675. + if (b) {
  36676. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  36677. + qh->channel = NULL;
  36678. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  36679. + dwc_otg_hcd_qh_remove(hcd, qh);
  36680. + }
  36681. + } else {
  36682. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  36683. + }
  36684. + return 0;
  36685. +}
  36686. +
  36687. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  36688. + int retry)
  36689. +{
  36690. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  36691. + int retval = 0;
  36692. + dwc_irqflags_t flags;
  36693. +
  36694. + if (retry < 0) {
  36695. + retval = -DWC_E_INVALID;
  36696. + goto done;
  36697. + }
  36698. +
  36699. + if (!qh) {
  36700. + retval = -DWC_E_INVALID;
  36701. + goto done;
  36702. + }
  36703. +
  36704. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36705. +
  36706. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  36707. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36708. + retry--;
  36709. + dwc_msleep(5);
  36710. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36711. + }
  36712. +
  36713. + dwc_otg_hcd_qh_remove(hcd, qh);
  36714. +
  36715. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36716. + /*
  36717. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  36718. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  36719. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  36720. + * and dwc_otg_hcd_frame_list_alloc().
  36721. + */
  36722. + dwc_otg_hcd_qh_free(hcd, qh);
  36723. +
  36724. +done:
  36725. + return retval;
  36726. +}
  36727. +
  36728. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  36729. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  36730. +{
  36731. + int retval = 0;
  36732. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  36733. + if (!qh)
  36734. + return -DWC_E_INVALID;
  36735. +
  36736. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  36737. + return retval;
  36738. +}
  36739. +#endif
  36740. +
  36741. +/**
  36742. + * HCD Callback structure for handling mode switching.
  36743. + */
  36744. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  36745. + .start = dwc_otg_hcd_start_cb,
  36746. + .stop = dwc_otg_hcd_stop_cb,
  36747. + .disconnect = dwc_otg_hcd_disconnect_cb,
  36748. + .session_start = dwc_otg_hcd_session_start_cb,
  36749. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  36750. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36751. + .sleep = dwc_otg_hcd_sleep_cb,
  36752. +#endif
  36753. + .p = 0,
  36754. +};
  36755. +
  36756. +/**
  36757. + * Reset tasklet function
  36758. + */
  36759. +static void reset_tasklet_func(void *data)
  36760. +{
  36761. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  36762. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  36763. + hprt0_data_t hprt0;
  36764. +
  36765. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  36766. +
  36767. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  36768. + hprt0.b.prtrst = 1;
  36769. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36770. + dwc_mdelay(60);
  36771. +
  36772. + hprt0.b.prtrst = 0;
  36773. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36774. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  36775. +}
  36776. +
  36777. +static void completion_tasklet_func(void *ptr)
  36778. +{
  36779. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  36780. + struct urb *urb;
  36781. + urb_tq_entry_t *item;
  36782. + dwc_irqflags_t flags;
  36783. +
  36784. + /* This could just be spin_lock_irq */
  36785. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36786. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  36787. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  36788. + urb = item->urb;
  36789. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  36790. + urb_tq_entries);
  36791. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36792. + DWC_FREE(item);
  36793. +
  36794. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  36795. +
  36796. +
  36797. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36798. + }
  36799. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36800. + return;
  36801. +}
  36802. +
  36803. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  36804. +{
  36805. + dwc_list_link_t *item;
  36806. + dwc_otg_qh_t *qh;
  36807. + dwc_irqflags_t flags;
  36808. +
  36809. + if (!qh_list->next) {
  36810. + /* The list hasn't been initialized yet. */
  36811. + return;
  36812. + }
  36813. + /*
  36814. + * Hold spinlock here. Not needed in that case if bellow
  36815. + * function is being called from ISR
  36816. + */
  36817. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36818. + /* Ensure there are no QTDs or URBs left. */
  36819. + kill_urbs_in_qh_list(hcd, qh_list);
  36820. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36821. +
  36822. + DWC_LIST_FOREACH(item, qh_list) {
  36823. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36824. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  36825. + }
  36826. +}
  36827. +
  36828. +/**
  36829. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  36830. + * Device during SRP time by host power up.
  36831. + */
  36832. +void dwc_otg_hcd_power_up(void *ptr)
  36833. +{
  36834. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  36835. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  36836. +
  36837. + DWC_PRINTF("%s called\n", __FUNCTION__);
  36838. +
  36839. + if (!core_if->hibernation_suspend) {
  36840. + DWC_PRINTF("Already exited from Hibernation\n");
  36841. + return;
  36842. + }
  36843. +
  36844. + /* Switch on the voltage to the core */
  36845. + gpwrdn.b.pwrdnswtch = 1;
  36846. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36847. + dwc_udelay(10);
  36848. +
  36849. + /* Reset the core */
  36850. + gpwrdn.d32 = 0;
  36851. + gpwrdn.b.pwrdnrstn = 1;
  36852. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36853. + dwc_udelay(10);
  36854. +
  36855. + /* Disable power clamps */
  36856. + gpwrdn.d32 = 0;
  36857. + gpwrdn.b.pwrdnclmp = 1;
  36858. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36859. +
  36860. + /* Remove reset the core signal */
  36861. + gpwrdn.d32 = 0;
  36862. + gpwrdn.b.pwrdnrstn = 1;
  36863. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  36864. + dwc_udelay(10);
  36865. +
  36866. + /* Disable PMU interrupt */
  36867. + gpwrdn.d32 = 0;
  36868. + gpwrdn.b.pmuintsel = 1;
  36869. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36870. +
  36871. + core_if->hibernation_suspend = 0;
  36872. +
  36873. + /* Disable PMU */
  36874. + gpwrdn.d32 = 0;
  36875. + gpwrdn.b.pmuactv = 1;
  36876. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36877. + dwc_udelay(10);
  36878. +
  36879. + /* Enable VBUS */
  36880. + gpwrdn.d32 = 0;
  36881. + gpwrdn.b.dis_vbus = 1;
  36882. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36883. +
  36884. + core_if->op_state = A_HOST;
  36885. + dwc_otg_core_init(core_if);
  36886. + dwc_otg_enable_global_interrupts(core_if);
  36887. + cil_hcd_start(core_if);
  36888. +}
  36889. +
  36890. +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
  36891. +{
  36892. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  36893. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  36894. + int i;
  36895. +
  36896. + st->fsm = FIQ_PASSTHROUGH;
  36897. + st->hcchar_copy.d32 = 0;
  36898. + st->hcsplt_copy.d32 = 0;
  36899. + st->hcint_copy.d32 = 0;
  36900. + st->hcintmsk_copy.d32 = 0;
  36901. + st->hctsiz_copy.d32 = 0;
  36902. + st->hcdma_copy.d32 = 0;
  36903. + st->nr_errors = 0;
  36904. + st->hub_addr = 0;
  36905. + st->port_addr = 0;
  36906. + st->expected_uframe = 0;
  36907. + st->nrpackets = 0;
  36908. + st->dma_info.index = 0;
  36909. + for (i = 0; i < 6; i++)
  36910. + st->dma_info.slot_len[i] = 255;
  36911. + st->hs_isoc_info.index = 0;
  36912. + st->hs_isoc_info.iso_desc = NULL;
  36913. + st->hs_isoc_info.nrframes = 0;
  36914. +
  36915. + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
  36916. +}
  36917. +
  36918. +/**
  36919. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  36920. + * in the struct usb_hcd field.
  36921. + */
  36922. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  36923. +{
  36924. + int i;
  36925. +
  36926. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  36927. +
  36928. + del_timers(dwc_otg_hcd);
  36929. +
  36930. + /* Free memory for QH/QTD lists */
  36931. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  36932. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  36933. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  36934. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  36935. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  36936. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  36937. +
  36938. + /* Free memory for the host channels. */
  36939. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  36940. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  36941. +
  36942. +#ifdef DEBUG
  36943. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  36944. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  36945. + }
  36946. +#endif
  36947. + if (hc != NULL) {
  36948. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  36949. + i, hc);
  36950. + DWC_FREE(hc);
  36951. + }
  36952. + }
  36953. +
  36954. + if (dwc_otg_hcd->core_if->dma_enable) {
  36955. + if (dwc_otg_hcd->status_buf_dma) {
  36956. + DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
  36957. + dwc_otg_hcd->status_buf,
  36958. + dwc_otg_hcd->status_buf_dma);
  36959. + }
  36960. + } else if (dwc_otg_hcd->status_buf != NULL) {
  36961. + DWC_FREE(dwc_otg_hcd->status_buf);
  36962. + }
  36963. + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
  36964. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  36965. + /* Set core_if's lock pointer to NULL */
  36966. + dwc_otg_hcd->core_if->lock = NULL;
  36967. +
  36968. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  36969. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  36970. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  36971. + DWC_FREE(dwc_otg_hcd->fiq_state);
  36972. +
  36973. +#ifdef DWC_DEV_SRPCAP
  36974. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  36975. + dwc_otg_hcd->core_if->pwron_timer) {
  36976. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  36977. + }
  36978. +#endif
  36979. + DWC_FREE(dwc_otg_hcd);
  36980. +}
  36981. +
  36982. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  36983. +
  36984. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  36985. +{
  36986. + int retval = 0;
  36987. + int num_channels;
  36988. + int i;
  36989. + dwc_hc_t *channel;
  36990. +
  36991. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  36992. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
  36993. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->channel_lock);
  36994. +#else
  36995. + hcd->lock = DWC_SPINLOCK_ALLOC();
  36996. + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
  36997. +#endif
  36998. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  36999. + hcd, core_if);
  37000. + if (!hcd->lock) {
  37001. + DWC_ERROR("Could not allocate lock for pcd");
  37002. + DWC_FREE(hcd);
  37003. + retval = -DWC_E_NO_MEMORY;
  37004. + goto out;
  37005. + }
  37006. + hcd->core_if = core_if;
  37007. +
  37008. + /* Register the HCD CIL Callbacks */
  37009. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  37010. + &hcd_cil_callbacks, hcd);
  37011. +
  37012. + /* Initialize the non-periodic schedule. */
  37013. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  37014. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  37015. +
  37016. + /* Initialize the periodic schedule. */
  37017. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  37018. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  37019. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  37020. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  37021. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  37022. + /*
  37023. + * Create a host channel descriptor for each host channel implemented
  37024. + * in the controller. Initialize the channel descriptor array.
  37025. + */
  37026. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  37027. + num_channels = hcd->core_if->core_params->host_channels;
  37028. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  37029. + for (i = 0; i < num_channels; i++) {
  37030. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  37031. + if (channel == NULL) {
  37032. + retval = -DWC_E_NO_MEMORY;
  37033. + DWC_ERROR("%s: host channel allocation failed\n",
  37034. + __func__);
  37035. + dwc_otg_hcd_free(hcd);
  37036. + goto out;
  37037. + }
  37038. + channel->hc_num = i;
  37039. + hcd->hc_ptr_array[i] = channel;
  37040. +#ifdef DEBUG
  37041. + hcd->core_if->hc_xfer_timer[i] =
  37042. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  37043. + &hcd->core_if->hc_xfer_info[i]);
  37044. +#endif
  37045. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  37046. + channel);
  37047. + }
  37048. +
  37049. + if (fiq_enable) {
  37050. + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
  37051. + if (!hcd->fiq_state) {
  37052. + retval = -DWC_E_NO_MEMORY;
  37053. + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
  37054. + dwc_otg_hcd_free(hcd);
  37055. + goto out;
  37056. + }
  37057. + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
  37058. +
  37059. + for (i = 0; i < num_channels; i++) {
  37060. + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
  37061. + }
  37062. + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
  37063. +
  37064. + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
  37065. + if (!hcd->fiq_stack) {
  37066. + retval = -DWC_E_NO_MEMORY;
  37067. + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
  37068. + dwc_otg_hcd_free(hcd);
  37069. + goto out;
  37070. + }
  37071. + hcd->fiq_stack->magic1 = 0xDEADBEEF;
  37072. + hcd->fiq_stack->magic2 = 0xD00DFEED;
  37073. + hcd->fiq_state->gintmsk_saved.d32 = ~0;
  37074. + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  37075. +
  37076. + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
  37077. + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
  37078. + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
  37079. + * moderately readable array casts.
  37080. + */
  37081. + hcd->fiq_dmab = DWC_DMA_ALLOC((sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
  37082. + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
  37083. + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
  37084. + sizeof(struct fiq_dma_channel) * num_channels);
  37085. +
  37086. + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
  37087. +
  37088. + /* pointer for debug in fiq_print */
  37089. + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
  37090. + if (fiq_fsm_enable) {
  37091. + int i;
  37092. + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
  37093. + dwc_otg_cleanup_fiq_channel(hcd, i);
  37094. + }
  37095. + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
  37096. + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
  37097. + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
  37098. + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
  37099. + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
  37100. + }
  37101. + }
  37102. +
  37103. + /* Initialize the Connection timeout timer. */
  37104. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  37105. + dwc_otg_hcd_connect_timeout, 0);
  37106. +
  37107. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  37108. + if (microframe_schedule)
  37109. + init_hcd_usecs(hcd);
  37110. +
  37111. + /* Initialize reset tasklet. */
  37112. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  37113. +
  37114. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  37115. + completion_tasklet_func, hcd);
  37116. +#ifdef DWC_DEV_SRPCAP
  37117. + if (hcd->core_if->power_down == 2) {
  37118. + /* Initialize Power on timer for Host power up in case hibernation */
  37119. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  37120. + dwc_otg_hcd_power_up, core_if);
  37121. + }
  37122. +#endif
  37123. +
  37124. + /*
  37125. + * Allocate space for storing data on status transactions. Normally no
  37126. + * data is sent, but this space acts as a bit bucket. This must be
  37127. + * done after usb_add_hcd since that function allocates the DMA buffer
  37128. + * pool.
  37129. + */
  37130. + if (hcd->core_if->dma_enable) {
  37131. + hcd->status_buf =
  37132. + DWC_DMA_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE,
  37133. + &hcd->status_buf_dma);
  37134. + } else {
  37135. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  37136. + }
  37137. + if (!hcd->status_buf) {
  37138. + retval = -DWC_E_NO_MEMORY;
  37139. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  37140. + dwc_otg_hcd_free(hcd);
  37141. + goto out;
  37142. + }
  37143. +
  37144. + hcd->otg_port = 1;
  37145. + hcd->frame_list = NULL;
  37146. + hcd->frame_list_dma = 0;
  37147. + hcd->periodic_qh_count = 0;
  37148. +
  37149. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  37150. +#ifdef FIQ_DEBUG
  37151. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  37152. +#endif
  37153. +
  37154. +out:
  37155. + return retval;
  37156. +}
  37157. +
  37158. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  37159. +{
  37160. + /* Turn off all host-specific interrupts. */
  37161. + dwc_otg_disable_host_interrupts(hcd->core_if);
  37162. +
  37163. + dwc_otg_hcd_free(hcd);
  37164. +}
  37165. +
  37166. +/**
  37167. + * Initializes dynamic portions of the DWC_otg HCD state.
  37168. + */
  37169. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  37170. +{
  37171. + int num_channels;
  37172. + int i;
  37173. + dwc_hc_t *channel;
  37174. + dwc_hc_t *channel_tmp;
  37175. +
  37176. + hcd->flags.d32 = 0;
  37177. +
  37178. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  37179. + if (!microframe_schedule) {
  37180. + hcd->non_periodic_channels = 0;
  37181. + hcd->periodic_channels = 0;
  37182. + } else {
  37183. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  37184. + }
  37185. + /*
  37186. + * Put all channels in the free channel list and clean up channel
  37187. + * states.
  37188. + */
  37189. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  37190. + &hcd->free_hc_list, hc_list_entry) {
  37191. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  37192. + }
  37193. +
  37194. + num_channels = hcd->core_if->core_params->host_channels;
  37195. + for (i = 0; i < num_channels; i++) {
  37196. + channel = hcd->hc_ptr_array[i];
  37197. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  37198. + hc_list_entry);
  37199. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  37200. + }
  37201. +
  37202. + /* Initialize the DWC core for host mode operation. */
  37203. + dwc_otg_core_host_init(hcd->core_if);
  37204. +
  37205. + /* Set core_if's lock pointer to the hcd->lock */
  37206. + hcd->core_if->lock = hcd->lock;
  37207. +}
  37208. +
  37209. +/**
  37210. + * Assigns transactions from a QTD to a free host channel and initializes the
  37211. + * host channel to perform the transactions. The host channel is removed from
  37212. + * the free list.
  37213. + *
  37214. + * @param hcd The HCD state structure.
  37215. + * @param qh Transactions from the first QTD for this QH are selected and
  37216. + * assigned to a free host channel.
  37217. + */
  37218. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  37219. +{
  37220. + dwc_hc_t *hc;
  37221. + dwc_otg_qtd_t *qtd;
  37222. + dwc_otg_hcd_urb_t *urb;
  37223. + void* ptr = NULL;
  37224. + uint32_t intr_enable;
  37225. + unsigned long flags;
  37226. + gintmsk_data_t gintmsk = { .d32 = 0, };
  37227. +
  37228. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37229. +
  37230. + urb = qtd->urb;
  37231. +
  37232. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  37233. +
  37234. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  37235. + urb->actual_length = urb->length;
  37236. +
  37237. +
  37238. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  37239. +
  37240. + /* Remove the host channel from the free list. */
  37241. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  37242. +
  37243. + qh->channel = hc;
  37244. +
  37245. + qtd->in_process = 1;
  37246. +
  37247. + /*
  37248. + * Use usb_pipedevice to determine device address. This address is
  37249. + * 0 before the SET_ADDRESS command and the correct address afterward.
  37250. + */
  37251. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  37252. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  37253. + hc->speed = qh->dev_speed;
  37254. + hc->max_packet = dwc_max_packet(qh->maxp);
  37255. +
  37256. + hc->xfer_started = 0;
  37257. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  37258. + hc->error_state = (qtd->error_count > 0);
  37259. + hc->halt_on_queue = 0;
  37260. + hc->halt_pending = 0;
  37261. + hc->requests = 0;
  37262. +
  37263. + /*
  37264. + * The following values may be modified in the transfer type section
  37265. + * below. The xfer_len value may be reduced when the transfer is
  37266. + * started to accommodate the max widths of the XferSize and PktCnt
  37267. + * fields in the HCTSIZn register.
  37268. + */
  37269. +
  37270. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  37271. + if (hc->ep_is_in) {
  37272. + hc->do_ping = 0;
  37273. + } else {
  37274. + hc->do_ping = qh->ping_state;
  37275. + }
  37276. +
  37277. + hc->data_pid_start = qh->data_toggle;
  37278. + hc->multi_count = 1;
  37279. +
  37280. + if (hcd->core_if->dma_enable) {
  37281. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  37282. +
  37283. + /* For non-dword aligned case */
  37284. + if (((unsigned long)hc->xfer_buff & 0x3)
  37285. + && !hcd->core_if->dma_desc_enable) {
  37286. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  37287. + }
  37288. + } else {
  37289. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  37290. + }
  37291. + hc->xfer_len = urb->length - urb->actual_length;
  37292. + hc->xfer_count = 0;
  37293. +
  37294. + /*
  37295. + * Set the split attributes
  37296. + */
  37297. + hc->do_split = 0;
  37298. + if (qh->do_split) {
  37299. + uint32_t hub_addr, port_addr;
  37300. + hc->do_split = 1;
  37301. + hc->xact_pos = qtd->isoc_split_pos;
  37302. + /* We don't need to do complete splits anymore */
  37303. +// if(fiq_fsm_enable)
  37304. + if (0)
  37305. + hc->complete_split = qtd->complete_split = 0;
  37306. + else
  37307. + hc->complete_split = qtd->complete_split;
  37308. +
  37309. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  37310. + hc->hub_addr = (uint8_t) hub_addr;
  37311. + hc->port_addr = (uint8_t) port_addr;
  37312. + }
  37313. +
  37314. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  37315. + case UE_CONTROL:
  37316. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  37317. + switch (qtd->control_phase) {
  37318. + case DWC_OTG_CONTROL_SETUP:
  37319. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  37320. + hc->do_ping = 0;
  37321. + hc->ep_is_in = 0;
  37322. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  37323. + if (hcd->core_if->dma_enable) {
  37324. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  37325. + } else {
  37326. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  37327. + }
  37328. + hc->xfer_len = 8;
  37329. + ptr = NULL;
  37330. + break;
  37331. + case DWC_OTG_CONTROL_DATA:
  37332. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  37333. + hc->data_pid_start = qtd->data_toggle;
  37334. + break;
  37335. + case DWC_OTG_CONTROL_STATUS:
  37336. + /*
  37337. + * Direction is opposite of data direction or IN if no
  37338. + * data.
  37339. + */
  37340. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  37341. + if (urb->length == 0) {
  37342. + hc->ep_is_in = 1;
  37343. + } else {
  37344. + hc->ep_is_in =
  37345. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  37346. + }
  37347. + if (hc->ep_is_in) {
  37348. + hc->do_ping = 0;
  37349. + }
  37350. +
  37351. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  37352. +
  37353. + hc->xfer_len = 0;
  37354. + if (hcd->core_if->dma_enable) {
  37355. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  37356. + } else {
  37357. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  37358. + }
  37359. + ptr = NULL;
  37360. + break;
  37361. + }
  37362. + break;
  37363. + case UE_BULK:
  37364. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  37365. + break;
  37366. + case UE_INTERRUPT:
  37367. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  37368. + break;
  37369. + case UE_ISOCHRONOUS:
  37370. + {
  37371. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  37372. +
  37373. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  37374. +
  37375. + if (hcd->core_if->dma_desc_enable)
  37376. + break;
  37377. +
  37378. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  37379. +
  37380. + frame_desc->status = 0;
  37381. +
  37382. + if (hcd->core_if->dma_enable) {
  37383. + hc->xfer_buff = (uint8_t *) urb->dma;
  37384. + } else {
  37385. + hc->xfer_buff = (uint8_t *) urb->buf;
  37386. + }
  37387. + hc->xfer_buff +=
  37388. + frame_desc->offset + qtd->isoc_split_offset;
  37389. + hc->xfer_len =
  37390. + frame_desc->length - qtd->isoc_split_offset;
  37391. +
  37392. + /* For non-dword aligned buffers */
  37393. + if (((unsigned long)hc->xfer_buff & 0x3)
  37394. + && hcd->core_if->dma_enable) {
  37395. + ptr =
  37396. + (uint8_t *) urb->buf + frame_desc->offset +
  37397. + qtd->isoc_split_offset;
  37398. + } else
  37399. + ptr = NULL;
  37400. +
  37401. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  37402. + if (hc->xfer_len <= 188) {
  37403. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  37404. + } else {
  37405. + hc->xact_pos =
  37406. + DWC_HCSPLIT_XACTPOS_BEGIN;
  37407. + }
  37408. + }
  37409. + }
  37410. + break;
  37411. + }
  37412. + /* non DWORD-aligned buffer case */
  37413. + if (ptr) {
  37414. + uint32_t buf_size;
  37415. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  37416. + buf_size = hcd->core_if->core_params->max_transfer_size;
  37417. + } else {
  37418. + buf_size = 4096;
  37419. + }
  37420. + if (!qh->dw_align_buf) {
  37421. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
  37422. + &qh->dw_align_buf_dma);
  37423. + if (!qh->dw_align_buf) {
  37424. + DWC_ERROR
  37425. + ("%s: Failed to allocate memory to handle "
  37426. + "non-dword aligned buffer case\n",
  37427. + __func__);
  37428. + return;
  37429. + }
  37430. + }
  37431. + if (!hc->ep_is_in) {
  37432. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  37433. + }
  37434. + hc->align_buff = qh->dw_align_buf_dma;
  37435. + } else {
  37436. + hc->align_buff = 0;
  37437. + }
  37438. +
  37439. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  37440. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  37441. + /*
  37442. + * This value may be modified when the transfer is started to
  37443. + * reflect the actual transfer length.
  37444. + */
  37445. + hc->multi_count = dwc_hb_mult(qh->maxp);
  37446. + }
  37447. +
  37448. + if (hcd->core_if->dma_desc_enable)
  37449. + hc->desc_list_addr = qh->desc_list_dma;
  37450. +
  37451. + dwc_otg_hc_init(hcd->core_if, hc);
  37452. +
  37453. + local_irq_save(flags);
  37454. +
  37455. + if (fiq_enable) {
  37456. + local_fiq_disable();
  37457. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37458. + }
  37459. +
  37460. + /* Enable the top level host channel interrupt. */
  37461. + intr_enable = (1 << hc->hc_num);
  37462. + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
  37463. +
  37464. + /* Make sure host channel interrupts are enabled. */
  37465. + gintmsk.b.hcintr = 1;
  37466. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  37467. +
  37468. + if (fiq_enable) {
  37469. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37470. + local_fiq_enable();
  37471. + }
  37472. +
  37473. + local_irq_restore(flags);
  37474. + hc->qh = qh;
  37475. +}
  37476. +
  37477. +
  37478. +/**
  37479. + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
  37480. + * @qh: pointer to the endpoint's queue head
  37481. + *
  37482. + * Transaction start/end control flow is grafted onto the existing dwc_otg
  37483. + * mechanisms, to avoid spaghettifying the functions more than they already are.
  37484. + * This function's eligibility check is altered by debug parameter.
  37485. + *
  37486. + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
  37487. + */
  37488. +
  37489. +int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh)
  37490. +{
  37491. + if (qh->do_split) {
  37492. + switch (qh->ep_type) {
  37493. + case UE_CONTROL:
  37494. + case UE_BULK:
  37495. + if (fiq_fsm_mask & (1 << 0))
  37496. + return 1;
  37497. + break;
  37498. + case UE_INTERRUPT:
  37499. + case UE_ISOCHRONOUS:
  37500. + if (fiq_fsm_mask & (1 << 1))
  37501. + return 1;
  37502. + break;
  37503. + default:
  37504. + break;
  37505. + }
  37506. + } else if (qh->ep_type == UE_ISOCHRONOUS) {
  37507. + if (fiq_fsm_mask & (1 << 2)) {
  37508. + /* HS ISOCH support. We test for compatibility:
  37509. + * - DWORD aligned buffers
  37510. + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
  37511. + * If yes, then the fsm enqueue function will handle the state machine setup.
  37512. + */
  37513. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37514. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  37515. + struct dwc_otg_hcd_iso_packet_desc (*iso_descs)[0] = &urb->iso_descs;
  37516. + int nr_iso_frames = urb->packet_count;
  37517. + int i;
  37518. + uint32_t ptr;
  37519. +
  37520. + if (nr_iso_frames < 2)
  37521. + return 0;
  37522. + for (i = 0; i < nr_iso_frames; i++) {
  37523. + ptr = urb->dma + iso_descs[i]->offset;
  37524. + if (ptr & 0x3) {
  37525. + printk_ratelimited("%s: Non-Dword aligned isochronous frame offset."
  37526. + " Cannot queue FIQ-accelerated transfer to device %d endpoint %d\n",
  37527. + __FUNCTION__, qh->channel->dev_addr, qh->channel->ep_num);
  37528. + return 0;
  37529. + }
  37530. + }
  37531. + return 1;
  37532. + }
  37533. + }
  37534. + return 0;
  37535. +}
  37536. +
  37537. +/**
  37538. + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
  37539. + * @hcd: Pointer to the dwc_otg_hcd struct
  37540. + * @qh: Pointer to the endpoint's queue head
  37541. + *
  37542. + * Periodic split transactions are transmitted modulo 188 bytes.
  37543. + * This necessitates slicing data up into buckets for isochronous out
  37544. + * and fixing up the DMA address for all IN transfers.
  37545. + *
  37546. + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
  37547. + * HC buffer has been used.
  37548. + */
  37549. +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
  37550. + {
  37551. + int frame_length, i = 0;
  37552. + uint8_t *ptr = NULL;
  37553. + dwc_hc_t *hc = qh->channel;
  37554. + struct fiq_dma_blob *blob;
  37555. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  37556. +
  37557. + for (i = 0; i < 6; i++) {
  37558. + st->dma_info.slot_len[i] = 255;
  37559. + }
  37560. + st->dma_info.index = 0;
  37561. + i = 0;
  37562. + if (hc->ep_is_in) {
  37563. + /*
  37564. + * Set dma_regs to bounce buffer. FIQ will update the
  37565. + * state depending on transaction progress.
  37566. + */
  37567. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  37568. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  37569. + /* Calculate the max number of CSPLITS such that the FIQ can time out
  37570. + * a transaction if it fails.
  37571. + */
  37572. + frame_length = st->hcchar_copy.b.mps;
  37573. + do {
  37574. + i++;
  37575. + frame_length -= 188;
  37576. + } while (frame_length >= 0);
  37577. + st->nrpackets = i;
  37578. + return 1;
  37579. + } else {
  37580. + if (qh->ep_type == UE_ISOCHRONOUS) {
  37581. +
  37582. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37583. +
  37584. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  37585. + frame_length = frame_desc->length;
  37586. +
  37587. + /* Virtual address for bounce buffers */
  37588. + blob = hcd->fiq_dmab;
  37589. +
  37590. + ptr = qtd->urb->buf + frame_desc->offset;
  37591. + if (frame_length == 0) {
  37592. + /*
  37593. + * for isochronous transactions, we must still transmit a packet
  37594. + * even if the length is zero.
  37595. + */
  37596. + st->dma_info.slot_len[0] = 0;
  37597. + st->nrpackets = 1;
  37598. + } else {
  37599. + do {
  37600. + if (frame_length <= 188) {
  37601. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
  37602. + st->dma_info.slot_len[i] = frame_length;
  37603. + ptr += frame_length;
  37604. + } else {
  37605. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
  37606. + st->dma_info.slot_len[i] = 188;
  37607. + ptr += 188;
  37608. + }
  37609. + i++;
  37610. + frame_length -= 188;
  37611. + } while (frame_length > 0);
  37612. + st->nrpackets = i;
  37613. + }
  37614. + ptr = qtd->urb->buf + frame_desc->offset;
  37615. + /* Point the HC at the DMA address of the bounce buffers */
  37616. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  37617. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  37618. +
  37619. + /* fixup xfersize to the actual packet size */
  37620. + st->hctsiz_copy.b.pid = 0;
  37621. + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
  37622. + return 1;
  37623. + } else {
  37624. + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
  37625. + return 0;
  37626. + }
  37627. + }
  37628. +}
  37629. +
  37630. +/*
  37631. + * Pushing a periodic request into the queue near the EOF1 point
  37632. + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
  37633. + * Usually, the request goes out on the bus causing a transfer but
  37634. + * the core does not transfer the data to memory.
  37635. + * This guard interval (in number of 60MHz clocks) is required which
  37636. + * must cater for CPU latency between reading the value and enabling
  37637. + * the channel.
  37638. + */
  37639. +#define PERIODIC_FRREM_BACKOFF 1000
  37640. +
  37641. +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37642. +{
  37643. + dwc_hc_t *hc = qh->channel;
  37644. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  37645. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37646. + int frame;
  37647. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  37648. + int xfer_len, nrpackets;
  37649. + hcdma_data_t hcdma;
  37650. + hfnum_data_t hfnum;
  37651. +
  37652. + if (st->fsm != FIQ_PASSTHROUGH)
  37653. + return 0;
  37654. +
  37655. + st->nr_errors = 0;
  37656. +
  37657. + st->hcchar_copy.d32 = 0;
  37658. + st->hcchar_copy.b.mps = hc->max_packet;
  37659. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  37660. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  37661. + st->hcchar_copy.b.epnum = hc->ep_num;
  37662. + st->hcchar_copy.b.eptype = hc->ep_type;
  37663. +
  37664. + st->hcintmsk_copy.b.chhltd = 1;
  37665. +
  37666. + frame = dwc_otg_hcd_get_frame_number(hcd);
  37667. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  37668. +
  37669. + st->hcchar_copy.b.lspddev = 0;
  37670. + /* Enable the channel later as a final register write. */
  37671. +
  37672. + st->hcsplt_copy.d32 = 0;
  37673. +
  37674. + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
  37675. + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
  37676. + /* grab the next DMA address offset from the array */
  37677. + st->hcdma_copy.d32 = qtd->urb->dma;
  37678. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
  37679. +
  37680. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  37681. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  37682. + * this is always set to the maximum size of the endpoint. */
  37683. + xfer_len = st->hs_isoc_info.iso_desc[0].length;
  37684. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  37685. + if (nrpackets == 0)
  37686. + nrpackets = 1;
  37687. + st->hcchar_copy.b.multicnt = nrpackets;
  37688. + st->hctsiz_copy.b.pktcnt = nrpackets;
  37689. +
  37690. + /* Initial PID also needs to be set */
  37691. + if (st->hcchar_copy.b.epdir == 0) {
  37692. + st->hctsiz_copy.b.xfersize = xfer_len;
  37693. + switch (st->hcchar_copy.b.multicnt) {
  37694. + case 1:
  37695. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  37696. + break;
  37697. + case 2:
  37698. + case 3:
  37699. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  37700. + break;
  37701. + }
  37702. +
  37703. + } else {
  37704. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  37705. + switch (st->hcchar_copy.b.multicnt) {
  37706. + case 1:
  37707. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  37708. + break;
  37709. + case 2:
  37710. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  37711. + break;
  37712. + case 3:
  37713. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  37714. + break;
  37715. + }
  37716. + }
  37717. +
  37718. + st->hs_isoc_info.stride = qh->interval;
  37719. + st->uframe_sleeps = 0;
  37720. +
  37721. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
  37722. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
  37723. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  37724. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  37725. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  37726. + local_fiq_disable();
  37727. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37728. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  37729. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  37730. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  37731. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37732. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  37733. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  37734. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  37735. + * split transaction is queued very close to EOF. SOF interrupt handler
  37736. + * will wake this channel at the next interrupt.
  37737. + */
  37738. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  37739. + st->uframe_sleeps = 1;
  37740. + } else {
  37741. + st->fsm = FIQ_HS_ISOC_TURBO;
  37742. + st->hcchar_copy.b.chen = 1;
  37743. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37744. + }
  37745. + mb();
  37746. + st->hcchar_copy.b.chen = 0;
  37747. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37748. + local_fiq_enable();
  37749. + return 0;
  37750. +}
  37751. +
  37752. +
  37753. +/**
  37754. + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
  37755. + * @hcd: Pointer to the dwc_otg_hcd struct
  37756. + * @qh: Pointer to the endpoint's queue head
  37757. + *
  37758. + * This overrides the dwc_otg driver's normal method of queueing a transaction.
  37759. + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
  37760. + * for the nominated host channel.
  37761. + *
  37762. + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
  37763. + * start is possible. If not, then the FIQ is left to start the transfer.
  37764. + */
  37765. +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37766. +{
  37767. + int start_immediate = 1, i;
  37768. + hfnum_data_t hfnum;
  37769. + dwc_hc_t *hc = qh->channel;
  37770. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  37771. + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
  37772. + int hub_addr, port_addr, frame, uframe;
  37773. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  37774. +
  37775. + if (st->fsm != FIQ_PASSTHROUGH)
  37776. + return 0;
  37777. + st->nr_errors = 0;
  37778. +
  37779. + st->hcchar_copy.d32 = 0;
  37780. + st->hcchar_copy.b.mps = hc->max_packet;
  37781. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  37782. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  37783. + st->hcchar_copy.b.epnum = hc->ep_num;
  37784. + st->hcchar_copy.b.eptype = hc->ep_type;
  37785. + if (hc->ep_type & 0x1) {
  37786. + if (hc->ep_is_in)
  37787. + st->hcchar_copy.b.multicnt = 3;
  37788. + else
  37789. + /* Docs say set this to 1, but driver sets to 0! */
  37790. + st->hcchar_copy.b.multicnt = 0;
  37791. + } else {
  37792. + st->hcchar_copy.b.multicnt = 1;
  37793. + st->hcchar_copy.b.oddfrm = 0;
  37794. + }
  37795. + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
  37796. + /* Enable the channel later as a final register write. */
  37797. +
  37798. + st->hcsplt_copy.d32 = 0;
  37799. + if(qh->do_split) {
  37800. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  37801. + st->hcsplt_copy.b.compsplt = 0;
  37802. + st->hcsplt_copy.b.spltena = 1;
  37803. + // XACTPOS is for isoc-out only but needs initialising anyway.
  37804. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
  37805. + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
  37806. + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
  37807. + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
  37808. + * will update as necessary.
  37809. + */
  37810. + if (hc->xfer_len > 188) {
  37811. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
  37812. + }
  37813. + }
  37814. + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
  37815. + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
  37816. + st->hub_addr = hub_addr;
  37817. + st->port_addr = port_addr;
  37818. + }
  37819. +
  37820. + st->hctsiz_copy.d32 = 0;
  37821. + st->hctsiz_copy.b.dopng = 0;
  37822. + st->hctsiz_copy.b.pid = hc->data_pid_start;
  37823. +
  37824. + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  37825. + hc->xfer_len = hc->max_packet;
  37826. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  37827. + hc->xfer_len = 188;
  37828. + }
  37829. + st->hctsiz_copy.b.xfersize = hc->xfer_len;
  37830. +
  37831. + st->hctsiz_copy.b.pktcnt = 1;
  37832. +
  37833. + if (hc->ep_type & 0x1) {
  37834. + /*
  37835. + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
  37836. + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
  37837. + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
  37838. + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
  37839. + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
  37840. + * must not touch internal driver state.
  37841. + */
  37842. + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
  37843. + if (hc->align_buff) {
  37844. + st->hcdma_copy.d32 = hc->align_buff;
  37845. + } else {
  37846. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37847. + }
  37848. + }
  37849. + } else {
  37850. + if (hc->align_buff) {
  37851. + st->hcdma_copy.d32 = hc->align_buff;
  37852. + } else {
  37853. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37854. + }
  37855. + }
  37856. + /* The FIQ depends upon no other interrupts being enabled except channel halt.
  37857. + * Fixup channel interrupt mask. */
  37858. + st->hcintmsk_copy.d32 = 0;
  37859. + st->hcintmsk_copy.b.chhltd = 1;
  37860. + st->hcintmsk_copy.b.ahberr = 1;
  37861. +
  37862. + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
  37863. + * as Control puts the transfer into the non-periodic request queue and the
  37864. + * non-periodic handler in the hub. Makes things lots easier.
  37865. + */
  37866. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
  37867. + st->hcchar_copy.b.multicnt = 0;
  37868. + st->hcchar_copy.b.oddfrm = 0;
  37869. + st->hcchar_copy.b.eptype = UE_CONTROL;
  37870. + if (hc->align_buff) {
  37871. + st->hcdma_copy.d32 = hc->align_buff;
  37872. + } else {
  37873. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37874. + }
  37875. + }
  37876. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  37877. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  37878. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  37879. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37880. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  37881. +
  37882. + local_fiq_disable();
  37883. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37884. +
  37885. + if (hc->ep_type & 0x1) {
  37886. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  37887. + frame = (hfnum.b.frnum & ~0x7) >> 3;
  37888. + uframe = hfnum.b.frnum & 0x7;
  37889. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  37890. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  37891. + * split transaction is queued very close to EOF.
  37892. + */
  37893. + start_immediate = 0;
  37894. + } else if (uframe == 5) {
  37895. + start_immediate = 0;
  37896. + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
  37897. + start_immediate = 0;
  37898. + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
  37899. + start_immediate = 0;
  37900. + } else {
  37901. + /* Search through all host channels to determine if a transaction
  37902. + * is currently in progress */
  37903. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  37904. + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
  37905. + continue;
  37906. + switch (hcd->fiq_state->channel[i].fsm) {
  37907. + /* TT is reserved for channels that are in the middle of a periodic
  37908. + * split transaction.
  37909. + */
  37910. + case FIQ_PER_SSPLIT_STARTED:
  37911. + case FIQ_PER_CSPLIT_WAIT:
  37912. + case FIQ_PER_CSPLIT_NYET1:
  37913. + case FIQ_PER_CSPLIT_POLL:
  37914. + case FIQ_PER_ISO_OUT_ACTIVE:
  37915. + case FIQ_PER_ISO_OUT_LAST:
  37916. + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
  37917. + hcd->fiq_state->channel[i].port_addr == port_addr) {
  37918. + start_immediate = 0;
  37919. + }
  37920. + break;
  37921. + default:
  37922. + break;
  37923. + }
  37924. + if (!start_immediate)
  37925. + break;
  37926. + }
  37927. + }
  37928. + }
  37929. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
  37930. + start_immediate = 1;
  37931. +
  37932. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
  37933. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
  37934. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
  37935. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  37936. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  37937. + switch (hc->ep_type) {
  37938. + case UE_CONTROL:
  37939. + case UE_BULK:
  37940. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  37941. + break;
  37942. + case UE_ISOCHRONOUS:
  37943. + if (hc->ep_is_in) {
  37944. + if (start_immediate) {
  37945. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  37946. + } else {
  37947. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  37948. + }
  37949. + } else {
  37950. + if (start_immediate) {
  37951. + /* Single-isoc OUT packets don't require FIQ involvement */
  37952. + if (st->nrpackets == 1) {
  37953. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  37954. + } else {
  37955. + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
  37956. + }
  37957. + } else {
  37958. + st->fsm = FIQ_PER_ISO_OUT_PENDING;
  37959. + }
  37960. + }
  37961. + break;
  37962. + case UE_INTERRUPT:
  37963. + if (fiq_fsm_mask & 0x8) {
  37964. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  37965. + } else if (start_immediate) {
  37966. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  37967. + } else {
  37968. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  37969. + }
  37970. + default:
  37971. + break;
  37972. + }
  37973. + if (start_immediate) {
  37974. + /* Set the oddfrm bit as close as possible to actual queueing */
  37975. + frame = dwc_otg_hcd_get_frame_number(hcd);
  37976. + st->expected_uframe = (frame + 1) & 0x3FFF;
  37977. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  37978. + st->hcchar_copy.b.chen = 1;
  37979. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37980. + }
  37981. + mb();
  37982. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37983. + local_fiq_enable();
  37984. + return 0;
  37985. +}
  37986. +
  37987. +
  37988. +/**
  37989. + * This function selects transactions from the HCD transfer schedule and
  37990. + * assigns them to available host channels. It is called from HCD interrupt
  37991. + * handler functions.
  37992. + *
  37993. + * @param hcd The HCD state structure.
  37994. + *
  37995. + * @return The types of new transactions that were assigned to host channels.
  37996. + */
  37997. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  37998. +{
  37999. + dwc_list_link_t *qh_ptr;
  38000. + dwc_otg_qh_t *qh;
  38001. + int num_channels;
  38002. + dwc_irqflags_t flags;
  38003. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  38004. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  38005. +
  38006. +#ifdef DEBUG_HOST_CHANNELS
  38007. + last_sel_trans_num_per_scheduled = 0;
  38008. + last_sel_trans_num_nonper_scheduled = 0;
  38009. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  38010. +#endif /* DEBUG_HOST_CHANNELS */
  38011. +
  38012. + /* Process entries in the periodic ready list. */
  38013. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  38014. +
  38015. + while (qh_ptr != &hcd->periodic_sched_ready &&
  38016. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  38017. +
  38018. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38019. +
  38020. + if (microframe_schedule) {
  38021. + // Make sure we leave one channel for non periodic transactions.
  38022. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  38023. + if (hcd->available_host_channels <= 1) {
  38024. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38025. + break;
  38026. + }
  38027. + hcd->available_host_channels--;
  38028. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38029. +#ifdef DEBUG_HOST_CHANNELS
  38030. + last_sel_trans_num_per_scheduled++;
  38031. +#endif /* DEBUG_HOST_CHANNELS */
  38032. + }
  38033. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38034. + assign_and_init_hc(hcd, qh);
  38035. +
  38036. + /*
  38037. + * Move the QH from the periodic ready schedule to the
  38038. + * periodic assigned schedule.
  38039. + */
  38040. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38041. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  38042. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  38043. + &qh->qh_list_entry);
  38044. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38045. + }
  38046. +
  38047. + /*
  38048. + * Process entries in the inactive portion of the non-periodic
  38049. + * schedule. Some free host channels may not be used if they are
  38050. + * reserved for periodic transfers.
  38051. + */
  38052. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  38053. + num_channels = hcd->core_if->core_params->host_channels;
  38054. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  38055. + (microframe_schedule || hcd->non_periodic_channels <
  38056. + num_channels - hcd->periodic_channels) &&
  38057. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  38058. +
  38059. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38060. + /*
  38061. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  38062. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  38063. + * cheeky devices that just hold off using NAKs
  38064. + */
  38065. + if (fiq_enable && nak_holdoff && qh->do_split) {
  38066. + if (qh->nak_frame != 0xffff) {
  38067. + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
  38068. + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
  38069. + if (dwc_frame_num_le(frame, next_frame)) {
  38070. + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
  38071. + hcd->fiq_state->next_sched_frame = next_frame;
  38072. + }
  38073. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38074. + continue;
  38075. + } else {
  38076. + qh->nak_frame = 0xFFFF;
  38077. + }
  38078. + }
  38079. + }
  38080. +
  38081. + if (microframe_schedule) {
  38082. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  38083. + if (hcd->available_host_channels < 1) {
  38084. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38085. + break;
  38086. + }
  38087. + hcd->available_host_channels--;
  38088. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38089. +#ifdef DEBUG_HOST_CHANNELS
  38090. + last_sel_trans_num_nonper_scheduled++;
  38091. +#endif /* DEBUG_HOST_CHANNELS */
  38092. + }
  38093. +
  38094. + assign_and_init_hc(hcd, qh);
  38095. +
  38096. + /*
  38097. + * Move the QH from the non-periodic inactive schedule to the
  38098. + * non-periodic active schedule.
  38099. + */
  38100. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38101. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  38102. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  38103. + &qh->qh_list_entry);
  38104. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38105. +
  38106. +
  38107. + if (!microframe_schedule)
  38108. + hcd->non_periodic_channels++;
  38109. + }
  38110. + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
  38111. + * stop the FIQ from kicking us. We could potentially still have elements here if we
  38112. + * ran out of host channels.
  38113. + */
  38114. + if (fiq_enable) {
  38115. + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
  38116. + hcd->fiq_state->kick_np_queues = 0;
  38117. + } else {
  38118. + /* For each entry remaining in the NP inactive queue,
  38119. + * if this a NAK'd retransmit then don't set the kick flag.
  38120. + */
  38121. + if(nak_holdoff) {
  38122. + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
  38123. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38124. + if (qh->nak_frame == 0xFFFF) {
  38125. + hcd->fiq_state->kick_np_queues = 1;
  38126. + }
  38127. + }
  38128. + }
  38129. + }
  38130. + }
  38131. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  38132. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  38133. +
  38134. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  38135. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  38136. +
  38137. +
  38138. +#ifdef DEBUG_HOST_CHANNELS
  38139. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  38140. +#endif /* DEBUG_HOST_CHANNELS */
  38141. + return ret_val;
  38142. +}
  38143. +
  38144. +/**
  38145. + * Attempts to queue a single transaction request for a host channel
  38146. + * associated with either a periodic or non-periodic transfer. This function
  38147. + * assumes that there is space available in the appropriate request queue. For
  38148. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  38149. + * is available in the appropriate Tx FIFO.
  38150. + *
  38151. + * @param hcd The HCD state structure.
  38152. + * @param hc Host channel descriptor associated with either a periodic or
  38153. + * non-periodic transfer.
  38154. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  38155. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  38156. + * transfers.
  38157. + *
  38158. + * @return 1 if a request is queued and more requests may be needed to
  38159. + * complete the transfer, 0 if no more requests are required for this
  38160. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  38161. + */
  38162. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  38163. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  38164. +{
  38165. + int retval;
  38166. +
  38167. + if (hcd->core_if->dma_enable) {
  38168. + if (hcd->core_if->dma_desc_enable) {
  38169. + if (!hc->xfer_started
  38170. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  38171. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  38172. + hc->qh->ping_state = 0;
  38173. + }
  38174. + } else if (!hc->xfer_started) {
  38175. + if (fiq_fsm_enable && hc->error_state) {
  38176. + hcd->fiq_state->channel[hc->hc_num].nr_errors =
  38177. + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
  38178. + hcd->fiq_state->channel[hc->hc_num].fsm =
  38179. + FIQ_PASSTHROUGH_ERRORSTATE;
  38180. + }
  38181. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38182. + hc->qh->ping_state = 0;
  38183. + }
  38184. + retval = 0;
  38185. + } else if (hc->halt_pending) {
  38186. + /* Don't queue a request if the channel has been halted. */
  38187. + retval = 0;
  38188. + } else if (hc->halt_on_queue) {
  38189. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  38190. + retval = 0;
  38191. + } else if (hc->do_ping) {
  38192. + if (!hc->xfer_started) {
  38193. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38194. + }
  38195. + retval = 0;
  38196. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  38197. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  38198. + if (!hc->xfer_started) {
  38199. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38200. + retval = 1;
  38201. + } else {
  38202. + retval =
  38203. + dwc_otg_hc_continue_transfer(hcd->core_if,
  38204. + hc);
  38205. + }
  38206. + } else {
  38207. + retval = -1;
  38208. + }
  38209. + } else {
  38210. + if (!hc->xfer_started) {
  38211. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38212. + retval = 1;
  38213. + } else {
  38214. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  38215. + }
  38216. + }
  38217. +
  38218. + return retval;
  38219. +}
  38220. +
  38221. +/**
  38222. + * Processes periodic channels for the next frame and queues transactions for
  38223. + * these channels to the DWC_otg controller. After queueing transactions, the
  38224. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  38225. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  38226. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  38227. + */
  38228. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  38229. +{
  38230. + hptxsts_data_t tx_status;
  38231. + dwc_list_link_t *qh_ptr;
  38232. + dwc_otg_qh_t *qh;
  38233. + int status = 0;
  38234. + int no_queue_space = 0;
  38235. + int no_fifo_space = 0;
  38236. +
  38237. + dwc_otg_host_global_regs_t *host_regs;
  38238. + host_regs = hcd->core_if->host_if->host_global_regs;
  38239. +
  38240. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  38241. +#ifdef DEBUG
  38242. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38243. + DWC_DEBUGPL(DBG_HCDV,
  38244. + " P Tx Req Queue Space Avail (before queue): %d\n",
  38245. + tx_status.b.ptxqspcavail);
  38246. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  38247. + tx_status.b.ptxfspcavail);
  38248. +#endif
  38249. +
  38250. + qh_ptr = hcd->periodic_sched_assigned.next;
  38251. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  38252. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38253. + if (tx_status.b.ptxqspcavail == 0) {
  38254. + no_queue_space = 1;
  38255. + break;
  38256. + }
  38257. +
  38258. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38259. +
  38260. + // Do not send a split start transaction any later than frame .6
  38261. + // Note, we have to schedule a periodic in .5 to make it go in .6
  38262. + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  38263. + {
  38264. + qh_ptr = qh_ptr->next;
  38265. + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  38266. + continue;
  38267. + }
  38268. +
  38269. + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  38270. + if (qh->do_split)
  38271. + fiq_fsm_queue_split_transaction(hcd, qh);
  38272. + else
  38273. + fiq_fsm_queue_isoc_transaction(hcd, qh);
  38274. + } else {
  38275. +
  38276. + /*
  38277. + * Set a flag if we're queueing high-bandwidth in slave mode.
  38278. + * The flag prevents any halts to get into the request queue in
  38279. + * the middle of multiple high-bandwidth packets getting queued.
  38280. + */
  38281. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  38282. + hcd->core_if->queuing_high_bandwidth = 1;
  38283. + }
  38284. + status = queue_transaction(hcd, qh->channel,
  38285. + tx_status.b.ptxfspcavail);
  38286. + if (status < 0) {
  38287. + no_fifo_space = 1;
  38288. + break;
  38289. + }
  38290. + }
  38291. +
  38292. + /*
  38293. + * In Slave mode, stay on the current transfer until there is
  38294. + * nothing more to do or the high-bandwidth request count is
  38295. + * reached. In DMA mode, only need to queue one request. The
  38296. + * controller automatically handles multiple packets for
  38297. + * high-bandwidth transfers.
  38298. + */
  38299. + if (hcd->core_if->dma_enable || status == 0 ||
  38300. + qh->channel->requests == qh->channel->multi_count) {
  38301. + qh_ptr = qh_ptr->next;
  38302. + /*
  38303. + * Move the QH from the periodic assigned schedule to
  38304. + * the periodic queued schedule.
  38305. + */
  38306. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  38307. + &qh->qh_list_entry);
  38308. +
  38309. + /* done queuing high bandwidth */
  38310. + hcd->core_if->queuing_high_bandwidth = 0;
  38311. + }
  38312. + }
  38313. +
  38314. + if (!hcd->core_if->dma_enable) {
  38315. + dwc_otg_core_global_regs_t *global_regs;
  38316. + gintmsk_data_t intr_mask = {.d32 = 0 };
  38317. +
  38318. + global_regs = hcd->core_if->core_global_regs;
  38319. + intr_mask.b.ptxfempty = 1;
  38320. +#ifdef DEBUG
  38321. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38322. + DWC_DEBUGPL(DBG_HCDV,
  38323. + " P Tx Req Queue Space Avail (after queue): %d\n",
  38324. + tx_status.b.ptxqspcavail);
  38325. + DWC_DEBUGPL(DBG_HCDV,
  38326. + " P Tx FIFO Space Avail (after queue): %d\n",
  38327. + tx_status.b.ptxfspcavail);
  38328. +#endif
  38329. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  38330. + no_queue_space || no_fifo_space) {
  38331. + /*
  38332. + * May need to queue more transactions as the request
  38333. + * queue or Tx FIFO empties. Enable the periodic Tx
  38334. + * FIFO empty interrupt. (Always use the half-empty
  38335. + * level to ensure that new requests are loaded as
  38336. + * soon as possible.)
  38337. + */
  38338. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  38339. + intr_mask.d32);
  38340. + } else {
  38341. + /*
  38342. + * Disable the Tx FIFO empty interrupt since there are
  38343. + * no more transactions that need to be queued right
  38344. + * now. This function is called from interrupt
  38345. + * handlers to queue more transactions as transfer
  38346. + * states change.
  38347. + */
  38348. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  38349. + 0);
  38350. + }
  38351. + }
  38352. +}
  38353. +
  38354. +/**
  38355. + * Processes active non-periodic channels and queues transactions for these
  38356. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  38357. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  38358. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  38359. + * FIFO Empty interrupt is disabled.
  38360. + */
  38361. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  38362. +{
  38363. + gnptxsts_data_t tx_status;
  38364. + dwc_list_link_t *orig_qh_ptr;
  38365. + dwc_otg_qh_t *qh;
  38366. + int status;
  38367. + int no_queue_space = 0;
  38368. + int no_fifo_space = 0;
  38369. + int more_to_do = 0;
  38370. +
  38371. + dwc_otg_core_global_regs_t *global_regs =
  38372. + hcd->core_if->core_global_regs;
  38373. +
  38374. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  38375. +#ifdef DEBUG
  38376. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38377. + DWC_DEBUGPL(DBG_HCDV,
  38378. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  38379. + tx_status.b.nptxqspcavail);
  38380. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  38381. + tx_status.b.nptxfspcavail);
  38382. +#endif
  38383. + /*
  38384. + * Keep track of the starting point. Skip over the start-of-list
  38385. + * entry.
  38386. + */
  38387. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  38388. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  38389. + }
  38390. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  38391. +
  38392. + /*
  38393. + * Process once through the active list or until no more space is
  38394. + * available in the request queue or the Tx FIFO.
  38395. + */
  38396. + do {
  38397. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38398. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  38399. + no_queue_space = 1;
  38400. + break;
  38401. + }
  38402. +
  38403. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  38404. + qh_list_entry);
  38405. +
  38406. + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  38407. + fiq_fsm_queue_split_transaction(hcd, qh);
  38408. + } else {
  38409. + status = queue_transaction(hcd, qh->channel,
  38410. + tx_status.b.nptxfspcavail);
  38411. +
  38412. + if (status > 0) {
  38413. + more_to_do = 1;
  38414. + } else if (status < 0) {
  38415. + no_fifo_space = 1;
  38416. + break;
  38417. + }
  38418. + }
  38419. + /* Advance to next QH, skipping start-of-list entry. */
  38420. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  38421. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  38422. + hcd->non_periodic_qh_ptr =
  38423. + hcd->non_periodic_qh_ptr->next;
  38424. + }
  38425. +
  38426. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  38427. +
  38428. + if (!hcd->core_if->dma_enable) {
  38429. + gintmsk_data_t intr_mask = {.d32 = 0 };
  38430. + intr_mask.b.nptxfempty = 1;
  38431. +
  38432. +#ifdef DEBUG
  38433. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38434. + DWC_DEBUGPL(DBG_HCDV,
  38435. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  38436. + tx_status.b.nptxqspcavail);
  38437. + DWC_DEBUGPL(DBG_HCDV,
  38438. + " NP Tx FIFO Space Avail (after queue): %d\n",
  38439. + tx_status.b.nptxfspcavail);
  38440. +#endif
  38441. + if (more_to_do || no_queue_space || no_fifo_space) {
  38442. + /*
  38443. + * May need to queue more transactions as the request
  38444. + * queue or Tx FIFO empties. Enable the non-periodic
  38445. + * Tx FIFO empty interrupt. (Always use the half-empty
  38446. + * level to ensure that new requests are loaded as
  38447. + * soon as possible.)
  38448. + */
  38449. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  38450. + intr_mask.d32);
  38451. + } else {
  38452. + /*
  38453. + * Disable the Tx FIFO empty interrupt since there are
  38454. + * no more transactions that need to be queued right
  38455. + * now. This function is called from interrupt
  38456. + * handlers to queue more transactions as transfer
  38457. + * states change.
  38458. + */
  38459. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  38460. + 0);
  38461. + }
  38462. + }
  38463. +}
  38464. +
  38465. +/**
  38466. + * This function processes the currently active host channels and queues
  38467. + * transactions for these channels to the DWC_otg controller. It is called
  38468. + * from HCD interrupt handler functions.
  38469. + *
  38470. + * @param hcd The HCD state structure.
  38471. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  38472. + * periodic, or both).
  38473. + */
  38474. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  38475. + dwc_otg_transaction_type_e tr_type)
  38476. +{
  38477. +#ifdef DEBUG_SOF
  38478. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  38479. +#endif
  38480. + /* Process host channels associated with periodic transfers. */
  38481. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  38482. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  38483. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  38484. +
  38485. + process_periodic_channels(hcd);
  38486. + }
  38487. +
  38488. + /* Process host channels associated with non-periodic transfers. */
  38489. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  38490. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  38491. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  38492. + process_non_periodic_channels(hcd);
  38493. + } else {
  38494. + /*
  38495. + * Ensure NP Tx FIFO empty interrupt is disabled when
  38496. + * there are no non-periodic transfers to process.
  38497. + */
  38498. + gintmsk_data_t gintmsk = {.d32 = 0 };
  38499. + gintmsk.b.nptxfempty = 1;
  38500. +
  38501. + if (fiq_enable) {
  38502. + local_fiq_disable();
  38503. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  38504. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  38505. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  38506. + local_fiq_enable();
  38507. + } else {
  38508. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  38509. + }
  38510. + }
  38511. + }
  38512. +}
  38513. +
  38514. +#ifdef DWC_HS_ELECT_TST
  38515. +/*
  38516. + * Quick and dirty hack to implement the HS Electrical Test
  38517. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  38518. + *
  38519. + * This code was copied from our userspace app "hset". It sends a
  38520. + * Get Device Descriptor control sequence in two parts, first the
  38521. + * Setup packet by itself, followed some time later by the In and
  38522. + * Ack packets. Rather than trying to figure out how to add this
  38523. + * functionality to the normal driver code, we just hijack the
  38524. + * hardware, using these two function to drive the hardware
  38525. + * directly.
  38526. + */
  38527. +
  38528. +static dwc_otg_core_global_regs_t *global_regs;
  38529. +static dwc_otg_host_global_regs_t *hc_global_regs;
  38530. +static dwc_otg_hc_regs_t *hc_regs;
  38531. +static uint32_t *data_fifo;
  38532. +
  38533. +static void do_setup(void)
  38534. +{
  38535. + gintsts_data_t gintsts;
  38536. + hctsiz_data_t hctsiz;
  38537. + hcchar_data_t hcchar;
  38538. + haint_data_t haint;
  38539. + hcint_data_t hcint;
  38540. +
  38541. + /* Enable HAINTs */
  38542. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  38543. +
  38544. + /* Enable HCINTs */
  38545. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  38546. +
  38547. + /* Read GINTSTS */
  38548. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38549. +
  38550. + /* Read HAINT */
  38551. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38552. +
  38553. + /* Read HCINT */
  38554. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38555. +
  38556. + /* Read HCCHAR */
  38557. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38558. +
  38559. + /* Clear HCINT */
  38560. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38561. +
  38562. + /* Clear HAINT */
  38563. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38564. +
  38565. + /* Clear GINTSTS */
  38566. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38567. +
  38568. + /* Read GINTSTS */
  38569. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38570. +
  38571. + /*
  38572. + * Send Setup packet (Get Device Descriptor)
  38573. + */
  38574. +
  38575. + /* Make sure channel is disabled */
  38576. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38577. + if (hcchar.b.chen) {
  38578. + hcchar.b.chdis = 1;
  38579. +// hcchar.b.chen = 1;
  38580. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38581. + //sleep(1);
  38582. + dwc_mdelay(1000);
  38583. +
  38584. + /* Read GINTSTS */
  38585. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38586. +
  38587. + /* Read HAINT */
  38588. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38589. +
  38590. + /* Read HCINT */
  38591. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38592. +
  38593. + /* Read HCCHAR */
  38594. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38595. +
  38596. + /* Clear HCINT */
  38597. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38598. +
  38599. + /* Clear HAINT */
  38600. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38601. +
  38602. + /* Clear GINTSTS */
  38603. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38604. +
  38605. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38606. + }
  38607. +
  38608. + /* Set HCTSIZ */
  38609. + hctsiz.d32 = 0;
  38610. + hctsiz.b.xfersize = 8;
  38611. + hctsiz.b.pktcnt = 1;
  38612. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  38613. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38614. +
  38615. + /* Set HCCHAR */
  38616. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38617. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38618. + hcchar.b.epdir = 0;
  38619. + hcchar.b.epnum = 0;
  38620. + hcchar.b.mps = 8;
  38621. + hcchar.b.chen = 1;
  38622. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38623. +
  38624. + /* Fill FIFO with Setup data for Get Device Descriptor */
  38625. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  38626. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  38627. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  38628. +
  38629. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38630. +
  38631. + /* Wait for host channel interrupt */
  38632. + do {
  38633. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38634. + } while (gintsts.b.hcintr == 0);
  38635. +
  38636. + /* Disable HCINTs */
  38637. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  38638. +
  38639. + /* Disable HAINTs */
  38640. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  38641. +
  38642. + /* Read HAINT */
  38643. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38644. +
  38645. + /* Read HCINT */
  38646. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38647. +
  38648. + /* Read HCCHAR */
  38649. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38650. +
  38651. + /* Clear HCINT */
  38652. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38653. +
  38654. + /* Clear HAINT */
  38655. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38656. +
  38657. + /* Clear GINTSTS */
  38658. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38659. +
  38660. + /* Read GINTSTS */
  38661. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38662. +}
  38663. +
  38664. +static void do_in_ack(void)
  38665. +{
  38666. + gintsts_data_t gintsts;
  38667. + hctsiz_data_t hctsiz;
  38668. + hcchar_data_t hcchar;
  38669. + haint_data_t haint;
  38670. + hcint_data_t hcint;
  38671. + host_grxsts_data_t grxsts;
  38672. +
  38673. + /* Enable HAINTs */
  38674. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  38675. +
  38676. + /* Enable HCINTs */
  38677. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  38678. +
  38679. + /* Read GINTSTS */
  38680. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38681. +
  38682. + /* Read HAINT */
  38683. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38684. +
  38685. + /* Read HCINT */
  38686. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38687. +
  38688. + /* Read HCCHAR */
  38689. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38690. +
  38691. + /* Clear HCINT */
  38692. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38693. +
  38694. + /* Clear HAINT */
  38695. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38696. +
  38697. + /* Clear GINTSTS */
  38698. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38699. +
  38700. + /* Read GINTSTS */
  38701. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38702. +
  38703. + /*
  38704. + * Receive Control In packet
  38705. + */
  38706. +
  38707. + /* Make sure channel is disabled */
  38708. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38709. + if (hcchar.b.chen) {
  38710. + hcchar.b.chdis = 1;
  38711. + hcchar.b.chen = 1;
  38712. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38713. + //sleep(1);
  38714. + dwc_mdelay(1000);
  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. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38738. + }
  38739. +
  38740. + /* Set HCTSIZ */
  38741. + hctsiz.d32 = 0;
  38742. + hctsiz.b.xfersize = 8;
  38743. + hctsiz.b.pktcnt = 1;
  38744. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  38745. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38746. +
  38747. + /* Set HCCHAR */
  38748. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38749. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38750. + hcchar.b.epdir = 1;
  38751. + hcchar.b.epnum = 0;
  38752. + hcchar.b.mps = 8;
  38753. + hcchar.b.chen = 1;
  38754. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38755. +
  38756. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38757. +
  38758. + /* Wait for receive status queue interrupt */
  38759. + do {
  38760. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38761. + } while (gintsts.b.rxstsqlvl == 0);
  38762. +
  38763. + /* Read RXSTS */
  38764. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  38765. +
  38766. + /* Clear RXSTSQLVL in GINTSTS */
  38767. + gintsts.d32 = 0;
  38768. + gintsts.b.rxstsqlvl = 1;
  38769. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38770. +
  38771. + switch (grxsts.b.pktsts) {
  38772. + case DWC_GRXSTS_PKTSTS_IN:
  38773. + /* Read the data into the host buffer */
  38774. + if (grxsts.b.bcnt > 0) {
  38775. + int i;
  38776. + int word_count = (grxsts.b.bcnt + 3) / 4;
  38777. +
  38778. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  38779. +
  38780. + for (i = 0; i < word_count; i++) {
  38781. + (void)DWC_READ_REG32(data_fifo++);
  38782. + }
  38783. + }
  38784. + break;
  38785. +
  38786. + default:
  38787. + break;
  38788. + }
  38789. +
  38790. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38791. +
  38792. + /* Wait for receive status queue interrupt */
  38793. + do {
  38794. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38795. + } while (gintsts.b.rxstsqlvl == 0);
  38796. +
  38797. + /* Read RXSTS */
  38798. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  38799. +
  38800. + /* Clear RXSTSQLVL in GINTSTS */
  38801. + gintsts.d32 = 0;
  38802. + gintsts.b.rxstsqlvl = 1;
  38803. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38804. +
  38805. + switch (grxsts.b.pktsts) {
  38806. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  38807. + break;
  38808. +
  38809. + default:
  38810. + break;
  38811. + }
  38812. +
  38813. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38814. +
  38815. + /* Wait for host channel interrupt */
  38816. + do {
  38817. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38818. + } while (gintsts.b.hcintr == 0);
  38819. +
  38820. + /* Read HAINT */
  38821. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38822. +
  38823. + /* Read HCINT */
  38824. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38825. +
  38826. + /* Read HCCHAR */
  38827. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38828. +
  38829. + /* Clear HCINT */
  38830. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38831. +
  38832. + /* Clear HAINT */
  38833. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38834. +
  38835. + /* Clear GINTSTS */
  38836. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38837. +
  38838. + /* Read GINTSTS */
  38839. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38840. +
  38841. +// usleep(100000);
  38842. +// mdelay(100);
  38843. + dwc_mdelay(1);
  38844. +
  38845. + /*
  38846. + * Send handshake packet
  38847. + */
  38848. +
  38849. + /* Read HAINT */
  38850. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38851. +
  38852. + /* Read HCINT */
  38853. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38854. +
  38855. + /* Read HCCHAR */
  38856. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38857. +
  38858. + /* Clear HCINT */
  38859. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38860. +
  38861. + /* Clear HAINT */
  38862. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38863. +
  38864. + /* Clear GINTSTS */
  38865. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38866. +
  38867. + /* Read GINTSTS */
  38868. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38869. +
  38870. + /* Make sure channel is disabled */
  38871. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38872. + if (hcchar.b.chen) {
  38873. + hcchar.b.chdis = 1;
  38874. + hcchar.b.chen = 1;
  38875. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38876. + //sleep(1);
  38877. + dwc_mdelay(1000);
  38878. +
  38879. + /* Read GINTSTS */
  38880. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38881. +
  38882. + /* Read HAINT */
  38883. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38884. +
  38885. + /* Read HCINT */
  38886. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38887. +
  38888. + /* Read HCCHAR */
  38889. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38890. +
  38891. + /* Clear HCINT */
  38892. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38893. +
  38894. + /* Clear HAINT */
  38895. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38896. +
  38897. + /* Clear GINTSTS */
  38898. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38899. +
  38900. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38901. + }
  38902. +
  38903. + /* Set HCTSIZ */
  38904. + hctsiz.d32 = 0;
  38905. + hctsiz.b.xfersize = 0;
  38906. + hctsiz.b.pktcnt = 1;
  38907. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  38908. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38909. +
  38910. + /* Set HCCHAR */
  38911. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38912. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38913. + hcchar.b.epdir = 0;
  38914. + hcchar.b.epnum = 0;
  38915. + hcchar.b.mps = 8;
  38916. + hcchar.b.chen = 1;
  38917. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38918. +
  38919. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38920. +
  38921. + /* Wait for host channel interrupt */
  38922. + do {
  38923. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38924. + } while (gintsts.b.hcintr == 0);
  38925. +
  38926. + /* Disable HCINTs */
  38927. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  38928. +
  38929. + /* Disable HAINTs */
  38930. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  38931. +
  38932. + /* Read HAINT */
  38933. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38934. +
  38935. + /* Read HCINT */
  38936. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38937. +
  38938. + /* Read HCCHAR */
  38939. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38940. +
  38941. + /* Clear HCINT */
  38942. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38943. +
  38944. + /* Clear HAINT */
  38945. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38946. +
  38947. + /* Clear GINTSTS */
  38948. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38949. +
  38950. + /* Read GINTSTS */
  38951. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38952. +}
  38953. +#endif
  38954. +
  38955. +/** Handles hub class-specific requests. */
  38956. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  38957. + uint16_t typeReq,
  38958. + uint16_t wValue,
  38959. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  38960. +{
  38961. + int retval = 0;
  38962. +
  38963. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  38964. + usb_hub_descriptor_t *hub_desc;
  38965. + hprt0_data_t hprt0 = {.d32 = 0 };
  38966. +
  38967. + uint32_t port_status;
  38968. +
  38969. + switch (typeReq) {
  38970. + case UCR_CLEAR_HUB_FEATURE:
  38971. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38972. + "ClearHubFeature 0x%x\n", wValue);
  38973. + switch (wValue) {
  38974. + case UHF_C_HUB_LOCAL_POWER:
  38975. + case UHF_C_HUB_OVER_CURRENT:
  38976. + /* Nothing required here */
  38977. + break;
  38978. + default:
  38979. + retval = -DWC_E_INVALID;
  38980. + DWC_ERROR("DWC OTG HCD - "
  38981. + "ClearHubFeature request %xh unknown\n",
  38982. + wValue);
  38983. + }
  38984. + break;
  38985. + case UCR_CLEAR_PORT_FEATURE:
  38986. +#ifdef CONFIG_USB_DWC_OTG_LPM
  38987. + if (wValue != UHF_PORT_L1)
  38988. +#endif
  38989. + if (!wIndex || wIndex > 1)
  38990. + goto error;
  38991. +
  38992. + switch (wValue) {
  38993. + case UHF_PORT_ENABLE:
  38994. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  38995. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  38996. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  38997. + hprt0.b.prtena = 1;
  38998. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  38999. + break;
  39000. + case UHF_PORT_SUSPEND:
  39001. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39002. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  39003. +
  39004. + if (core_if->power_down == 2) {
  39005. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  39006. + } else {
  39007. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  39008. + dwc_mdelay(5);
  39009. +
  39010. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39011. + hprt0.b.prtres = 1;
  39012. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39013. + hprt0.b.prtsusp = 0;
  39014. + /* Clear Resume bit */
  39015. + dwc_mdelay(100);
  39016. + hprt0.b.prtres = 0;
  39017. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39018. + }
  39019. + break;
  39020. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39021. + case UHF_PORT_L1:
  39022. + {
  39023. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39024. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  39025. +
  39026. + lpmcfg.d32 =
  39027. + DWC_READ_REG32(&core_if->
  39028. + core_global_regs->glpmcfg);
  39029. + lpmcfg.b.en_utmi_sleep = 0;
  39030. + lpmcfg.b.hird_thres &= (~(1 << 4));
  39031. + lpmcfg.b.prt_sleep_sts = 1;
  39032. + DWC_WRITE_REG32(&core_if->
  39033. + core_global_regs->glpmcfg,
  39034. + lpmcfg.d32);
  39035. +
  39036. + /* Clear Enbl_L1Gating bit. */
  39037. + pcgcctl.b.enbl_sleep_gating = 1;
  39038. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  39039. + 0);
  39040. +
  39041. + dwc_mdelay(5);
  39042. +
  39043. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39044. + hprt0.b.prtres = 1;
  39045. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39046. + hprt0.d32);
  39047. + /* This bit will be cleared in wakeup interrupt handle */
  39048. + break;
  39049. + }
  39050. +#endif
  39051. + case UHF_PORT_POWER:
  39052. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39053. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  39054. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39055. + hprt0.b.prtpwr = 0;
  39056. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39057. + break;
  39058. + case UHF_PORT_INDICATOR:
  39059. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39060. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  39061. + /* Port inidicator not supported */
  39062. + break;
  39063. + case UHF_C_PORT_CONNECTION:
  39064. + /* Clears drivers internal connect status change
  39065. + * flag */
  39066. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39067. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  39068. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  39069. + break;
  39070. + case UHF_C_PORT_RESET:
  39071. + /* Clears the driver's internal Port Reset Change
  39072. + * flag */
  39073. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39074. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  39075. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  39076. + break;
  39077. + case UHF_C_PORT_ENABLE:
  39078. + /* Clears the driver's internal Port
  39079. + * Enable/Disable Change flag */
  39080. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39081. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  39082. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  39083. + break;
  39084. + case UHF_C_PORT_SUSPEND:
  39085. + /* Clears the driver's internal Port Suspend
  39086. + * Change flag, which is set when resume signaling on
  39087. + * the host port is complete */
  39088. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39089. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  39090. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  39091. + break;
  39092. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39093. + case UHF_C_PORT_L1:
  39094. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  39095. + break;
  39096. +#endif
  39097. + case UHF_C_PORT_OVER_CURRENT:
  39098. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39099. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  39100. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  39101. + break;
  39102. + default:
  39103. + retval = -DWC_E_INVALID;
  39104. + DWC_ERROR("DWC OTG HCD - "
  39105. + "ClearPortFeature request %xh "
  39106. + "unknown or unsupported\n", wValue);
  39107. + }
  39108. + break;
  39109. + case UCR_GET_HUB_DESCRIPTOR:
  39110. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39111. + "GetHubDescriptor\n");
  39112. + hub_desc = (usb_hub_descriptor_t *) buf;
  39113. + hub_desc->bDescLength = 9;
  39114. + hub_desc->bDescriptorType = 0x29;
  39115. + hub_desc->bNbrPorts = 1;
  39116. + USETW(hub_desc->wHubCharacteristics, 0x08);
  39117. + hub_desc->bPwrOn2PwrGood = 1;
  39118. + hub_desc->bHubContrCurrent = 0;
  39119. + hub_desc->DeviceRemovable[0] = 0;
  39120. + hub_desc->DeviceRemovable[1] = 0xff;
  39121. + break;
  39122. + case UCR_GET_HUB_STATUS:
  39123. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39124. + "GetHubStatus\n");
  39125. + DWC_MEMSET(buf, 0, 4);
  39126. + break;
  39127. + case UCR_GET_PORT_STATUS:
  39128. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39129. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  39130. + wIndex, dwc_otg_hcd->flags.d32);
  39131. + if (!wIndex || wIndex > 1)
  39132. + goto error;
  39133. +
  39134. + port_status = 0;
  39135. +
  39136. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  39137. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  39138. +
  39139. + if (dwc_otg_hcd->flags.b.port_enable_change)
  39140. + port_status |= (1 << UHF_C_PORT_ENABLE);
  39141. +
  39142. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  39143. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  39144. +
  39145. + if (dwc_otg_hcd->flags.b.port_l1_change)
  39146. + port_status |= (1 << UHF_C_PORT_L1);
  39147. +
  39148. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  39149. + port_status |= (1 << UHF_C_PORT_RESET);
  39150. + }
  39151. +
  39152. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  39153. + DWC_WARN("Overcurrent change detected\n");
  39154. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  39155. + }
  39156. +
  39157. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  39158. + /*
  39159. + * The port is disconnected, which means the core is
  39160. + * either in device mode or it soon will be. Just
  39161. + * return 0's for the remainder of the port status
  39162. + * since the port register can't be read if the core
  39163. + * is in device mode.
  39164. + */
  39165. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  39166. + break;
  39167. + }
  39168. +
  39169. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  39170. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  39171. +
  39172. + if (hprt0.b.prtconnsts)
  39173. + port_status |= (1 << UHF_PORT_CONNECTION);
  39174. +
  39175. + if (hprt0.b.prtena)
  39176. + port_status |= (1 << UHF_PORT_ENABLE);
  39177. +
  39178. + if (hprt0.b.prtsusp)
  39179. + port_status |= (1 << UHF_PORT_SUSPEND);
  39180. +
  39181. + if (hprt0.b.prtovrcurract)
  39182. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  39183. +
  39184. + if (hprt0.b.prtrst)
  39185. + port_status |= (1 << UHF_PORT_RESET);
  39186. +
  39187. + if (hprt0.b.prtpwr)
  39188. + port_status |= (1 << UHF_PORT_POWER);
  39189. +
  39190. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  39191. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  39192. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  39193. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  39194. +
  39195. + if (hprt0.b.prttstctl)
  39196. + port_status |= (1 << UHF_PORT_TEST);
  39197. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  39198. + port_status |= (1 << UHF_PORT_L1);
  39199. + }
  39200. + /*
  39201. + For Synopsys HW emulation of Power down wkup_control asserts the
  39202. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  39203. + We intentionally tell the software that port is in L2Suspend state.
  39204. + Only for STE.
  39205. + */
  39206. + if ((core_if->power_down == 2)
  39207. + && (core_if->hibernation_suspend == 1)) {
  39208. + port_status |= (1 << UHF_PORT_SUSPEND);
  39209. + }
  39210. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  39211. +
  39212. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  39213. +
  39214. + break;
  39215. + case UCR_SET_HUB_FEATURE:
  39216. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39217. + "SetHubFeature\n");
  39218. + /* No HUB features supported */
  39219. + break;
  39220. + case UCR_SET_PORT_FEATURE:
  39221. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  39222. + goto error;
  39223. +
  39224. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  39225. + /*
  39226. + * The port is disconnected, which means the core is
  39227. + * either in device mode or it soon will be. Just
  39228. + * return without doing anything since the port
  39229. + * register can't be written if the core is in device
  39230. + * mode.
  39231. + */
  39232. + break;
  39233. + }
  39234. +
  39235. + switch (wValue) {
  39236. + case UHF_PORT_SUSPEND:
  39237. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39238. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  39239. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  39240. + goto error;
  39241. + }
  39242. + if (core_if->power_down == 2) {
  39243. + int timeout = 300;
  39244. + dwc_irqflags_t flags;
  39245. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39246. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  39247. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  39248. +#ifdef DWC_DEV_SRPCAP
  39249. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  39250. +#endif
  39251. + DWC_PRINTF("Preparing for complete power-off\n");
  39252. +
  39253. + /* Save registers before hibernation */
  39254. + dwc_otg_save_global_regs(core_if);
  39255. + dwc_otg_save_host_regs(core_if);
  39256. +
  39257. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39258. + hprt0.b.prtsusp = 1;
  39259. + hprt0.b.prtena = 0;
  39260. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39261. + /* Spin hprt0.b.prtsusp to became 1 */
  39262. + do {
  39263. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39264. + if (hprt0.b.prtsusp) {
  39265. + break;
  39266. + }
  39267. + dwc_mdelay(1);
  39268. + } while (--timeout);
  39269. + if (!timeout) {
  39270. + DWC_WARN("Suspend wasn't genereted\n");
  39271. + }
  39272. + dwc_udelay(10);
  39273. +
  39274. + /*
  39275. + * We need to disable interrupts to prevent servicing of any IRQ
  39276. + * during going to hibernation
  39277. + */
  39278. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  39279. + core_if->lx_state = DWC_OTG_L2;
  39280. +#ifdef DWC_DEV_SRPCAP
  39281. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39282. + hprt0.b.prtpwr = 0;
  39283. + hprt0.b.prtena = 0;
  39284. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39285. + hprt0.d32);
  39286. +#endif
  39287. + gusbcfg.d32 =
  39288. + DWC_READ_REG32(&core_if->core_global_regs->
  39289. + gusbcfg);
  39290. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  39291. + /* ULPI interface */
  39292. + /* Suspend the Phy Clock */
  39293. + pcgcctl.d32 = 0;
  39294. + pcgcctl.b.stoppclk = 1;
  39295. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  39296. + pcgcctl.d32);
  39297. + dwc_udelay(10);
  39298. + gpwrdn.b.pmuactv = 1;
  39299. + DWC_MODIFY_REG32(&core_if->
  39300. + core_global_regs->
  39301. + gpwrdn, 0, gpwrdn.d32);
  39302. + } else {
  39303. + /* UTMI+ Interface */
  39304. + gpwrdn.b.pmuactv = 1;
  39305. + DWC_MODIFY_REG32(&core_if->
  39306. + core_global_regs->
  39307. + gpwrdn, 0, gpwrdn.d32);
  39308. + dwc_udelay(10);
  39309. + pcgcctl.b.stoppclk = 1;
  39310. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  39311. + dwc_udelay(10);
  39312. + }
  39313. +#ifdef DWC_DEV_SRPCAP
  39314. + gpwrdn.d32 = 0;
  39315. + gpwrdn.b.dis_vbus = 1;
  39316. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39317. + gpwrdn, 0, gpwrdn.d32);
  39318. +#endif
  39319. + gpwrdn.d32 = 0;
  39320. + gpwrdn.b.pmuintsel = 1;
  39321. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39322. + gpwrdn, 0, gpwrdn.d32);
  39323. + dwc_udelay(10);
  39324. +
  39325. + gpwrdn.d32 = 0;
  39326. +#ifdef DWC_DEV_SRPCAP
  39327. + gpwrdn.b.srp_det_msk = 1;
  39328. +#endif
  39329. + gpwrdn.b.disconn_det_msk = 1;
  39330. + gpwrdn.b.lnstchng_msk = 1;
  39331. + gpwrdn.b.sts_chngint_msk = 1;
  39332. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39333. + gpwrdn, 0, gpwrdn.d32);
  39334. + dwc_udelay(10);
  39335. +
  39336. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  39337. + gpwrdn.d32 = 0;
  39338. + gpwrdn.b.pwrdnclmp = 1;
  39339. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39340. + gpwrdn, 0, gpwrdn.d32);
  39341. + dwc_udelay(10);
  39342. +
  39343. + /* Switch off VDD */
  39344. + gpwrdn.d32 = 0;
  39345. + gpwrdn.b.pwrdnswtch = 1;
  39346. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39347. + gpwrdn, 0, gpwrdn.d32);
  39348. +
  39349. +#ifdef DWC_DEV_SRPCAP
  39350. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  39351. + {
  39352. + core_if->pwron_timer_started = 1;
  39353. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  39354. + }
  39355. +#endif
  39356. + /* Save gpwrdn register for further usage if stschng interrupt */
  39357. + core_if->gr_backup->gpwrdn_local =
  39358. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  39359. +
  39360. + /* Set flag to indicate that we are in hibernation */
  39361. + core_if->hibernation_suspend = 1;
  39362. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  39363. +
  39364. + DWC_PRINTF("Host hibernation completed\n");
  39365. + // Exit from case statement
  39366. + break;
  39367. +
  39368. + }
  39369. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  39370. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  39371. + gotgctl_data_t gotgctl = {.d32 = 0 };
  39372. + gotgctl.b.hstsethnpen = 1;
  39373. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39374. + gotgctl, 0, gotgctl.d32);
  39375. + core_if->op_state = A_SUSPEND;
  39376. + }
  39377. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39378. + hprt0.b.prtsusp = 1;
  39379. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39380. + {
  39381. + dwc_irqflags_t flags;
  39382. + /* Update lx_state */
  39383. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  39384. + core_if->lx_state = DWC_OTG_L2;
  39385. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  39386. + }
  39387. + /* Suspend the Phy Clock */
  39388. + {
  39389. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39390. + pcgcctl.b.stoppclk = 1;
  39391. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  39392. + pcgcctl.d32);
  39393. + dwc_udelay(10);
  39394. + }
  39395. +
  39396. + /* For HNP the bus must be suspended for at least 200ms. */
  39397. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  39398. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39399. + pcgcctl.b.stoppclk = 1;
  39400. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  39401. + dwc_mdelay(200);
  39402. + }
  39403. +
  39404. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  39405. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  39406. + if (core_if->adp_enable) {
  39407. + gotgctl_data_t gotgctl = {.d32 = 0 };
  39408. + gpwrdn_data_t gpwrdn;
  39409. +
  39410. + while (gotgctl.b.asesvld == 1) {
  39411. + gotgctl.d32 =
  39412. + DWC_READ_REG32(&core_if->
  39413. + core_global_regs->
  39414. + gotgctl);
  39415. + dwc_mdelay(100);
  39416. + }
  39417. +
  39418. + /* Enable Power Down Logic */
  39419. + gpwrdn.d32 = 0;
  39420. + gpwrdn.b.pmuactv = 1;
  39421. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39422. + gpwrdn, 0, gpwrdn.d32);
  39423. +
  39424. + /* Unmask SRP detected interrupt from Power Down Logic */
  39425. + gpwrdn.d32 = 0;
  39426. + gpwrdn.b.srp_det_msk = 1;
  39427. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39428. + gpwrdn, 0, gpwrdn.d32);
  39429. +
  39430. + dwc_otg_adp_probe_start(core_if);
  39431. + }
  39432. +#endif
  39433. + break;
  39434. + case UHF_PORT_POWER:
  39435. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39436. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  39437. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39438. + hprt0.b.prtpwr = 1;
  39439. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39440. + break;
  39441. + case UHF_PORT_RESET:
  39442. + if ((core_if->power_down == 2)
  39443. + && (core_if->hibernation_suspend == 1)) {
  39444. + /* If we are going to exit from Hibernated
  39445. + * state via USB RESET.
  39446. + */
  39447. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  39448. + } else {
  39449. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39450. +
  39451. + DWC_DEBUGPL(DBG_HCD,
  39452. + "DWC OTG HCD HUB CONTROL - "
  39453. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  39454. + {
  39455. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39456. + pcgcctl.b.enbl_sleep_gating = 1;
  39457. + pcgcctl.b.stoppclk = 1;
  39458. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  39459. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  39460. + }
  39461. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39462. + {
  39463. + glpmcfg_data_t lpmcfg;
  39464. + lpmcfg.d32 =
  39465. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39466. + if (lpmcfg.b.prt_sleep_sts) {
  39467. + lpmcfg.b.en_utmi_sleep = 0;
  39468. + lpmcfg.b.hird_thres &= (~(1 << 4));
  39469. + DWC_WRITE_REG32
  39470. + (&core_if->core_global_regs->glpmcfg,
  39471. + lpmcfg.d32);
  39472. + dwc_mdelay(1);
  39473. + }
  39474. + }
  39475. +#endif
  39476. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39477. + /* Clear suspend bit if resetting from suspended state. */
  39478. + hprt0.b.prtsusp = 0;
  39479. + /* When B-Host the Port reset bit is set in
  39480. + * the Start HCD Callback function, so that
  39481. + * the reset is started within 1ms of the HNP
  39482. + * success interrupt. */
  39483. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  39484. + hprt0.b.prtpwr = 1;
  39485. + hprt0.b.prtrst = 1;
  39486. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  39487. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39488. + hprt0.d32);
  39489. + }
  39490. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  39491. + dwc_mdelay(60);
  39492. + hprt0.b.prtrst = 0;
  39493. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39494. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  39495. + }
  39496. + break;
  39497. +#ifdef DWC_HS_ELECT_TST
  39498. + case UHF_PORT_TEST:
  39499. + {
  39500. + uint32_t t;
  39501. + gintmsk_data_t gintmsk;
  39502. +
  39503. + t = (wIndex >> 8); /* MSB wIndex USB */
  39504. + DWC_DEBUGPL(DBG_HCD,
  39505. + "DWC OTG HCD HUB CONTROL - "
  39506. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  39507. + t);
  39508. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  39509. + if (t < 6) {
  39510. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39511. + hprt0.b.prttstctl = t;
  39512. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39513. + hprt0.d32);
  39514. + } else {
  39515. + /* Setup global vars with reg addresses (quick and
  39516. + * dirty hack, should be cleaned up)
  39517. + */
  39518. + global_regs = core_if->core_global_regs;
  39519. + hc_global_regs =
  39520. + core_if->host_if->host_global_regs;
  39521. + hc_regs =
  39522. + (dwc_otg_hc_regs_t *) ((char *)
  39523. + global_regs +
  39524. + 0x500);
  39525. + data_fifo =
  39526. + (uint32_t *) ((char *)global_regs +
  39527. + 0x1000);
  39528. +
  39529. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  39530. + /* Save current interrupt mask */
  39531. + gintmsk.d32 =
  39532. + DWC_READ_REG32
  39533. + (&global_regs->gintmsk);
  39534. +
  39535. + /* Disable all interrupts while we muck with
  39536. + * the hardware directly
  39537. + */
  39538. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39539. +
  39540. + /* 15 second delay per the test spec */
  39541. + dwc_mdelay(15000);
  39542. +
  39543. + /* Drive suspend on the root port */
  39544. + hprt0.d32 =
  39545. + dwc_otg_read_hprt0(core_if);
  39546. + hprt0.b.prtsusp = 1;
  39547. + hprt0.b.prtres = 0;
  39548. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39549. +
  39550. + /* 15 second delay per the test spec */
  39551. + dwc_mdelay(15000);
  39552. +
  39553. + /* Drive resume on the root port */
  39554. + hprt0.d32 =
  39555. + dwc_otg_read_hprt0(core_if);
  39556. + hprt0.b.prtsusp = 0;
  39557. + hprt0.b.prtres = 1;
  39558. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39559. + dwc_mdelay(100);
  39560. +
  39561. + /* Clear the resume bit */
  39562. + hprt0.b.prtres = 0;
  39563. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39564. +
  39565. + /* Restore interrupts */
  39566. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39567. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  39568. + /* Save current interrupt mask */
  39569. + gintmsk.d32 =
  39570. + DWC_READ_REG32
  39571. + (&global_regs->gintmsk);
  39572. +
  39573. + /* Disable all interrupts while we muck with
  39574. + * the hardware directly
  39575. + */
  39576. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39577. +
  39578. + /* 15 second delay per the test spec */
  39579. + dwc_mdelay(15000);
  39580. +
  39581. + /* Send the Setup packet */
  39582. + do_setup();
  39583. +
  39584. + /* 15 second delay so nothing else happens for awhile */
  39585. + dwc_mdelay(15000);
  39586. +
  39587. + /* Restore interrupts */
  39588. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39589. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  39590. + /* Save current interrupt mask */
  39591. + gintmsk.d32 =
  39592. + DWC_READ_REG32
  39593. + (&global_regs->gintmsk);
  39594. +
  39595. + /* Disable all interrupts while we muck with
  39596. + * the hardware directly
  39597. + */
  39598. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39599. +
  39600. + /* Send the Setup packet */
  39601. + do_setup();
  39602. +
  39603. + /* 15 second delay so nothing else happens for awhile */
  39604. + dwc_mdelay(15000);
  39605. +
  39606. + /* Send the In and Ack packets */
  39607. + do_in_ack();
  39608. +
  39609. + /* 15 second delay so nothing else happens for awhile */
  39610. + dwc_mdelay(15000);
  39611. +
  39612. + /* Restore interrupts */
  39613. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39614. + }
  39615. + }
  39616. + break;
  39617. + }
  39618. +#endif /* DWC_HS_ELECT_TST */
  39619. +
  39620. + case UHF_PORT_INDICATOR:
  39621. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39622. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  39623. + /* Not supported */
  39624. + break;
  39625. + default:
  39626. + retval = -DWC_E_INVALID;
  39627. + DWC_ERROR("DWC OTG HCD - "
  39628. + "SetPortFeature request %xh "
  39629. + "unknown or unsupported\n", wValue);
  39630. + break;
  39631. + }
  39632. + break;
  39633. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39634. + case UCR_SET_AND_TEST_PORT_FEATURE:
  39635. + if (wValue != UHF_PORT_L1) {
  39636. + goto error;
  39637. + }
  39638. + {
  39639. + int portnum, hird, devaddr, remwake;
  39640. + glpmcfg_data_t lpmcfg;
  39641. + uint32_t time_usecs;
  39642. + gintsts_data_t gintsts;
  39643. + gintmsk_data_t gintmsk;
  39644. +
  39645. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  39646. + goto error;
  39647. + }
  39648. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  39649. + goto error;
  39650. + }
  39651. + /* Check if the port currently is in SLEEP state */
  39652. + lpmcfg.d32 =
  39653. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39654. + if (lpmcfg.b.prt_sleep_sts) {
  39655. + DWC_INFO("Port is already in sleep mode\n");
  39656. + buf[0] = 0; /* Return success */
  39657. + break;
  39658. + }
  39659. +
  39660. + portnum = wIndex & 0xf;
  39661. + hird = (wIndex >> 4) & 0xf;
  39662. + devaddr = (wIndex >> 8) & 0x7f;
  39663. + remwake = (wIndex >> 15);
  39664. +
  39665. + if (portnum != 1) {
  39666. + retval = -DWC_E_INVALID;
  39667. + DWC_WARN
  39668. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  39669. + portnum);
  39670. + break;
  39671. + }
  39672. +
  39673. + DWC_PRINTF
  39674. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  39675. + portnum, hird, devaddr, remwake);
  39676. + /* Disable LPM interrupt */
  39677. + gintmsk.d32 = 0;
  39678. + gintmsk.b.lpmtranrcvd = 1;
  39679. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  39680. + gintmsk.d32, 0);
  39681. +
  39682. + if (dwc_otg_hcd_send_lpm
  39683. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  39684. + retval = -DWC_E_INVALID;
  39685. + break;
  39686. + }
  39687. +
  39688. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  39689. + /* We will consider timeout if time_usecs microseconds pass,
  39690. + * and we don't receive LPM transaction status.
  39691. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  39692. + * core will set lpmtranrcvd bit.
  39693. + */
  39694. + do {
  39695. + gintsts.d32 =
  39696. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  39697. + if (gintsts.b.lpmtranrcvd) {
  39698. + break;
  39699. + }
  39700. + dwc_udelay(1);
  39701. + } while (--time_usecs);
  39702. + /* lpm_int bit will be cleared in LPM interrupt handler */
  39703. +
  39704. + /* Now fill status
  39705. + * 0x00 - Success
  39706. + * 0x10 - NYET
  39707. + * 0x11 - Timeout
  39708. + */
  39709. + if (!gintsts.b.lpmtranrcvd) {
  39710. + buf[0] = 0x3; /* Completion code is Timeout */
  39711. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  39712. + } else {
  39713. + lpmcfg.d32 =
  39714. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39715. + if (lpmcfg.b.lpm_resp == 0x3) {
  39716. + /* ACK responce from the device */
  39717. + buf[0] = 0x00; /* Success */
  39718. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  39719. + /* NYET responce from the device */
  39720. + buf[0] = 0x2;
  39721. + } else {
  39722. + /* Otherwise responce with Timeout */
  39723. + buf[0] = 0x3;
  39724. + }
  39725. + }
  39726. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  39727. + lpmcfg.b.lpm_resp);
  39728. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  39729. + gintmsk.d32);
  39730. +
  39731. + break;
  39732. + }
  39733. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  39734. + default:
  39735. +error:
  39736. + retval = -DWC_E_INVALID;
  39737. + DWC_WARN("DWC OTG HCD - "
  39738. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  39739. + typeReq, wIndex, wValue);
  39740. + break;
  39741. + }
  39742. +
  39743. + return retval;
  39744. +}
  39745. +
  39746. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39747. +/** Returns index of host channel to perform LPM transaction. */
  39748. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  39749. +{
  39750. + dwc_otg_core_if_t *core_if = hcd->core_if;
  39751. + dwc_hc_t *hc;
  39752. + hcchar_data_t hcchar;
  39753. + gintmsk_data_t gintmsk = {.d32 = 0 };
  39754. +
  39755. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  39756. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  39757. + return -1;
  39758. + }
  39759. +
  39760. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  39761. +
  39762. + /* Mask host channel interrupts. */
  39763. + gintmsk.b.hcintr = 1;
  39764. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  39765. +
  39766. + /* Fill fields that core needs for LPM transaction */
  39767. + hcchar.b.devaddr = devaddr;
  39768. + hcchar.b.epnum = 0;
  39769. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  39770. + hcchar.b.mps = 64;
  39771. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  39772. + hcchar.b.epdir = 0; /* OUT */
  39773. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  39774. + hcchar.d32);
  39775. +
  39776. + /* Remove the host channel from the free list. */
  39777. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  39778. +
  39779. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  39780. +
  39781. + return hc->hc_num;
  39782. +}
  39783. +
  39784. +/** Release hc after performing LPM transaction */
  39785. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  39786. +{
  39787. + dwc_hc_t *hc;
  39788. + glpmcfg_data_t lpmcfg;
  39789. + uint8_t hc_num;
  39790. +
  39791. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  39792. + hc_num = lpmcfg.b.lpm_chan_index;
  39793. +
  39794. + hc = hcd->hc_ptr_array[hc_num];
  39795. +
  39796. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  39797. + /* Return host channel to free list */
  39798. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  39799. +}
  39800. +
  39801. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  39802. + uint8_t bRemoteWake)
  39803. +{
  39804. + glpmcfg_data_t lpmcfg;
  39805. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39806. + int channel;
  39807. +
  39808. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  39809. + if (channel < 0) {
  39810. + return channel;
  39811. + }
  39812. +
  39813. + pcgcctl.b.enbl_sleep_gating = 1;
  39814. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  39815. +
  39816. + /* Read LPM config register */
  39817. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  39818. +
  39819. + /* Program LPM transaction fields */
  39820. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  39821. + lpmcfg.b.hird = hird;
  39822. + lpmcfg.b.hird_thres = 0x1c;
  39823. + lpmcfg.b.lpm_chan_index = channel;
  39824. + lpmcfg.b.en_utmi_sleep = 1;
  39825. + /* Program LPM config register */
  39826. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  39827. +
  39828. + /* Send LPM transaction */
  39829. + lpmcfg.b.send_lpm = 1;
  39830. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  39831. +
  39832. + return 0;
  39833. +}
  39834. +
  39835. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  39836. +
  39837. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  39838. +{
  39839. + int retval;
  39840. +
  39841. + if (port != 1) {
  39842. + return -DWC_E_INVALID;
  39843. + }
  39844. +
  39845. + retval = (hcd->flags.b.port_connect_status_change ||
  39846. + hcd->flags.b.port_reset_change ||
  39847. + hcd->flags.b.port_enable_change ||
  39848. + hcd->flags.b.port_suspend_change ||
  39849. + hcd->flags.b.port_over_current_change);
  39850. +#ifdef DEBUG
  39851. + if (retval) {
  39852. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  39853. + " Root port status changed\n");
  39854. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  39855. + hcd->flags.b.port_connect_status_change);
  39856. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  39857. + hcd->flags.b.port_reset_change);
  39858. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  39859. + hcd->flags.b.port_enable_change);
  39860. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  39861. + hcd->flags.b.port_suspend_change);
  39862. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  39863. + hcd->flags.b.port_over_current_change);
  39864. + }
  39865. +#endif
  39866. + return retval;
  39867. +}
  39868. +
  39869. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  39870. +{
  39871. + hfnum_data_t hfnum;
  39872. + hfnum.d32 =
  39873. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  39874. + hfnum);
  39875. +
  39876. +#ifdef DEBUG_SOF
  39877. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  39878. + hfnum.b.frnum);
  39879. +#endif
  39880. + return hfnum.b.frnum;
  39881. +}
  39882. +
  39883. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  39884. + struct dwc_otg_hcd_function_ops *fops)
  39885. +{
  39886. + int retval = 0;
  39887. +
  39888. + hcd->fops = fops;
  39889. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  39890. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  39891. + dwc_otg_hcd_reinit(hcd);
  39892. + } else {
  39893. + retval = -DWC_E_NO_DEVICE;
  39894. + }
  39895. +
  39896. + return retval;
  39897. +}
  39898. +
  39899. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  39900. +{
  39901. + return hcd->priv;
  39902. +}
  39903. +
  39904. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  39905. +{
  39906. + hcd->priv = priv_data;
  39907. +}
  39908. +
  39909. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  39910. +{
  39911. + return hcd->otg_port;
  39912. +}
  39913. +
  39914. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  39915. +{
  39916. + uint32_t is_b_host;
  39917. + if (hcd->core_if->op_state == B_HOST) {
  39918. + is_b_host = 1;
  39919. + } else {
  39920. + is_b_host = 0;
  39921. + }
  39922. +
  39923. + return is_b_host;
  39924. +}
  39925. +
  39926. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  39927. + int iso_desc_count, int atomic_alloc)
  39928. +{
  39929. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  39930. + uint32_t size;
  39931. +
  39932. + size =
  39933. + sizeof(*dwc_otg_urb) +
  39934. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  39935. + if (atomic_alloc)
  39936. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  39937. + else
  39938. + dwc_otg_urb = DWC_ALLOC(size);
  39939. +
  39940. + if (dwc_otg_urb)
  39941. + dwc_otg_urb->packet_count = iso_desc_count;
  39942. + else {
  39943. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  39944. + "%salloc of %db failed\n",
  39945. + atomic_alloc?"atomic ":"", size);
  39946. + }
  39947. + return dwc_otg_urb;
  39948. +}
  39949. +
  39950. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39951. + uint8_t dev_addr, uint8_t ep_num,
  39952. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  39953. +{
  39954. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  39955. + ep_type, ep_dir, mps);
  39956. +#if 0
  39957. + DWC_PRINTF
  39958. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  39959. + dev_addr, ep_num, ep_dir, ep_type, mps);
  39960. +#endif
  39961. +}
  39962. +
  39963. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39964. + void *urb_handle, void *buf, dwc_dma_t dma,
  39965. + uint32_t buflen, void *setup_packet,
  39966. + dwc_dma_t setup_dma, uint32_t flags,
  39967. + uint16_t interval)
  39968. +{
  39969. + dwc_otg_urb->priv = urb_handle;
  39970. + dwc_otg_urb->buf = buf;
  39971. + dwc_otg_urb->dma = dma;
  39972. + dwc_otg_urb->length = buflen;
  39973. + dwc_otg_urb->setup_packet = setup_packet;
  39974. + dwc_otg_urb->setup_dma = setup_dma;
  39975. + dwc_otg_urb->flags = flags;
  39976. + dwc_otg_urb->interval = interval;
  39977. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  39978. +}
  39979. +
  39980. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  39981. +{
  39982. + return dwc_otg_urb->status;
  39983. +}
  39984. +
  39985. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  39986. +{
  39987. + return dwc_otg_urb->actual_length;
  39988. +}
  39989. +
  39990. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  39991. +{
  39992. + return dwc_otg_urb->error_count;
  39993. +}
  39994. +
  39995. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39996. + int desc_num, uint32_t offset,
  39997. + uint32_t length)
  39998. +{
  39999. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  40000. + dwc_otg_urb->iso_descs[desc_num].length = length;
  40001. +}
  40002. +
  40003. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  40004. + int desc_num)
  40005. +{
  40006. + return dwc_otg_urb->iso_descs[desc_num].status;
  40007. +}
  40008. +
  40009. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  40010. + dwc_otg_urb, int desc_num)
  40011. +{
  40012. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  40013. +}
  40014. +
  40015. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  40016. +{
  40017. + int allocated = 0;
  40018. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40019. +
  40020. + if (qh) {
  40021. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  40022. + allocated = 1;
  40023. + }
  40024. + }
  40025. + return allocated;
  40026. +}
  40027. +
  40028. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  40029. +{
  40030. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40031. + int freed = 0;
  40032. + DWC_ASSERT(qh, "qh is not allocated\n");
  40033. +
  40034. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  40035. + freed = 1;
  40036. + }
  40037. +
  40038. + return freed;
  40039. +}
  40040. +
  40041. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  40042. +{
  40043. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40044. + DWC_ASSERT(qh, "qh is not allocated\n");
  40045. + return qh->usecs;
  40046. +}
  40047. +
  40048. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  40049. +{
  40050. +#ifdef DEBUG
  40051. + int num_channels;
  40052. + int i;
  40053. + gnptxsts_data_t np_tx_status;
  40054. + hptxsts_data_t p_tx_status;
  40055. +
  40056. + num_channels = hcd->core_if->core_params->host_channels;
  40057. + DWC_PRINTF("\n");
  40058. + DWC_PRINTF
  40059. + ("************************************************************\n");
  40060. + DWC_PRINTF("HCD State:\n");
  40061. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  40062. + for (i = 0; i < num_channels; i++) {
  40063. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  40064. + DWC_PRINTF(" Channel %d:\n", i);
  40065. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  40066. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  40067. + DWC_PRINTF(" speed: %d\n", hc->speed);
  40068. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  40069. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  40070. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  40071. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  40072. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  40073. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  40074. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  40075. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  40076. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  40077. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  40078. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  40079. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  40080. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  40081. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  40082. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  40083. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  40084. + DWC_PRINTF(" requests: %d\n", hc->requests);
  40085. + DWC_PRINTF(" qh: %p\n", hc->qh);
  40086. + if (hc->xfer_started) {
  40087. + hfnum_data_t hfnum;
  40088. + hcchar_data_t hcchar;
  40089. + hctsiz_data_t hctsiz;
  40090. + hcint_data_t hcint;
  40091. + hcintmsk_data_t hcintmsk;
  40092. + hfnum.d32 =
  40093. + DWC_READ_REG32(&hcd->core_if->
  40094. + host_if->host_global_regs->hfnum);
  40095. + hcchar.d32 =
  40096. + DWC_READ_REG32(&hcd->core_if->host_if->
  40097. + hc_regs[i]->hcchar);
  40098. + hctsiz.d32 =
  40099. + DWC_READ_REG32(&hcd->core_if->host_if->
  40100. + hc_regs[i]->hctsiz);
  40101. + hcint.d32 =
  40102. + DWC_READ_REG32(&hcd->core_if->host_if->
  40103. + hc_regs[i]->hcint);
  40104. + hcintmsk.d32 =
  40105. + DWC_READ_REG32(&hcd->core_if->host_if->
  40106. + hc_regs[i]->hcintmsk);
  40107. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  40108. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  40109. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  40110. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  40111. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  40112. + }
  40113. + if (hc->xfer_started && hc->qh) {
  40114. + dwc_otg_qtd_t *qtd;
  40115. + dwc_otg_hcd_urb_t *urb;
  40116. +
  40117. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  40118. + if (!qtd->in_process)
  40119. + break;
  40120. +
  40121. + urb = qtd->urb;
  40122. + DWC_PRINTF(" URB Info:\n");
  40123. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  40124. + if (urb) {
  40125. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  40126. + dwc_otg_hcd_get_dev_addr(&urb->
  40127. + pipe_info),
  40128. + dwc_otg_hcd_get_ep_num(&urb->
  40129. + pipe_info),
  40130. + dwc_otg_hcd_is_pipe_in(&urb->
  40131. + pipe_info) ?
  40132. + "IN" : "OUT");
  40133. + DWC_PRINTF(" Max packet size: %d\n",
  40134. + dwc_otg_hcd_get_mps(&urb->
  40135. + pipe_info));
  40136. + DWC_PRINTF(" transfer_buffer: %p\n",
  40137. + urb->buf);
  40138. + DWC_PRINTF(" transfer_dma: %p\n",
  40139. + (void *)urb->dma);
  40140. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  40141. + urb->length);
  40142. + DWC_PRINTF(" actual_length: %d\n",
  40143. + urb->actual_length);
  40144. + }
  40145. + }
  40146. + }
  40147. + }
  40148. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  40149. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  40150. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  40151. + np_tx_status.d32 =
  40152. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  40153. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  40154. + np_tx_status.b.nptxqspcavail);
  40155. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  40156. + np_tx_status.b.nptxfspcavail);
  40157. + p_tx_status.d32 =
  40158. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  40159. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  40160. + p_tx_status.b.ptxqspcavail);
  40161. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  40162. + dwc_otg_hcd_dump_frrem(hcd);
  40163. + dwc_otg_dump_global_registers(hcd->core_if);
  40164. + dwc_otg_dump_host_registers(hcd->core_if);
  40165. + DWC_PRINTF
  40166. + ("************************************************************\n");
  40167. + DWC_PRINTF("\n");
  40168. +#endif
  40169. +}
  40170. +
  40171. +#ifdef DEBUG
  40172. +void dwc_print_setup_data(uint8_t * setup)
  40173. +{
  40174. + int i;
  40175. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  40176. + DWC_PRINTF("Setup Data = MSB ");
  40177. + for (i = 7; i >= 0; i--)
  40178. + DWC_PRINTF("%02x ", setup[i]);
  40179. + DWC_PRINTF("\n");
  40180. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  40181. + (setup[0] & 0x80) ? "Device-to-Host" :
  40182. + "Host-to-Device");
  40183. + DWC_PRINTF(" bmRequestType Type = ");
  40184. + switch ((setup[0] & 0x60) >> 5) {
  40185. + case 0:
  40186. + DWC_PRINTF("Standard\n");
  40187. + break;
  40188. + case 1:
  40189. + DWC_PRINTF("Class\n");
  40190. + break;
  40191. + case 2:
  40192. + DWC_PRINTF("Vendor\n");
  40193. + break;
  40194. + case 3:
  40195. + DWC_PRINTF("Reserved\n");
  40196. + break;
  40197. + }
  40198. + DWC_PRINTF(" bmRequestType Recipient = ");
  40199. + switch (setup[0] & 0x1f) {
  40200. + case 0:
  40201. + DWC_PRINTF("Device\n");
  40202. + break;
  40203. + case 1:
  40204. + DWC_PRINTF("Interface\n");
  40205. + break;
  40206. + case 2:
  40207. + DWC_PRINTF("Endpoint\n");
  40208. + break;
  40209. + case 3:
  40210. + DWC_PRINTF("Other\n");
  40211. + break;
  40212. + default:
  40213. + DWC_PRINTF("Reserved\n");
  40214. + break;
  40215. + }
  40216. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  40217. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  40218. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  40219. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  40220. + }
  40221. +}
  40222. +#endif
  40223. +
  40224. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  40225. +{
  40226. +#if 0
  40227. + DWC_PRINTF("Frame remaining at SOF:\n");
  40228. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40229. + hcd->frrem_samples, hcd->frrem_accum,
  40230. + (hcd->frrem_samples > 0) ?
  40231. + hcd->frrem_accum / hcd->frrem_samples : 0);
  40232. +
  40233. + DWC_PRINTF("\n");
  40234. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  40235. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40236. + hcd->core_if->hfnum_7_samples,
  40237. + hcd->core_if->hfnum_7_frrem_accum,
  40238. + (hcd->core_if->hfnum_7_samples >
  40239. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  40240. + hcd->core_if->hfnum_7_samples : 0);
  40241. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  40242. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40243. + hcd->core_if->hfnum_0_samples,
  40244. + hcd->core_if->hfnum_0_frrem_accum,
  40245. + (hcd->core_if->hfnum_0_samples >
  40246. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  40247. + hcd->core_if->hfnum_0_samples : 0);
  40248. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  40249. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40250. + hcd->core_if->hfnum_other_samples,
  40251. + hcd->core_if->hfnum_other_frrem_accum,
  40252. + (hcd->core_if->hfnum_other_samples >
  40253. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  40254. + hcd->core_if->hfnum_other_samples : 0);
  40255. +
  40256. + DWC_PRINTF("\n");
  40257. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  40258. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40259. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  40260. + (hcd->hfnum_7_samples_a > 0) ?
  40261. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  40262. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  40263. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40264. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  40265. + (hcd->hfnum_0_samples_a > 0) ?
  40266. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  40267. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  40268. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40269. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  40270. + (hcd->hfnum_other_samples_a > 0) ?
  40271. + hcd->hfnum_other_frrem_accum_a /
  40272. + hcd->hfnum_other_samples_a : 0);
  40273. +
  40274. + DWC_PRINTF("\n");
  40275. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  40276. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40277. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  40278. + (hcd->hfnum_7_samples_b > 0) ?
  40279. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  40280. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  40281. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40282. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  40283. + (hcd->hfnum_0_samples_b > 0) ?
  40284. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  40285. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  40286. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40287. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  40288. + (hcd->hfnum_other_samples_b > 0) ?
  40289. + hcd->hfnum_other_frrem_accum_b /
  40290. + hcd->hfnum_other_samples_b : 0);
  40291. +#endif
  40292. +}
  40293. +
  40294. +#endif /* DWC_DEVICE_ONLY */
  40295. --- /dev/null
  40296. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  40297. @@ -0,0 +1,862 @@
  40298. +/* ==========================================================================
  40299. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  40300. + * $Revision: #58 $
  40301. + * $Date: 2011/09/15 $
  40302. + * $Change: 1846647 $
  40303. + *
  40304. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  40305. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  40306. + * otherwise expressly agreed to in writing between Synopsys and you.
  40307. + *
  40308. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  40309. + * any End User Software License Agreement or Agreement for Licensed Product
  40310. + * with Synopsys or any supplement thereto. You are permitted to use and
  40311. + * redistribute this Software in source and binary forms, with or without
  40312. + * modification, provided that redistributions of source code must retain this
  40313. + * notice. You may not view, use, disclose, copy or distribute this file or
  40314. + * any information contained herein except pursuant to this license grant from
  40315. + * Synopsys. If you do not agree with this notice, including the disclaimer
  40316. + * below, then you are not authorized to use the Software.
  40317. + *
  40318. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  40319. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40320. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40321. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  40322. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  40323. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  40324. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40325. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40326. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40327. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40328. + * DAMAGE.
  40329. + * ========================================================================== */
  40330. +#ifndef DWC_DEVICE_ONLY
  40331. +#ifndef __DWC_HCD_H__
  40332. +#define __DWC_HCD_H__
  40333. +
  40334. +#include "dwc_otg_os_dep.h"
  40335. +#include "usb.h"
  40336. +#include "dwc_otg_hcd_if.h"
  40337. +#include "dwc_otg_core_if.h"
  40338. +#include "dwc_list.h"
  40339. +#include "dwc_otg_cil.h"
  40340. +#include "dwc_otg_fiq_fsm.h"
  40341. +
  40342. +
  40343. +/**
  40344. + * @file
  40345. + *
  40346. + * This file contains the structures, constants, and interfaces for
  40347. + * the Host Contoller Driver (HCD).
  40348. + *
  40349. + * The Host Controller Driver (HCD) is responsible for translating requests
  40350. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  40351. + * It isolates the USBD from the specifics of the controller by providing an
  40352. + * API to the USBD.
  40353. + */
  40354. +
  40355. +struct dwc_otg_hcd_pipe_info {
  40356. + uint8_t dev_addr;
  40357. + uint8_t ep_num;
  40358. + uint8_t pipe_type;
  40359. + uint8_t pipe_dir;
  40360. + uint16_t mps;
  40361. +};
  40362. +
  40363. +struct dwc_otg_hcd_iso_packet_desc {
  40364. + uint32_t offset;
  40365. + uint32_t length;
  40366. + uint32_t actual_length;
  40367. + uint32_t status;
  40368. +};
  40369. +
  40370. +struct dwc_otg_qtd;
  40371. +
  40372. +struct dwc_otg_hcd_urb {
  40373. + void *priv;
  40374. + struct dwc_otg_qtd *qtd;
  40375. + void *buf;
  40376. + dwc_dma_t dma;
  40377. + void *setup_packet;
  40378. + dwc_dma_t setup_dma;
  40379. + uint32_t length;
  40380. + uint32_t actual_length;
  40381. + uint32_t status;
  40382. + uint32_t error_count;
  40383. + uint32_t packet_count;
  40384. + uint32_t flags;
  40385. + uint16_t interval;
  40386. + struct dwc_otg_hcd_pipe_info pipe_info;
  40387. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  40388. +};
  40389. +
  40390. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  40391. +{
  40392. + return pipe->ep_num;
  40393. +}
  40394. +
  40395. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  40396. + *pipe)
  40397. +{
  40398. + return pipe->pipe_type;
  40399. +}
  40400. +
  40401. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  40402. +{
  40403. + return pipe->mps;
  40404. +}
  40405. +
  40406. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  40407. + *pipe)
  40408. +{
  40409. + return pipe->dev_addr;
  40410. +}
  40411. +
  40412. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  40413. + *pipe)
  40414. +{
  40415. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  40416. +}
  40417. +
  40418. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  40419. + *pipe)
  40420. +{
  40421. + return (pipe->pipe_type == UE_INTERRUPT);
  40422. +}
  40423. +
  40424. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  40425. + *pipe)
  40426. +{
  40427. + return (pipe->pipe_type == UE_BULK);
  40428. +}
  40429. +
  40430. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  40431. + *pipe)
  40432. +{
  40433. + return (pipe->pipe_type == UE_CONTROL);
  40434. +}
  40435. +
  40436. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  40437. +{
  40438. + return (pipe->pipe_dir == UE_DIR_IN);
  40439. +}
  40440. +
  40441. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  40442. + *pipe)
  40443. +{
  40444. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  40445. +}
  40446. +
  40447. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  40448. + uint8_t devaddr, uint8_t ep_num,
  40449. + uint8_t pipe_type, uint8_t pipe_dir,
  40450. + uint16_t mps)
  40451. +{
  40452. + pipe->dev_addr = devaddr;
  40453. + pipe->ep_num = ep_num;
  40454. + pipe->pipe_type = pipe_type;
  40455. + pipe->pipe_dir = pipe_dir;
  40456. + pipe->mps = mps;
  40457. +}
  40458. +
  40459. +/**
  40460. + * Phases for control transfers.
  40461. + */
  40462. +typedef enum dwc_otg_control_phase {
  40463. + DWC_OTG_CONTROL_SETUP,
  40464. + DWC_OTG_CONTROL_DATA,
  40465. + DWC_OTG_CONTROL_STATUS
  40466. +} dwc_otg_control_phase_e;
  40467. +
  40468. +/** Transaction types. */
  40469. +typedef enum dwc_otg_transaction_type {
  40470. + DWC_OTG_TRANSACTION_NONE = 0,
  40471. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  40472. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  40473. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  40474. +} dwc_otg_transaction_type_e;
  40475. +
  40476. +struct dwc_otg_qh;
  40477. +
  40478. +/**
  40479. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  40480. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  40481. + * (of one of these types) submitted to the HCD. The transfer associated with
  40482. + * a QTD may require one or multiple transactions.
  40483. + *
  40484. + * A QTD is linked to a Queue Head, which is entered in either the
  40485. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  40486. + * execution, some or all of its transactions may be executed. After
  40487. + * execution, the state of the QTD is updated. The QTD may be retired if all
  40488. + * its transactions are complete or if an error occurred. Otherwise, it
  40489. + * remains in the schedule so more transactions can be executed later.
  40490. + */
  40491. +typedef struct dwc_otg_qtd {
  40492. + /**
  40493. + * Determines the PID of the next data packet for the data phase of
  40494. + * control transfers. Ignored for other transfer types.<br>
  40495. + * One of the following values:
  40496. + * - DWC_OTG_HC_PID_DATA0
  40497. + * - DWC_OTG_HC_PID_DATA1
  40498. + */
  40499. + uint8_t data_toggle;
  40500. +
  40501. + /** Current phase for control transfers (Setup, Data, or Status). */
  40502. + dwc_otg_control_phase_e control_phase;
  40503. +
  40504. + /** Keep track of the current split type
  40505. + * for FS/LS endpoints on a HS Hub */
  40506. + uint8_t complete_split;
  40507. +
  40508. + /** How many bytes transferred during SSPLIT OUT */
  40509. + uint32_t ssplit_out_xfer_count;
  40510. +
  40511. + /**
  40512. + * Holds the number of bus errors that have occurred for a transaction
  40513. + * within this transfer.
  40514. + */
  40515. + uint8_t error_count;
  40516. +
  40517. + /**
  40518. + * Index of the next frame descriptor for an isochronous transfer. A
  40519. + * frame descriptor describes the buffer position and length of the
  40520. + * data to be transferred in the next scheduled (micro)frame of an
  40521. + * isochronous transfer. It also holds status for that transaction.
  40522. + * The frame index starts at 0.
  40523. + */
  40524. + uint16_t isoc_frame_index;
  40525. +
  40526. + /** Position of the ISOC split on full/low speed */
  40527. + uint8_t isoc_split_pos;
  40528. +
  40529. + /** Position of the ISOC split in the buffer for the current frame */
  40530. + uint16_t isoc_split_offset;
  40531. +
  40532. + /** URB for this transfer */
  40533. + struct dwc_otg_hcd_urb *urb;
  40534. +
  40535. + struct dwc_otg_qh *qh;
  40536. +
  40537. + /** This list of QTDs */
  40538. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  40539. +
  40540. + /** Indicates if this QTD is currently processed by HW. */
  40541. + uint8_t in_process;
  40542. +
  40543. + /** Number of DMA descriptors for this QTD */
  40544. + uint8_t n_desc;
  40545. +
  40546. + /**
  40547. + * Last activated frame(packet) index.
  40548. + * Used in Descriptor DMA mode only.
  40549. + */
  40550. + uint16_t isoc_frame_index_last;
  40551. +
  40552. +} dwc_otg_qtd_t;
  40553. +
  40554. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  40555. +
  40556. +/**
  40557. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  40558. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  40559. + * be entered in either the non-periodic or periodic schedule.
  40560. + */
  40561. +typedef struct dwc_otg_qh {
  40562. + /**
  40563. + * Endpoint type.
  40564. + * One of the following values:
  40565. + * - UE_CONTROL
  40566. + * - UE_BULK
  40567. + * - UE_INTERRUPT
  40568. + * - UE_ISOCHRONOUS
  40569. + */
  40570. + uint8_t ep_type;
  40571. + uint8_t ep_is_in;
  40572. +
  40573. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  40574. + uint16_t maxp;
  40575. +
  40576. + /**
  40577. + * Device speed.
  40578. + * One of the following values:
  40579. + * - DWC_OTG_EP_SPEED_LOW
  40580. + * - DWC_OTG_EP_SPEED_FULL
  40581. + * - DWC_OTG_EP_SPEED_HIGH
  40582. + */
  40583. + uint8_t dev_speed;
  40584. +
  40585. + /**
  40586. + * Determines the PID of the next data packet for non-control
  40587. + * transfers. Ignored for control transfers.<br>
  40588. + * One of the following values:
  40589. + * - DWC_OTG_HC_PID_DATA0
  40590. + * - DWC_OTG_HC_PID_DATA1
  40591. + */
  40592. + uint8_t data_toggle;
  40593. +
  40594. + /** Ping state if 1. */
  40595. + uint8_t ping_state;
  40596. +
  40597. + /**
  40598. + * List of QTDs for this QH.
  40599. + */
  40600. + struct dwc_otg_qtd_list qtd_list;
  40601. +
  40602. + /** Host channel currently processing transfers for this QH. */
  40603. + struct dwc_hc *channel;
  40604. +
  40605. + /** Full/low speed endpoint on high-speed hub requires split. */
  40606. + uint8_t do_split;
  40607. +
  40608. + /** @name Periodic schedule information */
  40609. + /** @{ */
  40610. +
  40611. + /** Bandwidth in microseconds per (micro)frame. */
  40612. + uint16_t usecs;
  40613. +
  40614. + /** Interval between transfers in (micro)frames. */
  40615. + uint16_t interval;
  40616. +
  40617. + /**
  40618. + * (micro)frame to initialize a periodic transfer. The transfer
  40619. + * executes in the following (micro)frame.
  40620. + */
  40621. + uint16_t sched_frame;
  40622. +
  40623. + /*
  40624. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  40625. + */
  40626. + uint16_t nak_frame;
  40627. +
  40628. + /** (micro)frame at which last start split was initialized. */
  40629. + uint16_t start_split_frame;
  40630. +
  40631. + /** @} */
  40632. +
  40633. + /**
  40634. + * Used instead of original buffer if
  40635. + * it(physical address) is not dword-aligned.
  40636. + */
  40637. + uint8_t *dw_align_buf;
  40638. + dwc_dma_t dw_align_buf_dma;
  40639. +
  40640. + /** Entry for QH in either the periodic or non-periodic schedule. */
  40641. + dwc_list_link_t qh_list_entry;
  40642. +
  40643. + /** @name Descriptor DMA support */
  40644. + /** @{ */
  40645. +
  40646. + /** Descriptor List. */
  40647. + dwc_otg_host_dma_desc_t *desc_list;
  40648. +
  40649. + /** Descriptor List physical address. */
  40650. + dwc_dma_t desc_list_dma;
  40651. +
  40652. + /**
  40653. + * Xfer Bytes array.
  40654. + * Each element corresponds to a descriptor and indicates
  40655. + * original XferSize size value for the descriptor.
  40656. + */
  40657. + uint32_t *n_bytes;
  40658. +
  40659. + /** Actual number of transfer descriptors in a list. */
  40660. + uint16_t ntd;
  40661. +
  40662. + /** First activated isochronous transfer descriptor index. */
  40663. + uint8_t td_first;
  40664. + /** Last activated isochronous transfer descriptor index. */
  40665. + uint8_t td_last;
  40666. +
  40667. + /** @} */
  40668. +
  40669. +
  40670. + uint16_t speed;
  40671. + uint16_t frame_usecs[8];
  40672. +
  40673. + uint32_t skip_count;
  40674. +} dwc_otg_qh_t;
  40675. +
  40676. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  40677. +
  40678. +typedef struct urb_tq_entry {
  40679. + struct urb *urb;
  40680. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  40681. +} urb_tq_entry_t;
  40682. +
  40683. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  40684. +
  40685. +/**
  40686. + * This structure holds the state of the HCD, including the non-periodic and
  40687. + * periodic schedules.
  40688. + */
  40689. +struct dwc_otg_hcd {
  40690. + /** The DWC otg device pointer */
  40691. + struct dwc_otg_device *otg_dev;
  40692. + /** DWC OTG Core Interface Layer */
  40693. + dwc_otg_core_if_t *core_if;
  40694. +
  40695. + /** Function HCD driver callbacks */
  40696. + struct dwc_otg_hcd_function_ops *fops;
  40697. +
  40698. + /** Internal DWC HCD Flags */
  40699. + volatile union dwc_otg_hcd_internal_flags {
  40700. + uint32_t d32;
  40701. + struct {
  40702. + unsigned port_connect_status_change:1;
  40703. + unsigned port_connect_status:1;
  40704. + unsigned port_reset_change:1;
  40705. + unsigned port_enable_change:1;
  40706. + unsigned port_suspend_change:1;
  40707. + unsigned port_over_current_change:1;
  40708. + unsigned port_l1_change:1;
  40709. + unsigned reserved:26;
  40710. + } b;
  40711. + } flags;
  40712. +
  40713. + /**
  40714. + * Inactive items in the non-periodic schedule. This is a list of
  40715. + * Queue Heads. Transfers associated with these Queue Heads are not
  40716. + * currently assigned to a host channel.
  40717. + */
  40718. + dwc_list_link_t non_periodic_sched_inactive;
  40719. +
  40720. + /**
  40721. + * Active items in the non-periodic schedule. This is a list of
  40722. + * Queue Heads. Transfers associated with these Queue Heads are
  40723. + * currently assigned to a host channel.
  40724. + */
  40725. + dwc_list_link_t non_periodic_sched_active;
  40726. +
  40727. + /**
  40728. + * Pointer to the next Queue Head to process in the active
  40729. + * non-periodic schedule.
  40730. + */
  40731. + dwc_list_link_t *non_periodic_qh_ptr;
  40732. +
  40733. + /**
  40734. + * Inactive items in the periodic schedule. This is a list of QHs for
  40735. + * periodic transfers that are _not_ scheduled for the next frame.
  40736. + * Each QH in the list has an interval counter that determines when it
  40737. + * needs to be scheduled for execution. This scheduling mechanism
  40738. + * allows only a simple calculation for periodic bandwidth used (i.e.
  40739. + * must assume that all periodic transfers may need to execute in the
  40740. + * same frame). However, it greatly simplifies scheduling and should
  40741. + * be sufficient for the vast majority of OTG hosts, which need to
  40742. + * connect to a small number of peripherals at one time.
  40743. + *
  40744. + * Items move from this list to periodic_sched_ready when the QH
  40745. + * interval counter is 0 at SOF.
  40746. + */
  40747. + dwc_list_link_t periodic_sched_inactive;
  40748. +
  40749. + /**
  40750. + * List of periodic QHs that are ready for execution in the next
  40751. + * frame, but have not yet been assigned to host channels.
  40752. + *
  40753. + * Items move from this list to periodic_sched_assigned as host
  40754. + * channels become available during the current frame.
  40755. + */
  40756. + dwc_list_link_t periodic_sched_ready;
  40757. +
  40758. + /**
  40759. + * List of periodic QHs to be executed in the next frame that are
  40760. + * assigned to host channels.
  40761. + *
  40762. + * Items move from this list to periodic_sched_queued as the
  40763. + * transactions for the QH are queued to the DWC_otg controller.
  40764. + */
  40765. + dwc_list_link_t periodic_sched_assigned;
  40766. +
  40767. + /**
  40768. + * List of periodic QHs that have been queued for execution.
  40769. + *
  40770. + * Items move from this list to either periodic_sched_inactive or
  40771. + * periodic_sched_ready when the channel associated with the transfer
  40772. + * is released. If the interval for the QH is 1, the item moves to
  40773. + * periodic_sched_ready because it must be rescheduled for the next
  40774. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  40775. + */
  40776. + dwc_list_link_t periodic_sched_queued;
  40777. +
  40778. + /**
  40779. + * Total bandwidth claimed so far for periodic transfers. This value
  40780. + * is in microseconds per (micro)frame. The assumption is that all
  40781. + * periodic transfers may occur in the same (micro)frame.
  40782. + */
  40783. + uint16_t periodic_usecs;
  40784. +
  40785. + /**
  40786. + * Total bandwidth claimed so far for all periodic transfers
  40787. + * in a frame.
  40788. + * This will include a mixture of HS and FS transfers.
  40789. + * Units are microseconds per (micro)frame.
  40790. + * We have a budget per frame and have to schedule
  40791. + * transactions accordingly.
  40792. + * Watch out for the fact that things are actually scheduled for the
  40793. + * "next frame".
  40794. + */
  40795. + uint16_t frame_usecs[8];
  40796. +
  40797. +
  40798. + /**
  40799. + * Frame number read from the core at SOF. The value ranges from 0 to
  40800. + * DWC_HFNUM_MAX_FRNUM.
  40801. + */
  40802. + uint16_t frame_number;
  40803. +
  40804. + /**
  40805. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  40806. + */
  40807. + uint16_t periodic_qh_count;
  40808. +
  40809. + /**
  40810. + * Free host channels in the controller. This is a list of
  40811. + * dwc_hc_t items.
  40812. + */
  40813. + struct hc_list free_hc_list;
  40814. + /**
  40815. + * Number of host channels assigned to periodic transfers. Currently
  40816. + * assuming that there is a dedicated host channel for each periodic
  40817. + * transaction and at least one host channel available for
  40818. + * non-periodic transactions.
  40819. + */
  40820. + int periodic_channels; /* microframe_schedule==0 */
  40821. +
  40822. + /**
  40823. + * Number of host channels assigned to non-periodic transfers.
  40824. + */
  40825. + int non_periodic_channels; /* microframe_schedule==0 */
  40826. +
  40827. + /**
  40828. + * Number of host channels assigned to non-periodic transfers.
  40829. + */
  40830. + int available_host_channels;
  40831. +
  40832. + /**
  40833. + * Array of pointers to the host channel descriptors. Allows accessing
  40834. + * a host channel descriptor given the host channel number. This is
  40835. + * useful in interrupt handlers.
  40836. + */
  40837. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  40838. +
  40839. + /**
  40840. + * Buffer to use for any data received during the status phase of a
  40841. + * control transfer. Normally no data is transferred during the status
  40842. + * phase. This buffer is used as a bit bucket.
  40843. + */
  40844. + uint8_t *status_buf;
  40845. +
  40846. + /**
  40847. + * DMA address for status_buf.
  40848. + */
  40849. + dma_addr_t status_buf_dma;
  40850. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  40851. +
  40852. + /**
  40853. + * Connection timer. An OTG host must display a message if the device
  40854. + * does not connect. Started when the VBus power is turned on via
  40855. + * sysfs attribute "buspower".
  40856. + */
  40857. + dwc_timer_t *conn_timer;
  40858. +
  40859. + /* Tasket to do a reset */
  40860. + dwc_tasklet_t *reset_tasklet;
  40861. +
  40862. + dwc_tasklet_t *completion_tasklet;
  40863. + struct urb_list completed_urb_list;
  40864. +
  40865. + /* */
  40866. + dwc_spinlock_t *lock;
  40867. + dwc_spinlock_t *channel_lock;
  40868. + /**
  40869. + * Private data that could be used by OS wrapper.
  40870. + */
  40871. + void *priv;
  40872. +
  40873. + uint8_t otg_port;
  40874. +
  40875. + /** Frame List */
  40876. + uint32_t *frame_list;
  40877. +
  40878. + /** Hub - Port assignment */
  40879. + int hub_port[128];
  40880. +#ifdef FIQ_DEBUG
  40881. + int hub_port_alloc[2048];
  40882. +#endif
  40883. +
  40884. + /** Frame List DMA address */
  40885. + dma_addr_t frame_list_dma;
  40886. +
  40887. + struct fiq_stack *fiq_stack;
  40888. + struct fiq_state *fiq_state;
  40889. +
  40890. + /** Virtual address for split transaction DMA bounce buffers */
  40891. + struct fiq_dma_blob *fiq_dmab;
  40892. +
  40893. +#ifdef DEBUG
  40894. + uint32_t frrem_samples;
  40895. + uint64_t frrem_accum;
  40896. +
  40897. + uint32_t hfnum_7_samples_a;
  40898. + uint64_t hfnum_7_frrem_accum_a;
  40899. + uint32_t hfnum_0_samples_a;
  40900. + uint64_t hfnum_0_frrem_accum_a;
  40901. + uint32_t hfnum_other_samples_a;
  40902. + uint64_t hfnum_other_frrem_accum_a;
  40903. +
  40904. + uint32_t hfnum_7_samples_b;
  40905. + uint64_t hfnum_7_frrem_accum_b;
  40906. + uint32_t hfnum_0_samples_b;
  40907. + uint64_t hfnum_0_frrem_accum_b;
  40908. + uint32_t hfnum_other_samples_b;
  40909. + uint64_t hfnum_other_frrem_accum_b;
  40910. +#endif
  40911. +};
  40912. +
  40913. +/** @name Transaction Execution Functions */
  40914. +/** @{ */
  40915. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  40916. + * hcd);
  40917. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  40918. + dwc_otg_transaction_type_e tr_type);
  40919. +
  40920. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  40921. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  40922. +
  40923. +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  40924. +extern int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh);
  40925. +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
  40926. +
  40927. +/** @} */
  40928. +
  40929. +/** @name Interrupt Handler Functions */
  40930. +/** @{ */
  40931. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40932. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40933. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  40934. + dwc_otg_hcd);
  40935. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  40936. + dwc_otg_hcd);
  40937. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  40938. + dwc_otg_hcd);
  40939. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  40940. + dwc_otg_hcd);
  40941. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40942. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  40943. + dwc_otg_hcd);
  40944. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40945. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40946. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  40947. + uint32_t num);
  40948. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40949. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  40950. + dwc_otg_hcd);
  40951. +/** @} */
  40952. +
  40953. +/** @name Schedule Queue Functions */
  40954. +/** @{ */
  40955. +
  40956. +/* Implemented in dwc_otg_hcd_queue.c */
  40957. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  40958. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  40959. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40960. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40961. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40962. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  40963. + int sched_csplit);
  40964. +
  40965. +/** Remove and free a QH */
  40966. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  40967. + dwc_otg_qh_t * qh)
  40968. +{
  40969. + dwc_irqflags_t flags;
  40970. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  40971. + dwc_otg_hcd_qh_remove(hcd, qh);
  40972. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  40973. + dwc_otg_hcd_qh_free(hcd, qh);
  40974. +}
  40975. +
  40976. +/** Allocates memory for a QH structure.
  40977. + * @return Returns the memory allocate or NULL on error. */
  40978. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  40979. +{
  40980. + if (atomic_alloc)
  40981. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  40982. + else
  40983. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  40984. +}
  40985. +
  40986. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  40987. + int atomic_alloc);
  40988. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  40989. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  40990. + dwc_otg_qh_t ** qh, int atomic_alloc);
  40991. +
  40992. +/** Allocates memory for a QTD structure.
  40993. + * @return Returns the memory allocate or NULL on error. */
  40994. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  40995. +{
  40996. + if (atomic_alloc)
  40997. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  40998. + else
  40999. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  41000. +}
  41001. +
  41002. +/** Frees the memory for a QTD structure. QTD should already be removed from
  41003. + * list.
  41004. + * @param qtd QTD to free.*/
  41005. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  41006. +{
  41007. + DWC_FREE(qtd);
  41008. +}
  41009. +
  41010. +/** Removes a QTD from list.
  41011. + * @param hcd HCD instance.
  41012. + * @param qtd QTD to remove from list.
  41013. + * @param qh QTD belongs to.
  41014. + */
  41015. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  41016. + dwc_otg_qtd_t * qtd,
  41017. + dwc_otg_qh_t * qh)
  41018. +{
  41019. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  41020. +}
  41021. +
  41022. +/** Remove and free a QTD
  41023. + * Need to disable IRQ and hold hcd lock while calling this function out of
  41024. + * interrupt servicing chain */
  41025. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  41026. + dwc_otg_qtd_t * qtd,
  41027. + dwc_otg_qh_t * qh)
  41028. +{
  41029. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  41030. + dwc_otg_hcd_qtd_free(qtd);
  41031. +}
  41032. +
  41033. +/** @} */
  41034. +
  41035. +/** @name Descriptor DMA Supporting Functions */
  41036. +/** @{ */
  41037. +
  41038. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41039. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  41040. + dwc_hc_t * hc,
  41041. + dwc_otg_hc_regs_t * hc_regs,
  41042. + dwc_otg_halt_status_e halt_status);
  41043. +
  41044. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41045. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41046. +
  41047. +/** @} */
  41048. +
  41049. +/** @name Internal Functions */
  41050. +/** @{ */
  41051. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  41052. +/** @} */
  41053. +
  41054. +#ifdef CONFIG_USB_DWC_OTG_LPM
  41055. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  41056. + uint8_t devaddr);
  41057. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  41058. +#endif
  41059. +
  41060. +/** Gets the QH that contains the list_head */
  41061. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  41062. +
  41063. +/** Gets the QTD that contains the list_head */
  41064. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  41065. +
  41066. +/** Check if QH is non-periodic */
  41067. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  41068. + (_qh_ptr_->ep_type == UE_CONTROL))
  41069. +
  41070. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  41071. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  41072. +
  41073. +/** Packet size for any kind of endpoint descriptor */
  41074. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  41075. +
  41076. +/**
  41077. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  41078. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  41079. + * frame number when the max frame number is reached.
  41080. + */
  41081. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  41082. +{
  41083. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  41084. + (DWC_HFNUM_MAX_FRNUM >> 1);
  41085. +}
  41086. +
  41087. +/**
  41088. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  41089. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  41090. + * number when the max frame number is reached.
  41091. + */
  41092. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  41093. +{
  41094. + return (frame1 != frame2) &&
  41095. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  41096. + (DWC_HFNUM_MAX_FRNUM >> 1));
  41097. +}
  41098. +
  41099. +/**
  41100. + * Increments _frame by the amount specified by _inc. The addition is done
  41101. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  41102. + */
  41103. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  41104. +{
  41105. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  41106. +}
  41107. +
  41108. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  41109. +{
  41110. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  41111. +}
  41112. +
  41113. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  41114. +{
  41115. + return frame & 0x7;
  41116. +}
  41117. +
  41118. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  41119. + dwc_otg_hc_regs_t * hc_regs,
  41120. + dwc_otg_qtd_t * qtd);
  41121. +
  41122. +#ifdef DEBUG
  41123. +/**
  41124. + * Macro to sample the remaining PHY clocks left in the current frame. This
  41125. + * may be used during debugging to determine the average time it takes to
  41126. + * execute sections of code. There are two possible sample points, "a" and
  41127. + * "b", so the _letter argument must be one of these values.
  41128. + *
  41129. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  41130. + * example, "cat /sys/devices/lm0/hcd_frrem".
  41131. + */
  41132. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  41133. +{ \
  41134. + hfnum_data_t hfnum; \
  41135. + dwc_otg_qtd_t *qtd; \
  41136. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  41137. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  41138. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  41139. + switch (hfnum.b.frnum & 0x7) { \
  41140. + case 7: \
  41141. + _hcd->hfnum_7_samples_##_letter++; \
  41142. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  41143. + break; \
  41144. + case 0: \
  41145. + _hcd->hfnum_0_samples_##_letter++; \
  41146. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  41147. + break; \
  41148. + default: \
  41149. + _hcd->hfnum_other_samples_##_letter++; \
  41150. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  41151. + break; \
  41152. + } \
  41153. + } \
  41154. +}
  41155. +#else
  41156. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  41157. +#endif
  41158. +#endif
  41159. +#endif /* DWC_DEVICE_ONLY */
  41160. --- /dev/null
  41161. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  41162. @@ -0,0 +1,1132 @@
  41163. +/*==========================================================================
  41164. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  41165. + * $Revision: #10 $
  41166. + * $Date: 2011/10/20 $
  41167. + * $Change: 1869464 $
  41168. + *
  41169. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  41170. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  41171. + * otherwise expressly agreed to in writing between Synopsys and you.
  41172. + *
  41173. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  41174. + * any End User Software License Agreement or Agreement for Licensed Product
  41175. + * with Synopsys or any supplement thereto. You are permitted to use and
  41176. + * redistribute this Software in source and binary forms, with or without
  41177. + * modification, provided that redistributions of source code must retain this
  41178. + * notice. You may not view, use, disclose, copy or distribute this file or
  41179. + * any information contained herein except pursuant to this license grant from
  41180. + * Synopsys. If you do not agree with this notice, including the disclaimer
  41181. + * below, then you are not authorized to use the Software.
  41182. + *
  41183. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  41184. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41185. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41186. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  41187. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  41188. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  41189. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41190. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  41191. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41192. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  41193. + * DAMAGE.
  41194. + * ========================================================================== */
  41195. +#ifndef DWC_DEVICE_ONLY
  41196. +
  41197. +/** @file
  41198. + * This file contains Descriptor DMA support implementation for host mode.
  41199. + */
  41200. +
  41201. +#include "dwc_otg_hcd.h"
  41202. +#include "dwc_otg_regs.h"
  41203. +
  41204. +extern bool microframe_schedule;
  41205. +
  41206. +static inline uint8_t frame_list_idx(uint16_t frame)
  41207. +{
  41208. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  41209. +}
  41210. +
  41211. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  41212. +{
  41213. + return (idx + inc) &
  41214. + (((speed ==
  41215. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  41216. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  41217. +}
  41218. +
  41219. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  41220. +{
  41221. + return (idx - inc) &
  41222. + (((speed ==
  41223. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  41224. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  41225. +}
  41226. +
  41227. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  41228. +{
  41229. + return (((qh->ep_type == UE_ISOCHRONOUS)
  41230. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  41231. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  41232. +}
  41233. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  41234. +{
  41235. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  41236. + ? ((qh->interval + 8 - 1) / 8)
  41237. + : qh->interval);
  41238. +}
  41239. +
  41240. +static int desc_list_alloc(dwc_otg_qh_t * qh)
  41241. +{
  41242. + int retval = 0;
  41243. +
  41244. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  41245. + DWC_DMA_ALLOC(sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  41246. + &qh->desc_list_dma);
  41247. +
  41248. + if (!qh->desc_list) {
  41249. + retval = -DWC_E_NO_MEMORY;
  41250. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  41251. +
  41252. + }
  41253. +
  41254. + dwc_memset(qh->desc_list, 0x00,
  41255. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  41256. +
  41257. + qh->n_bytes =
  41258. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  41259. +
  41260. + if (!qh->n_bytes) {
  41261. + retval = -DWC_E_NO_MEMORY;
  41262. + DWC_ERROR
  41263. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  41264. + __func__);
  41265. +
  41266. + }
  41267. + return retval;
  41268. +
  41269. +}
  41270. +
  41271. +static void desc_list_free(dwc_otg_qh_t * qh)
  41272. +{
  41273. + if (qh->desc_list) {
  41274. + DWC_DMA_FREE(max_desc_num(qh), qh->desc_list,
  41275. + qh->desc_list_dma);
  41276. + qh->desc_list = NULL;
  41277. + }
  41278. +
  41279. + if (qh->n_bytes) {
  41280. + DWC_FREE(qh->n_bytes);
  41281. + qh->n_bytes = NULL;
  41282. + }
  41283. +}
  41284. +
  41285. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  41286. +{
  41287. + int retval = 0;
  41288. + if (hcd->frame_list)
  41289. + return 0;
  41290. +
  41291. + hcd->frame_list = DWC_DMA_ALLOC(4 * MAX_FRLIST_EN_NUM,
  41292. + &hcd->frame_list_dma);
  41293. + if (!hcd->frame_list) {
  41294. + retval = -DWC_E_NO_MEMORY;
  41295. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  41296. + }
  41297. +
  41298. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  41299. +
  41300. + return retval;
  41301. +}
  41302. +
  41303. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  41304. +{
  41305. + if (!hcd->frame_list)
  41306. + return;
  41307. +
  41308. + DWC_DMA_FREE(4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  41309. + hcd->frame_list = NULL;
  41310. +}
  41311. +
  41312. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  41313. +{
  41314. +
  41315. + hcfg_data_t hcfg;
  41316. +
  41317. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  41318. +
  41319. + if (hcfg.b.perschedena) {
  41320. + /* already enabled */
  41321. + return;
  41322. + }
  41323. +
  41324. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  41325. + hcd->frame_list_dma);
  41326. +
  41327. + switch (fr_list_en) {
  41328. + case 64:
  41329. + hcfg.b.frlisten = 3;
  41330. + break;
  41331. + case 32:
  41332. + hcfg.b.frlisten = 2;
  41333. + break;
  41334. + case 16:
  41335. + hcfg.b.frlisten = 1;
  41336. + break;
  41337. + case 8:
  41338. + hcfg.b.frlisten = 0;
  41339. + break;
  41340. + default:
  41341. + break;
  41342. + }
  41343. +
  41344. + hcfg.b.perschedena = 1;
  41345. +
  41346. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  41347. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  41348. +
  41349. +}
  41350. +
  41351. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  41352. +{
  41353. + hcfg_data_t hcfg;
  41354. +
  41355. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  41356. +
  41357. + if (!hcfg.b.perschedena) {
  41358. + /* already disabled */
  41359. + return;
  41360. + }
  41361. + hcfg.b.perschedena = 0;
  41362. +
  41363. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  41364. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  41365. +}
  41366. +
  41367. +/*
  41368. + * Activates/Deactivates FrameList entries for the channel
  41369. + * based on endpoint servicing period.
  41370. + */
  41371. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  41372. +{
  41373. + uint16_t i, j, inc;
  41374. + dwc_hc_t *hc = NULL;
  41375. +
  41376. + if (!qh->channel) {
  41377. + DWC_ERROR("qh->channel = %p", qh->channel);
  41378. + return;
  41379. + }
  41380. +
  41381. + if (!hcd) {
  41382. + DWC_ERROR("------hcd = %p", hcd);
  41383. + return;
  41384. + }
  41385. +
  41386. + if (!hcd->frame_list) {
  41387. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  41388. + return;
  41389. + }
  41390. +
  41391. + hc = qh->channel;
  41392. + inc = frame_incr_val(qh);
  41393. + if (qh->ep_type == UE_ISOCHRONOUS)
  41394. + i = frame_list_idx(qh->sched_frame);
  41395. + else
  41396. + i = 0;
  41397. +
  41398. + j = i;
  41399. + do {
  41400. + if (enable)
  41401. + hcd->frame_list[j] |= (1 << hc->hc_num);
  41402. + else
  41403. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  41404. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  41405. + }
  41406. + while (j != i);
  41407. + if (!enable)
  41408. + return;
  41409. + hc->schinfo = 0;
  41410. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  41411. + j = 1;
  41412. + /* TODO - check this */
  41413. + inc = (8 + qh->interval - 1) / qh->interval;
  41414. + for (i = 0; i < inc; i++) {
  41415. + hc->schinfo |= j;
  41416. + j = j << qh->interval;
  41417. + }
  41418. + } else {
  41419. + hc->schinfo = 0xff;
  41420. + }
  41421. +}
  41422. +
  41423. +#if 1
  41424. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  41425. +{
  41426. + int i = 0;
  41427. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  41428. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  41429. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  41430. + if (!(i % 8) && i)
  41431. + DWC_PRINTF("\n");
  41432. + }
  41433. + DWC_PRINTF("\n----\n");
  41434. +
  41435. +}
  41436. +#endif
  41437. +
  41438. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41439. +{
  41440. + dwc_irqflags_t flags;
  41441. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  41442. +
  41443. + dwc_hc_t *hc = qh->channel;
  41444. + if (dwc_qh_is_non_per(qh)) {
  41445. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  41446. + if (!microframe_schedule)
  41447. + hcd->non_periodic_channels--;
  41448. + else
  41449. + hcd->available_host_channels++;
  41450. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  41451. + } else
  41452. + update_frame_list(hcd, qh, 0);
  41453. +
  41454. + /*
  41455. + * The condition is added to prevent double cleanup try in case of device
  41456. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  41457. + */
  41458. + if (hc->qh) {
  41459. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  41460. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  41461. + hc->qh = NULL;
  41462. + }
  41463. +
  41464. + qh->channel = NULL;
  41465. + qh->ntd = 0;
  41466. +
  41467. + if (qh->desc_list) {
  41468. + dwc_memset(qh->desc_list, 0x00,
  41469. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  41470. + }
  41471. +}
  41472. +
  41473. +/**
  41474. + * Initializes a QH structure's Descriptor DMA related members.
  41475. + * Allocates memory for descriptor list.
  41476. + * On first periodic QH, allocates memory for FrameList
  41477. + * and enables periodic scheduling.
  41478. + *
  41479. + * @param hcd The HCD state structure for the DWC OTG controller.
  41480. + * @param qh The QH to init.
  41481. + *
  41482. + * @return 0 if successful, negative error code otherwise.
  41483. + */
  41484. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41485. +{
  41486. + int retval = 0;
  41487. +
  41488. + if (qh->do_split) {
  41489. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  41490. + return -1;
  41491. + }
  41492. +
  41493. + retval = desc_list_alloc(qh);
  41494. +
  41495. + if ((retval == 0)
  41496. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  41497. + if (!hcd->frame_list) {
  41498. + retval = frame_list_alloc(hcd);
  41499. + /* Enable periodic schedule on first periodic QH */
  41500. + if (retval == 0)
  41501. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  41502. + }
  41503. + }
  41504. +
  41505. + qh->ntd = 0;
  41506. +
  41507. + return retval;
  41508. +}
  41509. +
  41510. +/**
  41511. + * Frees descriptor list memory associated with the QH.
  41512. + * If QH is periodic and the last, frees FrameList memory
  41513. + * and disables periodic scheduling.
  41514. + *
  41515. + * @param hcd The HCD state structure for the DWC OTG controller.
  41516. + * @param qh The QH to init.
  41517. + */
  41518. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41519. +{
  41520. + desc_list_free(qh);
  41521. +
  41522. + /*
  41523. + * Channel still assigned due to some reasons.
  41524. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  41525. + * ChHalted interrupt to release the channel. Afterwards
  41526. + * when it comes here from endpoint disable routine
  41527. + * channel remains assigned.
  41528. + */
  41529. + if (qh->channel)
  41530. + release_channel_ddma(hcd, qh);
  41531. +
  41532. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  41533. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  41534. +
  41535. + per_sched_disable(hcd);
  41536. + frame_list_free(hcd);
  41537. + }
  41538. +}
  41539. +
  41540. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  41541. +{
  41542. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  41543. + /*
  41544. + * Descriptor set(8 descriptors) index
  41545. + * which is 8-aligned.
  41546. + */
  41547. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  41548. + } else {
  41549. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  41550. + }
  41551. +}
  41552. +
  41553. +/*
  41554. + * Determine starting frame for Isochronous transfer.
  41555. + * Few frames skipped to prevent race condition with HC.
  41556. + */
  41557. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41558. + uint8_t * skip_frames)
  41559. +{
  41560. + uint16_t frame = 0;
  41561. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  41562. +
  41563. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  41564. +
  41565. + /*
  41566. + * skip_frames is used to limit activated descriptors number
  41567. + * to avoid the situation when HC services the last activated
  41568. + * descriptor firstly.
  41569. + * Example for FS:
  41570. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  41571. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  41572. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  41573. + * list will be fully programmed with Active descriptors and it is possible
  41574. + * case(rare) that the latest descriptor(considering rollback) corresponding
  41575. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  41576. + * up to 11 uframes(16 in the code) may be skipped.
  41577. + */
  41578. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  41579. + /*
  41580. + * Consider uframe counter also, to start xfer asap.
  41581. + * If half of the frame elapsed skip 2 frames otherwise
  41582. + * just 1 frame.
  41583. + * Starting descriptor index must be 8-aligned, so
  41584. + * if the current frame is near to complete the next one
  41585. + * is skipped as well.
  41586. + */
  41587. +
  41588. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  41589. + *skip_frames = 2 * 8;
  41590. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  41591. + } else {
  41592. + *skip_frames = 1 * 8;
  41593. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  41594. + }
  41595. +
  41596. + frame = dwc_full_frame_num(frame);
  41597. + } else {
  41598. + /*
  41599. + * Two frames are skipped for FS - the current and the next.
  41600. + * But for descriptor programming, 1 frame(descriptor) is enough,
  41601. + * see example above.
  41602. + */
  41603. + *skip_frames = 1;
  41604. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  41605. + }
  41606. +
  41607. + return frame;
  41608. +}
  41609. +
  41610. +/*
  41611. + * Calculate initial descriptor index for isochronous transfer
  41612. + * based on scheduled frame.
  41613. + */
  41614. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41615. +{
  41616. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  41617. + uint8_t skip_frames = 0;
  41618. + /*
  41619. + * With current ISOC processing algorithm the channel is being
  41620. + * released when no more QTDs in the list(qh->ntd == 0).
  41621. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  41622. + *
  41623. + * So qh->channel != NULL branch is not used and just not removed from the
  41624. + * source file. It is required for another possible approach which is,
  41625. + * do not disable and release the channel when ISOC session completed,
  41626. + * just move QH to inactive schedule until new QTD arrives.
  41627. + * On new QTD, the QH moved back to 'ready' schedule,
  41628. + * starting frame and therefore starting desc_index are recalculated.
  41629. + * In this case channel is released only on ep_disable.
  41630. + */
  41631. +
  41632. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  41633. + if (qh->channel) {
  41634. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  41635. + /*
  41636. + * Calculate initial descriptor index based on FrameList current bitmap
  41637. + * and servicing period.
  41638. + */
  41639. + fr_idx_tmp = frame_list_idx(frame);
  41640. + fr_idx =
  41641. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  41642. + fr_idx_tmp)
  41643. + % frame_incr_val(qh);
  41644. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  41645. + } else {
  41646. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  41647. + fr_idx = frame_list_idx(qh->sched_frame);
  41648. + }
  41649. +
  41650. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  41651. +
  41652. + return skip_frames;
  41653. +}
  41654. +
  41655. +#define ISOC_URB_GIVEBACK_ASAP
  41656. +
  41657. +#define MAX_ISOC_XFER_SIZE_FS 1023
  41658. +#define MAX_ISOC_XFER_SIZE_HS 3072
  41659. +#define DESCNUM_THRESHOLD 4
  41660. +
  41661. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41662. + uint8_t skip_frames)
  41663. +{
  41664. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  41665. + dwc_otg_qtd_t *qtd;
  41666. + dwc_otg_host_dma_desc_t *dma_desc;
  41667. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  41668. +
  41669. + idx = qh->td_last;
  41670. + inc = qh->interval;
  41671. + n_desc = 0;
  41672. +
  41673. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  41674. + if (skip_frames && !qh->channel)
  41675. + ntd_max = ntd_max - skip_frames / qh->interval;
  41676. +
  41677. + max_xfer_size =
  41678. + (qh->dev_speed ==
  41679. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  41680. + MAX_ISOC_XFER_SIZE_FS;
  41681. +
  41682. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  41683. + while ((qh->ntd < ntd_max)
  41684. + && (qtd->isoc_frame_index_last <
  41685. + qtd->urb->packet_count)) {
  41686. +
  41687. + dma_desc = &qh->desc_list[idx];
  41688. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  41689. +
  41690. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  41691. +
  41692. + if (frame_desc->length > max_xfer_size)
  41693. + qh->n_bytes[idx] = max_xfer_size;
  41694. + else
  41695. + qh->n_bytes[idx] = frame_desc->length;
  41696. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  41697. + dma_desc->status.b_isoc.a = 1;
  41698. + dma_desc->status.b_isoc.sts = 0;
  41699. +
  41700. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  41701. +
  41702. + qh->ntd++;
  41703. +
  41704. + qtd->isoc_frame_index_last++;
  41705. +
  41706. +#ifdef ISOC_URB_GIVEBACK_ASAP
  41707. + /*
  41708. + * Set IOC for each descriptor corresponding to the
  41709. + * last frame of the URB.
  41710. + */
  41711. + if (qtd->isoc_frame_index_last ==
  41712. + qtd->urb->packet_count)
  41713. + dma_desc->status.b_isoc.ioc = 1;
  41714. +
  41715. +#endif
  41716. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  41717. + n_desc++;
  41718. +
  41719. + }
  41720. + qtd->in_process = 1;
  41721. + }
  41722. +
  41723. + qh->td_last = idx;
  41724. +
  41725. +#ifdef ISOC_URB_GIVEBACK_ASAP
  41726. + /* Set IOC for the last descriptor if descriptor list is full */
  41727. + if (qh->ntd == ntd_max) {
  41728. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  41729. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  41730. + }
  41731. +#else
  41732. + /*
  41733. + * Set IOC bit only for one descriptor.
  41734. + * Always try to be ahead of HW processing,
  41735. + * i.e. on IOC generation driver activates next descriptors but
  41736. + * core continues to process descriptors followed the one with IOC set.
  41737. + */
  41738. +
  41739. + if (n_desc > DESCNUM_THRESHOLD) {
  41740. + /*
  41741. + * Move IOC "up". Required even if there is only one QTD
  41742. + * in the list, cause QTDs migth continue to be queued,
  41743. + * but during the activation it was only one queued.
  41744. + * Actually more than one QTD might be in the list if this function called
  41745. + * from XferCompletion - QTDs was queued during HW processing of the previous
  41746. + * descriptor chunk.
  41747. + */
  41748. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  41749. + } else {
  41750. + /*
  41751. + * Set the IOC for the latest descriptor
  41752. + * if either number of descriptor is not greather than threshold
  41753. + * or no more new descriptors activated.
  41754. + */
  41755. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  41756. + }
  41757. +
  41758. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  41759. +#endif
  41760. +}
  41761. +
  41762. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41763. +{
  41764. +
  41765. + dwc_hc_t *hc;
  41766. + dwc_otg_host_dma_desc_t *dma_desc;
  41767. + dwc_otg_qtd_t *qtd;
  41768. + int num_packets, len, n_desc = 0;
  41769. +
  41770. + hc = qh->channel;
  41771. +
  41772. + /*
  41773. + * Start with hc->xfer_buff initialized in
  41774. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  41775. + * this pointer re-assigned to the buffer of the currently processed QTD.
  41776. + * For non-SG request there is always one QTD active.
  41777. + */
  41778. +
  41779. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  41780. +
  41781. + if (n_desc) {
  41782. + /* SG request - more than 1 QTDs */
  41783. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  41784. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  41785. + }
  41786. +
  41787. + qtd->n_desc = 0;
  41788. +
  41789. + do {
  41790. + dma_desc = &qh->desc_list[n_desc];
  41791. + len = hc->xfer_len;
  41792. +
  41793. + if (len > MAX_DMA_DESC_SIZE)
  41794. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  41795. +
  41796. + if (hc->ep_is_in) {
  41797. + if (len > 0) {
  41798. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  41799. + } else {
  41800. + /* Need 1 packet for transfer length of 0. */
  41801. + num_packets = 1;
  41802. + }
  41803. + /* Always program an integral # of max packets for IN transfers. */
  41804. + len = num_packets * hc->max_packet;
  41805. + }
  41806. +
  41807. + dma_desc->status.b.n_bytes = len;
  41808. +
  41809. + qh->n_bytes[n_desc] = len;
  41810. +
  41811. + if ((qh->ep_type == UE_CONTROL)
  41812. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  41813. + dma_desc->status.b.sup = 1; /* Setup Packet */
  41814. +
  41815. + dma_desc->status.b.a = 1; /* Active descriptor */
  41816. + dma_desc->status.b.sts = 0;
  41817. +
  41818. + dma_desc->buf =
  41819. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  41820. +
  41821. + /*
  41822. + * Last descriptor(or single) of IN transfer
  41823. + * with actual size less than MaxPacket.
  41824. + */
  41825. + if (len > hc->xfer_len) {
  41826. + hc->xfer_len = 0;
  41827. + } else {
  41828. + hc->xfer_buff += len;
  41829. + hc->xfer_len -= len;
  41830. + }
  41831. +
  41832. + qtd->n_desc++;
  41833. + n_desc++;
  41834. + }
  41835. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  41836. +
  41837. +
  41838. + qtd->in_process = 1;
  41839. +
  41840. + if (qh->ep_type == UE_CONTROL)
  41841. + break;
  41842. +
  41843. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  41844. + break;
  41845. + }
  41846. +
  41847. + if (n_desc) {
  41848. + /* Request Transfer Complete interrupt for the last descriptor */
  41849. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  41850. + /* End of List indicator */
  41851. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  41852. +
  41853. + hc->ntd = n_desc;
  41854. + }
  41855. +}
  41856. +
  41857. +/**
  41858. + * For Control and Bulk endpoints initializes descriptor list
  41859. + * and starts the transfer.
  41860. + *
  41861. + * For Interrupt and Isochronous endpoints initializes descriptor list
  41862. + * then updates FrameList, marking appropriate entries as active.
  41863. + * In case of Isochronous, the starting descriptor index is calculated based
  41864. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  41865. + * Then starts the transfer via enabling the channel.
  41866. + * For Isochronous endpoint the channel is not halted on XferComplete
  41867. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  41868. + *
  41869. + * @param hcd The HCD state structure for the DWC OTG controller.
  41870. + * @param qh The QH to init.
  41871. + *
  41872. + * @return 0 if successful, negative error code otherwise.
  41873. + */
  41874. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41875. +{
  41876. + /* Channel is already assigned */
  41877. + dwc_hc_t *hc = qh->channel;
  41878. + uint8_t skip_frames = 0;
  41879. +
  41880. + switch (hc->ep_type) {
  41881. + case DWC_OTG_EP_TYPE_CONTROL:
  41882. + case DWC_OTG_EP_TYPE_BULK:
  41883. + init_non_isoc_dma_desc(hcd, qh);
  41884. +
  41885. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41886. + break;
  41887. + case DWC_OTG_EP_TYPE_INTR:
  41888. + init_non_isoc_dma_desc(hcd, qh);
  41889. +
  41890. + update_frame_list(hcd, qh, 1);
  41891. +
  41892. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41893. + break;
  41894. + case DWC_OTG_EP_TYPE_ISOC:
  41895. +
  41896. + if (!qh->ntd)
  41897. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  41898. +
  41899. + init_isoc_dma_desc(hcd, qh, skip_frames);
  41900. +
  41901. + if (!hc->xfer_started) {
  41902. +
  41903. + update_frame_list(hcd, qh, 1);
  41904. +
  41905. + /*
  41906. + * Always set to max, instead of actual size.
  41907. + * Otherwise ntd will be changed with
  41908. + * channel being enabled. Not recommended.
  41909. + *
  41910. + */
  41911. + hc->ntd = max_desc_num(qh);
  41912. + /* Enable channel only once for ISOC */
  41913. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41914. + }
  41915. +
  41916. + break;
  41917. + default:
  41918. +
  41919. + break;
  41920. + }
  41921. +}
  41922. +
  41923. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  41924. + dwc_hc_t * hc,
  41925. + dwc_otg_hc_regs_t * hc_regs,
  41926. + dwc_otg_halt_status_e halt_status)
  41927. +{
  41928. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  41929. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  41930. + dwc_otg_qh_t *qh;
  41931. + dwc_otg_host_dma_desc_t *dma_desc;
  41932. + uint16_t idx, remain;
  41933. + uint8_t urb_compl;
  41934. +
  41935. + qh = hc->qh;
  41936. + idx = qh->td_first;
  41937. +
  41938. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  41939. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  41940. + qtd->in_process = 0;
  41941. + return;
  41942. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  41943. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  41944. + /*
  41945. + * Channel is halted in these error cases.
  41946. + * Considered as serious issues.
  41947. + * Complete all URBs marking all frames as failed,
  41948. + * irrespective whether some of the descriptors(frames) succeeded or no.
  41949. + * Pass error code to completion routine as well, to
  41950. + * update urb->status, some of class drivers might use it to stop
  41951. + * queing transfer requests.
  41952. + */
  41953. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  41954. + ? (-DWC_E_IO)
  41955. + : (-DWC_E_OVERFLOW);
  41956. +
  41957. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  41958. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  41959. + frame_desc = &qtd->urb->iso_descs[idx];
  41960. + frame_desc->status = err;
  41961. + }
  41962. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  41963. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  41964. + }
  41965. + return;
  41966. + }
  41967. +
  41968. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  41969. +
  41970. + if (!qtd->in_process)
  41971. + break;
  41972. +
  41973. + urb_compl = 0;
  41974. +
  41975. + do {
  41976. +
  41977. + dma_desc = &qh->desc_list[idx];
  41978. +
  41979. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  41980. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  41981. +
  41982. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  41983. + /*
  41984. + * XactError or, unable to complete all the transactions
  41985. + * in the scheduled micro-frame/frame,
  41986. + * both indicated by DMA_DESC_STS_PKTERR.
  41987. + */
  41988. + qtd->urb->error_count++;
  41989. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  41990. + frame_desc->status = -DWC_E_PROTOCOL;
  41991. + } else {
  41992. + /* Success */
  41993. +
  41994. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  41995. + frame_desc->status = 0;
  41996. + }
  41997. +
  41998. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  41999. + /*
  42000. + * urb->status is not used for isoc transfers here.
  42001. + * The individual frame_desc status are used instead.
  42002. + */
  42003. +
  42004. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  42005. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  42006. +
  42007. + /*
  42008. + * This check is necessary because urb_dequeue can be called
  42009. + * from urb complete callback(sound driver example).
  42010. + * All pending URBs are dequeued there, so no need for
  42011. + * further processing.
  42012. + */
  42013. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  42014. + return;
  42015. + }
  42016. +
  42017. + urb_compl = 1;
  42018. +
  42019. + }
  42020. +
  42021. + qh->ntd--;
  42022. +
  42023. + /* Stop if IOC requested descriptor reached */
  42024. + if (dma_desc->status.b_isoc.ioc) {
  42025. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  42026. + goto stop_scan;
  42027. + }
  42028. +
  42029. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  42030. +
  42031. + if (urb_compl)
  42032. + break;
  42033. + }
  42034. + while (idx != qh->td_first);
  42035. + }
  42036. +stop_scan:
  42037. + qh->td_first = idx;
  42038. +}
  42039. +
  42040. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  42041. + dwc_hc_t * hc,
  42042. + dwc_otg_qtd_t * qtd,
  42043. + dwc_otg_host_dma_desc_t * dma_desc,
  42044. + dwc_otg_halt_status_e halt_status,
  42045. + uint32_t n_bytes, uint8_t * xfer_done)
  42046. +{
  42047. +
  42048. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  42049. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  42050. +
  42051. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  42052. + urb->status = -DWC_E_IO;
  42053. + return 1;
  42054. + }
  42055. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  42056. + switch (halt_status) {
  42057. + case DWC_OTG_HC_XFER_STALL:
  42058. + urb->status = -DWC_E_PIPE;
  42059. + break;
  42060. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  42061. + urb->status = -DWC_E_OVERFLOW;
  42062. + break;
  42063. + case DWC_OTG_HC_XFER_XACT_ERR:
  42064. + urb->status = -DWC_E_PROTOCOL;
  42065. + break;
  42066. + default:
  42067. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  42068. + halt_status);
  42069. + break;
  42070. + }
  42071. + return 1;
  42072. + }
  42073. +
  42074. + if (dma_desc->status.b.a == 1) {
  42075. + DWC_DEBUGPL(DBG_HCDV,
  42076. + "Active descriptor encountered on channel %d\n",
  42077. + hc->hc_num);
  42078. + return 0;
  42079. + }
  42080. +
  42081. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  42082. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  42083. + urb->actual_length += n_bytes - remain;
  42084. + if (remain || urb->actual_length == urb->length) {
  42085. + /*
  42086. + * For Control Data stage do not set urb->status=0 to prevent
  42087. + * URB callback. Set it when Status phase done. See below.
  42088. + */
  42089. + *xfer_done = 1;
  42090. + }
  42091. +
  42092. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  42093. + urb->status = 0;
  42094. + *xfer_done = 1;
  42095. + }
  42096. + /* No handling for SETUP stage */
  42097. + } else {
  42098. + /* BULK and INTR */
  42099. + urb->actual_length += n_bytes - remain;
  42100. + if (remain || urb->actual_length == urb->length) {
  42101. + urb->status = 0;
  42102. + *xfer_done = 1;
  42103. + }
  42104. + }
  42105. +
  42106. + return 0;
  42107. +}
  42108. +
  42109. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  42110. + dwc_hc_t * hc,
  42111. + dwc_otg_hc_regs_t * hc_regs,
  42112. + dwc_otg_halt_status_e halt_status)
  42113. +{
  42114. + dwc_otg_hcd_urb_t *urb = NULL;
  42115. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  42116. + dwc_otg_qh_t *qh;
  42117. + dwc_otg_host_dma_desc_t *dma_desc;
  42118. + uint32_t n_bytes, n_desc, i;
  42119. + uint8_t failed = 0, xfer_done;
  42120. +
  42121. + n_desc = 0;
  42122. +
  42123. + qh = hc->qh;
  42124. +
  42125. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  42126. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  42127. + qtd->in_process = 0;
  42128. + }
  42129. + return;
  42130. + }
  42131. +
  42132. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  42133. +
  42134. + urb = qtd->urb;
  42135. +
  42136. + n_bytes = 0;
  42137. + xfer_done = 0;
  42138. +
  42139. + for (i = 0; i < qtd->n_desc; i++) {
  42140. + dma_desc = &qh->desc_list[n_desc];
  42141. +
  42142. + n_bytes = qh->n_bytes[n_desc];
  42143. +
  42144. + failed =
  42145. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  42146. + dma_desc,
  42147. + halt_status, n_bytes,
  42148. + &xfer_done);
  42149. +
  42150. + if (failed
  42151. + || (xfer_done
  42152. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  42153. +
  42154. + hcd->fops->complete(hcd, urb->priv, urb,
  42155. + urb->status);
  42156. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  42157. +
  42158. + if (failed)
  42159. + goto stop_scan;
  42160. + } else if (qh->ep_type == UE_CONTROL) {
  42161. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  42162. + if (urb->length > 0) {
  42163. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  42164. + } else {
  42165. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  42166. + }
  42167. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  42168. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  42169. + if (xfer_done) {
  42170. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  42171. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  42172. + } else if (i + 1 == qtd->n_desc) {
  42173. + /*
  42174. + * Last descriptor for Control data stage which is
  42175. + * not completed yet.
  42176. + */
  42177. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  42178. + }
  42179. + }
  42180. + }
  42181. +
  42182. + n_desc++;
  42183. + }
  42184. +
  42185. + }
  42186. +
  42187. +stop_scan:
  42188. +
  42189. + if (qh->ep_type != UE_CONTROL) {
  42190. + /*
  42191. + * Resetting the data toggle for bulk
  42192. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  42193. + */
  42194. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  42195. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  42196. + else
  42197. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  42198. + }
  42199. +
  42200. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  42201. + hcint_data_t hcint;
  42202. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  42203. + if (hcint.b.nyet) {
  42204. + /*
  42205. + * Got a NYET on the last transaction of the transfer. It
  42206. + * means that the endpoint should be in the PING state at the
  42207. + * beginning of the next transfer.
  42208. + */
  42209. + qh->ping_state = 1;
  42210. + clear_hc_int(hc_regs, nyet);
  42211. + }
  42212. +
  42213. + }
  42214. +
  42215. +}
  42216. +
  42217. +/**
  42218. + * This function is called from interrupt handlers.
  42219. + * Scans the descriptor list, updates URB's status and
  42220. + * calls completion routine for the URB if it's done.
  42221. + * Releases the channel to be used by other transfers.
  42222. + * In case of Isochronous endpoint the channel is not halted until
  42223. + * the end of the session, i.e. QTD list is empty.
  42224. + * If periodic channel released the FrameList is updated accordingly.
  42225. + *
  42226. + * Calls transaction selection routines to activate pending transfers.
  42227. + *
  42228. + * @param hcd The HCD state structure for the DWC OTG controller.
  42229. + * @param hc Host channel, the transfer is completed on.
  42230. + * @param hc_regs Host channel registers.
  42231. + * @param halt_status Reason the channel is being halted,
  42232. + * or just XferComplete for isochronous transfer
  42233. + */
  42234. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  42235. + dwc_hc_t * hc,
  42236. + dwc_otg_hc_regs_t * hc_regs,
  42237. + dwc_otg_halt_status_e halt_status)
  42238. +{
  42239. + uint8_t continue_isoc_xfer = 0;
  42240. + dwc_otg_transaction_type_e tr_type;
  42241. + dwc_otg_qh_t *qh = hc->qh;
  42242. +
  42243. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  42244. +
  42245. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  42246. +
  42247. + /* Release the channel if halted or session completed */
  42248. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  42249. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  42250. +
  42251. + /* Halt the channel if session completed */
  42252. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  42253. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  42254. + }
  42255. +
  42256. + release_channel_ddma(hcd, qh);
  42257. + dwc_otg_hcd_qh_remove(hcd, qh);
  42258. + } else {
  42259. + /* Keep in assigned schedule to continue transfer */
  42260. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  42261. + &qh->qh_list_entry);
  42262. + continue_isoc_xfer = 1;
  42263. +
  42264. + }
  42265. + /** @todo Consider the case when period exceeds FrameList size.
  42266. + * Frame Rollover interrupt should be used.
  42267. + */
  42268. + } else {
  42269. + /* Scan descriptor list to complete the URB(s), then release the channel */
  42270. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  42271. +
  42272. + release_channel_ddma(hcd, qh);
  42273. + dwc_otg_hcd_qh_remove(hcd, qh);
  42274. +
  42275. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  42276. + /* Add back to inactive non-periodic schedule on normal completion */
  42277. + dwc_otg_hcd_qh_add(hcd, qh);
  42278. + }
  42279. +
  42280. + }
  42281. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  42282. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  42283. + if (continue_isoc_xfer) {
  42284. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  42285. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  42286. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  42287. + tr_type = DWC_OTG_TRANSACTION_ALL;
  42288. + }
  42289. + }
  42290. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  42291. + }
  42292. +}
  42293. +
  42294. +#endif /* DWC_DEVICE_ONLY */
  42295. --- /dev/null
  42296. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  42297. @@ -0,0 +1,417 @@
  42298. +/* ==========================================================================
  42299. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  42300. + * $Revision: #12 $
  42301. + * $Date: 2011/10/26 $
  42302. + * $Change: 1873028 $
  42303. + *
  42304. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  42305. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  42306. + * otherwise expressly agreed to in writing between Synopsys and you.
  42307. + *
  42308. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  42309. + * any End User Software License Agreement or Agreement for Licensed Product
  42310. + * with Synopsys or any supplement thereto. You are permitted to use and
  42311. + * redistribute this Software in source and binary forms, with or without
  42312. + * modification, provided that redistributions of source code must retain this
  42313. + * notice. You may not view, use, disclose, copy or distribute this file or
  42314. + * any information contained herein except pursuant to this license grant from
  42315. + * Synopsys. If you do not agree with this notice, including the disclaimer
  42316. + * below, then you are not authorized to use the Software.
  42317. + *
  42318. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  42319. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42320. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42321. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  42322. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  42323. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42324. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42325. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42326. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42327. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42328. + * DAMAGE.
  42329. + * ========================================================================== */
  42330. +#ifndef DWC_DEVICE_ONLY
  42331. +#ifndef __DWC_HCD_IF_H__
  42332. +#define __DWC_HCD_IF_H__
  42333. +
  42334. +#include "dwc_otg_core_if.h"
  42335. +
  42336. +/** @file
  42337. + * This file defines DWC_OTG HCD Core API.
  42338. + */
  42339. +
  42340. +struct dwc_otg_hcd;
  42341. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  42342. +
  42343. +struct dwc_otg_hcd_urb;
  42344. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  42345. +
  42346. +/** @name HCD Function Driver Callbacks */
  42347. +/** @{ */
  42348. +
  42349. +/** This function is called whenever core switches to host mode. */
  42350. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  42351. +
  42352. +/** This function is called when device has been disconnected */
  42353. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  42354. +
  42355. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  42356. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42357. + void *urb_handle,
  42358. + uint32_t * hub_addr,
  42359. + uint32_t * port_addr);
  42360. +/** Via this function HCD core gets device speed */
  42361. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42362. + void *urb_handle);
  42363. +
  42364. +/** This function is called when urb is completed */
  42365. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42366. + void *urb_handle,
  42367. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  42368. + int32_t status);
  42369. +
  42370. +/** Via this function HCD core gets b_hnp_enable parameter */
  42371. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  42372. +
  42373. +struct dwc_otg_hcd_function_ops {
  42374. + dwc_otg_hcd_start_cb_t start;
  42375. + dwc_otg_hcd_disconnect_cb_t disconnect;
  42376. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  42377. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  42378. + dwc_otg_hcd_complete_urb_cb_t complete;
  42379. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  42380. +};
  42381. +/** @} */
  42382. +
  42383. +/** @name HCD Core API */
  42384. +/** @{ */
  42385. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  42386. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  42387. +
  42388. +/** This function should be called to initiate HCD Core.
  42389. + *
  42390. + * @param hcd The HCD
  42391. + * @param core_if The DWC_OTG Core
  42392. + *
  42393. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  42394. + * Returns 0 on success
  42395. + */
  42396. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  42397. +
  42398. +/** Frees HCD
  42399. + *
  42400. + * @param hcd The HCD
  42401. + */
  42402. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  42403. +
  42404. +/** This function should be called on every hardware interrupt.
  42405. + *
  42406. + * @param dwc_otg_hcd The HCD
  42407. + *
  42408. + * Returns non zero if interrupt is handled
  42409. + * Return 0 if interrupt is not handled
  42410. + */
  42411. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  42412. +
  42413. +/** This function is used to handle the fast interrupt
  42414. + *
  42415. + */
  42416. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  42417. +
  42418. +/**
  42419. + * Returns private data set by
  42420. + * dwc_otg_hcd_set_priv_data function.
  42421. + *
  42422. + * @param hcd The HCD
  42423. + */
  42424. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  42425. +
  42426. +/**
  42427. + * Set private data.
  42428. + *
  42429. + * @param hcd The HCD
  42430. + * @param priv_data pointer to be stored in private data
  42431. + */
  42432. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  42433. +
  42434. +/**
  42435. + * This function initializes the HCD Core.
  42436. + *
  42437. + * @param hcd The HCD
  42438. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  42439. + *
  42440. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  42441. + * Returns 0 on success
  42442. + */
  42443. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  42444. + struct dwc_otg_hcd_function_ops *fops);
  42445. +
  42446. +/**
  42447. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  42448. + * stopped.
  42449. + *
  42450. + * @param hcd The HCD
  42451. + */
  42452. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  42453. +
  42454. +/**
  42455. + * Handles hub class-specific requests.
  42456. + *
  42457. + * @param dwc_otg_hcd The HCD
  42458. + * @param typeReq Request Type
  42459. + * @param wValue wValue from control request
  42460. + * @param wIndex wIndex from control request
  42461. + * @param buf data buffer
  42462. + * @param wLength data buffer length
  42463. + *
  42464. + * Returns -DWC_E_INVALID if invalid argument is passed
  42465. + * Returns 0 on success
  42466. + */
  42467. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  42468. + uint16_t typeReq, uint16_t wValue,
  42469. + uint16_t wIndex, uint8_t * buf,
  42470. + uint16_t wLength);
  42471. +
  42472. +/**
  42473. + * Returns otg port number.
  42474. + *
  42475. + * @param hcd The HCD
  42476. + */
  42477. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  42478. +
  42479. +/**
  42480. + * Returns OTG version - either 1.3 or 2.0.
  42481. + *
  42482. + * @param core_if The core_if structure pointer
  42483. + */
  42484. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  42485. +
  42486. +/**
  42487. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  42488. + *
  42489. + * @param hcd The HCD
  42490. + */
  42491. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  42492. +
  42493. +/**
  42494. + * Returns current frame number.
  42495. + *
  42496. + * @param hcd The HCD
  42497. + */
  42498. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  42499. +
  42500. +/**
  42501. + * Dumps hcd state.
  42502. + *
  42503. + * @param hcd The HCD
  42504. + */
  42505. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  42506. +
  42507. +/**
  42508. + * Dump the average frame remaining at SOF. This can be used to
  42509. + * determine average interrupt latency. Frame remaining is also shown for
  42510. + * start transfer and two additional sample points.
  42511. + * Currently this function is not implemented.
  42512. + *
  42513. + * @param hcd The HCD
  42514. + */
  42515. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  42516. +
  42517. +/**
  42518. + * Sends LPM transaction to the local device.
  42519. + *
  42520. + * @param hcd The HCD
  42521. + * @param devaddr Device Address
  42522. + * @param hird Host initiated resume duration
  42523. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  42524. + *
  42525. + * Returns negative value if sending LPM transaction was not succeeded.
  42526. + * Returns 0 on success.
  42527. + */
  42528. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  42529. + uint8_t hird, uint8_t bRemoteWake);
  42530. +
  42531. +/* URB interface */
  42532. +
  42533. +/**
  42534. + * Allocates memory for dwc_otg_hcd_urb structure.
  42535. + * Allocated memory should be freed by call of DWC_FREE.
  42536. + *
  42537. + * @param hcd The HCD
  42538. + * @param iso_desc_count Count of ISOC descriptors
  42539. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  42540. + */
  42541. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  42542. + int iso_desc_count,
  42543. + int atomic_alloc);
  42544. +
  42545. +/**
  42546. + * Set pipe information in URB.
  42547. + *
  42548. + * @param hcd_urb DWC_OTG URB
  42549. + * @param devaddr Device Address
  42550. + * @param ep_num Endpoint Number
  42551. + * @param ep_type Endpoint Type
  42552. + * @param ep_dir Endpoint Direction
  42553. + * @param mps Max Packet Size
  42554. + */
  42555. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  42556. + uint8_t devaddr, uint8_t ep_num,
  42557. + uint8_t ep_type, uint8_t ep_dir,
  42558. + uint16_t mps);
  42559. +
  42560. +/* Transfer flags */
  42561. +#define URB_GIVEBACK_ASAP 0x1
  42562. +#define URB_SEND_ZERO_PACKET 0x2
  42563. +
  42564. +/**
  42565. + * Sets dwc_otg_hcd_urb parameters.
  42566. + *
  42567. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  42568. + * @param urb_handle Unique handle for request, this will be passed back
  42569. + * to function driver in completion callback.
  42570. + * @param buf The buffer for the data
  42571. + * @param dma The DMA buffer for the data
  42572. + * @param buflen Transfer length
  42573. + * @param sp Buffer for setup data
  42574. + * @param sp_dma DMA address of setup data buffer
  42575. + * @param flags Transfer flags
  42576. + * @param interval Polling interval for interrupt or isochronous transfers.
  42577. + */
  42578. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  42579. + void *urb_handle, void *buf,
  42580. + dwc_dma_t dma, uint32_t buflen, void *sp,
  42581. + dwc_dma_t sp_dma, uint32_t flags,
  42582. + uint16_t interval);
  42583. +
  42584. +/** Gets status from dwc_otg_hcd_urb
  42585. + *
  42586. + * @param dwc_otg_urb DWC_OTG URB
  42587. + */
  42588. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  42589. +
  42590. +/** Gets actual length from dwc_otg_hcd_urb
  42591. + *
  42592. + * @param dwc_otg_urb DWC_OTG URB
  42593. + */
  42594. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  42595. + dwc_otg_urb);
  42596. +
  42597. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  42598. + *
  42599. + * @param dwc_otg_urb DWC_OTG URB
  42600. + */
  42601. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  42602. + dwc_otg_urb);
  42603. +
  42604. +/** Set ISOC descriptor offset and length
  42605. + *
  42606. + * @param dwc_otg_urb DWC_OTG URB
  42607. + * @param desc_num ISOC descriptor number
  42608. + * @param offset Offset from beginig of buffer.
  42609. + * @param length Transaction length
  42610. + */
  42611. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  42612. + int desc_num, uint32_t offset,
  42613. + uint32_t length);
  42614. +
  42615. +/** Get status of ISOC descriptor, specified by desc_num
  42616. + *
  42617. + * @param dwc_otg_urb DWC_OTG URB
  42618. + * @param desc_num ISOC descriptor number
  42619. + */
  42620. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  42621. + dwc_otg_urb, int desc_num);
  42622. +
  42623. +/** Get actual length of ISOC descriptor, specified by desc_num
  42624. + *
  42625. + * @param dwc_otg_urb DWC_OTG URB
  42626. + * @param desc_num ISOC descriptor number
  42627. + */
  42628. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  42629. + dwc_otg_urb,
  42630. + int desc_num);
  42631. +
  42632. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  42633. + *
  42634. + * @param dwc_otg_hcd The HCD
  42635. + * @param dwc_otg_urb DWC_OTG URB
  42636. + * @param ep_handle Out parameter for returning endpoint handle
  42637. + * @param atomic_alloc Flag to do atomic allocation if needed
  42638. + *
  42639. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  42640. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  42641. + * Returns 0 on success.
  42642. + */
  42643. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  42644. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  42645. + void **ep_handle, int atomic_alloc);
  42646. +
  42647. +/** De-queue the specified URB
  42648. + *
  42649. + * @param dwc_otg_hcd The HCD
  42650. + * @param dwc_otg_urb DWC_OTG URB
  42651. + */
  42652. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  42653. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  42654. +
  42655. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  42656. + * Any URBs for the endpoint must already be dequeued.
  42657. + *
  42658. + * @param hcd The HCD
  42659. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  42660. + * @param retry Number of retries if there are queued transfers.
  42661. + *
  42662. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  42663. + * Returns 0 on success
  42664. + */
  42665. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  42666. + int retry);
  42667. +
  42668. +/* Resets the data toggle in qh structure. This function can be called from
  42669. + * usb_clear_halt routine.
  42670. + *
  42671. + * @param hcd The HCD
  42672. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  42673. + *
  42674. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  42675. + * Returns 0 on success
  42676. + */
  42677. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  42678. +
  42679. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  42680. + *
  42681. + * @param hcd The HCD
  42682. + * @param port Port number
  42683. + */
  42684. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  42685. +
  42686. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  42687. + * Only for ISOC and INTERRUPT endpoints.
  42688. + *
  42689. + * @param hcd The HCD
  42690. + * @param ep_handle Endpoint handle
  42691. + */
  42692. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  42693. + void *ep_handle);
  42694. +
  42695. +/** Call this function to check if bandwidth was freed for specified endpoint.
  42696. + *
  42697. + * @param hcd The HCD
  42698. + * @param ep_handle Endpoint handle
  42699. + */
  42700. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  42701. +
  42702. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  42703. + * Only for ISOC and INTERRUPT endpoints.
  42704. + *
  42705. + * @param hcd The HCD
  42706. + * @param ep_handle Endpoint handle
  42707. + */
  42708. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  42709. + void *ep_handle);
  42710. +
  42711. +/** @} */
  42712. +
  42713. +#endif /* __DWC_HCD_IF_H__ */
  42714. +#endif /* DWC_DEVICE_ONLY */
  42715. --- /dev/null
  42716. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  42717. @@ -0,0 +1,2714 @@
  42718. +/* ==========================================================================
  42719. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  42720. + * $Revision: #89 $
  42721. + * $Date: 2011/10/20 $
  42722. + * $Change: 1869487 $
  42723. + *
  42724. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  42725. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  42726. + * otherwise expressly agreed to in writing between Synopsys and you.
  42727. + *
  42728. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  42729. + * any End User Software License Agreement or Agreement for Licensed Product
  42730. + * with Synopsys or any supplement thereto. You are permitted to use and
  42731. + * redistribute this Software in source and binary forms, with or without
  42732. + * modification, provided that redistributions of source code must retain this
  42733. + * notice. You may not view, use, disclose, copy or distribute this file or
  42734. + * any information contained herein except pursuant to this license grant from
  42735. + * Synopsys. If you do not agree with this notice, including the disclaimer
  42736. + * below, then you are not authorized to use the Software.
  42737. + *
  42738. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  42739. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42740. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42741. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  42742. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  42743. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42744. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42745. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42746. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42747. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42748. + * DAMAGE.
  42749. + * ========================================================================== */
  42750. +#ifndef DWC_DEVICE_ONLY
  42751. +
  42752. +#include "dwc_otg_hcd.h"
  42753. +#include "dwc_otg_regs.h"
  42754. +
  42755. +#include <linux/jiffies.h>
  42756. +#include <asm/fiq.h>
  42757. +
  42758. +
  42759. +extern bool microframe_schedule;
  42760. +
  42761. +/** @file
  42762. + * This file contains the implementation of the HCD Interrupt handlers.
  42763. + */
  42764. +
  42765. +int fiq_done, int_done;
  42766. +
  42767. +#ifdef FIQ_DEBUG
  42768. +char buffer[1000*16];
  42769. +int wptr;
  42770. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  42771. +{
  42772. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  42773. + va_list args;
  42774. + char text[17];
  42775. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  42776. +
  42777. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  42778. + {
  42779. + local_fiq_disable();
  42780. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  42781. + va_start(args, fmt);
  42782. + vsnprintf(text+8, 9, fmt, args);
  42783. + va_end(args);
  42784. +
  42785. + memcpy(buffer + wptr, text, 16);
  42786. + wptr = (wptr + 16) % sizeof(buffer);
  42787. + local_fiq_enable();
  42788. + }
  42789. +}
  42790. +#endif
  42791. +
  42792. +/** This function handles interrupts for the HCD. */
  42793. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  42794. +{
  42795. + int retval = 0;
  42796. + static int last_time;
  42797. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  42798. + gintsts_data_t gintsts;
  42799. + gintmsk_data_t gintmsk;
  42800. + hfnum_data_t hfnum;
  42801. + haintmsk_data_t haintmsk;
  42802. +
  42803. +#ifdef DEBUG
  42804. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  42805. +
  42806. +#endif
  42807. +
  42808. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  42809. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  42810. +
  42811. + /* Exit from ISR if core is hibernated */
  42812. + if (core_if->hibernation_suspend == 1) {
  42813. + goto exit_handler_routine;
  42814. + }
  42815. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  42816. + /* Check if HOST Mode */
  42817. + if (dwc_otg_is_host_mode(core_if)) {
  42818. + if (fiq_enable) {
  42819. + local_fiq_disable();
  42820. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42821. + /* Pull in from the FIQ's disabled mask */
  42822. + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
  42823. + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
  42824. + }
  42825. +
  42826. + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
  42827. + gintsts.b.hcintr = 1;
  42828. + }
  42829. +
  42830. + /* Danger will robinson: fake a SOF if necessary */
  42831. + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
  42832. + gintsts.b.sofintr = 1;
  42833. + }
  42834. + gintsts.d32 &= gintmsk.d32;
  42835. +
  42836. + if (fiq_enable) {
  42837. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42838. + local_fiq_enable();
  42839. + }
  42840. +
  42841. + if (!gintsts.d32) {
  42842. + goto exit_handler_routine;
  42843. + }
  42844. +
  42845. +#ifdef DEBUG
  42846. + // We should be OK doing this because the common interrupts should already have been serviced
  42847. + /* Don't print debug message in the interrupt handler on SOF */
  42848. +#ifndef DEBUG_SOF
  42849. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42850. +#endif
  42851. + DWC_DEBUGPL(DBG_HCDI, "\n");
  42852. +#endif
  42853. +
  42854. +#ifdef DEBUG
  42855. +#ifndef DEBUG_SOF
  42856. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42857. +#endif
  42858. + DWC_DEBUGPL(DBG_HCDI,
  42859. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  42860. + gintsts.d32, core_if);
  42861. +#endif
  42862. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  42863. + if (gintsts.b.sofintr) {
  42864. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  42865. + }
  42866. +
  42867. + if (gintsts.b.rxstsqlvl) {
  42868. + retval |=
  42869. + dwc_otg_hcd_handle_rx_status_q_level_intr
  42870. + (dwc_otg_hcd);
  42871. + }
  42872. + if (gintsts.b.nptxfempty) {
  42873. + retval |=
  42874. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  42875. + (dwc_otg_hcd);
  42876. + }
  42877. + if (gintsts.b.i2cintr) {
  42878. + /** @todo Implement i2cintr handler. */
  42879. + }
  42880. + if (gintsts.b.portintr) {
  42881. +
  42882. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  42883. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  42884. + if (fiq_enable) {
  42885. + local_fiq_disable();
  42886. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42887. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  42888. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42889. + local_fiq_enable();
  42890. + } else {
  42891. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  42892. + }
  42893. + }
  42894. + if (gintsts.b.hcintr) {
  42895. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  42896. + }
  42897. + if (gintsts.b.ptxfempty) {
  42898. + retval |=
  42899. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  42900. + (dwc_otg_hcd);
  42901. + }
  42902. +#ifdef DEBUG
  42903. +#ifndef DEBUG_SOF
  42904. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42905. +#endif
  42906. + {
  42907. + DWC_DEBUGPL(DBG_HCDI,
  42908. + "DWC OTG HCD Finished Servicing Interrupts\n");
  42909. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  42910. + DWC_READ_REG32(&global_regs->gintsts));
  42911. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  42912. + DWC_READ_REG32(&global_regs->gintmsk));
  42913. + }
  42914. +#endif
  42915. +
  42916. +#ifdef DEBUG
  42917. +#ifndef DEBUG_SOF
  42918. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42919. +#endif
  42920. + DWC_DEBUGPL(DBG_HCDI, "\n");
  42921. +#endif
  42922. +
  42923. + }
  42924. +
  42925. +exit_handler_routine:
  42926. + if (fiq_enable) {
  42927. + gintmsk_data_t gintmsk_new;
  42928. + haintmsk_data_t haintmsk_new;
  42929. + local_fiq_disable();
  42930. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42931. + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
  42932. + if(fiq_fsm_enable)
  42933. + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
  42934. + else
  42935. + haintmsk_new.d32 = 0x0000FFFF;
  42936. +
  42937. + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
  42938. + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
  42939. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
  42940. + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
  42941. + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
  42942. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
  42943. + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
  42944. + ;
  42945. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
  42946. + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
  42947. + }
  42948. + int_done++;
  42949. + }
  42950. + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  42951. + /* Re-enable interrupts that the FIQ masked (first time round) */
  42952. + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
  42953. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42954. + local_fiq_enable();
  42955. +
  42956. + if ((jiffies / HZ) > last_time) {
  42957. + //dwc_otg_qh_t *qh;
  42958. + //dwc_list_link_t *cur;
  42959. + /* Once a second output the fiq and irq numbers, useful for debug */
  42960. + last_time = jiffies / HZ;
  42961. + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
  42962. + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
  42963. + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
  42964. + //printk(KERN_WARNING "Periodic queues:\n");
  42965. + }
  42966. + }
  42967. +
  42968. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  42969. + return retval;
  42970. +}
  42971. +
  42972. +#ifdef DWC_TRACK_MISSED_SOFS
  42973. +
  42974. +#warning Compiling code to track missed SOFs
  42975. +#define FRAME_NUM_ARRAY_SIZE 1000
  42976. +/**
  42977. + * This function is for debug only.
  42978. + */
  42979. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  42980. +{
  42981. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  42982. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  42983. + static int frame_num_idx = 0;
  42984. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  42985. + static int dumped_frame_num_array = 0;
  42986. +
  42987. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  42988. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  42989. + curr_frame_number) {
  42990. + frame_num_array[frame_num_idx] = curr_frame_number;
  42991. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  42992. + }
  42993. + } else if (!dumped_frame_num_array) {
  42994. + int i;
  42995. + DWC_PRINTF("Frame Last Frame\n");
  42996. + DWC_PRINTF("----- ----------\n");
  42997. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  42998. + DWC_PRINTF("0x%04x 0x%04x\n",
  42999. + frame_num_array[i], last_frame_num_array[i]);
  43000. + }
  43001. + dumped_frame_num_array = 1;
  43002. + }
  43003. + last_frame_num = curr_frame_number;
  43004. +}
  43005. +#endif
  43006. +
  43007. +/**
  43008. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  43009. + * transactions may be queued to the DWC_otg controller for the current
  43010. + * (micro)frame. Periodic transactions may be queued to the controller for the
  43011. + * next (micro)frame.
  43012. + */
  43013. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  43014. +{
  43015. + hfnum_data_t hfnum;
  43016. + gintsts_data_t gintsts = { .d32 = 0 };
  43017. + dwc_list_link_t *qh_entry;
  43018. + dwc_otg_qh_t *qh;
  43019. + dwc_otg_transaction_type_e tr_type;
  43020. + int did_something = 0;
  43021. + int32_t next_sched_frame = -1;
  43022. +
  43023. + hfnum.d32 =
  43024. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  43025. +
  43026. +#ifdef DEBUG_SOF
  43027. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  43028. +#endif
  43029. + hcd->frame_number = hfnum.b.frnum;
  43030. +
  43031. +#ifdef DEBUG
  43032. + hcd->frrem_accum += hfnum.b.frrem;
  43033. + hcd->frrem_samples++;
  43034. +#endif
  43035. +
  43036. +#ifdef DWC_TRACK_MISSED_SOFS
  43037. + track_missed_sofs(hcd->frame_number);
  43038. +#endif
  43039. + /* Determine whether any periodic QHs should be executed. */
  43040. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  43041. + while (qh_entry != &hcd->periodic_sched_inactive) {
  43042. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  43043. + qh_entry = qh_entry->next;
  43044. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  43045. +
  43046. + /*
  43047. + * Move QH to the ready list to be executed next
  43048. + * (micro)frame.
  43049. + */
  43050. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  43051. + &qh->qh_list_entry);
  43052. +
  43053. + did_something = 1;
  43054. + }
  43055. + else
  43056. + {
  43057. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  43058. + {
  43059. + next_sched_frame = qh->sched_frame;
  43060. + }
  43061. + }
  43062. + }
  43063. + if (fiq_enable)
  43064. + hcd->fiq_state->next_sched_frame = next_sched_frame;
  43065. +
  43066. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  43067. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  43068. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  43069. + did_something = 1;
  43070. + }
  43071. +
  43072. + /* Clear interrupt - but do not trample on the FIQ sof */
  43073. + if (!fiq_fsm_enable) {
  43074. + gintsts.b.sofintr = 1;
  43075. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  43076. + }
  43077. + return 1;
  43078. +}
  43079. +
  43080. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  43081. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  43082. + * memory if the DWC_otg controller is operating in Slave mode. */
  43083. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43084. +{
  43085. + host_grxsts_data_t grxsts;
  43086. + dwc_hc_t *hc = NULL;
  43087. +
  43088. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  43089. +
  43090. + grxsts.d32 =
  43091. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  43092. +
  43093. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  43094. + if (!hc) {
  43095. + DWC_ERROR("Unable to get corresponding channel\n");
  43096. + return 0;
  43097. + }
  43098. +
  43099. + /* Packet Status */
  43100. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  43101. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  43102. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  43103. + hc->data_pid_start);
  43104. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  43105. +
  43106. + switch (grxsts.b.pktsts) {
  43107. + case DWC_GRXSTS_PKTSTS_IN:
  43108. + /* Read the data into the host buffer. */
  43109. + if (grxsts.b.bcnt > 0) {
  43110. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  43111. + hc->xfer_buff, grxsts.b.bcnt);
  43112. +
  43113. + /* Update the HC fields for the next packet received. */
  43114. + hc->xfer_count += grxsts.b.bcnt;
  43115. + hc->xfer_buff += grxsts.b.bcnt;
  43116. + }
  43117. +
  43118. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  43119. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  43120. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  43121. + /* Handled in interrupt, just ignore data */
  43122. + break;
  43123. + default:
  43124. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  43125. + grxsts.b.pktsts);
  43126. + break;
  43127. + }
  43128. +
  43129. + return 1;
  43130. +}
  43131. +
  43132. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  43133. + * data packets may be written to the FIFO for OUT transfers. More requests
  43134. + * may be written to the non-periodic request queue for IN transfers. This
  43135. + * interrupt is enabled only in Slave mode. */
  43136. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43137. +{
  43138. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  43139. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  43140. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  43141. + return 1;
  43142. +}
  43143. +
  43144. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  43145. + * packets may be written to the FIFO for OUT transfers. More requests may be
  43146. + * written to the periodic request queue for IN transfers. This interrupt is
  43147. + * enabled only in Slave mode. */
  43148. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43149. +{
  43150. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  43151. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  43152. + DWC_OTG_TRANSACTION_PERIODIC);
  43153. + return 1;
  43154. +}
  43155. +
  43156. +/** There are multiple conditions that can cause a port interrupt. This function
  43157. + * determines which interrupt conditions have occurred and handles them
  43158. + * appropriately. */
  43159. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43160. +{
  43161. + int retval = 0;
  43162. + hprt0_data_t hprt0;
  43163. + hprt0_data_t hprt0_modify;
  43164. +
  43165. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43166. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43167. +
  43168. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  43169. + * GINTSTS */
  43170. +
  43171. + hprt0_modify.b.prtena = 0;
  43172. + hprt0_modify.b.prtconndet = 0;
  43173. + hprt0_modify.b.prtenchng = 0;
  43174. + hprt0_modify.b.prtovrcurrchng = 0;
  43175. +
  43176. + /* Port Connect Detected
  43177. + * Set flag and clear if detected */
  43178. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  43179. + // Dont modify port status if we are in hibernation state
  43180. + hprt0_modify.b.prtconndet = 1;
  43181. + hprt0_modify.b.prtenchng = 1;
  43182. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  43183. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43184. + return retval;
  43185. + }
  43186. +
  43187. + if (hprt0.b.prtconndet) {
  43188. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  43189. + if (dwc_otg_hcd->core_if->adp_enable &&
  43190. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  43191. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  43192. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  43193. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  43194. + /* TODO - check if this is required, as
  43195. + * host initialization was already performed
  43196. + * after initial ADP probing
  43197. + */
  43198. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  43199. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  43200. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  43201. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  43202. + } else {
  43203. +
  43204. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  43205. + "Port Connect Detected--\n", hprt0.d32);
  43206. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  43207. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  43208. + hprt0_modify.b.prtconndet = 1;
  43209. +
  43210. + /* B-Device has connected, Delete the connection timer. */
  43211. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  43212. + }
  43213. + /* The Hub driver asserts a reset when it sees port connect
  43214. + * status change flag */
  43215. + retval |= 1;
  43216. + }
  43217. +
  43218. + /* Port Enable Changed
  43219. + * Clear if detected - Set internal flag if disabled */
  43220. + if (hprt0.b.prtenchng) {
  43221. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  43222. + "Port Enable Changed--\n", hprt0.d32);
  43223. + hprt0_modify.b.prtenchng = 1;
  43224. + if (hprt0.b.prtena == 1) {
  43225. + hfir_data_t hfir;
  43226. + int do_reset = 0;
  43227. + dwc_otg_core_params_t *params =
  43228. + dwc_otg_hcd->core_if->core_params;
  43229. + dwc_otg_core_global_regs_t *global_regs =
  43230. + dwc_otg_hcd->core_if->core_global_regs;
  43231. + dwc_otg_host_if_t *host_if =
  43232. + dwc_otg_hcd->core_if->host_if;
  43233. +
  43234. + /* Every time when port enables calculate
  43235. + * HFIR.FrInterval
  43236. + */
  43237. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  43238. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  43239. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  43240. +
  43241. + /* Check if we need to adjust the PHY clock speed for
  43242. + * low power and adjust it */
  43243. + if (params->host_support_fs_ls_low_power) {
  43244. + gusbcfg_data_t usbcfg;
  43245. +
  43246. + usbcfg.d32 =
  43247. + DWC_READ_REG32(&global_regs->gusbcfg);
  43248. +
  43249. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  43250. + || hprt0.b.prtspd ==
  43251. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  43252. + /*
  43253. + * Low power
  43254. + */
  43255. + hcfg_data_t hcfg;
  43256. + if (usbcfg.b.phylpwrclksel == 0) {
  43257. + /* Set PHY low power clock select for FS/LS devices */
  43258. + usbcfg.b.phylpwrclksel = 1;
  43259. + DWC_WRITE_REG32
  43260. + (&global_regs->gusbcfg,
  43261. + usbcfg.d32);
  43262. + do_reset = 1;
  43263. + }
  43264. +
  43265. + hcfg.d32 =
  43266. + DWC_READ_REG32
  43267. + (&host_if->host_global_regs->hcfg);
  43268. +
  43269. + if (hprt0.b.prtspd ==
  43270. + DWC_HPRT0_PRTSPD_LOW_SPEED
  43271. + && params->host_ls_low_power_phy_clk
  43272. + ==
  43273. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  43274. + {
  43275. + /* 6 MHZ */
  43276. + DWC_DEBUGPL(DBG_CIL,
  43277. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  43278. + if (hcfg.b.fslspclksel !=
  43279. + DWC_HCFG_6_MHZ) {
  43280. + hcfg.b.fslspclksel =
  43281. + DWC_HCFG_6_MHZ;
  43282. + DWC_WRITE_REG32
  43283. + (&host_if->host_global_regs->hcfg,
  43284. + hcfg.d32);
  43285. + do_reset = 1;
  43286. + }
  43287. + } else {
  43288. + /* 48 MHZ */
  43289. + DWC_DEBUGPL(DBG_CIL,
  43290. + "FS_PHY programming HCFG to 48 MHz ()\n");
  43291. + if (hcfg.b.fslspclksel !=
  43292. + DWC_HCFG_48_MHZ) {
  43293. + hcfg.b.fslspclksel =
  43294. + DWC_HCFG_48_MHZ;
  43295. + DWC_WRITE_REG32
  43296. + (&host_if->host_global_regs->hcfg,
  43297. + hcfg.d32);
  43298. + do_reset = 1;
  43299. + }
  43300. + }
  43301. + } else {
  43302. + /*
  43303. + * Not low power
  43304. + */
  43305. + if (usbcfg.b.phylpwrclksel == 1) {
  43306. + usbcfg.b.phylpwrclksel = 0;
  43307. + DWC_WRITE_REG32
  43308. + (&global_regs->gusbcfg,
  43309. + usbcfg.d32);
  43310. + do_reset = 1;
  43311. + }
  43312. + }
  43313. +
  43314. + if (do_reset) {
  43315. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  43316. + }
  43317. + }
  43318. +
  43319. + if (!do_reset) {
  43320. + /* Port has been enabled set the reset change flag */
  43321. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  43322. + }
  43323. + } else {
  43324. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  43325. + }
  43326. + retval |= 1;
  43327. + }
  43328. +
  43329. + /** Overcurrent Change Interrupt */
  43330. + if (hprt0.b.prtovrcurrchng) {
  43331. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  43332. + "Port Overcurrent Changed--\n", hprt0.d32);
  43333. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  43334. + hprt0_modify.b.prtovrcurrchng = 1;
  43335. + retval |= 1;
  43336. + }
  43337. +
  43338. + /* Clear Port Interrupts */
  43339. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  43340. +
  43341. + return retval;
  43342. +}
  43343. +
  43344. +/** This interrupt indicates that one or more host channels has a pending
  43345. + * interrupt. There are multiple conditions that can cause each host channel
  43346. + * interrupt. This function determines which conditions have occurred for each
  43347. + * host channel interrupt and handles them appropriately. */
  43348. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43349. +{
  43350. + int i;
  43351. + int retval = 0;
  43352. + haint_data_t haint = { .d32 = 0 } ;
  43353. +
  43354. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  43355. + * GINTSTS */
  43356. +
  43357. + if (!fiq_fsm_enable)
  43358. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  43359. +
  43360. + // Overwrite with saved interrupts from fiq handler
  43361. + if(fiq_fsm_enable)
  43362. + {
  43363. + /* check the mask? */
  43364. + local_fiq_disable();
  43365. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  43366. + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
  43367. + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  43368. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  43369. + local_fiq_enable();
  43370. + }
  43371. +
  43372. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  43373. + if (haint.b2.chint & (1 << i)) {
  43374. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  43375. + }
  43376. + }
  43377. +
  43378. + return retval;
  43379. +}
  43380. +
  43381. +/**
  43382. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  43383. + * holds the reason for the halt.
  43384. + *
  43385. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  43386. + * *short_read is set to 1 upon return if less than the requested
  43387. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  43388. + * return. short_read may also be NULL on entry, in which case it remains
  43389. + * unchanged.
  43390. + */
  43391. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  43392. + dwc_otg_hc_regs_t * hc_regs,
  43393. + dwc_otg_qtd_t * qtd,
  43394. + dwc_otg_halt_status_e halt_status,
  43395. + int *short_read)
  43396. +{
  43397. + hctsiz_data_t hctsiz;
  43398. + uint32_t length;
  43399. +
  43400. + if (short_read != NULL) {
  43401. + *short_read = 0;
  43402. + }
  43403. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43404. +
  43405. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  43406. + if (hc->ep_is_in) {
  43407. + length = hc->xfer_len - hctsiz.b.xfersize;
  43408. + if (short_read != NULL) {
  43409. + *short_read = (hctsiz.b.xfersize != 0);
  43410. + }
  43411. + } else if (hc->qh->do_split) {
  43412. + //length = split_out_xfersize[hc->hc_num];
  43413. + length = qtd->ssplit_out_xfer_count;
  43414. + } else {
  43415. + length = hc->xfer_len;
  43416. + }
  43417. + } else {
  43418. + /*
  43419. + * Must use the hctsiz.pktcnt field to determine how much data
  43420. + * has been transferred. This field reflects the number of
  43421. + * packets that have been transferred via the USB. This is
  43422. + * always an integral number of packets if the transfer was
  43423. + * halted before its normal completion. (Can't use the
  43424. + * hctsiz.xfersize field because that reflects the number of
  43425. + * bytes transferred via the AHB, not the USB).
  43426. + */
  43427. + length =
  43428. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  43429. + }
  43430. +
  43431. + return length;
  43432. +}
  43433. +
  43434. +/**
  43435. + * Updates the state of the URB after a Transfer Complete interrupt on the
  43436. + * host channel. Updates the actual_length field of the URB based on the
  43437. + * number of bytes transferred via the host channel. Sets the URB status
  43438. + * if the data transfer is finished.
  43439. + *
  43440. + * @return 1 if the data transfer specified by the URB is completely finished,
  43441. + * 0 otherwise.
  43442. + */
  43443. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  43444. + dwc_otg_hc_regs_t * hc_regs,
  43445. + dwc_otg_hcd_urb_t * urb,
  43446. + dwc_otg_qtd_t * qtd)
  43447. +{
  43448. + int xfer_done = 0;
  43449. + int short_read = 0;
  43450. +
  43451. + int xfer_length;
  43452. +
  43453. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  43454. + DWC_OTG_HC_XFER_COMPLETE,
  43455. + &short_read);
  43456. +
  43457. + /* non DWORD-aligned buffer case handling. */
  43458. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  43459. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  43460. + xfer_length);
  43461. + }
  43462. +
  43463. + urb->actual_length += xfer_length;
  43464. +
  43465. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  43466. + (urb->flags & URB_SEND_ZERO_PACKET)
  43467. + && (urb->actual_length == urb->length)
  43468. + && !(urb->length % hc->max_packet)) {
  43469. + xfer_done = 0;
  43470. + } else if (short_read || urb->actual_length >= urb->length) {
  43471. + xfer_done = 1;
  43472. + urb->status = 0;
  43473. + }
  43474. +
  43475. +#ifdef DEBUG
  43476. + {
  43477. + hctsiz_data_t hctsiz;
  43478. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43479. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  43480. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  43481. + hc->hc_num);
  43482. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  43483. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  43484. + hctsiz.b.xfersize);
  43485. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  43486. + urb->length);
  43487. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  43488. + urb->actual_length);
  43489. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  43490. + short_read, xfer_done);
  43491. + }
  43492. +#endif
  43493. +
  43494. + return xfer_done;
  43495. +}
  43496. +
  43497. +/*
  43498. + * Save the starting data toggle for the next transfer. The data toggle is
  43499. + * saved in the QH for non-control transfers and it's saved in the QTD for
  43500. + * control transfers.
  43501. + */
  43502. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  43503. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  43504. +{
  43505. + hctsiz_data_t hctsiz;
  43506. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43507. +
  43508. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  43509. + dwc_otg_qh_t *qh = hc->qh;
  43510. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  43511. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  43512. + } else {
  43513. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  43514. + }
  43515. + } else {
  43516. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  43517. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  43518. + } else {
  43519. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  43520. + }
  43521. + }
  43522. +}
  43523. +
  43524. +/**
  43525. + * Updates the state of an Isochronous URB when the transfer is stopped for
  43526. + * any reason. The fields of the current entry in the frame descriptor array
  43527. + * are set based on the transfer state and the input _halt_status. Completes
  43528. + * the Isochronous URB if all the URB frames have been completed.
  43529. + *
  43530. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  43531. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  43532. + */
  43533. +static dwc_otg_halt_status_e
  43534. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  43535. + dwc_hc_t * hc,
  43536. + dwc_otg_hc_regs_t * hc_regs,
  43537. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  43538. +{
  43539. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  43540. + dwc_otg_halt_status_e ret_val = halt_status;
  43541. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  43542. +
  43543. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  43544. + switch (halt_status) {
  43545. + case DWC_OTG_HC_XFER_COMPLETE:
  43546. + frame_desc->status = 0;
  43547. + frame_desc->actual_length =
  43548. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  43549. +
  43550. + /* non DWORD-aligned buffer case handling. */
  43551. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  43552. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  43553. + hc->qh->dw_align_buf, frame_desc->actual_length);
  43554. + }
  43555. +
  43556. + break;
  43557. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  43558. + urb->error_count++;
  43559. + if (hc->ep_is_in) {
  43560. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  43561. + } else {
  43562. + frame_desc->status = -DWC_E_COMMUNICATION;
  43563. + }
  43564. + frame_desc->actual_length = 0;
  43565. + break;
  43566. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  43567. + urb->error_count++;
  43568. + frame_desc->status = -DWC_E_OVERFLOW;
  43569. + /* Don't need to update actual_length in this case. */
  43570. + break;
  43571. + case DWC_OTG_HC_XFER_XACT_ERR:
  43572. + urb->error_count++;
  43573. + frame_desc->status = -DWC_E_PROTOCOL;
  43574. + frame_desc->actual_length =
  43575. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  43576. +
  43577. + /* non DWORD-aligned buffer case handling. */
  43578. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  43579. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  43580. + hc->qh->dw_align_buf, frame_desc->actual_length);
  43581. + }
  43582. + /* Skip whole frame */
  43583. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  43584. + hc->ep_is_in && hcd->core_if->dma_enable) {
  43585. + qtd->complete_split = 0;
  43586. + qtd->isoc_split_offset = 0;
  43587. + }
  43588. +
  43589. + break;
  43590. + default:
  43591. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  43592. + break;
  43593. + }
  43594. + if (++qtd->isoc_frame_index == urb->packet_count) {
  43595. + /*
  43596. + * urb->status is not used for isoc transfers.
  43597. + * The individual frame_desc statuses are used instead.
  43598. + */
  43599. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  43600. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  43601. + } else {
  43602. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  43603. + }
  43604. + return ret_val;
  43605. +}
  43606. +
  43607. +/**
  43608. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  43609. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  43610. + * still linked to the QH, the QH is added to the end of the inactive
  43611. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  43612. + * schedule if no more QTDs are linked to the QH.
  43613. + */
  43614. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  43615. +{
  43616. + int continue_split = 0;
  43617. + dwc_otg_qtd_t *qtd;
  43618. +
  43619. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  43620. +
  43621. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  43622. +
  43623. + if (qtd->complete_split) {
  43624. + continue_split = 1;
  43625. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  43626. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  43627. + continue_split = 1;
  43628. + }
  43629. +
  43630. + if (free_qtd) {
  43631. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  43632. + continue_split = 0;
  43633. + }
  43634. +
  43635. + qh->channel = NULL;
  43636. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  43637. +}
  43638. +
  43639. +/**
  43640. + * Releases a host channel for use by other transfers. Attempts to select and
  43641. + * queue more transactions since at least one host channel is available.
  43642. + *
  43643. + * @param hcd The HCD state structure.
  43644. + * @param hc The host channel to release.
  43645. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  43646. + * if the transfer is complete or an error has occurred.
  43647. + * @param halt_status Reason the channel is being released. This status
  43648. + * determines the actions taken by this function.
  43649. + */
  43650. +static void release_channel(dwc_otg_hcd_t * hcd,
  43651. + dwc_hc_t * hc,
  43652. + dwc_otg_qtd_t * qtd,
  43653. + dwc_otg_halt_status_e halt_status)
  43654. +{
  43655. + dwc_otg_transaction_type_e tr_type;
  43656. + int free_qtd;
  43657. + dwc_irqflags_t flags;
  43658. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  43659. +
  43660. + int hog_port = 0;
  43661. +
  43662. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  43663. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  43664. +
  43665. + if(fiq_fsm_enable && hc->do_split) {
  43666. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  43667. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  43668. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  43669. + hog_port = 0;
  43670. + }
  43671. + }
  43672. + }
  43673. +
  43674. + switch (halt_status) {
  43675. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  43676. + free_qtd = 1;
  43677. + break;
  43678. + case DWC_OTG_HC_XFER_AHB_ERR:
  43679. + case DWC_OTG_HC_XFER_STALL:
  43680. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  43681. + free_qtd = 1;
  43682. + break;
  43683. + case DWC_OTG_HC_XFER_XACT_ERR:
  43684. + if (qtd->error_count >= 3) {
  43685. + DWC_DEBUGPL(DBG_HCDV,
  43686. + " Complete URB with transaction error\n");
  43687. + free_qtd = 1;
  43688. + qtd->urb->status = -DWC_E_PROTOCOL;
  43689. + hcd->fops->complete(hcd, qtd->urb->priv,
  43690. + qtd->urb, -DWC_E_PROTOCOL);
  43691. + } else {
  43692. + free_qtd = 0;
  43693. + }
  43694. + break;
  43695. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  43696. + /*
  43697. + * The QTD has already been removed and the QH has been
  43698. + * deactivated. Don't want to do anything except release the
  43699. + * host channel and try to queue more transfers.
  43700. + */
  43701. + goto cleanup;
  43702. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  43703. + free_qtd = 0;
  43704. + break;
  43705. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  43706. + DWC_DEBUGPL(DBG_HCDV,
  43707. + " Complete URB with I/O error\n");
  43708. + free_qtd = 1;
  43709. + qtd->urb->status = -DWC_E_IO;
  43710. + hcd->fops->complete(hcd, qtd->urb->priv,
  43711. + qtd->urb, -DWC_E_IO);
  43712. + break;
  43713. + default:
  43714. + free_qtd = 0;
  43715. + break;
  43716. + }
  43717. +
  43718. + deactivate_qh(hcd, hc->qh, free_qtd);
  43719. +
  43720. +cleanup:
  43721. + /*
  43722. + * Release the host channel for use by other transfers. The cleanup
  43723. + * function clears the channel interrupt enables and conditions, so
  43724. + * there's no need to clear the Channel Halted interrupt separately.
  43725. + */
  43726. + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
  43727. + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
  43728. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  43729. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  43730. +
  43731. + if (!microframe_schedule) {
  43732. + switch (hc->ep_type) {
  43733. + case DWC_OTG_EP_TYPE_CONTROL:
  43734. + case DWC_OTG_EP_TYPE_BULK:
  43735. + hcd->non_periodic_channels--;
  43736. + break;
  43737. +
  43738. + default:
  43739. + /*
  43740. + * Don't release reservations for periodic channels here.
  43741. + * That's done when a periodic transfer is descheduled (i.e.
  43742. + * when the QH is removed from the periodic schedule).
  43743. + */
  43744. + break;
  43745. + }
  43746. + } else {
  43747. +
  43748. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  43749. + hcd->available_host_channels++;
  43750. + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
  43751. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  43752. + }
  43753. +
  43754. + /* Try to queue more transfers now that there's a free channel. */
  43755. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  43756. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  43757. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  43758. + }
  43759. +}
  43760. +
  43761. +/**
  43762. + * Halts a host channel. If the channel cannot be halted immediately because
  43763. + * the request queue is full, this function ensures that the FIFO empty
  43764. + * interrupt for the appropriate queue is enabled so that the halt request can
  43765. + * be queued when there is space in the request queue.
  43766. + *
  43767. + * This function may also be called in DMA mode. In that case, the channel is
  43768. + * simply released since the core always halts the channel automatically in
  43769. + * DMA mode.
  43770. + */
  43771. +static void halt_channel(dwc_otg_hcd_t * hcd,
  43772. + dwc_hc_t * hc,
  43773. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  43774. +{
  43775. + if (hcd->core_if->dma_enable) {
  43776. + release_channel(hcd, hc, qtd, halt_status);
  43777. + return;
  43778. + }
  43779. +
  43780. + /* Slave mode processing... */
  43781. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  43782. +
  43783. + if (hc->halt_on_queue) {
  43784. + gintmsk_data_t gintmsk = {.d32 = 0 };
  43785. + dwc_otg_core_global_regs_t *global_regs;
  43786. + global_regs = hcd->core_if->core_global_regs;
  43787. +
  43788. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  43789. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  43790. + /*
  43791. + * Make sure the Non-periodic Tx FIFO empty interrupt
  43792. + * is enabled so that the non-periodic schedule will
  43793. + * be processed.
  43794. + */
  43795. + gintmsk.b.nptxfempty = 1;
  43796. + if (fiq_enable) {
  43797. + local_fiq_disable();
  43798. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  43799. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43800. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  43801. + local_fiq_enable();
  43802. + } else {
  43803. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43804. + }
  43805. + } else {
  43806. + /*
  43807. + * Move the QH from the periodic queued schedule to
  43808. + * the periodic assigned schedule. This allows the
  43809. + * halt to be queued when the periodic schedule is
  43810. + * processed.
  43811. + */
  43812. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  43813. + &hc->qh->qh_list_entry);
  43814. +
  43815. + /*
  43816. + * Make sure the Periodic Tx FIFO Empty interrupt is
  43817. + * enabled so that the periodic schedule will be
  43818. + * processed.
  43819. + */
  43820. + gintmsk.b.ptxfempty = 1;
  43821. + if (fiq_enable) {
  43822. + local_fiq_disable();
  43823. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  43824. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43825. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  43826. + local_fiq_enable();
  43827. + } else {
  43828. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43829. + }
  43830. + }
  43831. + }
  43832. +}
  43833. +
  43834. +/**
  43835. + * Performs common cleanup for non-periodic transfers after a Transfer
  43836. + * Complete interrupt. This function should be called after any endpoint type
  43837. + * specific handling is finished to release the host channel.
  43838. + */
  43839. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  43840. + dwc_hc_t * hc,
  43841. + dwc_otg_hc_regs_t * hc_regs,
  43842. + dwc_otg_qtd_t * qtd,
  43843. + dwc_otg_halt_status_e halt_status)
  43844. +{
  43845. + hcint_data_t hcint;
  43846. +
  43847. + qtd->error_count = 0;
  43848. +
  43849. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  43850. + if (hcint.b.nyet) {
  43851. + /*
  43852. + * Got a NYET on the last transaction of the transfer. This
  43853. + * means that the endpoint should be in the PING state at the
  43854. + * beginning of the next transfer.
  43855. + */
  43856. + hc->qh->ping_state = 1;
  43857. + clear_hc_int(hc_regs, nyet);
  43858. + }
  43859. +
  43860. + /*
  43861. + * Always halt and release the host channel to make it available for
  43862. + * more transfers. There may still be more phases for a control
  43863. + * transfer or more data packets for a bulk transfer at this point,
  43864. + * but the host channel is still halted. A channel will be reassigned
  43865. + * to the transfer when the non-periodic schedule is processed after
  43866. + * the channel is released. This allows transactions to be queued
  43867. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  43868. + * Tx FIFO Empty interrupt if necessary.
  43869. + */
  43870. + if (hc->ep_is_in) {
  43871. + /*
  43872. + * IN transfers in Slave mode require an explicit disable to
  43873. + * halt the channel. (In DMA mode, this call simply releases
  43874. + * the channel.)
  43875. + */
  43876. + halt_channel(hcd, hc, qtd, halt_status);
  43877. + } else {
  43878. + /*
  43879. + * The channel is automatically disabled by the core for OUT
  43880. + * transfers in Slave mode.
  43881. + */
  43882. + release_channel(hcd, hc, qtd, halt_status);
  43883. + }
  43884. +}
  43885. +
  43886. +/**
  43887. + * Performs common cleanup for periodic transfers after a Transfer Complete
  43888. + * interrupt. This function should be called after any endpoint type specific
  43889. + * handling is finished to release the host channel.
  43890. + */
  43891. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  43892. + dwc_hc_t * hc,
  43893. + dwc_otg_hc_regs_t * hc_regs,
  43894. + dwc_otg_qtd_t * qtd,
  43895. + dwc_otg_halt_status_e halt_status)
  43896. +{
  43897. + hctsiz_data_t hctsiz;
  43898. + qtd->error_count = 0;
  43899. +
  43900. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43901. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  43902. + /* Core halts channel in these cases. */
  43903. + release_channel(hcd, hc, qtd, halt_status);
  43904. + } else {
  43905. + /* Flush any outstanding requests from the Tx queue. */
  43906. + halt_channel(hcd, hc, qtd, halt_status);
  43907. + }
  43908. +}
  43909. +
  43910. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  43911. + dwc_hc_t * hc,
  43912. + dwc_otg_hc_regs_t * hc_regs,
  43913. + dwc_otg_qtd_t * qtd)
  43914. +{
  43915. + uint32_t len;
  43916. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  43917. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  43918. +
  43919. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  43920. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  43921. +
  43922. + if (!len) {
  43923. + qtd->complete_split = 0;
  43924. + qtd->isoc_split_offset = 0;
  43925. + return 0;
  43926. + }
  43927. + frame_desc->actual_length += len;
  43928. +
  43929. + if (hc->align_buff && len)
  43930. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  43931. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  43932. + qtd->isoc_split_offset += len;
  43933. +
  43934. + if (frame_desc->length == frame_desc->actual_length) {
  43935. + frame_desc->status = 0;
  43936. + qtd->isoc_frame_index++;
  43937. + qtd->complete_split = 0;
  43938. + qtd->isoc_split_offset = 0;
  43939. + }
  43940. +
  43941. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  43942. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  43943. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  43944. + } else {
  43945. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  43946. + }
  43947. +
  43948. + return 1; /* Indicates that channel released */
  43949. +}
  43950. +
  43951. +/**
  43952. + * Handles a host channel Transfer Complete interrupt. This handler may be
  43953. + * called in either DMA mode or Slave mode.
  43954. + */
  43955. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  43956. + dwc_hc_t * hc,
  43957. + dwc_otg_hc_regs_t * hc_regs,
  43958. + dwc_otg_qtd_t * qtd)
  43959. +{
  43960. + int urb_xfer_done;
  43961. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  43962. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  43963. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  43964. +
  43965. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  43966. + "Transfer Complete--\n", hc->hc_num);
  43967. +
  43968. + if (hcd->core_if->dma_desc_enable) {
  43969. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  43970. + if (pipe_type == UE_ISOCHRONOUS) {
  43971. + /* Do not disable the interrupt, just clear it */
  43972. + clear_hc_int(hc_regs, xfercomp);
  43973. + return 1;
  43974. + }
  43975. + goto handle_xfercomp_done;
  43976. + }
  43977. +
  43978. + /*
  43979. + * Handle xfer complete on CSPLIT.
  43980. + */
  43981. +
  43982. + if (hc->qh->do_split) {
  43983. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  43984. + && hcd->core_if->dma_enable) {
  43985. + if (qtd->complete_split
  43986. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  43987. + qtd))
  43988. + goto handle_xfercomp_done;
  43989. + } else {
  43990. + qtd->complete_split = 0;
  43991. + }
  43992. + }
  43993. +
  43994. + /* Update the QTD and URB states. */
  43995. + switch (pipe_type) {
  43996. + case UE_CONTROL:
  43997. + switch (qtd->control_phase) {
  43998. + case DWC_OTG_CONTROL_SETUP:
  43999. + if (urb->length > 0) {
  44000. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  44001. + } else {
  44002. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  44003. + }
  44004. + DWC_DEBUGPL(DBG_HCDV,
  44005. + " Control setup transaction done\n");
  44006. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44007. + break;
  44008. + case DWC_OTG_CONTROL_DATA:{
  44009. + urb_xfer_done =
  44010. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  44011. + qtd);
  44012. + if (urb_xfer_done) {
  44013. + qtd->control_phase =
  44014. + DWC_OTG_CONTROL_STATUS;
  44015. + DWC_DEBUGPL(DBG_HCDV,
  44016. + " Control data transfer done\n");
  44017. + } else {
  44018. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44019. + }
  44020. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44021. + break;
  44022. + }
  44023. + case DWC_OTG_CONTROL_STATUS:
  44024. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  44025. + if (urb->status == -DWC_E_IN_PROGRESS) {
  44026. + urb->status = 0;
  44027. + }
  44028. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44029. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44030. + break;
  44031. + }
  44032. +
  44033. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44034. + break;
  44035. + case UE_BULK:
  44036. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  44037. + urb_xfer_done =
  44038. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  44039. + if (urb_xfer_done) {
  44040. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44041. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44042. + } else {
  44043. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44044. + }
  44045. +
  44046. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44047. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44048. + break;
  44049. + case UE_INTERRUPT:
  44050. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  44051. + urb_xfer_done =
  44052. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  44053. +
  44054. + /*
  44055. + * Interrupt URB is done on the first transfer complete
  44056. + * interrupt.
  44057. + */
  44058. + if (urb_xfer_done) {
  44059. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44060. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44061. + } else {
  44062. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44063. + }
  44064. +
  44065. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44066. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44067. + break;
  44068. + case UE_ISOCHRONOUS:
  44069. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  44070. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  44071. + halt_status =
  44072. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44073. + DWC_OTG_HC_XFER_COMPLETE);
  44074. + }
  44075. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44076. + break;
  44077. + }
  44078. +
  44079. +handle_xfercomp_done:
  44080. + disable_hc_int(hc_regs, xfercompl);
  44081. +
  44082. + return 1;
  44083. +}
  44084. +
  44085. +/**
  44086. + * Handles a host channel STALL interrupt. This handler may be called in
  44087. + * either DMA mode or Slave mode.
  44088. + */
  44089. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  44090. + dwc_hc_t * hc,
  44091. + dwc_otg_hc_regs_t * hc_regs,
  44092. + dwc_otg_qtd_t * qtd)
  44093. +{
  44094. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44095. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  44096. +
  44097. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  44098. + "STALL Received--\n", hc->hc_num);
  44099. +
  44100. + if (hcd->core_if->dma_desc_enable) {
  44101. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  44102. + goto handle_stall_done;
  44103. + }
  44104. +
  44105. + if (pipe_type == UE_CONTROL) {
  44106. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  44107. + }
  44108. +
  44109. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  44110. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  44111. + /*
  44112. + * USB protocol requires resetting the data toggle for bulk
  44113. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  44114. + * setup command is issued to the endpoint. Anticipate the
  44115. + * CLEAR_FEATURE command since a STALL has occurred and reset
  44116. + * the data toggle now.
  44117. + */
  44118. + hc->qh->data_toggle = 0;
  44119. + }
  44120. +
  44121. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  44122. +
  44123. +handle_stall_done:
  44124. + disable_hc_int(hc_regs, stall);
  44125. +
  44126. + return 1;
  44127. +}
  44128. +
  44129. +/*
  44130. + * Updates the state of the URB when a transfer has been stopped due to an
  44131. + * abnormal condition before the transfer completes. Modifies the
  44132. + * actual_length field of the URB to reflect the number of bytes that have
  44133. + * actually been transferred via the host channel.
  44134. + */
  44135. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  44136. + dwc_otg_hc_regs_t * hc_regs,
  44137. + dwc_otg_hcd_urb_t * urb,
  44138. + dwc_otg_qtd_t * qtd,
  44139. + dwc_otg_halt_status_e halt_status)
  44140. +{
  44141. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  44142. + halt_status, NULL);
  44143. + /* non DWORD-aligned buffer case handling. */
  44144. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  44145. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  44146. + bytes_transferred);
  44147. + }
  44148. +
  44149. + urb->actual_length += bytes_transferred;
  44150. +
  44151. +#ifdef DEBUG
  44152. + {
  44153. + hctsiz_data_t hctsiz;
  44154. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44155. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  44156. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  44157. + hc->hc_num);
  44158. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  44159. + hc->start_pkt_count);
  44160. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  44161. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  44162. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  44163. + bytes_transferred);
  44164. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  44165. + urb->actual_length);
  44166. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  44167. + urb->length);
  44168. + }
  44169. +#endif
  44170. +}
  44171. +
  44172. +/**
  44173. + * Handles a host channel NAK interrupt. This handler may be called in either
  44174. + * DMA mode or Slave mode.
  44175. + */
  44176. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  44177. + dwc_hc_t * hc,
  44178. + dwc_otg_hc_regs_t * hc_regs,
  44179. + dwc_otg_qtd_t * qtd)
  44180. +{
  44181. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44182. + "NAK Received--\n", hc->hc_num);
  44183. +
  44184. + /*
  44185. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  44186. + * the beginning of the next frame
  44187. + */
  44188. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44189. + case UE_BULK:
  44190. + case UE_CONTROL:
  44191. + if (nak_holdoff && qtd->qh->do_split)
  44192. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  44193. + }
  44194. +
  44195. + /*
  44196. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  44197. + * interrupt. Re-start the SSPLIT transfer.
  44198. + */
  44199. + if (hc->do_split) {
  44200. + if (hc->complete_split) {
  44201. + qtd->error_count = 0;
  44202. + }
  44203. + qtd->complete_split = 0;
  44204. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44205. + goto handle_nak_done;
  44206. + }
  44207. +
  44208. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44209. + case UE_CONTROL:
  44210. + case UE_BULK:
  44211. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  44212. + /*
  44213. + * NAK interrupts are enabled on bulk/control IN
  44214. + * transfers in DMA mode for the sole purpose of
  44215. + * resetting the error count after a transaction error
  44216. + * occurs. The core will continue transferring data.
  44217. + * Disable other interrupts unmasked for the same
  44218. + * reason.
  44219. + */
  44220. + disable_hc_int(hc_regs, datatglerr);
  44221. + disable_hc_int(hc_regs, ack);
  44222. + qtd->error_count = 0;
  44223. + goto handle_nak_done;
  44224. + }
  44225. +
  44226. + /*
  44227. + * NAK interrupts normally occur during OUT transfers in DMA
  44228. + * or Slave mode. For IN transfers, more requests will be
  44229. + * queued as request queue space is available.
  44230. + */
  44231. + qtd->error_count = 0;
  44232. +
  44233. + if (!hc->qh->ping_state) {
  44234. + update_urb_state_xfer_intr(hc, hc_regs,
  44235. + qtd->urb, qtd,
  44236. + DWC_OTG_HC_XFER_NAK);
  44237. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44238. +
  44239. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  44240. + hc->qh->ping_state = 1;
  44241. + }
  44242. +
  44243. + /*
  44244. + * Halt the channel so the transfer can be re-started from
  44245. + * the appropriate point or the PING protocol will
  44246. + * start/continue.
  44247. + */
  44248. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44249. + break;
  44250. + case UE_INTERRUPT:
  44251. + qtd->error_count = 0;
  44252. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44253. + break;
  44254. + case UE_ISOCHRONOUS:
  44255. + /* Should never get called for isochronous transfers. */
  44256. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  44257. + break;
  44258. + }
  44259. +
  44260. +handle_nak_done:
  44261. + disable_hc_int(hc_regs, nak);
  44262. +
  44263. + return 1;
  44264. +}
  44265. +
  44266. +/**
  44267. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  44268. + * performing the PING protocol in Slave mode, when errors occur during
  44269. + * either Slave mode or DMA mode, and during Start Split transactions.
  44270. + */
  44271. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  44272. + dwc_hc_t * hc,
  44273. + dwc_otg_hc_regs_t * hc_regs,
  44274. + dwc_otg_qtd_t * qtd)
  44275. +{
  44276. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44277. + "ACK Received--\n", hc->hc_num);
  44278. +
  44279. + if (hc->do_split) {
  44280. + /*
  44281. + * Handle ACK on SSPLIT.
  44282. + * ACK should not occur in CSPLIT.
  44283. + */
  44284. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  44285. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  44286. + }
  44287. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  44288. + /* Don't need complete for isochronous out transfers. */
  44289. + qtd->complete_split = 1;
  44290. + }
  44291. +
  44292. + /* ISOC OUT */
  44293. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  44294. + switch (hc->xact_pos) {
  44295. + case DWC_HCSPLIT_XACTPOS_ALL:
  44296. + break;
  44297. + case DWC_HCSPLIT_XACTPOS_END:
  44298. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  44299. + qtd->isoc_split_offset = 0;
  44300. + break;
  44301. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  44302. + case DWC_HCSPLIT_XACTPOS_MID:
  44303. + /*
  44304. + * For BEGIN or MID, calculate the length for
  44305. + * the next microframe to determine the correct
  44306. + * SSPLIT token, either MID or END.
  44307. + */
  44308. + {
  44309. + struct dwc_otg_hcd_iso_packet_desc
  44310. + *frame_desc;
  44311. +
  44312. + frame_desc =
  44313. + &qtd->urb->
  44314. + iso_descs[qtd->isoc_frame_index];
  44315. + qtd->isoc_split_offset += 188;
  44316. +
  44317. + if ((frame_desc->length -
  44318. + qtd->isoc_split_offset) <= 188) {
  44319. + qtd->isoc_split_pos =
  44320. + DWC_HCSPLIT_XACTPOS_END;
  44321. + } else {
  44322. + qtd->isoc_split_pos =
  44323. + DWC_HCSPLIT_XACTPOS_MID;
  44324. + }
  44325. +
  44326. + }
  44327. + break;
  44328. + }
  44329. + } else {
  44330. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  44331. + }
  44332. + } else {
  44333. + /*
  44334. + * An unmasked ACK on a non-split DMA transaction is
  44335. + * for the sole purpose of resetting error counts. Disable other
  44336. + * interrupts unmasked for the same reason.
  44337. + */
  44338. + if(hcd->core_if->dma_enable) {
  44339. + disable_hc_int(hc_regs, datatglerr);
  44340. + disable_hc_int(hc_regs, nak);
  44341. + }
  44342. + qtd->error_count = 0;
  44343. +
  44344. + if (hc->qh->ping_state) {
  44345. + hc->qh->ping_state = 0;
  44346. + /*
  44347. + * Halt the channel so the transfer can be re-started
  44348. + * from the appropriate point. This only happens in
  44349. + * Slave mode. In DMA mode, the ping_state is cleared
  44350. + * when the transfer is started because the core
  44351. + * automatically executes the PING, then the transfer.
  44352. + */
  44353. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  44354. + }
  44355. + }
  44356. +
  44357. + /*
  44358. + * If the ACK occurred when _not_ in the PING state, let the channel
  44359. + * continue transferring data after clearing the error count.
  44360. + */
  44361. +
  44362. + disable_hc_int(hc_regs, ack);
  44363. +
  44364. + return 1;
  44365. +}
  44366. +
  44367. +/**
  44368. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  44369. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  44370. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  44371. + * handled in the xfercomp interrupt handler, not here. This handler may be
  44372. + * called in either DMA mode or Slave mode.
  44373. + */
  44374. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  44375. + dwc_hc_t * hc,
  44376. + dwc_otg_hc_regs_t * hc_regs,
  44377. + dwc_otg_qtd_t * qtd)
  44378. +{
  44379. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44380. + "NYET Received--\n", hc->hc_num);
  44381. +
  44382. + /*
  44383. + * NYET on CSPLIT
  44384. + * re-do the CSPLIT immediately on non-periodic
  44385. + */
  44386. + if (hc->do_split && hc->complete_split) {
  44387. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  44388. + && hcd->core_if->dma_enable) {
  44389. + qtd->complete_split = 0;
  44390. + qtd->isoc_split_offset = 0;
  44391. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  44392. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  44393. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  44394. + }
  44395. + else
  44396. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  44397. + goto handle_nyet_done;
  44398. + }
  44399. +
  44400. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  44401. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  44402. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  44403. +
  44404. + // With the FIQ running we only ever see the failed NYET
  44405. + if (dwc_full_frame_num(frnum) !=
  44406. + dwc_full_frame_num(hc->qh->sched_frame) ||
  44407. + fiq_fsm_enable) {
  44408. + /*
  44409. + * No longer in the same full speed frame.
  44410. + * Treat this as a transaction error.
  44411. + */
  44412. +#if 0
  44413. + /** @todo Fix system performance so this can
  44414. + * be treated as an error. Right now complete
  44415. + * splits cannot be scheduled precisely enough
  44416. + * due to other system activity, so this error
  44417. + * occurs regularly in Slave mode.
  44418. + */
  44419. + qtd->error_count++;
  44420. +#endif
  44421. + qtd->complete_split = 0;
  44422. + halt_channel(hcd, hc, qtd,
  44423. + DWC_OTG_HC_XFER_XACT_ERR);
  44424. + /** @todo add support for isoc release */
  44425. + goto handle_nyet_done;
  44426. + }
  44427. + }
  44428. +
  44429. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  44430. + goto handle_nyet_done;
  44431. + }
  44432. +
  44433. + hc->qh->ping_state = 1;
  44434. + qtd->error_count = 0;
  44435. +
  44436. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  44437. + DWC_OTG_HC_XFER_NYET);
  44438. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44439. +
  44440. + /*
  44441. + * Halt the channel and re-start the transfer so the PING
  44442. + * protocol will start.
  44443. + */
  44444. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  44445. +
  44446. +handle_nyet_done:
  44447. + disable_hc_int(hc_regs, nyet);
  44448. + return 1;
  44449. +}
  44450. +
  44451. +/**
  44452. + * Handles a host channel babble interrupt. This handler may be called in
  44453. + * either DMA mode or Slave mode.
  44454. + */
  44455. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  44456. + dwc_hc_t * hc,
  44457. + dwc_otg_hc_regs_t * hc_regs,
  44458. + dwc_otg_qtd_t * qtd)
  44459. +{
  44460. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44461. + "Babble Error--\n", hc->hc_num);
  44462. +
  44463. + if (hcd->core_if->dma_desc_enable) {
  44464. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44465. + DWC_OTG_HC_XFER_BABBLE_ERR);
  44466. + goto handle_babble_done;
  44467. + }
  44468. +
  44469. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  44470. + hcd->fops->complete(hcd, qtd->urb->priv,
  44471. + qtd->urb, -DWC_E_OVERFLOW);
  44472. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  44473. + } else {
  44474. + dwc_otg_halt_status_e halt_status;
  44475. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44476. + DWC_OTG_HC_XFER_BABBLE_ERR);
  44477. + halt_channel(hcd, hc, qtd, halt_status);
  44478. + }
  44479. +
  44480. +handle_babble_done:
  44481. + disable_hc_int(hc_regs, bblerr);
  44482. + return 1;
  44483. +}
  44484. +
  44485. +/**
  44486. + * Handles a host channel AHB error interrupt. This handler is only called in
  44487. + * DMA mode.
  44488. + */
  44489. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  44490. + dwc_hc_t * hc,
  44491. + dwc_otg_hc_regs_t * hc_regs,
  44492. + dwc_otg_qtd_t * qtd)
  44493. +{
  44494. + hcchar_data_t hcchar;
  44495. + hcsplt_data_t hcsplt;
  44496. + hctsiz_data_t hctsiz;
  44497. + uint32_t hcdma;
  44498. + char *pipetype, *speed;
  44499. +
  44500. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44501. +
  44502. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44503. + "AHB Error--\n", hc->hc_num);
  44504. +
  44505. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44506. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  44507. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44508. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  44509. +
  44510. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  44511. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  44512. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  44513. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  44514. + DWC_ERROR(" Device address: %d\n",
  44515. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  44516. + DWC_ERROR(" Endpoint: %d, %s\n",
  44517. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  44518. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  44519. +
  44520. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  44521. + case UE_CONTROL:
  44522. + pipetype = "CONTROL";
  44523. + break;
  44524. + case UE_BULK:
  44525. + pipetype = "BULK";
  44526. + break;
  44527. + case UE_INTERRUPT:
  44528. + pipetype = "INTERRUPT";
  44529. + break;
  44530. + case UE_ISOCHRONOUS:
  44531. + pipetype = "ISOCHRONOUS";
  44532. + break;
  44533. + default:
  44534. + pipetype = "UNKNOWN";
  44535. + break;
  44536. + }
  44537. +
  44538. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  44539. +
  44540. + switch (hc->speed) {
  44541. + case DWC_OTG_EP_SPEED_HIGH:
  44542. + speed = "HIGH";
  44543. + break;
  44544. + case DWC_OTG_EP_SPEED_FULL:
  44545. + speed = "FULL";
  44546. + break;
  44547. + case DWC_OTG_EP_SPEED_LOW:
  44548. + speed = "LOW";
  44549. + break;
  44550. + default:
  44551. + speed = "UNKNOWN";
  44552. + break;
  44553. + };
  44554. +
  44555. + DWC_ERROR(" Speed: %s\n", speed);
  44556. +
  44557. + DWC_ERROR(" Max packet size: %d\n",
  44558. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  44559. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  44560. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  44561. + urb->buf, (void *)urb->dma);
  44562. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  44563. + urb->setup_packet, (void *)urb->setup_dma);
  44564. + DWC_ERROR(" Interval: %d\n", urb->interval);
  44565. +
  44566. + /* Core haltes the channel for Descriptor DMA mode */
  44567. + if (hcd->core_if->dma_desc_enable) {
  44568. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44569. + DWC_OTG_HC_XFER_AHB_ERR);
  44570. + goto handle_ahberr_done;
  44571. + }
  44572. +
  44573. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  44574. +
  44575. + /*
  44576. + * Force a channel halt. Don't call halt_channel because that won't
  44577. + * write to the HCCHARn register in DMA mode to force the halt.
  44578. + */
  44579. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  44580. +handle_ahberr_done:
  44581. + disable_hc_int(hc_regs, ahberr);
  44582. + return 1;
  44583. +}
  44584. +
  44585. +/**
  44586. + * Handles a host channel transaction error interrupt. This handler may be
  44587. + * called in either DMA mode or Slave mode.
  44588. + */
  44589. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  44590. + dwc_hc_t * hc,
  44591. + dwc_otg_hc_regs_t * hc_regs,
  44592. + dwc_otg_qtd_t * qtd)
  44593. +{
  44594. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44595. + "Transaction Error--\n", hc->hc_num);
  44596. +
  44597. + if (hcd->core_if->dma_desc_enable) {
  44598. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44599. + DWC_OTG_HC_XFER_XACT_ERR);
  44600. + goto handle_xacterr_done;
  44601. + }
  44602. +
  44603. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44604. + case UE_CONTROL:
  44605. + case UE_BULK:
  44606. + qtd->error_count++;
  44607. + if (!hc->qh->ping_state) {
  44608. +
  44609. + update_urb_state_xfer_intr(hc, hc_regs,
  44610. + qtd->urb, qtd,
  44611. + DWC_OTG_HC_XFER_XACT_ERR);
  44612. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44613. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  44614. + hc->qh->ping_state = 1;
  44615. + }
  44616. + }
  44617. +
  44618. + /*
  44619. + * Halt the channel so the transfer can be re-started from
  44620. + * the appropriate point or the PING protocol will start.
  44621. + */
  44622. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44623. + break;
  44624. + case UE_INTERRUPT:
  44625. + qtd->error_count++;
  44626. + if (hc->do_split && hc->complete_split) {
  44627. + qtd->complete_split = 0;
  44628. + }
  44629. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44630. + break;
  44631. + case UE_ISOCHRONOUS:
  44632. + {
  44633. + dwc_otg_halt_status_e halt_status;
  44634. + halt_status =
  44635. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44636. + DWC_OTG_HC_XFER_XACT_ERR);
  44637. +
  44638. + halt_channel(hcd, hc, qtd, halt_status);
  44639. + }
  44640. + break;
  44641. + }
  44642. +handle_xacterr_done:
  44643. + disable_hc_int(hc_regs, xacterr);
  44644. +
  44645. + return 1;
  44646. +}
  44647. +
  44648. +/**
  44649. + * Handles a host channel frame overrun interrupt. This handler may be called
  44650. + * in either DMA mode or Slave mode.
  44651. + */
  44652. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  44653. + dwc_hc_t * hc,
  44654. + dwc_otg_hc_regs_t * hc_regs,
  44655. + dwc_otg_qtd_t * qtd)
  44656. +{
  44657. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44658. + "Frame Overrun--\n", hc->hc_num);
  44659. +
  44660. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44661. + case UE_CONTROL:
  44662. + case UE_BULK:
  44663. + break;
  44664. + case UE_INTERRUPT:
  44665. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  44666. + break;
  44667. + case UE_ISOCHRONOUS:
  44668. + {
  44669. + dwc_otg_halt_status_e halt_status;
  44670. + halt_status =
  44671. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44672. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  44673. +
  44674. + halt_channel(hcd, hc, qtd, halt_status);
  44675. + }
  44676. + break;
  44677. + }
  44678. +
  44679. + disable_hc_int(hc_regs, frmovrun);
  44680. +
  44681. + return 1;
  44682. +}
  44683. +
  44684. +/**
  44685. + * Handles a host channel data toggle error interrupt. This handler may be
  44686. + * called in either DMA mode or Slave mode.
  44687. + */
  44688. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  44689. + dwc_hc_t * hc,
  44690. + dwc_otg_hc_regs_t * hc_regs,
  44691. + dwc_otg_qtd_t * qtd)
  44692. +{
  44693. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44694. + "Data Toggle Error on %s transfer--\n",
  44695. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  44696. +
  44697. + /* Data toggles on split transactions cause the hc to halt.
  44698. + * restart transfer */
  44699. + if(hc->qh->do_split)
  44700. + {
  44701. + qtd->error_count++;
  44702. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44703. + update_urb_state_xfer_intr(hc, hc_regs,
  44704. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44705. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44706. + } else if (hc->ep_is_in) {
  44707. + /* An unmasked data toggle error on a non-split DMA transaction is
  44708. + * for the sole purpose of resetting error counts. Disable other
  44709. + * interrupts unmasked for the same reason.
  44710. + */
  44711. + if(hcd->core_if->dma_enable) {
  44712. + disable_hc_int(hc_regs, ack);
  44713. + disable_hc_int(hc_regs, nak);
  44714. + }
  44715. + qtd->error_count = 0;
  44716. + }
  44717. +
  44718. + disable_hc_int(hc_regs, datatglerr);
  44719. +
  44720. + return 1;
  44721. +}
  44722. +
  44723. +#ifdef DEBUG
  44724. +/**
  44725. + * This function is for debug only. It checks that a valid halt status is set
  44726. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  44727. + * taken and a warning is issued.
  44728. + * @return 1 if halt status is ok, 0 otherwise.
  44729. + */
  44730. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  44731. + dwc_hc_t * hc,
  44732. + dwc_otg_hc_regs_t * hc_regs,
  44733. + dwc_otg_qtd_t * qtd)
  44734. +{
  44735. + hcchar_data_t hcchar;
  44736. + hctsiz_data_t hctsiz;
  44737. + hcint_data_t hcint;
  44738. + hcintmsk_data_t hcintmsk;
  44739. + hcsplt_data_t hcsplt;
  44740. +
  44741. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  44742. + /*
  44743. + * This code is here only as a check. This condition should
  44744. + * never happen. Ignore the halt if it does occur.
  44745. + */
  44746. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44747. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44748. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  44749. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  44750. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  44751. + DWC_WARN
  44752. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  44753. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  44754. + "hcint 0x%08x, hcintmsk 0x%08x, "
  44755. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  44756. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  44757. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  44758. +
  44759. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  44760. + __func__, hc->hc_num);
  44761. + DWC_WARN("\n");
  44762. + clear_hc_int(hc_regs, chhltd);
  44763. + return 0;
  44764. + }
  44765. +
  44766. + /*
  44767. + * This code is here only as a check. hcchar.chdis should
  44768. + * never be set when the halt interrupt occurs. Halt the
  44769. + * channel again if it does occur.
  44770. + */
  44771. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44772. + if (hcchar.b.chdis) {
  44773. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  44774. + "hcchar 0x%08x, trying to halt again\n",
  44775. + __func__, hcchar.d32);
  44776. + clear_hc_int(hc_regs, chhltd);
  44777. + hc->halt_pending = 0;
  44778. + halt_channel(hcd, hc, qtd, hc->halt_status);
  44779. + return 0;
  44780. + }
  44781. +
  44782. + return 1;
  44783. +}
  44784. +#endif
  44785. +
  44786. +/**
  44787. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  44788. + * determines the reason the channel halted and proceeds accordingly.
  44789. + */
  44790. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  44791. + dwc_hc_t * hc,
  44792. + dwc_otg_hc_regs_t * hc_regs,
  44793. + dwc_otg_qtd_t * qtd)
  44794. +{
  44795. + int out_nak_enh = 0;
  44796. + hcint_data_t hcint;
  44797. + hcintmsk_data_t hcintmsk;
  44798. + /* For core with OUT NAK enhancement, the flow for high-
  44799. + * speed CONTROL/BULK OUT is handled a little differently.
  44800. + */
  44801. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  44802. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  44803. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  44804. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  44805. + out_nak_enh = 1;
  44806. + }
  44807. + }
  44808. +
  44809. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  44810. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  44811. + && !hcd->core_if->dma_desc_enable)) {
  44812. + /*
  44813. + * Just release the channel. A dequeue can happen on a
  44814. + * transfer timeout. In the case of an AHB Error, the channel
  44815. + * was forced to halt because there's no way to gracefully
  44816. + * recover.
  44817. + */
  44818. + if (hcd->core_if->dma_desc_enable)
  44819. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44820. + hc->halt_status);
  44821. + else
  44822. + release_channel(hcd, hc, qtd, hc->halt_status);
  44823. + return;
  44824. + }
  44825. +
  44826. + /* Read the HCINTn register to determine the cause for the halt. */
  44827. +
  44828. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  44829. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  44830. +
  44831. + if (hcint.b.xfercomp) {
  44832. + /** @todo This is here because of a possible hardware bug. Spec
  44833. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  44834. + * interrupt w/ACK bit set should occur, but I only see the
  44835. + * XFERCOMP bit, even with it masked out. This is a workaround
  44836. + * for that behavior. Should fix this when hardware is fixed.
  44837. + */
  44838. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  44839. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  44840. + }
  44841. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  44842. + } else if (hcint.b.stall) {
  44843. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  44844. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  44845. + if (out_nak_enh) {
  44846. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  44847. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  44848. + qtd->error_count = 0;
  44849. + } else {
  44850. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  44851. + }
  44852. + }
  44853. +
  44854. + /*
  44855. + * Must handle xacterr before nak or ack. Could get a xacterr
  44856. + * at the same time as either of these on a BULK/CONTROL OUT
  44857. + * that started with a PING. The xacterr takes precedence.
  44858. + */
  44859. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  44860. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  44861. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  44862. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  44863. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  44864. + } else if (hcint.b.bblerr) {
  44865. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  44866. + } else if (hcint.b.frmovrun) {
  44867. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  44868. + } else if (hcint.b.datatglerr) {
  44869. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  44870. + } else if (!out_nak_enh) {
  44871. + if (hcint.b.nyet) {
  44872. + /*
  44873. + * Must handle nyet before nak or ack. Could get a nyet at the
  44874. + * same time as either of those on a BULK/CONTROL OUT that
  44875. + * started with a PING. The nyet takes precedence.
  44876. + */
  44877. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  44878. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  44879. + /*
  44880. + * If nak is not masked, it's because a non-split IN transfer
  44881. + * is in an error state. In that case, the nak is handled by
  44882. + * the nak interrupt handler, not here. Handle nak here for
  44883. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  44884. + * rewinding the buffer pointer.
  44885. + */
  44886. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  44887. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  44888. + /*
  44889. + * If ack is not masked, it's because a non-split IN transfer
  44890. + * is in an error state. In that case, the ack is handled by
  44891. + * the ack interrupt handler, not here. Handle ack here for
  44892. + * split transfers. Start splits halt on ACK.
  44893. + */
  44894. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  44895. + } else {
  44896. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  44897. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  44898. + /*
  44899. + * A periodic transfer halted with no other channel
  44900. + * interrupts set. Assume it was halted by the core
  44901. + * because it could not be completed in its scheduled
  44902. + * (micro)frame.
  44903. + */
  44904. +#ifdef DEBUG
  44905. + DWC_PRINTF
  44906. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  44907. + __func__, hc->hc_num);
  44908. +#endif
  44909. + halt_channel(hcd, hc, qtd,
  44910. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  44911. + } else {
  44912. + DWC_ERROR
  44913. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  44914. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  44915. + __func__, hc->hc_num, hcint.d32,
  44916. + DWC_READ_REG32(&hcd->
  44917. + core_if->core_global_regs->
  44918. + gintsts));
  44919. + /* Failthrough: use 3-strikes rule */
  44920. + qtd->error_count++;
  44921. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44922. + update_urb_state_xfer_intr(hc, hc_regs,
  44923. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44924. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44925. + }
  44926. +
  44927. + }
  44928. + } else {
  44929. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  44930. + hcint.d32);
  44931. + /* Failthrough: use 3-strikes rule */
  44932. + qtd->error_count++;
  44933. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44934. + update_urb_state_xfer_intr(hc, hc_regs,
  44935. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44936. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44937. + }
  44938. +}
  44939. +
  44940. +/**
  44941. + * Handles a host channel Channel Halted interrupt.
  44942. + *
  44943. + * In slave mode, this handler is called only when the driver specifically
  44944. + * requests a halt. This occurs during handling other host channel interrupts
  44945. + * (e.g. nak, xacterr, stall, nyet, etc.).
  44946. + *
  44947. + * In DMA mode, this is the interrupt that occurs when the core has finished
  44948. + * processing a transfer on a channel. Other host channel interrupts (except
  44949. + * ahberr) are disabled in DMA mode.
  44950. + */
  44951. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  44952. + dwc_hc_t * hc,
  44953. + dwc_otg_hc_regs_t * hc_regs,
  44954. + dwc_otg_qtd_t * qtd)
  44955. +{
  44956. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44957. + "Channel Halted--\n", hc->hc_num);
  44958. +
  44959. + if (hcd->core_if->dma_enable) {
  44960. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
  44961. + } else {
  44962. +#ifdef DEBUG
  44963. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  44964. + return 1;
  44965. + }
  44966. +#endif
  44967. + release_channel(hcd, hc, qtd, hc->halt_status);
  44968. + }
  44969. +
  44970. + return 1;
  44971. +}
  44972. +
  44973. +
  44974. +/**
  44975. + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
  44976. + * FIQ transfer completion
  44977. + * @hcd: Pointer to dwc_otg_hcd struct
  44978. + * @num: Host channel number
  44979. + *
  44980. + * 1. Un-mangle the status as recorded in each iso_frame_desc status
  44981. + * 2. Copy it from the dwc_otg_urb into the real URB
  44982. + */
  44983. +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  44984. +{
  44985. + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
  44986. + int nr_frames = dwc_urb->packet_count;
  44987. + int i;
  44988. + hcint_data_t frame_hcint;
  44989. +
  44990. + for (i = 0; i < nr_frames; i++) {
  44991. + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
  44992. + if (frame_hcint.b.xfercomp) {
  44993. + dwc_urb->iso_descs[i].status = 0;
  44994. + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
  44995. + } else if (frame_hcint.b.frmovrun) {
  44996. + if (qh->ep_is_in)
  44997. + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
  44998. + else
  44999. + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
  45000. + dwc_urb->error_count++;
  45001. + dwc_urb->iso_descs[i].actual_length = 0;
  45002. + } else if (frame_hcint.b.xacterr) {
  45003. + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
  45004. + dwc_urb->error_count++;
  45005. + dwc_urb->iso_descs[i].actual_length = 0;
  45006. + } else if (frame_hcint.b.bblerr) {
  45007. + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
  45008. + dwc_urb->error_count++;
  45009. + dwc_urb->iso_descs[i].actual_length = 0;
  45010. + } else {
  45011. + /* Something went wrong */
  45012. + dwc_urb->iso_descs[i].status = -1;
  45013. + dwc_urb->iso_descs[i].actual_length = 0;
  45014. + dwc_urb->error_count++;
  45015. + }
  45016. + }
  45017. + qh->sched_frame = dwc_frame_num_inc(qh->sched_frame, qh->interval * (nr_frames - 1));
  45018. +
  45019. + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
  45020. + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
  45021. +}
  45022. +
  45023. +/**
  45024. + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
  45025. + * @hcd: Pointer to dwc_otg_hcd struct
  45026. + * @num: Host channel number
  45027. + *
  45028. + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
  45029. + * Returns total length of data or -1 if the buffers were not used.
  45030. + *
  45031. + */
  45032. +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)
  45033. +{
  45034. + dwc_hc_t *hc = qh->channel;
  45035. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  45036. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  45037. + uint8_t *ptr = NULL;
  45038. + int index = 0, len = 0;
  45039. + int i = 0;
  45040. + if (hc->ep_is_in) {
  45041. + /* Copy data out of the DMA bounce buffers to the URB's buffer.
  45042. + * The align_buf is ignored as this is ignored on FSM enqueue. */
  45043. + ptr = qtd->urb->buf;
  45044. + if (qh->ep_type == UE_ISOCHRONOUS) {
  45045. + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
  45046. + index = qtd->isoc_frame_index;
  45047. + ptr += qtd->urb->iso_descs[index].offset;
  45048. + } else {
  45049. + /* Need to increment by actual_length for interrupt IN */
  45050. + ptr += qtd->urb->actual_length;
  45051. + }
  45052. +
  45053. + for (i = 0; i < st->dma_info.index; i++) {
  45054. + len += st->dma_info.slot_len[i];
  45055. + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
  45056. + ptr += st->dma_info.slot_len[i];
  45057. + }
  45058. + return len;
  45059. + } else {
  45060. + /* OUT endpoints - nothing to do. */
  45061. + return -1;
  45062. + }
  45063. +
  45064. +}
  45065. +/**
  45066. + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
  45067. + * from a channel handled in the FIQ
  45068. + * @hcd: Pointer to dwc_otg_hcd struct
  45069. + * @num: Host channel number
  45070. + *
  45071. + * If a host channel interrupt was received by the IRQ and this was a channel
  45072. + * used by the FIQ, the execution flow for transfer completion is substantially
  45073. + * different from the normal (messy) path. This function and its friends handles
  45074. + * channel cleanup and transaction completion from a FIQ transaction.
  45075. + */
  45076. +void dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
  45077. +{
  45078. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  45079. + dwc_hc_t *hc = hcd->hc_ptr_array[num];
  45080. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  45081. + dwc_otg_qh_t *qh = hc->qh;
  45082. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
  45083. + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
  45084. + int hostchannels = 0;
  45085. + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
  45086. +
  45087. + hostchannels = hcd->available_host_channels;
  45088. + switch (st->fsm) {
  45089. + case FIQ_TEST:
  45090. + break;
  45091. +
  45092. + case FIQ_DEQUEUE_ISSUED:
  45093. + /* hc_halt was called. QTD no longer exists. */
  45094. + /* TODO: for a nonperiodic split transaction, need to issue a
  45095. + * CLEAR_TT_BUFFER hub command if we were in the start-split phase.
  45096. + */
  45097. + release_channel(hcd, hc, NULL, hc->halt_status);
  45098. + break;
  45099. +
  45100. + case FIQ_NP_SPLIT_DONE:
  45101. + /* Nonperiodic transaction complete. */
  45102. + if (!hc->ep_is_in) {
  45103. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  45104. + }
  45105. + if (hcint.b.xfercomp) {
  45106. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  45107. + } else if (hcint.b.nak) {
  45108. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45109. + }
  45110. + break;
  45111. +
  45112. + case FIQ_NP_SPLIT_HS_ABORTED:
  45113. + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
  45114. + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
  45115. + * because there's no guarantee which order a non-periodic split happened in.
  45116. + * We could end up clearing a perfectly good transaction out of the buffer.
  45117. + */
  45118. + if (hcint.b.xacterr) {
  45119. + qtd->error_count += st->nr_errors;
  45120. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45121. + } else if (hcint.b.ahberr) {
  45122. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45123. + } else {
  45124. + local_fiq_disable();
  45125. + BUG();
  45126. + }
  45127. + break;
  45128. +
  45129. + case FIQ_NP_SPLIT_LS_ABORTED:
  45130. + /* A few cases can cause this - either an unknown state on a SSPLIT or
  45131. + * STALL/data toggle error response on a CSPLIT */
  45132. + if (hcint.b.stall) {
  45133. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45134. + } else if (hcint.b.datatglerr) {
  45135. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  45136. + } else if (hcint.b.bblerr) {
  45137. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45138. + } else if (hcint.b.ahberr) {
  45139. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45140. + } else {
  45141. + local_fiq_disable();
  45142. + BUG();
  45143. + }
  45144. + break;
  45145. +
  45146. + case FIQ_PER_SPLIT_DONE:
  45147. + /* Isoc IN or Interrupt IN/OUT */
  45148. +
  45149. + /* Flow control here is different from the normal execution by the driver.
  45150. + * We need to completely ignore most of the driver's method of handling
  45151. + * split transactions and do it ourselves.
  45152. + */
  45153. + if (hc->ep_type == UE_INTERRUPT) {
  45154. + if (hcint.b.nak) {
  45155. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45156. + } else if (hc->ep_is_in) {
  45157. + int len;
  45158. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  45159. + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
  45160. + qtd->urb->actual_length += len;
  45161. + if (qtd->urb->actual_length >= qtd->urb->length) {
  45162. + qtd->urb->status = 0;
  45163. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45164. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45165. + } else {
  45166. + /* Interrupt transfer not complete yet - is it a short read? */
  45167. + if (len < hc->max_packet) {
  45168. + /* Interrupt transaction complete */
  45169. + qtd->urb->status = 0;
  45170. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45171. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45172. + } else {
  45173. + /* Further transactions required */
  45174. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45175. + }
  45176. + }
  45177. + } else {
  45178. + /* Interrupt OUT complete. */
  45179. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  45180. + qtd->urb->actual_length += hc->xfer_len;
  45181. + if (qtd->urb->actual_length >= qtd->urb->length) {
  45182. + qtd->urb->status = 0;
  45183. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45184. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45185. + } else {
  45186. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45187. + }
  45188. + }
  45189. + } else {
  45190. + /* ISOC IN complete. */
  45191. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45192. + int len = 0;
  45193. + /* Record errors, update qtd. */
  45194. + if (st->nr_errors) {
  45195. + frame_desc->actual_length = 0;
  45196. + frame_desc->status = -DWC_E_PROTOCOL;
  45197. + } else {
  45198. + frame_desc->status = 0;
  45199. + /* Unswizzle dma */
  45200. + len = dwc_otg_fiq_unsetup_per_dma(hcd, qh, qtd, num);
  45201. + frame_desc->actual_length = len;
  45202. + }
  45203. + qtd->isoc_frame_index++;
  45204. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45205. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45206. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45207. + } else {
  45208. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45209. + }
  45210. + }
  45211. + break;
  45212. +
  45213. + case FIQ_PER_ISO_OUT_DONE: {
  45214. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45215. + /* Record errors, update qtd. */
  45216. + if (st->nr_errors) {
  45217. + frame_desc->actual_length = 0;
  45218. + frame_desc->status = -DWC_E_PROTOCOL;
  45219. + } else {
  45220. + frame_desc->status = 0;
  45221. + frame_desc->actual_length = frame_desc->length;
  45222. + }
  45223. + qtd->isoc_frame_index++;
  45224. + qtd->isoc_split_offset = 0;
  45225. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45226. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45227. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45228. + } else {
  45229. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45230. + }
  45231. + }
  45232. + break;
  45233. +
  45234. + case FIQ_PER_SPLIT_NYET_ABORTED:
  45235. + /* Doh. lost the data. */
  45236. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  45237. + "- FIQ reported NYET. Data may have been lost.\n",
  45238. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  45239. + if (hc->ep_type == UE_ISOCHRONOUS) {
  45240. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45241. + /* Record errors, update qtd. */
  45242. + frame_desc->actual_length = 0;
  45243. + frame_desc->status = -DWC_E_PROTOCOL;
  45244. + qtd->isoc_frame_index++;
  45245. + qtd->isoc_split_offset = 0;
  45246. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45247. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45248. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45249. + } else {
  45250. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45251. + }
  45252. + } else {
  45253. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45254. + }
  45255. + break;
  45256. +
  45257. + case FIQ_HS_ISOC_DONE:
  45258. + /* The FIQ has performed a whole pile of isochronous transactions.
  45259. + * The status is recorded as the interrupt state should the transaction
  45260. + * fail.
  45261. + */
  45262. + dwc_otg_fiq_unmangle_isoc(hcd, qh, qtd, num);
  45263. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45264. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45265. + break;
  45266. +
  45267. + case FIQ_PER_SPLIT_LS_ABORTED:
  45268. + if (hcint.b.xacterr) {
  45269. + /* Hub has responded with an ERR packet. Device
  45270. + * has been unplugged or the port has been disabled.
  45271. + * TODO: need to issue a reset to the hub port. */
  45272. + qtd->error_count += 3;
  45273. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45274. + } else if (hcint.b.stall) {
  45275. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45276. + } else if (hcint.b.bblerr) {
  45277. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45278. + } else {
  45279. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
  45280. + "- FIQ reported FSM=%d. Data may have been lost.\n",
  45281. + st->fsm, hc->dev_addr, hc->ep_num);
  45282. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45283. + }
  45284. + break;
  45285. +
  45286. + case FIQ_PER_SPLIT_HS_ABORTED:
  45287. + /* Either the SSPLIT phase suffered transaction errors or something
  45288. + * unexpected happened.
  45289. + */
  45290. + qtd->error_count += 3;
  45291. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45292. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45293. + break;
  45294. +
  45295. + case FIQ_PER_SPLIT_TIMEOUT:
  45296. + /* Couldn't complete in the nominated frame */
  45297. + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  45298. + "- FIQ timed out. Data may have been lost.\n",
  45299. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  45300. + if (hc->ep_type == UE_ISOCHRONOUS) {
  45301. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45302. + /* Record errors, update qtd. */
  45303. + frame_desc->actual_length = 0;
  45304. + if (hc->ep_is_in) {
  45305. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  45306. + } else {
  45307. + frame_desc->status = -DWC_E_COMMUNICATION;
  45308. + }
  45309. + qtd->isoc_frame_index++;
  45310. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45311. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45312. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45313. + } else {
  45314. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45315. + }
  45316. + } else {
  45317. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45318. + }
  45319. + break;
  45320. +
  45321. + default:
  45322. + DWC_WARN("Unexpected state received on hc=%d fsm=%d on transfer to device %d ep 0x%x",
  45323. + hc->hc_num, st->fsm, hc->dev_addr, hc->ep_num);
  45324. + qtd->error_count++;
  45325. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45326. + }
  45327. + return;
  45328. +}
  45329. +
  45330. +/** Handles interrupt for a specific Host Channel */
  45331. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  45332. +{
  45333. + int retval = 0;
  45334. + hcint_data_t hcint;
  45335. + hcintmsk_data_t hcintmsk;
  45336. + dwc_hc_t *hc;
  45337. + dwc_otg_hc_regs_t *hc_regs;
  45338. + dwc_otg_qtd_t *qtd;
  45339. +
  45340. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  45341. +
  45342. + hc = dwc_otg_hcd->hc_ptr_array[num];
  45343. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  45344. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  45345. + /* We are responding to a channel disable. Driver
  45346. + * state is cleared - our qtd has gone away.
  45347. + */
  45348. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  45349. + return 1;
  45350. + }
  45351. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  45352. +
  45353. + /*
  45354. + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
  45355. + * Execution path is fundamentally different for the channels after a FIQ has completed
  45356. + * a split transaction.
  45357. + */
  45358. + if (fiq_fsm_enable) {
  45359. + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
  45360. + case FIQ_PASSTHROUGH:
  45361. + break;
  45362. + case FIQ_PASSTHROUGH_ERRORSTATE:
  45363. + /* Hook into the error count */
  45364. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
  45365. + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
  45366. + qtd->error_count = 0;
  45367. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
  45368. + }
  45369. + break;
  45370. + default:
  45371. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  45372. + return 1;
  45373. + }
  45374. + }
  45375. +
  45376. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  45377. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  45378. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  45379. + if (!dwc_otg_hcd->core_if->dma_enable) {
  45380. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  45381. + hcint.b.chhltd = 0;
  45382. + }
  45383. + }
  45384. +
  45385. + if (hcint.b.xfercomp) {
  45386. + retval |=
  45387. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45388. + /*
  45389. + * If NYET occurred at same time as Xfer Complete, the NYET is
  45390. + * handled by the Xfer Complete interrupt handler. Don't want
  45391. + * to call the NYET interrupt handler in this case.
  45392. + */
  45393. + hcint.b.nyet = 0;
  45394. + }
  45395. + if (hcint.b.chhltd) {
  45396. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45397. + }
  45398. + if (hcint.b.ahberr) {
  45399. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45400. + }
  45401. + if (hcint.b.stall) {
  45402. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45403. + }
  45404. + if (hcint.b.nak) {
  45405. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45406. + }
  45407. + if (hcint.b.ack) {
  45408. + if(!hcint.b.chhltd)
  45409. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45410. + }
  45411. + if (hcint.b.nyet) {
  45412. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45413. + }
  45414. + if (hcint.b.xacterr) {
  45415. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45416. + }
  45417. + if (hcint.b.bblerr) {
  45418. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45419. + }
  45420. + if (hcint.b.frmovrun) {
  45421. + retval |=
  45422. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45423. + }
  45424. + if (hcint.b.datatglerr) {
  45425. + retval |=
  45426. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45427. + }
  45428. +
  45429. + return retval;
  45430. +}
  45431. +#endif /* DWC_DEVICE_ONLY */
  45432. --- /dev/null
  45433. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  45434. @@ -0,0 +1,1005 @@
  45435. +
  45436. +/* ==========================================================================
  45437. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  45438. + * $Revision: #20 $
  45439. + * $Date: 2011/10/26 $
  45440. + * $Change: 1872981 $
  45441. + *
  45442. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  45443. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  45444. + * otherwise expressly agreed to in writing between Synopsys and you.
  45445. + *
  45446. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  45447. + * any End User Software License Agreement or Agreement for Licensed Product
  45448. + * with Synopsys or any supplement thereto. You are permitted to use and
  45449. + * redistribute this Software in source and binary forms, with or without
  45450. + * modification, provided that redistributions of source code must retain this
  45451. + * notice. You may not view, use, disclose, copy or distribute this file or
  45452. + * any information contained herein except pursuant to this license grant from
  45453. + * Synopsys. If you do not agree with this notice, including the disclaimer
  45454. + * below, then you are not authorized to use the Software.
  45455. + *
  45456. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  45457. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45458. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45459. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  45460. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  45461. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45462. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45463. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45464. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45465. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  45466. + * DAMAGE.
  45467. + * ========================================================================== */
  45468. +#ifndef DWC_DEVICE_ONLY
  45469. +
  45470. +/**
  45471. + * @file
  45472. + *
  45473. + * This file contains the implementation of the HCD. In Linux, the HCD
  45474. + * implements the hc_driver API.
  45475. + */
  45476. +#include <linux/kernel.h>
  45477. +#include <linux/module.h>
  45478. +#include <linux/moduleparam.h>
  45479. +#include <linux/init.h>
  45480. +#include <linux/device.h>
  45481. +#include <linux/errno.h>
  45482. +#include <linux/list.h>
  45483. +#include <linux/interrupt.h>
  45484. +#include <linux/string.h>
  45485. +#include <linux/dma-mapping.h>
  45486. +#include <linux/version.h>
  45487. +#include <asm/io.h>
  45488. +#include <asm/fiq.h>
  45489. +#include <linux/usb.h>
  45490. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  45491. +#include <../drivers/usb/core/hcd.h>
  45492. +#else
  45493. +#include <linux/usb/hcd.h>
  45494. +#endif
  45495. +#include <asm/bug.h>
  45496. +
  45497. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  45498. +#define USB_URB_EP_LINKING 1
  45499. +#else
  45500. +#define USB_URB_EP_LINKING 0
  45501. +#endif
  45502. +
  45503. +#include "dwc_otg_hcd_if.h"
  45504. +#include "dwc_otg_dbg.h"
  45505. +#include "dwc_otg_driver.h"
  45506. +#include "dwc_otg_hcd.h"
  45507. +
  45508. +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
  45509. +
  45510. +/**
  45511. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  45512. + * qualified with its direction (possible 32 endpoints per device).
  45513. + */
  45514. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  45515. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  45516. +
  45517. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  45518. +
  45519. +extern bool fiq_enable;
  45520. +
  45521. +/** @name Linux HC Driver API Functions */
  45522. +/** @{ */
  45523. +/* manage i/o requests, device state */
  45524. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  45525. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45526. + struct usb_host_endpoint *ep,
  45527. +#endif
  45528. + struct urb *urb, gfp_t mem_flags);
  45529. +
  45530. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  45531. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45532. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  45533. +#endif
  45534. +#else /* kernels at or post 2.6.30 */
  45535. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  45536. + struct urb *urb, int status);
  45537. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  45538. +
  45539. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  45540. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  45541. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  45542. +#endif
  45543. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  45544. +extern int hcd_start(struct usb_hcd *hcd);
  45545. +extern void hcd_stop(struct usb_hcd *hcd);
  45546. +static int get_frame_number(struct usb_hcd *hcd);
  45547. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  45548. +extern int hub_control(struct usb_hcd *hcd,
  45549. + u16 typeReq,
  45550. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  45551. +
  45552. +struct wrapper_priv_data {
  45553. + dwc_otg_hcd_t *dwc_otg_hcd;
  45554. +};
  45555. +
  45556. +/** @} */
  45557. +
  45558. +static struct hc_driver dwc_otg_hc_driver = {
  45559. +
  45560. + .description = dwc_otg_hcd_name,
  45561. + .product_desc = "DWC OTG Controller",
  45562. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  45563. +
  45564. + .irq = dwc_otg_hcd_irq,
  45565. +
  45566. + .flags = HCD_MEMORY | HCD_USB2,
  45567. +
  45568. + //.reset =
  45569. + .start = hcd_start,
  45570. + //.suspend =
  45571. + //.resume =
  45572. + .stop = hcd_stop,
  45573. +
  45574. + .urb_enqueue = dwc_otg_urb_enqueue,
  45575. + .urb_dequeue = dwc_otg_urb_dequeue,
  45576. + .endpoint_disable = endpoint_disable,
  45577. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  45578. + .endpoint_reset = endpoint_reset,
  45579. +#endif
  45580. + .get_frame_number = get_frame_number,
  45581. +
  45582. + .hub_status_data = hub_status_data,
  45583. + .hub_control = hub_control,
  45584. + //.bus_suspend =
  45585. + //.bus_resume =
  45586. +};
  45587. +
  45588. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  45589. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  45590. +{
  45591. + struct wrapper_priv_data *p;
  45592. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  45593. + return p->dwc_otg_hcd;
  45594. +}
  45595. +
  45596. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  45597. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  45598. +{
  45599. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  45600. +}
  45601. +
  45602. +/** Gets the usb_host_endpoint associated with an URB. */
  45603. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  45604. +{
  45605. + struct usb_device *dev = urb->dev;
  45606. + int ep_num = usb_pipeendpoint(urb->pipe);
  45607. +
  45608. + if (usb_pipein(urb->pipe))
  45609. + return dev->ep_in[ep_num];
  45610. + else
  45611. + return dev->ep_out[ep_num];
  45612. +}
  45613. +
  45614. +static int _disconnect(dwc_otg_hcd_t * hcd)
  45615. +{
  45616. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45617. +
  45618. + usb_hcd->self.is_b_host = 0;
  45619. + return 0;
  45620. +}
  45621. +
  45622. +static int _start(dwc_otg_hcd_t * hcd)
  45623. +{
  45624. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45625. +
  45626. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  45627. + hcd_start(usb_hcd);
  45628. +
  45629. + return 0;
  45630. +}
  45631. +
  45632. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  45633. + uint32_t * port_addr)
  45634. +{
  45635. + struct urb *urb = (struct urb *)urb_handle;
  45636. + struct usb_bus *bus;
  45637. +#if 1 //GRAYG - temporary
  45638. + if (NULL == urb_handle)
  45639. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  45640. + if (NULL == urb->dev)
  45641. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  45642. + if (NULL == port_addr)
  45643. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  45644. +#endif
  45645. + if (urb->dev->tt) {
  45646. + if (NULL == urb->dev->tt->hub) {
  45647. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  45648. + __func__); //GRAYG
  45649. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  45650. + *hub_addr = 0; //GRAYG
  45651. + // we probably shouldn't have a transaction translator if
  45652. + // there's no associated hub?
  45653. + } else {
  45654. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  45655. + if (urb->dev->tt->hub == bus->root_hub)
  45656. + *hub_addr = 0;
  45657. + else
  45658. + *hub_addr = urb->dev->tt->hub->devnum;
  45659. + }
  45660. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  45661. + } else {
  45662. + *hub_addr = 0;
  45663. + *port_addr = urb->dev->ttport;
  45664. + }
  45665. + return 0;
  45666. +}
  45667. +
  45668. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  45669. +{
  45670. + struct urb *urb = (struct urb *)urb_handle;
  45671. + return urb->dev->speed;
  45672. +}
  45673. +
  45674. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  45675. +{
  45676. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45677. + return usb_hcd->self.b_hnp_enable;
  45678. +}
  45679. +
  45680. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  45681. + struct urb *urb)
  45682. +{
  45683. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  45684. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45685. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  45686. + } else {
  45687. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  45688. + }
  45689. +}
  45690. +
  45691. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  45692. + struct urb *urb)
  45693. +{
  45694. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  45695. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45696. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  45697. + } else {
  45698. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  45699. + }
  45700. +}
  45701. +
  45702. +/**
  45703. + * Sets the final status of an URB and returns it to the device driver. Any
  45704. + * required cleanup of the URB is performed. The HCD lock should be held on
  45705. + * entry.
  45706. + */
  45707. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  45708. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  45709. +{
  45710. + struct urb *urb = (struct urb *)urb_handle;
  45711. + urb_tq_entry_t *new_entry;
  45712. + int rc = 0;
  45713. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  45714. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  45715. + __func__, urb, usb_pipedevice(urb->pipe),
  45716. + usb_pipeendpoint(urb->pipe),
  45717. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  45718. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45719. + int i;
  45720. + for (i = 0; i < urb->number_of_packets; i++) {
  45721. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  45722. + i, urb->iso_frame_desc[i].status);
  45723. + }
  45724. + }
  45725. + }
  45726. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  45727. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  45728. + /* Convert status value. */
  45729. + switch (status) {
  45730. + case -DWC_E_PROTOCOL:
  45731. + status = -EPROTO;
  45732. + break;
  45733. + case -DWC_E_IN_PROGRESS:
  45734. + status = -EINPROGRESS;
  45735. + break;
  45736. + case -DWC_E_PIPE:
  45737. + status = -EPIPE;
  45738. + break;
  45739. + case -DWC_E_IO:
  45740. + status = -EIO;
  45741. + break;
  45742. + case -DWC_E_TIMEOUT:
  45743. + status = -ETIMEDOUT;
  45744. + break;
  45745. + case -DWC_E_OVERFLOW:
  45746. + status = -EOVERFLOW;
  45747. + break;
  45748. + case -DWC_E_SHUTDOWN:
  45749. + status = -ESHUTDOWN;
  45750. + break;
  45751. + default:
  45752. + if (status) {
  45753. + DWC_PRINTF("Uknown urb status %d\n", status);
  45754. +
  45755. + }
  45756. + }
  45757. +
  45758. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45759. + int i;
  45760. +
  45761. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  45762. + for (i = 0; i < urb->number_of_packets; ++i) {
  45763. + urb->iso_frame_desc[i].actual_length =
  45764. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  45765. + (dwc_otg_urb, i);
  45766. + urb->iso_frame_desc[i].status =
  45767. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  45768. + }
  45769. + }
  45770. +
  45771. + urb->status = status;
  45772. + urb->hcpriv = NULL;
  45773. + if (!status) {
  45774. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  45775. + (urb->actual_length < urb->transfer_buffer_length)) {
  45776. + urb->status = -EREMOTEIO;
  45777. + }
  45778. + }
  45779. +
  45780. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  45781. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  45782. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  45783. + if (ep) {
  45784. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  45785. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  45786. + ep->hcpriv),
  45787. + urb);
  45788. + }
  45789. + }
  45790. + DWC_FREE(dwc_otg_urb);
  45791. + if (!new_entry) {
  45792. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  45793. + urb->status = -EPROTO;
  45794. + /* don't schedule the tasklet -
  45795. + * directly return the packet here with error. */
  45796. +#if USB_URB_EP_LINKING
  45797. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  45798. +#endif
  45799. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45800. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  45801. +#else
  45802. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  45803. +#endif
  45804. + } else {
  45805. + new_entry->urb = urb;
  45806. +#if USB_URB_EP_LINKING
  45807. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  45808. + if(0 == rc) {
  45809. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  45810. + }
  45811. +#endif
  45812. + if(0 == rc) {
  45813. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  45814. + urb_tq_entries);
  45815. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  45816. + }
  45817. + }
  45818. + return 0;
  45819. +}
  45820. +
  45821. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  45822. + .start = _start,
  45823. + .disconnect = _disconnect,
  45824. + .hub_info = _hub_info,
  45825. + .speed = _speed,
  45826. + .complete = _complete,
  45827. + .get_b_hnp_enable = _get_b_hnp_enable,
  45828. +};
  45829. +
  45830. +static struct fiq_handler fh = {
  45831. + .name = "usb_fiq",
  45832. +};
  45833. +
  45834. +static void hcd_init_fiq(void *cookie)
  45835. +{
  45836. + dwc_otg_device_t *otg_dev = cookie;
  45837. + dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
  45838. + struct pt_regs regs;
  45839. + int irq;
  45840. +
  45841. + if (claim_fiq(&fh)) {
  45842. + DWC_ERROR("Can't claim FIQ");
  45843. + BUG();
  45844. + }
  45845. + DWC_WARN("FIQ on core %d at 0x%08x",
  45846. + smp_processor_id(),
  45847. + (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
  45848. + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
  45849. + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
  45850. + memset(&regs,0,sizeof(regs));
  45851. +
  45852. + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
  45853. + if (fiq_fsm_enable) {
  45854. + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
  45855. + //regs.ARM_r10 = dwc_otg_hcd->dma;
  45856. + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
  45857. + } else {
  45858. + regs.ARM_fp = (long) dwc_otg_fiq_nop;
  45859. + }
  45860. +
  45861. + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
  45862. +
  45863. +// __show_regs(&regs);
  45864. + set_fiq_regs(&regs);
  45865. +
  45866. + //Set the mphi periph to the required registers
  45867. + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
  45868. + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  45869. + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  45870. + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  45871. + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  45872. + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
  45873. + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
  45874. + //Enable mphi peripheral
  45875. + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
  45876. +#ifdef DEBUG
  45877. + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
  45878. + DWC_WARN("MPHI periph has been enabled");
  45879. + else
  45880. + DWC_WARN("MPHI periph has NOT been enabled");
  45881. +#endif
  45882. + // Enable FIQ interrupt from USB peripheral
  45883. +#ifdef CONFIG_MULTI_IRQ_HANDLER
  45884. + irq = platform_get_irq(otg_dev->os_dep.platformdev, 1);
  45885. +#else
  45886. + irq = INTERRUPT_VC_USB;
  45887. +#endif
  45888. + if (irq < 0) {
  45889. + DWC_ERROR("Can't get FIQ irq");
  45890. + return;
  45891. + }
  45892. + enable_fiq(irq);
  45893. + local_fiq_enable();
  45894. +}
  45895. +
  45896. +/**
  45897. + * Initializes the HCD. This function allocates memory for and initializes the
  45898. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  45899. + * USB bus with the core and calls the hc_driver->start() function. It returns
  45900. + * a negative error on failure.
  45901. + */
  45902. +int hcd_init(dwc_bus_dev_t *_dev)
  45903. +{
  45904. + struct usb_hcd *hcd = NULL;
  45905. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  45906. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  45907. + int retval = 0;
  45908. + u64 dmamask;
  45909. +
  45910. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  45911. +
  45912. + /* Set device flags indicating whether the HCD supports DMA. */
  45913. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  45914. + dmamask = DMA_BIT_MASK(32);
  45915. + else
  45916. + dmamask = 0;
  45917. +
  45918. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  45919. + dma_set_mask(&_dev->dev, dmamask);
  45920. + dma_set_coherent_mask(&_dev->dev, dmamask);
  45921. +#elif defined(PCI_INTERFACE)
  45922. + pci_set_dma_mask(_dev, dmamask);
  45923. + pci_set_consistent_dma_mask(_dev, dmamask);
  45924. +#endif
  45925. +
  45926. + /*
  45927. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  45928. + * Initialize the base HCD.
  45929. + */
  45930. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  45931. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  45932. +#else
  45933. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  45934. + hcd->has_tt = 1;
  45935. +// hcd->uses_new_polling = 1;
  45936. +// hcd->poll_rh = 0;
  45937. +#endif
  45938. + if (!hcd) {
  45939. + retval = -ENOMEM;
  45940. + goto error1;
  45941. + }
  45942. +
  45943. + hcd->regs = otg_dev->os_dep.base;
  45944. +
  45945. +
  45946. + /* Initialize the DWC OTG HCD. */
  45947. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  45948. + if (!dwc_otg_hcd) {
  45949. + goto error2;
  45950. + }
  45951. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  45952. + dwc_otg_hcd;
  45953. + otg_dev->hcd = dwc_otg_hcd;
  45954. +
  45955. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  45956. + goto error2;
  45957. + }
  45958. +
  45959. + if (fiq_enable) {
  45960. + if (num_online_cpus() > 1) {
  45961. + /* bcm2709: can run the FIQ on a separate core to IRQs */
  45962. + smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
  45963. + } else {
  45964. + smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
  45965. + }
  45966. + }
  45967. +
  45968. + otg_dev->hcd->otg_dev = otg_dev;
  45969. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  45970. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  45971. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  45972. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  45973. +#endif
  45974. + /* Don't support SG list at this point */
  45975. + hcd->self.sg_tablesize = 0;
  45976. +#endif
  45977. + /*
  45978. + * Finish generic HCD initialization and start the HCD. This function
  45979. + * allocates the DMA buffer pool, registers the USB bus, requests the
  45980. + * IRQ line, and calls hcd_start method.
  45981. + */
  45982. +#ifdef PLATFORM_INTERFACE
  45983. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED);
  45984. +#else
  45985. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED);
  45986. +#endif
  45987. + if (retval < 0) {
  45988. + goto error2;
  45989. + }
  45990. +
  45991. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  45992. + return 0;
  45993. +
  45994. +error2:
  45995. + usb_put_hcd(hcd);
  45996. +error1:
  45997. + return retval;
  45998. +}
  45999. +
  46000. +/**
  46001. + * Removes the HCD.
  46002. + * Frees memory and resources associated with the HCD and deregisters the bus.
  46003. + */
  46004. +void hcd_remove(dwc_bus_dev_t *_dev)
  46005. +{
  46006. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  46007. + dwc_otg_hcd_t *dwc_otg_hcd;
  46008. + struct usb_hcd *hcd;
  46009. +
  46010. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  46011. +
  46012. + if (!otg_dev) {
  46013. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  46014. + return;
  46015. + }
  46016. +
  46017. + dwc_otg_hcd = otg_dev->hcd;
  46018. +
  46019. + if (!dwc_otg_hcd) {
  46020. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  46021. + return;
  46022. + }
  46023. +
  46024. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  46025. +
  46026. + if (!hcd) {
  46027. + DWC_DEBUGPL(DBG_ANY,
  46028. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  46029. + __func__);
  46030. + return;
  46031. + }
  46032. + usb_remove_hcd(hcd);
  46033. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  46034. + dwc_otg_hcd_remove(dwc_otg_hcd);
  46035. + usb_put_hcd(hcd);
  46036. +}
  46037. +
  46038. +/* =========================================================================
  46039. + * Linux HC Driver Functions
  46040. + * ========================================================================= */
  46041. +
  46042. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  46043. + * mode operation. Activates the root port. Returns 0 on success and a negative
  46044. + * error code on failure. */
  46045. +int hcd_start(struct usb_hcd *hcd)
  46046. +{
  46047. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46048. + struct usb_bus *bus;
  46049. +
  46050. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  46051. + bus = hcd_to_bus(hcd);
  46052. +
  46053. + hcd->state = HC_STATE_RUNNING;
  46054. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  46055. + return 0;
  46056. + }
  46057. +
  46058. + /* Initialize and connect root hub if one is not already attached */
  46059. + if (bus->root_hub) {
  46060. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  46061. + /* Inform the HUB driver to resume. */
  46062. + usb_hcd_resume_root_hub(hcd);
  46063. + }
  46064. +
  46065. + return 0;
  46066. +}
  46067. +
  46068. +/**
  46069. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  46070. + * stopped.
  46071. + */
  46072. +void hcd_stop(struct usb_hcd *hcd)
  46073. +{
  46074. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46075. +
  46076. + dwc_otg_hcd_stop(dwc_otg_hcd);
  46077. +}
  46078. +
  46079. +/** Returns the current frame number. */
  46080. +static int get_frame_number(struct usb_hcd *hcd)
  46081. +{
  46082. + hprt0_data_t hprt0;
  46083. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46084. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  46085. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  46086. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
  46087. + else
  46088. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  46089. +}
  46090. +
  46091. +#ifdef DEBUG
  46092. +static void dump_urb_info(struct urb *urb, char *fn_name)
  46093. +{
  46094. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  46095. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  46096. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  46097. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  46098. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  46099. + char *pipetype;
  46100. + switch (usb_pipetype(urb->pipe)) {
  46101. +case PIPE_CONTROL:
  46102. +pipetype = "CONTROL"; break; case PIPE_BULK:
  46103. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  46104. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  46105. +pipetype = "ISOCHRONOUS"; break; default:
  46106. + pipetype = "UNKNOWN"; break;};
  46107. + pipetype;}
  46108. + )) ;
  46109. + DWC_PRINTF(" Speed: %s\n", ( {
  46110. + char *speed; switch (urb->dev->speed) {
  46111. +case USB_SPEED_HIGH:
  46112. +speed = "HIGH"; break; case USB_SPEED_FULL:
  46113. +speed = "FULL"; break; case USB_SPEED_LOW:
  46114. +speed = "LOW"; break; default:
  46115. + speed = "UNKNOWN"; break;};
  46116. + speed;}
  46117. + )) ;
  46118. + DWC_PRINTF(" Max packet size: %d\n",
  46119. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  46120. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  46121. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  46122. + urb->transfer_buffer, (void *)urb->transfer_dma);
  46123. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  46124. + urb->setup_packet, (void *)urb->setup_dma);
  46125. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  46126. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  46127. + int i;
  46128. + for (i = 0; i < urb->number_of_packets; i++) {
  46129. + DWC_PRINTF(" ISO Desc %d:\n", i);
  46130. + DWC_PRINTF(" offset: %d, length %d\n",
  46131. + urb->iso_frame_desc[i].offset,
  46132. + urb->iso_frame_desc[i].length);
  46133. + }
  46134. + }
  46135. +}
  46136. +#endif
  46137. +
  46138. +/** Starts processing a USB transfer request specified by a USB Request Block
  46139. + * (URB). mem_flags indicates the type of memory allocation to use while
  46140. + * processing this URB. */
  46141. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  46142. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46143. + struct usb_host_endpoint *ep,
  46144. +#endif
  46145. + struct urb *urb, gfp_t mem_flags)
  46146. +{
  46147. + int retval = 0;
  46148. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  46149. + struct usb_host_endpoint *ep = urb->ep;
  46150. +#endif
  46151. + dwc_irqflags_t irqflags;
  46152. + void **ref_ep_hcpriv = &ep->hcpriv;
  46153. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46154. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  46155. + int i;
  46156. + int alloc_bandwidth = 0;
  46157. + uint8_t ep_type = 0;
  46158. + uint32_t flags = 0;
  46159. + void *buf;
  46160. +
  46161. +#ifdef DEBUG
  46162. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46163. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  46164. + }
  46165. +#endif
  46166. +
  46167. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  46168. + return -EINVAL;
  46169. +
  46170. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  46171. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  46172. + if (!dwc_otg_hcd_is_bandwidth_allocated
  46173. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  46174. + alloc_bandwidth = 1;
  46175. + }
  46176. + }
  46177. +
  46178. + switch (usb_pipetype(urb->pipe)) {
  46179. + case PIPE_CONTROL:
  46180. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  46181. + break;
  46182. + case PIPE_ISOCHRONOUS:
  46183. + ep_type = USB_ENDPOINT_XFER_ISOC;
  46184. + break;
  46185. + case PIPE_BULK:
  46186. + ep_type = USB_ENDPOINT_XFER_BULK;
  46187. + break;
  46188. + case PIPE_INTERRUPT:
  46189. + ep_type = USB_ENDPOINT_XFER_INT;
  46190. + break;
  46191. + default:
  46192. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  46193. + }
  46194. +
  46195. + /* # of packets is often 0 - do we really need to call this then? */
  46196. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  46197. + urb->number_of_packets,
  46198. + mem_flags == GFP_ATOMIC ? 1 : 0);
  46199. +
  46200. + if(dwc_otg_urb == NULL)
  46201. + return -ENOMEM;
  46202. +
  46203. + if (!dwc_otg_urb && urb->number_of_packets)
  46204. + return -ENOMEM;
  46205. +
  46206. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  46207. + usb_pipeendpoint(urb->pipe), ep_type,
  46208. + usb_pipein(urb->pipe),
  46209. + usb_maxpacket(urb->dev, urb->pipe,
  46210. + !(usb_pipein(urb->pipe))));
  46211. +
  46212. + buf = urb->transfer_buffer;
  46213. + if (hcd->self.uses_dma && !buf && urb->transfer_buffer_length) {
  46214. + /*
  46215. + * Calculate virtual address from physical address,
  46216. + * because some class driver may not fill transfer_buffer.
  46217. + * In Buffer DMA mode virual address is used,
  46218. + * when handling non DWORD aligned buffers.
  46219. + */
  46220. + buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
  46221. + dev_warn_once(&urb->dev->dev,
  46222. + "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
  46223. + &urb->transfer_dma, buf);
  46224. + }
  46225. +
  46226. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  46227. + flags |= URB_GIVEBACK_ASAP;
  46228. + if (urb->transfer_flags & URB_ZERO_PACKET)
  46229. + flags |= URB_SEND_ZERO_PACKET;
  46230. +
  46231. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  46232. + urb->transfer_dma,
  46233. + urb->transfer_buffer_length,
  46234. + urb->setup_packet,
  46235. + urb->setup_dma, flags, urb->interval);
  46236. +
  46237. + for (i = 0; i < urb->number_of_packets; ++i) {
  46238. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  46239. + urb->
  46240. + iso_frame_desc[i].offset,
  46241. + urb->
  46242. + iso_frame_desc[i].length);
  46243. + }
  46244. +
  46245. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  46246. + urb->hcpriv = dwc_otg_urb;
  46247. +#if USB_URB_EP_LINKING
  46248. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  46249. + if (0 == retval)
  46250. +#endif
  46251. + {
  46252. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  46253. + /*(dwc_otg_qh_t **)*/
  46254. + ref_ep_hcpriv, 1);
  46255. + if (0 == retval) {
  46256. + if (alloc_bandwidth) {
  46257. + allocate_bus_bandwidth(hcd,
  46258. + dwc_otg_hcd_get_ep_bandwidth(
  46259. + dwc_otg_hcd, *ref_ep_hcpriv),
  46260. + urb);
  46261. + }
  46262. + } else {
  46263. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  46264. +#if USB_URB_EP_LINKING
  46265. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  46266. +#endif
  46267. + DWC_FREE(dwc_otg_urb);
  46268. + urb->hcpriv = NULL;
  46269. + if (retval == -DWC_E_NO_DEVICE)
  46270. + retval = -ENODEV;
  46271. + }
  46272. + }
  46273. +#if USB_URB_EP_LINKING
  46274. + else
  46275. + {
  46276. + DWC_FREE(dwc_otg_urb);
  46277. + urb->hcpriv = NULL;
  46278. + }
  46279. +#endif
  46280. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  46281. + return retval;
  46282. +}
  46283. +
  46284. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  46285. + * success. */
  46286. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46287. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  46288. +#else
  46289. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  46290. +#endif
  46291. +{
  46292. + dwc_irqflags_t flags;
  46293. + dwc_otg_hcd_t *dwc_otg_hcd;
  46294. + int rc;
  46295. +
  46296. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  46297. +
  46298. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46299. +
  46300. +#ifdef DEBUG
  46301. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46302. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  46303. + }
  46304. +#endif
  46305. +
  46306. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  46307. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  46308. + if (0 == rc) {
  46309. + if(urb->hcpriv != NULL) {
  46310. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  46311. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  46312. +
  46313. + DWC_FREE(urb->hcpriv);
  46314. + urb->hcpriv = NULL;
  46315. + }
  46316. + }
  46317. +
  46318. + if (0 == rc) {
  46319. + /* Higher layer software sets URB status. */
  46320. +#if USB_URB_EP_LINKING
  46321. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  46322. +#endif
  46323. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46324. +
  46325. +
  46326. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46327. + usb_hcd_giveback_urb(hcd, urb);
  46328. +#else
  46329. + usb_hcd_giveback_urb(hcd, urb, status);
  46330. +#endif
  46331. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46332. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  46333. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  46334. + }
  46335. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  46336. + } else {
  46337. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46338. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  46339. + rc);
  46340. + }
  46341. +
  46342. + return rc;
  46343. +}
  46344. +
  46345. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  46346. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  46347. + * must already be dequeued. */
  46348. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  46349. +{
  46350. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46351. +
  46352. + DWC_DEBUGPL(DBG_HCD,
  46353. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  46354. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  46355. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  46356. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  46357. + ep->hcpriv = NULL;
  46358. +}
  46359. +
  46360. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  46361. +/* Resets endpoint specific parameter values, in current version used to reset
  46362. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  46363. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  46364. +{
  46365. + dwc_irqflags_t flags;
  46366. + struct usb_device *udev = NULL;
  46367. + int epnum = usb_endpoint_num(&ep->desc);
  46368. + int is_out = usb_endpoint_dir_out(&ep->desc);
  46369. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  46370. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46371. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  46372. +
  46373. + if (dev)
  46374. + udev = to_usb_device(dev);
  46375. + else
  46376. + return;
  46377. +
  46378. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  46379. +
  46380. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  46381. + usb_settoggle(udev, epnum, is_out, 0);
  46382. + if (is_control)
  46383. + usb_settoggle(udev, epnum, !is_out, 0);
  46384. +
  46385. + if (ep->hcpriv) {
  46386. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  46387. + }
  46388. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46389. +}
  46390. +#endif
  46391. +
  46392. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  46393. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  46394. + * interrupt.
  46395. + *
  46396. + * This function is called by the USB core when an interrupt occurs */
  46397. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  46398. +{
  46399. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46400. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  46401. + if (retval != 0) {
  46402. + S3C2410X_CLEAR_EINTPEND();
  46403. + }
  46404. + return IRQ_RETVAL(retval);
  46405. +}
  46406. +
  46407. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  46408. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  46409. + * is the status change indicator for the single root port. Returns 1 if either
  46410. + * change indicator is 1, otherwise returns 0. */
  46411. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  46412. +{
  46413. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46414. +
  46415. + buf[0] = 0;
  46416. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  46417. +
  46418. + return (buf[0] != 0);
  46419. +}
  46420. +
  46421. +/** Handles hub class-specific requests. */
  46422. +int hub_control(struct usb_hcd *hcd,
  46423. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  46424. +{
  46425. + int retval;
  46426. +
  46427. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  46428. + typeReq, wValue, wIndex, buf, wLength);
  46429. +
  46430. + switch (retval) {
  46431. + case -DWC_E_INVALID:
  46432. + retval = -EINVAL;
  46433. + break;
  46434. + }
  46435. +
  46436. + return retval;
  46437. +}
  46438. +
  46439. +#endif /* DWC_DEVICE_ONLY */
  46440. --- /dev/null
  46441. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  46442. @@ -0,0 +1,957 @@
  46443. +/* ==========================================================================
  46444. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  46445. + * $Revision: #44 $
  46446. + * $Date: 2011/10/26 $
  46447. + * $Change: 1873028 $
  46448. + *
  46449. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  46450. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  46451. + * otherwise expressly agreed to in writing between Synopsys and you.
  46452. + *
  46453. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  46454. + * any End User Software License Agreement or Agreement for Licensed Product
  46455. + * with Synopsys or any supplement thereto. You are permitted to use and
  46456. + * redistribute this Software in source and binary forms, with or without
  46457. + * modification, provided that redistributions of source code must retain this
  46458. + * notice. You may not view, use, disclose, copy or distribute this file or
  46459. + * any information contained herein except pursuant to this license grant from
  46460. + * Synopsys. If you do not agree with this notice, including the disclaimer
  46461. + * below, then you are not authorized to use the Software.
  46462. + *
  46463. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  46464. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46465. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46466. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  46467. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46468. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  46469. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46470. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46471. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46472. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46473. + * DAMAGE.
  46474. + * ========================================================================== */
  46475. +#ifndef DWC_DEVICE_ONLY
  46476. +
  46477. +/**
  46478. + * @file
  46479. + *
  46480. + * This file contains the functions to manage Queue Heads and Queue
  46481. + * Transfer Descriptors.
  46482. + */
  46483. +
  46484. +#include "dwc_otg_hcd.h"
  46485. +#include "dwc_otg_regs.h"
  46486. +
  46487. +extern bool microframe_schedule;
  46488. +
  46489. +/**
  46490. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  46491. + * removed from a list. QTD list should already be empty if called from URB
  46492. + * Dequeue.
  46493. + *
  46494. + * @param hcd HCD instance.
  46495. + * @param qh The QH to free.
  46496. + */
  46497. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  46498. +{
  46499. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  46500. + dwc_irqflags_t flags;
  46501. +
  46502. + /* Free each QTD in the QTD list */
  46503. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  46504. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  46505. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  46506. + dwc_otg_hcd_qtd_free(qtd);
  46507. + }
  46508. +
  46509. + if (hcd->core_if->dma_desc_enable) {
  46510. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  46511. + } else if (qh->dw_align_buf) {
  46512. + uint32_t buf_size;
  46513. + if (qh->ep_type == UE_ISOCHRONOUS) {
  46514. + buf_size = 4096;
  46515. + } else {
  46516. + buf_size = hcd->core_if->core_params->max_transfer_size;
  46517. + }
  46518. + DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
  46519. + }
  46520. +
  46521. + DWC_FREE(qh);
  46522. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  46523. + return;
  46524. +}
  46525. +
  46526. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  46527. +#define HS_HOST_DELAY 5 /* nanoseconds */
  46528. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  46529. +#define HUB_LS_SETUP 333 /* nanoseconds */
  46530. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  46531. + /* convert & round nanoseconds to microseconds */
  46532. +
  46533. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  46534. +{
  46535. + unsigned long retval;
  46536. +
  46537. + switch (speed) {
  46538. + case USB_SPEED_HIGH:
  46539. + if (is_isoc) {
  46540. + retval =
  46541. + ((38 * 8 * 2083) +
  46542. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  46543. + HS_HOST_DELAY;
  46544. + } else {
  46545. + retval =
  46546. + ((55 * 8 * 2083) +
  46547. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  46548. + HS_HOST_DELAY;
  46549. + }
  46550. + break;
  46551. + case USB_SPEED_FULL:
  46552. + if (is_isoc) {
  46553. + retval =
  46554. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  46555. + if (is_in) {
  46556. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  46557. + } else {
  46558. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  46559. + }
  46560. + } else {
  46561. + retval =
  46562. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  46563. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  46564. + }
  46565. + break;
  46566. + case USB_SPEED_LOW:
  46567. + if (is_in) {
  46568. + retval =
  46569. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  46570. + 1000;
  46571. + retval =
  46572. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  46573. + retval;
  46574. + } else {
  46575. + retval =
  46576. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  46577. + 1000;
  46578. + retval =
  46579. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  46580. + retval;
  46581. + }
  46582. + break;
  46583. + default:
  46584. + DWC_WARN("Unknown device speed\n");
  46585. + retval = -1;
  46586. + }
  46587. +
  46588. + return NS_TO_US(retval);
  46589. +}
  46590. +
  46591. +/**
  46592. + * Initializes a QH structure.
  46593. + *
  46594. + * @param hcd The HCD state structure for the DWC OTG controller.
  46595. + * @param qh The QH to init.
  46596. + * @param urb Holds the information about the device/endpoint that we need
  46597. + * to initialize the QH.
  46598. + */
  46599. +#define SCHEDULE_SLOP 10
  46600. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  46601. +{
  46602. + char *speed, *type;
  46603. + int dev_speed;
  46604. + uint32_t hub_addr, hub_port;
  46605. +
  46606. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  46607. +
  46608. + /* Initialize QH */
  46609. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  46610. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  46611. +
  46612. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  46613. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  46614. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  46615. + DWC_LIST_INIT(&qh->qh_list_entry);
  46616. + qh->channel = NULL;
  46617. +
  46618. + /* FS/LS Enpoint on HS Hub
  46619. + * NOT virtual root hub */
  46620. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  46621. +
  46622. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  46623. + qh->do_split = 0;
  46624. + if (microframe_schedule)
  46625. + qh->speed = dev_speed;
  46626. +
  46627. + qh->nak_frame = 0xffff;
  46628. +
  46629. + if (((dev_speed == USB_SPEED_LOW) ||
  46630. + (dev_speed == USB_SPEED_FULL)) &&
  46631. + (hub_addr != 0 && hub_addr != 1)) {
  46632. + DWC_DEBUGPL(DBG_HCD,
  46633. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  46634. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  46635. + hub_port);
  46636. + qh->do_split = 1;
  46637. + qh->skip_count = 0;
  46638. + }
  46639. +
  46640. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  46641. + /* Compute scheduling parameters once and save them. */
  46642. + hprt0_data_t hprt;
  46643. +
  46644. + /** @todo Account for split transfers in the bus time. */
  46645. + int bytecount =
  46646. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  46647. +
  46648. + qh->usecs =
  46649. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  46650. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  46651. + bytecount);
  46652. + /* Start in a slightly future (micro)frame. */
  46653. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  46654. + SCHEDULE_SLOP);
  46655. + qh->interval = urb->interval;
  46656. +
  46657. +#if 0
  46658. + /* Increase interrupt polling rate for debugging. */
  46659. + if (qh->ep_type == UE_INTERRUPT) {
  46660. + qh->interval = 8;
  46661. + }
  46662. +#endif
  46663. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  46664. + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
  46665. + ((dev_speed == USB_SPEED_LOW) ||
  46666. + (dev_speed == USB_SPEED_FULL))) {
  46667. + qh->interval *= 8;
  46668. + qh->sched_frame |= 0x7;
  46669. + qh->start_split_frame = qh->sched_frame;
  46670. + }
  46671. +
  46672. + }
  46673. +
  46674. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  46675. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  46676. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  46677. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  46678. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  46679. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  46680. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  46681. + switch (dev_speed) {
  46682. + case USB_SPEED_LOW:
  46683. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  46684. + speed = "low";
  46685. + break;
  46686. + case USB_SPEED_FULL:
  46687. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  46688. + speed = "full";
  46689. + break;
  46690. + case USB_SPEED_HIGH:
  46691. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  46692. + speed = "high";
  46693. + break;
  46694. + default:
  46695. + speed = "?";
  46696. + break;
  46697. + }
  46698. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  46699. +
  46700. + switch (qh->ep_type) {
  46701. + case UE_ISOCHRONOUS:
  46702. + type = "isochronous";
  46703. + break;
  46704. + case UE_INTERRUPT:
  46705. + type = "interrupt";
  46706. + break;
  46707. + case UE_CONTROL:
  46708. + type = "control";
  46709. + break;
  46710. + case UE_BULK:
  46711. + type = "bulk";
  46712. + break;
  46713. + default:
  46714. + type = "?";
  46715. + break;
  46716. + }
  46717. +
  46718. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  46719. +
  46720. +#ifdef DEBUG
  46721. + if (qh->ep_type == UE_INTERRUPT) {
  46722. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  46723. + qh->usecs);
  46724. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  46725. + qh->interval);
  46726. + }
  46727. +#endif
  46728. +
  46729. +}
  46730. +
  46731. +/**
  46732. + * This function allocates and initializes a QH.
  46733. + *
  46734. + * @param hcd The HCD state structure for the DWC OTG controller.
  46735. + * @param urb Holds the information about the device/endpoint that we need
  46736. + * to initialize the QH.
  46737. + * @param atomic_alloc Flag to do atomic allocation if needed
  46738. + *
  46739. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  46740. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  46741. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  46742. +{
  46743. + dwc_otg_qh_t *qh;
  46744. +
  46745. + /* Allocate memory */
  46746. + /** @todo add memflags argument */
  46747. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  46748. + if (qh == NULL) {
  46749. + DWC_ERROR("qh allocation failed");
  46750. + return NULL;
  46751. + }
  46752. +
  46753. + qh_init(hcd, qh, urb);
  46754. +
  46755. + if (hcd->core_if->dma_desc_enable
  46756. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  46757. + dwc_otg_hcd_qh_free(hcd, qh);
  46758. + return NULL;
  46759. + }
  46760. +
  46761. + return qh;
  46762. +}
  46763. +
  46764. +/* microframe_schedule=0 start */
  46765. +
  46766. +/**
  46767. + * Checks that a channel is available for a periodic transfer.
  46768. + *
  46769. + * @return 0 if successful, negative error code otherise.
  46770. + */
  46771. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  46772. +{
  46773. + /*
  46774. + * Currently assuming that there is a dedicated host channnel for each
  46775. + * periodic transaction plus at least one host channel for
  46776. + * non-periodic transactions.
  46777. + */
  46778. + int status;
  46779. + int num_channels;
  46780. +
  46781. + num_channels = hcd->core_if->core_params->host_channels;
  46782. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  46783. + && (hcd->periodic_channels < num_channels - 1)) {
  46784. + status = 0;
  46785. + } else {
  46786. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  46787. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  46788. + status = -DWC_E_NO_SPACE;
  46789. + }
  46790. +
  46791. + return status;
  46792. +}
  46793. +
  46794. +/**
  46795. + * Checks that there is sufficient bandwidth for the specified QH in the
  46796. + * periodic schedule. For simplicity, this calculation assumes that all the
  46797. + * transfers in the periodic schedule may occur in the same (micro)frame.
  46798. + *
  46799. + * @param hcd The HCD state structure for the DWC OTG controller.
  46800. + * @param qh QH containing periodic bandwidth required.
  46801. + *
  46802. + * @return 0 if successful, negative error code otherwise.
  46803. + */
  46804. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  46805. +{
  46806. + int status;
  46807. + int16_t max_claimed_usecs;
  46808. +
  46809. + status = 0;
  46810. +
  46811. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  46812. + /*
  46813. + * High speed mode.
  46814. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  46815. + */
  46816. +
  46817. + max_claimed_usecs = 100 - qh->usecs;
  46818. + } else {
  46819. + /*
  46820. + * Full speed mode.
  46821. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  46822. + */
  46823. + max_claimed_usecs = 900 - qh->usecs;
  46824. + }
  46825. +
  46826. + if (hcd->periodic_usecs > max_claimed_usecs) {
  46827. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  46828. + status = -DWC_E_NO_SPACE;
  46829. + }
  46830. +
  46831. + return status;
  46832. +}
  46833. +
  46834. +/* microframe_schedule=0 end */
  46835. +
  46836. +/**
  46837. + * Microframe scheduler
  46838. + * track the total use in hcd->frame_usecs
  46839. + * keep each qh use in qh->frame_usecs
  46840. + * when surrendering the qh then donate the time back
  46841. + */
  46842. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  46843. +
  46844. +/*
  46845. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  46846. + */
  46847. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  46848. +{
  46849. + int i;
  46850. + for (i=0; i<8; i++) {
  46851. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  46852. + }
  46853. + return 0;
  46854. +}
  46855. +
  46856. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  46857. +{
  46858. + int i;
  46859. + unsigned short utime;
  46860. + int t_left;
  46861. + int ret;
  46862. + int done;
  46863. +
  46864. + ret = -1;
  46865. + utime = _qh->usecs;
  46866. + t_left = utime;
  46867. + i = 0;
  46868. + done = 0;
  46869. + while (done == 0) {
  46870. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  46871. + if (utime <= _hcd->frame_usecs[i]) {
  46872. + _hcd->frame_usecs[i] -= utime;
  46873. + _qh->frame_usecs[i] += utime;
  46874. + t_left -= utime;
  46875. + ret = i;
  46876. + done = 1;
  46877. + return ret;
  46878. + } else {
  46879. + i++;
  46880. + if (i == 8) {
  46881. + done = 1;
  46882. + ret = -1;
  46883. + }
  46884. + }
  46885. + }
  46886. + return ret;
  46887. + }
  46888. +
  46889. +/*
  46890. + * use this for FS apps that can span multiple uframes
  46891. + */
  46892. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  46893. +{
  46894. + int i;
  46895. + int j;
  46896. + unsigned short utime;
  46897. + int t_left;
  46898. + int ret;
  46899. + int done;
  46900. + unsigned short xtime;
  46901. +
  46902. + ret = -1;
  46903. + utime = _qh->usecs;
  46904. + t_left = utime;
  46905. + i = 0;
  46906. + done = 0;
  46907. +loop:
  46908. + while (done == 0) {
  46909. + if(_hcd->frame_usecs[i] <= 0) {
  46910. + i++;
  46911. + if (i == 8) {
  46912. + done = 1;
  46913. + ret = -1;
  46914. + }
  46915. + goto loop;
  46916. + }
  46917. +
  46918. + /*
  46919. + * we need n consecutive slots
  46920. + * so use j as a start slot j plus j+1 must be enough time (for now)
  46921. + */
  46922. + xtime= _hcd->frame_usecs[i];
  46923. + for (j = i+1 ; j < 8 ; j++ ) {
  46924. + /*
  46925. + * if we add this frame remaining time to xtime we may
  46926. + * be OK, if not we need to test j for a complete frame
  46927. + */
  46928. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  46929. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  46930. + j = 8;
  46931. + ret = -1;
  46932. + continue;
  46933. + }
  46934. + }
  46935. + if (xtime >= utime) {
  46936. + ret = i;
  46937. + j = 8; /* stop loop with a good value ret */
  46938. + continue;
  46939. + }
  46940. + /* add the frame time to x time */
  46941. + xtime += _hcd->frame_usecs[j];
  46942. + /* we must have a fully available next frame or break */
  46943. + if ((xtime < utime)
  46944. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  46945. + ret = -1;
  46946. + j = 8; /* stop loop with a bad value ret */
  46947. + continue;
  46948. + }
  46949. + }
  46950. + if (ret >= 0) {
  46951. + t_left = utime;
  46952. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  46953. + t_left -= _hcd->frame_usecs[j];
  46954. + if ( t_left <= 0 ) {
  46955. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  46956. + _hcd->frame_usecs[j]= -t_left;
  46957. + ret = i;
  46958. + done = 1;
  46959. + } else {
  46960. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  46961. + _hcd->frame_usecs[j] = 0;
  46962. + }
  46963. + }
  46964. + } else {
  46965. + i++;
  46966. + if (i == 8) {
  46967. + done = 1;
  46968. + ret = -1;
  46969. + }
  46970. + }
  46971. + }
  46972. + return ret;
  46973. +}
  46974. +
  46975. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  46976. +{
  46977. + int ret;
  46978. + ret = -1;
  46979. +
  46980. + if (_qh->speed == USB_SPEED_HIGH) {
  46981. + /* if this is a hs transaction we need a full frame */
  46982. + ret = find_single_uframe(_hcd, _qh);
  46983. + } else {
  46984. + /* if this is a fs transaction we may need a sequence of frames */
  46985. + ret = find_multi_uframe(_hcd, _qh);
  46986. + }
  46987. + return ret;
  46988. +}
  46989. +
  46990. +/**
  46991. + * Checks that the max transfer size allowed in a host channel is large enough
  46992. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  46993. + * transfer.
  46994. + *
  46995. + * @param hcd The HCD state structure for the DWC OTG controller.
  46996. + * @param qh QH for a periodic endpoint.
  46997. + *
  46998. + * @return 0 if successful, negative error code otherwise.
  46999. + */
  47000. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47001. +{
  47002. + int status;
  47003. + uint32_t max_xfer_size;
  47004. + uint32_t max_channel_xfer_size;
  47005. +
  47006. + status = 0;
  47007. +
  47008. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  47009. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  47010. +
  47011. + if (max_xfer_size > max_channel_xfer_size) {
  47012. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  47013. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  47014. + status = -DWC_E_NO_SPACE;
  47015. + }
  47016. +
  47017. + return status;
  47018. +}
  47019. +
  47020. +
  47021. +
  47022. +/**
  47023. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  47024. + *
  47025. + * @param hcd The HCD state structure for the DWC OTG controller.
  47026. + * @param qh QH for the periodic transfer. The QH should already contain the
  47027. + * scheduling information.
  47028. + *
  47029. + * @return 0 if successful, negative error code otherwise.
  47030. + */
  47031. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47032. +{
  47033. + int status = 0;
  47034. +
  47035. + if (microframe_schedule) {
  47036. + int frame;
  47037. + status = find_uframe(hcd, qh);
  47038. + frame = -1;
  47039. + if (status == 0) {
  47040. + frame = 7;
  47041. + } else {
  47042. + if (status > 0 )
  47043. + frame = status-1;
  47044. + }
  47045. +
  47046. + /* Set the new frame up */
  47047. + if (frame > -1) {
  47048. + qh->sched_frame &= ~0x7;
  47049. + qh->sched_frame |= (frame & 7);
  47050. + }
  47051. +
  47052. + if (status != -1)
  47053. + status = 0;
  47054. + } else {
  47055. + status = periodic_channel_available(hcd);
  47056. + if (status) {
  47057. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  47058. + return status;
  47059. + }
  47060. +
  47061. + status = check_periodic_bandwidth(hcd, qh);
  47062. + }
  47063. + if (status) {
  47064. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  47065. + "periodic transfer.\n", __func__);
  47066. + return status;
  47067. + }
  47068. + status = check_max_xfer_size(hcd, qh);
  47069. + if (status) {
  47070. + DWC_INFO("%s: Channel max transfer size too small "
  47071. + "for periodic transfer.\n", __func__);
  47072. + return status;
  47073. + }
  47074. +
  47075. + if (hcd->core_if->dma_desc_enable) {
  47076. + /* Don't rely on SOF and start in ready schedule */
  47077. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  47078. + }
  47079. + else {
  47080. + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
  47081. + {
  47082. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  47083. +
  47084. + }
  47085. + /* Always start in the inactive schedule. */
  47086. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  47087. + }
  47088. +
  47089. + if (!microframe_schedule) {
  47090. + /* Reserve the periodic channel. */
  47091. + hcd->periodic_channels++;
  47092. + }
  47093. +
  47094. + /* Update claimed usecs per (micro)frame. */
  47095. + hcd->periodic_usecs += qh->usecs;
  47096. +
  47097. + return status;
  47098. +}
  47099. +
  47100. +
  47101. +/**
  47102. + * This function adds a QH to either the non periodic or periodic schedule if
  47103. + * it is not already in the schedule. If the QH is already in the schedule, no
  47104. + * action is taken.
  47105. + *
  47106. + * @return 0 if successful, negative error code otherwise.
  47107. + */
  47108. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47109. +{
  47110. + int status = 0;
  47111. + gintmsk_data_t intr_mask = {.d32 = 0 };
  47112. +
  47113. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  47114. + /* QH already in a schedule. */
  47115. + return status;
  47116. + }
  47117. +
  47118. + /* Add the new QH to the appropriate schedule */
  47119. + if (dwc_qh_is_non_per(qh)) {
  47120. + /* Always start in the inactive schedule. */
  47121. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  47122. + &qh->qh_list_entry);
  47123. + //hcd->fiq_state->kick_np_queues = 1;
  47124. + } else {
  47125. + status = schedule_periodic(hcd, qh);
  47126. + if ( !hcd->periodic_qh_count ) {
  47127. + intr_mask.b.sofintr = 1;
  47128. + if (fiq_enable) {
  47129. + local_fiq_disable();
  47130. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  47131. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  47132. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  47133. + local_fiq_enable();
  47134. + } else {
  47135. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  47136. + }
  47137. + }
  47138. + hcd->periodic_qh_count++;
  47139. + }
  47140. +
  47141. + return status;
  47142. +}
  47143. +
  47144. +/**
  47145. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  47146. + *
  47147. + * @param hcd The HCD state structure for the DWC OTG controller.
  47148. + * @param qh QH for the periodic transfer.
  47149. + */
  47150. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47151. +{
  47152. + int i;
  47153. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  47154. +
  47155. + /* Update claimed usecs per (micro)frame. */
  47156. + hcd->periodic_usecs -= qh->usecs;
  47157. +
  47158. + if (!microframe_schedule) {
  47159. + /* Release the periodic channel reservation. */
  47160. + hcd->periodic_channels--;
  47161. + } else {
  47162. + for (i = 0; i < 8; i++) {
  47163. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  47164. + qh->frame_usecs[i] = 0;
  47165. + }
  47166. + }
  47167. +}
  47168. +
  47169. +/**
  47170. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  47171. + * not freed.
  47172. + *
  47173. + * @param hcd The HCD state structure.
  47174. + * @param qh QH to remove from schedule. */
  47175. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47176. +{
  47177. + gintmsk_data_t intr_mask = {.d32 = 0 };
  47178. +
  47179. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  47180. + /* QH is not in a schedule. */
  47181. + return;
  47182. + }
  47183. +
  47184. + if (dwc_qh_is_non_per(qh)) {
  47185. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  47186. + hcd->non_periodic_qh_ptr =
  47187. + hcd->non_periodic_qh_ptr->next;
  47188. + }
  47189. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  47190. + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
  47191. + // hcd->fiq_state->kick_np_queues = 1;
  47192. + } else {
  47193. + deschedule_periodic(hcd, qh);
  47194. + hcd->periodic_qh_count--;
  47195. + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
  47196. + intr_mask.b.sofintr = 1;
  47197. + if (fiq_enable) {
  47198. + local_fiq_disable();
  47199. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  47200. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  47201. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  47202. + local_fiq_enable();
  47203. + } else {
  47204. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  47205. + }
  47206. + }
  47207. + }
  47208. +}
  47209. +
  47210. +/**
  47211. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  47212. + * non-periodic schedule. The QH is added to the inactive non-periodic
  47213. + * schedule if any QTDs are still attached to the QH.
  47214. + *
  47215. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  47216. + * there are any QTDs still attached to the QH, the QH is added to either the
  47217. + * periodic inactive schedule or the periodic ready schedule and its next
  47218. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  47219. + * the scheduled frame has been reached already. Otherwise it's placed in the
  47220. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  47221. + * completely removed from the periodic schedule.
  47222. + */
  47223. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  47224. + int sched_next_periodic_split)
  47225. +{
  47226. + if (dwc_qh_is_non_per(qh)) {
  47227. + dwc_otg_hcd_qh_remove(hcd, qh);
  47228. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  47229. + /* Add back to inactive non-periodic schedule. */
  47230. + dwc_otg_hcd_qh_add(hcd, qh);
  47231. + //hcd->fiq_state->kick_np_queues = 1;
  47232. + }
  47233. + } else {
  47234. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  47235. +
  47236. + if (qh->do_split) {
  47237. + /* Schedule the next continuing periodic split transfer */
  47238. + if (sched_next_periodic_split) {
  47239. +
  47240. + qh->sched_frame = frame_number;
  47241. +
  47242. + if (dwc_frame_num_le(frame_number,
  47243. + dwc_frame_num_inc
  47244. + (qh->start_split_frame,
  47245. + 1))) {
  47246. + /*
  47247. + * Allow one frame to elapse after start
  47248. + * split microframe before scheduling
  47249. + * complete split, but DONT if we are
  47250. + * doing the next start split in the
  47251. + * same frame for an ISOC out.
  47252. + */
  47253. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  47254. + (qh->ep_is_in != 0)) {
  47255. + qh->sched_frame =
  47256. + dwc_frame_num_inc(qh->sched_frame, 1);
  47257. + }
  47258. + }
  47259. + } else {
  47260. + qh->sched_frame =
  47261. + dwc_frame_num_inc(qh->start_split_frame,
  47262. + qh->interval);
  47263. + if (dwc_frame_num_le
  47264. + (qh->sched_frame, frame_number)) {
  47265. + qh->sched_frame = frame_number;
  47266. + }
  47267. + qh->sched_frame |= 0x7;
  47268. + qh->start_split_frame = qh->sched_frame;
  47269. + }
  47270. + } else {
  47271. + qh->sched_frame =
  47272. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  47273. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  47274. + qh->sched_frame = frame_number;
  47275. + }
  47276. + }
  47277. +
  47278. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  47279. + dwc_otg_hcd_qh_remove(hcd, qh);
  47280. + } else {
  47281. + /*
  47282. + * Remove from periodic_sched_queued and move to
  47283. + * appropriate queue.
  47284. + */
  47285. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  47286. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  47287. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  47288. + &qh->qh_list_entry);
  47289. + } else {
  47290. + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
  47291. + {
  47292. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  47293. + }
  47294. +
  47295. + DWC_LIST_MOVE_HEAD
  47296. + (&hcd->periodic_sched_inactive,
  47297. + &qh->qh_list_entry);
  47298. + }
  47299. + }
  47300. + }
  47301. +}
  47302. +
  47303. +/**
  47304. + * This function allocates and initializes a QTD.
  47305. + *
  47306. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  47307. + * pointing to each other so each pair should have a unique correlation.
  47308. + * @param atomic_alloc Flag to do atomic alloc if needed
  47309. + *
  47310. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  47311. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  47312. +{
  47313. + dwc_otg_qtd_t *qtd;
  47314. +
  47315. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  47316. + if (qtd == NULL) {
  47317. + return NULL;
  47318. + }
  47319. +
  47320. + dwc_otg_hcd_qtd_init(qtd, urb);
  47321. + return qtd;
  47322. +}
  47323. +
  47324. +/**
  47325. + * Initializes a QTD structure.
  47326. + *
  47327. + * @param qtd The QTD to initialize.
  47328. + * @param urb The URB to use for initialization. */
  47329. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  47330. +{
  47331. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  47332. + qtd->urb = urb;
  47333. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  47334. + /*
  47335. + * The only time the QTD data toggle is used is on the data
  47336. + * phase of control transfers. This phase always starts with
  47337. + * DATA1.
  47338. + */
  47339. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  47340. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  47341. + }
  47342. +
  47343. + /* start split */
  47344. + qtd->complete_split = 0;
  47345. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  47346. + qtd->isoc_split_offset = 0;
  47347. + qtd->in_process = 0;
  47348. +
  47349. + /* Store the qtd ptr in the urb to reference what QTD. */
  47350. + urb->qtd = qtd;
  47351. + return;
  47352. +}
  47353. +
  47354. +/**
  47355. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  47356. + * QH to place the QTD into. If it does not find a QH, then it will create a
  47357. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  47358. + * is placed into the proper schedule based on its EP type.
  47359. + * HCD lock must be held and interrupts must be disabled on entry
  47360. + *
  47361. + * @param[in] qtd The QTD to add
  47362. + * @param[in] hcd The DWC HCD structure
  47363. + * @param[out] qh out parameter to return queue head
  47364. + * @param atomic_alloc Flag to do atomic alloc if needed
  47365. + *
  47366. + * @return 0 if successful, negative error code otherwise.
  47367. + */
  47368. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  47369. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  47370. +{
  47371. + int retval = 0;
  47372. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  47373. +
  47374. + /*
  47375. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  47376. + * doesn't exist.
  47377. + */
  47378. + if (*qh == NULL) {
  47379. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  47380. + if (*qh == NULL) {
  47381. + retval = -DWC_E_NO_MEMORY;
  47382. + goto done;
  47383. + } else {
  47384. + if (fiq_enable)
  47385. + hcd->fiq_state->kick_np_queues = 1;
  47386. + }
  47387. + }
  47388. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  47389. + if (retval == 0) {
  47390. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  47391. + qtd_list_entry);
  47392. + qtd->qh = *qh;
  47393. + }
  47394. +done:
  47395. +
  47396. + return retval;
  47397. +}
  47398. +
  47399. +#endif /* DWC_DEVICE_ONLY */
  47400. --- /dev/null
  47401. +++ b/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  47402. @@ -0,0 +1,188 @@
  47403. +#ifndef _DWC_OS_DEP_H_
  47404. +#define _DWC_OS_DEP_H_
  47405. +
  47406. +/**
  47407. + * @file
  47408. + *
  47409. + * This file contains OS dependent structures.
  47410. + *
  47411. + */
  47412. +
  47413. +#include <linux/kernel.h>
  47414. +#include <linux/module.h>
  47415. +#include <linux/moduleparam.h>
  47416. +#include <linux/init.h>
  47417. +#include <linux/device.h>
  47418. +#include <linux/errno.h>
  47419. +#include <linux/types.h>
  47420. +#include <linux/slab.h>
  47421. +#include <linux/list.h>
  47422. +#include <linux/interrupt.h>
  47423. +#include <linux/ctype.h>
  47424. +#include <linux/string.h>
  47425. +#include <linux/dma-mapping.h>
  47426. +#include <linux/jiffies.h>
  47427. +#include <linux/delay.h>
  47428. +#include <linux/timer.h>
  47429. +#include <linux/workqueue.h>
  47430. +#include <linux/stat.h>
  47431. +#include <linux/pci.h>
  47432. +
  47433. +#include <linux/version.h>
  47434. +
  47435. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  47436. +# include <linux/irq.h>
  47437. +#endif
  47438. +
  47439. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  47440. +# include <linux/usb/ch9.h>
  47441. +#else
  47442. +# include <linux/usb_ch9.h>
  47443. +#endif
  47444. +
  47445. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  47446. +# include <linux/usb/gadget.h>
  47447. +#else
  47448. +# include <linux/usb_gadget.h>
  47449. +#endif
  47450. +
  47451. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  47452. +# include <asm/irq.h>
  47453. +#endif
  47454. +
  47455. +#ifdef PCI_INTERFACE
  47456. +# include <asm/io.h>
  47457. +#endif
  47458. +
  47459. +#ifdef LM_INTERFACE
  47460. +# include <asm/unaligned.h>
  47461. +# include <asm/sizes.h>
  47462. +# include <asm/param.h>
  47463. +# include <asm/io.h>
  47464. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  47465. +# include <asm/arch/hardware.h>
  47466. +# include <asm/arch/lm.h>
  47467. +# include <asm/arch/irqs.h>
  47468. +# include <asm/arch/regs-irq.h>
  47469. +# else
  47470. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  47471. + here we assume that the machine architecture provides definitions
  47472. + in its own header
  47473. +*/
  47474. +# include <mach/lm.h>
  47475. +# include <mach/hardware.h>
  47476. +# endif
  47477. +#endif
  47478. +
  47479. +#ifdef PLATFORM_INTERFACE
  47480. +#include <linux/platform_device.h>
  47481. +#include <asm/mach/map.h>
  47482. +#endif
  47483. +
  47484. +/** The OS page size */
  47485. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  47486. +
  47487. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  47488. +typedef int gfp_t;
  47489. +#endif
  47490. +
  47491. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  47492. +# define IRQF_SHARED SA_SHIRQ
  47493. +#endif
  47494. +
  47495. +typedef struct os_dependent {
  47496. + /** Base address returned from ioremap() */
  47497. + void *base;
  47498. +
  47499. + /** Register offset for Diagnostic API */
  47500. + uint32_t reg_offset;
  47501. +
  47502. + /** Base address for MPHI peripheral */
  47503. + void *mphi_base;
  47504. +
  47505. +#ifdef LM_INTERFACE
  47506. + struct lm_device *lmdev;
  47507. +#elif defined(PCI_INTERFACE)
  47508. + struct pci_dev *pcidev;
  47509. +
  47510. + /** Start address of a PCI region */
  47511. + resource_size_t rsrc_start;
  47512. +
  47513. + /** Length address of a PCI region */
  47514. + resource_size_t rsrc_len;
  47515. +#elif defined(PLATFORM_INTERFACE)
  47516. + struct platform_device *platformdev;
  47517. +#endif
  47518. +
  47519. +} os_dependent_t;
  47520. +
  47521. +#ifdef __cplusplus
  47522. +}
  47523. +#endif
  47524. +
  47525. +
  47526. +
  47527. +/* Type for the our device on the chosen bus */
  47528. +#if defined(LM_INTERFACE)
  47529. +typedef struct lm_device dwc_bus_dev_t;
  47530. +#elif defined(PCI_INTERFACE)
  47531. +typedef struct pci_dev dwc_bus_dev_t;
  47532. +#elif defined(PLATFORM_INTERFACE)
  47533. +typedef struct platform_device dwc_bus_dev_t;
  47534. +#endif
  47535. +
  47536. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  47537. +#if defined(LM_INTERFACE)
  47538. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  47539. +#elif defined(PCI_INTERFACE)
  47540. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  47541. +#elif defined(PLATFORM_INTERFACE)
  47542. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  47543. +#endif
  47544. +
  47545. +/**
  47546. + * Helper macro returning the otg_device structure of a given struct device
  47547. + *
  47548. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  47549. + */
  47550. +#ifdef LM_INTERFACE
  47551. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47552. + struct lm_device *lm_dev = \
  47553. + container_of(_dev, struct lm_device, dev); \
  47554. + _var = lm_get_drvdata(lm_dev); \
  47555. + } while (0)
  47556. +
  47557. +#elif defined(PCI_INTERFACE)
  47558. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47559. + _var = dev_get_drvdata(_dev); \
  47560. + } while (0)
  47561. +
  47562. +#elif defined(PLATFORM_INTERFACE)
  47563. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47564. + struct platform_device *platform_dev = \
  47565. + container_of(_dev, struct platform_device, dev); \
  47566. + _var = platform_get_drvdata(platform_dev); \
  47567. + } while (0)
  47568. +#endif
  47569. +
  47570. +
  47571. +/**
  47572. + * Helper macro returning the struct dev of the given struct os_dependent
  47573. + *
  47574. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  47575. + */
  47576. +#ifdef LM_INTERFACE
  47577. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47578. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  47579. +#elif defined(PCI_INTERFACE)
  47580. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47581. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  47582. +#elif defined(PLATFORM_INTERFACE)
  47583. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47584. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  47585. +#endif
  47586. +
  47587. +
  47588. +
  47589. +
  47590. +#endif /* _DWC_OS_DEP_H_ */
  47591. --- /dev/null
  47592. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  47593. @@ -0,0 +1,2712 @@
  47594. +/* ==========================================================================
  47595. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  47596. + * $Revision: #101 $
  47597. + * $Date: 2012/08/10 $
  47598. + * $Change: 2047372 $
  47599. + *
  47600. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  47601. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  47602. + * otherwise expressly agreed to in writing between Synopsys and you.
  47603. + *
  47604. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  47605. + * any End User Software License Agreement or Agreement for Licensed Product
  47606. + * with Synopsys or any supplement thereto. You are permitted to use and
  47607. + * redistribute this Software in source and binary forms, with or without
  47608. + * modification, provided that redistributions of source code must retain this
  47609. + * notice. You may not view, use, disclose, copy or distribute this file or
  47610. + * any information contained herein except pursuant to this license grant from
  47611. + * Synopsys. If you do not agree with this notice, including the disclaimer
  47612. + * below, then you are not authorized to use the Software.
  47613. + *
  47614. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  47615. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47616. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47617. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  47618. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  47619. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  47620. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  47621. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47622. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47623. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  47624. + * DAMAGE.
  47625. + * ========================================================================== */
  47626. +#ifndef DWC_HOST_ONLY
  47627. +
  47628. +/** @file
  47629. + * This file implements PCD Core. All code in this file is portable and doesn't
  47630. + * use any OS specific functions.
  47631. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  47632. + * header file, which can be used to implement OS specific PCD interface.
  47633. + *
  47634. + * An important function of the PCD is managing interrupts generated
  47635. + * by the DWC_otg controller. The implementation of the DWC_otg device
  47636. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  47637. + *
  47638. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  47639. + * @todo Does it work when the request size is greater than DEPTSIZ
  47640. + * transfer size
  47641. + *
  47642. + */
  47643. +
  47644. +#include "dwc_otg_pcd.h"
  47645. +
  47646. +#ifdef DWC_UTE_CFI
  47647. +#include "dwc_otg_cfi.h"
  47648. +
  47649. +extern int init_cfi(cfiobject_t * cfiobj);
  47650. +#endif
  47651. +
  47652. +/**
  47653. + * Choose endpoint from ep arrays using usb_ep structure.
  47654. + */
  47655. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  47656. +{
  47657. + int i;
  47658. + if (pcd->ep0.priv == handle) {
  47659. + return &pcd->ep0;
  47660. + }
  47661. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  47662. + if (pcd->in_ep[i].priv == handle)
  47663. + return &pcd->in_ep[i];
  47664. + if (pcd->out_ep[i].priv == handle)
  47665. + return &pcd->out_ep[i];
  47666. + }
  47667. +
  47668. + return NULL;
  47669. +}
  47670. +
  47671. +/**
  47672. + * This function completes a request. It call's the request call back.
  47673. + */
  47674. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  47675. + int32_t status)
  47676. +{
  47677. + unsigned stopped = ep->stopped;
  47678. +
  47679. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  47680. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  47681. +
  47682. + /* don't modify queue heads during completion callback */
  47683. + ep->stopped = 1;
  47684. + /* spin_unlock/spin_lock now done in fops->complete() */
  47685. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  47686. + req->actual);
  47687. +
  47688. + if (ep->pcd->request_pending > 0) {
  47689. + --ep->pcd->request_pending;
  47690. + }
  47691. +
  47692. + ep->stopped = stopped;
  47693. + DWC_FREE(req);
  47694. +}
  47695. +
  47696. +/**
  47697. + * This function terminates all the requsts in the EP request queue.
  47698. + */
  47699. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  47700. +{
  47701. + dwc_otg_pcd_request_t *req;
  47702. +
  47703. + ep->stopped = 1;
  47704. +
  47705. + /* called with irqs blocked?? */
  47706. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  47707. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  47708. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  47709. + }
  47710. +}
  47711. +
  47712. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  47713. + const struct dwc_otg_pcd_function_ops *fops)
  47714. +{
  47715. + pcd->fops = fops;
  47716. +}
  47717. +
  47718. +/**
  47719. + * PCD Callback function for initializing the PCD when switching to
  47720. + * device mode.
  47721. + *
  47722. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47723. + */
  47724. +static int32_t dwc_otg_pcd_start_cb(void *p)
  47725. +{
  47726. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47727. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  47728. +
  47729. + /*
  47730. + * Initialized the Core for Device mode.
  47731. + */
  47732. + if (dwc_otg_is_device_mode(core_if)) {
  47733. + dwc_otg_core_dev_init(core_if);
  47734. + /* Set core_if's lock pointer to the pcd->lock */
  47735. + core_if->lock = pcd->lock;
  47736. + }
  47737. + return 1;
  47738. +}
  47739. +
  47740. +/** CFI-specific buffer allocation function for EP */
  47741. +#ifdef DWC_UTE_CFI
  47742. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  47743. + size_t buflen, int flags)
  47744. +{
  47745. + dwc_otg_pcd_ep_t *ep;
  47746. + ep = get_ep_from_handle(pcd, pep);
  47747. + if (!ep) {
  47748. + DWC_WARN("bad ep\n");
  47749. + return -DWC_E_INVALID;
  47750. + }
  47751. +
  47752. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  47753. + flags);
  47754. +}
  47755. +#else
  47756. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  47757. + size_t buflen, int flags);
  47758. +#endif
  47759. +
  47760. +/**
  47761. + * PCD Callback function for notifying the PCD when resuming from
  47762. + * suspend.
  47763. + *
  47764. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47765. + */
  47766. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  47767. +{
  47768. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47769. +
  47770. + if (pcd->fops->resume) {
  47771. + pcd->fops->resume(pcd);
  47772. + }
  47773. +
  47774. + /* Stop the SRP timeout timer. */
  47775. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  47776. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  47777. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  47778. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  47779. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  47780. + }
  47781. + }
  47782. + return 1;
  47783. +}
  47784. +
  47785. +/**
  47786. + * PCD Callback function for notifying the PCD device is suspended.
  47787. + *
  47788. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47789. + */
  47790. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  47791. +{
  47792. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47793. +
  47794. + if (pcd->fops->suspend) {
  47795. + DWC_SPINUNLOCK(pcd->lock);
  47796. + pcd->fops->suspend(pcd);
  47797. + DWC_SPINLOCK(pcd->lock);
  47798. + }
  47799. +
  47800. + return 1;
  47801. +}
  47802. +
  47803. +/**
  47804. + * PCD Callback function for stopping the PCD when switching to Host
  47805. + * mode.
  47806. + *
  47807. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47808. + */
  47809. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  47810. +{
  47811. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47812. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  47813. +
  47814. + dwc_otg_pcd_stop(pcd);
  47815. + return 1;
  47816. +}
  47817. +
  47818. +/**
  47819. + * PCD Callback structure for handling mode switching.
  47820. + */
  47821. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  47822. + .start = dwc_otg_pcd_start_cb,
  47823. + .stop = dwc_otg_pcd_stop_cb,
  47824. + .suspend = dwc_otg_pcd_suspend_cb,
  47825. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  47826. + .p = 0, /* Set at registration */
  47827. +};
  47828. +
  47829. +/**
  47830. + * This function allocates a DMA Descriptor chain for the Endpoint
  47831. + * buffer to be used for a transfer to/from the specified endpoint.
  47832. + */
  47833. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(dwc_dma_t * dma_desc_addr,
  47834. + uint32_t count)
  47835. +{
  47836. + return DWC_DMA_ALLOC_ATOMIC(count * sizeof(dwc_otg_dev_dma_desc_t),
  47837. + dma_desc_addr);
  47838. +}
  47839. +
  47840. +/**
  47841. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  47842. + */
  47843. +void dwc_otg_ep_free_desc_chain(dwc_otg_dev_dma_desc_t * desc_addr,
  47844. + uint32_t dma_desc_addr, uint32_t count)
  47845. +{
  47846. + DWC_DMA_FREE(count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  47847. + dma_desc_addr);
  47848. +}
  47849. +
  47850. +#ifdef DWC_EN_ISOC
  47851. +
  47852. +/**
  47853. + * This function initializes a descriptor chain for Isochronous transfer
  47854. + *
  47855. + * @param core_if Programming view of DWC_otg controller.
  47856. + * @param dwc_ep The EP to start the transfer on.
  47857. + *
  47858. + */
  47859. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  47860. + dwc_ep_t * dwc_ep)
  47861. +{
  47862. +
  47863. + dsts_data_t dsts = {.d32 = 0 };
  47864. + depctl_data_t depctl = {.d32 = 0 };
  47865. + volatile uint32_t *addr;
  47866. + int i, j;
  47867. + uint32_t len;
  47868. +
  47869. + if (dwc_ep->is_in)
  47870. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  47871. + else
  47872. + dwc_ep->desc_cnt =
  47873. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  47874. + dwc_ep->bInterval;
  47875. +
  47876. + /** Allocate descriptors for double buffering */
  47877. + dwc_ep->iso_desc_addr =
  47878. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  47879. + dwc_ep->desc_cnt * 2);
  47880. + if (dwc_ep->desc_addr) {
  47881. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  47882. + return;
  47883. + }
  47884. +
  47885. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  47886. +
  47887. + /** ISO OUT EP */
  47888. + if (dwc_ep->is_in == 0) {
  47889. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  47890. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  47891. + dma_addr_t dma_ad;
  47892. + uint32_t data_per_desc;
  47893. + dwc_otg_dev_out_ep_regs_t *out_regs =
  47894. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  47895. + int offset;
  47896. +
  47897. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  47898. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  47899. +
  47900. + /** Buffer 0 descriptors setup */
  47901. + dma_ad = dwc_ep->dma_addr0;
  47902. +
  47903. + sts.b_iso_out.bs = BS_HOST_READY;
  47904. + sts.b_iso_out.rxsts = 0;
  47905. + sts.b_iso_out.l = 0;
  47906. + sts.b_iso_out.sp = 0;
  47907. + sts.b_iso_out.ioc = 0;
  47908. + sts.b_iso_out.pid = 0;
  47909. + sts.b_iso_out.framenum = 0;
  47910. +
  47911. + offset = 0;
  47912. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  47913. + i += dwc_ep->pkt_per_frm) {
  47914. +
  47915. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  47916. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  47917. + if (len > dwc_ep->data_per_frame)
  47918. + data_per_desc =
  47919. + dwc_ep->data_per_frame -
  47920. + j * dwc_ep->maxpacket;
  47921. + else
  47922. + data_per_desc = dwc_ep->maxpacket;
  47923. + len = data_per_desc % 4;
  47924. + if (len)
  47925. + data_per_desc += 4 - len;
  47926. +
  47927. + sts.b_iso_out.rxbytes = data_per_desc;
  47928. + dma_desc->buf = dma_ad;
  47929. + dma_desc->status.d32 = sts.d32;
  47930. +
  47931. + offset += data_per_desc;
  47932. + dma_desc++;
  47933. + dma_ad += data_per_desc;
  47934. + }
  47935. + }
  47936. +
  47937. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  47938. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  47939. + if (len > dwc_ep->data_per_frame)
  47940. + data_per_desc =
  47941. + dwc_ep->data_per_frame -
  47942. + j * dwc_ep->maxpacket;
  47943. + else
  47944. + data_per_desc = dwc_ep->maxpacket;
  47945. + len = data_per_desc % 4;
  47946. + if (len)
  47947. + data_per_desc += 4 - len;
  47948. + sts.b_iso_out.rxbytes = data_per_desc;
  47949. + dma_desc->buf = dma_ad;
  47950. + dma_desc->status.d32 = sts.d32;
  47951. +
  47952. + offset += data_per_desc;
  47953. + dma_desc++;
  47954. + dma_ad += data_per_desc;
  47955. + }
  47956. +
  47957. + sts.b_iso_out.ioc = 1;
  47958. + len = (j + 1) * dwc_ep->maxpacket;
  47959. + if (len > dwc_ep->data_per_frame)
  47960. + data_per_desc =
  47961. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  47962. + else
  47963. + data_per_desc = dwc_ep->maxpacket;
  47964. + len = data_per_desc % 4;
  47965. + if (len)
  47966. + data_per_desc += 4 - len;
  47967. + sts.b_iso_out.rxbytes = data_per_desc;
  47968. +
  47969. + dma_desc->buf = dma_ad;
  47970. + dma_desc->status.d32 = sts.d32;
  47971. + dma_desc++;
  47972. +
  47973. + /** Buffer 1 descriptors setup */
  47974. + sts.b_iso_out.ioc = 0;
  47975. + dma_ad = dwc_ep->dma_addr1;
  47976. +
  47977. + offset = 0;
  47978. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  47979. + i += dwc_ep->pkt_per_frm) {
  47980. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  47981. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  47982. + if (len > dwc_ep->data_per_frame)
  47983. + data_per_desc =
  47984. + dwc_ep->data_per_frame -
  47985. + j * dwc_ep->maxpacket;
  47986. + else
  47987. + data_per_desc = dwc_ep->maxpacket;
  47988. + len = data_per_desc % 4;
  47989. + if (len)
  47990. + data_per_desc += 4 - len;
  47991. +
  47992. + data_per_desc =
  47993. + sts.b_iso_out.rxbytes = data_per_desc;
  47994. + dma_desc->buf = dma_ad;
  47995. + dma_desc->status.d32 = sts.d32;
  47996. +
  47997. + offset += data_per_desc;
  47998. + dma_desc++;
  47999. + dma_ad += data_per_desc;
  48000. + }
  48001. + }
  48002. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  48003. + data_per_desc =
  48004. + ((j + 1) * dwc_ep->maxpacket >
  48005. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  48006. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  48007. + data_per_desc +=
  48008. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  48009. + sts.b_iso_out.rxbytes = data_per_desc;
  48010. + dma_desc->buf = dma_ad;
  48011. + dma_desc->status.d32 = sts.d32;
  48012. +
  48013. + offset += data_per_desc;
  48014. + dma_desc++;
  48015. + dma_ad += data_per_desc;
  48016. + }
  48017. +
  48018. + sts.b_iso_out.ioc = 1;
  48019. + sts.b_iso_out.l = 1;
  48020. + data_per_desc =
  48021. + ((j + 1) * dwc_ep->maxpacket >
  48022. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  48023. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  48024. + data_per_desc +=
  48025. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  48026. + sts.b_iso_out.rxbytes = data_per_desc;
  48027. +
  48028. + dma_desc->buf = dma_ad;
  48029. + dma_desc->status.d32 = sts.d32;
  48030. +
  48031. + dwc_ep->next_frame = 0;
  48032. +
  48033. + /** Write dma_ad into DOEPDMA register */
  48034. + DWC_WRITE_REG32(&(out_regs->doepdma),
  48035. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  48036. +
  48037. + }
  48038. + /** ISO IN EP */
  48039. + else {
  48040. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  48041. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  48042. + dma_addr_t dma_ad;
  48043. + dwc_otg_dev_in_ep_regs_t *in_regs =
  48044. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  48045. + unsigned int frmnumber;
  48046. + fifosize_data_t txfifosize, rxfifosize;
  48047. +
  48048. + txfifosize.d32 =
  48049. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  48050. + dtxfsts);
  48051. + rxfifosize.d32 =
  48052. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  48053. +
  48054. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  48055. +
  48056. + dma_ad = dwc_ep->dma_addr0;
  48057. +
  48058. + dsts.d32 =
  48059. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48060. +
  48061. + sts.b_iso_in.bs = BS_HOST_READY;
  48062. + sts.b_iso_in.txsts = 0;
  48063. + sts.b_iso_in.sp =
  48064. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  48065. + sts.b_iso_in.ioc = 0;
  48066. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  48067. +
  48068. + frmnumber = dwc_ep->next_frame;
  48069. +
  48070. + sts.b_iso_in.framenum = frmnumber;
  48071. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  48072. + sts.b_iso_in.l = 0;
  48073. +
  48074. + /** Buffer 0 descriptors setup */
  48075. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  48076. + dma_desc->buf = dma_ad;
  48077. + dma_desc->status.d32 = sts.d32;
  48078. + dma_desc++;
  48079. +
  48080. + dma_ad += dwc_ep->data_per_frame;
  48081. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  48082. + }
  48083. +
  48084. + sts.b_iso_in.ioc = 1;
  48085. + dma_desc->buf = dma_ad;
  48086. + dma_desc->status.d32 = sts.d32;
  48087. + ++dma_desc;
  48088. +
  48089. + /** Buffer 1 descriptors setup */
  48090. + sts.b_iso_in.ioc = 0;
  48091. + dma_ad = dwc_ep->dma_addr1;
  48092. +
  48093. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  48094. + i += dwc_ep->pkt_per_frm) {
  48095. + dma_desc->buf = dma_ad;
  48096. + dma_desc->status.d32 = sts.d32;
  48097. + dma_desc++;
  48098. +
  48099. + dma_ad += dwc_ep->data_per_frame;
  48100. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  48101. +
  48102. + sts.b_iso_in.ioc = 0;
  48103. + }
  48104. + sts.b_iso_in.ioc = 1;
  48105. + sts.b_iso_in.l = 1;
  48106. +
  48107. + dma_desc->buf = dma_ad;
  48108. + dma_desc->status.d32 = sts.d32;
  48109. +
  48110. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  48111. +
  48112. + /** Write dma_ad into diepdma register */
  48113. + DWC_WRITE_REG32(&(in_regs->diepdma),
  48114. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  48115. + }
  48116. + /** Enable endpoint, clear nak */
  48117. + depctl.d32 = 0;
  48118. + depctl.b.epena = 1;
  48119. + depctl.b.usbactep = 1;
  48120. + depctl.b.cnak = 1;
  48121. +
  48122. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  48123. + depctl.d32 = DWC_READ_REG32(addr);
  48124. +}
  48125. +
  48126. +/**
  48127. + * This function initializes a descriptor chain for Isochronous transfer
  48128. + *
  48129. + * @param core_if Programming view of DWC_otg controller.
  48130. + * @param ep The EP to start the transfer on.
  48131. + *
  48132. + */
  48133. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  48134. + dwc_ep_t * ep)
  48135. +{
  48136. + depctl_data_t depctl = {.d32 = 0 };
  48137. + volatile uint32_t *addr;
  48138. +
  48139. + if (ep->is_in) {
  48140. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  48141. + } else {
  48142. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  48143. + }
  48144. +
  48145. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  48146. + return;
  48147. + } else {
  48148. + deptsiz_data_t deptsiz = {.d32 = 0 };
  48149. +
  48150. + ep->xfer_len =
  48151. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  48152. + ep->pkt_cnt =
  48153. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  48154. + ep->xfer_count = 0;
  48155. + ep->xfer_buff =
  48156. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  48157. + ep->dma_addr =
  48158. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  48159. +
  48160. + if (ep->is_in) {
  48161. + /* Program the transfer size and packet count
  48162. + * as follows: xfersize = N * maxpacket +
  48163. + * short_packet pktcnt = N + (short_packet
  48164. + * exist ? 1 : 0)
  48165. + */
  48166. + deptsiz.b.mc = ep->pkt_per_frm;
  48167. + deptsiz.b.xfersize = ep->xfer_len;
  48168. + deptsiz.b.pktcnt =
  48169. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  48170. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  48171. + dieptsiz, deptsiz.d32);
  48172. +
  48173. + /* Write the DMA register */
  48174. + DWC_WRITE_REG32(&
  48175. + (core_if->dev_if->in_ep_regs[ep->num]->
  48176. + diepdma), (uint32_t) ep->dma_addr);
  48177. +
  48178. + } else {
  48179. + deptsiz.b.pktcnt =
  48180. + (ep->xfer_len + (ep->maxpacket - 1)) /
  48181. + ep->maxpacket;
  48182. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  48183. +
  48184. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  48185. + doeptsiz, deptsiz.d32);
  48186. +
  48187. + /* Write the DMA register */
  48188. + DWC_WRITE_REG32(&
  48189. + (core_if->dev_if->out_ep_regs[ep->num]->
  48190. + doepdma), (uint32_t) ep->dma_addr);
  48191. +
  48192. + }
  48193. + /** Enable endpoint, clear nak */
  48194. + depctl.d32 = 0;
  48195. + depctl.b.epena = 1;
  48196. + depctl.b.cnak = 1;
  48197. +
  48198. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  48199. + }
  48200. +}
  48201. +
  48202. +/**
  48203. + * This function does the setup for a data transfer for an EP and
  48204. + * starts the transfer. For an IN transfer, the packets will be
  48205. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  48206. + * the packets are unloaded from the Rx FIFO in the ISR.
  48207. + *
  48208. + * @param core_if Programming view of DWC_otg controller.
  48209. + * @param ep The EP to start the transfer on.
  48210. + */
  48211. +
  48212. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  48213. + dwc_ep_t * ep)
  48214. +{
  48215. + if (core_if->dma_enable) {
  48216. + if (core_if->dma_desc_enable) {
  48217. + if (ep->is_in) {
  48218. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  48219. + } else {
  48220. + ep->desc_cnt = ep->pkt_cnt;
  48221. + }
  48222. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  48223. + } else {
  48224. + if (core_if->pti_enh_enable) {
  48225. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  48226. + } else {
  48227. + ep->cur_pkt_addr =
  48228. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  48229. + xfer_buff0;
  48230. + ep->cur_pkt_dma_addr =
  48231. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  48232. + dma_addr0;
  48233. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  48234. + }
  48235. + }
  48236. + } else {
  48237. + ep->cur_pkt_addr =
  48238. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  48239. + ep->cur_pkt_dma_addr =
  48240. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  48241. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  48242. + }
  48243. +}
  48244. +
  48245. +/**
  48246. + * This function stops transfer for an EP and
  48247. + * resets the ep's variables.
  48248. + *
  48249. + * @param core_if Programming view of DWC_otg controller.
  48250. + * @param ep The EP to start the transfer on.
  48251. + */
  48252. +
  48253. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  48254. +{
  48255. + depctl_data_t depctl = {.d32 = 0 };
  48256. + volatile uint32_t *addr;
  48257. +
  48258. + if (ep->is_in == 1) {
  48259. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  48260. + } else {
  48261. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  48262. + }
  48263. +
  48264. + /* disable the ep */
  48265. + depctl.d32 = DWC_READ_REG32(addr);
  48266. +
  48267. + depctl.b.epdis = 1;
  48268. + depctl.b.snak = 1;
  48269. +
  48270. + DWC_WRITE_REG32(addr, depctl.d32);
  48271. +
  48272. + if (core_if->dma_desc_enable &&
  48273. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  48274. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  48275. + ep->iso_dma_desc_addr,
  48276. + ep->desc_cnt * 2);
  48277. + }
  48278. +
  48279. + /* reset varibales */
  48280. + ep->dma_addr0 = 0;
  48281. + ep->dma_addr1 = 0;
  48282. + ep->xfer_buff0 = 0;
  48283. + ep->xfer_buff1 = 0;
  48284. + ep->data_per_frame = 0;
  48285. + ep->data_pattern_frame = 0;
  48286. + ep->sync_frame = 0;
  48287. + ep->buf_proc_intrvl = 0;
  48288. + ep->bInterval = 0;
  48289. + ep->proc_buf_num = 0;
  48290. + ep->pkt_per_frm = 0;
  48291. + ep->pkt_per_frm = 0;
  48292. + ep->desc_cnt = 0;
  48293. + ep->iso_desc_addr = 0;
  48294. + ep->iso_dma_desc_addr = 0;
  48295. +}
  48296. +
  48297. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  48298. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  48299. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  48300. + int data_per_frame, int start_frame,
  48301. + int buf_proc_intrvl, void *req_handle,
  48302. + int atomic_alloc)
  48303. +{
  48304. + dwc_otg_pcd_ep_t *ep;
  48305. + dwc_irqflags_t flags = 0;
  48306. + dwc_ep_t *dwc_ep;
  48307. + int32_t frm_data;
  48308. + dsts_data_t dsts;
  48309. + dwc_otg_core_if_t *core_if;
  48310. +
  48311. + ep = get_ep_from_handle(pcd, ep_handle);
  48312. +
  48313. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  48314. + DWC_WARN("bad ep\n");
  48315. + return -DWC_E_INVALID;
  48316. + }
  48317. +
  48318. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  48319. + core_if = GET_CORE_IF(pcd);
  48320. + dwc_ep = &ep->dwc_ep;
  48321. +
  48322. + if (ep->iso_req_handle) {
  48323. + DWC_WARN("ISO request in progress\n");
  48324. + }
  48325. +
  48326. + dwc_ep->dma_addr0 = dma0;
  48327. + dwc_ep->dma_addr1 = dma1;
  48328. +
  48329. + dwc_ep->xfer_buff0 = buf0;
  48330. + dwc_ep->xfer_buff1 = buf1;
  48331. +
  48332. + dwc_ep->data_per_frame = data_per_frame;
  48333. +
  48334. + /** @todo - pattern data support is to be implemented in the future */
  48335. + dwc_ep->data_pattern_frame = dp_frame;
  48336. + dwc_ep->sync_frame = sync_frame;
  48337. +
  48338. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  48339. +
  48340. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  48341. +
  48342. + dwc_ep->proc_buf_num = 0;
  48343. +
  48344. + dwc_ep->pkt_per_frm = 0;
  48345. + frm_data = ep->dwc_ep.data_per_frame;
  48346. + while (frm_data > 0) {
  48347. + dwc_ep->pkt_per_frm++;
  48348. + frm_data -= ep->dwc_ep.maxpacket;
  48349. + }
  48350. +
  48351. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48352. +
  48353. + if (start_frame == -1) {
  48354. + dwc_ep->next_frame = dsts.b.soffn + 1;
  48355. + if (dwc_ep->bInterval != 1) {
  48356. + dwc_ep->next_frame =
  48357. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  48358. + dwc_ep->next_frame %
  48359. + dwc_ep->bInterval);
  48360. + }
  48361. + } else {
  48362. + dwc_ep->next_frame = start_frame;
  48363. + }
  48364. +
  48365. + if (!core_if->pti_enh_enable) {
  48366. + dwc_ep->pkt_cnt =
  48367. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48368. + dwc_ep->bInterval;
  48369. + } else {
  48370. + dwc_ep->pkt_cnt =
  48371. + (dwc_ep->data_per_frame *
  48372. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  48373. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  48374. + }
  48375. +
  48376. + if (core_if->dma_desc_enable) {
  48377. + dwc_ep->desc_cnt =
  48378. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48379. + dwc_ep->bInterval;
  48380. + }
  48381. +
  48382. + if (atomic_alloc) {
  48383. + dwc_ep->pkt_info =
  48384. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48385. + } else {
  48386. + dwc_ep->pkt_info =
  48387. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48388. + }
  48389. + if (!dwc_ep->pkt_info) {
  48390. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48391. + return -DWC_E_NO_MEMORY;
  48392. + }
  48393. + if (core_if->pti_enh_enable) {
  48394. + dwc_memset(dwc_ep->pkt_info, 0,
  48395. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48396. + }
  48397. +
  48398. + dwc_ep->cur_pkt = 0;
  48399. + ep->iso_req_handle = req_handle;
  48400. +
  48401. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48402. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  48403. + return 0;
  48404. +}
  48405. +
  48406. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  48407. + void *req_handle)
  48408. +{
  48409. + dwc_irqflags_t flags = 0;
  48410. + dwc_otg_pcd_ep_t *ep;
  48411. + dwc_ep_t *dwc_ep;
  48412. +
  48413. + ep = get_ep_from_handle(pcd, ep_handle);
  48414. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  48415. + DWC_WARN("bad ep\n");
  48416. + return -DWC_E_INVALID;
  48417. + }
  48418. + dwc_ep = &ep->dwc_ep;
  48419. +
  48420. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  48421. +
  48422. + DWC_FREE(dwc_ep->pkt_info);
  48423. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  48424. + if (ep->iso_req_handle != req_handle) {
  48425. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48426. + return -DWC_E_INVALID;
  48427. + }
  48428. +
  48429. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48430. +
  48431. + ep->iso_req_handle = 0;
  48432. + return 0;
  48433. +}
  48434. +
  48435. +/**
  48436. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  48437. + * for Isochronous EPs
  48438. + *
  48439. + * - Every time a sync period completes this function is called to
  48440. + * perform data exchange between PCD and gadget
  48441. + */
  48442. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  48443. + void *req_handle)
  48444. +{
  48445. + int i;
  48446. + dwc_ep_t *dwc_ep;
  48447. +
  48448. + dwc_ep = &ep->dwc_ep;
  48449. +
  48450. + DWC_SPINUNLOCK(ep->pcd->lock);
  48451. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  48452. + dwc_ep->proc_buf_num ^ 0x1);
  48453. + DWC_SPINLOCK(ep->pcd->lock);
  48454. +
  48455. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  48456. + dwc_ep->pkt_info[i].status = 0;
  48457. + dwc_ep->pkt_info[i].offset = 0;
  48458. + dwc_ep->pkt_info[i].length = 0;
  48459. + }
  48460. +}
  48461. +
  48462. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  48463. + void *iso_req_handle)
  48464. +{
  48465. + dwc_otg_pcd_ep_t *ep;
  48466. + dwc_ep_t *dwc_ep;
  48467. +
  48468. + ep = get_ep_from_handle(pcd, ep_handle);
  48469. + if (!ep->desc || ep->dwc_ep.num == 0) {
  48470. + DWC_WARN("bad ep\n");
  48471. + return -DWC_E_INVALID;
  48472. + }
  48473. + dwc_ep = &ep->dwc_ep;
  48474. +
  48475. + return dwc_ep->pkt_cnt;
  48476. +}
  48477. +
  48478. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  48479. + void *iso_req_handle, int packet,
  48480. + int *status, int *actual, int *offset)
  48481. +{
  48482. + dwc_otg_pcd_ep_t *ep;
  48483. + dwc_ep_t *dwc_ep;
  48484. +
  48485. + ep = get_ep_from_handle(pcd, ep_handle);
  48486. + if (!ep)
  48487. + DWC_WARN("bad ep\n");
  48488. +
  48489. + dwc_ep = &ep->dwc_ep;
  48490. +
  48491. + *status = dwc_ep->pkt_info[packet].status;
  48492. + *actual = dwc_ep->pkt_info[packet].length;
  48493. + *offset = dwc_ep->pkt_info[packet].offset;
  48494. +}
  48495. +
  48496. +#endif /* DWC_EN_ISOC */
  48497. +
  48498. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  48499. + uint32_t is_in, uint32_t ep_num)
  48500. +{
  48501. + /* Init EP structure */
  48502. + pcd_ep->desc = 0;
  48503. + pcd_ep->pcd = pcd;
  48504. + pcd_ep->stopped = 1;
  48505. + pcd_ep->queue_sof = 0;
  48506. +
  48507. + /* Init DWC ep structure */
  48508. + pcd_ep->dwc_ep.is_in = is_in;
  48509. + pcd_ep->dwc_ep.num = ep_num;
  48510. + pcd_ep->dwc_ep.active = 0;
  48511. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  48512. + /* Control until ep is actvated */
  48513. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  48514. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  48515. + pcd_ep->dwc_ep.dma_addr = 0;
  48516. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  48517. + pcd_ep->dwc_ep.xfer_buff = 0;
  48518. + pcd_ep->dwc_ep.xfer_len = 0;
  48519. + pcd_ep->dwc_ep.xfer_count = 0;
  48520. + pcd_ep->dwc_ep.sent_zlp = 0;
  48521. + pcd_ep->dwc_ep.total_len = 0;
  48522. + pcd_ep->dwc_ep.desc_addr = 0;
  48523. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  48524. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  48525. +}
  48526. +
  48527. +/**
  48528. + * Initialize ep's
  48529. + */
  48530. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  48531. +{
  48532. + int i;
  48533. + uint32_t hwcfg1;
  48534. + dwc_otg_pcd_ep_t *ep;
  48535. + int in_ep_cntr, out_ep_cntr;
  48536. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  48537. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  48538. +
  48539. + /**
  48540. + * Initialize the EP0 structure.
  48541. + */
  48542. + ep = &pcd->ep0;
  48543. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  48544. +
  48545. + in_ep_cntr = 0;
  48546. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  48547. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  48548. + if ((hwcfg1 & 0x1) == 0) {
  48549. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  48550. + in_ep_cntr++;
  48551. + /**
  48552. + * @todo NGS: Add direction to EP, based on contents
  48553. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  48554. + * sprintf(";r
  48555. + */
  48556. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  48557. +
  48558. + DWC_CIRCLEQ_INIT(&ep->queue);
  48559. + }
  48560. + hwcfg1 >>= 2;
  48561. + }
  48562. +
  48563. + out_ep_cntr = 0;
  48564. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  48565. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  48566. + if ((hwcfg1 & 0x1) == 0) {
  48567. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  48568. + out_ep_cntr++;
  48569. + /**
  48570. + * @todo NGS: Add direction to EP, based on contents
  48571. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  48572. + * sprintf(";r
  48573. + */
  48574. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  48575. + DWC_CIRCLEQ_INIT(&ep->queue);
  48576. + }
  48577. + hwcfg1 >>= 2;
  48578. + }
  48579. +
  48580. + pcd->ep0state = EP0_DISCONNECT;
  48581. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  48582. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  48583. +}
  48584. +
  48585. +/**
  48586. + * This function is called when the SRP timer expires. The SRP should
  48587. + * complete within 6 seconds.
  48588. + */
  48589. +static void srp_timeout(void *ptr)
  48590. +{
  48591. + gotgctl_data_t gotgctl;
  48592. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  48593. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  48594. +
  48595. + gotgctl.d32 = DWC_READ_REG32(addr);
  48596. +
  48597. + core_if->srp_timer_started = 0;
  48598. +
  48599. + if (core_if->adp_enable) {
  48600. + if (gotgctl.b.bsesvld == 0) {
  48601. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  48602. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  48603. + /* Power off the core */
  48604. + if (core_if->power_down == 2) {
  48605. + gpwrdn.b.pwrdnswtch = 1;
  48606. + DWC_MODIFY_REG32(&core_if->
  48607. + core_global_regs->gpwrdn,
  48608. + gpwrdn.d32, 0);
  48609. + }
  48610. +
  48611. + gpwrdn.d32 = 0;
  48612. + gpwrdn.b.pmuintsel = 1;
  48613. + gpwrdn.b.pmuactv = 1;
  48614. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  48615. + gpwrdn.d32);
  48616. + dwc_otg_adp_probe_start(core_if);
  48617. + } else {
  48618. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  48619. + core_if->op_state = B_PERIPHERAL;
  48620. + dwc_otg_core_init(core_if);
  48621. + dwc_otg_enable_global_interrupts(core_if);
  48622. + cil_pcd_start(core_if);
  48623. + }
  48624. + }
  48625. +
  48626. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  48627. + (core_if->core_params->i2c_enable)) {
  48628. + DWC_PRINTF("SRP Timeout\n");
  48629. +
  48630. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  48631. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  48632. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  48633. + }
  48634. +
  48635. + /* Clear Session Request */
  48636. + gotgctl.d32 = 0;
  48637. + gotgctl.b.sesreq = 1;
  48638. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  48639. + gotgctl.d32, 0);
  48640. +
  48641. + core_if->srp_success = 0;
  48642. + } else {
  48643. + __DWC_ERROR("Device not connected/responding\n");
  48644. + gotgctl.b.sesreq = 0;
  48645. + DWC_WRITE_REG32(addr, gotgctl.d32);
  48646. + }
  48647. + } else if (gotgctl.b.sesreq) {
  48648. + DWC_PRINTF("SRP Timeout\n");
  48649. +
  48650. + __DWC_ERROR("Device not connected/responding\n");
  48651. + gotgctl.b.sesreq = 0;
  48652. + DWC_WRITE_REG32(addr, gotgctl.d32);
  48653. + } else {
  48654. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  48655. + }
  48656. +}
  48657. +
  48658. +/**
  48659. + * Tasklet
  48660. + *
  48661. + */
  48662. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  48663. +
  48664. +static void start_xfer_tasklet_func(void *data)
  48665. +{
  48666. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  48667. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  48668. +
  48669. + int i;
  48670. + depctl_data_t diepctl;
  48671. +
  48672. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  48673. +
  48674. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  48675. +
  48676. + if (pcd->ep0.queue_sof) {
  48677. + pcd->ep0.queue_sof = 0;
  48678. + start_next_request(&pcd->ep0);
  48679. + // break;
  48680. + }
  48681. +
  48682. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  48683. + depctl_data_t diepctl;
  48684. + diepctl.d32 =
  48685. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  48686. +
  48687. + if (pcd->in_ep[i].queue_sof) {
  48688. + pcd->in_ep[i].queue_sof = 0;
  48689. + start_next_request(&pcd->in_ep[i]);
  48690. + // break;
  48691. + }
  48692. + }
  48693. +
  48694. + return;
  48695. +}
  48696. +
  48697. +/**
  48698. + * This function initialized the PCD portion of the driver.
  48699. + *
  48700. + */
  48701. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if)
  48702. +{
  48703. + dwc_otg_pcd_t *pcd = NULL;
  48704. + dwc_otg_dev_if_t *dev_if;
  48705. + int i;
  48706. +
  48707. + /*
  48708. + * Allocate PCD structure
  48709. + */
  48710. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  48711. +
  48712. + if (pcd == NULL) {
  48713. + return NULL;
  48714. + }
  48715. +
  48716. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  48717. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
  48718. +#else
  48719. + pcd->lock = DWC_SPINLOCK_ALLOC();
  48720. +#endif
  48721. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  48722. + pcd, core_if);//GRAYG
  48723. + if (!pcd->lock) {
  48724. + DWC_ERROR("Could not allocate lock for pcd");
  48725. + DWC_FREE(pcd);
  48726. + return NULL;
  48727. + }
  48728. + /* Set core_if's lock pointer to hcd->lock */
  48729. + core_if->lock = pcd->lock;
  48730. + pcd->core_if = core_if;
  48731. +
  48732. + dev_if = core_if->dev_if;
  48733. + dev_if->isoc_ep = NULL;
  48734. +
  48735. + if (core_if->hwcfg4.b.ded_fifo_en) {
  48736. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  48737. + } else {
  48738. + DWC_PRINTF("Shared Tx FIFO mode\n");
  48739. + }
  48740. +
  48741. + /*
  48742. + * Initialized the Core for Device mode here if there is nod ADP support.
  48743. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  48744. + */
  48745. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  48746. + dwc_otg_core_dev_init(core_if);
  48747. + }
  48748. +
  48749. + /*
  48750. + * Register the PCD Callbacks.
  48751. + */
  48752. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  48753. +
  48754. + /*
  48755. + * Initialize the DMA buffer for SETUP packets
  48756. + */
  48757. + if (GET_CORE_IF(pcd)->dma_enable) {
  48758. + pcd->setup_pkt =
  48759. + DWC_DMA_ALLOC(sizeof(*pcd->setup_pkt) * 5,
  48760. + &pcd->setup_pkt_dma_handle);
  48761. + if (pcd->setup_pkt == NULL) {
  48762. + DWC_FREE(pcd);
  48763. + return NULL;
  48764. + }
  48765. +
  48766. + pcd->status_buf =
  48767. + DWC_DMA_ALLOC(sizeof(uint16_t),
  48768. + &pcd->status_buf_dma_handle);
  48769. + if (pcd->status_buf == NULL) {
  48770. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  48771. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  48772. + DWC_FREE(pcd);
  48773. + return NULL;
  48774. + }
  48775. +
  48776. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  48777. + dev_if->setup_desc_addr[0] =
  48778. + dwc_otg_ep_alloc_desc_chain
  48779. + (&dev_if->dma_setup_desc_addr[0], 1);
  48780. + dev_if->setup_desc_addr[1] =
  48781. + dwc_otg_ep_alloc_desc_chain
  48782. + (&dev_if->dma_setup_desc_addr[1], 1);
  48783. + dev_if->in_desc_addr =
  48784. + dwc_otg_ep_alloc_desc_chain
  48785. + (&dev_if->dma_in_desc_addr, 1);
  48786. + dev_if->out_desc_addr =
  48787. + dwc_otg_ep_alloc_desc_chain
  48788. + (&dev_if->dma_out_desc_addr, 1);
  48789. + pcd->data_terminated = 0;
  48790. +
  48791. + if (dev_if->setup_desc_addr[0] == 0
  48792. + || dev_if->setup_desc_addr[1] == 0
  48793. + || dev_if->in_desc_addr == 0
  48794. + || dev_if->out_desc_addr == 0) {
  48795. +
  48796. + if (dev_if->out_desc_addr)
  48797. + dwc_otg_ep_free_desc_chain
  48798. + (dev_if->out_desc_addr,
  48799. + dev_if->dma_out_desc_addr, 1);
  48800. + if (dev_if->in_desc_addr)
  48801. + dwc_otg_ep_free_desc_chain
  48802. + (dev_if->in_desc_addr,
  48803. + dev_if->dma_in_desc_addr, 1);
  48804. + if (dev_if->setup_desc_addr[1])
  48805. + dwc_otg_ep_free_desc_chain
  48806. + (dev_if->setup_desc_addr[1],
  48807. + dev_if->dma_setup_desc_addr[1], 1);
  48808. + if (dev_if->setup_desc_addr[0])
  48809. + dwc_otg_ep_free_desc_chain
  48810. + (dev_if->setup_desc_addr[0],
  48811. + dev_if->dma_setup_desc_addr[0], 1);
  48812. +
  48813. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  48814. + pcd->setup_pkt,
  48815. + pcd->setup_pkt_dma_handle);
  48816. + DWC_DMA_FREE(sizeof(*pcd->status_buf),
  48817. + pcd->status_buf,
  48818. + pcd->status_buf_dma_handle);
  48819. +
  48820. + DWC_FREE(pcd);
  48821. +
  48822. + return NULL;
  48823. + }
  48824. + }
  48825. + } else {
  48826. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  48827. + if (pcd->setup_pkt == NULL) {
  48828. + DWC_FREE(pcd);
  48829. + return NULL;
  48830. + }
  48831. +
  48832. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  48833. + if (pcd->status_buf == NULL) {
  48834. + DWC_FREE(pcd->setup_pkt);
  48835. + DWC_FREE(pcd);
  48836. + return NULL;
  48837. + }
  48838. + }
  48839. +
  48840. + dwc_otg_pcd_reinit(pcd);
  48841. +
  48842. + /* Allocate the cfi object for the PCD */
  48843. +#ifdef DWC_UTE_CFI
  48844. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  48845. + if (NULL == pcd->cfi)
  48846. + goto fail;
  48847. + if (init_cfi(pcd->cfi)) {
  48848. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  48849. + goto fail;
  48850. + }
  48851. +#endif
  48852. +
  48853. + /* Initialize tasklets */
  48854. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  48855. + start_xfer_tasklet_func, pcd);
  48856. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  48857. + do_test_mode, pcd);
  48858. +
  48859. + /* Initialize SRP timer */
  48860. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  48861. +
  48862. + if (core_if->core_params->dev_out_nak) {
  48863. + /**
  48864. + * Initialize xfer timeout timer. Implemented for
  48865. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  48866. + */
  48867. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  48868. + pcd->core_if->ep_xfer_timer[i] =
  48869. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  48870. + &pcd->core_if->ep_xfer_info[i]);
  48871. + }
  48872. + }
  48873. +
  48874. + return pcd;
  48875. +#ifdef DWC_UTE_CFI
  48876. +fail:
  48877. +#endif
  48878. + if (pcd->setup_pkt)
  48879. + DWC_FREE(pcd->setup_pkt);
  48880. + if (pcd->status_buf)
  48881. + DWC_FREE(pcd->status_buf);
  48882. +#ifdef DWC_UTE_CFI
  48883. + if (pcd->cfi)
  48884. + DWC_FREE(pcd->cfi);
  48885. +#endif
  48886. + if (pcd)
  48887. + DWC_FREE(pcd);
  48888. + return NULL;
  48889. +
  48890. +}
  48891. +
  48892. +/**
  48893. + * Remove PCD specific data
  48894. + */
  48895. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  48896. +{
  48897. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  48898. + int i;
  48899. + if (pcd->core_if->core_params->dev_out_nak) {
  48900. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  48901. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  48902. + pcd->core_if->ep_xfer_info[i].state = 0;
  48903. + }
  48904. + }
  48905. +
  48906. + if (GET_CORE_IF(pcd)->dma_enable) {
  48907. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  48908. + pcd->setup_pkt_dma_handle);
  48909. + DWC_DMA_FREE(sizeof(uint16_t), pcd->status_buf,
  48910. + pcd->status_buf_dma_handle);
  48911. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  48912. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[0],
  48913. + dev_if->dma_setup_desc_addr
  48914. + [0], 1);
  48915. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[1],
  48916. + dev_if->dma_setup_desc_addr
  48917. + [1], 1);
  48918. + dwc_otg_ep_free_desc_chain(dev_if->in_desc_addr,
  48919. + dev_if->dma_in_desc_addr, 1);
  48920. + dwc_otg_ep_free_desc_chain(dev_if->out_desc_addr,
  48921. + dev_if->dma_out_desc_addr,
  48922. + 1);
  48923. + }
  48924. + } else {
  48925. + DWC_FREE(pcd->setup_pkt);
  48926. + DWC_FREE(pcd->status_buf);
  48927. + }
  48928. + DWC_SPINLOCK_FREE(pcd->lock);
  48929. + /* Set core_if's lock pointer to NULL */
  48930. + pcd->core_if->lock = NULL;
  48931. +
  48932. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  48933. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  48934. + if (pcd->core_if->core_params->dev_out_nak) {
  48935. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  48936. + if (pcd->core_if->ep_xfer_timer[i]) {
  48937. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  48938. + }
  48939. + }
  48940. + }
  48941. +
  48942. +/* Release the CFI object's dynamic memory */
  48943. +#ifdef DWC_UTE_CFI
  48944. + if (pcd->cfi->ops.release) {
  48945. + pcd->cfi->ops.release(pcd->cfi);
  48946. + }
  48947. +#endif
  48948. +
  48949. + DWC_FREE(pcd);
  48950. +}
  48951. +
  48952. +/**
  48953. + * Returns whether registered pcd is dual speed or not
  48954. + */
  48955. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  48956. +{
  48957. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  48958. +
  48959. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  48960. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  48961. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  48962. + (core_if->core_params->ulpi_fs_ls))) {
  48963. + return 0;
  48964. + }
  48965. +
  48966. + return 1;
  48967. +}
  48968. +
  48969. +/**
  48970. + * Returns whether registered pcd is OTG capable or not
  48971. + */
  48972. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  48973. +{
  48974. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  48975. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  48976. +
  48977. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  48978. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  48979. + return 0;
  48980. + }
  48981. +
  48982. + return 1;
  48983. +}
  48984. +
  48985. +/**
  48986. + * This function assigns periodic Tx FIFO to an periodic EP
  48987. + * in shared Tx FIFO mode
  48988. + */
  48989. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  48990. +{
  48991. + uint32_t TxMsk = 1;
  48992. + int i;
  48993. +
  48994. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  48995. + if ((TxMsk & core_if->tx_msk) == 0) {
  48996. + core_if->tx_msk |= TxMsk;
  48997. + return i + 1;
  48998. + }
  48999. + TxMsk <<= 1;
  49000. + }
  49001. + return 0;
  49002. +}
  49003. +
  49004. +/**
  49005. + * This function assigns periodic Tx FIFO to an periodic EP
  49006. + * in shared Tx FIFO mode
  49007. + */
  49008. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  49009. +{
  49010. + uint32_t PerTxMsk = 1;
  49011. + int i;
  49012. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  49013. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  49014. + core_if->p_tx_msk |= PerTxMsk;
  49015. + return i + 1;
  49016. + }
  49017. + PerTxMsk <<= 1;
  49018. + }
  49019. + return 0;
  49020. +}
  49021. +
  49022. +/**
  49023. + * This function releases periodic Tx FIFO
  49024. + * in shared Tx FIFO mode
  49025. + */
  49026. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  49027. + uint32_t fifo_num)
  49028. +{
  49029. + core_if->p_tx_msk =
  49030. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  49031. +}
  49032. +
  49033. +/**
  49034. + * This function releases periodic Tx FIFO
  49035. + * in shared Tx FIFO mode
  49036. + */
  49037. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  49038. +{
  49039. + core_if->tx_msk =
  49040. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  49041. +}
  49042. +
  49043. +/**
  49044. + * This function is being called from gadget
  49045. + * to enable PCD endpoint.
  49046. + */
  49047. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  49048. + const uint8_t * ep_desc, void *usb_ep)
  49049. +{
  49050. + int num, dir;
  49051. + dwc_otg_pcd_ep_t *ep = NULL;
  49052. + const usb_endpoint_descriptor_t *desc;
  49053. + dwc_irqflags_t flags;
  49054. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  49055. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  49056. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  49057. + int retval = 0;
  49058. + int i, epcount;
  49059. +
  49060. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  49061. +
  49062. + if (!desc) {
  49063. + pcd->ep0.priv = usb_ep;
  49064. + ep = &pcd->ep0;
  49065. + retval = -DWC_E_INVALID;
  49066. + goto out;
  49067. + }
  49068. +
  49069. + num = UE_GET_ADDR(desc->bEndpointAddress);
  49070. + dir = UE_GET_DIR(desc->bEndpointAddress);
  49071. +
  49072. + if (!desc->wMaxPacketSize) {
  49073. + DWC_WARN("bad maxpacketsize\n");
  49074. + retval = -DWC_E_INVALID;
  49075. + goto out;
  49076. + }
  49077. +
  49078. + if (dir == UE_DIR_IN) {
  49079. + epcount = pcd->core_if->dev_if->num_in_eps;
  49080. + for (i = 0; i < epcount; i++) {
  49081. + if (num == pcd->in_ep[i].dwc_ep.num) {
  49082. + ep = &pcd->in_ep[i];
  49083. + break;
  49084. + }
  49085. + }
  49086. + } else {
  49087. + epcount = pcd->core_if->dev_if->num_out_eps;
  49088. + for (i = 0; i < epcount; i++) {
  49089. + if (num == pcd->out_ep[i].dwc_ep.num) {
  49090. + ep = &pcd->out_ep[i];
  49091. + break;
  49092. + }
  49093. + }
  49094. + }
  49095. +
  49096. + if (!ep) {
  49097. + DWC_WARN("bad address\n");
  49098. + retval = -DWC_E_INVALID;
  49099. + goto out;
  49100. + }
  49101. +
  49102. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49103. +
  49104. + ep->desc = desc;
  49105. + ep->priv = usb_ep;
  49106. +
  49107. + /*
  49108. + * Activate the EP
  49109. + */
  49110. + ep->stopped = 0;
  49111. +
  49112. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  49113. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  49114. +
  49115. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  49116. +
  49117. + if (ep->dwc_ep.is_in) {
  49118. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49119. + ep->dwc_ep.tx_fifo_num = 0;
  49120. +
  49121. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  49122. + /*
  49123. + * if ISOC EP then assign a Periodic Tx FIFO.
  49124. + */
  49125. + ep->dwc_ep.tx_fifo_num =
  49126. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  49127. + }
  49128. + } else {
  49129. + /*
  49130. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  49131. + */
  49132. + ep->dwc_ep.tx_fifo_num =
  49133. + assign_tx_fifo(GET_CORE_IF(pcd));
  49134. + }
  49135. +
  49136. + /* Calculating EP info controller base address */
  49137. + if (ep->dwc_ep.tx_fifo_num
  49138. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49139. + gdfifocfg.d32 =
  49140. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  49141. + core_global_regs->gdfifocfg);
  49142. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  49143. + dptxfsiz.d32 =
  49144. + (DWC_READ_REG32
  49145. + (&GET_CORE_IF(pcd)->core_global_regs->
  49146. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  49147. + gdfifocfg.b.epinfobase =
  49148. + gdfifocfgbase.d32 + dptxfsiz.d32;
  49149. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  49150. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  49151. + core_global_regs->gdfifocfg,
  49152. + gdfifocfg.d32);
  49153. + }
  49154. + }
  49155. + }
  49156. + /* Set initial data PID. */
  49157. + if (ep->dwc_ep.type == UE_BULK) {
  49158. + ep->dwc_ep.data_pid_start = 0;
  49159. + }
  49160. +
  49161. + /* Alloc DMA Descriptors */
  49162. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49163. +#ifndef DWC_UTE_PER_IO
  49164. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  49165. +#endif
  49166. + ep->dwc_ep.desc_addr =
  49167. + dwc_otg_ep_alloc_desc_chain(&ep->
  49168. + dwc_ep.dma_desc_addr,
  49169. + MAX_DMA_DESC_CNT);
  49170. + if (!ep->dwc_ep.desc_addr) {
  49171. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  49172. + __func__);
  49173. + retval = -DWC_E_SHUTDOWN;
  49174. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49175. + goto out;
  49176. + }
  49177. +#ifndef DWC_UTE_PER_IO
  49178. + }
  49179. +#endif
  49180. + }
  49181. +
  49182. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  49183. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  49184. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  49185. +#ifdef DWC_UTE_PER_IO
  49186. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  49187. +#endif
  49188. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  49189. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  49190. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  49191. + }
  49192. +
  49193. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  49194. +
  49195. +#ifdef DWC_UTE_CFI
  49196. + if (pcd->cfi->ops.ep_enable) {
  49197. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  49198. + }
  49199. +#endif
  49200. +
  49201. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49202. +
  49203. +out:
  49204. + return retval;
  49205. +}
  49206. +
  49207. +/**
  49208. + * This function is being called from gadget
  49209. + * to disable PCD endpoint.
  49210. + */
  49211. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  49212. +{
  49213. + dwc_otg_pcd_ep_t *ep;
  49214. + dwc_irqflags_t flags;
  49215. + dwc_otg_dev_dma_desc_t *desc_addr;
  49216. + dwc_dma_t dma_desc_addr;
  49217. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  49218. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  49219. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  49220. +
  49221. + ep = get_ep_from_handle(pcd, ep_handle);
  49222. +
  49223. + if (!ep || !ep->desc) {
  49224. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  49225. + return -DWC_E_INVALID;
  49226. + }
  49227. +
  49228. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49229. +
  49230. + dwc_otg_request_nuke(ep);
  49231. +
  49232. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  49233. + if (pcd->core_if->core_params->dev_out_nak) {
  49234. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  49235. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  49236. + }
  49237. + ep->desc = NULL;
  49238. + ep->stopped = 1;
  49239. +
  49240. + gdfifocfg.d32 =
  49241. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  49242. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  49243. +
  49244. + if (ep->dwc_ep.is_in) {
  49245. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49246. + /* Flush the Tx FIFO */
  49247. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  49248. + ep->dwc_ep.tx_fifo_num);
  49249. + }
  49250. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  49251. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  49252. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49253. + /* Decreasing EPinfo Base Addr */
  49254. + dptxfsiz.d32 =
  49255. + (DWC_READ_REG32
  49256. + (&GET_CORE_IF(pcd)->
  49257. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  49258. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  49259. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  49260. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  49261. + gdfifocfg.d32);
  49262. + }
  49263. + }
  49264. + }
  49265. +
  49266. + /* Free DMA Descriptors */
  49267. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49268. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  49269. + desc_addr = ep->dwc_ep.desc_addr;
  49270. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  49271. +
  49272. + /* Cannot call dma_free_coherent() with IRQs disabled */
  49273. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49274. + dwc_otg_ep_free_desc_chain(desc_addr, dma_desc_addr,
  49275. + MAX_DMA_DESC_CNT);
  49276. +
  49277. + goto out_unlocked;
  49278. + }
  49279. + }
  49280. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49281. +
  49282. +out_unlocked:
  49283. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  49284. + ep->dwc_ep.is_in ? "IN" : "OUT");
  49285. + return 0;
  49286. +
  49287. +}
  49288. +
  49289. +/******************************************************************************/
  49290. +#ifdef DWC_UTE_PER_IO
  49291. +
  49292. +/**
  49293. + * Free the request and its extended parts
  49294. + *
  49295. + */
  49296. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  49297. +{
  49298. + DWC_FREE(req->ext_req.per_io_frame_descs);
  49299. + DWC_FREE(req);
  49300. +}
  49301. +
  49302. +/**
  49303. + * Start the next request in the endpoint's queue.
  49304. + *
  49305. + */
  49306. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  49307. + dwc_otg_pcd_ep_t * ep)
  49308. +{
  49309. + int i;
  49310. + dwc_otg_pcd_request_t *req = NULL;
  49311. + dwc_ep_t *dwcep = NULL;
  49312. + struct dwc_iso_xreq_port *ereq = NULL;
  49313. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  49314. + uint16_t nat;
  49315. + depctl_data_t diepctl;
  49316. +
  49317. + dwcep = &ep->dwc_ep;
  49318. +
  49319. + if (dwcep->xiso_active_xfers > 0) {
  49320. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  49321. + DWC_WARN("There are currently active transfers for EP%d \
  49322. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  49323. + dwcep->xiso_queued_xfers);
  49324. +#endif
  49325. + return 0;
  49326. + }
  49327. +
  49328. + nat = UGETW(ep->desc->wMaxPacketSize);
  49329. + nat = (nat >> 11) & 0x03;
  49330. +
  49331. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49332. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  49333. + ereq = &req->ext_req;
  49334. + ep->stopped = 0;
  49335. +
  49336. + /* Get the frame number */
  49337. + dwcep->xiso_frame_num =
  49338. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  49339. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  49340. +
  49341. + ddesc_iso = ereq->per_io_frame_descs;
  49342. +
  49343. + if (dwcep->is_in) {
  49344. + /* Setup DMA Descriptor chain for IN Isoc request */
  49345. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49346. + //if ((i % (nat + 1)) == 0)
  49347. + if ( i > 0 )
  49348. + dwcep->xiso_frame_num =
  49349. + (dwcep->xiso_bInterval +
  49350. + dwcep->xiso_frame_num) & 0x3FFF;
  49351. + dwcep->desc_addr[i].buf =
  49352. + req->dma + ddesc_iso[i].offset;
  49353. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  49354. + ddesc_iso[i].length;
  49355. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  49356. + dwcep->xiso_frame_num;
  49357. + dwcep->desc_addr[i].status.b_iso_in.bs =
  49358. + BS_HOST_READY;
  49359. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  49360. + dwcep->desc_addr[i].status.b_iso_in.sp =
  49361. + (ddesc_iso[i].length %
  49362. + dwcep->maxpacket) ? 1 : 0;
  49363. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  49364. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  49365. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  49366. +
  49367. + /* Process the last descriptor */
  49368. + if (i == ereq->pio_pkt_count - 1) {
  49369. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  49370. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  49371. + }
  49372. + }
  49373. +
  49374. + /* Setup and start the transfer for this endpoint */
  49375. + dwcep->xiso_active_xfers++;
  49376. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  49377. + in_ep_regs[dwcep->num]->diepdma,
  49378. + dwcep->dma_desc_addr);
  49379. + diepctl.d32 = 0;
  49380. + diepctl.b.epena = 1;
  49381. + diepctl.b.cnak = 1;
  49382. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  49383. + in_ep_regs[dwcep->num]->diepctl, 0,
  49384. + diepctl.d32);
  49385. + } else {
  49386. + /* Setup DMA Descriptor chain for OUT Isoc request */
  49387. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49388. + //if ((i % (nat + 1)) == 0)
  49389. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  49390. + dwcep->xiso_frame_num) & 0x3FFF;
  49391. + dwcep->desc_addr[i].buf =
  49392. + req->dma + ddesc_iso[i].offset;
  49393. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  49394. + ddesc_iso[i].length;
  49395. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  49396. + dwcep->xiso_frame_num;
  49397. + dwcep->desc_addr[i].status.b_iso_out.bs =
  49398. + BS_HOST_READY;
  49399. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  49400. + dwcep->desc_addr[i].status.b_iso_out.sp =
  49401. + (ddesc_iso[i].length %
  49402. + dwcep->maxpacket) ? 1 : 0;
  49403. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  49404. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  49405. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  49406. +
  49407. + /* Process the last descriptor */
  49408. + if (i == ereq->pio_pkt_count - 1) {
  49409. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  49410. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  49411. + }
  49412. + }
  49413. +
  49414. + /* Setup and start the transfer for this endpoint */
  49415. + dwcep->xiso_active_xfers++;
  49416. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  49417. + dev_if->out_ep_regs[dwcep->num]->
  49418. + doepdma, dwcep->dma_desc_addr);
  49419. + diepctl.d32 = 0;
  49420. + diepctl.b.epena = 1;
  49421. + diepctl.b.cnak = 1;
  49422. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49423. + dev_if->out_ep_regs[dwcep->num]->
  49424. + doepctl, 0, diepctl.d32);
  49425. + }
  49426. +
  49427. + } else {
  49428. + ep->stopped = 1;
  49429. + }
  49430. +
  49431. + return 0;
  49432. +}
  49433. +
  49434. +/**
  49435. + * - Remove the request from the queue
  49436. + */
  49437. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  49438. +{
  49439. + dwc_otg_pcd_request_t *req = NULL;
  49440. + struct dwc_iso_xreq_port *ereq = NULL;
  49441. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  49442. + dwc_ep_t *dwcep = NULL;
  49443. + int i;
  49444. +
  49445. + //DWC_DEBUG();
  49446. + dwcep = &ep->dwc_ep;
  49447. +
  49448. + /* Get the first pending request from the queue */
  49449. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49450. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  49451. + if (!req) {
  49452. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  49453. + return;
  49454. + }
  49455. + dwcep->xiso_active_xfers--;
  49456. + dwcep->xiso_queued_xfers--;
  49457. + /* Remove this request from the queue */
  49458. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  49459. + } else {
  49460. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  49461. + return;
  49462. + }
  49463. +
  49464. + ep->stopped = 1;
  49465. + ereq = &req->ext_req;
  49466. + ddesc_iso = ereq->per_io_frame_descs;
  49467. +
  49468. + if (dwcep->xiso_active_xfers < 0) {
  49469. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  49470. + dwcep->xiso_active_xfers);
  49471. + }
  49472. +
  49473. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  49474. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49475. + if (dwcep->is_in) { /* IN endpoints */
  49476. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  49477. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  49478. + ddesc_iso[i].status =
  49479. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  49480. + } else { /* OUT endpoints */
  49481. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  49482. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  49483. + ddesc_iso[i].status =
  49484. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  49485. + }
  49486. + }
  49487. +
  49488. + DWC_SPINUNLOCK(ep->pcd->lock);
  49489. +
  49490. + /* Call the completion function in the non-portable logic */
  49491. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  49492. + &req->ext_req);
  49493. +
  49494. + DWC_SPINLOCK(ep->pcd->lock);
  49495. +
  49496. + /* Free the request - specific freeing needed for extended request object */
  49497. + dwc_pcd_xiso_ereq_free(ep, req);
  49498. +
  49499. + /* Start the next request */
  49500. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  49501. +
  49502. + return;
  49503. +}
  49504. +
  49505. +/**
  49506. + * Create and initialize the Isoc pkt descriptors of the extended request.
  49507. + *
  49508. + */
  49509. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  49510. + void *ereq_nonport,
  49511. + int atomic_alloc)
  49512. +{
  49513. + struct dwc_iso_xreq_port *ereq = NULL;
  49514. + struct dwc_iso_xreq_port *req_mapped = NULL;
  49515. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  49516. + uint32_t pkt_count;
  49517. + int i;
  49518. +
  49519. + ereq = &req->ext_req;
  49520. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  49521. + pkt_count = req_mapped->pio_pkt_count;
  49522. +
  49523. + /* Create the isoc descs */
  49524. + if (atomic_alloc) {
  49525. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  49526. + } else {
  49527. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  49528. + }
  49529. +
  49530. + if (!ipds) {
  49531. + DWC_ERROR("Failed to allocate isoc descriptors");
  49532. + return -DWC_E_NO_MEMORY;
  49533. + }
  49534. +
  49535. + /* Initialize the extended request fields */
  49536. + ereq->per_io_frame_descs = ipds;
  49537. + ereq->error_count = 0;
  49538. + ereq->pio_alloc_pkt_count = pkt_count;
  49539. + ereq->pio_pkt_count = pkt_count;
  49540. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  49541. +
  49542. + /* Init the Isoc descriptors */
  49543. + for (i = 0; i < pkt_count; i++) {
  49544. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  49545. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  49546. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  49547. + ipds[i].actual_length =
  49548. + req_mapped->per_io_frame_descs[i].actual_length;
  49549. + }
  49550. +
  49551. + return 0;
  49552. +}
  49553. +
  49554. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  49555. +{
  49556. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  49557. + int i;
  49558. +
  49559. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  49560. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  49561. + DWC_DEBUG("error_count=%d", ereq->error_count);
  49562. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  49563. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  49564. + DWC_DEBUG("res=%d", ereq->res);
  49565. +
  49566. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49567. + xfd = &ereq->per_io_frame_descs[0];
  49568. + DWC_DEBUG("FD #%d", i);
  49569. +
  49570. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  49571. + DWC_DEBUG("xfd->length=%d", xfd->length);
  49572. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  49573. + DWC_DEBUG("xfd->status=%d", xfd->status);
  49574. + }
  49575. +}
  49576. +
  49577. +/**
  49578. + *
  49579. + */
  49580. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49581. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  49582. + int zero, void *req_handle, int atomic_alloc,
  49583. + void *ereq_nonport)
  49584. +{
  49585. + dwc_otg_pcd_request_t *req = NULL;
  49586. + dwc_otg_pcd_ep_t *ep;
  49587. + dwc_irqflags_t flags;
  49588. + int res;
  49589. +
  49590. + ep = get_ep_from_handle(pcd, ep_handle);
  49591. + if (!ep) {
  49592. + DWC_WARN("bad ep\n");
  49593. + return -DWC_E_INVALID;
  49594. + }
  49595. +
  49596. + /* We support this extension only for DDMA mode */
  49597. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  49598. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  49599. + return -DWC_E_INVALID;
  49600. +
  49601. + /* Create a dwc_otg_pcd_request_t object */
  49602. + if (atomic_alloc) {
  49603. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  49604. + } else {
  49605. + req = DWC_ALLOC(sizeof(*req));
  49606. + }
  49607. +
  49608. + if (!req) {
  49609. + return -DWC_E_NO_MEMORY;
  49610. + }
  49611. +
  49612. + /* Create the Isoc descs for this request which shall be the exact match
  49613. + * of the structure sent to us from the non-portable logic */
  49614. + res =
  49615. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  49616. + if (res) {
  49617. + DWC_WARN("Failed to init the Isoc descriptors");
  49618. + DWC_FREE(req);
  49619. + return res;
  49620. + }
  49621. +
  49622. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49623. +
  49624. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  49625. + req->buf = buf;
  49626. + req->dma = dma_buf;
  49627. + req->length = buflen;
  49628. + req->sent_zlp = zero;
  49629. + req->priv = req_handle;
  49630. +
  49631. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49632. + ep->dwc_ep.dma_addr = dma_buf;
  49633. + ep->dwc_ep.start_xfer_buff = buf;
  49634. + ep->dwc_ep.xfer_buff = buf;
  49635. + ep->dwc_ep.xfer_len = 0;
  49636. + ep->dwc_ep.xfer_count = 0;
  49637. + ep->dwc_ep.sent_zlp = 0;
  49638. + ep->dwc_ep.total_len = buflen;
  49639. +
  49640. + /* Add this request to the tail */
  49641. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49642. + ep->dwc_ep.xiso_queued_xfers++;
  49643. +
  49644. +//DWC_DEBUG("CP_0");
  49645. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  49646. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  49647. +//prn_ext_request(&req->ext_req);
  49648. +
  49649. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49650. +
  49651. + /* If the req->status == ASAP then check if there is any active transfer
  49652. + * for this endpoint. If no active transfers, then get the first entry
  49653. + * from the queue and start that transfer
  49654. + */
  49655. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  49656. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  49657. + if (res) {
  49658. + DWC_WARN("Failed to start the next Isoc transfer");
  49659. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49660. + DWC_FREE(req);
  49661. + return res;
  49662. + }
  49663. + }
  49664. +
  49665. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49666. + return 0;
  49667. +}
  49668. +
  49669. +#endif
  49670. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  49671. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49672. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  49673. + int zero, void *req_handle, int atomic_alloc)
  49674. +{
  49675. + dwc_irqflags_t flags;
  49676. + dwc_otg_pcd_request_t *req;
  49677. + dwc_otg_pcd_ep_t *ep;
  49678. + uint32_t max_transfer;
  49679. +
  49680. + ep = get_ep_from_handle(pcd, ep_handle);
  49681. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  49682. + DWC_WARN("bad ep\n");
  49683. + return -DWC_E_INVALID;
  49684. + }
  49685. +
  49686. + if (atomic_alloc) {
  49687. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  49688. + } else {
  49689. + req = DWC_ALLOC(sizeof(*req));
  49690. + }
  49691. +
  49692. + if (!req) {
  49693. + return -DWC_E_NO_MEMORY;
  49694. + }
  49695. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  49696. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  49697. + if (ep->dwc_ep.num != 0) {
  49698. + DWC_ERROR("queue req %p, len %d buf %p\n",
  49699. + req_handle, buflen, buf);
  49700. + }
  49701. + }
  49702. +
  49703. + req->buf = buf;
  49704. + req->dma = dma_buf;
  49705. + req->length = buflen;
  49706. + req->sent_zlp = zero;
  49707. + req->priv = req_handle;
  49708. + req->dw_align_buf = NULL;
  49709. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  49710. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  49711. + req->dw_align_buf = DWC_DMA_ALLOC(buflen,
  49712. + &req->dw_align_buf_dma);
  49713. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49714. +
  49715. + /*
  49716. + * After adding request to the queue for IN ISOC wait for In Token Received
  49717. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  49718. + * Received when EP is disabled interrupt to obtain starting microframe
  49719. + * (odd/even) start transfer
  49720. + */
  49721. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  49722. + if (req != 0) {
  49723. + depctl_data_t depctl = {.d32 =
  49724. + DWC_READ_REG32(&pcd->core_if->dev_if->
  49725. + in_ep_regs[ep->dwc_ep.num]->
  49726. + diepctl) };
  49727. + ++pcd->request_pending;
  49728. +
  49729. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49730. + if (ep->dwc_ep.is_in) {
  49731. + depctl.b.cnak = 1;
  49732. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  49733. + in_ep_regs[ep->dwc_ep.num]->
  49734. + diepctl, depctl.d32);
  49735. + }
  49736. +
  49737. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49738. + }
  49739. + return 0;
  49740. + }
  49741. +
  49742. + /*
  49743. + * For EP0 IN without premature status, zlp is required?
  49744. + */
  49745. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  49746. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  49747. + //_req->zero = 1;
  49748. + }
  49749. +
  49750. + /* Start the transfer */
  49751. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  49752. + /* EP0 Transfer? */
  49753. + if (ep->dwc_ep.num == 0) {
  49754. + switch (pcd->ep0state) {
  49755. + case EP0_IN_DATA_PHASE:
  49756. + DWC_DEBUGPL(DBG_PCD,
  49757. + "%s ep0: EP0_IN_DATA_PHASE\n",
  49758. + __func__);
  49759. + break;
  49760. +
  49761. + case EP0_OUT_DATA_PHASE:
  49762. + DWC_DEBUGPL(DBG_PCD,
  49763. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  49764. + __func__);
  49765. + if (pcd->request_config) {
  49766. + /* Complete STATUS PHASE */
  49767. + ep->dwc_ep.is_in = 1;
  49768. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  49769. + }
  49770. + break;
  49771. +
  49772. + case EP0_IN_STATUS_PHASE:
  49773. + DWC_DEBUGPL(DBG_PCD,
  49774. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  49775. + __func__);
  49776. + break;
  49777. +
  49778. + default:
  49779. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  49780. + pcd->ep0state);
  49781. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49782. + return -DWC_E_SHUTDOWN;
  49783. + }
  49784. +
  49785. + ep->dwc_ep.dma_addr = dma_buf;
  49786. + ep->dwc_ep.start_xfer_buff = buf;
  49787. + ep->dwc_ep.xfer_buff = buf;
  49788. + ep->dwc_ep.xfer_len = buflen;
  49789. + ep->dwc_ep.xfer_count = 0;
  49790. + ep->dwc_ep.sent_zlp = 0;
  49791. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  49792. +
  49793. + if (zero) {
  49794. + if ((ep->dwc_ep.xfer_len %
  49795. + ep->dwc_ep.maxpacket == 0)
  49796. + && (ep->dwc_ep.xfer_len != 0)) {
  49797. + ep->dwc_ep.sent_zlp = 1;
  49798. + }
  49799. +
  49800. + }
  49801. +
  49802. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  49803. + &ep->dwc_ep);
  49804. + } // non-ep0 endpoints
  49805. + else {
  49806. +#ifdef DWC_UTE_CFI
  49807. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  49808. + /* store the request length */
  49809. + ep->dwc_ep.cfi_req_len = buflen;
  49810. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  49811. + ep, req);
  49812. + } else {
  49813. +#endif
  49814. + max_transfer =
  49815. + GET_CORE_IF(ep->pcd)->core_params->
  49816. + max_transfer_size;
  49817. +
  49818. + /* Setup and start the Transfer */
  49819. + if (req->dw_align_buf){
  49820. + if (ep->dwc_ep.is_in)
  49821. + dwc_memcpy(req->dw_align_buf,
  49822. + buf, buflen);
  49823. + ep->dwc_ep.dma_addr =
  49824. + req->dw_align_buf_dma;
  49825. + ep->dwc_ep.start_xfer_buff =
  49826. + req->dw_align_buf;
  49827. + ep->dwc_ep.xfer_buff =
  49828. + req->dw_align_buf;
  49829. + } else {
  49830. + ep->dwc_ep.dma_addr = dma_buf;
  49831. + ep->dwc_ep.start_xfer_buff = buf;
  49832. + ep->dwc_ep.xfer_buff = buf;
  49833. + }
  49834. + ep->dwc_ep.xfer_len = 0;
  49835. + ep->dwc_ep.xfer_count = 0;
  49836. + ep->dwc_ep.sent_zlp = 0;
  49837. + ep->dwc_ep.total_len = buflen;
  49838. +
  49839. + ep->dwc_ep.maxxfer = max_transfer;
  49840. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49841. + uint32_t out_max_xfer =
  49842. + DDMA_MAX_TRANSFER_SIZE -
  49843. + (DDMA_MAX_TRANSFER_SIZE % 4);
  49844. + if (ep->dwc_ep.is_in) {
  49845. + if (ep->dwc_ep.maxxfer >
  49846. + DDMA_MAX_TRANSFER_SIZE) {
  49847. + ep->dwc_ep.maxxfer =
  49848. + DDMA_MAX_TRANSFER_SIZE;
  49849. + }
  49850. + } else {
  49851. + if (ep->dwc_ep.maxxfer >
  49852. + out_max_xfer) {
  49853. + ep->dwc_ep.maxxfer =
  49854. + out_max_xfer;
  49855. + }
  49856. + }
  49857. + }
  49858. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  49859. + ep->dwc_ep.maxxfer -=
  49860. + (ep->dwc_ep.maxxfer %
  49861. + ep->dwc_ep.maxpacket);
  49862. + }
  49863. +
  49864. + if (zero) {
  49865. + if ((ep->dwc_ep.total_len %
  49866. + ep->dwc_ep.maxpacket == 0)
  49867. + && (ep->dwc_ep.total_len != 0)) {
  49868. + ep->dwc_ep.sent_zlp = 1;
  49869. + }
  49870. + }
  49871. +#ifdef DWC_UTE_CFI
  49872. + }
  49873. +#endif
  49874. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  49875. + &ep->dwc_ep);
  49876. + }
  49877. + }
  49878. +
  49879. + if (req != 0) {
  49880. + ++pcd->request_pending;
  49881. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49882. + if (ep->dwc_ep.is_in && ep->stopped
  49883. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  49884. + /** @todo NGS Create a function for this. */
  49885. + diepmsk_data_t diepmsk = {.d32 = 0 };
  49886. + diepmsk.b.intktxfemp = 1;
  49887. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  49888. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49889. + dev_if->dev_global_regs->diepeachintmsk
  49890. + [ep->dwc_ep.num], 0,
  49891. + diepmsk.d32);
  49892. + } else {
  49893. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49894. + dev_if->dev_global_regs->
  49895. + diepmsk, 0, diepmsk.d32);
  49896. + }
  49897. +
  49898. + }
  49899. + }
  49900. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49901. +
  49902. + return 0;
  49903. +}
  49904. +
  49905. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49906. + void *req_handle)
  49907. +{
  49908. + dwc_irqflags_t flags;
  49909. + dwc_otg_pcd_request_t *req;
  49910. + dwc_otg_pcd_ep_t *ep;
  49911. +
  49912. + ep = get_ep_from_handle(pcd, ep_handle);
  49913. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  49914. + DWC_WARN("bad argument\n");
  49915. + return -DWC_E_INVALID;
  49916. + }
  49917. +
  49918. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49919. +
  49920. + /* make sure it's actually queued on this endpoint */
  49921. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  49922. + if (req->priv == (void *)req_handle) {
  49923. + break;
  49924. + }
  49925. + }
  49926. +
  49927. + if (req->priv != (void *)req_handle) {
  49928. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49929. + return -DWC_E_INVALID;
  49930. + }
  49931. +
  49932. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  49933. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  49934. + } else {
  49935. + req = NULL;
  49936. + }
  49937. +
  49938. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49939. +
  49940. + return req ? 0 : -DWC_E_SHUTDOWN;
  49941. +
  49942. +}
  49943. +
  49944. +/**
  49945. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  49946. + *
  49947. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  49948. + * requests. If the gadget driver clears the halt status, it will
  49949. + * automatically unwedge the endpoint.
  49950. + *
  49951. + * Returns zero on success, else negative DWC error code.
  49952. + */
  49953. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  49954. +{
  49955. + dwc_otg_pcd_ep_t *ep;
  49956. + dwc_irqflags_t flags;
  49957. + int retval = 0;
  49958. +
  49959. + ep = get_ep_from_handle(pcd, ep_handle);
  49960. +
  49961. + if ((!ep->desc && ep != &pcd->ep0) ||
  49962. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  49963. + DWC_WARN("%s, bad ep\n", __func__);
  49964. + return -DWC_E_INVALID;
  49965. + }
  49966. +
  49967. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49968. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49969. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  49970. + ep->dwc_ep.is_in ? "IN" : "OUT");
  49971. + retval = -DWC_E_AGAIN;
  49972. + } else {
  49973. + /* This code needs to be reviewed */
  49974. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  49975. + dtxfsts_data_t txstatus;
  49976. + fifosize_data_t txfifosize;
  49977. +
  49978. + txfifosize.d32 =
  49979. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  49980. + core_global_regs->dtxfsiz[ep->dwc_ep.
  49981. + tx_fifo_num]);
  49982. + txstatus.d32 =
  49983. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  49984. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  49985. + dtxfsts);
  49986. +
  49987. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  49988. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  49989. + retval = -DWC_E_AGAIN;
  49990. + } else {
  49991. + if (ep->dwc_ep.num == 0) {
  49992. + pcd->ep0state = EP0_STALL;
  49993. + }
  49994. +
  49995. + ep->stopped = 1;
  49996. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  49997. + &ep->dwc_ep);
  49998. + }
  49999. + } else {
  50000. + if (ep->dwc_ep.num == 0) {
  50001. + pcd->ep0state = EP0_STALL;
  50002. + }
  50003. +
  50004. + ep->stopped = 1;
  50005. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50006. + }
  50007. + }
  50008. +
  50009. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50010. +
  50011. + return retval;
  50012. +}
  50013. +
  50014. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  50015. +{
  50016. + dwc_otg_pcd_ep_t *ep;
  50017. + dwc_irqflags_t flags;
  50018. + int retval = 0;
  50019. +
  50020. + ep = get_ep_from_handle(pcd, ep_handle);
  50021. +
  50022. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  50023. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  50024. + DWC_WARN("%s, bad ep\n", __func__);
  50025. + return -DWC_E_INVALID;
  50026. + }
  50027. +
  50028. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50029. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  50030. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  50031. + ep->dwc_ep.is_in ? "IN" : "OUT");
  50032. + retval = -DWC_E_AGAIN;
  50033. + } else if (value == 0) {
  50034. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50035. + } else if (value == 1) {
  50036. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  50037. + dtxfsts_data_t txstatus;
  50038. + fifosize_data_t txfifosize;
  50039. +
  50040. + txfifosize.d32 =
  50041. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  50042. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  50043. + txstatus.d32 =
  50044. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  50045. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  50046. +
  50047. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  50048. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  50049. + retval = -DWC_E_AGAIN;
  50050. + } else {
  50051. + if (ep->dwc_ep.num == 0) {
  50052. + pcd->ep0state = EP0_STALL;
  50053. + }
  50054. +
  50055. + ep->stopped = 1;
  50056. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  50057. + &ep->dwc_ep);
  50058. + }
  50059. + } else {
  50060. + if (ep->dwc_ep.num == 0) {
  50061. + pcd->ep0state = EP0_STALL;
  50062. + }
  50063. +
  50064. + ep->stopped = 1;
  50065. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50066. + }
  50067. + } else if (value == 2) {
  50068. + ep->dwc_ep.stall_clear_flag = 0;
  50069. + } else if (value == 3) {
  50070. + ep->dwc_ep.stall_clear_flag = 1;
  50071. + }
  50072. +
  50073. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50074. +
  50075. + return retval;
  50076. +}
  50077. +
  50078. +/**
  50079. + * This function initiates remote wakeup of the host from suspend state.
  50080. + */
  50081. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  50082. +{
  50083. + dctl_data_t dctl = { 0 };
  50084. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50085. + dsts_data_t dsts;
  50086. +
  50087. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  50088. + if (!dsts.b.suspsts) {
  50089. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  50090. + }
  50091. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  50092. + if (pcd->remote_wakeup_enable) {
  50093. + if (set) {
  50094. +
  50095. + if (core_if->adp_enable) {
  50096. + gpwrdn_data_t gpwrdn;
  50097. +
  50098. + dwc_otg_adp_probe_stop(core_if);
  50099. +
  50100. + /* Mask SRP detected interrupt from Power Down Logic */
  50101. + gpwrdn.d32 = 0;
  50102. + gpwrdn.b.srp_det_msk = 1;
  50103. + DWC_MODIFY_REG32(&core_if->
  50104. + core_global_regs->gpwrdn,
  50105. + gpwrdn.d32, 0);
  50106. +
  50107. + /* Disable Power Down Logic */
  50108. + gpwrdn.d32 = 0;
  50109. + gpwrdn.b.pmuactv = 1;
  50110. + DWC_MODIFY_REG32(&core_if->
  50111. + core_global_regs->gpwrdn,
  50112. + gpwrdn.d32, 0);
  50113. +
  50114. + /*
  50115. + * Initialize the Core for Device mode.
  50116. + */
  50117. + core_if->op_state = B_PERIPHERAL;
  50118. + dwc_otg_core_init(core_if);
  50119. + dwc_otg_enable_global_interrupts(core_if);
  50120. + cil_pcd_start(core_if);
  50121. +
  50122. + dwc_otg_initiate_srp(core_if);
  50123. + }
  50124. +
  50125. + dctl.b.rmtwkupsig = 1;
  50126. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  50127. + dctl, 0, dctl.d32);
  50128. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  50129. +
  50130. + dwc_mdelay(2);
  50131. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  50132. + dctl, dctl.d32, 0);
  50133. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  50134. + }
  50135. + } else {
  50136. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  50137. + }
  50138. +}
  50139. +
  50140. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50141. +/**
  50142. + * This function initiates remote wakeup of the host from L1 sleep state.
  50143. + */
  50144. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  50145. +{
  50146. + glpmcfg_data_t lpmcfg;
  50147. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50148. +
  50149. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  50150. +
  50151. + /* Check if we are in L1 state */
  50152. + if (!lpmcfg.b.prt_sleep_sts) {
  50153. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  50154. + return;
  50155. + }
  50156. +
  50157. + /* Check if host allows remote wakeup */
  50158. + if (!lpmcfg.b.rem_wkup_en) {
  50159. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  50160. + return;
  50161. + }
  50162. +
  50163. + /* Check if Resume OK */
  50164. + if (!lpmcfg.b.sleep_state_resumeok) {
  50165. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  50166. + return;
  50167. + }
  50168. +
  50169. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  50170. + lpmcfg.b.en_utmi_sleep = 0;
  50171. + lpmcfg.b.hird_thres &= (~(1 << 4));
  50172. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  50173. +
  50174. + if (set) {
  50175. + dctl_data_t dctl = {.d32 = 0 };
  50176. + dctl.b.rmtwkupsig = 1;
  50177. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  50178. + * Hardware will automatically clear this bit.
  50179. + */
  50180. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  50181. + 0, dctl.d32);
  50182. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  50183. + }
  50184. +
  50185. +}
  50186. +#endif
  50187. +
  50188. +/**
  50189. + * Performs remote wakeup.
  50190. + */
  50191. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  50192. +{
  50193. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50194. + dwc_irqflags_t flags;
  50195. + if (dwc_otg_is_device_mode(core_if)) {
  50196. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50197. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50198. + if (core_if->lx_state == DWC_OTG_L1) {
  50199. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  50200. + } else {
  50201. +#endif
  50202. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  50203. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50204. + }
  50205. +#endif
  50206. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50207. + }
  50208. + return;
  50209. +}
  50210. +
  50211. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  50212. +{
  50213. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50214. + dctl_data_t dctl = { 0 };
  50215. +
  50216. + if (dwc_otg_is_device_mode(core_if)) {
  50217. + dctl.b.sftdiscon = 1;
  50218. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  50219. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  50220. + dwc_udelay(no_of_usecs);
  50221. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  50222. +
  50223. + } else{
  50224. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  50225. + }
  50226. + return;
  50227. +
  50228. +}
  50229. +
  50230. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  50231. +{
  50232. + dsts_data_t dsts;
  50233. + gotgctl_data_t gotgctl;
  50234. +
  50235. + /*
  50236. + * This function starts the Protocol if no session is in progress. If
  50237. + * a session is already in progress, but the device is suspended,
  50238. + * remote wakeup signaling is started.
  50239. + */
  50240. +
  50241. + /* Check if valid session */
  50242. + gotgctl.d32 =
  50243. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  50244. + if (gotgctl.b.bsesvld) {
  50245. + /* Check if suspend state */
  50246. + dsts.d32 =
  50247. + DWC_READ_REG32(&
  50248. + (GET_CORE_IF(pcd)->dev_if->
  50249. + dev_global_regs->dsts));
  50250. + if (dsts.b.suspsts) {
  50251. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  50252. + }
  50253. + } else {
  50254. + dwc_otg_pcd_initiate_srp(pcd);
  50255. + }
  50256. +
  50257. + return 0;
  50258. +
  50259. +}
  50260. +
  50261. +/**
  50262. + * Start the SRP timer to detect when the SRP does not complete within
  50263. + * 6 seconds.
  50264. + *
  50265. + * @param pcd the pcd structure.
  50266. + */
  50267. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  50268. +{
  50269. + dwc_irqflags_t flags;
  50270. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50271. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  50272. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50273. +}
  50274. +
  50275. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  50276. +{
  50277. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  50278. +}
  50279. +
  50280. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  50281. +{
  50282. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  50283. +}
  50284. +
  50285. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  50286. +{
  50287. + return pcd->b_hnp_enable;
  50288. +}
  50289. +
  50290. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  50291. +{
  50292. + return pcd->a_hnp_support;
  50293. +}
  50294. +
  50295. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  50296. +{
  50297. + return pcd->a_alt_hnp_support;
  50298. +}
  50299. +
  50300. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  50301. +{
  50302. + return pcd->remote_wakeup_enable;
  50303. +}
  50304. +
  50305. +#endif /* DWC_HOST_ONLY */
  50306. --- /dev/null
  50307. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  50308. @@ -0,0 +1,266 @@
  50309. +/* ==========================================================================
  50310. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  50311. + * $Revision: #48 $
  50312. + * $Date: 2012/08/10 $
  50313. + * $Change: 2047372 $
  50314. + *
  50315. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50316. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50317. + * otherwise expressly agreed to in writing between Synopsys and you.
  50318. + *
  50319. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50320. + * any End User Software License Agreement or Agreement for Licensed Product
  50321. + * with Synopsys or any supplement thereto. You are permitted to use and
  50322. + * redistribute this Software in source and binary forms, with or without
  50323. + * modification, provided that redistributions of source code must retain this
  50324. + * notice. You may not view, use, disclose, copy or distribute this file or
  50325. + * any information contained herein except pursuant to this license grant from
  50326. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50327. + * below, then you are not authorized to use the Software.
  50328. + *
  50329. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50330. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50331. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50332. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50333. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50334. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50335. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50336. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50337. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50338. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50339. + * DAMAGE.
  50340. + * ========================================================================== */
  50341. +#ifndef DWC_HOST_ONLY
  50342. +#if !defined(__DWC_PCD_H__)
  50343. +#define __DWC_PCD_H__
  50344. +
  50345. +#include "dwc_otg_os_dep.h"
  50346. +#include "usb.h"
  50347. +#include "dwc_otg_cil.h"
  50348. +#include "dwc_otg_pcd_if.h"
  50349. +struct cfiobject;
  50350. +
  50351. +/**
  50352. + * @file
  50353. + *
  50354. + * This file contains the structures, constants, and interfaces for
  50355. + * the Perpherial Contoller Driver (PCD).
  50356. + *
  50357. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  50358. + * Gadget API, so that the existing Gadget drivers can be used. For
  50359. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  50360. + * (FBS) driver will be used. The FBS driver supports the
  50361. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  50362. + * transports.
  50363. + *
  50364. + */
  50365. +
  50366. +/** Invalid DMA Address */
  50367. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  50368. +
  50369. +/** Max Transfer size for any EP */
  50370. +#define DDMA_MAX_TRANSFER_SIZE 65535
  50371. +
  50372. +/**
  50373. + * Get the pointer to the core_if from the pcd pointer.
  50374. + */
  50375. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  50376. +
  50377. +/**
  50378. + * States of EP0.
  50379. + */
  50380. +typedef enum ep0_state {
  50381. + EP0_DISCONNECT, /* no host */
  50382. + EP0_IDLE,
  50383. + EP0_IN_DATA_PHASE,
  50384. + EP0_OUT_DATA_PHASE,
  50385. + EP0_IN_STATUS_PHASE,
  50386. + EP0_OUT_STATUS_PHASE,
  50387. + EP0_STALL,
  50388. +} ep0state_e;
  50389. +
  50390. +/** Fordward declaration.*/
  50391. +struct dwc_otg_pcd;
  50392. +
  50393. +/** DWC_otg iso request structure.
  50394. + *
  50395. + */
  50396. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  50397. +
  50398. +#ifdef DWC_UTE_PER_IO
  50399. +
  50400. +/**
  50401. + * This shall be the exact analogy of the same type structure defined in the
  50402. + * usb_gadget.h. Each descriptor contains
  50403. + */
  50404. +struct dwc_iso_pkt_desc_port {
  50405. + uint32_t offset;
  50406. + uint32_t length; /* expected length */
  50407. + uint32_t actual_length;
  50408. + uint32_t status;
  50409. +};
  50410. +
  50411. +struct dwc_iso_xreq_port {
  50412. + /** transfer/submission flag */
  50413. + uint32_t tr_sub_flags;
  50414. + /** Start the request ASAP */
  50415. +#define DWC_EREQ_TF_ASAP 0x00000002
  50416. + /** Just enqueue the request w/o initiating a transfer */
  50417. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  50418. +
  50419. + /**
  50420. + * count of ISO packets attached to this request - shall
  50421. + * not exceed the pio_alloc_pkt_count
  50422. + */
  50423. + uint32_t pio_pkt_count;
  50424. + /** count of ISO packets allocated for this request */
  50425. + uint32_t pio_alloc_pkt_count;
  50426. + /** number of ISO packet errors */
  50427. + uint32_t error_count;
  50428. + /** reserved for future extension */
  50429. + uint32_t res;
  50430. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  50431. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  50432. +};
  50433. +#endif
  50434. +/** DWC_otg request structure.
  50435. + * This structure is a list of requests.
  50436. + */
  50437. +typedef struct dwc_otg_pcd_request {
  50438. + void *priv;
  50439. + void *buf;
  50440. + dwc_dma_t dma;
  50441. + uint32_t length;
  50442. + uint32_t actual;
  50443. + unsigned sent_zlp:1;
  50444. + /**
  50445. + * Used instead of original buffer if
  50446. + * it(physical address) is not dword-aligned.
  50447. + **/
  50448. + uint8_t *dw_align_buf;
  50449. + dwc_dma_t dw_align_buf_dma;
  50450. +
  50451. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  50452. +#ifdef DWC_UTE_PER_IO
  50453. + struct dwc_iso_xreq_port ext_req;
  50454. + //void *priv_ereq_nport; /* */
  50455. +#endif
  50456. +} dwc_otg_pcd_request_t;
  50457. +
  50458. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  50459. +
  50460. +/** PCD EP structure.
  50461. + * This structure describes an EP, there is an array of EPs in the PCD
  50462. + * structure.
  50463. + */
  50464. +typedef struct dwc_otg_pcd_ep {
  50465. + /** USB EP Descriptor */
  50466. + const usb_endpoint_descriptor_t *desc;
  50467. +
  50468. + /** queue of dwc_otg_pcd_requests. */
  50469. + struct req_list queue;
  50470. + unsigned stopped:1;
  50471. + unsigned disabling:1;
  50472. + unsigned dma:1;
  50473. + unsigned queue_sof:1;
  50474. +
  50475. +#ifdef DWC_EN_ISOC
  50476. + /** ISOC req handle passed */
  50477. + void *iso_req_handle;
  50478. +#endif //_EN_ISOC_
  50479. +
  50480. + /** DWC_otg ep data. */
  50481. + dwc_ep_t dwc_ep;
  50482. +
  50483. + /** Pointer to PCD */
  50484. + struct dwc_otg_pcd *pcd;
  50485. +
  50486. + void *priv;
  50487. +} dwc_otg_pcd_ep_t;
  50488. +
  50489. +/** DWC_otg PCD Structure.
  50490. + * This structure encapsulates the data for the dwc_otg PCD.
  50491. + */
  50492. +struct dwc_otg_pcd {
  50493. + const struct dwc_otg_pcd_function_ops *fops;
  50494. + /** The DWC otg device pointer */
  50495. + struct dwc_otg_device *otg_dev;
  50496. + /** Core Interface */
  50497. + dwc_otg_core_if_t *core_if;
  50498. + /** State of EP0 */
  50499. + ep0state_e ep0state;
  50500. + /** EP0 Request is pending */
  50501. + unsigned ep0_pending:1;
  50502. + /** Indicates when SET CONFIGURATION Request is in process */
  50503. + unsigned request_config:1;
  50504. + /** The state of the Remote Wakeup Enable. */
  50505. + unsigned remote_wakeup_enable:1;
  50506. + /** The state of the B-Device HNP Enable. */
  50507. + unsigned b_hnp_enable:1;
  50508. + /** The state of A-Device HNP Support. */
  50509. + unsigned a_hnp_support:1;
  50510. + /** The state of the A-Device Alt HNP support. */
  50511. + unsigned a_alt_hnp_support:1;
  50512. + /** Count of pending Requests */
  50513. + unsigned request_pending;
  50514. +
  50515. + /** SETUP packet for EP0
  50516. + * This structure is allocated as a DMA buffer on PCD initialization
  50517. + * with enough space for up to 3 setup packets.
  50518. + */
  50519. + union {
  50520. + usb_device_request_t req;
  50521. + uint32_t d32[2];
  50522. + } *setup_pkt;
  50523. +
  50524. + dwc_dma_t setup_pkt_dma_handle;
  50525. +
  50526. + /* Additional buffer and flag for CTRL_WR premature case */
  50527. + uint8_t *backup_buf;
  50528. + unsigned data_terminated;
  50529. +
  50530. + /** 2-byte dma buffer used to return status from GET_STATUS */
  50531. + uint16_t *status_buf;
  50532. + dwc_dma_t status_buf_dma_handle;
  50533. +
  50534. + /** EP0 */
  50535. + dwc_otg_pcd_ep_t ep0;
  50536. +
  50537. + /** Array of IN EPs. */
  50538. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  50539. + /** Array of OUT EPs. */
  50540. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  50541. + /** number of valid EPs in the above array. */
  50542. +// unsigned num_eps : 4;
  50543. + dwc_spinlock_t *lock;
  50544. +
  50545. + /** Tasklet to defer starting of TEST mode transmissions until
  50546. + * Status Phase has been completed.
  50547. + */
  50548. + dwc_tasklet_t *test_mode_tasklet;
  50549. +
  50550. + /** Tasklet to delay starting of xfer in DMA mode */
  50551. + dwc_tasklet_t *start_xfer_tasklet;
  50552. +
  50553. + /** The test mode to enter when the tasklet is executed. */
  50554. + unsigned test_mode;
  50555. + /** The cfi_api structure that implements most of the CFI API
  50556. + * and OTG specific core configuration functionality
  50557. + */
  50558. +#ifdef DWC_UTE_CFI
  50559. + struct cfiobject *cfi;
  50560. +#endif
  50561. +
  50562. +};
  50563. +
  50564. +//FIXME this functions should be static, and this prototypes should be removed
  50565. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  50566. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  50567. + dwc_otg_pcd_request_t * req, int32_t status);
  50568. +
  50569. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  50570. + void *req_handle);
  50571. +
  50572. +extern void do_test_mode(void *data);
  50573. +#endif
  50574. +#endif /* DWC_HOST_ONLY */
  50575. --- /dev/null
  50576. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  50577. @@ -0,0 +1,360 @@
  50578. +/* ==========================================================================
  50579. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  50580. + * $Revision: #11 $
  50581. + * $Date: 2011/10/26 $
  50582. + * $Change: 1873028 $
  50583. + *
  50584. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50585. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50586. + * otherwise expressly agreed to in writing between Synopsys and you.
  50587. + *
  50588. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50589. + * any End User Software License Agreement or Agreement for Licensed Product
  50590. + * with Synopsys or any supplement thereto. You are permitted to use and
  50591. + * redistribute this Software in source and binary forms, with or without
  50592. + * modification, provided that redistributions of source code must retain this
  50593. + * notice. You may not view, use, disclose, copy or distribute this file or
  50594. + * any information contained herein except pursuant to this license grant from
  50595. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50596. + * below, then you are not authorized to use the Software.
  50597. + *
  50598. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50599. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50600. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50601. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50602. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50603. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50604. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50605. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50606. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50607. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50608. + * DAMAGE.
  50609. + * ========================================================================== */
  50610. +#ifndef DWC_HOST_ONLY
  50611. +
  50612. +#if !defined(__DWC_PCD_IF_H__)
  50613. +#define __DWC_PCD_IF_H__
  50614. +
  50615. +//#include "dwc_os.h"
  50616. +#include "dwc_otg_core_if.h"
  50617. +
  50618. +/** @file
  50619. + * This file defines DWC_OTG PCD Core API.
  50620. + */
  50621. +
  50622. +struct dwc_otg_pcd;
  50623. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  50624. +
  50625. +/** Maxpacket size for EP0 */
  50626. +#define MAX_EP0_SIZE 64
  50627. +/** Maxpacket size for any EP */
  50628. +#define MAX_PACKET_SIZE 1024
  50629. +
  50630. +/** @name Function Driver Callbacks */
  50631. +/** @{ */
  50632. +
  50633. +/** This function will be called whenever a previously queued request has
  50634. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  50635. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  50636. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  50637. + * parameters. */
  50638. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50639. + void *req_handle, int32_t status,
  50640. + uint32_t actual);
  50641. +/**
  50642. + * This function will be called whenever a previousle queued ISOC request has
  50643. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  50644. + * function.
  50645. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  50646. + * functions.
  50647. + */
  50648. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50649. + void *req_handle, int proc_buf_num);
  50650. +/** This function should handle any SETUP request that cannot be handled by the
  50651. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  50652. + * class-specific requests, etc. The function must non-blocking.
  50653. + *
  50654. + * Returns 0 on success.
  50655. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  50656. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  50657. + * Returns -DWC_E_SHUTDOWN on any other error. */
  50658. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  50659. +/** This is called whenever the device has been disconnected. The function
  50660. + * driver should take appropriate action to clean up all pending requests in the
  50661. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  50662. + * state. */
  50663. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  50664. +/** This function is called when device has been connected. */
  50665. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  50666. +/** This function is called when device has been suspended */
  50667. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  50668. +/** This function is called when device has received LPM tokens, i.e.
  50669. + * device has been sent to sleep state. */
  50670. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  50671. +/** This function is called when device has been resumed
  50672. + * from suspend(L2) or L1 sleep state. */
  50673. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  50674. +/** This function is called whenever hnp params has been changed.
  50675. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  50676. + * to get hnp parameters. */
  50677. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  50678. +/** This function is called whenever USB RESET is detected. */
  50679. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  50680. +
  50681. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  50682. +
  50683. +/**
  50684. + *
  50685. + * @param ep_handle Void pointer to the usb_ep structure
  50686. + * @param ereq_port Pointer to the extended request structure created in the
  50687. + * portable part.
  50688. + */
  50689. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50690. + void *req_handle, int32_t status,
  50691. + void *ereq_port);
  50692. +/** Function Driver Ops Data Structure */
  50693. +struct dwc_otg_pcd_function_ops {
  50694. + dwc_connect_cb_t connect;
  50695. + dwc_disconnect_cb_t disconnect;
  50696. + dwc_setup_cb_t setup;
  50697. + dwc_completion_cb_t complete;
  50698. + dwc_isoc_completion_cb_t isoc_complete;
  50699. + dwc_suspend_cb_t suspend;
  50700. + dwc_sleep_cb_t sleep;
  50701. + dwc_resume_cb_t resume;
  50702. + dwc_reset_cb_t reset;
  50703. + dwc_hnp_params_changed_cb_t hnp_changed;
  50704. + cfi_setup_cb_t cfi_setup;
  50705. +#ifdef DWC_UTE_PER_IO
  50706. + xiso_completion_cb_t xisoc_complete;
  50707. +#endif
  50708. +};
  50709. +/** @} */
  50710. +
  50711. +/** @name Function Driver Functions */
  50712. +/** @{ */
  50713. +
  50714. +/** Call this function to get pointer on dwc_otg_pcd_t,
  50715. + * this pointer will be used for all PCD API functions.
  50716. + *
  50717. + * @param core_if The DWC_OTG Core
  50718. + */
  50719. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if);
  50720. +
  50721. +/** Frees PCD allocated by dwc_otg_pcd_init
  50722. + *
  50723. + * @param pcd The PCD
  50724. + */
  50725. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  50726. +
  50727. +/** Call this to bind the function driver to the PCD Core.
  50728. + *
  50729. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  50730. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  50731. + */
  50732. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  50733. + const struct dwc_otg_pcd_function_ops *fops);
  50734. +
  50735. +/** Enables an endpoint for use. This function enables an endpoint in
  50736. + * the PCD. The endpoint is described by the ep_desc which has the
  50737. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  50738. + * to the endpoint from other API functions and in callbacks. Normally this
  50739. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  50740. + * core for that interface.
  50741. + *
  50742. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50743. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50744. + * Returns 0 on success.
  50745. + *
  50746. + * @param pcd The PCD
  50747. + * @param ep_desc Endpoint descriptor
  50748. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  50749. + */
  50750. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  50751. + const uint8_t * ep_desc, void *usb_ep);
  50752. +
  50753. +/** Disable the endpoint referenced by ep_handle.
  50754. + *
  50755. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50756. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  50757. + * Returns 0 on success. */
  50758. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  50759. +
  50760. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  50761. + * After the transfer is completes, the complete callback will be called with
  50762. + * the request status.
  50763. + *
  50764. + * @param pcd The PCD
  50765. + * @param ep_handle The handle of the endpoint
  50766. + * @param buf The buffer for the data
  50767. + * @param dma_buf The DMA buffer for the data
  50768. + * @param buflen The length of the data transfer
  50769. + * @param zero Specifies whether to send zero length last packet.
  50770. + * @param req_handle Set this handle to any value to use to reference this
  50771. + * request in the ep_dequeue function or from the complete callback
  50772. + * @param atomic_alloc If driver need to perform atomic allocations
  50773. + * for internal data structures.
  50774. + *
  50775. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50776. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50777. + * Returns 0 on success. */
  50778. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50779. + uint8_t * buf, dwc_dma_t dma_buf,
  50780. + uint32_t buflen, int zero, void *req_handle,
  50781. + int atomic_alloc);
  50782. +#ifdef DWC_UTE_PER_IO
  50783. +/**
  50784. + *
  50785. + * @param ereq_nonport Pointer to the extended request part of the
  50786. + * usb_request structure defined in usb_gadget.h file.
  50787. + */
  50788. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50789. + uint8_t * buf, dwc_dma_t dma_buf,
  50790. + uint32_t buflen, int zero,
  50791. + void *req_handle, int atomic_alloc,
  50792. + void *ereq_nonport);
  50793. +
  50794. +#endif
  50795. +
  50796. +/** De-queue the specified data transfer that has not yet completed.
  50797. + *
  50798. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50799. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50800. + * Returns 0 on success. */
  50801. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50802. + void *req_handle);
  50803. +
  50804. +/** Halt (STALL) an endpoint or clear it.
  50805. + *
  50806. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50807. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50808. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  50809. + * Returns 0 on success. */
  50810. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  50811. +
  50812. +/** This function */
  50813. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  50814. +
  50815. +/** This function should be called on every hardware interrupt */
  50816. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  50817. +
  50818. +/** This function returns current frame number */
  50819. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  50820. +
  50821. +/**
  50822. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  50823. + * For isochronous transfers duble buffering is used.
  50824. + * After processing each of buffers comlete callback will be called with
  50825. + * status for each transaction.
  50826. + *
  50827. + * @param pcd The PCD
  50828. + * @param ep_handle The handle of the endpoint
  50829. + * @param buf0 The virtual address of first data buffer
  50830. + * @param buf1 The virtual address of second data buffer
  50831. + * @param dma0 The DMA address of first data buffer
  50832. + * @param dma1 The DMA address of second data buffer
  50833. + * @param sync_frame Data pattern frame number
  50834. + * @param dp_frame Data size for pattern frame
  50835. + * @param data_per_frame Data size for regular frame
  50836. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  50837. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  50838. + * @param req_handle Handle of ISOC request
  50839. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  50840. + * internal data structures.
  50841. + *
  50842. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  50843. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  50844. + * Returns -DW_E_SHUTDOWN for any other error.
  50845. + * Returns 0 on success
  50846. + */
  50847. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  50848. + uint8_t * buf0, uint8_t * buf1,
  50849. + dwc_dma_t dma0, dwc_dma_t dma1,
  50850. + int sync_frame, int dp_frame,
  50851. + int data_per_frame, int start_frame,
  50852. + int buf_proc_intrvl, void *req_handle,
  50853. + int atomic_alloc);
  50854. +
  50855. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  50856. + *
  50857. + * @param pcd The PCD
  50858. + * @param ep_handle The handle of the endpoint
  50859. + * @param req_handle Handle of ISOC request
  50860. + *
  50861. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  50862. + * Returns 0 on success
  50863. + */
  50864. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  50865. + void *req_handle);
  50866. +
  50867. +/** Get ISOC packet status.
  50868. + *
  50869. + * @param pcd The PCD
  50870. + * @param ep_handle The handle of the endpoint
  50871. + * @param iso_req_handle Isochronoush request handle
  50872. + * @param packet Number of packet
  50873. + * @param status Out parameter for returning status
  50874. + * @param actual Out parameter for returning actual length
  50875. + * @param offset Out parameter for returning offset
  50876. + *
  50877. + */
  50878. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  50879. + void *ep_handle,
  50880. + void *iso_req_handle, int packet,
  50881. + int *status, int *actual,
  50882. + int *offset);
  50883. +
  50884. +/** Get ISOC packet count.
  50885. + *
  50886. + * @param pcd The PCD
  50887. + * @param ep_handle The handle of the endpoint
  50888. + * @param iso_req_handle
  50889. + */
  50890. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  50891. + void *ep_handle,
  50892. + void *iso_req_handle);
  50893. +
  50894. +/** This function starts the SRP Protocol if no session is in progress. If
  50895. + * a session is already in progress, but the device is suspended,
  50896. + * remote wakeup signaling is started.
  50897. + */
  50898. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  50899. +
  50900. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  50901. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  50902. +
  50903. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  50904. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  50905. +
  50906. +/** Initiate SRP */
  50907. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  50908. +
  50909. +/** Starts remote wakeup signaling. */
  50910. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  50911. +
  50912. +/** Starts micorsecond soft disconnect. */
  50913. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  50914. +/** This function returns whether device is dualspeed.*/
  50915. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  50916. +
  50917. +/** This function returns whether device is otg. */
  50918. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  50919. +
  50920. +/** These functions allow to get hnp parameters */
  50921. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  50922. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  50923. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  50924. +
  50925. +/** CFI specific Interface functions */
  50926. +/** Allocate a cfi buffer */
  50927. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  50928. + dwc_dma_t * addr, size_t buflen,
  50929. + int flags);
  50930. +
  50931. +/******************************************************************************/
  50932. +
  50933. +/** @} */
  50934. +
  50935. +#endif /* __DWC_PCD_IF_H__ */
  50936. +
  50937. +#endif /* DWC_HOST_ONLY */
  50938. --- /dev/null
  50939. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  50940. @@ -0,0 +1,5147 @@
  50941. +/* ==========================================================================
  50942. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  50943. + * $Revision: #116 $
  50944. + * $Date: 2012/08/10 $
  50945. + * $Change: 2047372 $
  50946. + *
  50947. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50948. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50949. + * otherwise expressly agreed to in writing between Synopsys and you.
  50950. + *
  50951. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50952. + * any End User Software License Agreement or Agreement for Licensed Product
  50953. + * with Synopsys or any supplement thereto. You are permitted to use and
  50954. + * redistribute this Software in source and binary forms, with or without
  50955. + * modification, provided that redistributions of source code must retain this
  50956. + * notice. You may not view, use, disclose, copy or distribute this file or
  50957. + * any information contained herein except pursuant to this license grant from
  50958. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50959. + * below, then you are not authorized to use the Software.
  50960. + *
  50961. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50962. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50963. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50964. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50965. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50966. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50967. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50968. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50969. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50970. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50971. + * DAMAGE.
  50972. + * ========================================================================== */
  50973. +#ifndef DWC_HOST_ONLY
  50974. +
  50975. +#include "dwc_otg_pcd.h"
  50976. +
  50977. +#ifdef DWC_UTE_CFI
  50978. +#include "dwc_otg_cfi.h"
  50979. +#endif
  50980. +
  50981. +#ifdef DWC_UTE_PER_IO
  50982. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  50983. +#endif
  50984. +//#define PRINT_CFI_DMA_DESCS
  50985. +
  50986. +#define DEBUG_EP0
  50987. +
  50988. +/**
  50989. + * This function updates OTG.
  50990. + */
  50991. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  50992. +{
  50993. +
  50994. + if (reset) {
  50995. + pcd->b_hnp_enable = 0;
  50996. + pcd->a_hnp_support = 0;
  50997. + pcd->a_alt_hnp_support = 0;
  50998. + }
  50999. +
  51000. + if (pcd->fops->hnp_changed) {
  51001. + pcd->fops->hnp_changed(pcd);
  51002. + }
  51003. +}
  51004. +
  51005. +/** @file
  51006. + * This file contains the implementation of the PCD Interrupt handlers.
  51007. + *
  51008. + * The PCD handles the device interrupts. Many conditions can cause a
  51009. + * device interrupt. When an interrupt occurs, the device interrupt
  51010. + * service routine determines the cause of the interrupt and
  51011. + * dispatches handling to the appropriate function. These interrupt
  51012. + * handling functions are described below.
  51013. + * All interrupt registers are processed from LSB to MSB.
  51014. + */
  51015. +
  51016. +/**
  51017. + * This function prints the ep0 state for debug purposes.
  51018. + */
  51019. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  51020. +{
  51021. +#ifdef DEBUG
  51022. + char str[40];
  51023. +
  51024. + switch (pcd->ep0state) {
  51025. + case EP0_DISCONNECT:
  51026. + dwc_strcpy(str, "EP0_DISCONNECT");
  51027. + break;
  51028. + case EP0_IDLE:
  51029. + dwc_strcpy(str, "EP0_IDLE");
  51030. + break;
  51031. + case EP0_IN_DATA_PHASE:
  51032. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  51033. + break;
  51034. + case EP0_OUT_DATA_PHASE:
  51035. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  51036. + break;
  51037. + case EP0_IN_STATUS_PHASE:
  51038. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  51039. + break;
  51040. + case EP0_OUT_STATUS_PHASE:
  51041. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  51042. + break;
  51043. + case EP0_STALL:
  51044. + dwc_strcpy(str, "EP0_STALL");
  51045. + break;
  51046. + default:
  51047. + dwc_strcpy(str, "EP0_INVALID");
  51048. + }
  51049. +
  51050. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  51051. +#endif
  51052. +}
  51053. +
  51054. +/**
  51055. + * This function calculate the size of the payload in the memory
  51056. + * for out endpoints and prints size for debug purposes(used in
  51057. + * 2.93a DevOutNak feature).
  51058. + */
  51059. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  51060. +{
  51061. +#ifdef DEBUG
  51062. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  51063. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  51064. + int pack_num;
  51065. + unsigned payload;
  51066. +
  51067. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  51068. + deptsiz_updt.d32 =
  51069. + DWC_READ_REG32(&pcd->core_if->dev_if->
  51070. + out_ep_regs[ep->num]->doeptsiz);
  51071. + /* Payload will be */
  51072. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  51073. + /* Packet count is decremented every time a packet
  51074. + * is written to the RxFIFO not in to the external memory
  51075. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  51076. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  51077. + DWC_DEBUGPL(DBG_PCDV,
  51078. + "Payload for EP%d-%s\n",
  51079. + ep->num, (ep->is_in ? "IN" : "OUT"));
  51080. + DWC_DEBUGPL(DBG_PCDV,
  51081. + "Number of transfered bytes = 0x%08x\n", payload);
  51082. + DWC_DEBUGPL(DBG_PCDV,
  51083. + "Number of transfered packets = %d\n", pack_num);
  51084. +#endif
  51085. +}
  51086. +
  51087. +
  51088. +#ifdef DWC_UTE_CFI
  51089. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  51090. + const uint8_t * epname, int descnum)
  51091. +{
  51092. + CFI_INFO
  51093. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  51094. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  51095. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  51096. + ddesc->status.b.bs);
  51097. +}
  51098. +#endif
  51099. +
  51100. +/**
  51101. + * This function returns pointer to in ep struct with number ep_num
  51102. + */
  51103. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  51104. +{
  51105. + int i;
  51106. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  51107. + if (ep_num == 0) {
  51108. + return &pcd->ep0;
  51109. + } else {
  51110. + for (i = 0; i < num_in_eps; ++i) {
  51111. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  51112. + return &pcd->in_ep[i];
  51113. + }
  51114. + return 0;
  51115. + }
  51116. +}
  51117. +
  51118. +/**
  51119. + * This function returns pointer to out ep struct with number ep_num
  51120. + */
  51121. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  51122. +{
  51123. + int i;
  51124. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  51125. + if (ep_num == 0) {
  51126. + return &pcd->ep0;
  51127. + } else {
  51128. + for (i = 0; i < num_out_eps; ++i) {
  51129. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  51130. + return &pcd->out_ep[i];
  51131. + }
  51132. + return 0;
  51133. + }
  51134. +}
  51135. +
  51136. +/**
  51137. + * This functions gets a pointer to an EP from the wIndex address
  51138. + * value of the control request.
  51139. + */
  51140. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  51141. +{
  51142. + dwc_otg_pcd_ep_t *ep;
  51143. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  51144. +
  51145. + if (ep_num == 0) {
  51146. + ep = &pcd->ep0;
  51147. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  51148. + ep = &pcd->in_ep[ep_num - 1];
  51149. + } else {
  51150. + ep = &pcd->out_ep[ep_num - 1];
  51151. + }
  51152. +
  51153. + return ep;
  51154. +}
  51155. +
  51156. +/**
  51157. + * This function checks the EP request queue, if the queue is not
  51158. + * empty the next request is started.
  51159. + */
  51160. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  51161. +{
  51162. + dwc_otg_pcd_request_t *req = 0;
  51163. + uint32_t max_transfer =
  51164. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  51165. +
  51166. +#ifdef DWC_UTE_CFI
  51167. + struct dwc_otg_pcd *pcd;
  51168. + pcd = ep->pcd;
  51169. +#endif
  51170. +
  51171. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  51172. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  51173. +
  51174. +#ifdef DWC_UTE_CFI
  51175. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  51176. + ep->dwc_ep.cfi_req_len = req->length;
  51177. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  51178. + } else {
  51179. +#endif
  51180. + /* Setup and start the Transfer */
  51181. + if (req->dw_align_buf) {
  51182. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  51183. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  51184. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  51185. + } else {
  51186. + ep->dwc_ep.dma_addr = req->dma;
  51187. + ep->dwc_ep.start_xfer_buff = req->buf;
  51188. + ep->dwc_ep.xfer_buff = req->buf;
  51189. + }
  51190. + ep->dwc_ep.sent_zlp = 0;
  51191. + ep->dwc_ep.total_len = req->length;
  51192. + ep->dwc_ep.xfer_len = 0;
  51193. + ep->dwc_ep.xfer_count = 0;
  51194. +
  51195. + ep->dwc_ep.maxxfer = max_transfer;
  51196. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  51197. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  51198. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  51199. + if (ep->dwc_ep.is_in) {
  51200. + if (ep->dwc_ep.maxxfer >
  51201. + DDMA_MAX_TRANSFER_SIZE) {
  51202. + ep->dwc_ep.maxxfer =
  51203. + DDMA_MAX_TRANSFER_SIZE;
  51204. + }
  51205. + } else {
  51206. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  51207. + ep->dwc_ep.maxxfer =
  51208. + out_max_xfer;
  51209. + }
  51210. + }
  51211. + }
  51212. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  51213. + ep->dwc_ep.maxxfer -=
  51214. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  51215. + }
  51216. + if (req->sent_zlp) {
  51217. + if ((ep->dwc_ep.total_len %
  51218. + ep->dwc_ep.maxpacket == 0)
  51219. + && (ep->dwc_ep.total_len != 0)) {
  51220. + ep->dwc_ep.sent_zlp = 1;
  51221. + }
  51222. +
  51223. + }
  51224. +#ifdef DWC_UTE_CFI
  51225. + }
  51226. +#endif
  51227. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  51228. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  51229. + DWC_PRINTF("There are no more ISOC requests \n");
  51230. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  51231. + }
  51232. +}
  51233. +
  51234. +/**
  51235. + * This function handles the SOF Interrupts. At this time the SOF
  51236. + * Interrupt is disabled.
  51237. + */
  51238. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  51239. +{
  51240. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51241. +
  51242. + gintsts_data_t gintsts;
  51243. +
  51244. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  51245. +
  51246. + /* Clear interrupt */
  51247. + gintsts.d32 = 0;
  51248. + gintsts.b.sofintr = 1;
  51249. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  51250. +
  51251. + return 1;
  51252. +}
  51253. +
  51254. +/**
  51255. + * This function handles the Rx Status Queue Level Interrupt, which
  51256. + * indicates that there is a least one packet in the Rx FIFO. The
  51257. + * packets are moved from the FIFO to memory, where they will be
  51258. + * processed when the Endpoint Interrupt Register indicates Transfer
  51259. + * Complete or SETUP Phase Done.
  51260. + *
  51261. + * Repeat the following until the Rx Status Queue is empty:
  51262. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  51263. + * info
  51264. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  51265. + * and exit
  51266. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  51267. + * SETUP data to the buffer
  51268. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  51269. + * to the destination buffer
  51270. + */
  51271. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  51272. +{
  51273. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51274. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  51275. + gintmsk_data_t gintmask = {.d32 = 0 };
  51276. + device_grxsts_data_t status;
  51277. + dwc_otg_pcd_ep_t *ep;
  51278. + gintsts_data_t gintsts;
  51279. +#ifdef DEBUG
  51280. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  51281. +#endif
  51282. +
  51283. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  51284. + /* Disable the Rx Status Queue Level interrupt */
  51285. + gintmask.b.rxstsqlvl = 1;
  51286. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  51287. +
  51288. + /* Get the Status from the top of the FIFO */
  51289. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  51290. +
  51291. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  51292. + "pktsts:%x Frame:%d(0x%0x)\n",
  51293. + status.b.epnum, status.b.bcnt,
  51294. + dpid_str[status.b.dpid],
  51295. + status.b.pktsts, status.b.fn, status.b.fn);
  51296. + /* Get pointer to EP structure */
  51297. + ep = get_out_ep(pcd, status.b.epnum);
  51298. +
  51299. + switch (status.b.pktsts) {
  51300. + case DWC_DSTS_GOUT_NAK:
  51301. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  51302. + break;
  51303. + case DWC_STS_DATA_UPDT:
  51304. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  51305. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  51306. + /** @todo NGS Check for buffer overflow? */
  51307. + dwc_otg_read_packet(core_if,
  51308. + ep->dwc_ep.xfer_buff,
  51309. + status.b.bcnt);
  51310. + ep->dwc_ep.xfer_count += status.b.bcnt;
  51311. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  51312. + }
  51313. + break;
  51314. + case DWC_STS_XFER_COMP:
  51315. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  51316. + break;
  51317. + case DWC_DSTS_SETUP_COMP:
  51318. +#ifdef DEBUG_EP0
  51319. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  51320. +#endif
  51321. + break;
  51322. + case DWC_DSTS_SETUP_UPDT:
  51323. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  51324. +#ifdef DEBUG_EP0
  51325. + DWC_DEBUGPL(DBG_PCD,
  51326. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  51327. + pcd->setup_pkt->req.bmRequestType,
  51328. + pcd->setup_pkt->req.bRequest,
  51329. + UGETW(pcd->setup_pkt->req.wValue),
  51330. + UGETW(pcd->setup_pkt->req.wIndex),
  51331. + UGETW(pcd->setup_pkt->req.wLength));
  51332. +#endif
  51333. + ep->dwc_ep.xfer_count += status.b.bcnt;
  51334. + break;
  51335. + default:
  51336. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  51337. + status.b.pktsts);
  51338. + break;
  51339. + }
  51340. +
  51341. + /* Enable the Rx Status Queue Level interrupt */
  51342. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  51343. + /* Clear interrupt */
  51344. + gintsts.d32 = 0;
  51345. + gintsts.b.rxstsqlvl = 1;
  51346. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  51347. +
  51348. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  51349. + return 1;
  51350. +}
  51351. +
  51352. +/**
  51353. + * This function examines the Device IN Token Learning Queue to
  51354. + * determine the EP number of the last IN token received. This
  51355. + * implementation is for the Mass Storage device where there are only
  51356. + * 2 IN EPs (Control-IN and BULK-IN).
  51357. + *
  51358. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  51359. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  51360. + *
  51361. + * @param core_if Programming view of DWC_otg controller.
  51362. + *
  51363. + */
  51364. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  51365. +{
  51366. + dwc_otg_device_global_regs_t *dev_global_regs =
  51367. + core_if->dev_if->dev_global_regs;
  51368. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  51369. + /* Number of Token Queue Registers */
  51370. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  51371. + dtknq1_data_t dtknqr1;
  51372. + uint32_t in_tkn_epnums[4];
  51373. + int ndx = 0;
  51374. + int i = 0;
  51375. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  51376. + int epnum = 0;
  51377. +
  51378. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  51379. +
  51380. + /* Read the DTKNQ Registers */
  51381. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  51382. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  51383. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  51384. + in_tkn_epnums[i]);
  51385. + if (addr == &dev_global_regs->dvbusdis) {
  51386. + addr = &dev_global_regs->dtknqr3_dthrctl;
  51387. + } else {
  51388. + ++addr;
  51389. + }
  51390. +
  51391. + }
  51392. +
  51393. + /* Copy the DTKNQR1 data to the bit field. */
  51394. + dtknqr1.d32 = in_tkn_epnums[0];
  51395. + /* Get the EP numbers */
  51396. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  51397. + ndx = dtknqr1.b.intknwptr - 1;
  51398. +
  51399. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  51400. + if (ndx == -1) {
  51401. + /** @todo Find a simpler way to calculate the max
  51402. + * queue position.*/
  51403. + int cnt = TOKEN_Q_DEPTH;
  51404. + if (TOKEN_Q_DEPTH <= 6) {
  51405. + cnt = TOKEN_Q_DEPTH - 1;
  51406. + } else if (TOKEN_Q_DEPTH <= 14) {
  51407. + cnt = TOKEN_Q_DEPTH - 7;
  51408. + } else if (TOKEN_Q_DEPTH <= 22) {
  51409. + cnt = TOKEN_Q_DEPTH - 15;
  51410. + } else {
  51411. + cnt = TOKEN_Q_DEPTH - 23;
  51412. + }
  51413. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  51414. + } else {
  51415. + if (ndx <= 5) {
  51416. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  51417. + } else if (ndx <= 13) {
  51418. + ndx -= 6;
  51419. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  51420. + } else if (ndx <= 21) {
  51421. + ndx -= 14;
  51422. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  51423. + } else if (ndx <= 29) {
  51424. + ndx -= 22;
  51425. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  51426. + }
  51427. + }
  51428. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  51429. + return epnum;
  51430. +}
  51431. +
  51432. +/**
  51433. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  51434. + * The active request is checked for the next packet to be loaded into
  51435. + * the non-periodic Tx FIFO.
  51436. + */
  51437. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  51438. +{
  51439. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51440. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  51441. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  51442. + gnptxsts_data_t txstatus = {.d32 = 0 };
  51443. + gintsts_data_t gintsts;
  51444. +
  51445. + int epnum = 0;
  51446. + dwc_otg_pcd_ep_t *ep = 0;
  51447. + uint32_t len = 0;
  51448. + int dwords;
  51449. +
  51450. + /* Get the epnum from the IN Token Learning Queue. */
  51451. + epnum = get_ep_of_last_in_token(core_if);
  51452. + ep = get_in_ep(pcd, epnum);
  51453. +
  51454. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  51455. +
  51456. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  51457. +
  51458. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51459. + if (len > ep->dwc_ep.maxpacket) {
  51460. + len = ep->dwc_ep.maxpacket;
  51461. + }
  51462. + dwords = (len + 3) / 4;
  51463. +
  51464. + /* While there is space in the queue and space in the FIFO and
  51465. + * More data to tranfer, Write packets to the Tx FIFO */
  51466. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  51467. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  51468. +
  51469. + while (txstatus.b.nptxqspcavail > 0 &&
  51470. + txstatus.b.nptxfspcavail > dwords &&
  51471. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  51472. + /* Write the FIFO */
  51473. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  51474. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51475. +
  51476. + if (len > ep->dwc_ep.maxpacket) {
  51477. + len = ep->dwc_ep.maxpacket;
  51478. + }
  51479. +
  51480. + dwords = (len + 3) / 4;
  51481. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  51482. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  51483. + }
  51484. +
  51485. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  51486. + DWC_READ_REG32(&global_regs->gnptxsts));
  51487. +
  51488. + /* Clear interrupt */
  51489. + gintsts.d32 = 0;
  51490. + gintsts.b.nptxfempty = 1;
  51491. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  51492. +
  51493. + return 1;
  51494. +}
  51495. +
  51496. +/**
  51497. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  51498. + * The active request is checked for the next packet to be loaded into
  51499. + * apropriate Tx FIFO.
  51500. + */
  51501. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  51502. +{
  51503. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51504. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51505. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  51506. + dtxfsts_data_t txstatus = {.d32 = 0 };
  51507. + dwc_otg_pcd_ep_t *ep = 0;
  51508. + uint32_t len = 0;
  51509. + int dwords;
  51510. +
  51511. + ep = get_in_ep(pcd, epnum);
  51512. +
  51513. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  51514. +
  51515. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  51516. +
  51517. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51518. +
  51519. + if (len > ep->dwc_ep.maxpacket) {
  51520. + len = ep->dwc_ep.maxpacket;
  51521. + }
  51522. +
  51523. + dwords = (len + 3) / 4;
  51524. +
  51525. + /* While there is space in the queue and space in the FIFO and
  51526. + * More data to tranfer, Write packets to the Tx FIFO */
  51527. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  51528. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  51529. +
  51530. + while (txstatus.b.txfspcavail > dwords &&
  51531. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  51532. + ep->dwc_ep.xfer_len != 0) {
  51533. + /* Write the FIFO */
  51534. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  51535. +
  51536. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51537. + if (len > ep->dwc_ep.maxpacket) {
  51538. + len = ep->dwc_ep.maxpacket;
  51539. + }
  51540. +
  51541. + dwords = (len + 3) / 4;
  51542. + txstatus.d32 =
  51543. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  51544. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  51545. + txstatus.d32);
  51546. + }
  51547. +
  51548. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  51549. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  51550. +
  51551. + return 1;
  51552. +}
  51553. +
  51554. +/**
  51555. + * This function is called when the Device is disconnected. It stops
  51556. + * any active requests and informs the Gadget driver of the
  51557. + * disconnect.
  51558. + */
  51559. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  51560. +{
  51561. + int i, num_in_eps, num_out_eps;
  51562. + dwc_otg_pcd_ep_t *ep;
  51563. +
  51564. + gintmsk_data_t intr_mask = {.d32 = 0 };
  51565. +
  51566. + DWC_SPINLOCK(pcd->lock);
  51567. +
  51568. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  51569. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  51570. +
  51571. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  51572. + /* don't disconnect drivers more than once */
  51573. + if (pcd->ep0state == EP0_DISCONNECT) {
  51574. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  51575. + DWC_SPINUNLOCK(pcd->lock);
  51576. + return;
  51577. + }
  51578. + pcd->ep0state = EP0_DISCONNECT;
  51579. +
  51580. + /* Reset the OTG state. */
  51581. + dwc_otg_pcd_update_otg(pcd, 1);
  51582. +
  51583. + /* Disable the NP Tx Fifo Empty Interrupt. */
  51584. + intr_mask.b.nptxfempty = 1;
  51585. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  51586. + intr_mask.d32, 0);
  51587. +
  51588. + /* Flush the FIFOs */
  51589. + /**@todo NGS Flush Periodic FIFOs */
  51590. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  51591. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  51592. +
  51593. + /* prevent new request submissions, kill any outstanding requests */
  51594. + ep = &pcd->ep0;
  51595. + dwc_otg_request_nuke(ep);
  51596. + /* prevent new request submissions, kill any outstanding requests */
  51597. + for (i = 0; i < num_in_eps; i++) {
  51598. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  51599. + dwc_otg_request_nuke(ep);
  51600. + }
  51601. + /* prevent new request submissions, kill any outstanding requests */
  51602. + for (i = 0; i < num_out_eps; i++) {
  51603. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  51604. + dwc_otg_request_nuke(ep);
  51605. + }
  51606. +
  51607. + /* report disconnect; the driver is already quiesced */
  51608. + if (pcd->fops->disconnect) {
  51609. + DWC_SPINUNLOCK(pcd->lock);
  51610. + pcd->fops->disconnect(pcd);
  51611. + DWC_SPINLOCK(pcd->lock);
  51612. + }
  51613. + DWC_SPINUNLOCK(pcd->lock);
  51614. +}
  51615. +
  51616. +/**
  51617. + * This interrupt indicates that ...
  51618. + */
  51619. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  51620. +{
  51621. + gintmsk_data_t intr_mask = {.d32 = 0 };
  51622. + gintsts_data_t gintsts;
  51623. +
  51624. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  51625. + intr_mask.b.i2cintr = 1;
  51626. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  51627. + intr_mask.d32, 0);
  51628. +
  51629. + /* Clear interrupt */
  51630. + gintsts.d32 = 0;
  51631. + gintsts.b.i2cintr = 1;
  51632. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  51633. + gintsts.d32);
  51634. + return 1;
  51635. +}
  51636. +
  51637. +/**
  51638. + * This interrupt indicates that ...
  51639. + */
  51640. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  51641. +{
  51642. + gintsts_data_t gintsts;
  51643. +#if defined(VERBOSE)
  51644. + DWC_PRINTF("Early Suspend Detected\n");
  51645. +#endif
  51646. +
  51647. + /* Clear interrupt */
  51648. + gintsts.d32 = 0;
  51649. + gintsts.b.erlysuspend = 1;
  51650. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  51651. + gintsts.d32);
  51652. + return 1;
  51653. +}
  51654. +
  51655. +/**
  51656. + * This function configures EPO to receive SETUP packets.
  51657. + *
  51658. + * @todo NGS: Update the comments from the HW FS.
  51659. + *
  51660. + * -# Program the following fields in the endpoint specific registers
  51661. + * for Control OUT EP 0, in order to receive a setup packet
  51662. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  51663. + * setup packets)
  51664. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  51665. + * to back setup packets)
  51666. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  51667. + * store any setup packets received
  51668. + *
  51669. + * @param core_if Programming view of DWC_otg controller.
  51670. + * @param pcd Programming view of the PCD.
  51671. + */
  51672. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  51673. + dwc_otg_pcd_t * pcd)
  51674. +{
  51675. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51676. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  51677. + dwc_otg_dev_dma_desc_t *dma_desc;
  51678. + depctl_data_t doepctl = {.d32 = 0 };
  51679. +
  51680. +#ifdef VERBOSE
  51681. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  51682. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  51683. +#endif
  51684. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  51685. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  51686. + if (doepctl.b.epena) {
  51687. + return;
  51688. + }
  51689. + }
  51690. +
  51691. + doeptsize0.b.supcnt = 3;
  51692. + doeptsize0.b.pktcnt = 1;
  51693. + doeptsize0.b.xfersize = 8 * 3;
  51694. +
  51695. + if (core_if->dma_enable) {
  51696. + if (!core_if->dma_desc_enable) {
  51697. + /** put here as for Hermes mode deptisz register should not be written */
  51698. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  51699. + doeptsize0.d32);
  51700. +
  51701. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  51702. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  51703. + pcd->setup_pkt_dma_handle);
  51704. + } else {
  51705. + dev_if->setup_desc_index =
  51706. + (dev_if->setup_desc_index + 1) & 1;
  51707. + dma_desc =
  51708. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  51709. +
  51710. + /** DMA Descriptor Setup */
  51711. + dma_desc->status.b.bs = BS_HOST_BUSY;
  51712. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  51713. + dma_desc->status.b.sr = 0;
  51714. + dma_desc->status.b.mtrf = 0;
  51715. + }
  51716. + dma_desc->status.b.l = 1;
  51717. + dma_desc->status.b.ioc = 1;
  51718. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  51719. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  51720. + dma_desc->status.b.sts = 0;
  51721. + dma_desc->status.b.bs = BS_HOST_READY;
  51722. +
  51723. + /** DOEPDMA0 Register write */
  51724. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  51725. + dev_if->dma_setup_desc_addr
  51726. + [dev_if->setup_desc_index]);
  51727. + }
  51728. +
  51729. + } else {
  51730. + /** put here as for Hermes mode deptisz register should not be written */
  51731. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  51732. + doeptsize0.d32);
  51733. + }
  51734. +
  51735. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  51736. + doepctl.d32 = 0;
  51737. + doepctl.b.epena = 1;
  51738. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  51739. + doepctl.b.cnak = 1;
  51740. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  51741. + } else {
  51742. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  51743. + }
  51744. +
  51745. +#ifdef VERBOSE
  51746. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  51747. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  51748. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  51749. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  51750. +#endif
  51751. +}
  51752. +
  51753. +/**
  51754. + * This interrupt occurs when a USB Reset is detected. When the USB
  51755. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  51756. + * EP0 state is set to IDLE.
  51757. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  51758. + * -# Unmask the following interrupt bits
  51759. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  51760. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  51761. + * - DOEPMSK.SETUP = 1
  51762. + * - DOEPMSK.XferCompl = 1
  51763. + * - DIEPMSK.XferCompl = 1
  51764. + * - DIEPMSK.TimeOut = 1
  51765. + * -# Program the following fields in the endpoint specific registers
  51766. + * for Control OUT EP 0, in order to receive a setup packet
  51767. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  51768. + * setup packets)
  51769. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  51770. + * to back setup packets)
  51771. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  51772. + * store any setup packets received
  51773. + * At this point, all the required initialization, except for enabling
  51774. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  51775. + */
  51776. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  51777. +{
  51778. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51779. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51780. + depctl_data_t doepctl = {.d32 = 0 };
  51781. + depctl_data_t diepctl = {.d32 = 0 };
  51782. + daint_data_t daintmsk = {.d32 = 0 };
  51783. + doepmsk_data_t doepmsk = {.d32 = 0 };
  51784. + diepmsk_data_t diepmsk = {.d32 = 0 };
  51785. + dcfg_data_t dcfg = {.d32 = 0 };
  51786. + grstctl_t resetctl = {.d32 = 0 };
  51787. + dctl_data_t dctl = {.d32 = 0 };
  51788. + int i = 0;
  51789. + gintsts_data_t gintsts;
  51790. + pcgcctl_data_t power = {.d32 = 0 };
  51791. +
  51792. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  51793. + if (power.b.stoppclk) {
  51794. + power.d32 = 0;
  51795. + power.b.stoppclk = 1;
  51796. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51797. +
  51798. + power.b.pwrclmp = 1;
  51799. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51800. +
  51801. + power.b.rstpdwnmodule = 1;
  51802. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51803. + }
  51804. +
  51805. + core_if->lx_state = DWC_OTG_L0;
  51806. +
  51807. + DWC_PRINTF("USB RESET\n");
  51808. +#ifdef DWC_EN_ISOC
  51809. + for (i = 1; i < 16; ++i) {
  51810. + dwc_otg_pcd_ep_t *ep;
  51811. + dwc_ep_t *dwc_ep;
  51812. + ep = get_in_ep(pcd, i);
  51813. + if (ep != 0) {
  51814. + dwc_ep = &ep->dwc_ep;
  51815. + dwc_ep->next_frame = 0xffffffff;
  51816. + }
  51817. + }
  51818. +#endif /* DWC_EN_ISOC */
  51819. +
  51820. + /* reset the HNP settings */
  51821. + dwc_otg_pcd_update_otg(pcd, 1);
  51822. +
  51823. + /* Clear the Remote Wakeup Signalling */
  51824. + dctl.b.rmtwkupsig = 1;
  51825. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  51826. +
  51827. + /* Set NAK for all OUT EPs */
  51828. + doepctl.b.snak = 1;
  51829. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  51830. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  51831. + }
  51832. +
  51833. + /* Flush the NP Tx FIFO */
  51834. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  51835. + /* Flush the Learning Queue */
  51836. + resetctl.b.intknqflsh = 1;
  51837. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  51838. +
  51839. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  51840. + core_if->start_predict = 0;
  51841. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  51842. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  51843. + }
  51844. + core_if->nextep_seq[0] = 0;
  51845. + core_if->first_in_nextep_seq = 0;
  51846. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  51847. + diepctl.b.nextep = 0;
  51848. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  51849. +
  51850. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  51851. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  51852. + dcfg.b.epmscnt = 2;
  51853. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  51854. +
  51855. + DWC_DEBUGPL(DBG_PCDV,
  51856. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  51857. + __func__, core_if->first_in_nextep_seq);
  51858. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  51859. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  51860. + }
  51861. + }
  51862. +
  51863. + if (core_if->multiproc_int_enable) {
  51864. + daintmsk.b.inep0 = 1;
  51865. + daintmsk.b.outep0 = 1;
  51866. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  51867. + daintmsk.d32);
  51868. +
  51869. + doepmsk.b.setup = 1;
  51870. + doepmsk.b.xfercompl = 1;
  51871. + doepmsk.b.ahberr = 1;
  51872. + doepmsk.b.epdisabled = 1;
  51873. +
  51874. + if ((core_if->dma_desc_enable) ||
  51875. + (core_if->dma_enable
  51876. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  51877. + doepmsk.b.stsphsercvd = 1;
  51878. + }
  51879. + if (core_if->dma_desc_enable)
  51880. + doepmsk.b.bna = 1;
  51881. +/*
  51882. + doepmsk.b.babble = 1;
  51883. + doepmsk.b.nyet = 1;
  51884. +
  51885. + if (core_if->dma_enable) {
  51886. + doepmsk.b.nak = 1;
  51887. + }
  51888. +*/
  51889. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  51890. + doepmsk.d32);
  51891. +
  51892. + diepmsk.b.xfercompl = 1;
  51893. + diepmsk.b.timeout = 1;
  51894. + diepmsk.b.epdisabled = 1;
  51895. + diepmsk.b.ahberr = 1;
  51896. + diepmsk.b.intknepmis = 1;
  51897. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  51898. + diepmsk.b.intknepmis = 0;
  51899. +
  51900. +/* if (core_if->dma_desc_enable) {
  51901. + diepmsk.b.bna = 1;
  51902. + }
  51903. +*/
  51904. +/*
  51905. + if (core_if->dma_enable) {
  51906. + diepmsk.b.nak = 1;
  51907. + }
  51908. +*/
  51909. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  51910. + diepmsk.d32);
  51911. + } else {
  51912. + daintmsk.b.inep0 = 1;
  51913. + daintmsk.b.outep0 = 1;
  51914. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  51915. + daintmsk.d32);
  51916. +
  51917. + doepmsk.b.setup = 1;
  51918. + doepmsk.b.xfercompl = 1;
  51919. + doepmsk.b.ahberr = 1;
  51920. + doepmsk.b.epdisabled = 1;
  51921. +
  51922. + if ((core_if->dma_desc_enable) ||
  51923. + (core_if->dma_enable
  51924. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  51925. + doepmsk.b.stsphsercvd = 1;
  51926. + }
  51927. + if (core_if->dma_desc_enable)
  51928. + doepmsk.b.bna = 1;
  51929. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  51930. +
  51931. + diepmsk.b.xfercompl = 1;
  51932. + diepmsk.b.timeout = 1;
  51933. + diepmsk.b.epdisabled = 1;
  51934. + diepmsk.b.ahberr = 1;
  51935. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  51936. + diepmsk.b.intknepmis = 0;
  51937. +/*
  51938. + if (core_if->dma_desc_enable) {
  51939. + diepmsk.b.bna = 1;
  51940. + }
  51941. +*/
  51942. +
  51943. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  51944. + }
  51945. +
  51946. + /* Reset Device Address */
  51947. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  51948. + dcfg.b.devaddr = 0;
  51949. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  51950. +
  51951. + /* setup EP0 to receive SETUP packets */
  51952. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  51953. + ep0_out_start(core_if, pcd);
  51954. +
  51955. + /* Clear interrupt */
  51956. + gintsts.d32 = 0;
  51957. + gintsts.b.usbreset = 1;
  51958. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  51959. +
  51960. + return 1;
  51961. +}
  51962. +
  51963. +/**
  51964. + * Get the device speed from the device status register and convert it
  51965. + * to USB speed constant.
  51966. + *
  51967. + * @param core_if Programming view of DWC_otg controller.
  51968. + */
  51969. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  51970. +{
  51971. + dsts_data_t dsts;
  51972. + int speed = 0;
  51973. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  51974. +
  51975. + switch (dsts.b.enumspd) {
  51976. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  51977. + speed = USB_SPEED_HIGH;
  51978. + break;
  51979. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  51980. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  51981. + speed = USB_SPEED_FULL;
  51982. + break;
  51983. +
  51984. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  51985. + speed = USB_SPEED_LOW;
  51986. + break;
  51987. + }
  51988. +
  51989. + return speed;
  51990. +}
  51991. +
  51992. +/**
  51993. + * Read the device status register and set the device speed in the
  51994. + * data structure.
  51995. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  51996. + */
  51997. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  51998. +{
  51999. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52000. + gintsts_data_t gintsts;
  52001. + gusbcfg_data_t gusbcfg;
  52002. + dwc_otg_core_global_regs_t *global_regs =
  52003. + GET_CORE_IF(pcd)->core_global_regs;
  52004. + uint8_t utmi16b, utmi8b;
  52005. + int speed;
  52006. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  52007. +
  52008. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  52009. + utmi16b = 6; //vahrama old value was 6;
  52010. + utmi8b = 9;
  52011. + } else {
  52012. + utmi16b = 4;
  52013. + utmi8b = 8;
  52014. + }
  52015. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52016. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  52017. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52018. + }
  52019. +
  52020. +#ifdef DEBUG_EP0
  52021. + print_ep0_state(pcd);
  52022. +#endif
  52023. +
  52024. + if (pcd->ep0state == EP0_DISCONNECT) {
  52025. + pcd->ep0state = EP0_IDLE;
  52026. + } else if (pcd->ep0state == EP0_STALL) {
  52027. + pcd->ep0state = EP0_IDLE;
  52028. + }
  52029. +
  52030. + pcd->ep0state = EP0_IDLE;
  52031. +
  52032. + ep0->stopped = 0;
  52033. +
  52034. + speed = get_device_speed(GET_CORE_IF(pcd));
  52035. + pcd->fops->connect(pcd, speed);
  52036. +
  52037. + /* Set USB turnaround time based on device speed and PHY interface. */
  52038. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  52039. + if (speed == USB_SPEED_HIGH) {
  52040. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52041. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  52042. + /* ULPI interface */
  52043. + gusbcfg.b.usbtrdtim = 9;
  52044. + }
  52045. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52046. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  52047. + /* UTMI+ interface */
  52048. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  52049. + gusbcfg.b.usbtrdtim = utmi8b;
  52050. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  52051. + b.utmi_phy_data_width == 1) {
  52052. + gusbcfg.b.usbtrdtim = utmi16b;
  52053. + } else if (GET_CORE_IF(pcd)->
  52054. + core_params->phy_utmi_width == 8) {
  52055. + gusbcfg.b.usbtrdtim = utmi8b;
  52056. + } else {
  52057. + gusbcfg.b.usbtrdtim = utmi16b;
  52058. + }
  52059. + }
  52060. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52061. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  52062. + /* UTMI+ OR ULPI interface */
  52063. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  52064. + /* ULPI interface */
  52065. + gusbcfg.b.usbtrdtim = 9;
  52066. + } else {
  52067. + /* UTMI+ interface */
  52068. + if (GET_CORE_IF(pcd)->
  52069. + core_params->phy_utmi_width == 16) {
  52070. + gusbcfg.b.usbtrdtim = utmi16b;
  52071. + } else {
  52072. + gusbcfg.b.usbtrdtim = utmi8b;
  52073. + }
  52074. + }
  52075. + }
  52076. + } else {
  52077. + /* Full or low speed */
  52078. + gusbcfg.b.usbtrdtim = 9;
  52079. + }
  52080. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  52081. +
  52082. + /* Clear interrupt */
  52083. + gintsts.d32 = 0;
  52084. + gintsts.b.enumdone = 1;
  52085. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52086. + gintsts.d32);
  52087. + return 1;
  52088. +}
  52089. +
  52090. +/**
  52091. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  52092. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  52093. + * read all the data from the Rx FIFO.
  52094. + */
  52095. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  52096. +{
  52097. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52098. + gintsts_data_t gintsts;
  52099. +
  52100. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  52101. + "ISOC Out Dropped");
  52102. +
  52103. + intr_mask.b.isooutdrop = 1;
  52104. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  52105. + intr_mask.d32, 0);
  52106. +
  52107. + /* Clear interrupt */
  52108. + gintsts.d32 = 0;
  52109. + gintsts.b.isooutdrop = 1;
  52110. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52111. + gintsts.d32);
  52112. +
  52113. + return 1;
  52114. +}
  52115. +
  52116. +/**
  52117. + * This interrupt indicates the end of the portion of the micro-frame
  52118. + * for periodic transactions. If there is a periodic transaction for
  52119. + * the next frame, load the packets into the EP periodic Tx FIFO.
  52120. + */
  52121. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  52122. +{
  52123. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52124. + gintsts_data_t gintsts;
  52125. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  52126. +
  52127. + intr_mask.b.eopframe = 1;
  52128. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  52129. + intr_mask.d32, 0);
  52130. +
  52131. + /* Clear interrupt */
  52132. + gintsts.d32 = 0;
  52133. + gintsts.b.eopframe = 1;
  52134. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52135. + gintsts.d32);
  52136. +
  52137. + return 1;
  52138. +}
  52139. +
  52140. +/**
  52141. + * This interrupt indicates that EP of the packet on the top of the
  52142. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  52143. + *
  52144. + * The "Device IN Token Queue" Registers are read to determine the
  52145. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  52146. + * is flushed, so it can be reloaded in the order seen in the IN Token
  52147. + * Queue.
  52148. + */
  52149. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  52150. +{
  52151. + gintsts_data_t gintsts;
  52152. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52153. + dctl_data_t dctl;
  52154. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52155. +
  52156. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  52157. + core_if->start_predict = 1;
  52158. +
  52159. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  52160. +
  52161. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  52162. + if (!gintsts.b.ginnakeff) {
  52163. + /* Disable EP Mismatch interrupt */
  52164. + intr_mask.d32 = 0;
  52165. + intr_mask.b.epmismatch = 1;
  52166. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  52167. + /* Enable the Global IN NAK Effective Interrupt */
  52168. + intr_mask.d32 = 0;
  52169. + intr_mask.b.ginnakeff = 1;
  52170. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  52171. + /* Set the global non-periodic IN NAK handshake */
  52172. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  52173. + dctl.b.sgnpinnak = 1;
  52174. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  52175. + } else {
  52176. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  52177. + }
  52178. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  52179. + * handler after Global IN NAK Effective interrupt will be asserted */
  52180. + }
  52181. + /* Clear interrupt */
  52182. + gintsts.d32 = 0;
  52183. + gintsts.b.epmismatch = 1;
  52184. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52185. +
  52186. + return 1;
  52187. +}
  52188. +
  52189. +/**
  52190. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  52191. + * core has stopped fetching data for IN endpoints due to the unavailability of
  52192. + * TxFIFO space or Request Queue space. This interrupt is used by the
  52193. + * application for an endpoint mismatch algorithm.
  52194. + *
  52195. + * @param pcd The PCD
  52196. + */
  52197. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  52198. +{
  52199. + gintsts_data_t gintsts;
  52200. + gintmsk_data_t gintmsk_data;
  52201. + dctl_data_t dctl;
  52202. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52203. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  52204. +
  52205. + /* Clear the global non-periodic IN NAK handshake */
  52206. + dctl.d32 = 0;
  52207. + dctl.b.cgnpinnak = 1;
  52208. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  52209. +
  52210. + /* Mask GINTSTS.FETSUSP interrupt */
  52211. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  52212. + gintmsk_data.b.fetsusp = 0;
  52213. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  52214. +
  52215. + /* Clear interrupt */
  52216. + gintsts.d32 = 0;
  52217. + gintsts.b.fetsusp = 1;
  52218. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52219. +
  52220. + return 1;
  52221. +}
  52222. +/**
  52223. + * This funcion stalls EP0.
  52224. + */
  52225. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  52226. +{
  52227. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52228. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  52229. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  52230. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  52231. +
  52232. + ep0->dwc_ep.is_in = 1;
  52233. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52234. + pcd->ep0.stopped = 1;
  52235. + pcd->ep0state = EP0_IDLE;
  52236. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52237. +}
  52238. +
  52239. +/**
  52240. + * This functions delegates the setup command to the gadget driver.
  52241. + */
  52242. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  52243. + usb_device_request_t * ctrl)
  52244. +{
  52245. + int ret = 0;
  52246. + DWC_SPINUNLOCK(pcd->lock);
  52247. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  52248. + DWC_SPINLOCK(pcd->lock);
  52249. + if (ret < 0) {
  52250. + ep0_do_stall(pcd, ret);
  52251. + }
  52252. +
  52253. + /** @todo This is a g_file_storage gadget driver specific
  52254. + * workaround: a DELAYED_STATUS result from the fsg_setup
  52255. + * routine will result in the gadget queueing a EP0 IN status
  52256. + * phase for a two-stage control transfer. Exactly the same as
  52257. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  52258. + * specific request. Need a generic way to know when the gadget
  52259. + * driver will queue the status phase. Can we assume when we
  52260. + * call the gadget driver setup() function that it will always
  52261. + * queue and require the following flag? Need to look into
  52262. + * this.
  52263. + */
  52264. +
  52265. + if (ret == 256 + 999) {
  52266. + pcd->request_config = 1;
  52267. + }
  52268. +}
  52269. +
  52270. +#ifdef DWC_UTE_CFI
  52271. +/**
  52272. + * This functions delegates the CFI setup commands to the gadget driver.
  52273. + * This function will return a negative value to indicate a failure.
  52274. + */
  52275. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  52276. + struct cfi_usb_ctrlrequest *ctrl_req)
  52277. +{
  52278. + int ret = 0;
  52279. +
  52280. + if (pcd->fops && pcd->fops->cfi_setup) {
  52281. + DWC_SPINUNLOCK(pcd->lock);
  52282. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  52283. + DWC_SPINLOCK(pcd->lock);
  52284. + if (ret < 0) {
  52285. + ep0_do_stall(pcd, ret);
  52286. + return ret;
  52287. + }
  52288. + }
  52289. +
  52290. + return ret;
  52291. +}
  52292. +#endif
  52293. +
  52294. +/**
  52295. + * This function starts the Zero-Length Packet for the IN status phase
  52296. + * of a 2 stage control transfer.
  52297. + */
  52298. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  52299. +{
  52300. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52301. + if (pcd->ep0state == EP0_STALL) {
  52302. + return;
  52303. + }
  52304. +
  52305. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  52306. +
  52307. + /* Prepare for more SETUP Packets */
  52308. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  52309. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  52310. + && (pcd->core_if->dma_desc_enable)
  52311. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  52312. + DWC_DEBUGPL(DBG_PCDV,
  52313. + "Data terminated wait next packet in out_desc_addr\n");
  52314. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  52315. + pcd->data_terminated = 1;
  52316. + }
  52317. + ep0->dwc_ep.xfer_len = 0;
  52318. + ep0->dwc_ep.xfer_count = 0;
  52319. + ep0->dwc_ep.is_in = 1;
  52320. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  52321. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52322. +
  52323. + /* Prepare for more SETUP Packets */
  52324. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  52325. +}
  52326. +
  52327. +/**
  52328. + * This function starts the Zero-Length Packet for the OUT status phase
  52329. + * of a 2 stage control transfer.
  52330. + */
  52331. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  52332. +{
  52333. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52334. + if (pcd->ep0state == EP0_STALL) {
  52335. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  52336. + return;
  52337. + }
  52338. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  52339. +
  52340. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  52341. + ep0->dwc_ep.xfer_len = 0;
  52342. + ep0->dwc_ep.xfer_count = 0;
  52343. + ep0->dwc_ep.is_in = 0;
  52344. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  52345. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52346. +
  52347. + /* Prepare for more SETUP Packets */
  52348. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  52349. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52350. + }
  52351. +}
  52352. +
  52353. +/**
  52354. + * Clear the EP halt (STALL) and if pending requests start the
  52355. + * transfer.
  52356. + */
  52357. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  52358. +{
  52359. + if (ep->dwc_ep.stall_clear_flag == 0)
  52360. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  52361. +
  52362. + /* Reactive the EP */
  52363. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  52364. + if (ep->stopped) {
  52365. + ep->stopped = 0;
  52366. + /* If there is a request in the EP queue start it */
  52367. +
  52368. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  52369. + * epmismatch not yet implemented. */
  52370. +
  52371. + /*
  52372. + * Above fixme is solved by implmenting a tasklet to call the
  52373. + * start_next_request(), outside of interrupt context at some
  52374. + * time after the current time, after a clear-halt setup packet.
  52375. + * Still need to implement ep mismatch in the future if a gadget
  52376. + * ever uses more than one endpoint at once
  52377. + */
  52378. + ep->queue_sof = 1;
  52379. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  52380. + }
  52381. + /* Start Control Status Phase */
  52382. + do_setup_in_status_phase(pcd);
  52383. +}
  52384. +
  52385. +/**
  52386. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  52387. + * is sent from the host. The Device Control register is written with
  52388. + * the Test Mode bits set to the specified Test Mode. This is done as
  52389. + * a tasklet so that the "Status" phase of the control transfer
  52390. + * completes before transmitting the TEST packets.
  52391. + *
  52392. + * @todo This has not been tested since the tasklet struct was put
  52393. + * into the PCD struct!
  52394. + *
  52395. + */
  52396. +void do_test_mode(void *data)
  52397. +{
  52398. + dctl_data_t dctl;
  52399. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  52400. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52401. + int test_mode = pcd->test_mode;
  52402. +
  52403. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  52404. +
  52405. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  52406. + switch (test_mode) {
  52407. + case 1: // TEST_J
  52408. + dctl.b.tstctl = 1;
  52409. + break;
  52410. +
  52411. + case 2: // TEST_K
  52412. + dctl.b.tstctl = 2;
  52413. + break;
  52414. +
  52415. + case 3: // TEST_SE0_NAK
  52416. + dctl.b.tstctl = 3;
  52417. + break;
  52418. +
  52419. + case 4: // TEST_PACKET
  52420. + dctl.b.tstctl = 4;
  52421. + break;
  52422. +
  52423. + case 5: // TEST_FORCE_ENABLE
  52424. + dctl.b.tstctl = 5;
  52425. + break;
  52426. + }
  52427. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  52428. +}
  52429. +
  52430. +/**
  52431. + * This function process the GET_STATUS Setup Commands.
  52432. + */
  52433. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  52434. +{
  52435. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52436. + dwc_otg_pcd_ep_t *ep;
  52437. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52438. + uint16_t *status = pcd->status_buf;
  52439. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52440. +
  52441. +#ifdef DEBUG_EP0
  52442. + DWC_DEBUGPL(DBG_PCD,
  52443. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  52444. + ctrl.bmRequestType, ctrl.bRequest,
  52445. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52446. + UGETW(ctrl.wLength));
  52447. +#endif
  52448. +
  52449. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52450. + case UT_DEVICE:
  52451. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  52452. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  52453. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  52454. + DWC_PRINTF("OTG CAP - %d, %d\n",
  52455. + core_if->core_params->otg_cap,
  52456. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  52457. + if (core_if->otg_ver == 1
  52458. + && core_if->core_params->otg_cap ==
  52459. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52460. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  52461. + *otgsts = (core_if->otg_sts & 0x1);
  52462. + pcd->ep0_pending = 1;
  52463. + ep0->dwc_ep.start_xfer_buff =
  52464. + (uint8_t *) otgsts;
  52465. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  52466. + ep0->dwc_ep.dma_addr =
  52467. + pcd->status_buf_dma_handle;
  52468. + ep0->dwc_ep.xfer_len = 1;
  52469. + ep0->dwc_ep.xfer_count = 0;
  52470. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  52471. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  52472. + &ep0->dwc_ep);
  52473. + return;
  52474. + } else {
  52475. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52476. + return;
  52477. + }
  52478. + break;
  52479. + } else {
  52480. + *status = 0x1; /* Self powered */
  52481. + *status |= pcd->remote_wakeup_enable << 1;
  52482. + break;
  52483. + }
  52484. + case UT_INTERFACE:
  52485. + *status = 0;
  52486. + break;
  52487. +
  52488. + case UT_ENDPOINT:
  52489. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52490. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  52491. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52492. + return;
  52493. + }
  52494. + /** @todo check for EP stall */
  52495. + *status = ep->stopped;
  52496. + break;
  52497. + }
  52498. + pcd->ep0_pending = 1;
  52499. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  52500. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  52501. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  52502. + ep0->dwc_ep.xfer_len = 2;
  52503. + ep0->dwc_ep.xfer_count = 0;
  52504. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  52505. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52506. +}
  52507. +
  52508. +/**
  52509. + * This function process the SET_FEATURE Setup Commands.
  52510. + */
  52511. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  52512. +{
  52513. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52514. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  52515. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52516. + dwc_otg_pcd_ep_t *ep = 0;
  52517. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  52518. + gotgctl_data_t gotgctl = {.d32 = 0 };
  52519. +
  52520. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  52521. + ctrl.bmRequestType, ctrl.bRequest,
  52522. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52523. + UGETW(ctrl.wLength));
  52524. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  52525. +
  52526. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52527. + case UT_DEVICE:
  52528. + switch (UGETW(ctrl.wValue)) {
  52529. + case UF_DEVICE_REMOTE_WAKEUP:
  52530. + pcd->remote_wakeup_enable = 1;
  52531. + break;
  52532. +
  52533. + case UF_TEST_MODE:
  52534. + /* Setup the Test Mode tasklet to do the Test
  52535. + * Packet generation after the SETUP Status
  52536. + * phase has completed. */
  52537. +
  52538. + /** @todo This has not been tested since the
  52539. + * tasklet struct was put into the PCD
  52540. + * struct! */
  52541. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  52542. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  52543. + break;
  52544. +
  52545. + case UF_DEVICE_B_HNP_ENABLE:
  52546. + DWC_DEBUGPL(DBG_PCDV,
  52547. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  52548. +
  52549. + /* dev may initiate HNP */
  52550. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52551. + pcd->b_hnp_enable = 1;
  52552. + dwc_otg_pcd_update_otg(pcd, 0);
  52553. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  52554. + /**@todo Is the gotgctl.devhnpen cleared
  52555. + * by a USB Reset? */
  52556. + gotgctl.b.devhnpen = 1;
  52557. + gotgctl.b.hnpreq = 1;
  52558. + DWC_WRITE_REG32(&global_regs->gotgctl,
  52559. + gotgctl.d32);
  52560. + } else {
  52561. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52562. + return;
  52563. + }
  52564. + break;
  52565. +
  52566. + case UF_DEVICE_A_HNP_SUPPORT:
  52567. + /* RH port supports HNP */
  52568. + DWC_DEBUGPL(DBG_PCDV,
  52569. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  52570. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52571. + pcd->a_hnp_support = 1;
  52572. + dwc_otg_pcd_update_otg(pcd, 0);
  52573. + } else {
  52574. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52575. + return;
  52576. + }
  52577. + break;
  52578. +
  52579. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  52580. + /* other RH port does */
  52581. + DWC_DEBUGPL(DBG_PCDV,
  52582. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  52583. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52584. + pcd->a_alt_hnp_support = 1;
  52585. + dwc_otg_pcd_update_otg(pcd, 0);
  52586. + } else {
  52587. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52588. + return;
  52589. + }
  52590. + break;
  52591. +
  52592. + default:
  52593. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52594. + return;
  52595. +
  52596. + }
  52597. + do_setup_in_status_phase(pcd);
  52598. + break;
  52599. +
  52600. + case UT_INTERFACE:
  52601. + do_gadget_setup(pcd, &ctrl);
  52602. + break;
  52603. +
  52604. + case UT_ENDPOINT:
  52605. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  52606. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52607. + if (ep == 0) {
  52608. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52609. + return;
  52610. + }
  52611. + ep->stopped = 1;
  52612. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  52613. + }
  52614. + do_setup_in_status_phase(pcd);
  52615. + break;
  52616. + }
  52617. +}
  52618. +
  52619. +/**
  52620. + * This function process the CLEAR_FEATURE Setup Commands.
  52621. + */
  52622. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  52623. +{
  52624. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52625. + dwc_otg_pcd_ep_t *ep = 0;
  52626. +
  52627. + DWC_DEBUGPL(DBG_PCD,
  52628. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  52629. + ctrl.bmRequestType, ctrl.bRequest,
  52630. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52631. + UGETW(ctrl.wLength));
  52632. +
  52633. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52634. + case UT_DEVICE:
  52635. + switch (UGETW(ctrl.wValue)) {
  52636. + case UF_DEVICE_REMOTE_WAKEUP:
  52637. + pcd->remote_wakeup_enable = 0;
  52638. + break;
  52639. +
  52640. + case UF_TEST_MODE:
  52641. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  52642. + break;
  52643. +
  52644. + default:
  52645. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52646. + return;
  52647. + }
  52648. + do_setup_in_status_phase(pcd);
  52649. + break;
  52650. +
  52651. + case UT_ENDPOINT:
  52652. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52653. + if (ep == 0) {
  52654. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52655. + return;
  52656. + }
  52657. +
  52658. + pcd_clear_halt(pcd, ep);
  52659. +
  52660. + break;
  52661. + }
  52662. +}
  52663. +
  52664. +/**
  52665. + * This function process the SET_ADDRESS Setup Commands.
  52666. + */
  52667. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  52668. +{
  52669. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  52670. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52671. +
  52672. + if (ctrl.bmRequestType == UT_DEVICE) {
  52673. + dcfg_data_t dcfg = {.d32 = 0 };
  52674. +
  52675. +#ifdef DEBUG_EP0
  52676. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  52677. +#endif
  52678. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  52679. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  52680. + do_setup_in_status_phase(pcd);
  52681. + }
  52682. +}
  52683. +
  52684. +/**
  52685. + * This function processes SETUP commands. In Linux, the USB Command
  52686. + * processing is done in two places - the first being the PCD and the
  52687. + * second in the Gadget Driver (for example, the File-Backed Storage
  52688. + * Gadget Driver).
  52689. + *
  52690. + * <table>
  52691. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  52692. + *
  52693. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  52694. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  52695. + * </td></tr>
  52696. + *
  52697. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  52698. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  52699. + * interface requests are ignored.</td></tr>
  52700. + *
  52701. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  52702. + * requests are processed by the PCD. Interface requests are passed
  52703. + * to the Gadget Driver.</td></tr>
  52704. + *
  52705. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  52706. + * with device address received </td></tr>
  52707. + *
  52708. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  52709. + * requested descriptor</td></tr>
  52710. + *
  52711. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  52712. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  52713. + *
  52714. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  52715. + * all EPs and enable EPs for new configuration.</td></tr>
  52716. + *
  52717. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  52718. + * the current configuration</td></tr>
  52719. + *
  52720. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  52721. + * EPs and enable EPs for new configuration.</td></tr>
  52722. + *
  52723. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  52724. + * current interface.</td></tr>
  52725. + *
  52726. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  52727. + * message.</td></tr>
  52728. + * </table>
  52729. + *
  52730. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  52731. + * processed by pcd_setup. Calling the Function Driver's setup function from
  52732. + * pcd_setup processes the gadget SETUP commands.
  52733. + */
  52734. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  52735. +{
  52736. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52737. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  52738. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52739. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52740. +
  52741. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  52742. +
  52743. +#ifdef DWC_UTE_CFI
  52744. + int retval = 0;
  52745. + struct cfi_usb_ctrlrequest cfi_req;
  52746. +#endif
  52747. +
  52748. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  52749. +
  52750. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  52751. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  52752. + && (doeptsize0.b.supcnt < 2)
  52753. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  52754. + DWC_ERROR
  52755. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  52756. + }
  52757. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  52758. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  52759. + ctrl =
  52760. + (pcd->setup_pkt +
  52761. + (3 - doeptsize0.b.supcnt - 1 +
  52762. + ep0->dwc_ep.stp_rollover))->req;
  52763. + }
  52764. +#ifdef DEBUG_EP0
  52765. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  52766. + ctrl.bmRequestType, ctrl.bRequest,
  52767. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52768. + UGETW(ctrl.wLength));
  52769. +#endif
  52770. +
  52771. + /* Clean up the request queue */
  52772. + dwc_otg_request_nuke(ep0);
  52773. + ep0->stopped = 0;
  52774. +
  52775. + if (ctrl.bmRequestType & UE_DIR_IN) {
  52776. + ep0->dwc_ep.is_in = 1;
  52777. + pcd->ep0state = EP0_IN_DATA_PHASE;
  52778. + } else {
  52779. + ep0->dwc_ep.is_in = 0;
  52780. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  52781. + }
  52782. +
  52783. + if (UGETW(ctrl.wLength) == 0) {
  52784. + ep0->dwc_ep.is_in = 1;
  52785. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  52786. + }
  52787. +
  52788. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  52789. +
  52790. +#ifdef DWC_UTE_CFI
  52791. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  52792. +
  52793. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  52794. + ctrl.bRequestType, ctrl.bRequest);
  52795. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  52796. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  52797. + retval = cfi_setup(pcd, &cfi_req);
  52798. + if (retval < 0) {
  52799. + ep0_do_stall(pcd, retval);
  52800. + pcd->ep0_pending = 0;
  52801. + return;
  52802. + }
  52803. +
  52804. + /* if need gadget setup then call it and check the retval */
  52805. + if (pcd->cfi->need_gadget_att) {
  52806. + retval =
  52807. + cfi_gadget_setup(pcd,
  52808. + &pcd->
  52809. + cfi->ctrl_req);
  52810. + if (retval < 0) {
  52811. + pcd->ep0_pending = 0;
  52812. + return;
  52813. + }
  52814. + }
  52815. +
  52816. + if (pcd->cfi->need_status_in_complete) {
  52817. + do_setup_in_status_phase(pcd);
  52818. + }
  52819. + return;
  52820. + }
  52821. + }
  52822. +#endif
  52823. +
  52824. + /* handle non-standard (class/vendor) requests in the gadget driver */
  52825. + do_gadget_setup(pcd, &ctrl);
  52826. + return;
  52827. + }
  52828. +
  52829. + /** @todo NGS: Handle bad setup packet? */
  52830. +
  52831. +///////////////////////////////////////////
  52832. +//// --- Standard Request handling --- ////
  52833. +
  52834. + switch (ctrl.bRequest) {
  52835. + case UR_GET_STATUS:
  52836. + do_get_status(pcd);
  52837. + break;
  52838. +
  52839. + case UR_CLEAR_FEATURE:
  52840. + do_clear_feature(pcd);
  52841. + break;
  52842. +
  52843. + case UR_SET_FEATURE:
  52844. + do_set_feature(pcd);
  52845. + break;
  52846. +
  52847. + case UR_SET_ADDRESS:
  52848. + do_set_address(pcd);
  52849. + break;
  52850. +
  52851. + case UR_SET_INTERFACE:
  52852. + case UR_SET_CONFIG:
  52853. +// _pcd->request_config = 1; /* Configuration changed */
  52854. + do_gadget_setup(pcd, &ctrl);
  52855. + break;
  52856. +
  52857. + case UR_SYNCH_FRAME:
  52858. + do_gadget_setup(pcd, &ctrl);
  52859. + break;
  52860. +
  52861. + default:
  52862. + /* Call the Gadget Driver's setup functions */
  52863. + do_gadget_setup(pcd, &ctrl);
  52864. + break;
  52865. + }
  52866. +}
  52867. +
  52868. +/**
  52869. + * This function completes the ep0 control transfer.
  52870. + */
  52871. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  52872. +{
  52873. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  52874. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  52875. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  52876. + dev_if->in_ep_regs[ep->dwc_ep.num];
  52877. +#ifdef DEBUG_EP0
  52878. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  52879. + dev_if->out_ep_regs[ep->dwc_ep.num];
  52880. +#endif
  52881. + deptsiz0_data_t deptsiz;
  52882. + dev_dma_desc_sts_t desc_sts;
  52883. + dwc_otg_pcd_request_t *req;
  52884. + int is_last = 0;
  52885. + dwc_otg_pcd_t *pcd = ep->pcd;
  52886. +
  52887. +#ifdef DWC_UTE_CFI
  52888. + struct cfi_usb_ctrlrequest *ctrlreq;
  52889. + int retval = -DWC_E_NOT_SUPPORTED;
  52890. +#endif
  52891. +
  52892. + desc_sts.b.bytes = 0;
  52893. +
  52894. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  52895. + if (ep->dwc_ep.is_in) {
  52896. +#ifdef DEBUG_EP0
  52897. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  52898. +#endif
  52899. + do_setup_out_status_phase(pcd);
  52900. + } else {
  52901. +#ifdef DEBUG_EP0
  52902. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  52903. +#endif
  52904. +
  52905. +#ifdef DWC_UTE_CFI
  52906. + ctrlreq = &pcd->cfi->ctrl_req;
  52907. +
  52908. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  52909. + if (ctrlreq->bRequest > 0xB0
  52910. + && ctrlreq->bRequest < 0xBF) {
  52911. +
  52912. + /* Return if the PCD failed to handle the request */
  52913. + if ((retval =
  52914. + pcd->cfi->ops.
  52915. + ctrl_write_complete(pcd->cfi,
  52916. + pcd)) < 0) {
  52917. + CFI_INFO
  52918. + ("ERROR setting a new value in the PCD(%d)\n",
  52919. + retval);
  52920. + ep0_do_stall(pcd, retval);
  52921. + pcd->ep0_pending = 0;
  52922. + return 0;
  52923. + }
  52924. +
  52925. + /* If the gadget needs to be notified on the request */
  52926. + if (pcd->cfi->need_gadget_att == 1) {
  52927. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  52928. + retval =
  52929. + cfi_gadget_setup(pcd,
  52930. + &pcd->cfi->
  52931. + ctrl_req);
  52932. +
  52933. + /* Return from the function if the gadget failed to process
  52934. + * the request properly - this should never happen !!!
  52935. + */
  52936. + if (retval < 0) {
  52937. + CFI_INFO
  52938. + ("ERROR setting a new value in the gadget(%d)\n",
  52939. + retval);
  52940. + pcd->ep0_pending = 0;
  52941. + return 0;
  52942. + }
  52943. + }
  52944. +
  52945. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  52946. + retval);
  52947. + /* If we hit here then the PCD and the gadget has properly
  52948. + * handled the request - so send the ZLP IN to the host.
  52949. + */
  52950. + /* @todo: MAS - decide whether we need to start the setup
  52951. + * stage based on the need_setup value of the cfi object
  52952. + */
  52953. + do_setup_in_status_phase(pcd);
  52954. + pcd->ep0_pending = 0;
  52955. + return 1;
  52956. + }
  52957. + }
  52958. +#endif
  52959. +
  52960. + do_setup_in_status_phase(pcd);
  52961. + }
  52962. + pcd->ep0_pending = 0;
  52963. + return 1;
  52964. + }
  52965. +
  52966. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  52967. + return 0;
  52968. + }
  52969. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  52970. +
  52971. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  52972. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  52973. + is_last = 1;
  52974. + } else if (ep->dwc_ep.is_in) {
  52975. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  52976. + if (core_if->dma_desc_enable != 0)
  52977. + desc_sts = dev_if->in_desc_addr->status;
  52978. +#ifdef DEBUG_EP0
  52979. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  52980. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  52981. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  52982. +#endif
  52983. +
  52984. + if (((core_if->dma_desc_enable == 0)
  52985. + && (deptsiz.b.xfersize == 0))
  52986. + || ((core_if->dma_desc_enable != 0)
  52987. + && (desc_sts.b.bytes == 0))) {
  52988. + req->actual = ep->dwc_ep.xfer_count;
  52989. + /* Is a Zero Len Packet needed? */
  52990. + if (req->sent_zlp) {
  52991. +#ifdef DEBUG_EP0
  52992. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  52993. +#endif
  52994. + req->sent_zlp = 0;
  52995. + }
  52996. + do_setup_out_status_phase(pcd);
  52997. + }
  52998. + } else {
  52999. + /* ep0-OUT */
  53000. +#ifdef DEBUG_EP0
  53001. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  53002. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  53003. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  53004. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53005. +#endif
  53006. + req->actual = ep->dwc_ep.xfer_count;
  53007. +
  53008. + /* Is a Zero Len Packet needed? */
  53009. + if (req->sent_zlp) {
  53010. +#ifdef DEBUG_EP0
  53011. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  53012. +#endif
  53013. + req->sent_zlp = 0;
  53014. + }
  53015. + /* For older cores do setup in status phase in Slave/BDMA modes,
  53016. + * starting from 3.00 do that only in slave, and for DMA modes
  53017. + * just re-enable ep 0 OUT here*/
  53018. + if (core_if->dma_enable == 0
  53019. + || (core_if->dma_desc_enable == 0
  53020. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  53021. + do_setup_in_status_phase(pcd);
  53022. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  53023. + DWC_DEBUGPL(DBG_PCDV,
  53024. + "Enable out ep before in status phase\n");
  53025. + ep0_out_start(core_if, pcd);
  53026. + }
  53027. + }
  53028. +
  53029. + /* Complete the request */
  53030. + if (is_last) {
  53031. + dwc_otg_request_done(ep, req, 0);
  53032. + ep->dwc_ep.start_xfer_buff = 0;
  53033. + ep->dwc_ep.xfer_buff = 0;
  53034. + ep->dwc_ep.xfer_len = 0;
  53035. + return 1;
  53036. + }
  53037. + return 0;
  53038. +}
  53039. +
  53040. +#ifdef DWC_UTE_CFI
  53041. +/**
  53042. + * This function calculates traverses all the CFI DMA descriptors and
  53043. + * and accumulates the bytes that are left to be transfered.
  53044. + *
  53045. + * @return The total bytes left to transfered, or a negative value as failure
  53046. + */
  53047. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  53048. +{
  53049. + int32_t ret = 0;
  53050. + int i;
  53051. + struct dwc_otg_dma_desc *ddesc = NULL;
  53052. + struct cfi_ep *cfiep;
  53053. +
  53054. + /* See if the pcd_ep has its respective cfi_ep mapped */
  53055. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  53056. + if (!cfiep) {
  53057. + CFI_INFO("%s: Failed to find ep\n", __func__);
  53058. + return -1;
  53059. + }
  53060. +
  53061. + ddesc = ep->dwc_ep.descs;
  53062. +
  53063. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  53064. +
  53065. +#if defined(PRINT_CFI_DMA_DESCS)
  53066. + print_desc(ddesc, ep->ep.name, i);
  53067. +#endif
  53068. + ret += ddesc->status.b.bytes;
  53069. + ddesc++;
  53070. + }
  53071. +
  53072. + if (ret)
  53073. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  53074. + ret);
  53075. +
  53076. + return ret;
  53077. +}
  53078. +#endif
  53079. +
  53080. +/**
  53081. + * This function completes the request for the EP. If there are
  53082. + * additional requests for the EP in the queue they will be started.
  53083. + */
  53084. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  53085. +{
  53086. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  53087. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  53088. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  53089. + dev_if->in_ep_regs[ep->dwc_ep.num];
  53090. + deptsiz_data_t deptsiz;
  53091. + dev_dma_desc_sts_t desc_sts;
  53092. + dwc_otg_pcd_request_t *req = 0;
  53093. + dwc_otg_dev_dma_desc_t *dma_desc;
  53094. + uint32_t byte_count = 0;
  53095. + int is_last = 0;
  53096. + int i;
  53097. +
  53098. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  53099. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  53100. +
  53101. + /* Get any pending requests */
  53102. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  53103. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  53104. + if (!req) {
  53105. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  53106. + return;
  53107. + }
  53108. + } else {
  53109. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  53110. + return;
  53111. + }
  53112. +
  53113. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  53114. +
  53115. + if (ep->dwc_ep.is_in) {
  53116. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  53117. +
  53118. + if (core_if->dma_enable) {
  53119. + if (core_if->dma_desc_enable == 0) {
  53120. + if (deptsiz.b.xfersize == 0
  53121. + && deptsiz.b.pktcnt == 0) {
  53122. + byte_count =
  53123. + ep->dwc_ep.xfer_len -
  53124. + ep->dwc_ep.xfer_count;
  53125. +
  53126. + ep->dwc_ep.xfer_buff += byte_count;
  53127. + ep->dwc_ep.dma_addr += byte_count;
  53128. + ep->dwc_ep.xfer_count += byte_count;
  53129. +
  53130. + DWC_DEBUGPL(DBG_PCDV,
  53131. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  53132. + ep->dwc_ep.num,
  53133. + (ep->dwc_ep.
  53134. + is_in ? "IN" : "OUT"),
  53135. + ep->dwc_ep.xfer_len,
  53136. + deptsiz.b.xfersize,
  53137. + deptsiz.b.pktcnt);
  53138. +
  53139. + if (ep->dwc_ep.xfer_len <
  53140. + ep->dwc_ep.total_len) {
  53141. + dwc_otg_ep_start_transfer
  53142. + (core_if, &ep->dwc_ep);
  53143. + } else if (ep->dwc_ep.sent_zlp) {
  53144. + /*
  53145. + * This fragment of code should initiate 0
  53146. + * length transfer in case if it is queued
  53147. + * a transfer with size divisible to EPs max
  53148. + * packet size and with usb_request zero field
  53149. + * is set, which means that after data is transfered,
  53150. + * it is also should be transfered
  53151. + * a 0 length packet at the end. For Slave and
  53152. + * Buffer DMA modes in this case SW has
  53153. + * to initiate 2 transfers one with transfer size,
  53154. + * and the second with 0 size. For Descriptor
  53155. + * DMA mode SW is able to initiate a transfer,
  53156. + * which will handle all the packets including
  53157. + * the last 0 length.
  53158. + */
  53159. + ep->dwc_ep.sent_zlp = 0;
  53160. + dwc_otg_ep_start_zl_transfer
  53161. + (core_if, &ep->dwc_ep);
  53162. + } else {
  53163. + is_last = 1;
  53164. + }
  53165. + } else {
  53166. + if (ep->dwc_ep.type ==
  53167. + DWC_OTG_EP_TYPE_ISOC) {
  53168. + req->actual = 0;
  53169. + dwc_otg_request_done(ep, req, 0);
  53170. +
  53171. + ep->dwc_ep.start_xfer_buff = 0;
  53172. + ep->dwc_ep.xfer_buff = 0;
  53173. + ep->dwc_ep.xfer_len = 0;
  53174. +
  53175. + /* If there is a request in the queue start it. */
  53176. + start_next_request(ep);
  53177. + } else
  53178. + DWC_WARN
  53179. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  53180. + ep->dwc_ep.num,
  53181. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  53182. + deptsiz.b.xfersize,
  53183. + deptsiz.b.pktcnt);
  53184. + }
  53185. + } else {
  53186. + dma_desc = ep->dwc_ep.desc_addr;
  53187. + byte_count = 0;
  53188. + ep->dwc_ep.sent_zlp = 0;
  53189. +
  53190. +#ifdef DWC_UTE_CFI
  53191. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  53192. + ep->dwc_ep.buff_mode);
  53193. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53194. + int residue;
  53195. +
  53196. + residue = cfi_calc_desc_residue(ep);
  53197. + if (residue < 0)
  53198. + return;
  53199. +
  53200. + byte_count = residue;
  53201. + } else {
  53202. +#endif
  53203. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  53204. + ++i) {
  53205. + desc_sts = dma_desc->status;
  53206. + byte_count += desc_sts.b.bytes;
  53207. + dma_desc++;
  53208. + }
  53209. +#ifdef DWC_UTE_CFI
  53210. + }
  53211. +#endif
  53212. + if (byte_count == 0) {
  53213. + ep->dwc_ep.xfer_count =
  53214. + ep->dwc_ep.total_len;
  53215. + is_last = 1;
  53216. + } else {
  53217. + DWC_WARN("Incomplete transfer\n");
  53218. + }
  53219. + }
  53220. + } else {
  53221. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  53222. + DWC_DEBUGPL(DBG_PCDV,
  53223. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  53224. + ep->dwc_ep.num,
  53225. + ep->dwc_ep.is_in ? "IN" : "OUT",
  53226. + ep->dwc_ep.xfer_len,
  53227. + deptsiz.b.xfersize,
  53228. + deptsiz.b.pktcnt);
  53229. +
  53230. + /* Check if the whole transfer was completed,
  53231. + * if no, setup transfer for next portion of data
  53232. + */
  53233. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53234. + dwc_otg_ep_start_transfer(core_if,
  53235. + &ep->dwc_ep);
  53236. + } else if (ep->dwc_ep.sent_zlp) {
  53237. + /*
  53238. + * This fragment of code should initiate 0
  53239. + * length trasfer in case if it is queued
  53240. + * a trasfer with size divisible to EPs max
  53241. + * packet size and with usb_request zero field
  53242. + * is set, which means that after data is transfered,
  53243. + * it is also should be transfered
  53244. + * a 0 length packet at the end. For Slave and
  53245. + * Buffer DMA modes in this case SW has
  53246. + * to initiate 2 transfers one with transfer size,
  53247. + * and the second with 0 size. For Desriptor
  53248. + * DMA mode SW is able to initiate a transfer,
  53249. + * which will handle all the packets including
  53250. + * the last 0 legth.
  53251. + */
  53252. + ep->dwc_ep.sent_zlp = 0;
  53253. + dwc_otg_ep_start_zl_transfer(core_if,
  53254. + &ep->dwc_ep);
  53255. + } else {
  53256. + is_last = 1;
  53257. + }
  53258. + } else {
  53259. + DWC_WARN
  53260. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  53261. + ep->dwc_ep.num,
  53262. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  53263. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53264. + }
  53265. + }
  53266. + } else {
  53267. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  53268. + dev_if->out_ep_regs[ep->dwc_ep.num];
  53269. + desc_sts.d32 = 0;
  53270. + if (core_if->dma_enable) {
  53271. + if (core_if->dma_desc_enable) {
  53272. + dma_desc = ep->dwc_ep.desc_addr;
  53273. + byte_count = 0;
  53274. + ep->dwc_ep.sent_zlp = 0;
  53275. +
  53276. +#ifdef DWC_UTE_CFI
  53277. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  53278. + ep->dwc_ep.buff_mode);
  53279. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53280. + int residue;
  53281. + residue = cfi_calc_desc_residue(ep);
  53282. + if (residue < 0)
  53283. + return;
  53284. + byte_count = residue;
  53285. + } else {
  53286. +#endif
  53287. +
  53288. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  53289. + ++i) {
  53290. + desc_sts = dma_desc->status;
  53291. + byte_count += desc_sts.b.bytes;
  53292. + dma_desc++;
  53293. + }
  53294. +
  53295. +#ifdef DWC_UTE_CFI
  53296. + }
  53297. +#endif
  53298. + /* Checking for interrupt Out transfers with not
  53299. + * dword aligned mps sizes
  53300. + */
  53301. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  53302. + (ep->dwc_ep.maxpacket%4)) {
  53303. + ep->dwc_ep.xfer_count =
  53304. + ep->dwc_ep.total_len - byte_count;
  53305. + if ((ep->dwc_ep.xfer_len %
  53306. + ep->dwc_ep.maxpacket)
  53307. + && (ep->dwc_ep.xfer_len /
  53308. + ep->dwc_ep.maxpacket <
  53309. + MAX_DMA_DESC_CNT))
  53310. + ep->dwc_ep.xfer_len -=
  53311. + (ep->dwc_ep.desc_cnt -
  53312. + 1) * ep->dwc_ep.maxpacket +
  53313. + ep->dwc_ep.xfer_len %
  53314. + ep->dwc_ep.maxpacket;
  53315. + else
  53316. + ep->dwc_ep.xfer_len -=
  53317. + ep->dwc_ep.desc_cnt *
  53318. + ep->dwc_ep.maxpacket;
  53319. + if (ep->dwc_ep.xfer_len > 0) {
  53320. + dwc_otg_ep_start_transfer
  53321. + (core_if, &ep->dwc_ep);
  53322. + } else {
  53323. + is_last = 1;
  53324. + }
  53325. + } else {
  53326. + ep->dwc_ep.xfer_count =
  53327. + ep->dwc_ep.total_len - byte_count +
  53328. + ((4 -
  53329. + (ep->dwc_ep.
  53330. + total_len & 0x3)) & 0x3);
  53331. + is_last = 1;
  53332. + }
  53333. + } else {
  53334. + deptsiz.d32 = 0;
  53335. + deptsiz.d32 =
  53336. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  53337. +
  53338. + byte_count = (ep->dwc_ep.xfer_len -
  53339. + ep->dwc_ep.xfer_count -
  53340. + deptsiz.b.xfersize);
  53341. + ep->dwc_ep.xfer_buff += byte_count;
  53342. + ep->dwc_ep.dma_addr += byte_count;
  53343. + ep->dwc_ep.xfer_count += byte_count;
  53344. +
  53345. + /* Check if the whole transfer was completed,
  53346. + * if no, setup transfer for next portion of data
  53347. + */
  53348. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53349. + dwc_otg_ep_start_transfer(core_if,
  53350. + &ep->dwc_ep);
  53351. + } else if (ep->dwc_ep.sent_zlp) {
  53352. + /*
  53353. + * This fragment of code should initiate 0
  53354. + * length trasfer in case if it is queued
  53355. + * a trasfer with size divisible to EPs max
  53356. + * packet size and with usb_request zero field
  53357. + * is set, which means that after data is transfered,
  53358. + * it is also should be transfered
  53359. + * a 0 length packet at the end. For Slave and
  53360. + * Buffer DMA modes in this case SW has
  53361. + * to initiate 2 transfers one with transfer size,
  53362. + * and the second with 0 size. For Desriptor
  53363. + * DMA mode SW is able to initiate a transfer,
  53364. + * which will handle all the packets including
  53365. + * the last 0 legth.
  53366. + */
  53367. + ep->dwc_ep.sent_zlp = 0;
  53368. + dwc_otg_ep_start_zl_transfer(core_if,
  53369. + &ep->dwc_ep);
  53370. + } else {
  53371. + is_last = 1;
  53372. + }
  53373. + }
  53374. + } else {
  53375. + /* Check if the whole transfer was completed,
  53376. + * if no, setup transfer for next portion of data
  53377. + */
  53378. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53379. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  53380. + } else if (ep->dwc_ep.sent_zlp) {
  53381. + /*
  53382. + * This fragment of code should initiate 0
  53383. + * length transfer in case if it is queued
  53384. + * a transfer with size divisible to EPs max
  53385. + * packet size and with usb_request zero field
  53386. + * is set, which means that after data is transfered,
  53387. + * it is also should be transfered
  53388. + * a 0 length packet at the end. For Slave and
  53389. + * Buffer DMA modes in this case SW has
  53390. + * to initiate 2 transfers one with transfer size,
  53391. + * and the second with 0 size. For Descriptor
  53392. + * DMA mode SW is able to initiate a transfer,
  53393. + * which will handle all the packets including
  53394. + * the last 0 length.
  53395. + */
  53396. + ep->dwc_ep.sent_zlp = 0;
  53397. + dwc_otg_ep_start_zl_transfer(core_if,
  53398. + &ep->dwc_ep);
  53399. + } else {
  53400. + is_last = 1;
  53401. + }
  53402. + }
  53403. +
  53404. + DWC_DEBUGPL(DBG_PCDV,
  53405. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  53406. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  53407. + ep->dwc_ep.is_in ? "IN" : "OUT",
  53408. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  53409. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53410. + }
  53411. +
  53412. + /* Complete the request */
  53413. + if (is_last) {
  53414. +#ifdef DWC_UTE_CFI
  53415. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53416. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  53417. + } else {
  53418. +#endif
  53419. + req->actual = ep->dwc_ep.xfer_count;
  53420. +#ifdef DWC_UTE_CFI
  53421. + }
  53422. +#endif
  53423. + if (req->dw_align_buf) {
  53424. + if (!ep->dwc_ep.is_in) {
  53425. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  53426. + }
  53427. + DWC_DMA_FREE(req->length, req->dw_align_buf,
  53428. + req->dw_align_buf_dma);
  53429. + }
  53430. +
  53431. + dwc_otg_request_done(ep, req, 0);
  53432. +
  53433. + ep->dwc_ep.start_xfer_buff = 0;
  53434. + ep->dwc_ep.xfer_buff = 0;
  53435. + ep->dwc_ep.xfer_len = 0;
  53436. +
  53437. + /* If there is a request in the queue start it. */
  53438. + start_next_request(ep);
  53439. + }
  53440. +}
  53441. +
  53442. +#ifdef DWC_EN_ISOC
  53443. +
  53444. +/**
  53445. + * This function BNA interrupt for Isochronous EPs
  53446. + *
  53447. + */
  53448. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  53449. +{
  53450. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  53451. + volatile uint32_t *addr;
  53452. + depctl_data_t depctl = {.d32 = 0 };
  53453. + dwc_otg_pcd_t *pcd = ep->pcd;
  53454. + dwc_otg_dev_dma_desc_t *dma_desc;
  53455. + int i;
  53456. +
  53457. + dma_desc =
  53458. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  53459. +
  53460. + if (dwc_ep->is_in) {
  53461. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53462. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  53463. + sts.d32 = dma_desc->status.d32;
  53464. + sts.b_iso_in.bs = BS_HOST_READY;
  53465. + dma_desc->status.d32 = sts.d32;
  53466. + }
  53467. + } else {
  53468. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53469. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  53470. + sts.d32 = dma_desc->status.d32;
  53471. + sts.b_iso_out.bs = BS_HOST_READY;
  53472. + dma_desc->status.d32 = sts.d32;
  53473. + }
  53474. + }
  53475. +
  53476. + if (dwc_ep->is_in == 0) {
  53477. + addr =
  53478. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  53479. + num]->doepctl;
  53480. + } else {
  53481. + addr =
  53482. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  53483. + }
  53484. + depctl.b.epena = 1;
  53485. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  53486. +}
  53487. +
  53488. +/**
  53489. + * This function sets latest iso packet information(non-PTI mode)
  53490. + *
  53491. + * @param core_if Programming view of DWC_otg controller.
  53492. + * @param ep The EP to start the transfer on.
  53493. + *
  53494. + */
  53495. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  53496. +{
  53497. + deptsiz_data_t deptsiz = {.d32 = 0 };
  53498. + dma_addr_t dma_addr;
  53499. + uint32_t offset;
  53500. +
  53501. + if (ep->proc_buf_num)
  53502. + dma_addr = ep->dma_addr1;
  53503. + else
  53504. + dma_addr = ep->dma_addr0;
  53505. +
  53506. + if (ep->is_in) {
  53507. + deptsiz.d32 =
  53508. + DWC_READ_REG32(&core_if->dev_if->
  53509. + in_ep_regs[ep->num]->dieptsiz);
  53510. + offset = ep->data_per_frame;
  53511. + } else {
  53512. + deptsiz.d32 =
  53513. + DWC_READ_REG32(&core_if->dev_if->
  53514. + out_ep_regs[ep->num]->doeptsiz);
  53515. + offset =
  53516. + ep->data_per_frame +
  53517. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  53518. + }
  53519. +
  53520. + if (!deptsiz.b.xfersize) {
  53521. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  53522. + ep->pkt_info[ep->cur_pkt].offset =
  53523. + ep->cur_pkt_dma_addr - dma_addr;
  53524. + ep->pkt_info[ep->cur_pkt].status = 0;
  53525. + } else {
  53526. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  53527. + ep->pkt_info[ep->cur_pkt].offset =
  53528. + ep->cur_pkt_dma_addr - dma_addr;
  53529. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  53530. + }
  53531. + ep->cur_pkt_addr += offset;
  53532. + ep->cur_pkt_dma_addr += offset;
  53533. + ep->cur_pkt++;
  53534. +}
  53535. +
  53536. +/**
  53537. + * This function sets latest iso packet information(DDMA mode)
  53538. + *
  53539. + * @param core_if Programming view of DWC_otg controller.
  53540. + * @param dwc_ep The EP to start the transfer on.
  53541. + *
  53542. + */
  53543. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  53544. + dwc_ep_t * dwc_ep)
  53545. +{
  53546. + dwc_otg_dev_dma_desc_t *dma_desc;
  53547. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53548. + iso_pkt_info_t *iso_packet;
  53549. + uint32_t data_per_desc;
  53550. + uint32_t offset;
  53551. + int i, j;
  53552. +
  53553. + iso_packet = dwc_ep->pkt_info;
  53554. +
  53555. + /** Reinit closed DMA Descriptors*/
  53556. + /** ISO OUT EP */
  53557. + if (dwc_ep->is_in == 0) {
  53558. + dma_desc =
  53559. + dwc_ep->iso_desc_addr +
  53560. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53561. + offset = 0;
  53562. +
  53563. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  53564. + i += dwc_ep->pkt_per_frm) {
  53565. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  53566. + data_per_desc =
  53567. + ((j + 1) * dwc_ep->maxpacket >
  53568. + dwc_ep->
  53569. + data_per_frame) ? dwc_ep->data_per_frame -
  53570. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53571. + data_per_desc +=
  53572. + (data_per_desc % 4) ? (4 -
  53573. + data_per_desc %
  53574. + 4) : 0;
  53575. +
  53576. + sts.d32 = dma_desc->status.d32;
  53577. +
  53578. + /* Write status in iso_packet_decsriptor */
  53579. + iso_packet->status =
  53580. + sts.b_iso_out.rxsts +
  53581. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53582. + if (iso_packet->status) {
  53583. + iso_packet->status = -DWC_E_NO_DATA;
  53584. + }
  53585. +
  53586. + /* Received data length */
  53587. + if (!sts.b_iso_out.rxbytes) {
  53588. + iso_packet->length =
  53589. + data_per_desc -
  53590. + sts.b_iso_out.rxbytes;
  53591. + } else {
  53592. + iso_packet->length =
  53593. + data_per_desc -
  53594. + sts.b_iso_out.rxbytes + (4 -
  53595. + dwc_ep->data_per_frame
  53596. + % 4);
  53597. + }
  53598. +
  53599. + iso_packet->offset = offset;
  53600. +
  53601. + offset += data_per_desc;
  53602. + dma_desc++;
  53603. + iso_packet++;
  53604. + }
  53605. + }
  53606. +
  53607. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  53608. + data_per_desc =
  53609. + ((j + 1) * dwc_ep->maxpacket >
  53610. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53611. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53612. + data_per_desc +=
  53613. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53614. +
  53615. + sts.d32 = dma_desc->status.d32;
  53616. +
  53617. + /* Write status in iso_packet_decsriptor */
  53618. + iso_packet->status =
  53619. + sts.b_iso_out.rxsts +
  53620. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53621. + if (iso_packet->status) {
  53622. + iso_packet->status = -DWC_E_NO_DATA;
  53623. + }
  53624. +
  53625. + /* Received data length */
  53626. + iso_packet->length =
  53627. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  53628. +
  53629. + iso_packet->offset = offset;
  53630. +
  53631. + offset += data_per_desc;
  53632. + iso_packet++;
  53633. + dma_desc++;
  53634. + }
  53635. +
  53636. + sts.d32 = dma_desc->status.d32;
  53637. +
  53638. + /* Write status in iso_packet_decsriptor */
  53639. + iso_packet->status =
  53640. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53641. + if (iso_packet->status) {
  53642. + iso_packet->status = -DWC_E_NO_DATA;
  53643. + }
  53644. + /* Received data length */
  53645. + if (!sts.b_iso_out.rxbytes) {
  53646. + iso_packet->length =
  53647. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  53648. + } else {
  53649. + iso_packet->length =
  53650. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  53651. + (4 - dwc_ep->data_per_frame % 4);
  53652. + }
  53653. +
  53654. + iso_packet->offset = offset;
  53655. + } else {
  53656. +/** ISO IN EP */
  53657. +
  53658. + dma_desc =
  53659. + dwc_ep->iso_desc_addr +
  53660. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53661. +
  53662. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  53663. + sts.d32 = dma_desc->status.d32;
  53664. +
  53665. + /* Write status in iso packet descriptor */
  53666. + iso_packet->status =
  53667. + sts.b_iso_in.txsts +
  53668. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  53669. + if (iso_packet->status != 0) {
  53670. + iso_packet->status = -DWC_E_NO_DATA;
  53671. +
  53672. + }
  53673. + /* Bytes has been transfered */
  53674. + iso_packet->length =
  53675. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  53676. +
  53677. + dma_desc++;
  53678. + iso_packet++;
  53679. + }
  53680. +
  53681. + sts.d32 = dma_desc->status.d32;
  53682. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  53683. + sts.d32 = dma_desc->status.d32;
  53684. + }
  53685. +
  53686. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  53687. + iso_packet->status =
  53688. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  53689. + if (iso_packet->status != 0) {
  53690. + iso_packet->status = -DWC_E_NO_DATA;
  53691. + }
  53692. +
  53693. + /* Bytes has been transfered */
  53694. + iso_packet->length =
  53695. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  53696. + }
  53697. +}
  53698. +
  53699. +/**
  53700. + * This function reinitialize DMA Descriptors for Isochronous transfer
  53701. + *
  53702. + * @param core_if Programming view of DWC_otg controller.
  53703. + * @param dwc_ep The EP to start the transfer on.
  53704. + *
  53705. + */
  53706. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  53707. +{
  53708. + int i, j;
  53709. + dwc_otg_dev_dma_desc_t *dma_desc;
  53710. + dma_addr_t dma_ad;
  53711. + volatile uint32_t *addr;
  53712. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53713. + uint32_t data_per_desc;
  53714. +
  53715. + if (dwc_ep->is_in == 0) {
  53716. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  53717. + } else {
  53718. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  53719. + }
  53720. +
  53721. + if (dwc_ep->proc_buf_num == 0) {
  53722. + /** Buffer 0 descriptors setup */
  53723. + dma_ad = dwc_ep->dma_addr0;
  53724. + } else {
  53725. + /** Buffer 1 descriptors setup */
  53726. + dma_ad = dwc_ep->dma_addr1;
  53727. + }
  53728. +
  53729. + /** Reinit closed DMA Descriptors*/
  53730. + /** ISO OUT EP */
  53731. + if (dwc_ep->is_in == 0) {
  53732. + dma_desc =
  53733. + dwc_ep->iso_desc_addr +
  53734. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53735. +
  53736. + sts.b_iso_out.bs = BS_HOST_READY;
  53737. + sts.b_iso_out.rxsts = 0;
  53738. + sts.b_iso_out.l = 0;
  53739. + sts.b_iso_out.sp = 0;
  53740. + sts.b_iso_out.ioc = 0;
  53741. + sts.b_iso_out.pid = 0;
  53742. + sts.b_iso_out.framenum = 0;
  53743. +
  53744. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  53745. + i += dwc_ep->pkt_per_frm) {
  53746. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  53747. + data_per_desc =
  53748. + ((j + 1) * dwc_ep->maxpacket >
  53749. + dwc_ep->
  53750. + data_per_frame) ? dwc_ep->data_per_frame -
  53751. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53752. + data_per_desc +=
  53753. + (data_per_desc % 4) ? (4 -
  53754. + data_per_desc %
  53755. + 4) : 0;
  53756. + sts.b_iso_out.rxbytes = data_per_desc;
  53757. + dma_desc->buf = dma_ad;
  53758. + dma_desc->status.d32 = sts.d32;
  53759. +
  53760. + dma_ad += data_per_desc;
  53761. + dma_desc++;
  53762. + }
  53763. + }
  53764. +
  53765. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  53766. +
  53767. + data_per_desc =
  53768. + ((j + 1) * dwc_ep->maxpacket >
  53769. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53770. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53771. + data_per_desc +=
  53772. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53773. + sts.b_iso_out.rxbytes = data_per_desc;
  53774. +
  53775. + dma_desc->buf = dma_ad;
  53776. + dma_desc->status.d32 = sts.d32;
  53777. +
  53778. + dma_desc++;
  53779. + dma_ad += data_per_desc;
  53780. + }
  53781. +
  53782. + sts.b_iso_out.ioc = 1;
  53783. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  53784. +
  53785. + data_per_desc =
  53786. + ((j + 1) * dwc_ep->maxpacket >
  53787. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53788. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53789. + data_per_desc +=
  53790. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53791. + sts.b_iso_out.rxbytes = data_per_desc;
  53792. +
  53793. + dma_desc->buf = dma_ad;
  53794. + dma_desc->status.d32 = sts.d32;
  53795. + } else {
  53796. +/** ISO IN EP */
  53797. +
  53798. + dma_desc =
  53799. + dwc_ep->iso_desc_addr +
  53800. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53801. +
  53802. + sts.b_iso_in.bs = BS_HOST_READY;
  53803. + sts.b_iso_in.txsts = 0;
  53804. + sts.b_iso_in.sp = 0;
  53805. + sts.b_iso_in.ioc = 0;
  53806. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  53807. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  53808. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  53809. + sts.b_iso_in.l = 0;
  53810. +
  53811. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  53812. + dma_desc->buf = dma_ad;
  53813. + dma_desc->status.d32 = sts.d32;
  53814. +
  53815. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  53816. + dma_ad += dwc_ep->data_per_frame;
  53817. + dma_desc++;
  53818. + }
  53819. +
  53820. + sts.b_iso_in.ioc = 1;
  53821. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  53822. +
  53823. + dma_desc->buf = dma_ad;
  53824. + dma_desc->status.d32 = sts.d32;
  53825. +
  53826. + dwc_ep->next_frame =
  53827. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  53828. + }
  53829. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  53830. +}
  53831. +
  53832. +/**
  53833. + * This function is to handle Iso EP transfer complete interrupt
  53834. + * in case Iso out packet was dropped
  53835. + *
  53836. + * @param core_if Programming view of DWC_otg controller.
  53837. + * @param dwc_ep The EP for wihich transfer complete was asserted
  53838. + *
  53839. + */
  53840. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  53841. + dwc_ep_t * dwc_ep)
  53842. +{
  53843. + uint32_t dma_addr;
  53844. + uint32_t drp_pkt;
  53845. + uint32_t drp_pkt_cnt;
  53846. + deptsiz_data_t deptsiz = {.d32 = 0 };
  53847. + depctl_data_t depctl = {.d32 = 0 };
  53848. + int i;
  53849. +
  53850. + deptsiz.d32 =
  53851. + DWC_READ_REG32(&core_if->dev_if->
  53852. + out_ep_regs[dwc_ep->num]->doeptsiz);
  53853. +
  53854. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  53855. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  53856. +
  53857. + /* Setting dropped packets status */
  53858. + for (i = 0; i < drp_pkt_cnt; ++i) {
  53859. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  53860. + drp_pkt++;
  53861. + deptsiz.b.pktcnt--;
  53862. + }
  53863. +
  53864. + if (deptsiz.b.pktcnt > 0) {
  53865. + deptsiz.b.xfersize =
  53866. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  53867. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  53868. + } else {
  53869. + deptsiz.b.xfersize = 0;
  53870. + deptsiz.b.pktcnt = 0;
  53871. + }
  53872. +
  53873. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  53874. + deptsiz.d32);
  53875. +
  53876. + if (deptsiz.b.pktcnt > 0) {
  53877. + if (dwc_ep->proc_buf_num) {
  53878. + dma_addr =
  53879. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  53880. + deptsiz.b.xfersize;
  53881. + } else {
  53882. + dma_addr =
  53883. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  53884. + deptsiz.b.xfersize;;
  53885. + }
  53886. +
  53887. + DWC_WRITE_REG32(&core_if->dev_if->
  53888. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  53889. +
  53890. + /** Re-enable endpoint, clear nak */
  53891. + depctl.d32 = 0;
  53892. + depctl.b.epena = 1;
  53893. + depctl.b.cnak = 1;
  53894. +
  53895. + DWC_MODIFY_REG32(&core_if->dev_if->
  53896. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  53897. + depctl.d32);
  53898. + return 0;
  53899. + } else {
  53900. + return 1;
  53901. + }
  53902. +}
  53903. +
  53904. +/**
  53905. + * This function sets iso packets information(PTI mode)
  53906. + *
  53907. + * @param core_if Programming view of DWC_otg controller.
  53908. + * @param ep The EP to start the transfer on.
  53909. + *
  53910. + */
  53911. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  53912. +{
  53913. + int i, j;
  53914. + dma_addr_t dma_ad;
  53915. + iso_pkt_info_t *packet_info = ep->pkt_info;
  53916. + uint32_t offset;
  53917. + uint32_t frame_data;
  53918. + deptsiz_data_t deptsiz;
  53919. +
  53920. + if (ep->proc_buf_num == 0) {
  53921. + /** Buffer 0 descriptors setup */
  53922. + dma_ad = ep->dma_addr0;
  53923. + } else {
  53924. + /** Buffer 1 descriptors setup */
  53925. + dma_ad = ep->dma_addr1;
  53926. + }
  53927. +
  53928. + if (ep->is_in) {
  53929. + deptsiz.d32 =
  53930. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  53931. + dieptsiz);
  53932. + } else {
  53933. + deptsiz.d32 =
  53934. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  53935. + doeptsiz);
  53936. + }
  53937. +
  53938. + if (!deptsiz.b.xfersize) {
  53939. + offset = 0;
  53940. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  53941. + frame_data = ep->data_per_frame;
  53942. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  53943. +
  53944. + /* Packet status - is not set as initially
  53945. + * it is set to 0 and if packet was sent
  53946. + successfully, status field will remain 0*/
  53947. +
  53948. + /* Bytes has been transfered */
  53949. + packet_info->length =
  53950. + (ep->maxpacket <
  53951. + frame_data) ? ep->maxpacket : frame_data;
  53952. +
  53953. + /* Received packet offset */
  53954. + packet_info->offset = offset;
  53955. + offset += packet_info->length;
  53956. + frame_data -= packet_info->length;
  53957. +
  53958. + packet_info++;
  53959. + }
  53960. + }
  53961. + return 1;
  53962. + } else {
  53963. + /* This is a workaround for in case of Transfer Complete with
  53964. + * PktDrpSts interrupts merging - in this case Transfer complete
  53965. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  53966. + * set and with DOEPTSIZ register non zero. Investigations showed,
  53967. + * that this happens when Out packet is dropped, but because of
  53968. + * interrupts merging during first interrupt handling PktDrpSts
  53969. + * bit is cleared and for next merged interrupts it is not reset.
  53970. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  53971. + */
  53972. + if (ep->is_in) {
  53973. + return 1;
  53974. + } else {
  53975. + return handle_iso_out_pkt_dropped(core_if, ep);
  53976. + }
  53977. + }
  53978. +}
  53979. +
  53980. +/**
  53981. + * This function is to handle Iso EP transfer complete interrupt
  53982. + *
  53983. + * @param pcd The PCD
  53984. + * @param ep The EP for which transfer complete was asserted
  53985. + *
  53986. + */
  53987. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  53988. +{
  53989. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  53990. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  53991. + uint8_t is_last = 0;
  53992. +
  53993. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  53994. + DWC_WARN("Next frame is not set!\n");
  53995. + return;
  53996. + }
  53997. +
  53998. + if (core_if->dma_enable) {
  53999. + if (core_if->dma_desc_enable) {
  54000. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  54001. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  54002. + is_last = 1;
  54003. + } else {
  54004. + if (core_if->pti_enh_enable) {
  54005. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  54006. + dwc_ep->proc_buf_num =
  54007. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54008. + dwc_otg_iso_ep_start_buf_transfer
  54009. + (core_if, dwc_ep);
  54010. + is_last = 1;
  54011. + }
  54012. + } else {
  54013. + set_current_pkt_info(core_if, dwc_ep);
  54014. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  54015. + is_last = 1;
  54016. + dwc_ep->cur_pkt = 0;
  54017. + dwc_ep->proc_buf_num =
  54018. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54019. + if (dwc_ep->proc_buf_num) {
  54020. + dwc_ep->cur_pkt_addr =
  54021. + dwc_ep->xfer_buff1;
  54022. + dwc_ep->cur_pkt_dma_addr =
  54023. + dwc_ep->dma_addr1;
  54024. + } else {
  54025. + dwc_ep->cur_pkt_addr =
  54026. + dwc_ep->xfer_buff0;
  54027. + dwc_ep->cur_pkt_dma_addr =
  54028. + dwc_ep->dma_addr0;
  54029. + }
  54030. +
  54031. + }
  54032. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  54033. + dwc_ep);
  54034. + }
  54035. + }
  54036. + } else {
  54037. + set_current_pkt_info(core_if, dwc_ep);
  54038. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  54039. + is_last = 1;
  54040. + dwc_ep->cur_pkt = 0;
  54041. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54042. + if (dwc_ep->proc_buf_num) {
  54043. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  54044. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  54045. + } else {
  54046. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  54047. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  54048. + }
  54049. +
  54050. + }
  54051. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  54052. + }
  54053. + if (is_last)
  54054. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  54055. +}
  54056. +#endif /* DWC_EN_ISOC */
  54057. +
  54058. +/**
  54059. + * This function handle BNA interrupt for Non Isochronous EPs
  54060. + *
  54061. + */
  54062. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  54063. +{
  54064. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  54065. + volatile uint32_t *addr;
  54066. + depctl_data_t depctl = {.d32 = 0 };
  54067. + dwc_otg_pcd_t *pcd = ep->pcd;
  54068. + dwc_otg_dev_dma_desc_t *dma_desc;
  54069. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  54070. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  54071. + int i, start;
  54072. +
  54073. + if (!dwc_ep->desc_cnt)
  54074. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  54075. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  54076. +
  54077. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  54078. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  54079. + uint32_t doepdma;
  54080. + dwc_otg_dev_out_ep_regs_t *out_regs =
  54081. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  54082. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  54083. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  54084. + dma_desc = &(dwc_ep->desc_addr[start]);
  54085. + } else {
  54086. + start = 0;
  54087. + dma_desc = dwc_ep->desc_addr;
  54088. + }
  54089. +
  54090. +
  54091. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  54092. + sts.d32 = dma_desc->status.d32;
  54093. + sts.b.bs = BS_HOST_READY;
  54094. + dma_desc->status.d32 = sts.d32;
  54095. + }
  54096. +
  54097. + if (dwc_ep->is_in == 0) {
  54098. + addr =
  54099. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  54100. + doepctl;
  54101. + } else {
  54102. + addr =
  54103. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  54104. + }
  54105. + depctl.b.epena = 1;
  54106. + depctl.b.cnak = 1;
  54107. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  54108. +}
  54109. +
  54110. +/**
  54111. + * This function handles EP0 Control transfers.
  54112. + *
  54113. + * The state of the control transfers are tracked in
  54114. + * <code>ep0state</code>.
  54115. + */
  54116. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  54117. +{
  54118. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54119. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  54120. + dev_dma_desc_sts_t desc_sts;
  54121. + deptsiz0_data_t deptsiz;
  54122. + uint32_t byte_count;
  54123. +
  54124. +#ifdef DEBUG_EP0
  54125. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  54126. + print_ep0_state(pcd);
  54127. +#endif
  54128. +
  54129. +// DWC_PRINTF("HANDLE EP0\n");
  54130. +
  54131. + switch (pcd->ep0state) {
  54132. + case EP0_DISCONNECT:
  54133. + break;
  54134. +
  54135. + case EP0_IDLE:
  54136. + pcd->request_config = 0;
  54137. +
  54138. + pcd_setup(pcd);
  54139. + break;
  54140. +
  54141. + case EP0_IN_DATA_PHASE:
  54142. +#ifdef DEBUG_EP0
  54143. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  54144. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  54145. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  54146. +#endif
  54147. +
  54148. + if (core_if->dma_enable != 0) {
  54149. + /*
  54150. + * For EP0 we can only program 1 packet at a time so we
  54151. + * need to do the make calculations after each complete.
  54152. + * Call write_packet to make the calculations, as in
  54153. + * slave mode, and use those values to determine if we
  54154. + * can complete.
  54155. + */
  54156. + if (core_if->dma_desc_enable == 0) {
  54157. + deptsiz.d32 =
  54158. + DWC_READ_REG32(&core_if->
  54159. + dev_if->in_ep_regs[0]->
  54160. + dieptsiz);
  54161. + byte_count =
  54162. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  54163. + } else {
  54164. + desc_sts =
  54165. + core_if->dev_if->in_desc_addr->status;
  54166. + byte_count =
  54167. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  54168. + }
  54169. + ep0->dwc_ep.xfer_count += byte_count;
  54170. + ep0->dwc_ep.xfer_buff += byte_count;
  54171. + ep0->dwc_ep.dma_addr += byte_count;
  54172. + }
  54173. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  54174. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54175. + &ep0->dwc_ep);
  54176. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  54177. + } else if (ep0->dwc_ep.sent_zlp) {
  54178. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54179. + &ep0->dwc_ep);
  54180. + ep0->dwc_ep.sent_zlp = 0;
  54181. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  54182. + } else {
  54183. + ep0_complete_request(ep0);
  54184. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  54185. + }
  54186. + break;
  54187. + case EP0_OUT_DATA_PHASE:
  54188. +#ifdef DEBUG_EP0
  54189. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  54190. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  54191. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  54192. +#endif
  54193. + if (core_if->dma_enable != 0) {
  54194. + if (core_if->dma_desc_enable == 0) {
  54195. + deptsiz.d32 =
  54196. + DWC_READ_REG32(&core_if->
  54197. + dev_if->out_ep_regs[0]->
  54198. + doeptsiz);
  54199. + byte_count =
  54200. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  54201. + } else {
  54202. + desc_sts =
  54203. + core_if->dev_if->out_desc_addr->status;
  54204. + byte_count =
  54205. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  54206. + }
  54207. + ep0->dwc_ep.xfer_count += byte_count;
  54208. + ep0->dwc_ep.xfer_buff += byte_count;
  54209. + ep0->dwc_ep.dma_addr += byte_count;
  54210. + }
  54211. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  54212. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54213. + &ep0->dwc_ep);
  54214. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  54215. + } else if (ep0->dwc_ep.sent_zlp) {
  54216. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54217. + &ep0->dwc_ep);
  54218. + ep0->dwc_ep.sent_zlp = 0;
  54219. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  54220. + } else {
  54221. + ep0_complete_request(ep0);
  54222. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  54223. + }
  54224. + break;
  54225. +
  54226. + case EP0_IN_STATUS_PHASE:
  54227. + case EP0_OUT_STATUS_PHASE:
  54228. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  54229. + ep0_complete_request(ep0);
  54230. + pcd->ep0state = EP0_IDLE;
  54231. + ep0->stopped = 1;
  54232. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  54233. +
  54234. + /* Prepare for more SETUP Packets */
  54235. + if (core_if->dma_enable) {
  54236. + ep0_out_start(core_if, pcd);
  54237. + }
  54238. + break;
  54239. +
  54240. + case EP0_STALL:
  54241. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  54242. + break;
  54243. + }
  54244. +#ifdef DEBUG_EP0
  54245. + print_ep0_state(pcd);
  54246. +#endif
  54247. +}
  54248. +
  54249. +/**
  54250. + * Restart transfer
  54251. + */
  54252. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  54253. +{
  54254. + dwc_otg_core_if_t *core_if;
  54255. + dwc_otg_dev_if_t *dev_if;
  54256. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54257. + dwc_otg_pcd_ep_t *ep;
  54258. +
  54259. + ep = get_in_ep(pcd, epnum);
  54260. +
  54261. +#ifdef DWC_EN_ISOC
  54262. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  54263. + return;
  54264. + }
  54265. +#endif /* DWC_EN_ISOC */
  54266. +
  54267. + core_if = GET_CORE_IF(pcd);
  54268. + dev_if = core_if->dev_if;
  54269. +
  54270. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  54271. +
  54272. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  54273. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  54274. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  54275. + /*
  54276. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  54277. + */
  54278. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  54279. + ep->dwc_ep.start_xfer_buff != 0) {
  54280. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  54281. + ep->dwc_ep.xfer_count = 0;
  54282. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  54283. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  54284. + } else {
  54285. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  54286. + /* convert packet size to dwords. */
  54287. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  54288. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  54289. + }
  54290. + ep->stopped = 0;
  54291. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  54292. + "xfer_len=%0x stopped=%d\n",
  54293. + ep->dwc_ep.xfer_buff,
  54294. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  54295. + ep->stopped);
  54296. + if (epnum == 0) {
  54297. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  54298. + } else {
  54299. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  54300. + }
  54301. + }
  54302. +}
  54303. +
  54304. +/*
  54305. + * This function create new nextep sequnce based on Learn Queue.
  54306. + *
  54307. + * @param core_if Programming view of DWC_otg controller
  54308. + */
  54309. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  54310. +{
  54311. + dwc_otg_device_global_regs_t *dev_global_regs =
  54312. + core_if->dev_if->dev_global_regs;
  54313. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  54314. + /* Number of Token Queue Registers */
  54315. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  54316. + dtknq1_data_t dtknqr1;
  54317. + uint32_t in_tkn_epnums[4];
  54318. + uint8_t seqnum[MAX_EPS_CHANNELS];
  54319. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  54320. + grstctl_t resetctl = {.d32 = 0 };
  54321. + uint8_t temp;
  54322. + int ndx = 0;
  54323. + int start = 0;
  54324. + int end = 0;
  54325. + int sort_done = 0;
  54326. + int i = 0;
  54327. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  54328. +
  54329. +
  54330. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  54331. +
  54332. + /* Read the DTKNQ Registers */
  54333. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  54334. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  54335. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  54336. + in_tkn_epnums[i]);
  54337. + if (addr == &dev_global_regs->dvbusdis) {
  54338. + addr = &dev_global_regs->dtknqr3_dthrctl;
  54339. + } else {
  54340. + ++addr;
  54341. + }
  54342. +
  54343. + }
  54344. +
  54345. + /* Copy the DTKNQR1 data to the bit field. */
  54346. + dtknqr1.d32 = in_tkn_epnums[0];
  54347. + if (dtknqr1.b.wrap_bit) {
  54348. + ndx = dtknqr1.b.intknwptr;
  54349. + end = ndx -1;
  54350. + if (end < 0)
  54351. + end = TOKEN_Q_DEPTH -1;
  54352. + } else {
  54353. + ndx = 0;
  54354. + end = dtknqr1.b.intknwptr -1;
  54355. + if (end < 0)
  54356. + end = 0;
  54357. + }
  54358. + start = ndx;
  54359. +
  54360. + /* Fill seqnum[] by initial values: EP number + 31 */
  54361. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  54362. + seqnum[i] = i +31;
  54363. + }
  54364. +
  54365. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  54366. + for (i=0; i < 6; i++)
  54367. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  54368. +
  54369. + if (TOKEN_Q_DEPTH > 6) {
  54370. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54371. + for (i=6; i < 14; i++)
  54372. + intkn_seq[i] =
  54373. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  54374. + }
  54375. +
  54376. + if (TOKEN_Q_DEPTH > 14) {
  54377. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54378. + for (i=14; i < 22; i++)
  54379. + intkn_seq[i] =
  54380. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  54381. + }
  54382. +
  54383. + if (TOKEN_Q_DEPTH > 22) {
  54384. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54385. + for (i=22; i < 30; i++)
  54386. + intkn_seq[i] =
  54387. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  54388. + }
  54389. +
  54390. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  54391. + start, end);
  54392. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  54393. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  54394. +
  54395. + /* Update seqnum based on intkn_seq[] */
  54396. + i = 0;
  54397. + do {
  54398. + seqnum[intkn_seq[ndx]] = i;
  54399. + ndx++;
  54400. + i++;
  54401. + if (ndx == TOKEN_Q_DEPTH)
  54402. + ndx = 0;
  54403. + } while ( i < TOKEN_Q_DEPTH );
  54404. +
  54405. + /* Mark non active EP's in seqnum[] by 0xff */
  54406. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  54407. + if (core_if->nextep_seq[i] == 0xff )
  54408. + seqnum[i] = 0xff;
  54409. + }
  54410. +
  54411. + /* Sort seqnum[] */
  54412. + sort_done = 0;
  54413. + while (!sort_done) {
  54414. + sort_done = 1;
  54415. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  54416. + if (seqnum[i] > seqnum[i+1]) {
  54417. + temp = seqnum[i];
  54418. + seqnum[i] = seqnum[i+1];
  54419. + seqnum[i+1] = temp;
  54420. + sort_done = 0;
  54421. + }
  54422. + }
  54423. + }
  54424. +
  54425. + ndx = start + seqnum[0];
  54426. + if (ndx >= TOKEN_Q_DEPTH)
  54427. + ndx = ndx % TOKEN_Q_DEPTH;
  54428. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  54429. +
  54430. + /* Update seqnum[] by EP numbers */
  54431. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  54432. + ndx = start + i;
  54433. + if (seqnum[i] < 31) {
  54434. + ndx = start + seqnum[i];
  54435. + if (ndx >= TOKEN_Q_DEPTH)
  54436. + ndx = ndx % TOKEN_Q_DEPTH;
  54437. + seqnum[i] = intkn_seq[ndx];
  54438. + } else {
  54439. + if (seqnum[i] < 0xff) {
  54440. + seqnum[i] = seqnum[i] - 31;
  54441. + } else {
  54442. + break;
  54443. + }
  54444. + }
  54445. + }
  54446. +
  54447. + /* Update nextep_seq[] based on seqnum[] */
  54448. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  54449. + if (seqnum[i] != 0xff) {
  54450. + if (seqnum[i+1] != 0xff) {
  54451. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  54452. + } else {
  54453. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  54454. + break;
  54455. + }
  54456. + } else {
  54457. + break;
  54458. + }
  54459. + }
  54460. +
  54461. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  54462. + __func__, core_if->first_in_nextep_seq);
  54463. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  54464. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  54465. + }
  54466. +
  54467. + /* Flush the Learning Queue */
  54468. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  54469. + resetctl.b.intknqflsh = 1;
  54470. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  54471. +
  54472. +
  54473. +}
  54474. +
  54475. +/**
  54476. + * handle the IN EP disable interrupt.
  54477. + */
  54478. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  54479. + const uint32_t epnum)
  54480. +{
  54481. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54482. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54483. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54484. + dctl_data_t dctl = {.d32 = 0 };
  54485. + dwc_otg_pcd_ep_t *ep;
  54486. + dwc_ep_t *dwc_ep;
  54487. + gintmsk_data_t gintmsk_data;
  54488. + depctl_data_t depctl;
  54489. + uint32_t diepdma;
  54490. + uint32_t remain_to_transfer = 0;
  54491. + uint8_t i;
  54492. + uint32_t xfer_size;
  54493. +
  54494. + ep = get_in_ep(pcd, epnum);
  54495. + dwc_ep = &ep->dwc_ep;
  54496. +
  54497. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54498. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  54499. + complete_ep(ep);
  54500. + return;
  54501. + }
  54502. +
  54503. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  54504. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  54505. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  54506. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  54507. +
  54508. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  54509. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  54510. +
  54511. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  54512. + if (ep->stopped) {
  54513. + if (core_if->en_multiple_tx_fifo)
  54514. + /* Flush the Tx FIFO */
  54515. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  54516. + /* Clear the Global IN NP NAK */
  54517. + dctl.d32 = 0;
  54518. + dctl.b.cgnpinnak = 1;
  54519. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54520. + /* Restart the transaction */
  54521. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  54522. + restart_transfer(pcd, epnum);
  54523. + }
  54524. + } else {
  54525. + /* Restart the transaction */
  54526. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  54527. + restart_transfer(pcd, epnum);
  54528. + }
  54529. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  54530. + }
  54531. + return;
  54532. + }
  54533. +
  54534. + if (core_if->start_predict > 2) { // NP IN EP
  54535. + core_if->start_predict--;
  54536. + return;
  54537. + }
  54538. +
  54539. + core_if->start_predict--;
  54540. +
  54541. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  54542. +
  54543. + predict_nextep_seq(core_if);
  54544. +
  54545. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  54546. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  54547. + depctl.d32 =
  54548. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54549. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  54550. + depctl.b.nextep = core_if->nextep_seq[i];
  54551. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  54552. + }
  54553. + }
  54554. + /* Flush Shared NP TxFIFO */
  54555. + dwc_otg_flush_tx_fifo(core_if, 0);
  54556. + /* Rewind buffers */
  54557. + if (!core_if->dma_desc_enable) {
  54558. + i = core_if->first_in_nextep_seq;
  54559. + do {
  54560. + ep = get_in_ep(pcd, i);
  54561. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  54562. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  54563. + if (xfer_size > ep->dwc_ep.maxxfer)
  54564. + xfer_size = ep->dwc_ep.maxxfer;
  54565. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54566. + if (dieptsiz.b.pktcnt != 0) {
  54567. + if (xfer_size == 0) {
  54568. + remain_to_transfer = 0;
  54569. + } else {
  54570. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  54571. + remain_to_transfer =
  54572. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  54573. + } else {
  54574. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  54575. + + (xfer_size % ep->dwc_ep.maxpacket);
  54576. + }
  54577. + }
  54578. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  54579. + dieptsiz.b.xfersize = remain_to_transfer;
  54580. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  54581. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  54582. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  54583. + }
  54584. + i = core_if->nextep_seq[i];
  54585. + } while (i != core_if->first_in_nextep_seq);
  54586. + } else { // dma_desc_enable
  54587. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  54588. + }
  54589. +
  54590. + /* Restart transfers in predicted sequences */
  54591. + i = core_if->first_in_nextep_seq;
  54592. + do {
  54593. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  54594. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54595. + if (dieptsiz.b.pktcnt != 0) {
  54596. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54597. + depctl.b.epena = 1;
  54598. + depctl.b.cnak = 1;
  54599. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  54600. + }
  54601. + i = core_if->nextep_seq[i];
  54602. + } while (i != core_if->first_in_nextep_seq);
  54603. +
  54604. + /* Clear the global non-periodic IN NAK handshake */
  54605. + dctl.d32 = 0;
  54606. + dctl.b.cgnpinnak = 1;
  54607. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54608. +
  54609. + /* Unmask EP Mismatch interrupt */
  54610. + gintmsk_data.d32 = 0;
  54611. + gintmsk_data.b.epmismatch = 1;
  54612. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  54613. +
  54614. + core_if->start_predict = 0;
  54615. +
  54616. + }
  54617. +}
  54618. +
  54619. +/**
  54620. + * Handler for the IN EP timeout handshake interrupt.
  54621. + */
  54622. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  54623. + const uint32_t epnum)
  54624. +{
  54625. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54626. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54627. +
  54628. +#ifdef DEBUG
  54629. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54630. + uint32_t num = 0;
  54631. +#endif
  54632. + dctl_data_t dctl = {.d32 = 0 };
  54633. + dwc_otg_pcd_ep_t *ep;
  54634. +
  54635. + gintmsk_data_t intr_mask = {.d32 = 0 };
  54636. +
  54637. + ep = get_in_ep(pcd, epnum);
  54638. +
  54639. + /* Disable the NP Tx Fifo Empty Interrrupt */
  54640. + if (!core_if->dma_enable) {
  54641. + intr_mask.b.nptxfempty = 1;
  54642. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  54643. + intr_mask.d32, 0);
  54644. + }
  54645. + /** @todo NGS Check EP type.
  54646. + * Implement for Periodic EPs */
  54647. + /*
  54648. + * Non-periodic EP
  54649. + */
  54650. + /* Enable the Global IN NAK Effective Interrupt */
  54651. + intr_mask.b.ginnakeff = 1;
  54652. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  54653. +
  54654. + /* Set Global IN NAK */
  54655. + dctl.b.sgnpinnak = 1;
  54656. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54657. +
  54658. + ep->stopped = 1;
  54659. +
  54660. +#ifdef DEBUG
  54661. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  54662. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  54663. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  54664. +#endif
  54665. +
  54666. +#ifdef DISABLE_PERIODIC_EP
  54667. + /*
  54668. + * Set the NAK bit for this EP to
  54669. + * start the disable process.
  54670. + */
  54671. + diepctl.d32 = 0;
  54672. + diepctl.b.snak = 1;
  54673. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  54674. + diepctl.d32);
  54675. + ep->disabling = 1;
  54676. + ep->stopped = 1;
  54677. +#endif
  54678. +}
  54679. +
  54680. +/**
  54681. + * Handler for the IN EP NAK interrupt.
  54682. + */
  54683. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  54684. + const uint32_t epnum)
  54685. +{
  54686. + /** @todo implement ISR */
  54687. + dwc_otg_core_if_t *core_if;
  54688. + diepmsk_data_t intr_mask = {.d32 = 0 };
  54689. +
  54690. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  54691. + core_if = GET_CORE_IF(pcd);
  54692. + intr_mask.b.nak = 1;
  54693. +
  54694. + if (core_if->multiproc_int_enable) {
  54695. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54696. + diepeachintmsk[epnum], intr_mask.d32, 0);
  54697. + } else {
  54698. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  54699. + intr_mask.d32, 0);
  54700. + }
  54701. +
  54702. + return 1;
  54703. +}
  54704. +
  54705. +/**
  54706. + * Handler for the OUT EP Babble interrupt.
  54707. + */
  54708. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  54709. + const uint32_t epnum)
  54710. +{
  54711. + /** @todo implement ISR */
  54712. + dwc_otg_core_if_t *core_if;
  54713. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54714. +
  54715. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  54716. + "OUT EP Babble");
  54717. + core_if = GET_CORE_IF(pcd);
  54718. + intr_mask.b.babble = 1;
  54719. +
  54720. + if (core_if->multiproc_int_enable) {
  54721. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54722. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54723. + } else {
  54724. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54725. + intr_mask.d32, 0);
  54726. + }
  54727. +
  54728. + return 1;
  54729. +}
  54730. +
  54731. +/**
  54732. + * Handler for the OUT EP NAK interrupt.
  54733. + */
  54734. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  54735. + const uint32_t epnum)
  54736. +{
  54737. + /** @todo implement ISR */
  54738. + dwc_otg_core_if_t *core_if;
  54739. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54740. +
  54741. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  54742. + core_if = GET_CORE_IF(pcd);
  54743. + intr_mask.b.nak = 1;
  54744. +
  54745. + if (core_if->multiproc_int_enable) {
  54746. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54747. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54748. + } else {
  54749. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54750. + intr_mask.d32, 0);
  54751. + }
  54752. +
  54753. + return 1;
  54754. +}
  54755. +
  54756. +/**
  54757. + * Handler for the OUT EP NYET interrupt.
  54758. + */
  54759. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  54760. + const uint32_t epnum)
  54761. +{
  54762. + /** @todo implement ISR */
  54763. + dwc_otg_core_if_t *core_if;
  54764. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54765. +
  54766. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  54767. + core_if = GET_CORE_IF(pcd);
  54768. + intr_mask.b.nyet = 1;
  54769. +
  54770. + if (core_if->multiproc_int_enable) {
  54771. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54772. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54773. + } else {
  54774. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54775. + intr_mask.d32, 0);
  54776. + }
  54777. +
  54778. + return 1;
  54779. +}
  54780. +
  54781. +/**
  54782. + * This interrupt indicates that an IN EP has a pending Interrupt.
  54783. + * The sequence for handling the IN EP interrupt is shown below:
  54784. + * -# Read the Device All Endpoint Interrupt register
  54785. + * -# Repeat the following for each IN EP interrupt bit set (from
  54786. + * LSB to MSB).
  54787. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  54788. + * -# If "Transfer Complete" call the request complete function
  54789. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  54790. + * -# If "AHB Error Interrupt" log error
  54791. + * -# If "Time-out Handshake" log error
  54792. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  54793. + * FIFO.
  54794. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  54795. + * Mismatch Interrupt)
  54796. + */
  54797. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  54798. +{
  54799. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  54800. +do { \
  54801. + diepint_data_t diepint = {.d32=0}; \
  54802. + diepint.b.__intr = 1; \
  54803. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  54804. + diepint.d32); \
  54805. +} while (0)
  54806. +
  54807. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54808. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54809. + diepint_data_t diepint = {.d32 = 0 };
  54810. + depctl_data_t depctl = {.d32 = 0 };
  54811. + uint32_t ep_intr;
  54812. + uint32_t epnum = 0;
  54813. + dwc_otg_pcd_ep_t *ep;
  54814. + dwc_ep_t *dwc_ep;
  54815. + gintmsk_data_t intr_mask = {.d32 = 0 };
  54816. +
  54817. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  54818. +
  54819. + /* Read in the device interrupt bits */
  54820. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  54821. +
  54822. + /* Service the Device IN interrupts for each endpoint */
  54823. + while (ep_intr) {
  54824. + if (ep_intr & 0x1) {
  54825. + uint32_t empty_msk;
  54826. + /* Get EP pointer */
  54827. + ep = get_in_ep(pcd, epnum);
  54828. + dwc_ep = &ep->dwc_ep;
  54829. +
  54830. + depctl.d32 =
  54831. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  54832. + empty_msk =
  54833. + DWC_READ_REG32(&dev_if->
  54834. + dev_global_regs->dtknqr4_fifoemptymsk);
  54835. +
  54836. + DWC_DEBUGPL(DBG_PCDV,
  54837. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  54838. + epnum, empty_msk, depctl.d32);
  54839. +
  54840. + DWC_DEBUGPL(DBG_PCD,
  54841. + "EP%d-%s: type=%d, mps=%d\n",
  54842. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  54843. + dwc_ep->type, dwc_ep->maxpacket);
  54844. +
  54845. + diepint.d32 =
  54846. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  54847. +
  54848. + DWC_DEBUGPL(DBG_PCDV,
  54849. + "EP %d Interrupt Register - 0x%x\n", epnum,
  54850. + diepint.d32);
  54851. + /* Transfer complete */
  54852. + if (diepint.b.xfercompl) {
  54853. + /* Disable the NP Tx FIFO Empty
  54854. + * Interrupt */
  54855. + if (core_if->en_multiple_tx_fifo == 0) {
  54856. + intr_mask.b.nptxfempty = 1;
  54857. + DWC_MODIFY_REG32
  54858. + (&core_if->core_global_regs->gintmsk,
  54859. + intr_mask.d32, 0);
  54860. + } else {
  54861. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  54862. + uint32_t fifoemptymsk =
  54863. + 0x1 << dwc_ep->num;
  54864. + DWC_MODIFY_REG32(&core_if->
  54865. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  54866. + fifoemptymsk, 0);
  54867. + }
  54868. + /* Clear the bit in DIEPINTn for this interrupt */
  54869. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  54870. +
  54871. + /* Complete the transfer */
  54872. + if (epnum == 0) {
  54873. + handle_ep0(pcd);
  54874. + }
  54875. +#ifdef DWC_EN_ISOC
  54876. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54877. + if (!ep->stopped)
  54878. + complete_iso_ep(pcd, ep);
  54879. + }
  54880. +#endif /* DWC_EN_ISOC */
  54881. +#ifdef DWC_UTE_PER_IO
  54882. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54883. + if (!ep->stopped)
  54884. + complete_xiso_ep(ep);
  54885. + }
  54886. +#endif /* DWC_UTE_PER_IO */
  54887. + else {
  54888. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  54889. + dwc_ep->bInterval > 1) {
  54890. + dwc_ep->frame_num += dwc_ep->bInterval;
  54891. + if (dwc_ep->frame_num > 0x3FFF)
  54892. + {
  54893. + dwc_ep->frm_overrun = 1;
  54894. + dwc_ep->frame_num &= 0x3FFF;
  54895. + } else
  54896. + dwc_ep->frm_overrun = 0;
  54897. + }
  54898. + complete_ep(ep);
  54899. + if(diepint.b.nak)
  54900. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  54901. + }
  54902. + }
  54903. + /* Endpoint disable */
  54904. + if (diepint.b.epdisabled) {
  54905. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  54906. + epnum);
  54907. + handle_in_ep_disable_intr(pcd, epnum);
  54908. +
  54909. + /* Clear the bit in DIEPINTn for this interrupt */
  54910. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  54911. + }
  54912. + /* AHB Error */
  54913. + if (diepint.b.ahberr) {
  54914. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  54915. + /* Clear the bit in DIEPINTn for this interrupt */
  54916. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  54917. + }
  54918. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  54919. + if (diepint.b.timeout) {
  54920. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  54921. + handle_in_ep_timeout_intr(pcd, epnum);
  54922. +
  54923. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  54924. + }
  54925. + /** IN Token received with TxF Empty */
  54926. + if (diepint.b.intktxfemp) {
  54927. + DWC_DEBUGPL(DBG_ANY,
  54928. + "EP%d IN TKN TxFifo Empty\n",
  54929. + epnum);
  54930. + if (!ep->stopped && epnum != 0) {
  54931. +
  54932. + diepmsk_data_t diepmsk = {.d32 = 0 };
  54933. + diepmsk.b.intktxfemp = 1;
  54934. +
  54935. + if (core_if->multiproc_int_enable) {
  54936. + DWC_MODIFY_REG32
  54937. + (&dev_if->dev_global_regs->diepeachintmsk
  54938. + [epnum], diepmsk.d32, 0);
  54939. + } else {
  54940. + DWC_MODIFY_REG32
  54941. + (&dev_if->dev_global_regs->diepmsk,
  54942. + diepmsk.d32, 0);
  54943. + }
  54944. + } else if (core_if->dma_desc_enable
  54945. + && epnum == 0
  54946. + && pcd->ep0state ==
  54947. + EP0_OUT_STATUS_PHASE) {
  54948. + // EP0 IN set STALL
  54949. + depctl.d32 =
  54950. + DWC_READ_REG32(&dev_if->in_ep_regs
  54951. + [epnum]->diepctl);
  54952. +
  54953. + /* set the disable and stall bits */
  54954. + if (depctl.b.epena) {
  54955. + depctl.b.epdis = 1;
  54956. + }
  54957. + depctl.b.stall = 1;
  54958. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  54959. + [epnum]->diepctl,
  54960. + depctl.d32);
  54961. + }
  54962. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  54963. + }
  54964. + /** IN Token Received with EP mismatch */
  54965. + if (diepint.b.intknepmis) {
  54966. + DWC_DEBUGPL(DBG_ANY,
  54967. + "EP%d IN TKN EP Mismatch\n", epnum);
  54968. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  54969. + }
  54970. + /** IN Endpoint NAK Effective */
  54971. + if (diepint.b.inepnakeff) {
  54972. + DWC_DEBUGPL(DBG_ANY,
  54973. + "EP%d IN EP NAK Effective\n",
  54974. + epnum);
  54975. + /* Periodic EP */
  54976. + if (ep->disabling) {
  54977. + depctl.d32 = 0;
  54978. + depctl.b.snak = 1;
  54979. + depctl.b.epdis = 1;
  54980. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  54981. + [epnum]->diepctl,
  54982. + depctl.d32,
  54983. + depctl.d32);
  54984. + }
  54985. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  54986. +
  54987. + }
  54988. +
  54989. + /** IN EP Tx FIFO Empty Intr */
  54990. + if (diepint.b.emptyintr) {
  54991. + DWC_DEBUGPL(DBG_ANY,
  54992. + "EP%d Tx FIFO Empty Intr \n",
  54993. + epnum);
  54994. + write_empty_tx_fifo(pcd, epnum);
  54995. +
  54996. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  54997. +
  54998. + }
  54999. +
  55000. + /** IN EP BNA Intr */
  55001. + if (diepint.b.bna) {
  55002. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  55003. + if (core_if->dma_desc_enable) {
  55004. +#ifdef DWC_EN_ISOC
  55005. + if (dwc_ep->type ==
  55006. + DWC_OTG_EP_TYPE_ISOC) {
  55007. + /*
  55008. + * This checking is performed to prevent first "false" BNA
  55009. + * handling occuring right after reconnect
  55010. + */
  55011. + if (dwc_ep->next_frame !=
  55012. + 0xffffffff)
  55013. + dwc_otg_pcd_handle_iso_bna(ep);
  55014. + } else
  55015. +#endif /* DWC_EN_ISOC */
  55016. + {
  55017. + dwc_otg_pcd_handle_noniso_bna(ep);
  55018. + }
  55019. + }
  55020. + }
  55021. + /* NAK Interrutp */
  55022. + if (diepint.b.nak) {
  55023. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  55024. + epnum);
  55025. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55026. + depctl_data_t depctl;
  55027. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  55028. + ep->dwc_ep.frame_num = core_if->frame_num;
  55029. + if (ep->dwc_ep.bInterval > 1) {
  55030. + depctl.d32 = 0;
  55031. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  55032. + if (ep->dwc_ep.frame_num & 0x1) {
  55033. + depctl.b.setd1pid = 1;
  55034. + depctl.b.setd0pid = 0;
  55035. + } else {
  55036. + depctl.b.setd0pid = 1;
  55037. + depctl.b.setd1pid = 0;
  55038. + }
  55039. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  55040. + }
  55041. + start_next_request(ep);
  55042. + }
  55043. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  55044. + if (dwc_ep->frame_num > 0x3FFF) {
  55045. + dwc_ep->frm_overrun = 1;
  55046. + dwc_ep->frame_num &= 0x3FFF;
  55047. + } else
  55048. + dwc_ep->frm_overrun = 0;
  55049. + }
  55050. +
  55051. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  55052. + }
  55053. + }
  55054. + epnum++;
  55055. + ep_intr >>= 1;
  55056. + }
  55057. +
  55058. + return 1;
  55059. +#undef CLEAR_IN_EP_INTR
  55060. +}
  55061. +
  55062. +/**
  55063. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  55064. + * The sequence for handling the OUT EP interrupt is shown below:
  55065. + * -# Read the Device All Endpoint Interrupt register
  55066. + * -# Repeat the following for each OUT EP interrupt bit set (from
  55067. + * LSB to MSB).
  55068. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  55069. + * -# If "Transfer Complete" call the request complete function
  55070. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  55071. + * -# If "AHB Error Interrupt" log error
  55072. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  55073. + * Command Processing)
  55074. + */
  55075. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  55076. +{
  55077. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  55078. +do { \
  55079. + doepint_data_t doepint = {.d32=0}; \
  55080. + doepint.b.__intr = 1; \
  55081. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  55082. + doepint.d32); \
  55083. +} while (0)
  55084. +
  55085. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55086. + uint32_t ep_intr;
  55087. + doepint_data_t doepint = {.d32 = 0 };
  55088. + uint32_t epnum = 0;
  55089. + dwc_otg_pcd_ep_t *ep;
  55090. + dwc_ep_t *dwc_ep;
  55091. + dctl_data_t dctl = {.d32 = 0 };
  55092. + gintmsk_data_t gintmsk = {.d32 = 0 };
  55093. +
  55094. +
  55095. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  55096. +
  55097. + /* Read in the device interrupt bits */
  55098. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  55099. +
  55100. + while (ep_intr) {
  55101. + if (ep_intr & 0x1) {
  55102. + /* Get EP pointer */
  55103. + ep = get_out_ep(pcd, epnum);
  55104. + dwc_ep = &ep->dwc_ep;
  55105. +
  55106. +#ifdef VERBOSE
  55107. + DWC_DEBUGPL(DBG_PCDV,
  55108. + "EP%d-%s: type=%d, mps=%d\n",
  55109. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  55110. + dwc_ep->type, dwc_ep->maxpacket);
  55111. +#endif
  55112. + doepint.d32 =
  55113. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  55114. + /* Moved this interrupt upper due to core deffect of asserting
  55115. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  55116. + if (doepint.b.stsphsercvd) {
  55117. + deptsiz0_data_t deptsiz;
  55118. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  55119. + deptsiz.d32 =
  55120. + DWC_READ_REG32(&core_if->dev_if->
  55121. + out_ep_regs[0]->doeptsiz);
  55122. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  55123. + && core_if->dma_enable
  55124. + && core_if->dma_desc_enable == 0
  55125. + && doepint.b.xfercompl
  55126. + && deptsiz.b.xfersize == 24) {
  55127. + CLEAR_OUT_EP_INTR(core_if, epnum,
  55128. + xfercompl);
  55129. + doepint.b.xfercompl = 0;
  55130. + ep0_out_start(core_if, pcd);
  55131. + }
  55132. + if ((core_if->dma_desc_enable) ||
  55133. + (core_if->dma_enable
  55134. + && core_if->snpsid >=
  55135. + OTG_CORE_REV_3_00a)) {
  55136. + do_setup_in_status_phase(pcd);
  55137. + }
  55138. + }
  55139. + /* Transfer complete */
  55140. + if (doepint.b.xfercompl) {
  55141. +
  55142. + if (epnum == 0) {
  55143. + /* Clear the bit in DOEPINTn for this interrupt */
  55144. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  55145. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  55146. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  55147. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  55148. + doepint.d32);
  55149. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  55150. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  55151. +
  55152. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  55153. + && core_if->dma_enable == 0) {
  55154. + doepint_data_t doepint;
  55155. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55156. + out_ep_regs[0]->doepint);
  55157. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  55158. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55159. + goto exit_xfercompl;
  55160. + }
  55161. + }
  55162. + /* In case of DDMA look at SR bit to go to the Data Stage */
  55163. + if (core_if->dma_desc_enable) {
  55164. + dev_dma_desc_sts_t status = {.d32 = 0};
  55165. + if (pcd->ep0state == EP0_IDLE) {
  55166. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  55167. + dev_if->setup_desc_index]->status.d32;
  55168. + if(pcd->data_terminated) {
  55169. + pcd->data_terminated = 0;
  55170. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  55171. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  55172. + }
  55173. + if (status.b.sr) {
  55174. + if (doepint.b.setup) {
  55175. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  55176. + /* Already started data stage, clear setup */
  55177. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55178. + doepint.b.setup = 0;
  55179. + handle_ep0(pcd);
  55180. + /* Prepare for more setup packets */
  55181. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  55182. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  55183. + ep0_out_start(core_if, pcd);
  55184. + }
  55185. +
  55186. + goto exit_xfercompl;
  55187. + } else {
  55188. + /* Prepare for more setup packets */
  55189. + DWC_DEBUGPL(DBG_PCDV,
  55190. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  55191. + ep0_out_start(core_if, pcd);
  55192. + }
  55193. + }
  55194. + } else {
  55195. + dwc_otg_pcd_request_t *req;
  55196. + dev_dma_desc_sts_t status = {.d32 = 0};
  55197. + diepint_data_t diepint0;
  55198. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55199. + in_ep_regs[0]->diepint);
  55200. +
  55201. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  55202. + DWC_ERROR("EP0 is stalled/disconnected\n");
  55203. + }
  55204. +
  55205. + /* Clear IN xfercompl if set */
  55206. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  55207. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  55208. + DWC_WRITE_REG32(&core_if->dev_if->
  55209. + in_ep_regs[0]->diepint, diepint0.d32);
  55210. + }
  55211. +
  55212. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  55213. + dev_if->setup_desc_index]->status.d32;
  55214. +
  55215. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  55216. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  55217. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  55218. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  55219. + status.d32 = core_if->dev_if->
  55220. + out_desc_addr->status.d32;
  55221. +
  55222. + if (status.b.sr) {
  55223. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55224. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  55225. + } else {
  55226. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  55227. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55228. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  55229. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  55230. + /* Read arrived setup packet from req->buf */
  55231. + dwc_memcpy(&pcd->setup_pkt->req,
  55232. + req->buf + ep->dwc_ep.xfer_count, 8);
  55233. + }
  55234. + req->actual = ep->dwc_ep.xfer_count;
  55235. + dwc_otg_request_done(ep, req, -ECONNRESET);
  55236. + ep->dwc_ep.start_xfer_buff = 0;
  55237. + ep->dwc_ep.xfer_buff = 0;
  55238. + ep->dwc_ep.xfer_len = 0;
  55239. + }
  55240. + pcd->ep0state = EP0_IDLE;
  55241. + if (doepint.b.setup) {
  55242. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  55243. + /* Data stage started, clear setup */
  55244. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55245. + doepint.b.setup = 0;
  55246. + handle_ep0(pcd);
  55247. + /* Prepare for setup packets if ep0in was enabled*/
  55248. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55249. + ep0_out_start(core_if, pcd);
  55250. + }
  55251. +
  55252. + goto exit_xfercompl;
  55253. + } else {
  55254. + /* Prepare for more setup packets */
  55255. + DWC_DEBUGPL(DBG_PCDV,
  55256. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  55257. + ep0_out_start(core_if, pcd);
  55258. + }
  55259. + }
  55260. + }
  55261. + }
  55262. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  55263. + && core_if->dma_desc_enable == 0) {
  55264. + doepint_data_t doepint_temp = {.d32 = 0};
  55265. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  55266. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  55267. + out_ep_regs[ep->dwc_ep.num]->doepint);
  55268. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55269. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  55270. + if (pcd->ep0state == EP0_IDLE) {
  55271. + if (doepint_temp.b.sr) {
  55272. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55273. + }
  55274. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55275. + out_ep_regs[0]->doepint);
  55276. + if (doeptsize0.b.supcnt == 3) {
  55277. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  55278. + ep->dwc_ep.stp_rollover = 1;
  55279. + }
  55280. + if (doepint.b.setup) {
  55281. +retry:
  55282. + /* Already started data stage, clear setup */
  55283. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55284. + doepint.b.setup = 0;
  55285. + handle_ep0(pcd);
  55286. + ep->dwc_ep.stp_rollover = 0;
  55287. + /* Prepare for more setup packets */
  55288. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  55289. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  55290. + ep0_out_start(core_if, pcd);
  55291. + }
  55292. + goto exit_xfercompl;
  55293. + } else {
  55294. + /* Prepare for more setup packets */
  55295. + DWC_DEBUGPL(DBG_ANY,
  55296. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  55297. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55298. + out_ep_regs[0]->doepint);
  55299. + if(doepint.b.setup)
  55300. + goto retry;
  55301. + ep0_out_start(core_if, pcd);
  55302. + }
  55303. + } else {
  55304. + dwc_otg_pcd_request_t *req;
  55305. + diepint_data_t diepint0 = {.d32 = 0};
  55306. + doepint_data_t doepint_temp = {.d32 = 0};
  55307. + depctl_data_t diepctl0;
  55308. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55309. + in_ep_regs[0]->diepint);
  55310. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55311. + in_ep_regs[0]->diepctl);
  55312. +
  55313. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  55314. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55315. + if (diepint0.b.xfercompl) {
  55316. + DWC_WRITE_REG32(&core_if->dev_if->
  55317. + in_ep_regs[0]->diepint, diepint0.d32);
  55318. + }
  55319. + if (diepctl0.b.epena) {
  55320. + diepint_data_t diepint = {.d32 = 0};
  55321. + diepctl0.b.snak = 1;
  55322. + DWC_WRITE_REG32(&core_if->dev_if->
  55323. + in_ep_regs[0]->diepctl, diepctl0.d32);
  55324. + do {
  55325. + dwc_udelay(10);
  55326. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55327. + in_ep_regs[0]->diepint);
  55328. + } while (!diepint.b.inepnakeff);
  55329. + diepint.b.inepnakeff = 1;
  55330. + DWC_WRITE_REG32(&core_if->dev_if->
  55331. + in_ep_regs[0]->diepint, diepint.d32);
  55332. + diepctl0.d32 = 0;
  55333. + diepctl0.b.epdis = 1;
  55334. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  55335. + diepctl0.d32);
  55336. + do {
  55337. + dwc_udelay(10);
  55338. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55339. + in_ep_regs[0]->diepint);
  55340. + } while (!diepint.b.epdisabled);
  55341. + diepint.b.epdisabled = 1;
  55342. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  55343. + diepint.d32);
  55344. + }
  55345. + }
  55346. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  55347. + out_ep_regs[ep->dwc_ep.num]->doepint);
  55348. + if (doepint_temp.b.sr) {
  55349. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55350. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55351. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  55352. + } else {
  55353. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  55354. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55355. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  55356. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  55357. + /* Read arrived setup packet from req->buf */
  55358. + dwc_memcpy(&pcd->setup_pkt->req,
  55359. + req->buf + ep->dwc_ep.xfer_count, 8);
  55360. + }
  55361. + req->actual = ep->dwc_ep.xfer_count;
  55362. + dwc_otg_request_done(ep, req, -ECONNRESET);
  55363. + ep->dwc_ep.start_xfer_buff = 0;
  55364. + ep->dwc_ep.xfer_buff = 0;
  55365. + ep->dwc_ep.xfer_len = 0;
  55366. + }
  55367. + pcd->ep0state = EP0_IDLE;
  55368. + if (doepint.b.setup) {
  55369. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  55370. + /* Data stage started, clear setup */
  55371. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55372. + doepint.b.setup = 0;
  55373. + handle_ep0(pcd);
  55374. + /* Prepare for setup packets if ep0in was enabled*/
  55375. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55376. + ep0_out_start(core_if, pcd);
  55377. + }
  55378. + goto exit_xfercompl;
  55379. + } else {
  55380. + /* Prepare for more setup packets */
  55381. + DWC_DEBUGPL(DBG_PCDV,
  55382. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  55383. + ep0_out_start(core_if, pcd);
  55384. + }
  55385. + }
  55386. + }
  55387. + }
  55388. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  55389. + handle_ep0(pcd);
  55390. +exit_xfercompl:
  55391. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  55392. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  55393. + } else {
  55394. + if (core_if->dma_desc_enable == 0
  55395. + || pcd->ep0state != EP0_IDLE)
  55396. + handle_ep0(pcd);
  55397. + }
  55398. +#ifdef DWC_EN_ISOC
  55399. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55400. + if (doepint.b.pktdrpsts == 0) {
  55401. + /* Clear the bit in DOEPINTn for this interrupt */
  55402. + CLEAR_OUT_EP_INTR(core_if,
  55403. + epnum,
  55404. + xfercompl);
  55405. + complete_iso_ep(pcd, ep);
  55406. + } else {
  55407. +
  55408. + doepint_data_t doepint = {.d32 = 0 };
  55409. + doepint.b.xfercompl = 1;
  55410. + doepint.b.pktdrpsts = 1;
  55411. + DWC_WRITE_REG32
  55412. + (&core_if->dev_if->out_ep_regs
  55413. + [epnum]->doepint,
  55414. + doepint.d32);
  55415. + if (handle_iso_out_pkt_dropped
  55416. + (core_if, dwc_ep)) {
  55417. + complete_iso_ep(pcd,
  55418. + ep);
  55419. + }
  55420. + }
  55421. +#endif /* DWC_EN_ISOC */
  55422. +#ifdef DWC_UTE_PER_IO
  55423. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55424. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  55425. + if (!ep->stopped)
  55426. + complete_xiso_ep(ep);
  55427. +#endif /* DWC_UTE_PER_IO */
  55428. + } else {
  55429. + /* Clear the bit in DOEPINTn for this interrupt */
  55430. + CLEAR_OUT_EP_INTR(core_if, epnum,
  55431. + xfercompl);
  55432. +
  55433. + if (core_if->core_params->dev_out_nak) {
  55434. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  55435. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  55436. +#ifdef DEBUG
  55437. + print_memory_payload(pcd, dwc_ep);
  55438. +#endif
  55439. + }
  55440. + complete_ep(ep);
  55441. + }
  55442. +
  55443. + }
  55444. +
  55445. + /* Endpoint disable */
  55446. + if (doepint.b.epdisabled) {
  55447. +
  55448. + /* Clear the bit in DOEPINTn for this interrupt */
  55449. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  55450. + if (core_if->core_params->dev_out_nak) {
  55451. +#ifdef DEBUG
  55452. + print_memory_payload(pcd, dwc_ep);
  55453. +#endif
  55454. + /* In case of timeout condition */
  55455. + if (core_if->ep_xfer_info[epnum].state == 2) {
  55456. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55457. + dev_global_regs->dctl);
  55458. + dctl.b.cgoutnak = 1;
  55459. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55460. + dctl.d32);
  55461. + /* Unmask goutnakeff interrupt which was masked
  55462. + * during handle nak out interrupt */
  55463. + gintmsk.b.goutnakeff = 1;
  55464. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  55465. + 0, gintmsk.d32);
  55466. +
  55467. + complete_ep(ep);
  55468. + }
  55469. + }
  55470. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  55471. + {
  55472. + dctl_data_t dctl;
  55473. + gintmsk_data_t intr_mask = {.d32 = 0};
  55474. + dwc_otg_pcd_request_t *req = 0;
  55475. +
  55476. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55477. + dev_global_regs->dctl);
  55478. + dctl.b.cgoutnak = 1;
  55479. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55480. + dctl.d32);
  55481. +
  55482. + intr_mask.d32 = 0;
  55483. + intr_mask.b.incomplisoout = 1;
  55484. +
  55485. + /* Get any pending requests */
  55486. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55487. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55488. + if (!req) {
  55489. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  55490. + } else {
  55491. + dwc_otg_request_done(ep, req, 0);
  55492. + start_next_request(ep);
  55493. + }
  55494. + } else {
  55495. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  55496. + }
  55497. + }
  55498. + }
  55499. + /* AHB Error */
  55500. + if (doepint.b.ahberr) {
  55501. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  55502. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  55503. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  55504. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  55505. + }
  55506. + /* Setup Phase Done (contorl EPs) */
  55507. + if (doepint.b.setup) {
  55508. +#ifdef DEBUG_EP0
  55509. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  55510. +#endif
  55511. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55512. +
  55513. + handle_ep0(pcd);
  55514. + }
  55515. +
  55516. + /** OUT EP BNA Intr */
  55517. + if (doepint.b.bna) {
  55518. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  55519. + if (core_if->dma_desc_enable) {
  55520. +#ifdef DWC_EN_ISOC
  55521. + if (dwc_ep->type ==
  55522. + DWC_OTG_EP_TYPE_ISOC) {
  55523. + /*
  55524. + * This checking is performed to prevent first "false" BNA
  55525. + * handling occuring right after reconnect
  55526. + */
  55527. + if (dwc_ep->next_frame !=
  55528. + 0xffffffff)
  55529. + dwc_otg_pcd_handle_iso_bna(ep);
  55530. + } else
  55531. +#endif /* DWC_EN_ISOC */
  55532. + {
  55533. + dwc_otg_pcd_handle_noniso_bna(ep);
  55534. + }
  55535. + }
  55536. + }
  55537. + /* Babble Interrupt */
  55538. + if (doepint.b.babble) {
  55539. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  55540. + epnum);
  55541. + handle_out_ep_babble_intr(pcd, epnum);
  55542. +
  55543. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  55544. + }
  55545. + if (doepint.b.outtknepdis) {
  55546. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  55547. + disabled\n",epnum);
  55548. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55549. + doepmsk_data_t doepmsk = {.d32 = 0};
  55550. + ep->dwc_ep.frame_num = core_if->frame_num;
  55551. + if (ep->dwc_ep.bInterval > 1) {
  55552. + depctl_data_t depctl;
  55553. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55554. + out_ep_regs[epnum]->doepctl);
  55555. + if (ep->dwc_ep.frame_num & 0x1) {
  55556. + depctl.b.setd1pid = 1;
  55557. + depctl.b.setd0pid = 0;
  55558. + } else {
  55559. + depctl.b.setd0pid = 1;
  55560. + depctl.b.setd1pid = 0;
  55561. + }
  55562. + DWC_WRITE_REG32(&core_if->dev_if->
  55563. + out_ep_regs[epnum]->doepctl, depctl.d32);
  55564. + }
  55565. + start_next_request(ep);
  55566. + doepmsk.b.outtknepdis = 1;
  55567. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  55568. + doepmsk.d32, 0);
  55569. + }
  55570. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  55571. + }
  55572. +
  55573. + /* NAK Interrutp */
  55574. + if (doepint.b.nak) {
  55575. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  55576. + handle_out_ep_nak_intr(pcd, epnum);
  55577. +
  55578. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  55579. + }
  55580. + /* NYET Interrutp */
  55581. + if (doepint.b.nyet) {
  55582. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  55583. + handle_out_ep_nyet_intr(pcd, epnum);
  55584. +
  55585. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  55586. + }
  55587. + }
  55588. +
  55589. + epnum++;
  55590. + ep_intr >>= 1;
  55591. + }
  55592. +
  55593. + return 1;
  55594. +
  55595. +#undef CLEAR_OUT_EP_INTR
  55596. +}
  55597. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  55598. +{
  55599. + int retval = 0;
  55600. + if(!frm_overrun && curr_fr >= trgt_fr)
  55601. + retval = 1;
  55602. + else if (frm_overrun
  55603. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  55604. + retval = 1;
  55605. + return retval;
  55606. +}
  55607. +/**
  55608. + * Incomplete ISO IN Transfer Interrupt.
  55609. + * This interrupt indicates one of the following conditions occurred
  55610. + * while transmitting an ISOC transaction.
  55611. + * - Corrupted IN Token for ISOC EP.
  55612. + * - Packet not complete in FIFO.
  55613. + * The follow actions will be taken:
  55614. + * -# Determine the EP
  55615. + * -# Set incomplete flag in dwc_ep structure
  55616. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  55617. + * Flush FIFO
  55618. + */
  55619. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  55620. +{
  55621. + gintsts_data_t gintsts;
  55622. +
  55623. +#ifdef DWC_EN_ISOC
  55624. + dwc_otg_dev_if_t *dev_if;
  55625. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55626. + depctl_data_t depctl = {.d32 = 0 };
  55627. + dsts_data_t dsts = {.d32 = 0 };
  55628. + dwc_ep_t *dwc_ep;
  55629. + int i;
  55630. +
  55631. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55632. +
  55633. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  55634. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  55635. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55636. + deptsiz.d32 =
  55637. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  55638. + depctl.d32 =
  55639. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55640. +
  55641. + if (depctl.b.epdis && deptsiz.d32) {
  55642. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  55643. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  55644. + dwc_ep->cur_pkt = 0;
  55645. + dwc_ep->proc_buf_num =
  55646. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  55647. +
  55648. + if (dwc_ep->proc_buf_num) {
  55649. + dwc_ep->cur_pkt_addr =
  55650. + dwc_ep->xfer_buff1;
  55651. + dwc_ep->cur_pkt_dma_addr =
  55652. + dwc_ep->dma_addr1;
  55653. + } else {
  55654. + dwc_ep->cur_pkt_addr =
  55655. + dwc_ep->xfer_buff0;
  55656. + dwc_ep->cur_pkt_dma_addr =
  55657. + dwc_ep->dma_addr0;
  55658. + }
  55659. +
  55660. + }
  55661. +
  55662. + dsts.d32 =
  55663. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  55664. + dev_global_regs->dsts);
  55665. + dwc_ep->next_frame = dsts.b.soffn;
  55666. +
  55667. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  55668. + (pcd),
  55669. + dwc_ep);
  55670. + }
  55671. + }
  55672. + }
  55673. +
  55674. +#else
  55675. + depctl_data_t depctl = {.d32 = 0 };
  55676. + dwc_ep_t *dwc_ep;
  55677. + dwc_otg_dev_if_t *dev_if;
  55678. + int i;
  55679. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55680. +
  55681. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  55682. +
  55683. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  55684. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  55685. + depctl.d32 =
  55686. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55687. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55688. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  55689. + dwc_ep->frm_overrun))
  55690. + {
  55691. + depctl.d32 =
  55692. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55693. + depctl.b.snak = 1;
  55694. + depctl.b.epdis = 1;
  55695. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  55696. + }
  55697. + }
  55698. + }
  55699. +
  55700. + /*intr_mask.b.incomplisoin = 1;
  55701. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55702. + intr_mask.d32, 0); */
  55703. +#endif //DWC_EN_ISOC
  55704. +
  55705. + /* Clear interrupt */
  55706. + gintsts.d32 = 0;
  55707. + gintsts.b.incomplisoin = 1;
  55708. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55709. + gintsts.d32);
  55710. +
  55711. + return 1;
  55712. +}
  55713. +
  55714. +/**
  55715. + * Incomplete ISO OUT Transfer Interrupt.
  55716. + *
  55717. + * This interrupt indicates that the core has dropped an ISO OUT
  55718. + * packet. The following conditions can be the cause:
  55719. + * - FIFO Full, the entire packet would not fit in the FIFO.
  55720. + * - CRC Error
  55721. + * - Corrupted Token
  55722. + * The follow actions will be taken:
  55723. + * -# Determine the EP
  55724. + * -# Set incomplete flag in dwc_ep structure
  55725. + * -# Read any data from the FIFO
  55726. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  55727. + * re-enable EP.
  55728. + */
  55729. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  55730. +{
  55731. +
  55732. + gintsts_data_t gintsts;
  55733. +
  55734. +#ifdef DWC_EN_ISOC
  55735. + dwc_otg_dev_if_t *dev_if;
  55736. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55737. + depctl_data_t depctl = {.d32 = 0 };
  55738. + dsts_data_t dsts = {.d32 = 0 };
  55739. + dwc_ep_t *dwc_ep;
  55740. + int i;
  55741. +
  55742. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55743. +
  55744. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  55745. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  55746. + if (pcd->out_ep[i].dwc_ep.active &&
  55747. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55748. + deptsiz.d32 =
  55749. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  55750. + depctl.d32 =
  55751. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  55752. +
  55753. + if (depctl.b.epdis && deptsiz.d32) {
  55754. + set_current_pkt_info(GET_CORE_IF(pcd),
  55755. + &pcd->out_ep[i].dwc_ep);
  55756. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  55757. + dwc_ep->cur_pkt = 0;
  55758. + dwc_ep->proc_buf_num =
  55759. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  55760. +
  55761. + if (dwc_ep->proc_buf_num) {
  55762. + dwc_ep->cur_pkt_addr =
  55763. + dwc_ep->xfer_buff1;
  55764. + dwc_ep->cur_pkt_dma_addr =
  55765. + dwc_ep->dma_addr1;
  55766. + } else {
  55767. + dwc_ep->cur_pkt_addr =
  55768. + dwc_ep->xfer_buff0;
  55769. + dwc_ep->cur_pkt_dma_addr =
  55770. + dwc_ep->dma_addr0;
  55771. + }
  55772. +
  55773. + }
  55774. +
  55775. + dsts.d32 =
  55776. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  55777. + dev_global_regs->dsts);
  55778. + dwc_ep->next_frame = dsts.b.soffn;
  55779. +
  55780. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  55781. + (pcd),
  55782. + dwc_ep);
  55783. + }
  55784. + }
  55785. + }
  55786. +#else
  55787. + /** @todo implement ISR */
  55788. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55789. + dwc_otg_core_if_t *core_if;
  55790. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55791. + depctl_data_t depctl = {.d32 = 0 };
  55792. + dctl_data_t dctl = {.d32 = 0 };
  55793. + dwc_ep_t *dwc_ep = NULL;
  55794. + int i;
  55795. + core_if = GET_CORE_IF(pcd);
  55796. +
  55797. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  55798. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  55799. + depctl.d32 =
  55800. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  55801. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  55802. + core_if->dev_if->isoc_ep = dwc_ep;
  55803. + deptsiz.d32 =
  55804. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  55805. + break;
  55806. + }
  55807. + }
  55808. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  55809. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  55810. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  55811. +
  55812. + if (!intr_mask.b.goutnakeff) {
  55813. + /* Unmask it */
  55814. + intr_mask.b.goutnakeff = 1;
  55815. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  55816. + }
  55817. + if (!gintsts.b.goutnakeff) {
  55818. + dctl.b.sgoutnak = 1;
  55819. + }
  55820. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  55821. +
  55822. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  55823. + if (depctl.b.epena) {
  55824. + depctl.b.epdis = 1;
  55825. + depctl.b.snak = 1;
  55826. + }
  55827. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  55828. +
  55829. + intr_mask.d32 = 0;
  55830. + intr_mask.b.incomplisoout = 1;
  55831. +
  55832. +#endif /* DWC_EN_ISOC */
  55833. +
  55834. + /* Clear interrupt */
  55835. + gintsts.d32 = 0;
  55836. + gintsts.b.incomplisoout = 1;
  55837. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55838. + gintsts.d32);
  55839. +
  55840. + return 1;
  55841. +}
  55842. +
  55843. +/**
  55844. + * This function handles the Global IN NAK Effective interrupt.
  55845. + *
  55846. + */
  55847. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  55848. +{
  55849. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  55850. + depctl_data_t diepctl = {.d32 = 0 };
  55851. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55852. + gintsts_data_t gintsts;
  55853. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55854. + int i;
  55855. +
  55856. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  55857. +
  55858. + /* Disable all active IN EPs */
  55859. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  55860. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55861. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  55862. + if (core_if->start_predict > 0)
  55863. + core_if->start_predict++;
  55864. + diepctl.b.epdis = 1;
  55865. + diepctl.b.snak = 1;
  55866. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  55867. + }
  55868. + }
  55869. +
  55870. +
  55871. + /* Disable the Global IN NAK Effective Interrupt */
  55872. + intr_mask.b.ginnakeff = 1;
  55873. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55874. + intr_mask.d32, 0);
  55875. +
  55876. + /* Clear interrupt */
  55877. + gintsts.d32 = 0;
  55878. + gintsts.b.ginnakeff = 1;
  55879. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55880. + gintsts.d32);
  55881. +
  55882. + return 1;
  55883. +}
  55884. +
  55885. +/**
  55886. + * OUT NAK Effective.
  55887. + *
  55888. + */
  55889. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  55890. +{
  55891. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  55892. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55893. + gintsts_data_t gintsts;
  55894. + depctl_data_t doepctl;
  55895. + int i;
  55896. +
  55897. + /* Disable the Global OUT NAK Effective Interrupt */
  55898. + intr_mask.b.goutnakeff = 1;
  55899. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55900. + intr_mask.d32, 0);
  55901. +
  55902. + /* If DEV OUT NAK enabled*/
  55903. + if (pcd->core_if->core_params->dev_out_nak) {
  55904. + /* Run over all out endpoints to determine the ep number on
  55905. + * which the timeout has happened
  55906. + */
  55907. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  55908. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  55909. + break;
  55910. + }
  55911. + if (i > dev_if->num_out_eps) {
  55912. + dctl_data_t dctl;
  55913. + dctl.d32 =
  55914. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  55915. + dctl.b.cgoutnak = 1;
  55916. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  55917. + dctl.d32);
  55918. + goto out;
  55919. + }
  55920. +
  55921. + /* Disable the endpoint */
  55922. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  55923. + if (doepctl.b.epena) {
  55924. + doepctl.b.epdis = 1;
  55925. + doepctl.b.snak = 1;
  55926. + }
  55927. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  55928. + return 1;
  55929. + }
  55930. + /* We come here from Incomplete ISO OUT handler */
  55931. + if (dev_if->isoc_ep) {
  55932. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  55933. + uint32_t epnum = dwc_ep->num;
  55934. + doepint_data_t doepint;
  55935. + doepint.d32 =
  55936. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  55937. + dev_if->isoc_ep = NULL;
  55938. + doepctl.d32 =
  55939. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  55940. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  55941. + if (doepctl.b.epena) {
  55942. + doepctl.b.epdis = 1;
  55943. + doepctl.b.snak = 1;
  55944. + }
  55945. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  55946. + doepctl.d32);
  55947. + return 1;
  55948. + } else
  55949. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  55950. + "Global OUT NAK Effective\n");
  55951. +
  55952. +out:
  55953. + /* Clear interrupt */
  55954. + gintsts.d32 = 0;
  55955. + gintsts.b.goutnakeff = 1;
  55956. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55957. + gintsts.d32);
  55958. +
  55959. + return 1;
  55960. +}
  55961. +
  55962. +/**
  55963. + * PCD interrupt handler.
  55964. + *
  55965. + * The PCD handles the device interrupts. Many conditions can cause a
  55966. + * device interrupt. When an interrupt occurs, the device interrupt
  55967. + * service routine determines the cause of the interrupt and
  55968. + * dispatches handling to the appropriate function. These interrupt
  55969. + * handling functions are described below.
  55970. + *
  55971. + * All interrupt registers are processed from LSB to MSB.
  55972. + *
  55973. + */
  55974. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  55975. +{
  55976. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55977. +#ifdef VERBOSE
  55978. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  55979. +#endif
  55980. + gintsts_data_t gintr_status;
  55981. + int32_t retval = 0;
  55982. +
  55983. + /* Exit from ISR if core is hibernated */
  55984. + if (core_if->hibernation_suspend == 1) {
  55985. + return retval;
  55986. + }
  55987. +#ifdef VERBOSE
  55988. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  55989. + __func__,
  55990. + DWC_READ_REG32(&global_regs->gintsts),
  55991. + DWC_READ_REG32(&global_regs->gintmsk));
  55992. +#endif
  55993. +
  55994. + if (dwc_otg_is_device_mode(core_if)) {
  55995. + DWC_SPINLOCK(pcd->lock);
  55996. +#ifdef VERBOSE
  55997. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  55998. + __func__,
  55999. + DWC_READ_REG32(&global_regs->gintsts),
  56000. + DWC_READ_REG32(&global_regs->gintmsk));
  56001. +#endif
  56002. +
  56003. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  56004. +
  56005. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  56006. + __func__, gintr_status.d32);
  56007. +
  56008. + if (gintr_status.b.sofintr) {
  56009. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  56010. + }
  56011. + if (gintr_status.b.rxstsqlvl) {
  56012. + retval |=
  56013. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  56014. + }
  56015. + if (gintr_status.b.nptxfempty) {
  56016. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  56017. + }
  56018. + if (gintr_status.b.goutnakeff) {
  56019. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  56020. + }
  56021. + if (gintr_status.b.i2cintr) {
  56022. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  56023. + }
  56024. + if (gintr_status.b.erlysuspend) {
  56025. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  56026. + }
  56027. + if (gintr_status.b.usbreset) {
  56028. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  56029. + }
  56030. + if (gintr_status.b.enumdone) {
  56031. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  56032. + }
  56033. + if (gintr_status.b.isooutdrop) {
  56034. + retval |=
  56035. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  56036. + (pcd);
  56037. + }
  56038. + if (gintr_status.b.eopframe) {
  56039. + retval |=
  56040. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  56041. + }
  56042. + if (gintr_status.b.inepint) {
  56043. + if (!core_if->multiproc_int_enable) {
  56044. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  56045. + }
  56046. + }
  56047. + if (gintr_status.b.outepintr) {
  56048. + if (!core_if->multiproc_int_enable) {
  56049. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  56050. + }
  56051. + }
  56052. + if (gintr_status.b.epmismatch) {
  56053. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  56054. + }
  56055. + if (gintr_status.b.fetsusp) {
  56056. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  56057. + }
  56058. + if (gintr_status.b.ginnakeff) {
  56059. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  56060. + }
  56061. + if (gintr_status.b.incomplisoin) {
  56062. + retval |=
  56063. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  56064. + }
  56065. + if (gintr_status.b.incomplisoout) {
  56066. + retval |=
  56067. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  56068. + }
  56069. +
  56070. + /* In MPI mode Device Endpoints interrupts are asserted
  56071. + * without setting outepintr and inepint bits set, so these
  56072. + * Interrupt handlers are called without checking these bit-fields
  56073. + */
  56074. + if (core_if->multiproc_int_enable) {
  56075. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  56076. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  56077. + }
  56078. +#ifdef VERBOSE
  56079. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  56080. + DWC_READ_REG32(&global_regs->gintsts));
  56081. +#endif
  56082. + DWC_SPINUNLOCK(pcd->lock);
  56083. + }
  56084. + return retval;
  56085. +}
  56086. +
  56087. +#endif /* DWC_HOST_ONLY */
  56088. --- /dev/null
  56089. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  56090. @@ -0,0 +1,1280 @@
  56091. + /* ==========================================================================
  56092. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  56093. + * $Revision: #21 $
  56094. + * $Date: 2012/08/10 $
  56095. + * $Change: 2047372 $
  56096. + *
  56097. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  56098. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  56099. + * otherwise expressly agreed to in writing between Synopsys and you.
  56100. + *
  56101. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  56102. + * any End User Software License Agreement or Agreement for Licensed Product
  56103. + * with Synopsys or any supplement thereto. You are permitted to use and
  56104. + * redistribute this Software in source and binary forms, with or without
  56105. + * modification, provided that redistributions of source code must retain this
  56106. + * notice. You may not view, use, disclose, copy or distribute this file or
  56107. + * any information contained herein except pursuant to this license grant from
  56108. + * Synopsys. If you do not agree with this notice, including the disclaimer
  56109. + * below, then you are not authorized to use the Software.
  56110. + *
  56111. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  56112. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  56113. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  56114. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  56115. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  56116. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  56117. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56118. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56119. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56120. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  56121. + * DAMAGE.
  56122. + * ========================================================================== */
  56123. +#ifndef DWC_HOST_ONLY
  56124. +
  56125. +/** @file
  56126. + * This file implements the Peripheral Controller Driver.
  56127. + *
  56128. + * The Peripheral Controller Driver (PCD) is responsible for
  56129. + * translating requests from the Function Driver into the appropriate
  56130. + * actions on the DWC_otg controller. It isolates the Function Driver
  56131. + * from the specifics of the controller by providing an API to the
  56132. + * Function Driver.
  56133. + *
  56134. + * The Peripheral Controller Driver for Linux will implement the
  56135. + * Gadget API, so that the existing Gadget drivers can be used.
  56136. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  56137. + *
  56138. + * The Linux Gadget API is defined in the header file
  56139. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  56140. + * defined in the structure <code>usb_ep_ops</code> and the USB
  56141. + * Controller API is defined in the structure
  56142. + * <code>usb_gadget_ops</code>.
  56143. + *
  56144. + */
  56145. +
  56146. +#include "dwc_otg_os_dep.h"
  56147. +#include "dwc_otg_pcd_if.h"
  56148. +#include "dwc_otg_pcd.h"
  56149. +#include "dwc_otg_driver.h"
  56150. +#include "dwc_otg_dbg.h"
  56151. +
  56152. +extern bool fiq_enable;
  56153. +
  56154. +static struct gadget_wrapper {
  56155. + dwc_otg_pcd_t *pcd;
  56156. +
  56157. + struct usb_gadget gadget;
  56158. + struct usb_gadget_driver *driver;
  56159. +
  56160. + struct usb_ep ep0;
  56161. + struct usb_ep in_ep[16];
  56162. + struct usb_ep out_ep[16];
  56163. +
  56164. +} *gadget_wrapper;
  56165. +
  56166. +/* Display the contents of the buffer */
  56167. +extern void dump_msg(const u8 * buf, unsigned int length);
  56168. +/**
  56169. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  56170. + * if the endpoint is not found
  56171. + */
  56172. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  56173. +{
  56174. + int i;
  56175. + if (pcd->ep0.priv == handle) {
  56176. + return &pcd->ep0;
  56177. + }
  56178. +
  56179. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  56180. + if (pcd->in_ep[i].priv == handle)
  56181. + return &pcd->in_ep[i];
  56182. + if (pcd->out_ep[i].priv == handle)
  56183. + return &pcd->out_ep[i];
  56184. + }
  56185. +
  56186. + return NULL;
  56187. +}
  56188. +
  56189. +/* USB Endpoint Operations */
  56190. +/*
  56191. + * The following sections briefly describe the behavior of the Gadget
  56192. + * API endpoint operations implemented in the DWC_otg driver
  56193. + * software. Detailed descriptions of the generic behavior of each of
  56194. + * these functions can be found in the Linux header file
  56195. + * include/linux/usb_gadget.h.
  56196. + *
  56197. + * The Gadget API provides wrapper functions for each of the function
  56198. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  56199. + * function, which then calls the underlying PCD function. The
  56200. + * following sections are named according to the wrapper
  56201. + * functions. Within each section, the corresponding DWC_otg PCD
  56202. + * function name is specified.
  56203. + *
  56204. + */
  56205. +
  56206. +/**
  56207. + * This function is called by the Gadget Driver for each EP to be
  56208. + * configured for the current configuration (SET_CONFIGURATION).
  56209. + *
  56210. + * This function initializes the dwc_otg_ep_t data structure, and then
  56211. + * calls dwc_otg_ep_activate.
  56212. + */
  56213. +static int ep_enable(struct usb_ep *usb_ep,
  56214. + const struct usb_endpoint_descriptor *ep_desc)
  56215. +{
  56216. + int retval;
  56217. +
  56218. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  56219. +
  56220. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  56221. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  56222. + return -EINVAL;
  56223. + }
  56224. + if (usb_ep == &gadget_wrapper->ep0) {
  56225. + DWC_WARN("%s, bad ep(0)\n", __func__);
  56226. + return -EINVAL;
  56227. + }
  56228. +
  56229. + /* Check FIFO size? */
  56230. + if (!ep_desc->wMaxPacketSize) {
  56231. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  56232. + return -ERANGE;
  56233. + }
  56234. +
  56235. + if (!gadget_wrapper->driver ||
  56236. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56237. + DWC_WARN("%s, bogus device state\n", __func__);
  56238. + return -ESHUTDOWN;
  56239. + }
  56240. +
  56241. + /* Delete after check - MAS */
  56242. +#if 0
  56243. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  56244. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  56245. + nat = (nat >> 11) & 0x03;
  56246. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  56247. +#endif
  56248. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  56249. + (const uint8_t *)ep_desc,
  56250. + (void *)usb_ep);
  56251. + if (retval) {
  56252. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  56253. + return -EINVAL;
  56254. + }
  56255. +
  56256. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  56257. +
  56258. + return 0;
  56259. +}
  56260. +
  56261. +/**
  56262. + * This function is called when an EP is disabled due to disconnect or
  56263. + * change in configuration. Any pending requests will terminate with a
  56264. + * status of -ESHUTDOWN.
  56265. + *
  56266. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  56267. + * and then calls dwc_otg_ep_deactivate.
  56268. + */
  56269. +static int ep_disable(struct usb_ep *usb_ep)
  56270. +{
  56271. + int retval;
  56272. +
  56273. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  56274. + if (!usb_ep) {
  56275. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  56276. + usb_ep ? usb_ep->name : NULL);
  56277. + return -EINVAL;
  56278. + }
  56279. +
  56280. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  56281. + if (retval) {
  56282. + retval = -EINVAL;
  56283. + }
  56284. +
  56285. + return retval;
  56286. +}
  56287. +
  56288. +/**
  56289. + * This function allocates a request object to use with the specified
  56290. + * endpoint.
  56291. + *
  56292. + * @param ep The endpoint to be used with with the request
  56293. + * @param gfp_flags the GFP_* flags to use.
  56294. + */
  56295. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  56296. + gfp_t gfp_flags)
  56297. +{
  56298. + struct usb_request *usb_req;
  56299. +
  56300. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  56301. + if (0 == ep) {
  56302. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  56303. + return 0;
  56304. + }
  56305. + usb_req = kmalloc(sizeof(*usb_req), gfp_flags);
  56306. + if (0 == usb_req) {
  56307. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  56308. + return 0;
  56309. + }
  56310. + memset(usb_req, 0, sizeof(*usb_req));
  56311. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  56312. +
  56313. + return usb_req;
  56314. +}
  56315. +
  56316. +/**
  56317. + * This function frees a request object.
  56318. + *
  56319. + * @param ep The endpoint associated with the request
  56320. + * @param req The request being freed
  56321. + */
  56322. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  56323. +{
  56324. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  56325. +
  56326. + if (0 == ep || 0 == req) {
  56327. + DWC_WARN("%s() %s\n", __func__,
  56328. + "Invalid ep or req argument!\n");
  56329. + return;
  56330. + }
  56331. +
  56332. + kfree(req);
  56333. +}
  56334. +
  56335. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56336. +/**
  56337. + * This function allocates an I/O buffer to be used for a transfer
  56338. + * to/from the specified endpoint.
  56339. + *
  56340. + * @param usb_ep The endpoint to be used with with the request
  56341. + * @param bytes The desired number of bytes for the buffer
  56342. + * @param dma Pointer to the buffer's DMA address; must be valid
  56343. + * @param gfp_flags the GFP_* flags to use.
  56344. + * @return address of a new buffer or null is buffer could not be allocated.
  56345. + */
  56346. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  56347. + dma_addr_t * dma, gfp_t gfp_flags)
  56348. +{
  56349. + void *buf;
  56350. + dwc_otg_pcd_t *pcd = 0;
  56351. +
  56352. + pcd = gadget_wrapper->pcd;
  56353. +
  56354. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  56355. + dma, gfp_flags);
  56356. +
  56357. + /* Check dword alignment */
  56358. + if ((bytes & 0x3UL) != 0) {
  56359. + DWC_WARN("%s() Buffer size is not a multiple of"
  56360. + "DWORD size (%d)", __func__, bytes);
  56361. + }
  56362. +
  56363. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  56364. +
  56365. + /* Check dword alignment */
  56366. + if (((int)buf & 0x3UL) != 0) {
  56367. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  56368. + __func__, buf);
  56369. + }
  56370. +
  56371. + return buf;
  56372. +}
  56373. +
  56374. +/**
  56375. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  56376. + *
  56377. + * @param usb_ep the endpoint associated with the buffer
  56378. + * @param buf address of the buffer
  56379. + * @param dma The buffer's DMA address
  56380. + * @param bytes The number of bytes of the buffer
  56381. + */
  56382. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  56383. + dma_addr_t dma, unsigned bytes)
  56384. +{
  56385. + dwc_otg_pcd_t *pcd = 0;
  56386. +
  56387. + pcd = gadget_wrapper->pcd;
  56388. +
  56389. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  56390. +
  56391. + dma_free_coherent(NULL, bytes, buf, dma);
  56392. +}
  56393. +#endif
  56394. +
  56395. +/**
  56396. + * This function is used to submit an I/O Request to an EP.
  56397. + *
  56398. + * - When the request completes the request's completion callback
  56399. + * is called to return the request to the driver.
  56400. + * - An EP, except control EPs, may have multiple requests
  56401. + * pending.
  56402. + * - Once submitted the request cannot be examined or modified.
  56403. + * - Each request is turned into one or more packets.
  56404. + * - A BULK EP can queue any amount of data; the transfer is
  56405. + * packetized.
  56406. + * - Zero length Packets are specified with the request 'zero'
  56407. + * flag.
  56408. + */
  56409. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  56410. + gfp_t gfp_flags)
  56411. +{
  56412. + dwc_otg_pcd_t *pcd;
  56413. + struct dwc_otg_pcd_ep *ep = NULL;
  56414. + int retval = 0, is_isoc_ep = 0;
  56415. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  56416. +
  56417. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  56418. + __func__, usb_ep, usb_req, gfp_flags);
  56419. +
  56420. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  56421. + DWC_WARN("bad params\n");
  56422. + return -EINVAL;
  56423. + }
  56424. +
  56425. + if (!usb_ep) {
  56426. + DWC_WARN("bad ep\n");
  56427. + return -EINVAL;
  56428. + }
  56429. +
  56430. + pcd = gadget_wrapper->pcd;
  56431. + if (!gadget_wrapper->driver ||
  56432. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56433. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  56434. + gadget_wrapper->gadget.speed);
  56435. + DWC_WARN("bogus device state\n");
  56436. + return -ESHUTDOWN;
  56437. + }
  56438. +
  56439. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  56440. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  56441. +
  56442. + usb_req->status = -EINPROGRESS;
  56443. + usb_req->actual = 0;
  56444. +
  56445. + ep = ep_from_handle(pcd, usb_ep);
  56446. + if (ep == NULL)
  56447. + is_isoc_ep = 0;
  56448. + else
  56449. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  56450. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56451. + dma_addr = usb_req->dma;
  56452. +#else
  56453. + if (GET_CORE_IF(pcd)->dma_enable) {
  56454. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  56455. + struct device *dev = NULL;
  56456. +
  56457. + if (otg_dev != NULL)
  56458. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  56459. +
  56460. + if (usb_req->length != 0 &&
  56461. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  56462. + dma_addr = dma_map_single(dev, usb_req->buf,
  56463. + usb_req->length,
  56464. + ep->dwc_ep.is_in ?
  56465. + DMA_TO_DEVICE:
  56466. + DMA_FROM_DEVICE);
  56467. + }
  56468. + }
  56469. +#endif
  56470. +
  56471. +#ifdef DWC_UTE_PER_IO
  56472. + if (is_isoc_ep == 1) {
  56473. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  56474. + usb_req->length, usb_req->zero, usb_req,
  56475. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  56476. + if (retval)
  56477. + return -EINVAL;
  56478. +
  56479. + return 0;
  56480. + }
  56481. +#endif
  56482. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  56483. + usb_req->length, usb_req->zero, usb_req,
  56484. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  56485. + if (retval) {
  56486. + return -EINVAL;
  56487. + }
  56488. +
  56489. + return 0;
  56490. +}
  56491. +
  56492. +/**
  56493. + * This function cancels an I/O request from an EP.
  56494. + */
  56495. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  56496. +{
  56497. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  56498. +
  56499. + if (!usb_ep || !usb_req) {
  56500. + DWC_WARN("bad argument\n");
  56501. + return -EINVAL;
  56502. + }
  56503. + if (!gadget_wrapper->driver ||
  56504. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56505. + DWC_WARN("bogus device state\n");
  56506. + return -ESHUTDOWN;
  56507. + }
  56508. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  56509. + return -EINVAL;
  56510. + }
  56511. +
  56512. + return 0;
  56513. +}
  56514. +
  56515. +/**
  56516. + * usb_ep_set_halt stalls an endpoint.
  56517. + *
  56518. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  56519. + * toggle.
  56520. + *
  56521. + * Both of these functions are implemented with the same underlying
  56522. + * function. The behavior depends on the value argument.
  56523. + *
  56524. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  56525. + * @param[in] value
  56526. + * - 0 means clear_halt.
  56527. + * - 1 means set_halt,
  56528. + * - 2 means clear stall lock flag.
  56529. + * - 3 means set stall lock flag.
  56530. + */
  56531. +static int ep_halt(struct usb_ep *usb_ep, int value)
  56532. +{
  56533. + int retval = 0;
  56534. +
  56535. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  56536. +
  56537. + if (!usb_ep) {
  56538. + DWC_WARN("bad ep\n");
  56539. + return -EINVAL;
  56540. + }
  56541. +
  56542. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  56543. + if (retval == -DWC_E_AGAIN) {
  56544. + return -EAGAIN;
  56545. + } else if (retval) {
  56546. + retval = -EINVAL;
  56547. + }
  56548. +
  56549. + return retval;
  56550. +}
  56551. +
  56552. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  56553. +#if 0
  56554. +/**
  56555. + * ep_wedge: sets the halt feature and ignores clear requests
  56556. + *
  56557. + * @usb_ep: the endpoint being wedged
  56558. + *
  56559. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  56560. + * requests. If the gadget driver clears the halt status, it will
  56561. + * automatically unwedge the endpoint.
  56562. + *
  56563. + * Returns zero on success, else negative errno. *
  56564. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  56565. + */
  56566. +static int ep_wedge(struct usb_ep *usb_ep)
  56567. +{
  56568. + int retval = 0;
  56569. +
  56570. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  56571. +
  56572. + if (!usb_ep) {
  56573. + DWC_WARN("bad ep\n");
  56574. + return -EINVAL;
  56575. + }
  56576. +
  56577. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  56578. + if (retval == -DWC_E_AGAIN) {
  56579. + retval = -EAGAIN;
  56580. + } else if (retval) {
  56581. + retval = -EINVAL;
  56582. + }
  56583. +
  56584. + return retval;
  56585. +}
  56586. +#endif
  56587. +
  56588. +#ifdef DWC_EN_ISOC
  56589. +/**
  56590. + * This function is used to submit an ISOC Transfer Request to an EP.
  56591. + *
  56592. + * - Every time a sync period completes the request's completion callback
  56593. + * is called to provide data to the gadget driver.
  56594. + * - Once submitted the request cannot be modified.
  56595. + * - Each request is turned into periodic data packets untill ISO
  56596. + * Transfer is stopped..
  56597. + */
  56598. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  56599. + gfp_t gfp_flags)
  56600. +{
  56601. + int retval = 0;
  56602. +
  56603. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  56604. + DWC_WARN("bad params\n");
  56605. + return -EINVAL;
  56606. + }
  56607. +
  56608. + if (!usb_ep) {
  56609. + DWC_PRINTF("bad params\n");
  56610. + return -EINVAL;
  56611. + }
  56612. +
  56613. + req->status = -EINPROGRESS;
  56614. +
  56615. + retval =
  56616. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  56617. + req->buf1, req->dma0, req->dma1,
  56618. + req->sync_frame, req->data_pattern_frame,
  56619. + req->data_per_frame,
  56620. + req->
  56621. + flags & USB_REQ_ISO_ASAP ? -1 :
  56622. + req->start_frame, req->buf_proc_intrvl,
  56623. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  56624. +
  56625. + if (retval) {
  56626. + return -EINVAL;
  56627. + }
  56628. +
  56629. + return retval;
  56630. +}
  56631. +
  56632. +/**
  56633. + * This function stops ISO EP Periodic Data Transfer.
  56634. + */
  56635. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  56636. +{
  56637. + int retval = 0;
  56638. + if (!usb_ep) {
  56639. + DWC_WARN("bad ep\n");
  56640. + }
  56641. +
  56642. + if (!gadget_wrapper->driver ||
  56643. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56644. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  56645. + gadget_wrapper->gadget.speed);
  56646. + DWC_WARN("bogus device state\n");
  56647. + }
  56648. +
  56649. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  56650. + if (retval) {
  56651. + retval = -EINVAL;
  56652. + }
  56653. +
  56654. + return retval;
  56655. +}
  56656. +
  56657. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  56658. + int packets, gfp_t gfp_flags)
  56659. +{
  56660. + struct usb_iso_request *pReq = NULL;
  56661. + uint32_t req_size;
  56662. +
  56663. + req_size = sizeof(struct usb_iso_request);
  56664. + req_size +=
  56665. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  56666. +
  56667. + pReq = kmalloc(req_size, gfp_flags);
  56668. + if (!pReq) {
  56669. + DWC_WARN("Can't allocate Iso Request\n");
  56670. + return 0;
  56671. + }
  56672. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  56673. +
  56674. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  56675. +
  56676. + return pReq;
  56677. +}
  56678. +
  56679. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  56680. +{
  56681. + kfree(req);
  56682. +}
  56683. +
  56684. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  56685. + .ep_ops = {
  56686. + .enable = ep_enable,
  56687. + .disable = ep_disable,
  56688. +
  56689. + .alloc_request = dwc_otg_pcd_alloc_request,
  56690. + .free_request = dwc_otg_pcd_free_request,
  56691. +
  56692. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56693. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  56694. + .free_buffer = dwc_otg_pcd_free_buffer,
  56695. +#endif
  56696. +
  56697. + .queue = ep_queue,
  56698. + .dequeue = ep_dequeue,
  56699. +
  56700. + .set_halt = ep_halt,
  56701. + .fifo_status = 0,
  56702. + .fifo_flush = 0,
  56703. + },
  56704. + .iso_ep_start = iso_ep_start,
  56705. + .iso_ep_stop = iso_ep_stop,
  56706. + .alloc_iso_request = alloc_iso_request,
  56707. + .free_iso_request = free_iso_request,
  56708. +};
  56709. +
  56710. +#else
  56711. +
  56712. + int (*enable) (struct usb_ep *ep,
  56713. + const struct usb_endpoint_descriptor *desc);
  56714. + int (*disable) (struct usb_ep *ep);
  56715. +
  56716. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  56717. + gfp_t gfp_flags);
  56718. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  56719. +
  56720. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  56721. + gfp_t gfp_flags);
  56722. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  56723. +
  56724. + int (*set_halt) (struct usb_ep *ep, int value);
  56725. + int (*set_wedge) (struct usb_ep *ep);
  56726. +
  56727. + int (*fifo_status) (struct usb_ep *ep);
  56728. + void (*fifo_flush) (struct usb_ep *ep);
  56729. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  56730. + .enable = ep_enable,
  56731. + .disable = ep_disable,
  56732. +
  56733. + .alloc_request = dwc_otg_pcd_alloc_request,
  56734. + .free_request = dwc_otg_pcd_free_request,
  56735. +
  56736. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56737. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  56738. + .free_buffer = dwc_otg_pcd_free_buffer,
  56739. +#else
  56740. + /* .set_wedge = ep_wedge, */
  56741. + .set_wedge = NULL, /* uses set_halt instead */
  56742. +#endif
  56743. +
  56744. + .queue = ep_queue,
  56745. + .dequeue = ep_dequeue,
  56746. +
  56747. + .set_halt = ep_halt,
  56748. + .fifo_status = 0,
  56749. + .fifo_flush = 0,
  56750. +
  56751. +};
  56752. +
  56753. +#endif /* _EN_ISOC_ */
  56754. +/* Gadget Operations */
  56755. +/**
  56756. + * The following gadget operations will be implemented in the DWC_otg
  56757. + * PCD. Functions in the API that are not described below are not
  56758. + * implemented.
  56759. + *
  56760. + * The Gadget API provides wrapper functions for each of the function
  56761. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  56762. + * wrapper function, which then calls the underlying PCD function. The
  56763. + * following sections are named according to the wrapper functions
  56764. + * (except for ioctl, which doesn't have a wrapper function). Within
  56765. + * each section, the corresponding DWC_otg PCD function name is
  56766. + * specified.
  56767. + *
  56768. + */
  56769. +
  56770. +/**
  56771. + *Gets the USB Frame number of the last SOF.
  56772. + */
  56773. +static int get_frame_number(struct usb_gadget *gadget)
  56774. +{
  56775. + struct gadget_wrapper *d;
  56776. +
  56777. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  56778. +
  56779. + if (gadget == 0) {
  56780. + return -ENODEV;
  56781. + }
  56782. +
  56783. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56784. + return dwc_otg_pcd_get_frame_number(d->pcd);
  56785. +}
  56786. +
  56787. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56788. +static int test_lpm_enabled(struct usb_gadget *gadget)
  56789. +{
  56790. + struct gadget_wrapper *d;
  56791. +
  56792. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56793. +
  56794. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  56795. +}
  56796. +#endif
  56797. +
  56798. +/**
  56799. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  56800. + * session is in progress. If a session is already in progress, but
  56801. + * the device is suspended, remote wakeup signaling is started.
  56802. + *
  56803. + */
  56804. +static int wakeup(struct usb_gadget *gadget)
  56805. +{
  56806. + struct gadget_wrapper *d;
  56807. +
  56808. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  56809. +
  56810. + if (gadget == 0) {
  56811. + return -ENODEV;
  56812. + } else {
  56813. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56814. + }
  56815. + dwc_otg_pcd_wakeup(d->pcd);
  56816. + return 0;
  56817. +}
  56818. +
  56819. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  56820. + .get_frame = get_frame_number,
  56821. + .wakeup = wakeup,
  56822. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56823. + .lpm_support = test_lpm_enabled,
  56824. +#endif
  56825. + // current versions must always be self-powered
  56826. +};
  56827. +
  56828. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  56829. +{
  56830. + int retval = -DWC_E_NOT_SUPPORTED;
  56831. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  56832. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  56833. + (struct usb_ctrlrequest
  56834. + *)bytes);
  56835. + }
  56836. +
  56837. + if (retval == -ENOTSUPP) {
  56838. + retval = -DWC_E_NOT_SUPPORTED;
  56839. + } else if (retval < 0) {
  56840. + retval = -DWC_E_INVALID;
  56841. + }
  56842. +
  56843. + return retval;
  56844. +}
  56845. +
  56846. +#ifdef DWC_EN_ISOC
  56847. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  56848. + void *req_handle, int proc_buf_num)
  56849. +{
  56850. + int i, packet_count;
  56851. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  56852. + struct usb_iso_request *iso_req = req_handle;
  56853. +
  56854. + if (proc_buf_num) {
  56855. + iso_packet = iso_req->iso_packet_desc1;
  56856. + } else {
  56857. + iso_packet = iso_req->iso_packet_desc0;
  56858. + }
  56859. + packet_count =
  56860. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  56861. + for (i = 0; i < packet_count; ++i) {
  56862. + int status;
  56863. + int actual;
  56864. + int offset;
  56865. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  56866. + i, &status, &actual, &offset);
  56867. + switch (status) {
  56868. + case -DWC_E_NO_DATA:
  56869. + status = -ENODATA;
  56870. + break;
  56871. + default:
  56872. + if (status) {
  56873. + DWC_PRINTF("unknown status in isoc packet\n");
  56874. + }
  56875. +
  56876. + }
  56877. + iso_packet[i].status = status;
  56878. + iso_packet[i].offset = offset;
  56879. + iso_packet[i].actual_length = actual;
  56880. + }
  56881. +
  56882. + iso_req->status = 0;
  56883. + iso_req->process_buffer(ep_handle, iso_req);
  56884. +
  56885. + return 0;
  56886. +}
  56887. +#endif /* DWC_EN_ISOC */
  56888. +
  56889. +#ifdef DWC_UTE_PER_IO
  56890. +/**
  56891. + * Copy the contents of the extended request to the Linux usb_request's
  56892. + * extended part and call the gadget's completion.
  56893. + *
  56894. + * @param pcd Pointer to the pcd structure
  56895. + * @param ep_handle Void pointer to the usb_ep structure
  56896. + * @param req_handle Void pointer to the usb_request structure
  56897. + * @param status Request status returned from the portable logic
  56898. + * @param ereq_port Void pointer to the extended request structure
  56899. + * created in the the portable part that contains the
  56900. + * results of the processed iso packets.
  56901. + */
  56902. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  56903. + void *req_handle, int32_t status, void *ereq_port)
  56904. +{
  56905. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  56906. + struct dwc_iso_xreq_port *ereqport = NULL;
  56907. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  56908. + int i;
  56909. + struct usb_request *req;
  56910. + //struct dwc_ute_iso_packet_descriptor *
  56911. + //int status = 0;
  56912. +
  56913. + req = (struct usb_request *)req_handle;
  56914. + ereqorg = &req->ext_req;
  56915. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  56916. + desc_org = ereqorg->per_io_frame_descs;
  56917. +
  56918. + if (req && req->complete) {
  56919. + /* Copy the request data from the portable logic to our request */
  56920. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  56921. + desc_org[i].actual_length =
  56922. + ereqport->per_io_frame_descs[i].actual_length;
  56923. + desc_org[i].status =
  56924. + ereqport->per_io_frame_descs[i].status;
  56925. + }
  56926. +
  56927. + switch (status) {
  56928. + case -DWC_E_SHUTDOWN:
  56929. + req->status = -ESHUTDOWN;
  56930. + break;
  56931. + case -DWC_E_RESTART:
  56932. + req->status = -ECONNRESET;
  56933. + break;
  56934. + case -DWC_E_INVALID:
  56935. + req->status = -EINVAL;
  56936. + break;
  56937. + case -DWC_E_TIMEOUT:
  56938. + req->status = -ETIMEDOUT;
  56939. + break;
  56940. + default:
  56941. + req->status = status;
  56942. + }
  56943. +
  56944. + /* And call the gadget's completion */
  56945. + req->complete(ep_handle, req);
  56946. + }
  56947. +
  56948. + return 0;
  56949. +}
  56950. +#endif /* DWC_UTE_PER_IO */
  56951. +
  56952. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  56953. + void *req_handle, int32_t status, uint32_t actual)
  56954. +{
  56955. + struct usb_request *req = (struct usb_request *)req_handle;
  56956. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  56957. + struct dwc_otg_pcd_ep *ep = NULL;
  56958. +#endif
  56959. +
  56960. + if (req && req->complete) {
  56961. + switch (status) {
  56962. + case -DWC_E_SHUTDOWN:
  56963. + req->status = -ESHUTDOWN;
  56964. + break;
  56965. + case -DWC_E_RESTART:
  56966. + req->status = -ECONNRESET;
  56967. + break;
  56968. + case -DWC_E_INVALID:
  56969. + req->status = -EINVAL;
  56970. + break;
  56971. + case -DWC_E_TIMEOUT:
  56972. + req->status = -ETIMEDOUT;
  56973. + break;
  56974. + default:
  56975. + req->status = status;
  56976. +
  56977. + }
  56978. +
  56979. + req->actual = actual;
  56980. + DWC_SPINUNLOCK(pcd->lock);
  56981. + req->complete(ep_handle, req);
  56982. + DWC_SPINLOCK(pcd->lock);
  56983. + }
  56984. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  56985. + ep = ep_from_handle(pcd, ep_handle);
  56986. + if (GET_CORE_IF(pcd)->dma_enable) {
  56987. + if (req->length != 0) {
  56988. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  56989. + struct device *dev = NULL;
  56990. +
  56991. + if (otg_dev != NULL)
  56992. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  56993. +
  56994. + dma_unmap_single(dev, req->dma, req->length,
  56995. + ep->dwc_ep.is_in ?
  56996. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  56997. + }
  56998. + }
  56999. +#endif
  57000. +
  57001. + return 0;
  57002. +}
  57003. +
  57004. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  57005. +{
  57006. + gadget_wrapper->gadget.speed = speed;
  57007. + return 0;
  57008. +}
  57009. +
  57010. +static int _disconnect(dwc_otg_pcd_t * pcd)
  57011. +{
  57012. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  57013. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  57014. + }
  57015. + return 0;
  57016. +}
  57017. +
  57018. +static int _resume(dwc_otg_pcd_t * pcd)
  57019. +{
  57020. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  57021. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  57022. + }
  57023. +
  57024. + return 0;
  57025. +}
  57026. +
  57027. +static int _suspend(dwc_otg_pcd_t * pcd)
  57028. +{
  57029. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  57030. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  57031. + }
  57032. + return 0;
  57033. +}
  57034. +
  57035. +/**
  57036. + * This function updates the otg values in the gadget structure.
  57037. + */
  57038. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  57039. +{
  57040. +
  57041. + if (!gadget_wrapper->gadget.is_otg)
  57042. + return 0;
  57043. +
  57044. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  57045. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  57046. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  57047. + return 0;
  57048. +}
  57049. +
  57050. +static int _reset(dwc_otg_pcd_t * pcd)
  57051. +{
  57052. + return 0;
  57053. +}
  57054. +
  57055. +#ifdef DWC_UTE_CFI
  57056. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  57057. +{
  57058. + int retval = -DWC_E_INVALID;
  57059. + if (gadget_wrapper->driver->cfi_feature_setup) {
  57060. + retval =
  57061. + gadget_wrapper->driver->
  57062. + cfi_feature_setup(&gadget_wrapper->gadget,
  57063. + (struct cfi_usb_ctrlrequest *)cfi_req);
  57064. + }
  57065. +
  57066. + return retval;
  57067. +}
  57068. +#endif
  57069. +
  57070. +static const struct dwc_otg_pcd_function_ops fops = {
  57071. + .complete = _complete,
  57072. +#ifdef DWC_EN_ISOC
  57073. + .isoc_complete = _isoc_complete,
  57074. +#endif
  57075. + .setup = _setup,
  57076. + .disconnect = _disconnect,
  57077. + .connect = _connect,
  57078. + .resume = _resume,
  57079. + .suspend = _suspend,
  57080. + .hnp_changed = _hnp_changed,
  57081. + .reset = _reset,
  57082. +#ifdef DWC_UTE_CFI
  57083. + .cfi_setup = _cfi_setup,
  57084. +#endif
  57085. +#ifdef DWC_UTE_PER_IO
  57086. + .xisoc_complete = _xisoc_complete,
  57087. +#endif
  57088. +};
  57089. +
  57090. +/**
  57091. + * This function is the top level PCD interrupt handler.
  57092. + */
  57093. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  57094. +{
  57095. + dwc_otg_pcd_t *pcd = dev;
  57096. + int32_t retval = IRQ_NONE;
  57097. +
  57098. + retval = dwc_otg_pcd_handle_intr(pcd);
  57099. + if (retval != 0) {
  57100. + S3C2410X_CLEAR_EINTPEND();
  57101. + }
  57102. + return IRQ_RETVAL(retval);
  57103. +}
  57104. +
  57105. +/**
  57106. + * This function initialized the usb_ep structures to there default
  57107. + * state.
  57108. + *
  57109. + * @param d Pointer on gadget_wrapper.
  57110. + */
  57111. +void gadget_add_eps(struct gadget_wrapper *d)
  57112. +{
  57113. + static const char *names[] = {
  57114. +
  57115. + "ep0",
  57116. + "ep1in",
  57117. + "ep2in",
  57118. + "ep3in",
  57119. + "ep4in",
  57120. + "ep5in",
  57121. + "ep6in",
  57122. + "ep7in",
  57123. + "ep8in",
  57124. + "ep9in",
  57125. + "ep10in",
  57126. + "ep11in",
  57127. + "ep12in",
  57128. + "ep13in",
  57129. + "ep14in",
  57130. + "ep15in",
  57131. + "ep1out",
  57132. + "ep2out",
  57133. + "ep3out",
  57134. + "ep4out",
  57135. + "ep5out",
  57136. + "ep6out",
  57137. + "ep7out",
  57138. + "ep8out",
  57139. + "ep9out",
  57140. + "ep10out",
  57141. + "ep11out",
  57142. + "ep12out",
  57143. + "ep13out",
  57144. + "ep14out",
  57145. + "ep15out"
  57146. + };
  57147. +
  57148. + int i;
  57149. + struct usb_ep *ep;
  57150. + int8_t dev_endpoints;
  57151. +
  57152. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  57153. +
  57154. + INIT_LIST_HEAD(&d->gadget.ep_list);
  57155. + d->gadget.ep0 = &d->ep0;
  57156. + d->gadget.speed = USB_SPEED_UNKNOWN;
  57157. +
  57158. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  57159. +
  57160. + /**
  57161. + * Initialize the EP0 structure.
  57162. + */
  57163. + ep = &d->ep0;
  57164. +
  57165. + /* Init the usb_ep structure. */
  57166. + ep->name = names[0];
  57167. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57168. +
  57169. + /**
  57170. + * @todo NGS: What should the max packet size be set to
  57171. + * here? Before EP type is set?
  57172. + */
  57173. + ep->maxpacket = MAX_PACKET_SIZE;
  57174. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  57175. +
  57176. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57177. +
  57178. + /**
  57179. + * Initialize the EP structures.
  57180. + */
  57181. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  57182. +
  57183. + for (i = 0; i < dev_endpoints; i++) {
  57184. + ep = &d->in_ep[i];
  57185. +
  57186. + /* Init the usb_ep structure. */
  57187. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  57188. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57189. +
  57190. + /**
  57191. + * @todo NGS: What should the max packet size be set to
  57192. + * here? Before EP type is set?
  57193. + */
  57194. + ep->maxpacket = MAX_PACKET_SIZE;
  57195. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57196. + }
  57197. +
  57198. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  57199. +
  57200. + for (i = 0; i < dev_endpoints; i++) {
  57201. + ep = &d->out_ep[i];
  57202. +
  57203. + /* Init the usb_ep structure. */
  57204. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  57205. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57206. +
  57207. + /**
  57208. + * @todo NGS: What should the max packet size be set to
  57209. + * here? Before EP type is set?
  57210. + */
  57211. + ep->maxpacket = MAX_PACKET_SIZE;
  57212. +
  57213. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57214. + }
  57215. +
  57216. + /* remove ep0 from the list. There is a ep0 pointer. */
  57217. + list_del_init(&d->ep0.ep_list);
  57218. +
  57219. + d->ep0.maxpacket = MAX_EP0_SIZE;
  57220. +}
  57221. +
  57222. +/**
  57223. + * This function releases the Gadget device.
  57224. + * required by device_unregister().
  57225. + *
  57226. + * @todo Should this do something? Should it free the PCD?
  57227. + */
  57228. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  57229. +{
  57230. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  57231. +}
  57232. +
  57233. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  57234. +{
  57235. + static char pcd_name[] = "dwc_otg_pcd";
  57236. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57237. + struct gadget_wrapper *d;
  57238. + int retval;
  57239. +
  57240. + d = DWC_ALLOC(sizeof(*d));
  57241. + if (d == NULL) {
  57242. + return NULL;
  57243. + }
  57244. +
  57245. + memset(d, 0, sizeof(*d));
  57246. +
  57247. + d->gadget.name = pcd_name;
  57248. + d->pcd = otg_dev->pcd;
  57249. +
  57250. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  57251. + strcpy(d->gadget.dev.bus_id, "gadget");
  57252. +#else
  57253. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  57254. +#endif
  57255. +
  57256. + d->gadget.dev.parent = &_dev->dev;
  57257. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  57258. + d->gadget.ops = &dwc_otg_pcd_ops;
  57259. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  57260. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  57261. +
  57262. + d->driver = 0;
  57263. + /* Register the gadget device */
  57264. + retval = device_register(&d->gadget.dev);
  57265. + if (retval != 0) {
  57266. + DWC_ERROR("device_register failed\n");
  57267. + DWC_FREE(d);
  57268. + return NULL;
  57269. + }
  57270. +
  57271. + return d;
  57272. +}
  57273. +
  57274. +static void free_wrapper(struct gadget_wrapper *d)
  57275. +{
  57276. + if (d->driver) {
  57277. + /* should have been done already by driver model core */
  57278. + DWC_WARN("driver '%s' is still registered\n",
  57279. + d->driver->driver.name);
  57280. +#ifdef CONFIG_USB_GADGET
  57281. + usb_gadget_unregister_driver(d->driver);
  57282. +#endif
  57283. + }
  57284. +
  57285. + device_unregister(&d->gadget.dev);
  57286. + DWC_FREE(d);
  57287. +}
  57288. +
  57289. +/**
  57290. + * This function initialized the PCD portion of the driver.
  57291. + *
  57292. + */
  57293. +int pcd_init(dwc_bus_dev_t *_dev)
  57294. +{
  57295. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57296. + int retval = 0;
  57297. +
  57298. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  57299. +
  57300. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev->core_if);
  57301. +
  57302. + if (!otg_dev->pcd) {
  57303. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  57304. + return -ENOMEM;
  57305. + }
  57306. +
  57307. + otg_dev->pcd->otg_dev = otg_dev;
  57308. + gadget_wrapper = alloc_wrapper(_dev);
  57309. +
  57310. + /*
  57311. + * Initialize EP structures
  57312. + */
  57313. + gadget_add_eps(gadget_wrapper);
  57314. + /*
  57315. + * Setup interupt handler
  57316. + */
  57317. +#ifdef PLATFORM_INTERFACE
  57318. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  57319. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  57320. + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
  57321. + IRQF_SHARED, gadget_wrapper->gadget.name,
  57322. + otg_dev->pcd);
  57323. + if (retval != 0) {
  57324. + DWC_ERROR("request of irq%d failed\n",
  57325. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  57326. + free_wrapper(gadget_wrapper);
  57327. + return -EBUSY;
  57328. + }
  57329. +#else
  57330. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  57331. + _dev->irq);
  57332. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  57333. + IRQF_SHARED | IRQF_DISABLED,
  57334. + gadget_wrapper->gadget.name, otg_dev->pcd);
  57335. + if (retval != 0) {
  57336. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  57337. + free_wrapper(gadget_wrapper);
  57338. + return -EBUSY;
  57339. + }
  57340. +#endif
  57341. +
  57342. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  57343. +
  57344. + return retval;
  57345. +}
  57346. +
  57347. +/**
  57348. + * Cleanup the PCD.
  57349. + */
  57350. +void pcd_remove(dwc_bus_dev_t *_dev)
  57351. +{
  57352. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57353. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  57354. +
  57355. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  57356. +
  57357. + /*
  57358. + * Free the IRQ
  57359. + */
  57360. +#ifdef PLATFORM_INTERFACE
  57361. + free_irq(platform_get_irq(_dev, 0), pcd);
  57362. +#else
  57363. + free_irq(_dev->irq, pcd);
  57364. +#endif
  57365. + dwc_otg_pcd_remove(otg_dev->pcd);
  57366. + free_wrapper(gadget_wrapper);
  57367. + otg_dev->pcd = 0;
  57368. +}
  57369. +
  57370. +#endif /* DWC_HOST_ONLY */
  57371. --- /dev/null
  57372. +++ b/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  57373. @@ -0,0 +1,2550 @@
  57374. +/* ==========================================================================
  57375. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  57376. + * $Revision: #98 $
  57377. + * $Date: 2012/08/10 $
  57378. + * $Change: 2047372 $
  57379. + *
  57380. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  57381. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  57382. + * otherwise expressly agreed to in writing between Synopsys and you.
  57383. + *
  57384. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  57385. + * any End User Software License Agreement or Agreement for Licensed Product
  57386. + * with Synopsys or any supplement thereto. You are permitted to use and
  57387. + * redistribute this Software in source and binary forms, with or without
  57388. + * modification, provided that redistributions of source code must retain this
  57389. + * notice. You may not view, use, disclose, copy or distribute this file or
  57390. + * any information contained herein except pursuant to this license grant from
  57391. + * Synopsys. If you do not agree with this notice, including the disclaimer
  57392. + * below, then you are not authorized to use the Software.
  57393. + *
  57394. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  57395. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57396. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57397. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  57398. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57399. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57400. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57401. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57402. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57403. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57404. + * DAMAGE.
  57405. + * ========================================================================== */
  57406. +
  57407. +#ifndef __DWC_OTG_REGS_H__
  57408. +#define __DWC_OTG_REGS_H__
  57409. +
  57410. +#include "dwc_otg_core_if.h"
  57411. +
  57412. +/**
  57413. + * @file
  57414. + *
  57415. + * This file contains the data structures for accessing the DWC_otg core registers.
  57416. + *
  57417. + * The application interfaces with the HS OTG core by reading from and
  57418. + * writing to the Control and Status Register (CSR) space through the
  57419. + * AHB Slave interface. These registers are 32 bits wide, and the
  57420. + * addresses are 32-bit-block aligned.
  57421. + * CSRs are classified as follows:
  57422. + * - Core Global Registers
  57423. + * - Device Mode Registers
  57424. + * - Device Global Registers
  57425. + * - Device Endpoint Specific Registers
  57426. + * - Host Mode Registers
  57427. + * - Host Global Registers
  57428. + * - Host Port CSRs
  57429. + * - Host Channel Specific Registers
  57430. + *
  57431. + * Only the Core Global registers can be accessed in both Device and
  57432. + * Host modes. When the HS OTG core is operating in one mode, either
  57433. + * Device or Host, the application must not access registers from the
  57434. + * other mode. When the core switches from one mode to another, the
  57435. + * registers in the new mode of operation must be reprogrammed as they
  57436. + * would be after a power-on reset.
  57437. + */
  57438. +
  57439. +/****************************************************************************/
  57440. +/** DWC_otg Core registers .
  57441. + * The dwc_otg_core_global_regs structure defines the size
  57442. + * and relative field offsets for the Core Global registers.
  57443. + */
  57444. +typedef struct dwc_otg_core_global_regs {
  57445. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  57446. + volatile uint32_t gotgctl;
  57447. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  57448. + volatile uint32_t gotgint;
  57449. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  57450. + volatile uint32_t gahbcfg;
  57451. +
  57452. +#define DWC_GLBINTRMASK 0x0001
  57453. +#define DWC_DMAENABLE 0x0020
  57454. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  57455. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  57456. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  57457. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  57458. +
  57459. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  57460. + volatile uint32_t gusbcfg;
  57461. + /**Core Reset Register. <i>Offset: 010h</i> */
  57462. + volatile uint32_t grstctl;
  57463. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  57464. + volatile uint32_t gintsts;
  57465. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  57466. + volatile uint32_t gintmsk;
  57467. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  57468. + volatile uint32_t grxstsr;
  57469. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  57470. + volatile uint32_t grxstsp;
  57471. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  57472. + volatile uint32_t grxfsiz;
  57473. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  57474. + volatile uint32_t gnptxfsiz;
  57475. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  57476. + * Only). <i>Offset: 02Ch</i> */
  57477. + volatile uint32_t gnptxsts;
  57478. + /**I2C Access Register. <i>Offset: 030h</i> */
  57479. + volatile uint32_t gi2cctl;
  57480. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  57481. + volatile uint32_t gpvndctl;
  57482. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  57483. + volatile uint32_t ggpio;
  57484. + /**User ID Register. <i>Offset: 03Ch</i> */
  57485. + volatile uint32_t guid;
  57486. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  57487. + volatile uint32_t gsnpsid;
  57488. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  57489. + volatile uint32_t ghwcfg1;
  57490. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  57491. + volatile uint32_t ghwcfg2;
  57492. +#define DWC_SLAVE_ONLY_ARCH 0
  57493. +#define DWC_EXT_DMA_ARCH 1
  57494. +#define DWC_INT_DMA_ARCH 2
  57495. +
  57496. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  57497. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  57498. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  57499. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  57500. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  57501. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  57502. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  57503. +
  57504. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  57505. + volatile uint32_t ghwcfg3;
  57506. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  57507. + volatile uint32_t ghwcfg4;
  57508. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  57509. + volatile uint32_t glpmcfg;
  57510. + /** Global PowerDn Register <i>Offset: 058h</i> */
  57511. + volatile uint32_t gpwrdn;
  57512. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  57513. + volatile uint32_t gdfifocfg;
  57514. + /** ADP Control Register <i>Offset: 060h</i> */
  57515. + volatile uint32_t adpctl;
  57516. + /** Reserved <i>Offset: 064h-0FFh</i> */
  57517. + volatile uint32_t reserved39[39];
  57518. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  57519. + volatile uint32_t hptxfsiz;
  57520. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  57521. + otherwise Device Transmit FIFO#n Register.
  57522. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  57523. + volatile uint32_t dtxfsiz[15];
  57524. +} dwc_otg_core_global_regs_t;
  57525. +
  57526. +/**
  57527. + * This union represents the bit fields of the Core OTG Control
  57528. + * and Status Register (GOTGCTL). Set the bits using the bit
  57529. + * fields then write the <i>d32</i> value to the register.
  57530. + */
  57531. +typedef union gotgctl_data {
  57532. + /** raw register data */
  57533. + uint32_t d32;
  57534. + /** register bits */
  57535. + struct {
  57536. + unsigned sesreqscs:1;
  57537. + unsigned sesreq:1;
  57538. + unsigned vbvalidoven:1;
  57539. + unsigned vbvalidovval:1;
  57540. + unsigned avalidoven:1;
  57541. + unsigned avalidovval:1;
  57542. + unsigned bvalidoven:1;
  57543. + unsigned bvalidovval:1;
  57544. + unsigned hstnegscs:1;
  57545. + unsigned hnpreq:1;
  57546. + unsigned hstsethnpen:1;
  57547. + unsigned devhnpen:1;
  57548. + unsigned reserved12_15:4;
  57549. + unsigned conidsts:1;
  57550. + unsigned dbnctime:1;
  57551. + unsigned asesvld:1;
  57552. + unsigned bsesvld:1;
  57553. + unsigned otgver:1;
  57554. + unsigned reserved1:1;
  57555. + unsigned multvalidbc:5;
  57556. + unsigned chirpen:1;
  57557. + unsigned reserved28_31:4;
  57558. + } b;
  57559. +} gotgctl_data_t;
  57560. +
  57561. +/**
  57562. + * This union represents the bit fields of the Core OTG Interrupt Register
  57563. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  57564. + * value to the register.
  57565. + */
  57566. +typedef union gotgint_data {
  57567. + /** raw register data */
  57568. + uint32_t d32;
  57569. + /** register bits */
  57570. + struct {
  57571. + /** Current Mode */
  57572. + unsigned reserved0_1:2;
  57573. +
  57574. + /** Session End Detected */
  57575. + unsigned sesenddet:1;
  57576. +
  57577. + unsigned reserved3_7:5;
  57578. +
  57579. + /** Session Request Success Status Change */
  57580. + unsigned sesreqsucstschng:1;
  57581. + /** Host Negotiation Success Status Change */
  57582. + unsigned hstnegsucstschng:1;
  57583. +
  57584. + unsigned reserved10_16:7;
  57585. +
  57586. + /** Host Negotiation Detected */
  57587. + unsigned hstnegdet:1;
  57588. + /** A-Device Timeout Change */
  57589. + unsigned adevtoutchng:1;
  57590. + /** Debounce Done */
  57591. + unsigned debdone:1;
  57592. + /** Multi-Valued input changed */
  57593. + unsigned mvic:1;
  57594. +
  57595. + unsigned reserved31_21:11;
  57596. +
  57597. + } b;
  57598. +} gotgint_data_t;
  57599. +
  57600. +/**
  57601. + * This union represents the bit fields of the Core AHB Configuration
  57602. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  57603. + * write the <i>d32</i> value to the register.
  57604. + */
  57605. +typedef union gahbcfg_data {
  57606. + /** raw register data */
  57607. + uint32_t d32;
  57608. + /** register bits */
  57609. + struct {
  57610. + unsigned glblintrmsk:1;
  57611. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  57612. +
  57613. + unsigned hburstlen:4;
  57614. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  57615. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  57616. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  57617. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  57618. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  57619. +
  57620. + unsigned dmaenable:1;
  57621. +#define DWC_GAHBCFG_DMAENABLE 1
  57622. + unsigned reserved:1;
  57623. + unsigned nptxfemplvl_txfemplvl:1;
  57624. + unsigned ptxfemplvl:1;
  57625. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  57626. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  57627. + unsigned reserved9_20:12;
  57628. + unsigned remmemsupp:1;
  57629. + unsigned notialldmawrit:1;
  57630. + unsigned ahbsingle:1;
  57631. + unsigned reserved24_31:8;
  57632. + } b;
  57633. +} gahbcfg_data_t;
  57634. +
  57635. +/**
  57636. + * This union represents the bit fields of the Core USB Configuration
  57637. + * Register (GUSBCFG). Set the bits using the bit fields then write
  57638. + * the <i>d32</i> value to the register.
  57639. + */
  57640. +typedef union gusbcfg_data {
  57641. + /** raw register data */
  57642. + uint32_t d32;
  57643. + /** register bits */
  57644. + struct {
  57645. + unsigned toutcal:3;
  57646. + unsigned phyif:1;
  57647. + unsigned ulpi_utmi_sel:1;
  57648. + unsigned fsintf:1;
  57649. + unsigned physel:1;
  57650. + unsigned ddrsel:1;
  57651. + unsigned srpcap:1;
  57652. + unsigned hnpcap:1;
  57653. + unsigned usbtrdtim:4;
  57654. + unsigned reserved1:1;
  57655. + unsigned phylpwrclksel:1;
  57656. + unsigned otgutmifssel:1;
  57657. + unsigned ulpi_fsls:1;
  57658. + unsigned ulpi_auto_res:1;
  57659. + unsigned ulpi_clk_sus_m:1;
  57660. + unsigned ulpi_ext_vbus_drv:1;
  57661. + unsigned ulpi_int_vbus_indicator:1;
  57662. + unsigned term_sel_dl_pulse:1;
  57663. + unsigned indicator_complement:1;
  57664. + unsigned indicator_pass_through:1;
  57665. + unsigned ulpi_int_prot_dis:1;
  57666. + unsigned ic_usb_cap:1;
  57667. + unsigned ic_traffic_pull_remove:1;
  57668. + unsigned tx_end_delay:1;
  57669. + unsigned force_host_mode:1;
  57670. + unsigned force_dev_mode:1;
  57671. + unsigned reserved31:1;
  57672. + } b;
  57673. +} gusbcfg_data_t;
  57674. +
  57675. +/**
  57676. + * This union represents the bit fields of the Core Reset Register
  57677. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  57678. + * <i>d32</i> value to the register.
  57679. + */
  57680. +typedef union grstctl_data {
  57681. + /** raw register data */
  57682. + uint32_t d32;
  57683. + /** register bits */
  57684. + struct {
  57685. + /** Core Soft Reset (CSftRst) (Device and Host)
  57686. + *
  57687. + * The application can flush the control logic in the
  57688. + * entire core using this bit. This bit resets the
  57689. + * pipelines in the AHB Clock domain as well as the
  57690. + * PHY Clock domain.
  57691. + *
  57692. + * The state machines are reset to an IDLE state, the
  57693. + * control bits in the CSRs are cleared, all the
  57694. + * transmit FIFOs and the receive FIFO are flushed.
  57695. + *
  57696. + * The status mask bits that control the generation of
  57697. + * the interrupt, are cleared, to clear the
  57698. + * interrupt. The interrupt status bits are not
  57699. + * cleared, so the application can get the status of
  57700. + * any events that occurred in the core after it has
  57701. + * set this bit.
  57702. + *
  57703. + * Any transactions on the AHB are terminated as soon
  57704. + * as possible following the protocol. Any
  57705. + * transactions on the USB are terminated immediately.
  57706. + *
  57707. + * The configuration settings in the CSRs are
  57708. + * unchanged, so the software doesn't have to
  57709. + * reprogram these registers (Device
  57710. + * Configuration/Host Configuration/Core System
  57711. + * Configuration/Core PHY Configuration).
  57712. + *
  57713. + * The application can write to this bit, any time it
  57714. + * wants to reset the core. This is a self clearing
  57715. + * bit and the core clears this bit after all the
  57716. + * necessary logic is reset in the core, which may
  57717. + * take several clocks, depending on the current state
  57718. + * of the core.
  57719. + */
  57720. + unsigned csftrst:1;
  57721. + /** Hclk Soft Reset
  57722. + *
  57723. + * The application uses this bit to reset the control logic in
  57724. + * the AHB clock domain. Only AHB clock domain pipelines are
  57725. + * reset.
  57726. + */
  57727. + unsigned hsftrst:1;
  57728. + /** Host Frame Counter Reset (Host Only)<br>
  57729. + *
  57730. + * The application can reset the (micro)frame number
  57731. + * counter inside the core, using this bit. When the
  57732. + * (micro)frame counter is reset, the subsequent SOF
  57733. + * sent out by the core, will have a (micro)frame
  57734. + * number of 0.
  57735. + */
  57736. + unsigned hstfrm:1;
  57737. + /** In Token Sequence Learning Queue Flush
  57738. + * (INTknQFlsh) (Device Only)
  57739. + */
  57740. + unsigned intknqflsh:1;
  57741. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  57742. + *
  57743. + * The application can flush the entire Receive FIFO
  57744. + * using this bit. The application must first
  57745. + * ensure that the core is not in the middle of a
  57746. + * transaction. The application should write into
  57747. + * this bit, only after making sure that neither the
  57748. + * DMA engine is reading from the RxFIFO nor the MAC
  57749. + * is writing the data in to the FIFO. The
  57750. + * application should wait until the bit is cleared
  57751. + * before performing any other operations. This bit
  57752. + * will takes 8 clocks (slowest of PHY or AHB clock)
  57753. + * to clear.
  57754. + */
  57755. + unsigned rxfflsh:1;
  57756. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  57757. + *
  57758. + * This bit is used to selectively flush a single or
  57759. + * all transmit FIFOs. The application must first
  57760. + * ensure that the core is not in the middle of a
  57761. + * transaction. The application should write into
  57762. + * this bit, only after making sure that neither the
  57763. + * DMA engine is writing into the TxFIFO nor the MAC
  57764. + * is reading the data out of the FIFO. The
  57765. + * application should wait until the core clears this
  57766. + * bit, before performing any operations. This bit
  57767. + * will takes 8 clocks (slowest of PHY or AHB clock)
  57768. + * to clear.
  57769. + */
  57770. + unsigned txfflsh:1;
  57771. +
  57772. + /** TxFIFO Number (TxFNum) (Device and Host).
  57773. + *
  57774. + * This is the FIFO number which needs to be flushed,
  57775. + * using the TxFIFO Flush bit. This field should not
  57776. + * be changed until the TxFIFO Flush bit is cleared by
  57777. + * the core.
  57778. + * - 0x0 : Non Periodic TxFIFO Flush
  57779. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  57780. + * or Periodic TxFIFO in host mode
  57781. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  57782. + * - ...
  57783. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  57784. + * - 0x10: Flush all the Transmit NonPeriodic and
  57785. + * Transmit Periodic FIFOs in the core
  57786. + */
  57787. + unsigned txfnum:5;
  57788. + /** Reserved */
  57789. + unsigned reserved11_29:19;
  57790. + /** DMA Request Signal. Indicated DMA request is in
  57791. + * probress. Used for debug purpose. */
  57792. + unsigned dmareq:1;
  57793. + /** AHB Master Idle. Indicates the AHB Master State
  57794. + * Machine is in IDLE condition. */
  57795. + unsigned ahbidle:1;
  57796. + } b;
  57797. +} grstctl_t;
  57798. +
  57799. +/**
  57800. + * This union represents the bit fields of the Core Interrupt Mask
  57801. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  57802. + * write the <i>d32</i> value to the register.
  57803. + */
  57804. +typedef union gintmsk_data {
  57805. + /** raw register data */
  57806. + uint32_t d32;
  57807. + /** register bits */
  57808. + struct {
  57809. + unsigned reserved0:1;
  57810. + unsigned modemismatch:1;
  57811. + unsigned otgintr:1;
  57812. + unsigned sofintr:1;
  57813. + unsigned rxstsqlvl:1;
  57814. + unsigned nptxfempty:1;
  57815. + unsigned ginnakeff:1;
  57816. + unsigned goutnakeff:1;
  57817. + unsigned ulpickint:1;
  57818. + unsigned i2cintr:1;
  57819. + unsigned erlysuspend:1;
  57820. + unsigned usbsuspend:1;
  57821. + unsigned usbreset:1;
  57822. + unsigned enumdone:1;
  57823. + unsigned isooutdrop:1;
  57824. + unsigned eopframe:1;
  57825. + unsigned restoredone:1;
  57826. + unsigned epmismatch:1;
  57827. + unsigned inepintr:1;
  57828. + unsigned outepintr:1;
  57829. + unsigned incomplisoin:1;
  57830. + unsigned incomplisoout:1;
  57831. + unsigned fetsusp:1;
  57832. + unsigned resetdet:1;
  57833. + unsigned portintr:1;
  57834. + unsigned hcintr:1;
  57835. + unsigned ptxfempty:1;
  57836. + unsigned lpmtranrcvd:1;
  57837. + unsigned conidstschng:1;
  57838. + unsigned disconnect:1;
  57839. + unsigned sessreqintr:1;
  57840. + unsigned wkupintr:1;
  57841. + } b;
  57842. +} gintmsk_data_t;
  57843. +/**
  57844. + * This union represents the bit fields of the Core Interrupt Register
  57845. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  57846. + * <i>d32</i> value to the register.
  57847. + */
  57848. +typedef union gintsts_data {
  57849. + /** raw register data */
  57850. + uint32_t d32;
  57851. +#define DWC_SOF_INTR_MASK 0x0008
  57852. + /** register bits */
  57853. + struct {
  57854. +#define DWC_HOST_MODE 1
  57855. + unsigned curmode:1;
  57856. + unsigned modemismatch:1;
  57857. + unsigned otgintr:1;
  57858. + unsigned sofintr:1;
  57859. + unsigned rxstsqlvl:1;
  57860. + unsigned nptxfempty:1;
  57861. + unsigned ginnakeff:1;
  57862. + unsigned goutnakeff:1;
  57863. + unsigned ulpickint:1;
  57864. + unsigned i2cintr:1;
  57865. + unsigned erlysuspend:1;
  57866. + unsigned usbsuspend:1;
  57867. + unsigned usbreset:1;
  57868. + unsigned enumdone:1;
  57869. + unsigned isooutdrop:1;
  57870. + unsigned eopframe:1;
  57871. + unsigned restoredone:1;
  57872. + unsigned epmismatch:1;
  57873. + unsigned inepint:1;
  57874. + unsigned outepintr:1;
  57875. + unsigned incomplisoin:1;
  57876. + unsigned incomplisoout:1;
  57877. + unsigned fetsusp:1;
  57878. + unsigned resetdet:1;
  57879. + unsigned portintr:1;
  57880. + unsigned hcintr:1;
  57881. + unsigned ptxfempty:1;
  57882. + unsigned lpmtranrcvd:1;
  57883. + unsigned conidstschng:1;
  57884. + unsigned disconnect:1;
  57885. + unsigned sessreqintr:1;
  57886. + unsigned wkupintr:1;
  57887. + } b;
  57888. +} gintsts_data_t;
  57889. +
  57890. +/**
  57891. + * This union represents the bit fields in the Device Receive Status Read and
  57892. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  57893. + * element then read out the bits using the <i>b</i>it elements.
  57894. + */
  57895. +typedef union device_grxsts_data {
  57896. + /** raw register data */
  57897. + uint32_t d32;
  57898. + /** register bits */
  57899. + struct {
  57900. + unsigned epnum:4;
  57901. + unsigned bcnt:11;
  57902. + unsigned dpid:2;
  57903. +
  57904. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  57905. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  57906. +
  57907. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  57908. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  57909. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  57910. + unsigned pktsts:4;
  57911. + unsigned fn:4;
  57912. + unsigned reserved25_31:7;
  57913. + } b;
  57914. +} device_grxsts_data_t;
  57915. +
  57916. +/**
  57917. + * This union represents the bit fields in the Host Receive Status Read and
  57918. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  57919. + * element then read out the bits using the <i>b</i>it elements.
  57920. + */
  57921. +typedef union host_grxsts_data {
  57922. + /** raw register data */
  57923. + uint32_t d32;
  57924. + /** register bits */
  57925. + struct {
  57926. + unsigned chnum:4;
  57927. + unsigned bcnt:11;
  57928. + unsigned dpid:2;
  57929. +
  57930. + unsigned pktsts:4;
  57931. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  57932. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  57933. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  57934. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  57935. +
  57936. + unsigned reserved21_31:11;
  57937. + } b;
  57938. +} host_grxsts_data_t;
  57939. +
  57940. +/**
  57941. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  57942. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  57943. + * then read out the bits using the <i>b</i>it elements.
  57944. + */
  57945. +typedef union fifosize_data {
  57946. + /** raw register data */
  57947. + uint32_t d32;
  57948. + /** register bits */
  57949. + struct {
  57950. + unsigned startaddr:16;
  57951. + unsigned depth:16;
  57952. + } b;
  57953. +} fifosize_data_t;
  57954. +
  57955. +/**
  57956. + * This union represents the bit fields in the Non-Periodic Transmit
  57957. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  57958. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  57959. + * elements.
  57960. + */
  57961. +typedef union gnptxsts_data {
  57962. + /** raw register data */
  57963. + uint32_t d32;
  57964. + /** register bits */
  57965. + struct {
  57966. + unsigned nptxfspcavail:16;
  57967. + unsigned nptxqspcavail:8;
  57968. + /** Top of the Non-Periodic Transmit Request Queue
  57969. + * - bit 24 - Terminate (Last entry for the selected
  57970. + * channel/EP)
  57971. + * - bits 26:25 - Token Type
  57972. + * - 2'b00 - IN/OUT
  57973. + * - 2'b01 - Zero Length OUT
  57974. + * - 2'b10 - PING/Complete Split
  57975. + * - 2'b11 - Channel Halt
  57976. + * - bits 30:27 - Channel/EP Number
  57977. + */
  57978. + unsigned nptxqtop_terminate:1;
  57979. + unsigned nptxqtop_token:2;
  57980. + unsigned nptxqtop_chnep:4;
  57981. + unsigned reserved:1;
  57982. + } b;
  57983. +} gnptxsts_data_t;
  57984. +
  57985. +/**
  57986. + * This union represents the bit fields in the Transmit
  57987. + * FIFO Status Register (DTXFSTS). Read the register into the
  57988. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  57989. + * elements.
  57990. + */
  57991. +typedef union dtxfsts_data {
  57992. + /** raw register data */
  57993. + uint32_t d32;
  57994. + /** register bits */
  57995. + struct {
  57996. + unsigned txfspcavail:16;
  57997. + unsigned reserved:16;
  57998. + } b;
  57999. +} dtxfsts_data_t;
  58000. +
  58001. +/**
  58002. + * This union represents the bit fields in the I2C Control Register
  58003. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  58004. + * bits using the <i>b</i>it elements.
  58005. + */
  58006. +typedef union gi2cctl_data {
  58007. + /** raw register data */
  58008. + uint32_t d32;
  58009. + /** register bits */
  58010. + struct {
  58011. + unsigned rwdata:8;
  58012. + unsigned regaddr:8;
  58013. + unsigned addr:7;
  58014. + unsigned i2cen:1;
  58015. + unsigned ack:1;
  58016. + unsigned i2csuspctl:1;
  58017. + unsigned i2cdevaddr:2;
  58018. + unsigned i2cdatse0:1;
  58019. + unsigned reserved:1;
  58020. + unsigned rw:1;
  58021. + unsigned bsydne:1;
  58022. + } b;
  58023. +} gi2cctl_data_t;
  58024. +
  58025. +/**
  58026. + * This union represents the bit fields in the PHY Vendor Control Register
  58027. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  58028. + * bits using the <i>b</i>it elements.
  58029. + */
  58030. +typedef union gpvndctl_data {
  58031. + /** raw register data */
  58032. + uint32_t d32;
  58033. + /** register bits */
  58034. + struct {
  58035. + unsigned regdata:8;
  58036. + unsigned vctrl:8;
  58037. + unsigned regaddr16_21:6;
  58038. + unsigned regwr:1;
  58039. + unsigned reserved23_24:2;
  58040. + unsigned newregreq:1;
  58041. + unsigned vstsbsy:1;
  58042. + unsigned vstsdone:1;
  58043. + unsigned reserved28_30:3;
  58044. + unsigned disulpidrvr:1;
  58045. + } b;
  58046. +} gpvndctl_data_t;
  58047. +
  58048. +/**
  58049. + * This union represents the bit fields in the General Purpose
  58050. + * Input/Output Register (GGPIO).
  58051. + * Read the register into the <i>d32</i> element then read out the
  58052. + * bits using the <i>b</i>it elements.
  58053. + */
  58054. +typedef union ggpio_data {
  58055. + /** raw register data */
  58056. + uint32_t d32;
  58057. + /** register bits */
  58058. + struct {
  58059. + unsigned gpi:16;
  58060. + unsigned gpo:16;
  58061. + } b;
  58062. +} ggpio_data_t;
  58063. +
  58064. +/**
  58065. + * This union represents the bit fields in the User ID Register
  58066. + * (GUID). Read the register into the <i>d32</i> element then read out the
  58067. + * bits using the <i>b</i>it elements.
  58068. + */
  58069. +typedef union guid_data {
  58070. + /** raw register data */
  58071. + uint32_t d32;
  58072. + /** register bits */
  58073. + struct {
  58074. + unsigned rwdata:32;
  58075. + } b;
  58076. +} guid_data_t;
  58077. +
  58078. +/**
  58079. + * This union represents the bit fields in the Synopsys ID Register
  58080. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  58081. + * bits using the <i>b</i>it elements.
  58082. + */
  58083. +typedef union gsnpsid_data {
  58084. + /** raw register data */
  58085. + uint32_t d32;
  58086. + /** register bits */
  58087. + struct {
  58088. + unsigned rwdata:32;
  58089. + } b;
  58090. +} gsnpsid_data_t;
  58091. +
  58092. +/**
  58093. + * This union represents the bit fields in the User HW Config1
  58094. + * Register. Read the register into the <i>d32</i> element then read
  58095. + * out the bits using the <i>b</i>it elements.
  58096. + */
  58097. +typedef union hwcfg1_data {
  58098. + /** raw register data */
  58099. + uint32_t d32;
  58100. + /** register bits */
  58101. + struct {
  58102. + unsigned ep_dir0:2;
  58103. + unsigned ep_dir1:2;
  58104. + unsigned ep_dir2:2;
  58105. + unsigned ep_dir3:2;
  58106. + unsigned ep_dir4:2;
  58107. + unsigned ep_dir5:2;
  58108. + unsigned ep_dir6:2;
  58109. + unsigned ep_dir7:2;
  58110. + unsigned ep_dir8:2;
  58111. + unsigned ep_dir9:2;
  58112. + unsigned ep_dir10:2;
  58113. + unsigned ep_dir11:2;
  58114. + unsigned ep_dir12:2;
  58115. + unsigned ep_dir13:2;
  58116. + unsigned ep_dir14:2;
  58117. + unsigned ep_dir15:2;
  58118. + } b;
  58119. +} hwcfg1_data_t;
  58120. +
  58121. +/**
  58122. + * This union represents the bit fields in the User HW Config2
  58123. + * Register. Read the register into the <i>d32</i> element then read
  58124. + * out the bits using the <i>b</i>it elements.
  58125. + */
  58126. +typedef union hwcfg2_data {
  58127. + /** raw register data */
  58128. + uint32_t d32;
  58129. + /** register bits */
  58130. + struct {
  58131. + /* GHWCFG2 */
  58132. + unsigned op_mode:3;
  58133. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  58134. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  58135. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  58136. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  58137. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  58138. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  58139. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  58140. +
  58141. + unsigned architecture:2;
  58142. + unsigned point2point:1;
  58143. + unsigned hs_phy_type:2;
  58144. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  58145. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  58146. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  58147. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  58148. +
  58149. + unsigned fs_phy_type:2;
  58150. + unsigned num_dev_ep:4;
  58151. + unsigned num_host_chan:4;
  58152. + unsigned perio_ep_supported:1;
  58153. + unsigned dynamic_fifo:1;
  58154. + unsigned multi_proc_int:1;
  58155. + unsigned reserved21:1;
  58156. + unsigned nonperio_tx_q_depth:2;
  58157. + unsigned host_perio_tx_q_depth:2;
  58158. + unsigned dev_token_q_depth:5;
  58159. + unsigned otg_enable_ic_usb:1;
  58160. + } b;
  58161. +} hwcfg2_data_t;
  58162. +
  58163. +/**
  58164. + * This union represents the bit fields in the User HW Config3
  58165. + * Register. Read the register into the <i>d32</i> element then read
  58166. + * out the bits using the <i>b</i>it elements.
  58167. + */
  58168. +typedef union hwcfg3_data {
  58169. + /** raw register data */
  58170. + uint32_t d32;
  58171. + /** register bits */
  58172. + struct {
  58173. + /* GHWCFG3 */
  58174. + unsigned xfer_size_cntr_width:4;
  58175. + unsigned packet_size_cntr_width:3;
  58176. + unsigned otg_func:1;
  58177. + unsigned i2c:1;
  58178. + unsigned vendor_ctrl_if:1;
  58179. + unsigned optional_features:1;
  58180. + unsigned synch_reset_type:1;
  58181. + unsigned adp_supp:1;
  58182. + unsigned otg_enable_hsic:1;
  58183. + unsigned bc_support:1;
  58184. + unsigned otg_lpm_en:1;
  58185. + unsigned dfifo_depth:16;
  58186. + } b;
  58187. +} hwcfg3_data_t;
  58188. +
  58189. +/**
  58190. + * This union represents the bit fields in the User HW Config4
  58191. + * Register. Read the register into the <i>d32</i> element then read
  58192. + * out the bits using the <i>b</i>it elements.
  58193. + */
  58194. +typedef union hwcfg4_data {
  58195. + /** raw register data */
  58196. + uint32_t d32;
  58197. + /** register bits */
  58198. + struct {
  58199. + unsigned num_dev_perio_in_ep:4;
  58200. + unsigned power_optimiz:1;
  58201. + unsigned min_ahb_freq:1;
  58202. + unsigned hiber:1;
  58203. + unsigned xhiber:1;
  58204. + unsigned reserved:6;
  58205. + unsigned utmi_phy_data_width:2;
  58206. + unsigned num_dev_mode_ctrl_ep:4;
  58207. + unsigned iddig_filt_en:1;
  58208. + unsigned vbus_valid_filt_en:1;
  58209. + unsigned a_valid_filt_en:1;
  58210. + unsigned b_valid_filt_en:1;
  58211. + unsigned session_end_filt_en:1;
  58212. + unsigned ded_fifo_en:1;
  58213. + unsigned num_in_eps:4;
  58214. + unsigned desc_dma:1;
  58215. + unsigned desc_dma_dyn:1;
  58216. + } b;
  58217. +} hwcfg4_data_t;
  58218. +
  58219. +/**
  58220. + * This union represents the bit fields of the Core LPM Configuration
  58221. + * Register (GLPMCFG). Set the bits using bit fields then write
  58222. + * the <i>d32</i> value to the register.
  58223. + */
  58224. +typedef union glpmctl_data {
  58225. + /** raw register data */
  58226. + uint32_t d32;
  58227. + /** register bits */
  58228. + struct {
  58229. + /** LPM-Capable (LPMCap) (Device and Host)
  58230. + * The application uses this bit to control
  58231. + * the DWC_otg core LPM capabilities.
  58232. + */
  58233. + unsigned lpm_cap_en:1;
  58234. + /** LPM response programmed by application (AppL1Res) (Device)
  58235. + * Handshake response to LPM token pre-programmed
  58236. + * by device application software.
  58237. + */
  58238. + unsigned appl_resp:1;
  58239. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  58240. + * In Host mode this field indicates the value of HIRD
  58241. + * to be sent in an LPM transaction.
  58242. + * In Device mode this field is updated with the
  58243. + * Received LPM Token HIRD bmAttribute
  58244. + * when an ACK/NYET/STALL response is sent
  58245. + * to an LPM transaction.
  58246. + */
  58247. + unsigned hird:4;
  58248. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  58249. + * In Host mode this bit indicates the value of remote
  58250. + * wake up to be sent in wIndex field of LPM transaction.
  58251. + * In Device mode this field is updated with the
  58252. + * Received LPM Token bRemoteWake bmAttribute
  58253. + * when an ACK/NYET/STALL response is sent
  58254. + * to an LPM transaction.
  58255. + */
  58256. + unsigned rem_wkup_en:1;
  58257. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  58258. + * The application uses this bit to control
  58259. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  58260. + */
  58261. + unsigned en_utmi_sleep:1;
  58262. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  58263. + */
  58264. + unsigned hird_thres:5;
  58265. + /** LPM Response (CoreL1Res) (Device and Host)
  58266. + * In Host mode this bit contains handsake response to
  58267. + * LPM transaction.
  58268. + * In Device mode the response of the core to
  58269. + * LPM transaction received is reflected in these two bits.
  58270. + - 0x0 : ERROR (No handshake response)
  58271. + - 0x1 : STALL
  58272. + - 0x2 : NYET
  58273. + - 0x3 : ACK
  58274. + */
  58275. + unsigned lpm_resp:2;
  58276. + /** Port Sleep Status (SlpSts) (Device and Host)
  58277. + * This bit is set as long as a Sleep condition
  58278. + * is present on the USB bus.
  58279. + */
  58280. + unsigned prt_sleep_sts:1;
  58281. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  58282. + * Indicates that the application or host
  58283. + * can start resume from Sleep state.
  58284. + */
  58285. + unsigned sleep_state_resumeok:1;
  58286. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  58287. + * The channel number on which the LPM transaction
  58288. + * has to be applied while sending
  58289. + * an LPM transaction to the local device.
  58290. + */
  58291. + unsigned lpm_chan_index:4;
  58292. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  58293. + * Number host retries that would be performed
  58294. + * if the device response was not valid response.
  58295. + */
  58296. + unsigned retry_count:3;
  58297. + /** Send LPM Transaction (SndLPM) (Host)
  58298. + * When set by application software,
  58299. + * an LPM transaction containing two tokens
  58300. + * is sent.
  58301. + */
  58302. + unsigned send_lpm:1;
  58303. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  58304. + * Number of LPM Host Retries still remaining
  58305. + * to be transmitted for the current LPM sequence
  58306. + */
  58307. + unsigned retry_count_sts:3;
  58308. + unsigned reserved28_29:2;
  58309. + /** In host mode once this bit is set, the host
  58310. + * configures to drive the HSIC Idle state on the bus.
  58311. + * It then waits for the device to initiate the Connect sequence.
  58312. + * In device mode once this bit is set, the device waits for
  58313. + * the HSIC Idle line state on the bus. Upon receving the Idle
  58314. + * line state, it initiates the HSIC Connect sequence.
  58315. + */
  58316. + unsigned hsic_connect:1;
  58317. + /** This bit overrides and functionally inverts
  58318. + * the if_select_hsic input port signal.
  58319. + */
  58320. + unsigned inv_sel_hsic:1;
  58321. + } b;
  58322. +} glpmcfg_data_t;
  58323. +
  58324. +/**
  58325. + * This union represents the bit fields of the Core ADP Timer, Control and
  58326. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  58327. + * the <i>d32</i> value to the register.
  58328. + */
  58329. +typedef union adpctl_data {
  58330. + /** raw register data */
  58331. + uint32_t d32;
  58332. + /** register bits */
  58333. + struct {
  58334. + /** Probe Discharge (PRB_DSCHG)
  58335. + * These bits set the times for TADP_DSCHG.
  58336. + * These bits are defined as follows:
  58337. + * 2'b00 - 4 msec
  58338. + * 2'b01 - 8 msec
  58339. + * 2'b10 - 16 msec
  58340. + * 2'b11 - 32 msec
  58341. + */
  58342. + unsigned prb_dschg:2;
  58343. + /** Probe Delta (PRB_DELTA)
  58344. + * These bits set the resolution for RTIM value.
  58345. + * The bits are defined in units of 32 kHz clock cycles as follows:
  58346. + * 2'b00 - 1 cycles
  58347. + * 2'b01 - 2 cycles
  58348. + * 2'b10 - 3 cycles
  58349. + * 2'b11 - 4 cycles
  58350. + * For example if this value is chosen to 2'b01, it means that RTIM
  58351. + * increments for every 3(three) 32Khz clock cycles.
  58352. + */
  58353. + unsigned prb_delta:2;
  58354. + /** Probe Period (PRB_PER)
  58355. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  58356. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  58357. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  58358. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  58359. + * 2'b11 - Reserved
  58360. + */
  58361. + unsigned prb_per:2;
  58362. + /** These bits capture the latest time it took for VBUS to ramp from
  58363. + * VADP_SINK to VADP_PRB.
  58364. + * 0x000 - 1 cycles
  58365. + * 0x001 - 2 cycles
  58366. + * 0x002 - 3 cycles
  58367. + * etc
  58368. + * 0x7FF - 2048 cycles
  58369. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  58370. + */
  58371. + unsigned rtim:11;
  58372. + /** Enable Probe (EnaPrb)
  58373. + * When programmed to 1'b1, the core performs a probe operation.
  58374. + * This bit is valid only if OTG_Ver = 1'b1.
  58375. + */
  58376. + unsigned enaprb:1;
  58377. + /** Enable Sense (EnaSns)
  58378. + * When programmed to 1'b1, the core performs a Sense operation.
  58379. + * This bit is valid only if OTG_Ver = 1'b1.
  58380. + */
  58381. + unsigned enasns:1;
  58382. + /** ADP Reset (ADPRes)
  58383. + * When set, ADP controller is reset.
  58384. + * This bit is valid only if OTG_Ver = 1'b1.
  58385. + */
  58386. + unsigned adpres:1;
  58387. + /** ADP Enable (ADPEn)
  58388. + * When set, the core performs either ADP probing or sensing
  58389. + * based on EnaPrb or EnaSns.
  58390. + * This bit is valid only if OTG_Ver = 1'b1.
  58391. + */
  58392. + unsigned adpen:1;
  58393. + /** ADP Probe Interrupt (ADP_PRB_INT)
  58394. + * When this bit is set, it means that the VBUS
  58395. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  58396. + * This bit is valid only if OTG_Ver = 1'b1.
  58397. + */
  58398. + unsigned adp_prb_int:1;
  58399. + /**
  58400. + * ADP Sense Interrupt (ADP_SNS_INT)
  58401. + * When this bit is set, it means that the VBUS voltage is greater than
  58402. + * VADP_SNS value or VADP_SNS is reached.
  58403. + * This bit is valid only if OTG_Ver = 1'b1.
  58404. + */
  58405. + unsigned adp_sns_int:1;
  58406. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  58407. + * This bit is relevant only for an ADP probe.
  58408. + * When this bit is set, it means that the ramp time has
  58409. + * completed ie ADPCTL.RTIM has reached its terminal value
  58410. + * of 0x7FF. This is a debug feature that allows software
  58411. + * to read the ramp time after each cycle.
  58412. + * This bit is valid only if OTG_Ver = 1'b1.
  58413. + */
  58414. + unsigned adp_tmout_int:1;
  58415. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  58416. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  58417. + * This bit is valid only if OTG_Ver = 1'b1.
  58418. + */
  58419. + unsigned adp_prb_int_msk:1;
  58420. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  58421. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  58422. + * This bit is valid only if OTG_Ver = 1'b1.
  58423. + */
  58424. + unsigned adp_sns_int_msk:1;
  58425. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  58426. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  58427. + * This bit is valid only if OTG_Ver = 1'b1.
  58428. + */
  58429. + unsigned adp_tmout_int_msk:1;
  58430. + /** Access Request
  58431. + * 2'b00 - Read/Write Valid (updated by the core)
  58432. + * 2'b01 - Read
  58433. + * 2'b00 - Write
  58434. + * 2'b00 - Reserved
  58435. + */
  58436. + unsigned ar:2;
  58437. + /** Reserved */
  58438. + unsigned reserved29_31:3;
  58439. + } b;
  58440. +} adpctl_data_t;
  58441. +
  58442. +////////////////////////////////////////////
  58443. +// Device Registers
  58444. +/**
  58445. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  58446. + *
  58447. + * The following structures define the size and relative field offsets
  58448. + * for the Device Mode Registers.
  58449. + *
  58450. + * <i>These registers are visible only in Device mode and must not be
  58451. + * accessed in Host mode, as the results are unknown.</i>
  58452. + */
  58453. +typedef struct dwc_otg_dev_global_regs {
  58454. + /** Device Configuration Register. <i>Offset 800h</i> */
  58455. + volatile uint32_t dcfg;
  58456. + /** Device Control Register. <i>Offset: 804h</i> */
  58457. + volatile uint32_t dctl;
  58458. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  58459. + volatile uint32_t dsts;
  58460. + /** Reserved. <i>Offset: 80Ch</i> */
  58461. + uint32_t unused;
  58462. + /** Device IN Endpoint Common Interrupt Mask
  58463. + * Register. <i>Offset: 810h</i> */
  58464. + volatile uint32_t diepmsk;
  58465. + /** Device OUT Endpoint Common Interrupt Mask
  58466. + * Register. <i>Offset: 814h</i> */
  58467. + volatile uint32_t doepmsk;
  58468. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  58469. + volatile uint32_t daint;
  58470. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  58471. + * 81Ch</i> */
  58472. + volatile uint32_t daintmsk;
  58473. + /** Device IN Token Queue Read Register-1 (Read Only).
  58474. + * <i>Offset: 820h</i> */
  58475. + volatile uint32_t dtknqr1;
  58476. + /** Device IN Token Queue Read Register-2 (Read Only).
  58477. + * <i>Offset: 824h</i> */
  58478. + volatile uint32_t dtknqr2;
  58479. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  58480. + volatile uint32_t dvbusdis;
  58481. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  58482. + volatile uint32_t dvbuspulse;
  58483. + /** Device IN Token Queue Read Register-3 (Read Only). /
  58484. + * Device Thresholding control register (Read/Write)
  58485. + * <i>Offset: 830h</i> */
  58486. + volatile uint32_t dtknqr3_dthrctl;
  58487. + /** Device IN Token Queue Read Register-4 (Read Only). /
  58488. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  58489. + * <i>Offset: 834h</i> */
  58490. + volatile uint32_t dtknqr4_fifoemptymsk;
  58491. + /** Device Each Endpoint Interrupt Register (Read Only). /
  58492. + * <i>Offset: 838h</i> */
  58493. + volatile uint32_t deachint;
  58494. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  58495. + * <i>Offset: 83Ch</i> */
  58496. + volatile uint32_t deachintmsk;
  58497. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  58498. + * <i>Offset: 840h</i> */
  58499. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  58500. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  58501. + * <i>Offset: 880h</i> */
  58502. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  58503. +} dwc_otg_device_global_regs_t;
  58504. +
  58505. +/**
  58506. + * This union represents the bit fields in the Device Configuration
  58507. + * Register. Read the register into the <i>d32</i> member then
  58508. + * set/clear the bits using the <i>b</i>it elements. Write the
  58509. + * <i>d32</i> member to the dcfg register.
  58510. + */
  58511. +typedef union dcfg_data {
  58512. + /** raw register data */
  58513. + uint32_t d32;
  58514. + /** register bits */
  58515. + struct {
  58516. + /** Device Speed */
  58517. + unsigned devspd:2;
  58518. + /** Non Zero Length Status OUT Handshake */
  58519. + unsigned nzstsouthshk:1;
  58520. +#define DWC_DCFG_SEND_STALL 1
  58521. +
  58522. + unsigned ena32khzs:1;
  58523. + /** Device Addresses */
  58524. + unsigned devaddr:7;
  58525. + /** Periodic Frame Interval */
  58526. + unsigned perfrint:2;
  58527. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  58528. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  58529. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  58530. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  58531. +
  58532. + /** Enable Device OUT NAK for bulk in DDMA mode */
  58533. + unsigned endevoutnak:1;
  58534. +
  58535. + unsigned reserved14_17:4;
  58536. + /** In Endpoint Mis-match count */
  58537. + unsigned epmscnt:5;
  58538. + /** Enable Descriptor DMA in Device mode */
  58539. + unsigned descdma:1;
  58540. + unsigned perschintvl:2;
  58541. + unsigned resvalid:6;
  58542. + } b;
  58543. +} dcfg_data_t;
  58544. +
  58545. +/**
  58546. + * This union represents the bit fields in the Device Control
  58547. + * Register. Read the register into the <i>d32</i> member then
  58548. + * set/clear the bits using the <i>b</i>it elements.
  58549. + */
  58550. +typedef union dctl_data {
  58551. + /** raw register data */
  58552. + uint32_t d32;
  58553. + /** register bits */
  58554. + struct {
  58555. + /** Remote Wakeup */
  58556. + unsigned rmtwkupsig:1;
  58557. + /** Soft Disconnect */
  58558. + unsigned sftdiscon:1;
  58559. + /** Global Non-Periodic IN NAK Status */
  58560. + unsigned gnpinnaksts:1;
  58561. + /** Global OUT NAK Status */
  58562. + unsigned goutnaksts:1;
  58563. + /** Test Control */
  58564. + unsigned tstctl:3;
  58565. + /** Set Global Non-Periodic IN NAK */
  58566. + unsigned sgnpinnak:1;
  58567. + /** Clear Global Non-Periodic IN NAK */
  58568. + unsigned cgnpinnak:1;
  58569. + /** Set Global OUT NAK */
  58570. + unsigned sgoutnak:1;
  58571. + /** Clear Global OUT NAK */
  58572. + unsigned cgoutnak:1;
  58573. + /** Power-On Programming Done */
  58574. + unsigned pwronprgdone:1;
  58575. + /** Reserved */
  58576. + unsigned reserved:1;
  58577. + /** Global Multi Count */
  58578. + unsigned gmc:2;
  58579. + /** Ignore Frame Number for ISOC EPs */
  58580. + unsigned ifrmnum:1;
  58581. + /** NAK on Babble */
  58582. + unsigned nakonbble:1;
  58583. + /** Enable Continue on BNA */
  58584. + unsigned encontonbna:1;
  58585. +
  58586. + unsigned reserved18_31:14;
  58587. + } b;
  58588. +} dctl_data_t;
  58589. +
  58590. +/**
  58591. + * This union represents the bit fields in the Device Status
  58592. + * Register. Read the register into the <i>d32</i> member then
  58593. + * set/clear the bits using the <i>b</i>it elements.
  58594. + */
  58595. +typedef union dsts_data {
  58596. + /** raw register data */
  58597. + uint32_t d32;
  58598. + /** register bits */
  58599. + struct {
  58600. + /** Suspend Status */
  58601. + unsigned suspsts:1;
  58602. + /** Enumerated Speed */
  58603. + unsigned enumspd:2;
  58604. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  58605. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  58606. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  58607. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  58608. + /** Erratic Error */
  58609. + unsigned errticerr:1;
  58610. + unsigned reserved4_7:4;
  58611. + /** Frame or Microframe Number of the received SOF */
  58612. + unsigned soffn:14;
  58613. + unsigned reserved22_31:10;
  58614. + } b;
  58615. +} dsts_data_t;
  58616. +
  58617. +/**
  58618. + * This union represents the bit fields in the Device IN EP Interrupt
  58619. + * Register and the Device IN EP Common Mask Register.
  58620. + *
  58621. + * - Read the register into the <i>d32</i> member then set/clear the
  58622. + * bits using the <i>b</i>it elements.
  58623. + */
  58624. +typedef union diepint_data {
  58625. + /** raw register data */
  58626. + uint32_t d32;
  58627. + /** register bits */
  58628. + struct {
  58629. + /** Transfer complete mask */
  58630. + unsigned xfercompl:1;
  58631. + /** Endpoint disable mask */
  58632. + unsigned epdisabled:1;
  58633. + /** AHB Error mask */
  58634. + unsigned ahberr:1;
  58635. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  58636. + unsigned timeout:1;
  58637. + /** IN Token received with TxF Empty mask */
  58638. + unsigned intktxfemp:1;
  58639. + /** IN Token Received with EP mismatch mask */
  58640. + unsigned intknepmis:1;
  58641. + /** IN Endpoint NAK Effective mask */
  58642. + unsigned inepnakeff:1;
  58643. + /** Reserved */
  58644. + unsigned emptyintr:1;
  58645. +
  58646. + unsigned txfifoundrn:1;
  58647. +
  58648. + /** BNA Interrupt mask */
  58649. + unsigned bna:1;
  58650. +
  58651. + unsigned reserved10_12:3;
  58652. + /** BNA Interrupt mask */
  58653. + unsigned nak:1;
  58654. +
  58655. + unsigned reserved14_31:18;
  58656. + } b;
  58657. +} diepint_data_t;
  58658. +
  58659. +/**
  58660. + * This union represents the bit fields in the Device IN EP
  58661. + * Common/Dedicated Interrupt Mask Register.
  58662. + */
  58663. +typedef union diepint_data diepmsk_data_t;
  58664. +
  58665. +/**
  58666. + * This union represents the bit fields in the Device OUT EP Interrupt
  58667. + * Registerand Device OUT EP Common Interrupt Mask Register.
  58668. + *
  58669. + * - Read the register into the <i>d32</i> member then set/clear the
  58670. + * bits using the <i>b</i>it elements.
  58671. + */
  58672. +typedef union doepint_data {
  58673. + /** raw register data */
  58674. + uint32_t d32;
  58675. + /** register bits */
  58676. + struct {
  58677. + /** Transfer complete */
  58678. + unsigned xfercompl:1;
  58679. + /** Endpoint disable */
  58680. + unsigned epdisabled:1;
  58681. + /** AHB Error */
  58682. + unsigned ahberr:1;
  58683. + /** Setup Phase Done (contorl EPs) */
  58684. + unsigned setup:1;
  58685. + /** OUT Token Received when Endpoint Disabled */
  58686. + unsigned outtknepdis:1;
  58687. +
  58688. + unsigned stsphsercvd:1;
  58689. + /** Back-to-Back SETUP Packets Received */
  58690. + unsigned back2backsetup:1;
  58691. +
  58692. + unsigned reserved7:1;
  58693. + /** OUT packet Error */
  58694. + unsigned outpkterr:1;
  58695. + /** BNA Interrupt */
  58696. + unsigned bna:1;
  58697. +
  58698. + unsigned reserved10:1;
  58699. + /** Packet Drop Status */
  58700. + unsigned pktdrpsts:1;
  58701. + /** Babble Interrupt */
  58702. + unsigned babble:1;
  58703. + /** NAK Interrupt */
  58704. + unsigned nak:1;
  58705. + /** NYET Interrupt */
  58706. + unsigned nyet:1;
  58707. + /** Bit indicating setup packet received */
  58708. + unsigned sr:1;
  58709. +
  58710. + unsigned reserved16_31:16;
  58711. + } b;
  58712. +} doepint_data_t;
  58713. +
  58714. +/**
  58715. + * This union represents the bit fields in the Device OUT EP
  58716. + * Common/Dedicated Interrupt Mask Register.
  58717. + */
  58718. +typedef union doepint_data doepmsk_data_t;
  58719. +
  58720. +/**
  58721. + * This union represents the bit fields in the Device All EP Interrupt
  58722. + * and Mask Registers.
  58723. + * - Read the register into the <i>d32</i> member then set/clear the
  58724. + * bits using the <i>b</i>it elements.
  58725. + */
  58726. +typedef union daint_data {
  58727. + /** raw register data */
  58728. + uint32_t d32;
  58729. + /** register bits */
  58730. + struct {
  58731. + /** IN Endpoint bits */
  58732. + unsigned in:16;
  58733. + /** OUT Endpoint bits */
  58734. + unsigned out:16;
  58735. + } ep;
  58736. + struct {
  58737. + /** IN Endpoint bits */
  58738. + unsigned inep0:1;
  58739. + unsigned inep1:1;
  58740. + unsigned inep2:1;
  58741. + unsigned inep3:1;
  58742. + unsigned inep4:1;
  58743. + unsigned inep5:1;
  58744. + unsigned inep6:1;
  58745. + unsigned inep7:1;
  58746. + unsigned inep8:1;
  58747. + unsigned inep9:1;
  58748. + unsigned inep10:1;
  58749. + unsigned inep11:1;
  58750. + unsigned inep12:1;
  58751. + unsigned inep13:1;
  58752. + unsigned inep14:1;
  58753. + unsigned inep15:1;
  58754. + /** OUT Endpoint bits */
  58755. + unsigned outep0:1;
  58756. + unsigned outep1:1;
  58757. + unsigned outep2:1;
  58758. + unsigned outep3:1;
  58759. + unsigned outep4:1;
  58760. + unsigned outep5:1;
  58761. + unsigned outep6:1;
  58762. + unsigned outep7:1;
  58763. + unsigned outep8:1;
  58764. + unsigned outep9:1;
  58765. + unsigned outep10:1;
  58766. + unsigned outep11:1;
  58767. + unsigned outep12:1;
  58768. + unsigned outep13:1;
  58769. + unsigned outep14:1;
  58770. + unsigned outep15:1;
  58771. + } b;
  58772. +} daint_data_t;
  58773. +
  58774. +/**
  58775. + * This union represents the bit fields in the Device IN Token Queue
  58776. + * Read Registers.
  58777. + * - Read the register into the <i>d32</i> member.
  58778. + * - READ-ONLY Register
  58779. + */
  58780. +typedef union dtknq1_data {
  58781. + /** raw register data */
  58782. + uint32_t d32;
  58783. + /** register bits */
  58784. + struct {
  58785. + /** In Token Queue Write Pointer */
  58786. + unsigned intknwptr:5;
  58787. + /** Reserved */
  58788. + unsigned reserved05_06:2;
  58789. + /** write pointer has wrapped. */
  58790. + unsigned wrap_bit:1;
  58791. + /** EP Numbers of IN Tokens 0 ... 4 */
  58792. + unsigned epnums0_5:24;
  58793. + } b;
  58794. +} dtknq1_data_t;
  58795. +
  58796. +/**
  58797. + * This union represents Threshold control Register
  58798. + * - Read and write the register into the <i>d32</i> member.
  58799. + * - READ-WRITABLE Register
  58800. + */
  58801. +typedef union dthrctl_data {
  58802. + /** raw register data */
  58803. + uint32_t d32;
  58804. + /** register bits */
  58805. + struct {
  58806. + /** non ISO Tx Thr. Enable */
  58807. + unsigned non_iso_thr_en:1;
  58808. + /** ISO Tx Thr. Enable */
  58809. + unsigned iso_thr_en:1;
  58810. + /** Tx Thr. Length */
  58811. + unsigned tx_thr_len:9;
  58812. + /** AHB Threshold ratio */
  58813. + unsigned ahb_thr_ratio:2;
  58814. + /** Reserved */
  58815. + unsigned reserved13_15:3;
  58816. + /** Rx Thr. Enable */
  58817. + unsigned rx_thr_en:1;
  58818. + /** Rx Thr. Length */
  58819. + unsigned rx_thr_len:9;
  58820. + unsigned reserved26:1;
  58821. + /** Arbiter Parking Enable*/
  58822. + unsigned arbprken:1;
  58823. + /** Reserved */
  58824. + unsigned reserved28_31:4;
  58825. + } b;
  58826. +} dthrctl_data_t;
  58827. +
  58828. +/**
  58829. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  58830. + * 900h-AFCh</i>
  58831. + *
  58832. + * There will be one set of endpoint registers per logical endpoint
  58833. + * implemented.
  58834. + *
  58835. + * <i>These registers are visible only in Device mode and must not be
  58836. + * accessed in Host mode, as the results are unknown.</i>
  58837. + */
  58838. +typedef struct dwc_otg_dev_in_ep_regs {
  58839. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  58840. + * (ep_num * 20h) + 00h</i> */
  58841. + volatile uint32_t diepctl;
  58842. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  58843. + uint32_t reserved04;
  58844. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  58845. + * (ep_num * 20h) + 08h</i> */
  58846. + volatile uint32_t diepint;
  58847. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  58848. + uint32_t reserved0C;
  58849. + /** Device IN Endpoint Transfer Size
  58850. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  58851. + volatile uint32_t dieptsiz;
  58852. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  58853. + * (ep_num * 20h) + 14h</i> */
  58854. + volatile uint32_t diepdma;
  58855. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  58856. + * (ep_num * 20h) + 18h</i> */
  58857. + volatile uint32_t dtxfsts;
  58858. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  58859. + * (ep_num * 20h) + 1Ch</i> */
  58860. + volatile uint32_t diepdmab;
  58861. +} dwc_otg_dev_in_ep_regs_t;
  58862. +
  58863. +/**
  58864. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  58865. + * B00h-CFCh</i>
  58866. + *
  58867. + * There will be one set of endpoint registers per logical endpoint
  58868. + * implemented.
  58869. + *
  58870. + * <i>These registers are visible only in Device mode and must not be
  58871. + * accessed in Host mode, as the results are unknown.</i>
  58872. + */
  58873. +typedef struct dwc_otg_dev_out_ep_regs {
  58874. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  58875. + * (ep_num * 20h) + 00h</i> */
  58876. + volatile uint32_t doepctl;
  58877. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  58878. + uint32_t reserved04;
  58879. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  58880. + * (ep_num * 20h) + 08h</i> */
  58881. + volatile uint32_t doepint;
  58882. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  58883. + uint32_t reserved0C;
  58884. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  58885. + * B00h + (ep_num * 20h) + 10h</i> */
  58886. + volatile uint32_t doeptsiz;
  58887. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  58888. + * + (ep_num * 20h) + 14h</i> */
  58889. + volatile uint32_t doepdma;
  58890. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  58891. + uint32_t unused;
  58892. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  58893. + * + (ep_num * 20h) + 1Ch</i> */
  58894. + uint32_t doepdmab;
  58895. +} dwc_otg_dev_out_ep_regs_t;
  58896. +
  58897. +/**
  58898. + * This union represents the bit fields in the Device EP Control
  58899. + * Register. Read the register into the <i>d32</i> member then
  58900. + * set/clear the bits using the <i>b</i>it elements.
  58901. + */
  58902. +typedef union depctl_data {
  58903. + /** raw register data */
  58904. + uint32_t d32;
  58905. + /** register bits */
  58906. + struct {
  58907. + /** Maximum Packet Size
  58908. + * IN/OUT EPn
  58909. + * IN/OUT EP0 - 2 bits
  58910. + * 2'b00: 64 Bytes
  58911. + * 2'b01: 32
  58912. + * 2'b10: 16
  58913. + * 2'b11: 8 */
  58914. + unsigned mps:11;
  58915. +#define DWC_DEP0CTL_MPS_64 0
  58916. +#define DWC_DEP0CTL_MPS_32 1
  58917. +#define DWC_DEP0CTL_MPS_16 2
  58918. +#define DWC_DEP0CTL_MPS_8 3
  58919. +
  58920. + /** Next Endpoint
  58921. + * IN EPn/IN EP0
  58922. + * OUT EPn/OUT EP0 - reserved */
  58923. + unsigned nextep:4;
  58924. +
  58925. + /** USB Active Endpoint */
  58926. + unsigned usbactep:1;
  58927. +
  58928. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  58929. + * This field contains the PID of the packet going to
  58930. + * be received or transmitted on this endpoint. The
  58931. + * application should program the PID of the first
  58932. + * packet going to be received or transmitted on this
  58933. + * endpoint , after the endpoint is
  58934. + * activated. Application use the SetD1PID and
  58935. + * SetD0PID fields of this register to program either
  58936. + * D0 or D1 PID.
  58937. + *
  58938. + * The encoding for this field is
  58939. + * - 0: D0
  58940. + * - 1: D1
  58941. + */
  58942. + unsigned dpid:1;
  58943. +
  58944. + /** NAK Status */
  58945. + unsigned naksts:1;
  58946. +
  58947. + /** Endpoint Type
  58948. + * 2'b00: Control
  58949. + * 2'b01: Isochronous
  58950. + * 2'b10: Bulk
  58951. + * 2'b11: Interrupt */
  58952. + unsigned eptype:2;
  58953. +
  58954. + /** Snoop Mode
  58955. + * OUT EPn/OUT EP0
  58956. + * IN EPn/IN EP0 - reserved */
  58957. + unsigned snp:1;
  58958. +
  58959. + /** Stall Handshake */
  58960. + unsigned stall:1;
  58961. +
  58962. + /** Tx Fifo Number
  58963. + * IN EPn/IN EP0
  58964. + * OUT EPn/OUT EP0 - reserved */
  58965. + unsigned txfnum:4;
  58966. +
  58967. + /** Clear NAK */
  58968. + unsigned cnak:1;
  58969. + /** Set NAK */
  58970. + unsigned snak:1;
  58971. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  58972. + * Writing to this field sets the Endpoint DPID (DPID)
  58973. + * field in this register to DATA0. Set Even
  58974. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  58975. + * Writing to this field sets the Even/Odd
  58976. + * (micro)frame (EO_FrNum) field to even (micro)
  58977. + * frame.
  58978. + */
  58979. + unsigned setd0pid:1;
  58980. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  58981. + * Writing to this field sets the Endpoint DPID (DPID)
  58982. + * field in this register to DATA1 Set Odd
  58983. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  58984. + * Writing to this field sets the Even/Odd
  58985. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  58986. + */
  58987. + unsigned setd1pid:1;
  58988. +
  58989. + /** Endpoint Disable */
  58990. + unsigned epdis:1;
  58991. + /** Endpoint Enable */
  58992. + unsigned epena:1;
  58993. + } b;
  58994. +} depctl_data_t;
  58995. +
  58996. +/**
  58997. + * This union represents the bit fields in the Device EP Transfer
  58998. + * Size Register. Read the register into the <i>d32</i> member then
  58999. + * set/clear the bits using the <i>b</i>it elements.
  59000. + */
  59001. +typedef union deptsiz_data {
  59002. + /** raw register data */
  59003. + uint32_t d32;
  59004. + /** register bits */
  59005. + struct {
  59006. + /** Transfer size */
  59007. + unsigned xfersize:19;
  59008. +/** Max packet count for EP (pow(2,10)-1) */
  59009. +#define MAX_PKT_CNT 1023
  59010. + /** Packet Count */
  59011. + unsigned pktcnt:10;
  59012. + /** Multi Count - Periodic IN endpoints */
  59013. + unsigned mc:2;
  59014. + unsigned reserved:1;
  59015. + } b;
  59016. +} deptsiz_data_t;
  59017. +
  59018. +/**
  59019. + * This union represents the bit fields in the Device EP 0 Transfer
  59020. + * Size Register. Read the register into the <i>d32</i> member then
  59021. + * set/clear the bits using the <i>b</i>it elements.
  59022. + */
  59023. +typedef union deptsiz0_data {
  59024. + /** raw register data */
  59025. + uint32_t d32;
  59026. + /** register bits */
  59027. + struct {
  59028. + /** Transfer size */
  59029. + unsigned xfersize:7;
  59030. + /** Reserved */
  59031. + unsigned reserved7_18:12;
  59032. + /** Packet Count */
  59033. + unsigned pktcnt:2;
  59034. + /** Reserved */
  59035. + unsigned reserved21_28:8;
  59036. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  59037. + unsigned supcnt:2;
  59038. + unsigned reserved31;
  59039. + } b;
  59040. +} deptsiz0_data_t;
  59041. +
  59042. +/////////////////////////////////////////////////
  59043. +// DMA Descriptor Specific Structures
  59044. +//
  59045. +
  59046. +/** Buffer status definitions */
  59047. +
  59048. +#define BS_HOST_READY 0x0
  59049. +#define BS_DMA_BUSY 0x1
  59050. +#define BS_DMA_DONE 0x2
  59051. +#define BS_HOST_BUSY 0x3
  59052. +
  59053. +/** Receive/Transmit status definitions */
  59054. +
  59055. +#define RTS_SUCCESS 0x0
  59056. +#define RTS_BUFFLUSH 0x1
  59057. +#define RTS_RESERVED 0x2
  59058. +#define RTS_BUFERR 0x3
  59059. +
  59060. +/**
  59061. + * This union represents the bit fields in the DMA Descriptor
  59062. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  59063. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  59064. + * <i>b_iso_in</i> elements.
  59065. + */
  59066. +typedef union dev_dma_desc_sts {
  59067. + /** raw register data */
  59068. + uint32_t d32;
  59069. + /** quadlet bits */
  59070. + struct {
  59071. + /** Received number of bytes */
  59072. + unsigned bytes:16;
  59073. + /** NAK bit - only for OUT EPs */
  59074. + unsigned nak:1;
  59075. + unsigned reserved17_22:6;
  59076. + /** Multiple Transfer - only for OUT EPs */
  59077. + unsigned mtrf:1;
  59078. + /** Setup Packet received - only for OUT EPs */
  59079. + unsigned sr:1;
  59080. + /** Interrupt On Complete */
  59081. + unsigned ioc:1;
  59082. + /** Short Packet */
  59083. + unsigned sp:1;
  59084. + /** Last */
  59085. + unsigned l:1;
  59086. + /** Receive Status */
  59087. + unsigned sts:2;
  59088. + /** Buffer Status */
  59089. + unsigned bs:2;
  59090. + } b;
  59091. +
  59092. +//#ifdef DWC_EN_ISOC
  59093. + /** iso out quadlet bits */
  59094. + struct {
  59095. + /** Received number of bytes */
  59096. + unsigned rxbytes:11;
  59097. +
  59098. + unsigned reserved11:1;
  59099. + /** Frame Number */
  59100. + unsigned framenum:11;
  59101. + /** Received ISO Data PID */
  59102. + unsigned pid:2;
  59103. + /** Interrupt On Complete */
  59104. + unsigned ioc:1;
  59105. + /** Short Packet */
  59106. + unsigned sp:1;
  59107. + /** Last */
  59108. + unsigned l:1;
  59109. + /** Receive Status */
  59110. + unsigned rxsts:2;
  59111. + /** Buffer Status */
  59112. + unsigned bs:2;
  59113. + } b_iso_out;
  59114. +
  59115. + /** iso in quadlet bits */
  59116. + struct {
  59117. + /** Transmited number of bytes */
  59118. + unsigned txbytes:12;
  59119. + /** Frame Number */
  59120. + unsigned framenum:11;
  59121. + /** Transmited ISO Data PID */
  59122. + unsigned pid:2;
  59123. + /** Interrupt On Complete */
  59124. + unsigned ioc:1;
  59125. + /** Short Packet */
  59126. + unsigned sp:1;
  59127. + /** Last */
  59128. + unsigned l:1;
  59129. + /** Transmit Status */
  59130. + unsigned txsts:2;
  59131. + /** Buffer Status */
  59132. + unsigned bs:2;
  59133. + } b_iso_in;
  59134. +//#endif /* DWC_EN_ISOC */
  59135. +} dev_dma_desc_sts_t;
  59136. +
  59137. +/**
  59138. + * DMA Descriptor structure
  59139. + *
  59140. + * DMA Descriptor structure contains two quadlets:
  59141. + * Status quadlet and Data buffer pointer.
  59142. + */
  59143. +typedef struct dwc_otg_dev_dma_desc {
  59144. + /** DMA Descriptor status quadlet */
  59145. + dev_dma_desc_sts_t status;
  59146. + /** DMA Descriptor data buffer pointer */
  59147. + uint32_t buf;
  59148. +} dwc_otg_dev_dma_desc_t;
  59149. +
  59150. +/**
  59151. + * The dwc_otg_dev_if structure contains information needed to manage
  59152. + * the DWC_otg controller acting in device mode. It represents the
  59153. + * programming view of the device-specific aspects of the controller.
  59154. + */
  59155. +typedef struct dwc_otg_dev_if {
  59156. + /** Pointer to device Global registers.
  59157. + * Device Global Registers starting at offset 800h
  59158. + */
  59159. + dwc_otg_device_global_regs_t *dev_global_regs;
  59160. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  59161. +
  59162. + /**
  59163. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  59164. + */
  59165. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  59166. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  59167. +#define DWC_EP_REG_OFFSET 0x20
  59168. +
  59169. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  59170. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  59171. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  59172. +
  59173. + /* Device configuration information */
  59174. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  59175. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  59176. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  59177. +
  59178. + /** Size of periodic FIFOs (Bytes) */
  59179. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  59180. +
  59181. + /** Size of Tx FIFOs (Bytes) */
  59182. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  59183. +
  59184. + /** Thresholding enable flags and length varaiables **/
  59185. + uint16_t rx_thr_en;
  59186. + uint16_t iso_tx_thr_en;
  59187. + uint16_t non_iso_tx_thr_en;
  59188. +
  59189. + uint16_t rx_thr_length;
  59190. + uint16_t tx_thr_length;
  59191. +
  59192. + /**
  59193. + * Pointers to the DMA Descriptors for EP0 Control
  59194. + * transfers (virtual and physical)
  59195. + */
  59196. +
  59197. + /** 2 descriptors for SETUP packets */
  59198. + dwc_dma_t dma_setup_desc_addr[2];
  59199. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  59200. +
  59201. + /** Pointer to Descriptor with latest SETUP packet */
  59202. + dwc_otg_dev_dma_desc_t *psetup;
  59203. +
  59204. + /** Index of current SETUP handler descriptor */
  59205. + uint32_t setup_desc_index;
  59206. +
  59207. + /** Descriptor for Data In or Status In phases */
  59208. + dwc_dma_t dma_in_desc_addr;
  59209. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  59210. +
  59211. + /** Descriptor for Data Out or Status Out phases */
  59212. + dwc_dma_t dma_out_desc_addr;
  59213. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  59214. +
  59215. + /** Setup Packet Detected - if set clear NAK when queueing */
  59216. + uint32_t spd;
  59217. + /** Isoc ep pointer on which incomplete happens */
  59218. + void *isoc_ep;
  59219. +
  59220. +} dwc_otg_dev_if_t;
  59221. +
  59222. +/////////////////////////////////////////////////
  59223. +// Host Mode Register Structures
  59224. +//
  59225. +/**
  59226. + * The Host Global Registers structure defines the size and relative
  59227. + * field offsets for the Host Mode Global Registers. Host Global
  59228. + * Registers offsets 400h-7FFh.
  59229. +*/
  59230. +typedef struct dwc_otg_host_global_regs {
  59231. + /** Host Configuration Register. <i>Offset: 400h</i> */
  59232. + volatile uint32_t hcfg;
  59233. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  59234. + volatile uint32_t hfir;
  59235. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  59236. + volatile uint32_t hfnum;
  59237. + /** Reserved. <i>Offset: 40Ch</i> */
  59238. + uint32_t reserved40C;
  59239. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  59240. + volatile uint32_t hptxsts;
  59241. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  59242. + volatile uint32_t haint;
  59243. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  59244. + volatile uint32_t haintmsk;
  59245. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  59246. + volatile uint32_t hflbaddr;
  59247. +} dwc_otg_host_global_regs_t;
  59248. +
  59249. +/**
  59250. + * This union represents the bit fields in the Host Configuration Register.
  59251. + * Read the register into the <i>d32</i> member then set/clear the bits using
  59252. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  59253. + */
  59254. +typedef union hcfg_data {
  59255. + /** raw register data */
  59256. + uint32_t d32;
  59257. +
  59258. + /** register bits */
  59259. + struct {
  59260. + /** FS/LS Phy Clock Select */
  59261. + unsigned fslspclksel:2;
  59262. +#define DWC_HCFG_30_60_MHZ 0
  59263. +#define DWC_HCFG_48_MHZ 1
  59264. +#define DWC_HCFG_6_MHZ 2
  59265. +
  59266. + /** FS/LS Only Support */
  59267. + unsigned fslssupp:1;
  59268. + unsigned reserved3_6:4;
  59269. + /** Enable 32-KHz Suspend Mode */
  59270. + unsigned ena32khzs:1;
  59271. + /** Resume Validation Periiod */
  59272. + unsigned resvalid:8;
  59273. + unsigned reserved16_22:7;
  59274. + /** Enable Scatter/gather DMA in Host mode */
  59275. + unsigned descdma:1;
  59276. + /** Frame List Entries */
  59277. + unsigned frlisten:2;
  59278. + /** Enable Periodic Scheduling */
  59279. + unsigned perschedena:1;
  59280. + unsigned reserved27_30:4;
  59281. + unsigned modechtimen:1;
  59282. + } b;
  59283. +} hcfg_data_t;
  59284. +
  59285. +/**
  59286. + * This union represents the bit fields in the Host Frame Remaing/Number
  59287. + * Register.
  59288. + */
  59289. +typedef union hfir_data {
  59290. + /** raw register data */
  59291. + uint32_t d32;
  59292. +
  59293. + /** register bits */
  59294. + struct {
  59295. + unsigned frint:16;
  59296. + unsigned hfirrldctrl:1;
  59297. + unsigned reserved:15;
  59298. + } b;
  59299. +} hfir_data_t;
  59300. +
  59301. +/**
  59302. + * This union represents the bit fields in the Host Frame Remaing/Number
  59303. + * Register.
  59304. + */
  59305. +typedef union hfnum_data {
  59306. + /** raw register data */
  59307. + uint32_t d32;
  59308. +
  59309. + /** register bits */
  59310. + struct {
  59311. + unsigned frnum:16;
  59312. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  59313. + unsigned frrem:16;
  59314. + } b;
  59315. +} hfnum_data_t;
  59316. +
  59317. +typedef union hptxsts_data {
  59318. + /** raw register data */
  59319. + uint32_t d32;
  59320. +
  59321. + /** register bits */
  59322. + struct {
  59323. + unsigned ptxfspcavail:16;
  59324. + unsigned ptxqspcavail:8;
  59325. + /** Top of the Periodic Transmit Request Queue
  59326. + * - bit 24 - Terminate (last entry for the selected channel)
  59327. + * - bits 26:25 - Token Type
  59328. + * - 2'b00 - Zero length
  59329. + * - 2'b01 - Ping
  59330. + * - 2'b10 - Disable
  59331. + * - bits 30:27 - Channel Number
  59332. + * - bit 31 - Odd/even microframe
  59333. + */
  59334. + unsigned ptxqtop_terminate:1;
  59335. + unsigned ptxqtop_token:2;
  59336. + unsigned ptxqtop_chnum:4;
  59337. + unsigned ptxqtop_odd:1;
  59338. + } b;
  59339. +} hptxsts_data_t;
  59340. +
  59341. +/**
  59342. + * This union represents the bit fields in the Host Port Control and Status
  59343. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59344. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59345. + * hprt0 register.
  59346. + */
  59347. +typedef union hprt0_data {
  59348. + /** raw register data */
  59349. + uint32_t d32;
  59350. + /** register bits */
  59351. + struct {
  59352. + unsigned prtconnsts:1;
  59353. + unsigned prtconndet:1;
  59354. + unsigned prtena:1;
  59355. + unsigned prtenchng:1;
  59356. + unsigned prtovrcurract:1;
  59357. + unsigned prtovrcurrchng:1;
  59358. + unsigned prtres:1;
  59359. + unsigned prtsusp:1;
  59360. + unsigned prtrst:1;
  59361. + unsigned reserved9:1;
  59362. + unsigned prtlnsts:2;
  59363. + unsigned prtpwr:1;
  59364. + unsigned prttstctl:4;
  59365. + unsigned prtspd:2;
  59366. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  59367. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  59368. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  59369. + unsigned reserved19_31:13;
  59370. + } b;
  59371. +} hprt0_data_t;
  59372. +
  59373. +/**
  59374. + * This union represents the bit fields in the Host All Interrupt
  59375. + * Register.
  59376. + */
  59377. +typedef union haint_data {
  59378. + /** raw register data */
  59379. + uint32_t d32;
  59380. + /** register bits */
  59381. + struct {
  59382. + unsigned ch0:1;
  59383. + unsigned ch1:1;
  59384. + unsigned ch2:1;
  59385. + unsigned ch3:1;
  59386. + unsigned ch4:1;
  59387. + unsigned ch5:1;
  59388. + unsigned ch6:1;
  59389. + unsigned ch7:1;
  59390. + unsigned ch8:1;
  59391. + unsigned ch9:1;
  59392. + unsigned ch10:1;
  59393. + unsigned ch11:1;
  59394. + unsigned ch12:1;
  59395. + unsigned ch13:1;
  59396. + unsigned ch14:1;
  59397. + unsigned ch15:1;
  59398. + unsigned reserved:16;
  59399. + } b;
  59400. +
  59401. + struct {
  59402. + unsigned chint:16;
  59403. + unsigned reserved:16;
  59404. + } b2;
  59405. +} haint_data_t;
  59406. +
  59407. +/**
  59408. + * This union represents the bit fields in the Host All Interrupt
  59409. + * Register.
  59410. + */
  59411. +typedef union haintmsk_data {
  59412. + /** raw register data */
  59413. + uint32_t d32;
  59414. + /** register bits */
  59415. + struct {
  59416. + unsigned ch0:1;
  59417. + unsigned ch1:1;
  59418. + unsigned ch2:1;
  59419. + unsigned ch3:1;
  59420. + unsigned ch4:1;
  59421. + unsigned ch5:1;
  59422. + unsigned ch6:1;
  59423. + unsigned ch7:1;
  59424. + unsigned ch8:1;
  59425. + unsigned ch9:1;
  59426. + unsigned ch10:1;
  59427. + unsigned ch11:1;
  59428. + unsigned ch12:1;
  59429. + unsigned ch13:1;
  59430. + unsigned ch14:1;
  59431. + unsigned ch15:1;
  59432. + unsigned reserved:16;
  59433. + } b;
  59434. +
  59435. + struct {
  59436. + unsigned chint:16;
  59437. + unsigned reserved:16;
  59438. + } b2;
  59439. +} haintmsk_data_t;
  59440. +
  59441. +/**
  59442. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  59443. + */
  59444. +typedef struct dwc_otg_hc_regs {
  59445. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  59446. + volatile uint32_t hcchar;
  59447. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  59448. + volatile uint32_t hcsplt;
  59449. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  59450. + volatile uint32_t hcint;
  59451. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  59452. + volatile uint32_t hcintmsk;
  59453. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  59454. + volatile uint32_t hctsiz;
  59455. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  59456. + volatile uint32_t hcdma;
  59457. + volatile uint32_t reserved;
  59458. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  59459. + volatile uint32_t hcdmab;
  59460. +} dwc_otg_hc_regs_t;
  59461. +
  59462. +/**
  59463. + * This union represents the bit fields in the Host Channel Characteristics
  59464. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59465. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59466. + * hcchar register.
  59467. + */
  59468. +typedef union hcchar_data {
  59469. + /** raw register data */
  59470. + uint32_t d32;
  59471. +
  59472. + /** register bits */
  59473. + struct {
  59474. + /** Maximum packet size in bytes */
  59475. + unsigned mps:11;
  59476. +
  59477. + /** Endpoint number */
  59478. + unsigned epnum:4;
  59479. +
  59480. + /** 0: OUT, 1: IN */
  59481. + unsigned epdir:1;
  59482. +
  59483. + unsigned reserved:1;
  59484. +
  59485. + /** 0: Full/high speed device, 1: Low speed device */
  59486. + unsigned lspddev:1;
  59487. +
  59488. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  59489. + unsigned eptype:2;
  59490. +
  59491. + /** Packets per frame for periodic transfers. 0 is reserved. */
  59492. + unsigned multicnt:2;
  59493. +
  59494. + /** Device address */
  59495. + unsigned devaddr:7;
  59496. +
  59497. + /**
  59498. + * Frame to transmit periodic transaction.
  59499. + * 0: even, 1: odd
  59500. + */
  59501. + unsigned oddfrm:1;
  59502. +
  59503. + /** Channel disable */
  59504. + unsigned chdis:1;
  59505. +
  59506. + /** Channel enable */
  59507. + unsigned chen:1;
  59508. + } b;
  59509. +} hcchar_data_t;
  59510. +
  59511. +typedef union hcsplt_data {
  59512. + /** raw register data */
  59513. + uint32_t d32;
  59514. +
  59515. + /** register bits */
  59516. + struct {
  59517. + /** Port Address */
  59518. + unsigned prtaddr:7;
  59519. +
  59520. + /** Hub Address */
  59521. + unsigned hubaddr:7;
  59522. +
  59523. + /** Transaction Position */
  59524. + unsigned xactpos:2;
  59525. +#define DWC_HCSPLIT_XACTPOS_MID 0
  59526. +#define DWC_HCSPLIT_XACTPOS_END 1
  59527. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  59528. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  59529. +
  59530. + /** Do Complete Split */
  59531. + unsigned compsplt:1;
  59532. +
  59533. + /** Reserved */
  59534. + unsigned reserved:14;
  59535. +
  59536. + /** Split Enble */
  59537. + unsigned spltena:1;
  59538. + } b;
  59539. +} hcsplt_data_t;
  59540. +
  59541. +/**
  59542. + * This union represents the bit fields in the Host All Interrupt
  59543. + * Register.
  59544. + */
  59545. +typedef union hcint_data {
  59546. + /** raw register data */
  59547. + uint32_t d32;
  59548. + /** register bits */
  59549. + struct {
  59550. + /** Transfer Complete */
  59551. + unsigned xfercomp:1;
  59552. + /** Channel Halted */
  59553. + unsigned chhltd:1;
  59554. + /** AHB Error */
  59555. + unsigned ahberr:1;
  59556. + /** STALL Response Received */
  59557. + unsigned stall:1;
  59558. + /** NAK Response Received */
  59559. + unsigned nak:1;
  59560. + /** ACK Response Received */
  59561. + unsigned ack:1;
  59562. + /** NYET Response Received */
  59563. + unsigned nyet:1;
  59564. + /** Transaction Err */
  59565. + unsigned xacterr:1;
  59566. + /** Babble Error */
  59567. + unsigned bblerr:1;
  59568. + /** Frame Overrun */
  59569. + unsigned frmovrun:1;
  59570. + /** Data Toggle Error */
  59571. + unsigned datatglerr:1;
  59572. + /** Buffer Not Available (only for DDMA mode) */
  59573. + unsigned bna:1;
  59574. + /** Exessive transaction error (only for DDMA mode) */
  59575. + unsigned xcs_xact:1;
  59576. + /** Frame List Rollover interrupt */
  59577. + unsigned frm_list_roll:1;
  59578. + /** Reserved */
  59579. + unsigned reserved14_31:18;
  59580. + } b;
  59581. +} hcint_data_t;
  59582. +
  59583. +/**
  59584. + * This union represents the bit fields in the Host Channel Interrupt Mask
  59585. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59586. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59587. + * hcintmsk register.
  59588. + */
  59589. +typedef union hcintmsk_data {
  59590. + /** raw register data */
  59591. + uint32_t d32;
  59592. +
  59593. + /** register bits */
  59594. + struct {
  59595. + unsigned xfercompl:1;
  59596. + unsigned chhltd:1;
  59597. + unsigned ahberr:1;
  59598. + unsigned stall:1;
  59599. + unsigned nak:1;
  59600. + unsigned ack:1;
  59601. + unsigned nyet:1;
  59602. + unsigned xacterr:1;
  59603. + unsigned bblerr:1;
  59604. + unsigned frmovrun:1;
  59605. + unsigned datatglerr:1;
  59606. + unsigned bna:1;
  59607. + unsigned xcs_xact:1;
  59608. + unsigned frm_list_roll:1;
  59609. + unsigned reserved14_31:18;
  59610. + } b;
  59611. +} hcintmsk_data_t;
  59612. +
  59613. +/**
  59614. + * This union represents the bit fields in the Host Channel Transfer Size
  59615. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59616. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59617. + * hcchar register.
  59618. + */
  59619. +
  59620. +typedef union hctsiz_data {
  59621. + /** raw register data */
  59622. + uint32_t d32;
  59623. +
  59624. + /** register bits */
  59625. + struct {
  59626. + /** Total transfer size in bytes */
  59627. + unsigned xfersize:19;
  59628. +
  59629. + /** Data packets to transfer */
  59630. + unsigned pktcnt:10;
  59631. +
  59632. + /**
  59633. + * Packet ID for next data packet
  59634. + * 0: DATA0
  59635. + * 1: DATA2
  59636. + * 2: DATA1
  59637. + * 3: MDATA (non-Control), SETUP (Control)
  59638. + */
  59639. + unsigned pid:2;
  59640. +#define DWC_HCTSIZ_DATA0 0
  59641. +#define DWC_HCTSIZ_DATA1 2
  59642. +#define DWC_HCTSIZ_DATA2 1
  59643. +#define DWC_HCTSIZ_MDATA 3
  59644. +#define DWC_HCTSIZ_SETUP 3
  59645. +
  59646. + /** Do PING protocol when 1 */
  59647. + unsigned dopng:1;
  59648. + } b;
  59649. +
  59650. + /** register bits */
  59651. + struct {
  59652. + /** Scheduling information */
  59653. + unsigned schinfo:8;
  59654. +
  59655. + /** Number of transfer descriptors.
  59656. + * Max value:
  59657. + * 64 in general,
  59658. + * 256 only for HS isochronous endpoint.
  59659. + */
  59660. + unsigned ntd:8;
  59661. +
  59662. + /** Data packets to transfer */
  59663. + unsigned reserved16_28:13;
  59664. +
  59665. + /**
  59666. + * Packet ID for next data packet
  59667. + * 0: DATA0
  59668. + * 1: DATA2
  59669. + * 2: DATA1
  59670. + * 3: MDATA (non-Control)
  59671. + */
  59672. + unsigned pid:2;
  59673. +
  59674. + /** Do PING protocol when 1 */
  59675. + unsigned dopng:1;
  59676. + } b_ddma;
  59677. +} hctsiz_data_t;
  59678. +
  59679. +/**
  59680. + * This union represents the bit fields in the Host DMA Address
  59681. + * Register used in Descriptor DMA mode.
  59682. + */
  59683. +typedef union hcdma_data {
  59684. + /** raw register data */
  59685. + uint32_t d32;
  59686. + /** register bits */
  59687. + struct {
  59688. + unsigned reserved0_2:3;
  59689. + /** Current Transfer Descriptor. Not used for ISOC */
  59690. + unsigned ctd:8;
  59691. + /** Start Address of Descriptor List */
  59692. + unsigned dma_addr:21;
  59693. + } b;
  59694. +} hcdma_data_t;
  59695. +
  59696. +/**
  59697. + * This union represents the bit fields in the DMA Descriptor
  59698. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  59699. + * set/clear the bits using the <i>b</i>it elements.
  59700. + */
  59701. +typedef union host_dma_desc_sts {
  59702. + /** raw register data */
  59703. + uint32_t d32;
  59704. + /** quadlet bits */
  59705. +
  59706. + /* for non-isochronous */
  59707. + struct {
  59708. + /** Number of bytes */
  59709. + unsigned n_bytes:17;
  59710. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  59711. + unsigned qtd_offset:6;
  59712. + /**
  59713. + * Set to request the core to jump to alternate QTD if
  59714. + * Short Packet received - only for IN EPs
  59715. + */
  59716. + unsigned a_qtd:1;
  59717. + /**
  59718. + * Setup Packet bit. When set indicates that buffer contains
  59719. + * setup packet.
  59720. + */
  59721. + unsigned sup:1;
  59722. + /** Interrupt On Complete */
  59723. + unsigned ioc:1;
  59724. + /** End of List */
  59725. + unsigned eol:1;
  59726. + unsigned reserved27:1;
  59727. + /** Rx/Tx Status */
  59728. + unsigned sts:2;
  59729. +#define DMA_DESC_STS_PKTERR 1
  59730. + unsigned reserved30:1;
  59731. + /** Active Bit */
  59732. + unsigned a:1;
  59733. + } b;
  59734. + /* for isochronous */
  59735. + struct {
  59736. + /** Number of bytes */
  59737. + unsigned n_bytes:12;
  59738. + unsigned reserved12_24:13;
  59739. + /** Interrupt On Complete */
  59740. + unsigned ioc:1;
  59741. + unsigned reserved26_27:2;
  59742. + /** Rx/Tx Status */
  59743. + unsigned sts:2;
  59744. + unsigned reserved30:1;
  59745. + /** Active Bit */
  59746. + unsigned a:1;
  59747. + } b_isoc;
  59748. +} host_dma_desc_sts_t;
  59749. +
  59750. +#define MAX_DMA_DESC_SIZE 131071
  59751. +#define MAX_DMA_DESC_NUM_GENERIC 64
  59752. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  59753. +#define MAX_FRLIST_EN_NUM 64
  59754. +/**
  59755. + * Host-mode DMA Descriptor structure
  59756. + *
  59757. + * DMA Descriptor structure contains two quadlets:
  59758. + * Status quadlet and Data buffer pointer.
  59759. + */
  59760. +typedef struct dwc_otg_host_dma_desc {
  59761. + /** DMA Descriptor status quadlet */
  59762. + host_dma_desc_sts_t status;
  59763. + /** DMA Descriptor data buffer pointer */
  59764. + uint32_t buf;
  59765. +} dwc_otg_host_dma_desc_t;
  59766. +
  59767. +/** OTG Host Interface Structure.
  59768. + *
  59769. + * The OTG Host Interface Structure structure contains information
  59770. + * needed to manage the DWC_otg controller acting in host mode. It
  59771. + * represents the programming view of the host-specific aspects of the
  59772. + * controller.
  59773. + */
  59774. +typedef struct dwc_otg_host_if {
  59775. + /** Host Global Registers starting at offset 400h.*/
  59776. + dwc_otg_host_global_regs_t *host_global_regs;
  59777. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  59778. +
  59779. + /** Host Port 0 Control and Status Register */
  59780. + volatile uint32_t *hprt0;
  59781. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  59782. +
  59783. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  59784. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  59785. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  59786. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  59787. +
  59788. + /* Host configuration information */
  59789. + /** Number of Host Channels (range: 1-16) */
  59790. + uint8_t num_host_channels;
  59791. + /** Periodic EPs supported (0: no, 1: yes) */
  59792. + uint8_t perio_eps_supported;
  59793. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  59794. + uint16_t perio_tx_fifo_size;
  59795. +
  59796. +} dwc_otg_host_if_t;
  59797. +
  59798. +/**
  59799. + * This union represents the bit fields in the Power and Clock Gating Control
  59800. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59801. + * bits using the <i>b</i>it elements.
  59802. + */
  59803. +typedef union pcgcctl_data {
  59804. + /** raw register data */
  59805. + uint32_t d32;
  59806. +
  59807. + /** register bits */
  59808. + struct {
  59809. + /** Stop Pclk */
  59810. + unsigned stoppclk:1;
  59811. + /** Gate Hclk */
  59812. + unsigned gatehclk:1;
  59813. + /** Power Clamp */
  59814. + unsigned pwrclmp:1;
  59815. + /** Reset Power Down Modules */
  59816. + unsigned rstpdwnmodule:1;
  59817. + /** Reserved */
  59818. + unsigned reserved:1;
  59819. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  59820. + unsigned enbl_sleep_gating:1;
  59821. + /** PHY In Sleep (PhySleep) */
  59822. + unsigned phy_in_sleep:1;
  59823. + /** Deep Sleep*/
  59824. + unsigned deep_sleep:1;
  59825. + unsigned resetaftsusp:1;
  59826. + unsigned restoremode:1;
  59827. + unsigned enbl_extnd_hiber:1;
  59828. + unsigned extnd_hiber_pwrclmp:1;
  59829. + unsigned extnd_hiber_switch:1;
  59830. + unsigned ess_reg_restored:1;
  59831. + unsigned prt_clk_sel:2;
  59832. + unsigned port_power:1;
  59833. + unsigned max_xcvrselect:2;
  59834. + unsigned max_termsel:1;
  59835. + unsigned mac_dev_addr:7;
  59836. + unsigned p2hd_dev_enum_spd:2;
  59837. + unsigned p2hd_prt_spd:2;
  59838. + unsigned if_dev_mode:1;
  59839. + } b;
  59840. +} pcgcctl_data_t;
  59841. +
  59842. +/**
  59843. + * This union represents the bit fields in the Global Data FIFO Software
  59844. + * Configuration Register. Read the register into the <i>d32</i> member then
  59845. + * set/clear the bits using the <i>b</i>it elements.
  59846. + */
  59847. +typedef union gdfifocfg_data {
  59848. + /* raw register data */
  59849. + uint32_t d32;
  59850. + /** register bits */
  59851. + struct {
  59852. + /** OTG Data FIFO depth */
  59853. + unsigned gdfifocfg:16;
  59854. + /** Start address of EP info controller */
  59855. + unsigned epinfobase:16;
  59856. + } b;
  59857. +} gdfifocfg_data_t;
  59858. +
  59859. +/**
  59860. + * This union represents the bit fields in the Global Power Down Register
  59861. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59862. + * bits using the <i>b</i>it elements.
  59863. + */
  59864. +typedef union gpwrdn_data {
  59865. + /* raw register data */
  59866. + uint32_t d32;
  59867. +
  59868. + /** register bits */
  59869. + struct {
  59870. + /** PMU Interrupt Select */
  59871. + unsigned pmuintsel:1;
  59872. + /** PMU Active */
  59873. + unsigned pmuactv:1;
  59874. + /** Restore */
  59875. + unsigned restore:1;
  59876. + /** Power Down Clamp */
  59877. + unsigned pwrdnclmp:1;
  59878. + /** Power Down Reset */
  59879. + unsigned pwrdnrstn:1;
  59880. + /** Power Down Switch */
  59881. + unsigned pwrdnswtch:1;
  59882. + /** Disable VBUS */
  59883. + unsigned dis_vbus:1;
  59884. + /** Line State Change */
  59885. + unsigned lnstschng:1;
  59886. + /** Line state change mask */
  59887. + unsigned lnstchng_msk:1;
  59888. + /** Reset Detected */
  59889. + unsigned rst_det:1;
  59890. + /** Reset Detect mask */
  59891. + unsigned rst_det_msk:1;
  59892. + /** Disconnect Detected */
  59893. + unsigned disconn_det:1;
  59894. + /** Disconnect Detect mask */
  59895. + unsigned disconn_det_msk:1;
  59896. + /** Connect Detected*/
  59897. + unsigned connect_det:1;
  59898. + /** Connect Detected Mask*/
  59899. + unsigned connect_det_msk:1;
  59900. + /** SRP Detected */
  59901. + unsigned srp_det:1;
  59902. + /** SRP Detect mask */
  59903. + unsigned srp_det_msk:1;
  59904. + /** Status Change Interrupt */
  59905. + unsigned sts_chngint:1;
  59906. + /** Status Change Interrupt Mask */
  59907. + unsigned sts_chngint_msk:1;
  59908. + /** Line State */
  59909. + unsigned linestate:2;
  59910. + /** Indicates current mode(status of IDDIG signal) */
  59911. + unsigned idsts:1;
  59912. + /** B Session Valid signal status*/
  59913. + unsigned bsessvld:1;
  59914. + /** ADP Event Detected */
  59915. + unsigned adp_int:1;
  59916. + /** Multi Valued ID pin */
  59917. + unsigned mult_val_id_bc:5;
  59918. + /** Reserved 24_31 */
  59919. + unsigned reserved29_31:3;
  59920. + } b;
  59921. +} gpwrdn_data_t;
  59922. +
  59923. +#endif
  59924. --- /dev/null
  59925. +++ b/drivers/usb/host/dwc_otg/test/Makefile
  59926. @@ -0,0 +1,16 @@
  59927. +
  59928. +PERL=/usr/bin/perl
  59929. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  59930. +
  59931. +.PHONY : test
  59932. +test : perl_tests
  59933. +
  59934. +perl_tests :
  59935. + @echo
  59936. + @echo Running perl tests
  59937. + @for test in $(PL_TESTS); do \
  59938. + if $(PERL) ./$$test ; then \
  59939. + echo "=======> $$test, PASSED" ; \
  59940. + else echo "=======> $$test, FAILED" ; \
  59941. + fi \
  59942. + done
  59943. --- /dev/null
  59944. +++ b/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  59945. @@ -0,0 +1,337 @@
  59946. +package dwc_otg_test;
  59947. +
  59948. +use strict;
  59949. +use Exporter ();
  59950. +
  59951. +use vars qw(@ISA @EXPORT
  59952. +$sysfsdir $paramdir $errors $params
  59953. +);
  59954. +
  59955. +@ISA = qw(Exporter);
  59956. +
  59957. +#
  59958. +# Globals
  59959. +#
  59960. +$sysfsdir = "/sys/devices/lm0";
  59961. +$paramdir = "/sys/module/dwc_otg";
  59962. +$errors = 0;
  59963. +
  59964. +$params = [
  59965. + {
  59966. + NAME => "otg_cap",
  59967. + DEFAULT => 0,
  59968. + ENUM => [],
  59969. + LOW => 0,
  59970. + HIGH => 2
  59971. + },
  59972. + {
  59973. + NAME => "dma_enable",
  59974. + DEFAULT => 0,
  59975. + ENUM => [],
  59976. + LOW => 0,
  59977. + HIGH => 1
  59978. + },
  59979. + {
  59980. + NAME => "dma_burst_size",
  59981. + DEFAULT => 32,
  59982. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  59983. + LOW => 1,
  59984. + HIGH => 256
  59985. + },
  59986. + {
  59987. + NAME => "host_speed",
  59988. + DEFAULT => 0,
  59989. + ENUM => [],
  59990. + LOW => 0,
  59991. + HIGH => 1
  59992. + },
  59993. + {
  59994. + NAME => "host_support_fs_ls_low_power",
  59995. + DEFAULT => 0,
  59996. + ENUM => [],
  59997. + LOW => 0,
  59998. + HIGH => 1
  59999. + },
  60000. + {
  60001. + NAME => "host_ls_low_power_phy_clk",
  60002. + DEFAULT => 0,
  60003. + ENUM => [],
  60004. + LOW => 0,
  60005. + HIGH => 1
  60006. + },
  60007. + {
  60008. + NAME => "dev_speed",
  60009. + DEFAULT => 0,
  60010. + ENUM => [],
  60011. + LOW => 0,
  60012. + HIGH => 1
  60013. + },
  60014. + {
  60015. + NAME => "enable_dynamic_fifo",
  60016. + DEFAULT => 1,
  60017. + ENUM => [],
  60018. + LOW => 0,
  60019. + HIGH => 1
  60020. + },
  60021. + {
  60022. + NAME => "data_fifo_size",
  60023. + DEFAULT => 8192,
  60024. + ENUM => [],
  60025. + LOW => 32,
  60026. + HIGH => 32768
  60027. + },
  60028. + {
  60029. + NAME => "dev_rx_fifo_size",
  60030. + DEFAULT => 1064,
  60031. + ENUM => [],
  60032. + LOW => 16,
  60033. + HIGH => 32768
  60034. + },
  60035. + {
  60036. + NAME => "dev_nperio_tx_fifo_size",
  60037. + DEFAULT => 1024,
  60038. + ENUM => [],
  60039. + LOW => 16,
  60040. + HIGH => 32768
  60041. + },
  60042. + {
  60043. + NAME => "dev_perio_tx_fifo_size_1",
  60044. + DEFAULT => 256,
  60045. + ENUM => [],
  60046. + LOW => 4,
  60047. + HIGH => 768
  60048. + },
  60049. + {
  60050. + NAME => "dev_perio_tx_fifo_size_2",
  60051. + DEFAULT => 256,
  60052. + ENUM => [],
  60053. + LOW => 4,
  60054. + HIGH => 768
  60055. + },
  60056. + {
  60057. + NAME => "dev_perio_tx_fifo_size_3",
  60058. + DEFAULT => 256,
  60059. + ENUM => [],
  60060. + LOW => 4,
  60061. + HIGH => 768
  60062. + },
  60063. + {
  60064. + NAME => "dev_perio_tx_fifo_size_4",
  60065. + DEFAULT => 256,
  60066. + ENUM => [],
  60067. + LOW => 4,
  60068. + HIGH => 768
  60069. + },
  60070. + {
  60071. + NAME => "dev_perio_tx_fifo_size_5",
  60072. + DEFAULT => 256,
  60073. + ENUM => [],
  60074. + LOW => 4,
  60075. + HIGH => 768
  60076. + },
  60077. + {
  60078. + NAME => "dev_perio_tx_fifo_size_6",
  60079. + DEFAULT => 256,
  60080. + ENUM => [],
  60081. + LOW => 4,
  60082. + HIGH => 768
  60083. + },
  60084. + {
  60085. + NAME => "dev_perio_tx_fifo_size_7",
  60086. + DEFAULT => 256,
  60087. + ENUM => [],
  60088. + LOW => 4,
  60089. + HIGH => 768
  60090. + },
  60091. + {
  60092. + NAME => "dev_perio_tx_fifo_size_8",
  60093. + DEFAULT => 256,
  60094. + ENUM => [],
  60095. + LOW => 4,
  60096. + HIGH => 768
  60097. + },
  60098. + {
  60099. + NAME => "dev_perio_tx_fifo_size_9",
  60100. + DEFAULT => 256,
  60101. + ENUM => [],
  60102. + LOW => 4,
  60103. + HIGH => 768
  60104. + },
  60105. + {
  60106. + NAME => "dev_perio_tx_fifo_size_10",
  60107. + DEFAULT => 256,
  60108. + ENUM => [],
  60109. + LOW => 4,
  60110. + HIGH => 768
  60111. + },
  60112. + {
  60113. + NAME => "dev_perio_tx_fifo_size_11",
  60114. + DEFAULT => 256,
  60115. + ENUM => [],
  60116. + LOW => 4,
  60117. + HIGH => 768
  60118. + },
  60119. + {
  60120. + NAME => "dev_perio_tx_fifo_size_12",
  60121. + DEFAULT => 256,
  60122. + ENUM => [],
  60123. + LOW => 4,
  60124. + HIGH => 768
  60125. + },
  60126. + {
  60127. + NAME => "dev_perio_tx_fifo_size_13",
  60128. + DEFAULT => 256,
  60129. + ENUM => [],
  60130. + LOW => 4,
  60131. + HIGH => 768
  60132. + },
  60133. + {
  60134. + NAME => "dev_perio_tx_fifo_size_14",
  60135. + DEFAULT => 256,
  60136. + ENUM => [],
  60137. + LOW => 4,
  60138. + HIGH => 768
  60139. + },
  60140. + {
  60141. + NAME => "dev_perio_tx_fifo_size_15",
  60142. + DEFAULT => 256,
  60143. + ENUM => [],
  60144. + LOW => 4,
  60145. + HIGH => 768
  60146. + },
  60147. + {
  60148. + NAME => "host_rx_fifo_size",
  60149. + DEFAULT => 1024,
  60150. + ENUM => [],
  60151. + LOW => 16,
  60152. + HIGH => 32768
  60153. + },
  60154. + {
  60155. + NAME => "host_nperio_tx_fifo_size",
  60156. + DEFAULT => 1024,
  60157. + ENUM => [],
  60158. + LOW => 16,
  60159. + HIGH => 32768
  60160. + },
  60161. + {
  60162. + NAME => "host_perio_tx_fifo_size",
  60163. + DEFAULT => 1024,
  60164. + ENUM => [],
  60165. + LOW => 16,
  60166. + HIGH => 32768
  60167. + },
  60168. + {
  60169. + NAME => "max_transfer_size",
  60170. + DEFAULT => 65535,
  60171. + ENUM => [],
  60172. + LOW => 2047,
  60173. + HIGH => 65535
  60174. + },
  60175. + {
  60176. + NAME => "max_packet_count",
  60177. + DEFAULT => 511,
  60178. + ENUM => [],
  60179. + LOW => 15,
  60180. + HIGH => 511
  60181. + },
  60182. + {
  60183. + NAME => "host_channels",
  60184. + DEFAULT => 12,
  60185. + ENUM => [],
  60186. + LOW => 1,
  60187. + HIGH => 16
  60188. + },
  60189. + {
  60190. + NAME => "dev_endpoints",
  60191. + DEFAULT => 6,
  60192. + ENUM => [],
  60193. + LOW => 1,
  60194. + HIGH => 15
  60195. + },
  60196. + {
  60197. + NAME => "phy_type",
  60198. + DEFAULT => 1,
  60199. + ENUM => [],
  60200. + LOW => 0,
  60201. + HIGH => 2
  60202. + },
  60203. + {
  60204. + NAME => "phy_utmi_width",
  60205. + DEFAULT => 16,
  60206. + ENUM => [8, 16],
  60207. + LOW => 8,
  60208. + HIGH => 16
  60209. + },
  60210. + {
  60211. + NAME => "phy_ulpi_ddr",
  60212. + DEFAULT => 0,
  60213. + ENUM => [],
  60214. + LOW => 0,
  60215. + HIGH => 1
  60216. + },
  60217. + ];
  60218. +
  60219. +
  60220. +#
  60221. +#
  60222. +sub check_arch {
  60223. + $_ = `uname -m`;
  60224. + chomp;
  60225. + unless (m/armv4tl/) {
  60226. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  60227. + return 0;
  60228. + }
  60229. + return 1;
  60230. +}
  60231. +
  60232. +#
  60233. +#
  60234. +sub load_module {
  60235. + my $params = shift;
  60236. + print "\nRemoving Module\n";
  60237. + system "rmmod dwc_otg";
  60238. + print "Loading Module\n";
  60239. + if ($params ne "") {
  60240. + print "Module Parameters: $params\n";
  60241. + }
  60242. + if (system("modprobe dwc_otg $params")) {
  60243. + warn "Unable to load module\n";
  60244. + return 0;
  60245. + }
  60246. + return 1;
  60247. +}
  60248. +
  60249. +#
  60250. +#
  60251. +sub test_status {
  60252. + my $arg = shift;
  60253. +
  60254. + print "\n";
  60255. +
  60256. + if (defined $arg) {
  60257. + warn "WARNING: $arg\n";
  60258. + }
  60259. +
  60260. + if ($errors > 0) {
  60261. + warn "TEST FAILED with $errors errors\n";
  60262. + return 0;
  60263. + } else {
  60264. + print "TEST PASSED\n";
  60265. + return 0 if (defined $arg);
  60266. + }
  60267. + return 1;
  60268. +}
  60269. +
  60270. +#
  60271. +#
  60272. +@EXPORT = qw(
  60273. +$sysfsdir
  60274. +$paramdir
  60275. +$params
  60276. +$errors
  60277. +check_arch
  60278. +load_module
  60279. +test_status
  60280. +);
  60281. +
  60282. +1;
  60283. --- /dev/null
  60284. +++ b/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  60285. @@ -0,0 +1,133 @@
  60286. +#!/usr/bin/perl -w
  60287. +#
  60288. +# Run this program on the integrator.
  60289. +#
  60290. +# - Tests module parameter default values.
  60291. +# - Tests setting of valid module parameter values via modprobe.
  60292. +# - Tests invalid module parameter values.
  60293. +# -----------------------------------------------------------------------------
  60294. +use strict;
  60295. +use dwc_otg_test;
  60296. +
  60297. +check_arch() or die;
  60298. +
  60299. +#
  60300. +#
  60301. +sub test {
  60302. + my ($param,$expected) = @_;
  60303. + my $value = get($param);
  60304. +
  60305. + if ($value == $expected) {
  60306. + print "$param = $value, okay\n";
  60307. + }
  60308. +
  60309. + else {
  60310. + warn "ERROR: value of $param != $expected, $value\n";
  60311. + $errors ++;
  60312. + }
  60313. +}
  60314. +
  60315. +#
  60316. +#
  60317. +sub get {
  60318. + my $param = shift;
  60319. + my $tmp = `cat $paramdir/$param`;
  60320. + chomp $tmp;
  60321. + return $tmp;
  60322. +}
  60323. +
  60324. +#
  60325. +#
  60326. +sub test_main {
  60327. +
  60328. + print "\nTesting Module Parameters\n";
  60329. +
  60330. + load_module("") or die;
  60331. +
  60332. + # Test initial values
  60333. + print "\nTesting Default Values\n";
  60334. + foreach (@{$params}) {
  60335. + test ($_->{NAME}, $_->{DEFAULT});
  60336. + }
  60337. +
  60338. + # Test low value
  60339. + print "\nTesting Low Value\n";
  60340. + my $cmd_params = "";
  60341. + foreach (@{$params}) {
  60342. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  60343. + }
  60344. + load_module($cmd_params) or die;
  60345. +
  60346. + foreach (@{$params}) {
  60347. + test ($_->{NAME}, $_->{LOW});
  60348. + }
  60349. +
  60350. + # Test high value
  60351. + print "\nTesting High Value\n";
  60352. + $cmd_params = "";
  60353. + foreach (@{$params}) {
  60354. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  60355. + }
  60356. + load_module($cmd_params) or die;
  60357. +
  60358. + foreach (@{$params}) {
  60359. + test ($_->{NAME}, $_->{HIGH});
  60360. + }
  60361. +
  60362. + # Test Enum
  60363. + print "\nTesting Enumerated\n";
  60364. + foreach (@{$params}) {
  60365. + if (defined $_->{ENUM}) {
  60366. + my $value;
  60367. + foreach $value (@{$_->{ENUM}}) {
  60368. + $cmd_params = "$_->{NAME}=$value";
  60369. + load_module($cmd_params) or die;
  60370. + test ($_->{NAME}, $value);
  60371. + }
  60372. + }
  60373. + }
  60374. +
  60375. + # Test Invalid Values
  60376. + print "\nTesting Invalid Values\n";
  60377. + $cmd_params = "";
  60378. + foreach (@{$params}) {
  60379. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  60380. + }
  60381. + load_module($cmd_params) or die;
  60382. +
  60383. + foreach (@{$params}) {
  60384. + test ($_->{NAME}, $_->{DEFAULT});
  60385. + }
  60386. +
  60387. + $cmd_params = "";
  60388. + foreach (@{$params}) {
  60389. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  60390. + }
  60391. + load_module($cmd_params) or die;
  60392. +
  60393. + foreach (@{$params}) {
  60394. + test ($_->{NAME}, $_->{DEFAULT});
  60395. + }
  60396. +
  60397. + print "\nTesting Enumerated\n";
  60398. + foreach (@{$params}) {
  60399. + if (defined $_->{ENUM}) {
  60400. + my $value;
  60401. + foreach $value (@{$_->{ENUM}}) {
  60402. + $value = $value + 1;
  60403. + $cmd_params = "$_->{NAME}=$value";
  60404. + load_module($cmd_params) or die;
  60405. + test ($_->{NAME}, $_->{DEFAULT});
  60406. + $value = $value - 2;
  60407. + $cmd_params = "$_->{NAME}=$value";
  60408. + load_module($cmd_params) or die;
  60409. + test ($_->{NAME}, $_->{DEFAULT});
  60410. + }
  60411. + }
  60412. + }
  60413. +
  60414. + test_status() or die;
  60415. +}
  60416. +
  60417. +test_main();
  60418. +0;
  60419. --- /dev/null
  60420. +++ b/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  60421. @@ -0,0 +1,193 @@
  60422. +#!/usr/bin/perl -w
  60423. +#
  60424. +# Run this program on the integrator
  60425. +# - Tests select sysfs attributes.
  60426. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  60427. +# -----------------------------------------------------------------------------
  60428. +use strict;
  60429. +use dwc_otg_test;
  60430. +
  60431. +check_arch() or die;
  60432. +
  60433. +#
  60434. +#
  60435. +sub test {
  60436. + my ($attr,$expected) = @_;
  60437. + my $string = get($attr);
  60438. +
  60439. + if ($string eq $expected) {
  60440. + printf("$attr = $string, okay\n");
  60441. + }
  60442. + else {
  60443. + warn "ERROR: value of $attr != $expected, $string\n";
  60444. + $errors ++;
  60445. + }
  60446. +}
  60447. +
  60448. +#
  60449. +#
  60450. +sub set {
  60451. + my ($reg, $value) = @_;
  60452. + system "echo $value > $sysfsdir/$reg";
  60453. +}
  60454. +
  60455. +#
  60456. +#
  60457. +sub get {
  60458. + my $attr = shift;
  60459. + my $string = `cat $sysfsdir/$attr`;
  60460. + chomp $string;
  60461. + if ($string =~ m/\s\=\s/) {
  60462. + my $tmp;
  60463. + ($tmp, $string) = split /\s=\s/, $string;
  60464. + }
  60465. + return $string;
  60466. +}
  60467. +
  60468. +#
  60469. +#
  60470. +sub test_main {
  60471. + print("\nTesting Sysfs Attributes\n");
  60472. +
  60473. + load_module("") or die;
  60474. +
  60475. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  60476. + print("\nTesting Default Values\n");
  60477. +
  60478. + test("regoffset", "0xffffffff");
  60479. + test("regvalue", "invalid offset");
  60480. + test("guid", "0x12345678"); # this will fail if it has been changed
  60481. + test("gsnpsid", "0x4f54200a");
  60482. +
  60483. + # Test operation of regoffset/regvalue
  60484. + print("\nTesting regoffset\n");
  60485. + set('regoffset', '5a5a5a5a');
  60486. + test("regoffset", "0xffffffff");
  60487. +
  60488. + set('regoffset', '0');
  60489. + test("regoffset", "0x00000000");
  60490. +
  60491. + set('regoffset', '40000');
  60492. + test("regoffset", "0x00000000");
  60493. +
  60494. + set('regoffset', '3ffff');
  60495. + test("regoffset", "0x0003ffff");
  60496. +
  60497. + set('regoffset', '1');
  60498. + test("regoffset", "0x00000001");
  60499. +
  60500. + print("\nTesting regvalue\n");
  60501. + set('regoffset', '3c');
  60502. + test("regvalue", "0x12345678");
  60503. + set('regvalue', '5a5a5a5a');
  60504. + test("regvalue", "0x5a5a5a5a");
  60505. + set('regvalue','a5a5a5a5');
  60506. + test("regvalue", "0xa5a5a5a5");
  60507. + set('guid','12345678');
  60508. +
  60509. + # Test HNP Capable
  60510. + print("\nTesting HNP Capable bit\n");
  60511. + set('hnpcapable', '1');
  60512. + test("hnpcapable", "0x1");
  60513. + set('hnpcapable','0');
  60514. + test("hnpcapable", "0x0");
  60515. +
  60516. + set('regoffset','0c');
  60517. +
  60518. + my $old = get('gusbcfg');
  60519. + print("setting hnpcapable\n");
  60520. + set('hnpcapable', '1');
  60521. + test("hnpcapable", "0x1");
  60522. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  60523. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  60524. +
  60525. + $old = get('gusbcfg');
  60526. + print("clearing hnpcapable\n");
  60527. + set('hnpcapable', '0');
  60528. + test("hnpcapable", "0x0");
  60529. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  60530. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  60531. +
  60532. + # Test SRP Capable
  60533. + print("\nTesting SRP Capable bit\n");
  60534. + set('srpcapable', '1');
  60535. + test("srpcapable", "0x1");
  60536. + set('srpcapable','0');
  60537. + test("srpcapable", "0x0");
  60538. +
  60539. + set('regoffset','0c');
  60540. +
  60541. + $old = get('gusbcfg');
  60542. + print("setting srpcapable\n");
  60543. + set('srpcapable', '1');
  60544. + test("srpcapable", "0x1");
  60545. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  60546. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  60547. +
  60548. + $old = get('gusbcfg');
  60549. + print("clearing srpcapable\n");
  60550. + set('srpcapable', '0');
  60551. + test("srpcapable", "0x0");
  60552. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  60553. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  60554. +
  60555. + # Test GGPIO
  60556. + print("\nTesting GGPIO\n");
  60557. + set('ggpio','5a5a5a5a');
  60558. + test('ggpio','0x5a5a0000');
  60559. + set('ggpio','a5a5a5a5');
  60560. + test('ggpio','0xa5a50000');
  60561. + set('ggpio','11110000');
  60562. + test('ggpio','0x11110000');
  60563. + set('ggpio','00001111');
  60564. + test('ggpio','0x00000000');
  60565. +
  60566. + # Test DEVSPEED
  60567. + print("\nTesting DEVSPEED\n");
  60568. + set('regoffset','800');
  60569. + $old = get('regvalue');
  60570. + set('devspeed','0');
  60571. + test('devspeed','0x0');
  60572. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  60573. + set('devspeed','1');
  60574. + test('devspeed','0x1');
  60575. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  60576. + set('devspeed','2');
  60577. + test('devspeed','0x2');
  60578. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  60579. + set('devspeed','3');
  60580. + test('devspeed','0x3');
  60581. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  60582. + set('devspeed','4');
  60583. + test('devspeed','0x0');
  60584. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  60585. + set('devspeed','5');
  60586. + test('devspeed','0x1');
  60587. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  60588. +
  60589. +
  60590. + # mode Returns the current mode:0 for device mode1 for host mode Read
  60591. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  60592. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  60593. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  60594. + # bussuspend Suspend the USB bus. Read/Write
  60595. + # busconnected Get the connection status of the bus Read
  60596. +
  60597. + # gotgctl Get or set the Core Control Status Register. Read/Write
  60598. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  60599. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  60600. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  60601. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  60602. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  60603. + ## guid Get or set the value of the User ID Register Read/Write
  60604. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  60605. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  60606. + # enumspeed Gets the device enumeration Speed. Read
  60607. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  60608. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  60609. +
  60610. + test_status("TEST NYI") or die;
  60611. +}
  60612. +
  60613. +test_main();
  60614. +0;